28std::uint64_t GetNewModelId()
30 static std::atomic<std::uint64_t> gLastModelId = 0;
31 return ++gLastModelId;
41 auto source = fieldMap.at(
target);
42 const bool hasCompatibleStructure =
43 (source->GetStructure() ==
target->GetStructure()) ||
45 if (!hasCompatibleStructure)
46 return R__FAIL(
"field mapping structural mismatch: " + source->GetFieldName() +
" --> " +
target->GetFieldName());
48 if (
target->GetTypeName() != source->GetTypeName())
49 return R__FAIL(
"field mapping type mismatch: " + source->GetFieldName() +
" --> " +
target->GetFieldName());
68 auto *sourceBreakPoint = fnBreakPoint(source);
70 return R__FAIL(
"unsupported field mapping (source structure)");
71 auto *targetBreakPoint = fnBreakPoint(
target);
73 return R__FAIL(
"unsupported field mapping (target structure)");
75 if (!sourceBreakPoint && !targetBreakPoint) {
79 if (sourceBreakPoint && targetBreakPoint) {
80 if (sourceBreakPoint == targetBreakPoint) {
84 if (
auto it = fieldMap.find(targetBreakPoint); it != fieldMap.end() && it->second == sourceBreakPoint) {
89 return R__FAIL(
"field mapping structure mismatch: " + source->GetFieldName() +
" --> " +
target->GetFieldName());
93 return R__FAIL(
"field mapping structure mismatch: " + source->GetFieldName() +
" --> " +
target->GetFieldName());
99 auto result = EnsureValidMapping(field.get(), fieldMap);
102 for (
const auto &
f : *field) {
103 result = EnsureValidMapping(&
f, fieldMap);
108 fFieldMap.insert(fieldMap.begin(), fieldMap.end());
116 if (
auto it = fFieldMap.find(
target); it != fFieldMap.end())
121std::unique_ptr<ROOT::Experimental::RNTupleModel::RProjectedFields>
124 auto cloneFieldZero = std::unique_ptr<RFieldZero>(
static_cast<RFieldZero *
>(
fFieldZero->Clone(
"").release()));
125 auto clone = std::unique_ptr<RProjectedFields>(
new RProjectedFields(std::move(cloneFieldZero)));
126 clone->fModel = newModel;
129 for (
const auto &[k,
v] : fFieldMap) {
130 for (
const auto &
f : *clone->GetFieldZero()) {
131 if (
f.GetQualifiedFieldName() == k->GetQualifiedFieldName()) {
132 clone->fFieldMap[&
f] = clone->fModel->
FindField(
v->GetQualifiedFieldName());
141 : fWriter(
writer), fOpenChangeset(fWriter.GetUpdatableModel())
147 fOpenChangeset.fModel.Unfreeze();
150 std::swap(fOpenChangeset.fModel.fModelId, fNewModelId);
155 fOpenChangeset.fModel.Freeze();
156 std::swap(fOpenChangeset.fModel.fModelId, fNewModelId);
157 if (fOpenChangeset.IsEmpty())
160 std::swap(fOpenChangeset.fAddedFields, toCommit.fAddedFields);
161 std::swap(fOpenChangeset.fAddedProjectedFields, toCommit.fAddedProjectedFields);
162 fWriter.GetSink().UpdateSchema(toCommit, fWriter.GetNEntries());
167 auto fieldp = field.get();
168 fOpenChangeset.fModel.AddField(std::move(field));
169 fOpenChangeset.fAddedFields.emplace_back(fieldp);
176 auto fieldp = field.get();
177 auto result = fOpenChangeset.fModel.AddProjectedField(std::move(field), mapping);
179 fOpenChangeset.fAddedProjectedFields.emplace_back(fieldp);
189 auto fieldNameStr = std::string(fieldName);
191 throw RException(
R__FAIL(
"field name '" + fieldNameStr +
"' already exists in NTuple model"));
212 return CreateBare(std::make_unique<RFieldZero>());
215std::unique_ptr<ROOT::Experimental::RNTupleModel>
218 auto model = std::unique_ptr<RNTupleModel>(
new RNTupleModel(std::move(fieldZero)));
219 model->fProjectedFields = std::make_unique<RProjectedFields>(model.get());
225 return Create(std::make_unique<RFieldZero>());
228std::unique_ptr<ROOT::Experimental::RNTupleModel>
231 auto model = CreateBare(std::move(fieldZero));
232 model->fDefaultEntry = std::unique_ptr<REntry>(
new REntry(model->fModelId, model->fSchemaId));
238 auto cloneModel = std::unique_ptr<RNTupleModel>(
239 new RNTupleModel(std::unique_ptr<RFieldZero>(
static_cast<RFieldZero *
>(fFieldZero->Clone(
"").release()))));
240 cloneModel->fModelId = GetNewModelId();
244 cloneModel->fSchemaId = fSchemaId;
246 cloneModel->fSchemaId = cloneModel->fModelId;
248 cloneModel->fIsFrozen = fIsFrozen;
249 cloneModel->fFieldNames = fFieldNames;
250 cloneModel->fDescription = fDescription;
251 cloneModel->fProjectedFields = fProjectedFields->Clone(cloneModel.get());
253 cloneModel->fDefaultEntry = std::unique_ptr<REntry>(
new REntry(cloneModel->fModelId, cloneModel->fSchemaId));
254 for (
const auto &
f : cloneModel->fFieldZero->GetSubFields()) {
255 cloneModel->fDefaultEntry->AddValue(
f->CreateValue());
263 if (fieldName.empty())
267 for (
auto subfieldName :
ROOT::Split(fieldName,
".")) {
269 auto it = std::find_if(subfields.begin(), subfields.end(),
270 [&](
const auto *
f) { return f->GetFieldName() == subfieldName; });
271 if (it != subfields.end()) {
287 EnsureValidFieldName(field->GetFieldName());
290 fDefaultEntry->AddValue(field->CreateValue());
291 fFieldNames.insert(field->GetFieldName());
292 fFieldZero->Attach(std::move(field));
301 auto fieldName = field->GetFieldName();
304 auto sourceField = FindField(mapping(fieldName));
306 return R__FAIL(
"no such field: " + mapping(fieldName));
307 fieldMap[field.get()] = sourceField;
308 for (
const auto &subField : *field) {
309 sourceField = FindField(mapping(subField.GetQualifiedFieldName()));
311 return R__FAIL(
"no such field: " + mapping(fieldName));
312 fieldMap[&subField] = sourceField;
315 EnsureValidFieldName(fieldName);
316 auto result = fProjectedFields->Add(std::move(field), fieldMap);
320 fFieldNames.insert(fieldName);
327 throw RException(
R__FAIL(
"invalid attempt to get mutable zero field of unfrozen model"));
333 auto f = FindField(fieldName);
343 return *fDefaultEntry;
349 throw RException(
R__FAIL(
"invalid attempt to get default entry of unfrozen model"));
351 return *fDefaultEntry;
359 auto entry = std::unique_ptr<REntry>(
new REntry(fModelId, fSchemaId));
360 for (
const auto &
f : fFieldZero->GetSubFields()) {
361 entry->AddValue(
f->CreateValue());
371 auto entry = std::unique_ptr<REntry>(
new REntry(fModelId, fSchemaId));
372 for (
const auto &
f : fFieldZero->GetSubFields()) {
373 entry->AddValue(
f->BindValue(
nullptr));
380 const auto &topLevelFields = fFieldZero->GetSubFields();
381 auto it = std::find_if(topLevelFields.begin(), topLevelFields.end(),
382 [&fieldName](
const RFieldBase *
f) { return f->GetFieldName() == fieldName; });
384 if (it == topLevelFields.end()) {
395 auto f = FindField(fieldName);
398 return f->CreateBulk();
406 fModelId = GetNewModelId();
407 fSchemaId = fModelId;
409 fDefaultEntry->fModelId = fModelId;
410 fDefaultEntry->fSchemaId = fSchemaId;
423 fDescription = std::string(description);
428 std::size_t
bytes = 0;
429 std::size_t minPageBufferSize = 0;
432 std::size_t nColumns = 0;
433 for (
auto &&field : *fFieldZero) {
434 for (
const auto &
r : field.GetColumnRepresentatives()) {
435 nColumns +=
r.size();
436 for (
auto columnType :
r) {
447 bytes += minPageBufferSize;
#define R__FORWARD_ERROR(res)
Short-hand to return an RResult<T> in an error state (i.e. after checking)
#define R__FORWARD_RESULT(res)
Short-hand to return an RResult<T> value from a subroutine to the calling stack frame.
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
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 r
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
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 bytes
static std::unique_ptr< RColumnElementBase > Generate(EColumnType type)
If CppT == void, use the default C++ type for the given column type.
An artificial field that transforms an RNTuple column that contains the offset of collections into co...
The field token identifies a top-level field in this entry.
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Base class for all ROOT issued exceptions.
Similar to RValue but manages an array of consecutive values.
A field translates read and write calls from/to underlying columns to/from tree values.
const RFieldBase * GetParent() const
std::vector< RFieldBase * > GetSubFields()
static RResult< void > EnsureValidFieldName(std::string_view fieldName)
Check whether a given string is a valid field name.
The container field for an ntuple model, which itself has no physical representation.
Projected fields are fields whose columns are reused from existing fields.
RResult< void > EnsureValidMapping(const 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::unordered_map< const RFieldBase *, const RFieldBase * > FieldMap_t
The map keys are the projected target fields, the map values are the backing source fields Note that ...
const RFieldBase * GetSourceField(const RFieldBase *target) const
RResult< void > Add(std::unique_ptr< RFieldBase > field, const FieldMap_t &fieldMap)
Adds a new projected field.
std::unique_ptr< RProjectedFields > Clone(const RNTupleModel *newModel) const
The new model needs to be a clone of fModel.
RUpdater(RNTupleWriter &writer)
void CommitUpdate()
Commit changes since the last call to BeginUpdate().
void BeginUpdate()
Begin a new set of alterations to the underlying model.
RResult< void > AddProjectedField(std::unique_ptr< RFieldBase > field, FieldMappingFunc_t mapping)
void AddField(std::unique_ptr< RFieldBase > field)
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.
std::uint64_t fModelId
Every model has a unique ID to distinguish it from other models.
std::function< std::string(const std::string &)> FieldMappingFunc_t
User provided function that describes the mapping of existing source fields to projected fields in te...
std::uint64_t fSchemaId
Models have a separate schema ID to remember that the clone of a frozen model still has the same sche...
REntry::RFieldToken GetToken(std::string_view fieldName) const
Creates a token to be used in REntry methods to address a top-level field.
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.
REntry & GetDefaultEntry()
std::size_t EstimateWriteMemoryUsage(const RNTupleWriteOptions &options=RNTupleWriteOptions()) const
Estimate the memory usage for this model during writing.
const RFieldBase & GetField(std::string_view fieldName) const
std::unique_ptr< REntry > CreateBareEntry() const
In a bare entry, all values point to nullptr.
std::unique_ptr< REntry > CreateEntry() const
RFieldBase::RBulk CreateBulk(std::string_view fieldName) const
Calls the given field's CreateBulk() method. Throws an exception if no field with the given name exis...
static std::unique_ptr< RNTupleModel > Create()
RResult< void > AddProjectedField(std::unique_ptr< RFieldBase > field, FieldMappingFunc_t mapping)
Adds a top-level field based on existing fields.
void SetDescription(std::string_view description)
RFieldBase * FindField(std::string_view fieldName) const
The field name can be a top-level field or a nested field. Returns nullptr if the field is not in the...
RNTupleModel(std::unique_ptr< RFieldZero > fieldZero)
static std::unique_ptr< RNTupleModel > CreateBare()
A bare model has no default entry.
void AddField(std::unique_ptr< RFieldBase > field)
Adds a field whose type is not known at compile time.
RFieldZero & GetFieldZero()
Non-const access to the root field is used to commit clusters during writing, and to make adjustments...
std::unique_ptr< RFieldZero > fFieldZero
Hierarchy of fields consisting of simple types and collections (sub trees)
Common user-tunable settings for storing ntuples.
int GetCompression() const
std::size_t GetPageBufferBudget() const
std::size_t GetApproxZippedClusterSize() const
std::size_t GetInitialNElementsPerPage() const
bool GetUseBufferedWrite() const
std::size_t GetMaxUnzippedPageSize() const
EImplicitMT GetUseImplicitMT() const
An RNTuple that gets filled with entries (data) and writes them to storage.
void Throw()
Throws an RException with fError.
The class is used as a return type for operations that can fail; wraps a value of type T or an RError...
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
The incremental changes to a RNTupleModel