Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleModel.cxx
Go to the documentation of this file.
1/// \file RNTupleModel.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-15
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#include <ROOT/RError.hxx>
17#include <ROOT/RField.hxx>
18#include <ROOT/RNTupleModel.hxx>
19#include <ROOT/RNTuple.hxx>
20#include <ROOT/StringUtils.hxx>
21
22#include <cstdlib>
23#include <memory>
24#include <utility>
25
26
28{
30 if (!nameValid) {
31 nameValid.Throw();
32 }
33 auto fieldNameStr = std::string(fieldName);
34 if (fFieldNames.insert(fieldNameStr).second == false) {
35 throw RException(R__FAIL("field name '" + fieldNameStr + "' already exists in NTuple model"));
36 }
37}
38
40 : fFieldZero(std::make_unique<RFieldZero>())
41 , fDefaultEntry(std::make_unique<REntry>())
42{}
43
44std::unique_ptr<ROOT::Experimental::RNTupleModel> ROOT::Experimental::RNTupleModel::Clone() const
45{
46 auto cloneModel = std::make_unique<RNTupleModel>();
47 auto cloneFieldZero = fFieldZero->Clone("");
48 cloneModel->fFieldZero = std::unique_ptr<RFieldZero>(static_cast<RFieldZero *>(cloneFieldZero.release()));
49 cloneModel->fDefaultEntry = cloneModel->fFieldZero->GenerateEntry();
50 cloneModel->fFieldNames = fFieldNames;
51 cloneModel->fDescription = fDescription;
52 return cloneModel;
53}
54
55
56void ROOT::Experimental::RNTupleModel::AddField(std::unique_ptr<Detail::RFieldBase> field)
57{
58 if (!field) {
59 throw RException(R__FAIL("null field"));
60 }
61 EnsureValidFieldName(field->GetName());
62 fDefaultEntry->AddValue(field->GenerateValue());
63 fFieldZero->Attach(std::move(field));
64}
65
66
67std::shared_ptr<ROOT::Experimental::RCollectionNTupleWriter> ROOT::Experimental::RNTupleModel::MakeCollection(
68 std::string_view fieldName, std::unique_ptr<RNTupleModel> collectionModel)
69{
70 EnsureValidFieldName(fieldName);
71 if (!collectionModel) {
72 throw RException(R__FAIL("null collectionModel"));
73 }
74 auto collectionNTuple = std::make_shared<RCollectionNTupleWriter>(std::move(collectionModel->fDefaultEntry));
75 auto field = std::make_unique<RCollectionField>(fieldName, collectionNTuple, std::move(collectionModel));
76 fDefaultEntry->CaptureValue(field->CaptureValue(collectionNTuple->GetOffsetPtr()));
77 fFieldZero->Attach(std::move(field));
78 return collectionNTuple;
79}
80
82{
83 if (fieldName.empty())
84 return nullptr;
85
86 auto *field = static_cast<ROOT::Experimental::Detail::RFieldBase *>(fFieldZero.get());
87 for (auto subfieldName : ROOT::Split(fieldName, ".")) {
88 const auto subfields = field->GetSubFields();
89 auto it =
90 std::find_if(subfields.begin(), subfields.end(), [&](const auto *f) { return f->GetName() == subfieldName; });
91 if (it != subfields.end()) {
92 field = *it;
93 } else {
94 field = nullptr;
95 break;
96 }
97 }
98
99 return field;
100}
101
102std::unique_ptr<ROOT::Experimental::REntry> ROOT::Experimental::RNTupleModel::CreateEntry()
103{
104 auto entry = std::make_unique<REntry>();
105 for (auto& f : *fFieldZero) {
106 if (f.GetParent() != GetFieldZero())
107 continue;
108 entry->AddValue(f.GenerateValue());
109 }
110 return entry;
111}
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:291
#define f(i)
Definition RSha256.hxx:104
static RResult< void > EnsureValidFieldName(std::string_view fieldName)
Check whether a given string is a valid field name.
Definition RField.cxx:232
std::vector< RFieldBase * > GetSubFields() const
Definition RField.cxx:291
void Throw()
Throws an RException with fError.
Definition RError.cxx:69
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:42
Base class for all ROOT issued exceptions.
Definition RError.hxx:114
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:271
std::unordered_set< std::string > fFieldNames
Keeps track of which field names are taken.
void EnsureValidFieldName(std::string_view fieldName)
Checks that user-provided field names are valid in the context of this NTuple model.
std::unique_ptr< RNTupleModel > Clone() const
std::unique_ptr< REntry > CreateEntry()
std::shared_ptr< RCollectionNTupleWriter > MakeCollection(std::string_view fieldName, std::unique_ptr< RNTupleModel > collectionModel)
Ingests a model for a sub collection and attaches it to the current model.
Detail::RFieldBase * GetField(std::string_view fieldName)
void AddField(std::unique_ptr< Detail::RFieldBase > field)
Adds a field whose type is not known at compile time.
The class is used as a return type for operations that can fail; wraps a value of type T or an RError...
Definition RError.hxx:195
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.