Logo ROOT  
Reference Guide
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 <atomic>
23#include <cstdlib>
24#include <memory>
25#include <utility>
26
29 const FieldMap_t &fieldMap)
30{
31 auto source = fieldMap.at(target);
32 const bool hasCompatibleStructure = (source->GetStructure() == target->GetStructure()) ||
33 ((source->GetStructure() == ENTupleStructure::kCollection) &&
34 (target->GetType() == "ROOT::Experimental::RNTupleCardinality"));
35 if (!hasCompatibleStructure)
36 return R__FAIL("field mapping structural mismatch: " + source->GetName() + " --> " + target->GetName());
37 if (source->GetStructure() == ENTupleStructure::kLeaf) {
38 if (target->GetType() != source->GetType())
39 return R__FAIL("field mapping type mismatch: " + source->GetName() + " --> " + target->GetName());
40 }
41
42 // We support projections only across records and collections. In the following, we check that the projected
43 // field is on the same path of collection fields in the field tree than the source field.
44
45 // Finds the first non-record parent field of the input field
46 auto fnBreakPoint = [](const Detail::RFieldBase *f) -> const Detail::RFieldBase * {
47 auto parent = f->GetParent();
48 while (parent) {
49 if (parent->GetStructure() != ENTupleStructure::kRecord)
50 return parent;
51 parent = parent->GetParent();
52 }
53 // We reached the zero field
54 return nullptr;
55 };
56
57 // If source or target has a variant or reference as a parent, error out
58 auto *sourceBreakPoint = fnBreakPoint(source);
59 if (sourceBreakPoint && sourceBreakPoint->GetStructure() != ENTupleStructure::kCollection)
60 return R__FAIL("unsupported field mapping (source structure)");
61 auto *targetBreakPoint = fnBreakPoint(target);
62 if (targetBreakPoint && sourceBreakPoint->GetStructure() != ENTupleStructure::kCollection)
63 return R__FAIL("unsupported field mapping (target structure)");
64
65 if (!sourceBreakPoint && !targetBreakPoint) {
66 // Source and target have no collections as parent
68 }
69 if (sourceBreakPoint && targetBreakPoint) {
70 if (sourceBreakPoint == targetBreakPoint) {
71 // Source and target are children of the same collection
73 }
74 if (auto it = fieldMap.find(targetBreakPoint); it != fieldMap.end() && it->second == sourceBreakPoint) {
75 // The parent collection of parent is mapped to the parent collection of the source
77 }
78 // Source and target are children of different collections
79 return R__FAIL("field mapping structure mismatch: " + source->GetName() + " --> " + target->GetName());
80 }
81
82 // Either source or target have no collection as a parent, but the other one has; that doesn't fit
83 return R__FAIL("field mapping structure mismatch: " + source->GetName() + " --> " + target->GetName());
84}
85
87ROOT::Experimental::RNTupleModel::RProjectedFields::Add(std::unique_ptr<Detail::RFieldBase> field,
88 const FieldMap_t &fieldMap)
89{
90 auto result = EnsureValidMapping(field.get(), fieldMap);
91 if (!result)
93 for (const auto &f : *field) {
94 result = EnsureValidMapping(&f, fieldMap);
95 if (!result)
97 }
98
99 fFieldMap.insert(fieldMap.begin(), fieldMap.end());
100 fFieldZero->Attach(std::move(field));
101 return RResult<void>::Success();
102}
103
106{
107 if (auto it = fFieldMap.find(target); it != fFieldMap.end())
108 return it->second;
109 return nullptr;
110}
111
112std::unique_ptr<ROOT::Experimental::RNTupleModel::RProjectedFields>
114{
115 auto cloneFieldZero = std::unique_ptr<RFieldZero>(static_cast<RFieldZero *>(fFieldZero->Clone("").release()));
116 auto clone = std::unique_ptr<RProjectedFields>(new RProjectedFields(std::move(cloneFieldZero)));
117 clone->fModel = newModel;
118 // TODO(jblomer): improve quadratic search to re-wire the field mappings given the new model and the cloned
119 // projected fields. Not too critical as we generally expect a limited number of projected fields
120 for (const auto &[k, v] : fFieldMap) {
121 for (const auto &f : *clone->GetFieldZero()) {
122 if (f.GetQualifiedFieldName() == k->GetQualifiedFieldName()) {
123 clone->fFieldMap[&f] = clone->fModel->GetField(v->GetQualifiedFieldName());
124 break;
125 }
126 }
127 }
128 return clone;
129}
130
132{
134 if (!nameValid) {
135 nameValid.Throw();
136 }
137 auto fieldNameStr = std::string(fieldName);
138 if (fFieldNames.insert(fieldNameStr).second == false) {
139 throw RException(R__FAIL("field name '" + fieldNameStr + "' already exists in NTuple model"));
140 }
141}
142
144{
145 if (IsFrozen())
146 throw RException(R__FAIL("invalid attempt to modify frozen model"));
147}
148
150{
151 if (!fDefaultEntry)
152 throw RException(R__FAIL("invalid attempt to use default entry of bare model"));
153}
154
156 : fFieldZero(std::make_unique<RFieldZero>())
157{}
158
159std::unique_ptr<ROOT::Experimental::RNTupleModel> ROOT::Experimental::RNTupleModel::CreateBare()
160{
161 auto model = std::unique_ptr<RNTupleModel>(new RNTupleModel());
162 model->fProjectedFields = std::make_unique<RProjectedFields>(model.get());
163 return model;
164}
165
166std::unique_ptr<ROOT::Experimental::RNTupleModel> ROOT::Experimental::RNTupleModel::Create()
167{
168 auto model = CreateBare();
169 model->fDefaultEntry = std::unique_ptr<REntry>(new REntry());
170 return model;
171}
172
173std::unique_ptr<ROOT::Experimental::RNTupleModel> ROOT::Experimental::RNTupleModel::Clone() const
174{
175 auto cloneModel = std::unique_ptr<RNTupleModel>(new RNTupleModel());
176 auto cloneFieldZero = fFieldZero->Clone("");
177 cloneModel->fModelId = fModelId;
178 cloneModel->fFieldZero = std::unique_ptr<RFieldZero>(static_cast<RFieldZero *>(cloneFieldZero.release()));
179 cloneModel->fFieldNames = fFieldNames;
180 cloneModel->fDescription = fDescription;
181 cloneModel->fProjectedFields = fProjectedFields->Clone(cloneModel.get());
182 if (fDefaultEntry) {
183 cloneModel->fDefaultEntry = std::unique_ptr<REntry>(new REntry(fModelId));
184 for (const auto &f : cloneModel->fFieldZero->GetSubFields()) {
185 cloneModel->fDefaultEntry->AddValue(f->GenerateValue());
186 }
187 }
188 return cloneModel;
189}
190
191
192void ROOT::Experimental::RNTupleModel::AddField(std::unique_ptr<Detail::RFieldBase> field)
193{
194 EnsureNotFrozen();
195 if (!field)
196 throw RException(R__FAIL("null field"));
197 EnsureValidFieldName(field->GetName());
198
199 if (fDefaultEntry)
200 fDefaultEntry->AddValue(field->GenerateValue());
201 fFieldZero->Attach(std::move(field));
202}
203
205ROOT::Experimental::RNTupleModel::AddProjectedField(std::unique_ptr<Detail::RFieldBase> field,
206 std::function<std::string(const std::string &)> mapping)
207{
208 EnsureNotFrozen();
209 if (!field)
210 return R__FAIL("null field");
211 auto fieldName = field->GetName();
212
214 auto sourceField = GetField(mapping(fieldName));
215 if (!sourceField)
216 return R__FAIL("no such field: " + mapping(fieldName));
217 fieldMap[field.get()] = sourceField;
218 for (const auto &subField : *field) {
219 sourceField = GetField(mapping(subField.GetQualifiedFieldName()));
220 if (!sourceField)
221 return R__FAIL("no such field: " + mapping(fieldName));
222 fieldMap[&subField] = sourceField;
223 }
224
225 EnsureValidFieldName(fieldName);
226 auto result = fProjectedFields->Add(std::move(field), fieldMap);
227 if (!result) {
228 fFieldNames.erase(fieldName);
229 return R__FORWARD_ERROR(result);
230 }
231 return RResult<void>::Success();
232}
233
234std::shared_ptr<ROOT::Experimental::RCollectionNTupleWriter> ROOT::Experimental::RNTupleModel::MakeCollection(
235 std::string_view fieldName, std::unique_ptr<RNTupleModel> collectionModel)
236{
237 EnsureNotFrozen();
238 EnsureValidFieldName(fieldName);
239 if (!collectionModel) {
240 throw RException(R__FAIL("null collectionModel"));
241 }
242 auto collectionNTuple = std::make_shared<RCollectionNTupleWriter>(std::move(collectionModel->fDefaultEntry));
243 auto field = std::make_unique<RCollectionField>(fieldName, collectionNTuple, std::move(collectionModel));
244 if (fDefaultEntry)
245 fDefaultEntry->CaptureValue(field->CaptureValue(collectionNTuple->GetOffsetPtr()));
246 fFieldZero->Attach(std::move(field));
247 return collectionNTuple;
248}
249
252{
253 if (fieldName.empty())
254 return nullptr;
255
256 auto *field = static_cast<ROOT::Experimental::Detail::RFieldBase *>(fFieldZero.get());
257 for (auto subfieldName : ROOT::Split(fieldName, ".")) {
258 const auto subfields = field->GetSubFields();
259 auto it =
260 std::find_if(subfields.begin(), subfields.end(), [&](const auto *f) { return f->GetName() == subfieldName; });
261 if (it != subfields.end()) {
262 field = *it;
263 } else {
264 field = nullptr;
265 break;
266 }
267 }
268
269 return field;
270}
271
273{
274 if (!IsFrozen())
275 throw RException(R__FAIL("invalid attempt to get default entry of unfrozen model"));
276 EnsureNotBare();
277 return fDefaultEntry.get();
278}
279
280std::unique_ptr<ROOT::Experimental::REntry> ROOT::Experimental::RNTupleModel::CreateEntry() const
281{
282 if (!IsFrozen())
283 throw RException(R__FAIL("invalid attempt to create entry of unfrozen model"));
284
285 auto entry = std::unique_ptr<REntry>(new REntry(fModelId));
286 for (const auto &f : fFieldZero->GetSubFields()) {
287 entry->AddValue(f->GenerateValue());
288 }
289 return entry;
290}
291
292std::unique_ptr<ROOT::Experimental::REntry> ROOT::Experimental::RNTupleModel::CreateBareEntry() const
293{
294 if (!IsFrozen())
295 throw RException(R__FAIL("invalid attempt to create entry of unfrozen model"));
296
297 auto entry = std::unique_ptr<REntry>(new REntry(fModelId));
298 for (const auto &f : fFieldZero->GetSubFields()) {
299 entry->CaptureValue(f->CaptureValue(nullptr));
300 }
301 return entry;
302}
303
305{
306 if (IsFrozen())
307 return;
308
309 static std::atomic<std::uint64_t> gLastModelId = 0;
310 fModelId = ++gLastModelId;
311 if (fDefaultEntry)
312 fDefaultEntry->fModelId = fModelId;
313}
314
316{
317 EnsureNotFrozen();
318 fDescription = std::string(description);
319}
#define R__FORWARD_ERROR(res)
Short-hand to return an RResult<T> in an error state (i.e. after checking)
Definition: RError.hxx:307
#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:303
#define f(i)
Definition: RSha256.hxx:104
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
@ kCollection
Definition: TStructNode.h:21
static RResult< void > EnsureValidFieldName(std::string_view fieldName)
Check whether a given string is a valid field name.
Definition: RField.cxx:332
std::vector< RFieldBase * > GetSubFields() const
Definition: RField.cxx:400
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:43
Base class for all ROOT issued exceptions.
Definition: RError.hxx:78
The container field for an ntuple model, which itself has no physical representation.
Definition: RField.hxx:372
Projected fields are fields whose columns are reused from existing fields.
std::unordered_map< const Detail::RFieldBase *, const Detail::RFieldBase * > FieldMap_t
The map keys are the projected target fields, the map values are the backing source fields Note that ...
RResult< void > Add(std::unique_ptr< Detail::RFieldBase > field, const FieldMap_t &fieldMap)
Adds a new projected field.
RResult< void > EnsureValidMapping(const Detail::RFieldBase *target, const FieldMap_t &fieldMap)
Asserts that the passed field is a valid target of the source field provided in the field map.
std::unique_ptr< RProjectedFields > Clone(const RNTupleModel *newModel) const
The new model needs to be a clone of fModel.
const Detail::RFieldBase * GetSourceField(const Detail::RFieldBase *target) const
The RNTupleModel encapulates the schema of an ntuple.
std::unordered_set< std::string > fFieldNames
Keeps track of which field names are taken, including projected field names.
void EnsureValidFieldName(std::string_view fieldName)
Checks that user-provided field names are valid in the context of this NTuple model.
void EnsureNotBare() const
Throws an RException if fDefaultEntry is nullptr.
std::unique_ptr< RNTupleModel > Clone() const
void EnsureNotFrozen() const
Throws an RException if fFrozen is true.
std::unique_ptr< REntry > CreateBareEntry() const
In a bare entry, all values point to nullptr.
std::unique_ptr< REntry > CreateEntry() const
static std::unique_ptr< RNTupleModel > Create()
void SetDescription(std::string_view description)
std::unique_ptr< REntry > fDefaultEntry
Contains field values corresponding to the created top-level fields.
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.
RResult< void > AddProjectedField(std::unique_ptr< Detail::RFieldBase > field, std::function< std::string(const std::string &)> mapping)
Adds a top-level field based on existing fields.
const Detail::RFieldBase * GetField(std::string_view fieldName) const
static std::unique_ptr< RNTupleModel > CreateBare()
A bare model has no default entry.
void AddField(std::unique_ptr< Detail::RFieldBase > field)
Adds a field whose type is not known at compile time.
std::unique_ptr< RFieldZero > fFieldZero
Hierarchy of fields consisting of simple types and collections (sub trees)
RResult<void> has no data member and no Inspect() method but instead a Success() factory method.
Definition: RError.hxx:269
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:207
basic_string_view< char > string_view
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:167
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
Definition: StringUtils.cxx:23