Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldMeta.cxx
Go to the documentation of this file.
1/// \file RFieldMeta.cxx
2/// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
3/// \date 2024-11-19
4
5// This file has concrete RField implementations that depend on ROOT Meta:
6// - RClassField
7// - RSoAField
8// - REnumField
9// - RPairField
10// - RTupleField
11// - RProxiedCollectionField
12// - RMapField
13// - RSetField
14// - RStreamerField
15// - RField<TObject>
16// - RVariantField
17
18#include <ROOT/BitUtils.hxx>
19#include <ROOT/RField.hxx>
20#include <ROOT/RFieldBase.hxx>
21#include <ROOT/RFieldUtils.hxx>
23#include <ROOT/RNTupleUtils.hxx>
24#include <ROOT/RSpan.hxx>
25
26#include <TBaseClass.h>
27#include <TBufferFile.h>
28#include <TClass.h>
29#include <TClassEdit.h>
30#include <TDataMember.h>
31#include <TEnum.h>
32#include <TObject.h>
33#include <TObjArray.h>
34#include <TObjString.h>
35#include <TRealData.h>
36#include <TSchemaRule.h>
37#include <TSchemaRuleSet.h>
38#include <TStreamerElement.h>
39#include <TVirtualObject.h>
41
42#include <algorithm>
43#include <array>
44#include <cstddef> // std::size_t
45#include <cstdint> // std::uint32_t et al.
46#include <cstring> // for memset
47#include <memory>
48#include <mutex>
49#include <string>
50#include <string_view>
51#include <unordered_set>
52#include <utility>
53#include <variant>
54
56
57namespace {
58
59TClass *EnsureValidClass(std::string_view className)
60{
61 auto cl = TClass::GetClass(std::string(className).c_str());
62 if (cl == nullptr) {
63 throw ROOT::RException(R__FAIL("RField: no I/O support for type " + std::string(className)));
64 }
65 return cl;
66}
67
68/// Common checks used both by RClassField and RSoAField
69void EnsureValidUserClass(TClass *cl, const ROOT::RFieldBase &field, std::string_view fieldType)
70{
71 if (cl->GetState() < TClass::kInterpreted) {
72 throw ROOT::RException(R__FAIL(std::string(fieldType) + " " + cl->GetName() +
73 " cannot be constructed from a class that's not at least Interpreted"));
74 }
75 // Avoid accidentally supporting std types through TClass.
76 if (cl->Property() & kIsDefinedInStd) {
77 throw ROOT::RException(R__FAIL(field.GetTypeName() + " is not supported"));
78 }
79 if (field.GetTypeName() == "TObject") {
80 throw ROOT::RException(R__FAIL("TObject is only supported through RField<TObject>"));
81 }
82 if (cl->GetCollectionProxy()) {
83 throw ROOT::RException(R__FAIL(field.GetTypeName() + " has an associated collection proxy; "
84 "use RProxiedCollectionField instead"));
85 }
86 // Classes with, e.g., custom streamers are not supported through this field. Empty classes, however, are.
87 // Can be overwritten with the "rntuple.streamerMode=true" class attribute
88 if (!cl->CanSplit() && cl->Size() > 1 &&
90 throw ROOT::RException(R__FAIL(field.GetTypeName() + " cannot be stored natively in RNTuple"));
91 }
94 throw ROOT::RException(
95 R__FAIL(field.GetTypeName() + " has streamer mode enforced, not supported as native RNTuple class"));
96 }
97 // Detect custom streamers set on individual members at runtime via
98 // TClass::SetMemberStreamer() or TClass::AdoptMemberStreamer().
99 // CanSplit() only checks for custom streamers set at compile time (fHasCustomStreamerMember),
100 // but runtime streamers are stored in TRealData and must be checked here.
101 if (!cl->GetListOfRealData()) {
102 cl->BuildRealData();
103 }
104 for (auto realMember : ROOT::Detail::TRangeStaticCast<TRealData>(*cl->GetListOfRealData())) {
105 if (realMember->GetStreamer()) {
106 throw ROOT::RException(R__FAIL(std::string(field.GetTypeName()) + " has member " + realMember->GetName() +
107 " with a custom streamer; not supported natively in RNTuple"));
108 }
109 }
110}
111
112TEnum *EnsureValidEnum(std::string_view enumName)
113{
114 auto e = TEnum::GetEnum(std::string(enumName).c_str());
115 if (e == nullptr) {
116 throw ROOT::RException(R__FAIL("RField: no I/O support for enum type " + std::string(enumName)));
117 }
118 return e;
119}
120
121void EnsureValidAlignment(std::size_t alignment)
122{
124 throw ROOT::RException(R__FAIL(std::string("invalid alignment: ") + std::to_string(alignment)));
125}
126
127/// Create a comma-separated list of type names from the given fields. Uses either the real type names or the
128/// type aliases (if there are any, otherwise the actual type name). Used to construct template argument lists
129/// for templated types such as std::pair<...>, std::tuple<...>, std::variant<...>.
130std::string GetTypeList(std::span<std::unique_ptr<ROOT::RFieldBase>> itemFields, bool useTypeAliases)
131{
132 std::string result;
133 for (size_t i = 0; i < itemFields.size(); ++i) {
134 if (useTypeAliases && !itemFields[i]->GetTypeAlias().empty()) {
135 result += itemFields[i]->GetTypeAlias();
136 } else {
137 result += itemFields[i]->GetTypeName();
138 }
139 result.push_back(',');
140 }
141 if (result.empty()) {
142 throw ROOT::RException(R__FAIL("invalid empty type list provided as template argument"));
143 }
144 result.pop_back(); // remove trailing comma
145 return result;
146}
147
148std::string BuildSetTypeName(ROOT::RSetField::ESetType setType, const ROOT::RFieldBase &innerField, bool useTypeAlias)
149{
150 if (innerField.GetTypeName().empty()) {
151 throw ROOT::RException(R__FAIL("invalid untyped inner field for outer set field"));
152 }
153 std::string typePrefix;
154 switch (setType) {
155 case ROOT::RSetField::ESetType::kSet: typePrefix = "std::set<"; break;
156 case ROOT::RSetField::ESetType::kUnorderedSet: typePrefix = "std::unordered_set<"; break;
157 case ROOT::RSetField::ESetType::kMultiSet: typePrefix = "std::multiset<"; break;
158 case ROOT::RSetField::ESetType::kUnorderedMultiSet: typePrefix = "std::unordered_multiset<"; break;
159 default: R__ASSERT(false);
160 }
161 return typePrefix +
162 ((useTypeAlias && !innerField.GetTypeAlias().empty()) ? innerField.GetTypeAlias()
163 : innerField.GetTypeName()) +
164 ">";
165}
166
167std::string BuildMapTypeName(ROOT::RMapField::EMapType mapType, const ROOT::RFieldBase *innerField, bool useTypeAliases)
168{
169 if (innerField->GetTypeName().empty()) {
170 throw ROOT::RException(R__FAIL("invalid untyped inner field for outer map field"));
171 }
172 if (const auto pairField = dynamic_cast<const ROOT::RPairField *>(innerField)) {
173 std::string typePrefix;
174 switch (mapType) {
175 case ROOT::RMapField::EMapType::kMap: typePrefix = "std::map<"; break;
176 case ROOT::RMapField::EMapType::kUnorderedMap: typePrefix = "std::unordered_map<"; break;
177 case ROOT::RMapField::EMapType::kMultiMap: typePrefix = "std::multimap<"; break;
178 case ROOT::RMapField::EMapType::kUnorderedMultiMap: typePrefix = "std::unordered_multimap<"; break;
179 default: R__ASSERT(false);
180 }
181 const auto &items = pairField->GetConstSubfields();
182 std::string type = typePrefix;
183 for (int i : {0, 1}) {
184 if (useTypeAliases && !items[i]->GetTypeAlias().empty()) {
185 type += items[i]->GetTypeAlias();
186 } else {
187 type += items[i]->GetTypeName();
188 }
189 if (i == 0)
190 type.push_back(',');
191 }
192 return type + ">";
193 }
194
195 throw ROOT::RException(R__FAIL("RMapField inner field type must be of RPairField"));
196}
197
198} // anonymous namespace
199
200ROOT::RRuleField::RRuleField(std::string_view fieldName, std::string_view typeName, TClass *classp,
201 ROOT::ENTupleStructure structure)
202 : RFieldBase(fieldName, typeName, structure, false /* isSimple */), fClass(classp)
203{
204}
205
207 ROOT::DescriptorId_t classFieldId) const
208{
209 auto idSourceMember = desc.FindFieldId(memberName, classFieldId);
210 if (idSourceMember != ROOT::kInvalidDescriptorId)
211 return idSourceMember;
212
213 for (const auto &subFieldDesc : desc.GetFieldIterable(classFieldId)) {
214 const auto &subFieldName = subFieldDesc.GetFieldName();
215 if (subFieldName.length() > 2 && subFieldName[0] == ':' && subFieldName[1] == '_') {
216 idSourceMember = LookupMember(desc, memberName, subFieldDesc.GetId());
217 if (idSourceMember != ROOT::kInvalidDescriptorId)
218 return idSourceMember;
219 }
220 }
221
223}
224
225void ROOT::RRuleField::SetStagingClass(const std::string &className, unsigned int classVersion)
226{
227 TClass::GetClass(className.c_str())->GetStreamerInfo(classVersion);
228 if (classVersion != GetTypeVersion() || className != GetTypeName()) {
229 fStagingClass = TClass::GetClass((className + std::string("@@") + std::to_string(classVersion)).c_str());
230 if (!fStagingClass) {
231 // For a rename rule, we may simply ask for the old class name
232 fStagingClass = TClass::GetClass(className.c_str());
233 }
234 } else {
235 fStagingClass = fClass;
236 }
237 R__ASSERT(fStagingClass);
238 R__ASSERT(static_cast<unsigned int>(fStagingClass->GetClassVersion()) == classVersion);
239}
240
241void ROOT::RRuleField::PrepareStagingArea(const std::vector<const TSchemaRule *> &rules,
242 const ROOT::RNTupleDescriptor &desc,
243 const ROOT::RFieldDescriptor &classFieldDesc)
244{
245 std::size_t stagingAreaSize = 0;
246 for (const auto rule : rules) {
247 for (auto source : TRangeDynCast<TSchemaRule::TSources>(rule->GetSource())) {
248 auto [itr, isNew] = fStagingItems.emplace(source->GetName(), RStagingItem());
249 if (!isNew) {
250 // This source member has already been processed by another rule (and we only support one type per member)
251 continue;
252 }
253 RStagingItem &stagingItem = itr->second;
254
255 const auto memberFieldId = LookupMember(desc, source->GetName(), classFieldDesc.GetId());
256 if (memberFieldId == kInvalidDescriptorId) {
257 throw RException(R__FAIL(std::string("cannot find on disk rule source member ") + GetTypeName() + "." +
258 source->GetName()));
259 }
260
261 auto memberType = source->GetTypeForDeclaration() + source->GetDimensions();
262 auto memberField = Create("" /* we don't need a field name */, std::string(memberType)).Unwrap();
263 memberField->SetOnDiskId(memberFieldId);
264 auto fieldZero = std::make_unique<RFieldZero>();
265 Internal::SetAllowFieldSubstitutions(*fieldZero, true);
266 fieldZero->Attach(std::move(memberField));
267 stagingItem.fField = std::move(fieldZero);
268
269 stagingItem.fOffset = fStagingClass->GetDataMemberOffset(source->GetName());
270 // Since we successfully looked up the source member in the RNTuple on-disk metadata, we expect it
271 // to be present in the TClass instance, too.
273 stagingAreaSize = std::max(stagingAreaSize, stagingItem.fOffset + stagingItem.fField->begin()->GetValueSize());
274 }
275 }
276
277 if (stagingAreaSize) {
278 R__ASSERT(static_cast<Int_t>(stagingAreaSize) <= fStagingClass->Size()); // we may have removed rules
279 // We use std::make_unique instead of MakeUninitArray to zero-initialize the staging area.
280 fStagingArea = std::make_unique<unsigned char[]>(stagingAreaSize);
281
282 for (const auto &[_, si] : fStagingItems) {
283 const auto &memberField = *si.fField->cbegin();
284 if (!(memberField.GetTraits() & kTraitTriviallyConstructible)) {
285 CallConstructValueOn(memberField, fStagingArea.get() + si.fOffset);
286 }
287 }
288 }
289}
290
291std::vector<const ROOT::TSchemaRule *> ROOT::RRuleField::FindRules(const ROOT::RFieldDescriptor *fieldDesc) const
292{
294
295 const auto ruleset = fClass->GetSchemaRules();
296 if (!ruleset)
297 return rules;
298
299 if (!fieldDesc) {
300 // If we have no on-disk information for the field, we still process the rules on the current in-memory version
301 // of the class
302 rules = ruleset->FindRules(fClass->GetName(), fClass->GetClassVersion(), fClass->GetCheckSum());
303 } else {
304 // We need to change (back) the name normalization from RNTuple to ROOT Meta
305 std::string normalizedName;
306 TClassEdit::GetNormalizedName(normalizedName, fieldDesc->GetTypeName());
307 // We do have an on-disk field that correspond to the current RClassField instance. Ask for rules matching the
308 // on-disk version of the field.
309 if (fieldDesc->GetTypeChecksum()) {
310 rules = ruleset->FindRules(normalizedName, fieldDesc->GetTypeVersion(), *fieldDesc->GetTypeChecksum());
311 } else {
312 rules = ruleset->FindRules(normalizedName, fieldDesc->GetTypeVersion());
313 }
314 }
315
316 // Cleanup and sort rules
317 // Check that any given source member uses the same type in all rules
318 std::unordered_map<std::string, std::string> sourceNameAndType;
319 std::size_t nskip = 0; // skip whole-object-rules that were moved to the end of the rules vector
320 for (auto itr = rules.begin(); itr != rules.end() - nskip;) {
321 const auto rule = *itr;
322
323 // Erase unknown rule types
324 if (rule->GetRuleType() != ROOT::TSchemaRule::kReadRule) {
326 << "ignoring I/O customization rule with unsupported type: " << rule->GetRuleType();
327 itr = rules.erase(itr);
328 continue;
329 }
330
331 bool hasConflictingSourceMembers = false;
332 for (auto source : TRangeDynCast<TSchemaRule::TSources>(rule->GetSource())) {
333 auto memberType = source->GetTypeForDeclaration() + source->GetDimensions();
334 auto [itrSrc, isNew] = sourceNameAndType.emplace(source->GetName(), memberType);
335 if (!isNew && (itrSrc->second != memberType)) {
337 << "ignoring I/O customization rule due to conflicting source member type: " << itrSrc->second << " vs. "
338 << memberType << " for member " << source->GetName();
339 hasConflictingSourceMembers = true;
340 break;
341 }
342 }
343 if (hasConflictingSourceMembers) {
344 itr = rules.erase(itr);
345 continue;
346 }
347
348 // Rules targeting the entire object need to be executed at the end
349 if (rule->GetTarget() == nullptr) {
350 nskip++;
351 if (itr != rules.end() - nskip)
352 std::iter_swap(itr++, rules.end() - nskip);
353 continue;
354 }
355
356 ++itr;
357 }
358
359 return rules;
360}
361
362void ROOT::RRuleField::AddReadCallbacksFromIORule(const TSchemaRule *rule, std::size_t subObjectOffset)
363{
364 auto func = rule->GetReadFunctionPointer();
365 if (func == nullptr) {
366 // Can happen for rename rules
367 return;
368 }
369 fReadCallbacks.emplace_back(
370 [func, subObjectOffset, stagingClass = fStagingClass, stagingArea = fStagingArea.get()](void *target) {
371 TVirtualObject onfileObj{nullptr};
372 onfileObj.fClass = stagingClass;
373 onfileObj.fObject = stagingArea;
374 func(static_cast<char *>(target) + subObjectOffset, &onfileObj);
375 onfileObj.fObject = nullptr; // TVirtualObject does not own the value
376 });
377}
378
379//------------------------------------------------------------------------------
380
381ROOT::RClassField::RClassField(std::string_view fieldName, const RClassField &source)
382 : ROOT::RRuleField(fieldName, source.GetTypeName(), source.fClass, ROOT::ENTupleStructure::kRecord),
383 fSubfieldsInfo(source.fSubfieldsInfo)
384{
385 for (const auto &f : source.GetConstSubfields()) {
386 RFieldBase::Attach(f->Clone(f->GetFieldName()));
387 }
388 fTraits = source.GetTraits();
389}
390
391ROOT::RClassField::RClassField(std::string_view fieldName, std::string_view className)
392 : RClassField(fieldName, EnsureValidClass(className))
393{
394}
395
396ROOT::RClassField::RClassField(std::string_view fieldName, TClass *classp)
397 : ROOT::RRuleField(fieldName, GetRenormalizedTypeName(classp->GetName()), classp, ROOT::ENTupleStructure::kRecord)
398{
399 EnsureValidUserClass(fClass, *this, "RClassField");
400
402 throw ROOT::RException(R__FAIL(GetTypeName() + " is a SoA field and cannot be used through RClassField"));
403 }
404
409
410 std::string renormalizedAlias;
411 if (Internal::NeedsMetaNameAsAlias(classp->GetName(), renormalizedAlias))
412 fTypeAlias = renormalizedAlias;
413
414 int i = 0;
415 const auto *bases = fClass->GetListOfBases();
416 assert(bases);
417 for (auto baseClass : ROOT::Detail::TRangeStaticCast<TBaseClass>(*bases)) {
418 if (baseClass->GetDelta() < 0) {
419 throw RException(R__FAIL(std::string("virtual inheritance is not supported: ") + GetTypeName() +
420 " virtually inherits from " + baseClass->GetName()));
421 }
422 TClass *c = baseClass->GetClassPointer();
423 auto subField =
424 RFieldBase::Create(std::string(kPrefixInherited) + "_" + std::to_string(i), c->GetName()).Unwrap();
425 fTraits &= subField->GetTraits();
426 Attach(std::move(subField), RSubfieldInfo{kBaseClass, static_cast<std::size_t>(baseClass->GetDelta())});
427 i++;
428 }
430 // Skip, for instance, unscoped enum constants defined in the class
431 if (dataMember->Property() & kIsStatic)
432 continue;
433 // Skip members explicitly marked as transient by user comment
434 if (!dataMember->IsPersistent()) {
435 // TODO(jblomer): we could do better
437 continue;
438 }
439
440 // NOTE: we use the already-resolved type name for the fields, otherwise TClass::GetClass may fail to resolve
441 // context-dependent types (e.g. typedefs defined in the class itself - which will not be fully qualified in
442 // the string returned by dataMember->GetFullTypeName())
443 std::string typeName{dataMember->GetTrueTypeName()};
444
445 // For C-style arrays, complete the type name with the size for each dimension, e.g. `int[4][2]`
446 if (dataMember->Property() & kIsArray) {
447 for (int dim = 0, n = dataMember->GetArrayDim(); dim < n; ++dim) {
448 typeName += "[" + std::to_string(dataMember->GetMaxIndex(dim)) + "]";
449 }
450 }
451
452 auto subField = RFieldBase::Create(dataMember->GetName(), typeName).Unwrap();
453
454 fTraits &= subField->GetTraits();
455 Attach(std::move(subField), RSubfieldInfo{kDataMember, static_cast<std::size_t>(dataMember->GetOffset())});
456 }
458}
459
461{
462 if (fStagingArea) {
463 for (const auto &[_, si] : fStagingItems) {
464 if (!(si.fField->GetTraits() & kTraitTriviallyDestructible)) {
465 auto deleter = GetDeleterOf(*si.fField);
466 deleter->operator()(fStagingArea.get() + si.fOffset, true /* dtorOnly */);
467 }
468 }
469 }
470}
471
472void ROOT::RClassField::Attach(std::unique_ptr<RFieldBase> child, RSubfieldInfo info)
473{
474 fSubfieldsInfo.push_back(info);
475 RFieldBase::Attach(std::move(child));
476}
477
478std::unique_ptr<ROOT::RFieldBase> ROOT::RClassField::CloneImpl(std::string_view newName) const
479{
480 return std::unique_ptr<RClassField>(new RClassField(newName, *this));
481}
482
483std::size_t ROOT::RClassField::AppendImpl(const void *from)
484{
485 std::size_t nbytes = 0;
486 for (unsigned i = 0; i < fSubfields.size(); i++) {
487 nbytes += CallAppendOn(*fSubfields[i], static_cast<const unsigned char *>(from) + fSubfieldsInfo[i].fOffset);
488 }
489 return nbytes;
490}
491
493{
494 for (const auto &[_, si] : fStagingItems) {
495 CallReadOn(*si.fField, globalIndex, fStagingArea.get() + si.fOffset);
496 }
497 for (unsigned i = 0; i < fSubfields.size(); i++) {
498 CallReadOn(*fSubfields[i], globalIndex, static_cast<unsigned char *>(to) + fSubfieldsInfo[i].fOffset);
499 }
500}
501
503{
504 for (const auto &[_, si] : fStagingItems) {
505 CallReadOn(*si.fField, localIndex, fStagingArea.get() + si.fOffset);
506 }
507 for (unsigned i = 0; i < fSubfields.size(); i++) {
508 CallReadOn(*fSubfields[i], localIndex, static_cast<unsigned char *>(to) + fSubfieldsInfo[i].fOffset);
509 }
510}
511
513{
514 std::vector<const TSchemaRule *> rules;
515 // On-disk members that are not targeted by an I/O rule; all other sub fields of the in-memory class
516 // will be marked as artificial (added member in a new class version or member set by rule).
517 std::unordered_set<std::string> regularSubfields;
518 // We generally don't support changing the number of base classes, with the exception of changing from/to zero
519 // base classes. The variable stores the number of on-disk base classes.
520 int nOnDiskBaseClasses = 0;
521
522 if (GetOnDiskId() == kInvalidDescriptorId) {
523 // This can happen for added base classes or added members of class type
524 rules = FindRules(nullptr);
525 if (!rules.empty())
526 SetStagingClass(GetTypeName(), GetTypeVersion());
527 } else {
528 const auto descriptorGuard = pageSource.GetSharedDescriptorGuard();
529 const ROOT::RNTupleDescriptor &desc = descriptorGuard.GetConstRef();
530 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
531
532 if (fieldDesc.GetStructure() == ENTupleStructure::kStreamer) {
533 // Streamer field on disk but meanwhile the type can be represented as a class field; replace this field
534 // by a streamer field to read the data from disk.
535 auto substitute = std::make_unique<RStreamerField>(GetFieldName(), GetTypeName());
536 substitute->SetOnDiskId(GetOnDiskId());
537 return substitute;
538 }
539
540 for (auto linkId : fieldDesc.GetLinkIds()) {
541 const auto &subFieldDesc = desc.GetFieldDescriptor(linkId);
542 regularSubfields.insert(subFieldDesc.GetFieldName());
543 if (!subFieldDesc.GetFieldName().empty() && subFieldDesc.GetFieldName()[0] == ':')
544 nOnDiskBaseClasses++;
545 }
546
547 rules = FindRules(&fieldDesc);
548
549 // If we found a rule, we know it is valid to read on-disk data because we found the rule according to the on-disk
550 // (source) type name and version/checksum.
551 if (rules.empty()) {
552 // Otherwise we require compatible type names, after renormalization. GetTypeName() is already renormalized,
553 // but RNTuple data written with ROOT v6.34 might not have renormalized the field type name. Ask the
554 // RNTupleDescriptor, which knows about the spec version, for a fixed up type name.
555 std::string descTypeName = desc.GetTypeNameForComparison(fieldDesc);
556 if (GetTypeName() != descTypeName) {
557 throw RException(R__FAIL("incompatible type name for field " + GetFieldName() + ": " + GetTypeName() +
558 " vs. " + descTypeName));
559 }
560 }
561
562 const bool hasSources = std::any_of(rules.begin(), rules.end(), [](const auto &r) {
563 return r->GetSource() && (r->GetSource()->GetEntries() > 0);
564 });
565
566 // A staging class (conversion streamer info) only exists if there is at least one rule that has an
567 // on disk source member defined.
568 if (hasSources) {
569 // For unversioned classes, the in-memory layout may by chance have the same transient version number
570 // than the recorded (transient, at the time of writing) on-disk version. Therefore, we also need to compare
571 // the checksums to find out if we need a conversion streamer info.
572 std::uint32_t assignedVersionForOnDiskLayout = fieldDesc.GetTypeVersion();
573 R__ASSERT(fieldDesc.GetTypeChecksum());
574 if (fieldDesc.GetTypeVersion() != GetTypeVersion() || *fieldDesc.GetTypeChecksum() != fClass->GetCheckSum() ||
575 fieldDesc.GetTypeName() != GetTypeName()) {
576 auto oldCl = TClass::GetClass(fieldDesc.GetTypeName().c_str());
577 R__ASSERT(oldCl);
578 auto onDiskStreamerInfo = oldCl->FindStreamerInfo(*fieldDesc.GetTypeChecksum());
579 R__ASSERT(onDiskStreamerInfo);
580 assignedVersionForOnDiskLayout = onDiskStreamerInfo->GetClassVersion();
581 }
582 SetStagingClass(fieldDesc.GetTypeName(), assignedVersionForOnDiskLayout);
583 PrepareStagingArea(rules, desc, fieldDesc);
584 for (auto &[_, si] : fStagingItems) {
585 Internal::CallConnectPageSourceOnField(*si.fField, pageSource);
586 si.fField = std::move(static_cast<RFieldZero *>(si.fField.get())->ReleaseSubfields()[0]);
587 }
588 }
589
590 // Remove target member of read rules from the list of regular members of the underlying on-disk field
591 for (const auto rule : rules) {
592 if (!rule->GetTarget())
593 continue;
594
595 for (const auto target : ROOT::Detail::TRangeStaticCast<const TObjString>(*rule->GetTarget())) {
596 regularSubfields.erase(std::string(target->GetString()));
597 }
598 }
599 }
600
601 for (const auto rule : rules) {
602 AddReadCallbacksFromIORule(rule);
603 }
604
605 // Iterate over all sub fields in memory and mark those as missing that are not in the descriptor.
606 int nInMemoryBaseClasses = 0;
607 for (auto &field : fSubfields) {
608 const auto &fieldName = field->GetFieldName();
609 if (regularSubfields.count(fieldName) == 0) {
610 CallSetArtificialOn(*field);
611 }
612 if (!fieldName.empty() && fieldName[0] == ':')
613 nInMemoryBaseClasses++;
614 }
615
616 if (nInMemoryBaseClasses != 0 && nOnDiskBaseClasses != 0 && nInMemoryBaseClasses != nOnDiskBaseClasses) {
617 throw RException(
618 R__FAIL(std::string("incompatible number of base classes for field ") + GetFieldName() + ": " + GetTypeName() +
619 ", " + std::to_string(nInMemoryBaseClasses) +
620 " base classes in memory "
621 " vs. " +
622 std::to_string(nOnDiskBaseClasses) + " base classes on-disk\n" +
624 }
625
626 return nullptr;
627}
628
630{
631 EnsureMatchingOnDiskField(desc, kDiffTypeVersion | kDiffTypeName).ThrowOnError();
632}
633
635{
636 fClass->New(where);
637}
638
640
641void ROOT::RClassField::RClassDeleter::operator()(void *objPtr, bool dtorOnly)
642{
643 fClass->Destructor(objPtr, true /* dtorOnly */);
644 RDeleter::operator()(objPtr, dtorOnly);
645}
646
647std::vector<ROOT::RFieldBase::RValue> ROOT::RClassField::SplitValue(const RValue &value) const
648{
649 std::vector<RValue> result;
650 auto valuePtr = value.GetPtr<void>();
651 auto charPtr = static_cast<unsigned char *>(valuePtr.get());
652 result.reserve(fSubfields.size());
653 for (unsigned i = 0; i < fSubfields.size(); i++) {
654 result.emplace_back(
655 fSubfields[i]->BindValue(std::shared_ptr<void>(valuePtr, charPtr + fSubfieldsInfo[i].fOffset)));
656 }
657 return result;
658}
659
661{
662 return fClass->GetClassSize();
663}
664
666{
667 const auto align = fClass->GetClassAlignment();
668 EnsureValidAlignment(align);
669 return align;
670}
671
673{
674 return fClass->GetClassVersion();
675}
676
678{
679 return fClass->GetCheckSum();
680}
681
682const std::type_info *ROOT::RClassField::GetPolymorphicTypeInfo() const
683{
684 bool polymorphic = fClass->ClassProperty() & kClassHasVirtual;
685 if (!polymorphic) {
686 return nullptr;
687 }
688 return fClass->GetTypeInfo();
689}
690
692{
693 visitor.VisitClassField(*this);
694}
695
696//------------------------------------------------------------------------------
697
698ROOT::RSoAField::RSoAField(std::string_view fieldName, const RSoAField &source)
699 : ROOT::RRuleField(fieldName, source.GetTypeName(), source.fClass, ROOT::ENTupleStructure::kCollection),
700 fSoAMemberOffsets(source.fSoAMemberOffsets)
701{
702 fTraits = source.GetTraits();
703 Attach(source.fSubfields[0]->Clone(source.fSubfields[0]->GetFieldName()));
704 fRecordMemberFields = fSubfields[0]->GetMutableSubfields();
706 for (const auto f : fRecordMemberFields)
707 fRecordMemberDeleters.emplace_back(GetDeleterOf(*f));
708 fLockSplitFields = std::make_unique<std::mutex>();
709}
710
711ROOT::RSoAField::RSoAField(std::string_view fieldName, std::string_view className)
712 : RSoAField(fieldName, EnsureValidClass(className))
713{
714}
715
717 const RSoAField &nestedSoA, std::size_t offsetInParent,
718 const std::function<RFieldBase *(const std::string &)> &fnRecordFieldFinder)
719{
720 const std::size_t nNestedRecordMemberFields = nestedSoA.fRecordMemberFields.size();
721
722 // The qualified field name of fields in nestedSoA->fRecordMemberFields will have a "<field name>._0."
723 // prefix because these fields are rooted in a collection named after the nested SoA field.
724 //
725 // E.g., in the following example:
726 //
727 // struct SoA_A { struct Record_A {
728 // SoA_B fB; Record_B fB;
729 // }; };
730 //
731 // struct SoA_B { struct Record_B {
732 // ROOT::RVec<float> fX; float fX;
733 // }; };
734 //
735 // The on-disk schema of SoA_A is "collection of Record_A", and the on-disk schema of SoA_B is
736 // "collection of Record_B".
737 // The qualified field name of fX in the subfield hiararchy of SoA_A is <field name>._0.fB.fX.
738 // The qualified field name of fX in the subfield hiararchy of SoA_B is <field name>._0.fX.
739 const auto lenPrefix = nestedSoA.GetFieldName().length() + strlen("._0.");
740
741 for (std::size_t i = 0; i < nNestedRecordMemberFields; ++i) {
742 const auto fieldNameForMatching =
743 nestedSoA.GetFieldName() + "." + nestedSoA.fRecordMemberFields[i]->GetQualifiedFieldName().substr(lenPrefix);
744
745 fRecordMemberFields.emplace_back(fnRecordFieldFinder(fieldNameForMatching));
746 fRecordMemberDeleters.emplace_back(GetDeleterOf(*nestedSoA.fRecordMemberFields[i]));
747 fSoAMemberOffsets.emplace_back(offsetInParent + nestedSoA.fSoAMemberOffsets[i]);
748 }
749}
750
752{
753 // Build a map of all subfields (nested) of the underlying record type. Map the fully qualified name of the
754 // subfields to their field pointer, so that we can later match the subfields of the SoA class to their corresponding
755 // fields in the underlying record type. Note that the members of the SoA class and the underlying record type
756 // can have different ordering. However, the base classes of the SoA class and the underlying record type must match
757 // in order.
758
759 std::vector<RFieldBase *> realRecordMemberFields; // Contains all subfields of the underlying record type
760 // Qualified field name --> index in realRecordMemberFields
761 std::unordered_map<std::string, std::size_t> recordFieldNameToIdx;
762
763 // Count the top-level subfields of the underlying record type for cross-check with the SoA type
764 unsigned int nDirectRecordSubfields = 0;
765 unsigned int nDirectRecordBases = 0;
766
767 for (auto itr = fSubfields[0]->begin(), iEnd = fSubfields[0]->end(); itr != iEnd; ++itr) {
768 if (itr->GetParent() == fSubfields[0].get()) {
769 if (itr->GetFieldName()[0] == ':') {
770 nDirectRecordBases++;
771 } else {
772 nDirectRecordSubfields++;
773 }
774 }
775
776 // Build the qualified field name for matching. We root the qualified field name at the underlying record type.
777 auto qualifiedName = itr->GetFieldName();
778 auto parent = itr->GetParent();
779 while (parent != fSubfields[0].get()) {
780 qualifiedName = parent->GetFieldName() + "." + qualifiedName;
781 parent = parent->GetParent();
782 }
783 recordFieldNameToIdx[qualifiedName] = realRecordMemberFields.size();
784
785 realRecordMemberFields.emplace_back(&(*itr));
786 }
787
788 // Base classes are treated as unrolled nested SoA classes
789 const auto *soaBases = fClass->GetListOfBases();
790 if (soaBases->GetSize() != static_cast<Int_t>(nDirectRecordBases)) {
791 throw RException(R__FAIL(std::string("number of base classes don't match between SoA class ") + GetFieldName() +
792 " and its underlying record type"));
793 }
794 unsigned int baseIdx = 0;
795 for (auto base : ROOT::Detail::TRangeStaticCast<TBaseClass>(*fClass->GetListOfBases())) {
796 if (base->GetDelta() < 0) {
797 throw RException(R__FAIL(std::string("virtual inheritance is not supported: ") + GetTypeName() +
798 " virtually inherits from " + base->GetName()));
799 }
800 TClass *cl = base->GetClassPointer();
801
802 const auto baseFieldName = std::string(":_") + std::to_string(baseIdx);
803
804 // SoA class `A` is allowed to inherit from a SoA class `B` whose underlying record type is `X` if and only if
805 // the underlying record type of `A` inherits from a type `X`.
806 const auto underlyingBaseTypeName = ROOT::Internal::GetRNTupleSoARecord(cl);
807 auto recordBaseField = realRecordMemberFields[recordFieldNameToIdx[baseFieldName]];
808 if (underlyingBaseTypeName != recordBaseField->GetTypeName()) {
809 throw RException(R__FAIL(std::string("inheritance of SoA class ") + GetFieldName() +
810 " does not match its underlying record type"));
811 }
812
813 std::unique_ptr<RSoAField> soaBaseField;
814 try {
815 soaBaseField = std::make_unique<RSoAField>(baseFieldName, cl->GetName());
816 } catch (const RException &e) {
817 throw RException(R__FAIL(std::string("invalid field type in base class: ") + cl->GetName() + " of SoA field " +
818 GetFieldName() + " (" + e.what() + ")"));
819 }
820
821 GraftNestedMemberFields(*soaBaseField, base->GetDelta(), [&](const std::string &name) {
822 return realRecordMemberFields[recordFieldNameToIdx[name]];
823 });
824
825 for (const RRule &r : soaBaseField->fRules) {
826 fRules.emplace_back(r);
827 fRules.back().fOffset += base->GetDelta();
828 }
829
830 baseIdx++;
831 }
832
833 unsigned int nMembers = 0;
834 for (auto dataMember : ROOT::Detail::TRangeStaticCast<TDataMember>(*fClass->GetListOfDataMembers())) {
835 // NOTE: ReconstructSplitFields() will also traverse the data members and need to apply the same rules for
836 // skipping members
837
838 if ((dataMember->Property() & kIsStatic) || !dataMember->IsPersistent())
839 continue;
840
841 if (dataMember->Property() & kIsArray) {
842 throw RException(R__FAIL(std::string("unsupported array type in SoA class: ") + dataMember->GetName()));
843 }
844
845 const std::string typeName{dataMember->GetTrueTypeName()};
846 auto dmField = RFieldBase::Create(dataMember->GetName(), typeName).Unwrap();
847
848 auto itr = recordFieldNameToIdx.find(dmField->GetFieldName());
849 if (itr == recordFieldNameToIdx.end()) {
850 throw RException(R__FAIL(std::string("unexpected SoA member: ") + dmField->GetFieldName()));
851 }
852 auto underlyingField = realRecordMemberFields[itr->second];
853 assert(dmField->GetFieldName() == underlyingField->GetFieldName());
854
855 if (auto soaField = dynamic_cast<RSoAField *>(dmField.get())) {
856 if (ROOT::Internal::GetRNTupleSoARecord(soaField->fClass) != underlyingField->GetTypeName()) {
857 throw RException(R__FAIL(std::string("nested SoA field ") + soaField->GetQualifiedFieldName() + " [" +
858 soaField->GetTypeName() + "] does not match underlying type " +
859 underlyingField->GetTypeName()));
860 }
861
862 GraftNestedMemberFields(*soaField, dataMember->GetOffset(), [&](const std::string &name) {
863 return realRecordMemberFields[recordFieldNameToIdx[name]];
864 });
865
866 for (const RRule &r : soaField->fRules) {
867 fRules.emplace_back(r);
868 fRules.back().fOffset += dataMember->GetOffset();
869 }
870 } else if (auto vecField = dynamic_cast<RRVecField *>(dmField.get())) {
871 if (vecField->begin()->GetTypeName() != underlyingField->GetTypeName() ||
872 vecField->begin()->GetTypeAlias() != underlyingField->GetTypeAlias()) {
873 const std::string leftType =
874 vecField->begin()->GetTypeName() +
875 (vecField->begin()->GetTypeAlias().empty() ? "" : " [" + vecField->begin()->GetTypeAlias() + "]");
876 const std::string rightType =
877 underlyingField->GetTypeName() +
878 (underlyingField->GetTypeAlias().empty() ? "" : " [" + underlyingField->GetTypeAlias() + "]");
879 throw RException(R__FAIL(std::string("SoA member type mismatch: ") + vecField->GetFieldName() + " (" +
880 leftType + " vs. " + rightType + ")"));
881 }
882
883 fRecordMemberFields.emplace_back(underlyingField);
884 fRecordMemberDeleters.emplace_back(GetDeleterOf(*underlyingField));
885 fSoAMemberOffsets.emplace_back(dataMember->GetOffset());
886 } else {
887 throw RException(R__FAIL("invalid field type in SoA class: " + dmField->GetTypeName()));
888 }
889
890 nMembers++;
891 }
892 if (nDirectRecordSubfields != nMembers) {
893 throw RException(R__FAIL("missing SoA members"));
894 }
895
896 auto schemaRules = FindRules(nullptr);
897 for (const auto &r : schemaRules)
898 fRules.emplace_back(RRule{r, 0});
899}
900
901ROOT::RSoAField::RSoAField(std::string_view fieldName, TClass *clSoA)
902 : ROOT::RRuleField(fieldName, GetRenormalizedTypeName(clSoA->GetName()), clSoA, ROOT::ENTupleStructure::kCollection)
903{
904 EnsureValidUserClass(fClass, *this, "RSoAField");
905 const auto recordTypeName = ROOT::Internal::GetRNTupleSoARecord(fClass);
906 if (recordTypeName.empty()) {
907 throw ROOT::RException(R__FAIL(std::string("class ") + GetTypeName() +
908 " is not marked with the rntupleSoARecord "
909 "dictionary option; cannot create corresponding RSoAField."));
910 }
911 try {
912 Attach(std::make_unique<ROOT::RClassField>("_0", recordTypeName));
913 } catch (ROOT::RException &e) {
914 throw RException(R__FAIL("invalid record type of SoA field " + GetTypeName() + " [" + e.what() + "]"));
915 }
916 if (fClass->GetClassVersion() <= 0) {
917 throw RException(R__FAIL(std::string("SoA class ") + GetTypeName() + " is transient or unversioned."));
918 }
919 if (static_cast<std::uint32_t>(fClass->GetClassVersion()) != fSubfields[0]->GetTypeVersion()) {
920 throw RException(R__FAIL(std::string("version mismatch between SoA type and underlying record type: ") +
921 std::to_string(fClass->GetClassVersion()) + " vs. " +
922 std::to_string(fSubfields[0]->GetTypeVersion())));
923 }
924
926
927 std::string renormalizedAlias;
928 if (ROOT::Internal::NeedsMetaNameAsAlias(fClass->GetName(), renormalizedAlias))
929 fTypeAlias = renormalizedAlias;
930
932 fLockSplitFields = std::make_unique<std::mutex>();
933}
934
935std::unique_ptr<ROOT::RFieldBase> ROOT::RSoAField::CloneImpl(std::string_view newName) const
936{
937 return std::unique_ptr<RSoAField>(new RSoAField(newName, *this));
938}
939
949
951{
952 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>();
953}
954
956{
957 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc);
958}
959
960std::size_t ROOT::RSoAField::AppendImpl(const void *from)
961{
962 const std::size_t nSoAMembers = fSoAMemberOffsets.size();
963
964 std::size_t N = 0; // Set by first SoA member and verified for the rest
965 for (std::size_t i = 0; i < nSoAMembers; ++i) {
966 const void *rvecPtr = static_cast<const unsigned char *>(from) + fSoAMemberOffsets[i];
967 auto [beginPtr, sizePtr, _] = ROOT::Internal::GetRVecDataMembers(rvecPtr);
968 assert(*sizePtr >= 0);
969 if (i == 0) {
970 N = *sizePtr;
971 } else {
972 if (static_cast<std::size_t>(*sizePtr) != N) {
973 const auto f = fRecordMemberFields[i];
974 throw RException(R__FAIL("SoA length mismatch for " + f->GetFieldName() + ": " + std::to_string(*sizePtr) +
975 " vs. " + std::to_string(N) + " (expected)"));
976 }
977 }
978 }
979
980 std::size_t nbytes = 0;
981 if (N > 0) {
982 for (std::size_t i = 0; i < nSoAMembers; ++i) {
983 const void *rvecPtr = static_cast<const unsigned char *>(from) + fSoAMemberOffsets[i];
984 auto [beginPtr, _, __] = ROOT::Internal::GetRVecDataMembers(rvecPtr);
985 RFieldBase *memberField = fRecordMemberFields[i];
986 if (memberField->IsSimple()) {
987 GetPrincipalColumnOf(*memberField)->AppendV(*beginPtr, N);
988 nbytes += N * GetPrincipalColumnOf(*memberField)->GetElement()->GetPackedSize();
989 } else {
990 for (std::size_t j = 0; j < N; ++j) {
991 nbytes += CallAppendOn(*memberField, *beginPtr + j * memberField->GetValueSize());
992 }
993 }
994 }
995 }
996
997 fNWritten += N;
998 fPrincipalColumn->Append(&fNWritten);
999 return nbytes + fPrincipalColumn->GetElement()->GetPackedSize();
1000}
1001
1003{
1004 // Read collection info for this entry
1006 RNTupleLocalIndex collectionStart;
1007 fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &N);
1008
1009 const auto nSoAMembers = fSoAMemberOffsets.size();
1010 for (std::size_t i = 0; i < nSoAMembers; ++i) {
1011 RFieldBase *memberField = fRecordMemberFields[i];
1012 const auto memberSize = memberField->GetValueSize();
1013 void *rvecPtr = static_cast<unsigned char *>(to) + fSoAMemberOffsets[i];
1014 auto begin = ROOT::RRVecField::ResizeRVec(rvecPtr, N, memberSize, memberField, fRecordMemberDeleters[i].get());
1015
1016 if (N == 0)
1017 continue;
1018
1019 if (memberField->IsSimple()) {
1020 GetPrincipalColumnOf(*memberField)->ReadV(collectionStart, N, begin);
1021 } else {
1022 if (memberField->IsArtificial()) {
1023 // Other artificial fields simply don't read at all. This does not work here because then
1024 // the vector elements of trivial types would be left uninitialized (complex types explicitly call
1025 // the constructor on vector resize). Thus we explicitly default-initialize trivial types.
1026 // Note that this causes a subtle difference in behavior: if the added member is default-initialized to
1027 // a non-zero value, this will be forgotten in the SoA layout.
1028 if (memberField->GetTraits() & kTraitTriviallyConstructible) {
1029 std::memset(begin, 0, N * memberSize);
1030 }
1031 } else {
1032 for (std::size_t j = 0; j < N; ++j) {
1033 CallReadOn(*memberField, collectionStart + j, begin + (j * memberSize));
1034 }
1035 }
1036 }
1037 }
1038}
1039
1040std::unique_ptr<ROOT::RFieldBase> ROOT::RSoAField::BeforeConnectPageSource(ROOT::Internal::RPageSource & /*pageSource*/)
1041{
1042 // Most of the heavy lifting is done by the actual subfields that map onto the on-disk AoS schema.
1043 // In the SoA field itself, we only need to take care of the in-memory part of the rule processing.
1044 // One complication is that for non-rename rules, we manually need to recurse into base (SoA) classes and
1045 // nested (SoA) classes because we don't have them in the field tree.
1046
1047 // For now, allow only rename rules and whole-object rules
1048
1049 const bool hasSources = std::any_of(fRules.begin(), fRules.end(), [](const auto &r) {
1050 return r.fRule->GetSource() && (r.fRule->GetSource()->GetEntries() > 0);
1051 });
1052 const bool hasTargets =
1053 std::any_of(fRules.begin(), fRules.end(), [](const auto &r) { return r.fRule->GetTarget(); });
1054
1055 if (hasSources) {
1056 throw RException(R__FAIL("I/O customization rules with sources are currently unsupported for SoA fields (" +
1057 GetTypeName() + ")"));
1058 }
1059
1060 if (hasTargets) {
1061 throw RException(R__FAIL("I/O customization rules with targets are currently unsupported for SoA fields (" +
1062 GetTypeName() + ")"));
1063 }
1064
1065 for (const auto &r : fRules) {
1066 AddReadCallbacksFromIORule(r.fRule, r.fOffset);
1067 }
1068
1069 return nullptr;
1070}
1071
1073{
1074 EnsureMatchingOnDiskField(desc, kDiffTypeName | kDiffTypeVersion).ThrowOnError();
1075}
1076
1077void ROOT::RSoAField::ConstructValue(void *where) const
1078{
1079 fClass->New(where);
1080}
1081
1082ROOT::RSoAField::RSoADeleter::RSoADeleter(TClass *cl) : RDeleter(cl->GetClassAlignment()), fSoAClass(cl) {}
1083
1084void ROOT::RSoAField::RSoADeleter::operator()(void *objPtr, bool dtorOnly)
1085{
1086 fSoAClass->Destructor(objPtr, true /* dtorOnly */);
1087 RDeleter::operator()(objPtr, dtorOnly);
1088}
1089
1091{
1092 std::lock_guard<std::mutex> lockGuard(*fLockSplitFields);
1093 if (fSplitFields)
1094 return;
1095
1096 fSplitFields = std::make_unique<std::vector<std::unique_ptr<ROOT::RFieldBase>>>();
1097 fSplitOffsets = std::make_unique<std::vector<std::size_t>>();
1098
1099 unsigned int baseIdx = 0;
1101 TClass *cl = base->GetClassPointer();
1102 auto baseField = RFieldBase::Create(std::string(":_" + std::to_string(baseIdx)), cl->GetName()).Unwrap();
1103 fSplitFields->emplace_back(std::move(baseField));
1104 fSplitOffsets->emplace_back(base->GetDelta());
1105 baseIdx++;
1106 }
1107
1109 if ((dataMember->Property() & kIsStatic) || !dataMember->IsPersistent())
1110 continue;
1111
1112 const std::string typeName{dataMember->GetTrueTypeName()};
1113 auto dmField = RFieldBase::Create(dataMember->GetName(), typeName).Unwrap();
1114 fSplitFields->emplace_back(std::move(dmField));
1115 fSplitOffsets->emplace_back(dataMember->GetOffset());
1116 }
1117}
1118
1119std::vector<ROOT::RFieldBase::RValue> ROOT::RSoAField::SplitValue(const RValue &value) const
1120{
1122 const auto nSplitFields = fSplitFields->size();
1123
1124 auto valuePtr = value.GetPtr<void>();
1125 auto soaPtr = static_cast<unsigned char *>(valuePtr.get());
1126 std::vector<RValue> values;
1127 values.reserve(nSplitFields);
1128 for (std::size_t i = 0; i < nSplitFields; ++i) {
1129 values.emplace_back((*fSplitFields)[i]->BindValue(std::shared_ptr<void>(valuePtr, soaPtr + (*fSplitOffsets)[i])));
1130 }
1131 return values;
1132}
1133
1135{
1136 return fClass->GetClassSize();
1137}
1138
1140{
1141 return fClass->GetClassVersion();
1142}
1143
1145{
1146 return fClass->GetCheckSum();
1147}
1148
1150{
1151 const auto align = fClass->GetClassAlignment();
1152 EnsureValidAlignment(align);
1153 return align;
1154}
1155
1156const std::type_info *ROOT::RSoAField::GetPolymorphicTypeInfo() const
1157{
1158 // TODO(jblomer): factor out
1159 bool polymorphic = fClass->ClassProperty() & kClassHasVirtual;
1160 if (!polymorphic) {
1161 return nullptr;
1162 }
1163 return fClass->GetTypeInfo();
1164}
1165
1167{
1168 visitor.VisitSoAField(*this);
1169}
1170
1171//------------------------------------------------------------------------------
1172
1173std::unique_ptr<ROOT::RFieldBase> ROOT::Internal::CreateEmulatedEnumField(std::string_view fieldName,
1174 std::string_view emulatedFromType,
1175 std::string_view underlyingIntType)
1176{
1177 return std::unique_ptr<RFieldBase>(new REnumField(fieldName, emulatedFromType, underlyingIntType));
1178}
1179
1180ROOT::REnumField::REnumField(std::string_view fieldName, std::string_view enumName)
1181 : REnumField(fieldName, EnsureValidEnum(enumName))
1182{
1183}
1184
1185ROOT::REnumField::REnumField(std::string_view fieldName, TEnum *enump)
1186 : ROOT::RFieldBase(fieldName, GetRenormalizedTypeName(enump->GetQualifiedName()), ROOT::ENTupleStructure::kPlain,
1187 false /* isSimple */)
1188{
1189 // Avoid accidentally supporting std types through TEnum.
1190 if (enump->Property() & kIsDefinedInStd) {
1191 throw RException(R__FAIL(GetTypeName() + " is not supported"));
1192 }
1193
1194 switch (enump->GetUnderlyingType()) {
1195 case kBool_t: Attach(std::make_unique<RField<Bool_t>>("_0")); break;
1196 case kChar_t: Attach(std::make_unique<RField<Char_t>>("_0")); break;
1197 case kUChar_t: Attach(std::make_unique<RField<UChar_t>>("_0")); break;
1198 case kShort_t: Attach(std::make_unique<RField<Short_t>>("_0")); break;
1199 case kUShort_t: Attach(std::make_unique<RField<UShort_t>>("_0")); break;
1200 case kInt_t: Attach(std::make_unique<RField<Int_t>>("_0")); break;
1201 case kUInt_t: Attach(std::make_unique<RField<UInt_t>>("_0")); break;
1202 case kLong_t: Attach(std::make_unique<RField<Long_t>>("_0")); break;
1203 case kLong64_t: Attach(std::make_unique<RField<Long64_t>>("_0")); break;
1204 case kULong_t: Attach(std::make_unique<RField<ULong_t>>("_0")); break;
1205 case kULong64_t: Attach(std::make_unique<RField<ULong64_t>>("_0")); break;
1206 default: throw RException(R__FAIL("Unsupported underlying integral type for enum type " + GetTypeName()));
1207 }
1208
1210}
1211
1212ROOT::REnumField::REnumField(std::string_view fieldName, std::string_view enumName,
1213 std::unique_ptr<RFieldBase> intField)
1214 : ROOT::RFieldBase(fieldName, enumName, ROOT::ENTupleStructure::kPlain, false /* isSimple */)
1215{
1216 Attach(std::move(intField));
1218}
1219
1220ROOT::REnumField::REnumField(std::string_view fieldName, std::string_view emulatedFromType,
1221 std::string_view underlyingIntType)
1222 : ROOT::RFieldBase(fieldName, emulatedFromType, ROOT::ENTupleStructure::kPlain, false /* isSimple */)
1223{
1224 auto intField = Create("_0", std::string(underlyingIntType)).Unwrap();
1225 Attach(std::move(intField));
1227}
1228
1229std::unique_ptr<ROOT::RFieldBase> ROOT::REnumField::CloneImpl(std::string_view newName) const
1230{
1231 auto newIntField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
1232 return std::unique_ptr<REnumField>(new REnumField(newName, GetTypeName(), std::move(newIntField)));
1233}
1234
1236{
1237 // TODO(jblomer): allow enum to enum conversion only by rename rule
1238 EnsureMatchingOnDiskField(desc, kDiffTypeName | kDiffTypeVersion).ThrowOnError();
1239}
1240
1241std::vector<ROOT::RFieldBase::RValue> ROOT::REnumField::SplitValue(const RValue &value) const
1242{
1243 std::vector<RValue> result;
1244 result.emplace_back(fSubfields[0]->BindValue(value.GetPtr<void>()));
1245 return result;
1246}
1247
1249{
1250 visitor.VisitEnumField(*this);
1251}
1252
1253//------------------------------------------------------------------------------
1254
1255ROOT::RPairField::RPairField(std::string_view fieldName, std::array<std::unique_ptr<RFieldBase>, 2> itemFields)
1256 : ROOT::RRecordField(fieldName, "std::pair<" + GetTypeList(itemFields, false /* useTypeAliases */) + ">")
1257{
1258 Internal::EnsureTypedChildField(*itemFields[0], *this);
1259 Internal::EnsureTypedChildField(*itemFields[1], *this);
1260 const std::string typeAlias = "std::pair<" + GetTypeList(itemFields, true /* useTypeAliases */) + ">";
1261 if (typeAlias != GetTypeName())
1262 fTypeAlias = typeAlias;
1263
1264 AttachItemFields(std::move(itemFields));
1265
1266 // ISO C++ does not guarantee any specific layout for `std::pair`; query TClass for the member offsets
1267 auto *c = TClass::GetClass(GetTypeName().c_str());
1268 if (!c) {
1270 fSubfields[0]->GetTypeName(), fSubfields[1]->GetTypeName(), true /* silent */, 0, 0);
1271 if (pairInfo)
1272 c = pairInfo->GetClass();
1273 }
1274 if (!c)
1275 throw RException(R__FAIL("cannot get type information for " + GetTypeName()));
1276 fSize = c->Size();
1277
1278 auto firstElem = c->GetRealData("first");
1279 if (!firstElem)
1280 throw RException(R__FAIL("first: no such member"));
1281 fOffsets.push_back(firstElem->GetThisOffset());
1282
1283 auto secondElem = c->GetRealData("second");
1284 if (!secondElem)
1285 throw RException(R__FAIL("second: no such member"));
1286 fOffsets.push_back(secondElem->GetThisOffset());
1287}
1288
1289std::unique_ptr<ROOT::RFieldBase> ROOT::RPairField::CloneImpl(std::string_view newName) const
1290{
1291 std::array<std::unique_ptr<RFieldBase>, 2> itemClones = {fSubfields[0]->Clone(fSubfields[0]->GetFieldName()),
1292 fSubfields[1]->Clone(fSubfields[1]->GetFieldName())};
1293 return std::unique_ptr<RPairField>(new RPairField(newName, std::move(itemClones)));
1294}
1295
1297{
1298 static const std::vector<std::string> prefixes = {"std::pair<", "std::tuple<"};
1299
1300 EnsureMatchingOnDiskField(desc, kDiffTypeName).ThrowOnError();
1301 EnsureMatchingTypePrefix(desc, prefixes).ThrowOnError();
1302
1303 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
1304 const auto nOnDiskSubfields = fieldDesc.GetLinkIds().size();
1305 if (nOnDiskSubfields != 2) {
1306 throw ROOT::RException(R__FAIL("invalid number of on-disk subfields for std::pair " +
1307 std::to_string(nOnDiskSubfields) + "\n" +
1308 Internal::GetTypeTraceReport(*this, desc)));
1309 }
1310}
1311
1312//------------------------------------------------------------------------------
1313
1316 bool readFromDisk)
1317{
1318 RIteratorFuncs ifuncs;
1319 ifuncs.fCreateIterators = proxy->GetFunctionCreateIterators(readFromDisk);
1320 ifuncs.fDeleteTwoIterators = proxy->GetFunctionDeleteTwoIterators(readFromDisk);
1321 ifuncs.fNext = proxy->GetFunctionNext(readFromDisk);
1322 R__ASSERT((ifuncs.fCreateIterators != nullptr) && (ifuncs.fDeleteTwoIterators != nullptr) &&
1323 (ifuncs.fNext != nullptr));
1324 return ifuncs;
1325}
1326
1328 : RFieldBase(fieldName, GetRenormalizedTypeName(classp->GetName()), ROOT::ENTupleStructure::kCollection,
1329 false /* isSimple */),
1330 fNWritten(0)
1331{
1332 if (!classp->GetCollectionProxy())
1333 throw RException(R__FAIL(std::string(classp->GetName()) + " has no associated collection proxy"));
1334 if (classp->Property() & kIsDefinedInStd) {
1335 static const std::vector<std::string> supportedStdTypes = {
1336 "std::set<", "std::unordered_set<", "std::multiset<", "std::unordered_multiset<",
1337 "std::map<", "std::unordered_map<", "std::multimap<", "std::unordered_multimap<"};
1338 bool isSupported = false;
1339 for (const auto &tn : supportedStdTypes) {
1340 if (GetTypeName().rfind(tn, 0) == 0) {
1341 isSupported = true;
1342 break;
1343 }
1344 }
1345 if (!isSupported)
1346 throw RException(R__FAIL(std::string(GetTypeName()) + " is not supported"));
1347 }
1348
1349 std::string renormalizedAlias;
1350 if (Internal::NeedsMetaNameAsAlias(classp->GetName(), renormalizedAlias))
1351 fTypeAlias = renormalizedAlias;
1352
1353 fProxy.reset(classp->GetCollectionProxy()->Generate());
1354 fProperties = fProxy->GetProperties();
1355 fCollectionType = fProxy->GetCollectionType();
1356 if (fProxy->HasPointers())
1357 throw RException(R__FAIL("collection proxies whose value type is a pointer are not supported"));
1358
1359 fIFuncsRead = RCollectionIterableOnce::GetIteratorFuncs(fProxy.get(), true /* readFromDisk */);
1360 fIFuncsWrite = RCollectionIterableOnce::GetIteratorFuncs(fProxy.get(), false /* readFromDisk */);
1361}
1362
1363ROOT::RProxiedCollectionField::RProxiedCollectionField(std::string_view fieldName, std::string_view typeName)
1364 : RProxiedCollectionField(fieldName, EnsureValidClass(typeName))
1365{
1366 // NOTE (fdegeus): std::map is supported, custom associative might be supported in the future if the need arises.
1368 throw RException(R__FAIL("custom associative collection proxies not supported"));
1369
1370 std::unique_ptr<ROOT::RFieldBase> itemField;
1371
1372 if (auto valueClass = fProxy->GetValueClass()) {
1373 // Element type is a class
1374 itemField = RFieldBase::Create("_0", valueClass->GetName()).Unwrap();
1375 } else {
1376 switch (fProxy->GetType()) {
1377 case EDataType::kChar_t: itemField = std::make_unique<RField<Char_t>>("_0"); break;
1378 case EDataType::kUChar_t: itemField = std::make_unique<RField<UChar_t>>("_0"); break;
1379 case EDataType::kShort_t: itemField = std::make_unique<RField<Short_t>>("_0"); break;
1380 case EDataType::kUShort_t: itemField = std::make_unique<RField<UShort_t>>("_0"); break;
1381 case EDataType::kInt_t: itemField = std::make_unique<RField<Int_t>>("_0"); break;
1382 case EDataType::kUInt_t: itemField = std::make_unique<RField<UInt_t>>("_0"); break;
1383 case EDataType::kLong_t: itemField = std::make_unique<RField<Long_t>>("_0"); break;
1384 case EDataType::kLong64_t: itemField = std::make_unique<RField<Long64_t>>("_0"); break;
1385 case EDataType::kULong_t: itemField = std::make_unique<RField<ULong_t>>("_0"); break;
1386 case EDataType::kULong64_t: itemField = std::make_unique<RField<ULong64_t>>("_0"); break;
1387 case EDataType::kFloat_t: itemField = std::make_unique<RField<Float_t>>("_0"); break;
1388 case EDataType::kDouble_t: itemField = std::make_unique<RField<Double_t>>("_0"); break;
1389 case EDataType::kBool_t: itemField = std::make_unique<RField<Bool_t>>("_0"); break;
1390 default: throw RException(R__FAIL("unsupported value type: " + std::to_string(fProxy->GetType())));
1391 }
1392 }
1393
1394 fItemSize = itemField->GetValueSize();
1395 Attach(std::move(itemField));
1396}
1397
1398std::unique_ptr<ROOT::RFieldBase> ROOT::RProxiedCollectionField::CloneImpl(std::string_view newName) const
1399{
1400 auto clone =
1401 std::unique_ptr<RProxiedCollectionField>(new RProxiedCollectionField(newName, fProxy->GetCollectionClass()));
1402 clone->fItemSize = fItemSize;
1403 clone->Attach(fSubfields[0]->Clone(fSubfields[0]->GetFieldName()));
1404 return clone;
1405}
1406
1408{
1409 std::size_t nbytes = 0;
1410 unsigned count = 0;
1411 TVirtualCollectionProxy::TPushPop RAII(fProxy.get(), const_cast<void *>(from));
1412 for (auto ptr : RCollectionIterableOnce{const_cast<void *>(from), fIFuncsWrite, fProxy.get(),
1413 (fCollectionType == kSTLvector ? fItemSize : 0U)}) {
1414 nbytes += CallAppendOn(*fSubfields[0], ptr);
1415 count++;
1416 }
1417
1418 fNWritten += count;
1419 fPrincipalColumn->Append(&fNWritten);
1420 return nbytes + fPrincipalColumn->GetElement()->GetPackedSize();
1421}
1422
1424{
1425 ROOT::NTupleSize_t nItems;
1426 RNTupleLocalIndex collectionStart;
1427 fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &nItems);
1428
1429 TVirtualCollectionProxy::TPushPop RAII(fProxy.get(), to);
1430 void *obj =
1431 fProxy->Allocate(static_cast<std::uint32_t>(nItems), (fProperties & TVirtualCollectionProxy::kNeedDelete));
1432
1433 unsigned i = 0;
1434 for (auto elementPtr : RCollectionIterableOnce{obj, fIFuncsRead, fProxy.get(),
1435 (fCollectionType == kSTLvector || obj != to ? fItemSize : 0U)}) {
1436 CallReadOn(*fSubfields[0], collectionStart + (i++), elementPtr);
1437 }
1438 if (obj != to)
1439 fProxy->Commit(obj);
1440}
1441
1451
1453{
1454 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>();
1455}
1456
1458{
1459 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc);
1460}
1461
1463{
1464 EnsureMatchingOnDiskCollection(desc).ThrowOnError();
1465}
1466
1468{
1469 fProxy->New(where);
1470}
1471
1472std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RProxiedCollectionField::GetDeleter() const
1473{
1474 if (fProperties & TVirtualCollectionProxy::kNeedDelete) {
1475 std::size_t itemSize = fCollectionType == kSTLvector ? fItemSize : 0U;
1476 return std::make_unique<RProxiedCollectionDeleter>(fProxy, GetDeleterOf(*fSubfields[0]), itemSize);
1477 }
1478 return std::make_unique<RProxiedCollectionDeleter>(fProxy);
1479}
1480
1482 std::shared_ptr<TVirtualCollectionProxy> proxy)
1483 : RDeleter(proxy->GetCollectionClass()->GetClassAlignment()), fProxy(std::move(proxy))
1484{
1485}
1486
1488 std::shared_ptr<TVirtualCollectionProxy> proxy, std::unique_ptr<RDeleter> itemDeleter, size_t itemSize)
1489 : RDeleter(proxy->GetCollectionClass()->GetClassAlignment()),
1490 fProxy(std::move(proxy)),
1491 fItemDeleter(std::move(itemDeleter)),
1492 fItemSize(itemSize)
1493{
1494 fIFuncsWrite = RCollectionIterableOnce::GetIteratorFuncs(fProxy.get(), false /* readFromDisk */);
1495}
1496
1498{
1499 if (fItemDeleter) {
1500 TVirtualCollectionProxy::TPushPop RAII(fProxy.get(), objPtr);
1501 for (auto ptr : RCollectionIterableOnce{objPtr, fIFuncsWrite, fProxy.get(), fItemSize}) {
1502 fItemDeleter->operator()(ptr, true /* dtorOnly */);
1503 }
1504 }
1505 fProxy->Destructor(objPtr, true /* dtorOnly */);
1506 RDeleter::operator()(objPtr, dtorOnly);
1507}
1508
1509std::vector<ROOT::RFieldBase::RValue> ROOT::RProxiedCollectionField::SplitValue(const RValue &value) const
1510{
1511 std::vector<RValue> result;
1512 auto valueRawPtr = value.GetPtr<void>().get();
1513 TVirtualCollectionProxy::TPushPop RAII(fProxy.get(), valueRawPtr);
1514 for (auto ptr : RCollectionIterableOnce{valueRawPtr, fIFuncsWrite, fProxy.get(),
1515 (fCollectionType == kSTLvector ? fItemSize : 0U)}) {
1516 result.emplace_back(fSubfields[0]->BindValue(std::shared_ptr<void>(value.GetPtr<void>(), ptr)));
1517 }
1518 return result;
1519}
1520
1522{
1523 return fProxy->Sizeof();
1524}
1525
1527{
1528 const auto align = fProxy->GetCollectionClass()->GetClassAlignment();
1529 EnsureValidAlignment(align);
1530 return align;
1531}
1532
1537
1538//------------------------------------------------------------------------------
1539
1540ROOT::RMapField::RMapField(std::string_view fieldName, EMapType mapType, std::unique_ptr<RFieldBase> itemField)
1541 : RProxiedCollectionField(fieldName,
1542 EnsureValidClass(BuildMapTypeName(mapType, itemField.get(), false /* useTypeAliases */))),
1543 fMapType(mapType)
1544{
1545 Internal::EnsureTypedChildField(*itemField, *this);
1546 if (!itemField->GetTypeAlias().empty())
1547 fTypeAlias = BuildMapTypeName(mapType, itemField.get(), true /* useTypeAliases */);
1548
1549 auto *itemClass = fProxy->GetValueClass();
1550 fItemSize = itemClass->GetClassSize();
1551
1552 Attach(std::move(itemField), "_0");
1553}
1554
1555std::unique_ptr<ROOT::RFieldBase> ROOT::RMapField::CloneImpl(std::string_view newName) const
1556{
1557 return std::make_unique<RMapField>(newName, fMapType, fSubfields[0]->Clone(fSubfields[0]->GetFieldName()));
1558}
1559
1561{
1562 static const std::vector<std::string> prefixesRegular = {"std::map<", "std::unordered_map<"};
1563
1564 EnsureMatchingOnDiskCollection(desc).ThrowOnError();
1565
1566 switch (fMapType) {
1567 case EMapType::kMap:
1568 case EMapType::kUnorderedMap: EnsureMatchingTypePrefix(desc, prefixesRegular).ThrowOnError(); break;
1569 default:
1570 break;
1571 // no restrictions for multimaps
1572 }
1573}
1574
1575//------------------------------------------------------------------------------
1576
1577ROOT::RSetField::RSetField(std::string_view fieldName, ESetType setType, std::unique_ptr<RFieldBase> itemField)
1578 : ROOT::RProxiedCollectionField(fieldName,
1579 EnsureValidClass(BuildSetTypeName(setType, *itemField, false /* useTypeAlias */))),
1580 fSetType(setType)
1581{
1582 if (!itemField->GetTypeAlias().empty())
1583 fTypeAlias = BuildSetTypeName(setType, *itemField, true /* useTypeAlias */);
1584
1585 fItemSize = itemField->GetValueSize();
1586
1587 Attach(std::move(itemField), "_0");
1588}
1589
1590std::unique_ptr<ROOT::RFieldBase> ROOT::RSetField::CloneImpl(std::string_view newName) const
1591{
1592 return std::make_unique<RSetField>(newName, fSetType, fSubfields[0]->Clone(fSubfields[0]->GetFieldName()));
1593}
1594
1596{
1597 static const std::vector<std::string> prefixesRegular = {"std::set<", "std::unordered_set<", "std::map<",
1598 "std::unordered_map<"};
1599
1600 EnsureMatchingOnDiskCollection(desc).ThrowOnError();
1601
1602 switch (fSetType) {
1603 case ESetType::kSet:
1604 case ESetType::kUnorderedSet: EnsureMatchingTypePrefix(desc, prefixesRegular).ThrowOnError(); break;
1605 default:
1606 break;
1607 // no restrictions for multisets
1608 }
1609}
1610
1611//------------------------------------------------------------------------------
1612
1613namespace {
1614
1615/// Used in RStreamerField::AppendImpl() in order to record the encountered streamer info records
1616class TBufferRecStreamer : public TBufferFile {
1617public:
1618 using RCallbackStreamerInfo = std::function<void(TVirtualStreamerInfo *)>;
1619
1620private:
1621 RCallbackStreamerInfo fCallbackStreamerInfo;
1622
1623public:
1624 TBufferRecStreamer(TBuffer::EMode mode, Int_t bufsize, RCallbackStreamerInfo callbackStreamerInfo)
1625 : TBufferFile(mode, bufsize), fCallbackStreamerInfo(std::move(callbackStreamerInfo))
1626 {
1627 }
1628 void TagStreamerInfo(TVirtualStreamerInfo *info) final { fCallbackStreamerInfo(info); }
1629};
1630
1631} // anonymous namespace
1632
1633ROOT::RStreamerField::RStreamerField(std::string_view fieldName, std::string_view className)
1634 : RStreamerField(fieldName, EnsureValidClass(className))
1635{
1636}
1637
1638ROOT::RStreamerField::RStreamerField(std::string_view fieldName, TClass *classp)
1639 : ROOT::RFieldBase(fieldName, GetRenormalizedTypeName(classp->GetName()), ROOT::ENTupleStructure::kStreamer,
1640 false /* isSimple */),
1641 fClass(classp),
1642 fIndex(0)
1643{
1645 throw RException(R__FAIL("streamer field " + GetFieldName() + " requires at least an emulated class"));
1648
1649 std::string renormalizedAlias;
1650 if (Internal::NeedsMetaNameAsAlias(classp->GetName(), renormalizedAlias))
1651 fTypeAlias = renormalizedAlias;
1652
1654 // For RClassField, we only check for explicit constructors and destructors and then recursively combine traits from
1655 // all member subfields. For RStreamerField, we treat the class as a black box and additionally need to check for
1656 // implicit constructors and destructors.
1661}
1662
1663std::unique_ptr<ROOT::RFieldBase> ROOT::RStreamerField::CloneImpl(std::string_view newName) const
1664{
1665 // To get the correct TClass instance in the clone, we clone using the un-normalized type name
1666 return std::unique_ptr<RStreamerField>(new RStreamerField(newName, fClass->GetName()));
1667}
1668
1669std::size_t ROOT::RStreamerField::AppendImpl(const void *from)
1670{
1671 TBufferRecStreamer buffer(TBuffer::kWrite, GetValueSize(),
1672 [this](TVirtualStreamerInfo *info) { fStreamerInfos[info->GetNumber()] = info; });
1673 fClass->Streamer(const_cast<void *>(from), buffer);
1674
1675 auto nbytes = buffer.Length();
1676 fAuxiliaryColumn->AppendV(buffer.Buffer(), buffer.Length());
1677 fIndex += nbytes;
1678 fPrincipalColumn->Append(&fIndex);
1679 return nbytes + fPrincipalColumn->GetElement()->GetPackedSize();
1680}
1681
1683{
1684 RNTupleLocalIndex collectionStart;
1685 ROOT::NTupleSize_t nbytes;
1686 fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &nbytes);
1687
1688 TBufferFile buffer(TBuffer::kRead, nbytes);
1689 fAuxiliaryColumn->ReadV(collectionStart, nbytes, buffer.Buffer());
1690 fClass->Streamer(to, buffer);
1691}
1692
1702
1704{
1705 GenerateColumnsImpl<ROOT::Internal::RColumnIndex, std::byte>();
1706}
1707
1709{
1710 GenerateColumnsImpl<ROOT::Internal::RColumnIndex, std::byte>(desc);
1711}
1712
1714{
1715 source.RegisterStreamerInfos();
1716 return nullptr;
1717}
1718
1720{
1721 EnsureMatchingOnDiskField(desc, kDiffTypeName | kDiffTypeVersion).ThrowOnError();
1722}
1723
1725{
1726 fClass->New(where);
1727}
1728
1733
1735{
1736 fClass->Destructor(objPtr, true /* dtorOnly */);
1737 RDeleter::operator()(objPtr, dtorOnly);
1738}
1739
1749
1751{
1752 const auto align = fClass->GetClassAlignment();
1753 EnsureValidAlignment(align);
1754 return align;
1755}
1756
1758{
1759 return fClass->GetClassSize();
1760}
1761
1763{
1764 return fClass->GetClassVersion();
1765}
1766
1768{
1769 return fClass->GetCheckSum();
1770}
1771
1773{
1774 visitor.VisitStreamerField(*this);
1775}
1776
1777//------------------------------------------------------------------------------
1778
1780{
1781 if (auto dataMember = TObject::Class()->GetDataMember(name)) {
1782 return dataMember->GetOffset();
1783 }
1784 throw RException(R__FAIL('\'' + std::string(name) + '\'' + " is an invalid data member"));
1785}
1786
1787ROOT::RField<TObject>::RField(std::string_view fieldName, const RField<TObject> &source)
1788 : ROOT::RFieldBase(fieldName, "TObject", ROOT::ENTupleStructure::kRecord, false /* isSimple */)
1789{
1791 Attach(source.GetConstSubfields()[0]->Clone("fUniqueID"));
1792 Attach(source.GetConstSubfields()[1]->Clone("fBits"));
1793}
1794
1795ROOT::RField<TObject>::RField(std::string_view fieldName)
1796 : ROOT::RFieldBase(fieldName, "TObject", ROOT::ENTupleStructure::kRecord, false /* isSimple */)
1797{
1798 assert(TObject::Class()->GetClassVersion() == 1);
1799
1801 Attach(std::make_unique<RField<UInt_t>>("fUniqueID"));
1802 Attach(std::make_unique<RField<UInt_t>>("fBits"));
1803}
1804
1805std::unique_ptr<ROOT::RFieldBase> ROOT::RField<TObject>::CloneImpl(std::string_view newName) const
1806{
1807 return std::unique_ptr<RField<TObject>>(new RField<TObject>(newName, *this));
1808}
1809
1810std::size_t ROOT::RField<TObject>::AppendImpl(const void *from)
1811{
1812 // Cf. TObject::Streamer()
1813
1814 auto *obj = static_cast<const TObject *>(from);
1815 if (obj->TestBit(TObject::kIsReferenced)) {
1816 throw RException(R__FAIL("RNTuple I/O on referenced TObject is unsupported"));
1817 }
1818
1819 std::size_t nbytes = 0;
1820 nbytes += CallAppendOn(*fSubfields[0], reinterpret_cast<const unsigned char *>(from) + GetOffsetUniqueID());
1821
1822 UInt_t bits = *reinterpret_cast<const UInt_t *>(reinterpret_cast<const unsigned char *>(from) + GetOffsetBits());
1823 bits &= (~TObject::kIsOnHeap & ~TObject::kNotDeleted);
1824 nbytes += CallAppendOn(*fSubfields[1], &bits);
1825
1826 return nbytes;
1827}
1828
1830{
1831 // Cf. TObject::Streamer()
1832
1833 auto *obj = static_cast<TObject *>(to);
1834 if (obj->TestBit(TObject::kIsReferenced)) {
1835 throw RException(R__FAIL("RNTuple I/O on referenced TObject is unsupported"));
1836 }
1837
1838 *reinterpret_cast<UInt_t *>(reinterpret_cast<unsigned char *>(to) + GetOffsetUniqueID()) = uniqueID;
1839
1840 const UInt_t bitIsOnHeap = obj->TestBit(TObject::kIsOnHeap) ? TObject::kIsOnHeap : 0;
1841 bits |= bitIsOnHeap | TObject::kNotDeleted;
1842 *reinterpret_cast<UInt_t *>(reinterpret_cast<unsigned char *>(to) + GetOffsetBits()) = bits;
1843}
1844
1846{
1847 UInt_t uniqueID, bits;
1848 CallReadOn(*fSubfields[0], globalIndex, &uniqueID);
1849 CallReadOn(*fSubfields[1], globalIndex, &bits);
1850 ReadTObject(to, uniqueID, bits);
1851}
1852
1854{
1855 UInt_t uniqueID, bits;
1856 CallReadOn(*fSubfields[0], localIndex, &uniqueID);
1857 CallReadOn(*fSubfields[1], localIndex, &bits);
1858 ReadTObject(to, uniqueID, bits);
1859}
1860
1862{
1863 return TObject::Class()->GetClassVersion();
1864}
1865
1867{
1868 return TObject::Class()->GetCheckSum();
1869}
1870
1872{
1873 new (where) TObject();
1874}
1875
1876std::vector<ROOT::RFieldBase::RValue> ROOT::RField<TObject>::SplitValue(const RValue &value) const
1877{
1878 std::vector<RValue> result;
1879 // Use GetPtr<TObject> to type-check
1880 std::shared_ptr<void> ptr = value.GetPtr<TObject>();
1881 auto charPtr = static_cast<unsigned char *>(ptr.get());
1882 result.emplace_back(fSubfields[0]->BindValue(std::shared_ptr<void>(ptr, charPtr + GetOffsetUniqueID())));
1883 result.emplace_back(fSubfields[1]->BindValue(std::shared_ptr<void>(ptr, charPtr + GetOffsetBits())));
1884 return result;
1885}
1886
1888{
1889 return sizeof(TObject);
1890}
1891
1893{
1894 return alignof(TObject);
1895}
1896
1898{
1899 visitor.VisitTObjectField(*this);
1900}
1901
1902//------------------------------------------------------------------------------
1903
1904ROOT::RTupleField::RTupleField(std::string_view fieldName, std::vector<std::unique_ptr<RFieldBase>> itemFields)
1905 : ROOT::RRecordField(fieldName, "std::tuple<" + GetTypeList(itemFields, false /* useTypeAliases */) + ">")
1906{
1907 for (const auto &f : itemFields) {
1909 }
1910 const std::string typeAlias = "std::tuple<" + GetTypeList(itemFields, true /* useTypeAliases */) + ">";
1911 if (typeAlias != GetTypeName())
1912 fTypeAlias = typeAlias;
1913
1914 AttachItemFields(std::move(itemFields));
1915
1916 auto *c = TClass::GetClass(GetTypeName().c_str());
1917 if (!c)
1918 throw RException(R__FAIL("cannot get type information for " + GetTypeName()));
1919 fSize = c->Size();
1920
1921 // ISO C++ does not guarantee neither specific layout nor member names for `std::tuple`. However, most
1922 // implementations including libstdc++ (gcc), libc++ (llvm), and MSVC name members as `_0`, `_1`, ..., `_N-1`,
1923 // following the order of the type list.
1924 // Use TClass to get their offsets; in case a particular `std::tuple` implementation does not define such
1925 // members, the assertion below will fail.
1926 for (unsigned i = 0; i < fSubfields.size(); ++i) {
1927 std::string memberName("_" + std::to_string(i));
1928 auto member = c->GetRealData(memberName.c_str());
1929 if (!member)
1930 throw RException(R__FAIL(memberName + ": no such member"));
1931 fOffsets.push_back(member->GetThisOffset());
1932 }
1933}
1934
1935std::unique_ptr<ROOT::RFieldBase> ROOT::RTupleField::CloneImpl(std::string_view newName) const
1936{
1937 std::vector<std::unique_ptr<RFieldBase>> itemClones;
1938 itemClones.reserve(fSubfields.size());
1939 for (const auto &f : fSubfields) {
1940 itemClones.emplace_back(f->Clone(f->GetFieldName()));
1941 }
1942 return std::unique_ptr<RTupleField>(new RTupleField(newName, std::move(itemClones)));
1943}
1944
1946{
1947 static const std::vector<std::string> prefixes = {"std::pair<", "std::tuple<"};
1948
1949 EnsureMatchingOnDiskField(desc, kDiffTypeName).ThrowOnError();
1950 EnsureMatchingTypePrefix(desc, prefixes).ThrowOnError();
1951
1952 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
1953 const auto nOnDiskSubfields = fieldDesc.GetLinkIds().size();
1954 const auto nSubfields = fSubfields.size();
1955 if (nOnDiskSubfields != nSubfields) {
1956 throw ROOT::RException(R__FAIL("invalid number of on-disk subfields for std::tuple " +
1957 std::to_string(nOnDiskSubfields) + " vs. " + std::to_string(nSubfields) + "\n" +
1958 Internal::GetTypeTraceReport(*this, desc)));
1959 }
1960}
1961
1962//------------------------------------------------------------------------------
1963
1964namespace {
1965
1966// Depending on the compiler, the variant tag is stored either in a trailing char or in a trailing unsigned int
1967constexpr std::size_t GetVariantTagSize()
1968{
1969 // Should be all zeros except for the tag, which is 1
1970 std::variant<char> t;
1971 constexpr auto sizeOfT = sizeof(t);
1972
1973 static_assert(sizeOfT == 2 || sizeOfT == 8, "unsupported std::variant layout");
1974 return sizeOfT == 2 ? 1 : 4;
1975}
1976
1977template <std::size_t VariantSizeT>
1978struct RVariantTag {
1979 using ValueType_t = typename std::conditional_t<VariantSizeT == 1, std::uint8_t,
1980 typename std::conditional_t<VariantSizeT == 4, std::uint32_t, void>>;
1981};
1982
1983} // anonymous namespace
1984
1986 : ROOT::RFieldBase(name, source.GetTypeName(), ROOT::ENTupleStructure::kVariant, false /* isSimple */),
1987 fMaxItemSize(source.fMaxItemSize),
1988 fMaxAlignment(source.fMaxAlignment),
1989 fTagOffset(source.fTagOffset),
1990 fVariantOffset(source.fVariantOffset),
1991 fNWritten(source.fNWritten.size(), 0)
1992{
1993 for (const auto &f : source.GetConstSubfields())
1994 Attach(f->Clone(f->GetFieldName()));
1995 fTraits = source.fTraits;
1996}
1997
1998ROOT::RVariantField::RVariantField(std::string_view fieldName, std::vector<std::unique_ptr<RFieldBase>> itemFields)
1999 : ROOT::RFieldBase(fieldName, "std::variant<" + GetTypeList(itemFields, false /* useTypeAliases */) + ">",
2000 ROOT::ENTupleStructure::kVariant, false /* isSimple */)
2001{
2002 for (const auto &f : itemFields) {
2004 }
2005 // The variant needs to initialize its own tag member
2006 fTraits |= kTraitTriviallyDestructible & ~kTraitTriviallyConstructible;
2007
2008 const std::string typeAlias = "std::variant<" + GetTypeList(itemFields, true /* useTypeAliases */) + ">";
2009 if (typeAlias != GetTypeName())
2010 fTypeAlias = typeAlias;
2011
2012 auto nFields = itemFields.size();
2013 if (nFields == 0 || nFields > kMaxVariants) {
2014 throw RException(R__FAIL("invalid number of variant fields (outside [1.." + std::to_string(kMaxVariants) + ")"));
2015 }
2016 fNWritten.resize(nFields, 0);
2017 for (unsigned int i = 0; i < nFields; ++i) {
2018 fMaxItemSize = std::max(fMaxItemSize, itemFields[i]->GetValueSize());
2019 fMaxAlignment = std::max(fMaxAlignment, itemFields[i]->GetAlignment());
2020 fTraits &= itemFields[i]->GetTraits();
2021 Attach(std::move(itemFields[i]), "_" + std::to_string(i));
2022 }
2023
2024 // With certain template parameters, the union of members of an std::variant starts at an offset > 0.
2025 // For instance, std::variant<std::optional<int>> on macOS.
2026 auto cl = TClass::GetClass(GetTypeName().c_str());
2027 assert(cl);
2028 auto dm = reinterpret_cast<TDataMember *>(cl->GetListOfDataMembers()->First());
2029 if (dm)
2030 fVariantOffset = dm->GetOffset();
2031
2032 const auto tagSize = GetVariantTagSize();
2033 const auto padding = tagSize - (fMaxItemSize % tagSize);
2034 fTagOffset = fVariantOffset + fMaxItemSize + ((padding == tagSize) ? 0 : padding);
2035}
2036
2037std::unique_ptr<ROOT::RFieldBase> ROOT::RVariantField::CloneImpl(std::string_view newName) const
2038{
2039 return std::unique_ptr<RVariantField>(new RVariantField(newName, *this));
2040}
2041
2042std::uint8_t ROOT::RVariantField::GetTag(const void *variantPtr, std::size_t tagOffset)
2043{
2044 using TagType_t = RVariantTag<GetVariantTagSize()>::ValueType_t;
2045 auto tag = *reinterpret_cast<const TagType_t *>(reinterpret_cast<const unsigned char *>(variantPtr) + tagOffset);
2046 return (tag == TagType_t(-1)) ? 0 : tag + 1;
2047}
2048
2049void ROOT::RVariantField::SetTag(void *variantPtr, std::size_t tagOffset, std::uint8_t tag)
2050{
2051 using TagType_t = RVariantTag<GetVariantTagSize()>::ValueType_t;
2052 auto tagPtr = reinterpret_cast<TagType_t *>(reinterpret_cast<unsigned char *>(variantPtr) + tagOffset);
2053 *tagPtr = (tag == 0) ? TagType_t(-1) : static_cast<TagType_t>(tag - 1);
2054}
2055
2056std::size_t ROOT::RVariantField::AppendImpl(const void *from)
2057{
2058 auto tag = GetTag(from, fTagOffset);
2059 std::size_t nbytes = 0;
2060 auto index = 0;
2061 if (tag > 0) {
2062 nbytes += CallAppendOn(*fSubfields[tag - 1], reinterpret_cast<const unsigned char *>(from) + fVariantOffset);
2063 index = fNWritten[tag - 1]++;
2064 }
2065 ROOT::Internal::RColumnSwitch varSwitch(index, tag);
2066 fPrincipalColumn->Append(&varSwitch);
2067 return nbytes + sizeof(ROOT::Internal::RColumnSwitch);
2068}
2069
2071{
2072 RNTupleLocalIndex variantIndex;
2073 std::uint32_t tag;
2074 fPrincipalColumn->GetSwitchInfo(globalIndex, &variantIndex, &tag);
2075 R__ASSERT(tag < 256);
2076
2077 // If `tag` equals 0, the variant is in the invalid state, i.e, it does not hold any of the valid alternatives in
2078 // the type list. This happens, e.g., if the field was late added; in this case, keep the invalid tag, which makes
2079 // any `std::holds_alternative<T>` check fail later.
2080 if (R__likely(tag > 0)) {
2081 void *varPtr = reinterpret_cast<unsigned char *>(to) + fVariantOffset;
2082 CallConstructValueOn(*fSubfields[tag - 1], varPtr);
2083 CallReadOn(*fSubfields[tag - 1], variantIndex, varPtr);
2084 }
2085 SetTag(to, fTagOffset, tag);
2086}
2087
2089{
2090 static RColumnRepresentations representations({{ENTupleColumnType::kSwitch}}, {});
2091 return representations;
2092}
2093
2095{
2096 GenerateColumnsImpl<ROOT::Internal::RColumnSwitch>();
2097}
2098
2100{
2101 GenerateColumnsImpl<ROOT::Internal::RColumnSwitch>(desc);
2102}
2103
2105{
2106 static const std::vector<std::string> prefixes = {"std::variant<"};
2107
2108 EnsureMatchingOnDiskField(desc, kDiffTypeName).ThrowOnError();
2109 EnsureMatchingTypePrefix(desc, prefixes).ThrowOnError();
2110
2111 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
2112 if (fSubfields.size() != fieldDesc.GetLinkIds().size()) {
2113 throw RException(R__FAIL("number of variants on-disk do not match for " + GetQualifiedFieldName() + "\n" +
2114 Internal::GetTypeTraceReport(*this, desc)));
2115 }
2116}
2117
2119{
2120 memset(where, 0, GetValueSize());
2121 CallConstructValueOn(*fSubfields[0], reinterpret_cast<unsigned char *>(where) + fVariantOffset);
2122 SetTag(where, fTagOffset, 1);
2123}
2124
2126{
2127 auto tag = GetTag(objPtr, fTagOffset);
2128 if (tag > 0) {
2129 fItemDeleters[tag - 1]->operator()(reinterpret_cast<unsigned char *>(objPtr) + fVariantOffset, true /*dtorOnly*/);
2130 }
2131 RDeleter::operator()(objPtr, dtorOnly);
2132}
2133
2134std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RVariantField::GetDeleter() const
2135{
2136 std::vector<std::unique_ptr<RDeleter>> itemDeleters;
2137 itemDeleters.reserve(fSubfields.size());
2138 for (const auto &f : fSubfields) {
2139 itemDeleters.emplace_back(GetDeleterOf(*f));
2140 }
2141 return std::make_unique<RVariantDeleter>(fTagOffset, fVariantOffset, GetAlignment(), std::move(itemDeleters));
2142}
2143
2145{
2146 return std::max(fMaxAlignment, alignof(RVariantTag<GetVariantTagSize()>::ValueType_t));
2147}
2148
2150{
2151 const auto alignment = GetAlignment();
2152 const auto actualSize = fTagOffset + GetVariantTagSize();
2153 const auto padding = alignment - (actualSize % alignment);
2154 return actualSize + ((padding == alignment) ? 0 : padding);
2155}
2156
2158{
2159 std::fill(fNWritten.begin(), fNWritten.end(), 0);
2160}
interop::TCppScope_t fClass
#define R__likely(expr)
Definition RConfig.hxx:569
#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:322
#define R__LOG_WARNING(...)
Definition RLogger.hxx:357
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define e(i)
Definition RSha256.hxx:103
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
@ kFloat_t
Definition TDataType.h:31
@ kULong64_t
Definition TDataType.h:32
@ kInt_t
Definition TDataType.h:30
@ kLong_t
Definition TDataType.h:30
@ kShort_t
Definition TDataType.h:29
@ kBool_t
Definition TDataType.h:32
@ kULong_t
Definition TDataType.h:30
@ kLong64_t
Definition TDataType.h:32
@ kUShort_t
Definition TDataType.h:29
@ kDouble_t
Definition TDataType.h:31
@ kChar_t
Definition TDataType.h:29
@ kUChar_t
Definition TDataType.h:29
@ kUInt_t
Definition TDataType.h:30
@ kClassHasExplicitCtor
@ kClassHasImplicitCtor
@ kClassHasVirtual
@ kClassHasExplicitDtor
@ kClassHasImplicitDtor
@ kIsArray
Definition TDictionary.h:79
@ kIsStatic
Definition TDictionary.h:80
@ kIsDefinedInStd
Definition TDictionary.h:98
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:139
#define N
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 index
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 child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char mode
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 Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
TCanvas * alignment()
Definition alignment.C:1
#define _(A, B)
Definition cfortran.h:112
Abstract base class for classes implementing the visitor design pattern.
virtual void VisitSoAField(const ROOT::RSoAField &field)
virtual void VisitProxiedCollectionField(const ROOT::RProxiedCollectionField &field)
virtual void VisitTObjectField(const ROOT::RField< TObject > &field)
virtual void VisitStreamerField(const ROOT::RStreamerField &field)
virtual void VisitEnumField(const ROOT::REnumField &field)
virtual void VisitClassField(const ROOT::RClassField &field)
Holds the index and the tag of a kSwitch column.
A helper class for piece-wise construction of an RExtraTypeInfoDescriptor.
RResult< RExtraTypeInfoDescriptor > MoveDescriptor()
RExtraTypeInfoDescriptorBuilder & ContentId(EExtraTypeInfoIds contentId)
RExtraTypeInfoDescriptorBuilder & TypeName(const std::string &typeName)
RExtraTypeInfoDescriptorBuilder & Content(const std::string &content)
RExtraTypeInfoDescriptorBuilder & TypeVersion(std::uint32_t typeVersion)
static std::string SerializeStreamerInfos(const StreamerInfoMap_t &infos)
const ROOT::RNTupleDescriptor & GetConstRef() const
Abstract interface to read data from an ntuple.
void RegisterStreamerInfos()
Builds the streamer info records from the descriptor's extra type info section.
RSharedDescriptorGuard GetSharedDescriptorGuard() const
Takes the read lock for the descriptor.
void operator()(void *objPtr, bool dtorOnly) final
The field for a class with dictionary.
Definition RField.hxx:184
std::unique_ptr< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::vector< RSubfieldInfo > fSubfieldsInfo
Additional information kept for each entry in fSubfields
Definition RField.hxx:205
void Attach(std::unique_ptr< RFieldBase > child, RSubfieldInfo info)
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final
std::uint32_t GetTypeVersion() const final
Indicates an evolution of the C++ type itself.
RClassField(std::string_view fieldName, const RClassField &source)
Used by CloneImpl.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
const std::type_info * GetPolymorphicTypeInfo() const
For polymorphic classes (that declare or inherit at least one virtual method), return the expected dy...
~RClassField() override
std::uint32_t GetTypeChecksum() const final
Return the current TClass reported checksum of this class. Only valid if kTraitTypeChecksum is set.
The field for an unscoped or scoped enum with dictionary.
Definition RField.hxx:305
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
REnumField(std::string_view fieldName, TEnum *enump)
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
Field specific extra type information from the header / extenstion header.
The list of column representations a field can have.
A functor to release the memory acquired by CreateValue() (memory and constructor).
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
A field translates read and write calls from/to underlying columns to/from tree values.
virtual size_t GetValueSize() const =0
What sizeof(T) for this type returns.
void Attach(std::unique_ptr< RFieldBase > child, std::string_view expectedChildName="")
Add a new subfield to the list of nested fields.
std::vector< std::unique_ptr< RFieldBase > > fSubfields
Collections and classes own subfields.
std::vector< const RFieldBase * > GetConstSubfields() const
static std::unique_ptr< RDeleter > GetDeleterOf(const RFieldBase &other)
@ kTraitEmulatedField
This field is a user defined type that was missing dictionaries and was reconstructed from the on-dis...
@ kTraitTriviallyDestructible
The type is cleaned up just by freeing its memory. I.e. the destructor performs a no-op.
@ kTraitTriviallyConstructible
No constructor needs to be called, i.e.
@ kTraitSoACollection
The field represents a collection in SoA layout.
@ kTraitTypeChecksum
The TClass checksum is set and valid.
const std::string & GetFieldName() const
const std::string & GetTypeAlias() const
std::uint32_t fTraits
Properties of the type that allow for optimizations of collections of that type.
bool IsArtificial() const
static RResult< std::unique_ptr< RFieldBase > > Create(const std::string &fieldName, const std::string &typeName, const ROOT::RCreateFieldOptions &options, const ROOT::RNTupleDescriptor *desc, ROOT::DescriptorId_t fieldId)
Factory method to resurrect a field from the stored on-disk type information.
std::string fTypeAlias
A typedef or using name that was used when creating the field.
bool IsSimple() const
std::uint32_t GetTraits() const
const std::string & GetTypeName() const
RValue BindValue(std::shared_ptr< void > objPtr)
Creates a value from a memory location with an already constructed object.
Metadata stored for every field of an RNTuple.
ROOT::DescriptorId_t GetId() const
const std::vector< ROOT::DescriptorId_t > & GetLinkIds() const
std::optional< std::uint32_t > GetTypeChecksum() const
std::uint32_t GetTypeVersion() const
const std::string & GetTypeName() const
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:58
std::vector< std::unique_ptr< RFieldBase > > ReleaseSubfields()
Moves all subfields into the returned vector.
Definition RField.cxx:64
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:342
RField(std::string_view name)
Definition RField.hxx:345
RMapField(std::string_view fieldName, EMapType mapType, std::unique_ptr< RFieldBase > itemField)
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
The on-storage metadata of an RNTuple.
RFieldDescriptorIterable GetFieldIterable(const RFieldDescriptor &fieldDesc) const
const RFieldDescriptor & GetFieldDescriptor(ROOT::DescriptorId_t fieldId) const
const std::string & GetName() const
std::string GetTypeNameForComparison(const RFieldDescriptor &fieldDesc) const
Adjust the type name of the passed RFieldDescriptor for comparison with another renormalized type nam...
ROOT::DescriptorId_t FindFieldId(std::string_view fieldName, ROOT::DescriptorId_t parentId) const
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
Template specializations for C++ std::pair.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
RPairField(std::string_view fieldName, std::array< std::unique_ptr< RFieldBase >, 2 > itemFields)
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
Allows for iterating over the elements of a proxied collection.
static RIteratorFuncs GetIteratorFuncs(TVirtualCollectionProxy *proxy, bool readFromDisk)
RProxiedCollectionDeleter(std::shared_ptr< TVirtualCollectionProxy > proxy)
void operator()(void *objPtr, bool dtorOnly) final
The field for a class representing a collection of elements via TVirtualCollectionProxy.
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const override
Called by Clone(), which additionally copies the on-disk ID.
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
RProxiedCollectionField(std::string_view fieldName, TClass *classp)
Constructor used when the value type of the collection is not known in advance, i....
RCollectionIterableOnce::RIteratorFuncs fIFuncsWrite
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
RCollectionIterableOnce::RIteratorFuncs fIFuncsRead
Two sets of functions to operate on iterators, to be used depending on the access type.
std::shared_ptr< TVirtualCollectionProxy > fProxy
The collection proxy is needed by the deleters and thus defined as a shared pointer.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
std::unique_ptr< RDeleter > GetDeleter() const final
void ReconcileOnDiskField(const RNTupleDescriptor &desc) override
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
Template specializations for ROOT's RVec.
static unsigned char * ResizeRVec(void *rvec, std::size_t nItems, std::size_t itemSize, const RFieldBase *itemField, RDeleter *itemDeleter)
The field for an untyped record.
void AttachItemFields(ContainerT &&itemFields)
std::vector< std::size_t > fOffsets
Base class for fields that are subject to I/O customization rules. Used by the class field and the So...
Definition RField.hxx:135
std::vector< const TSchemaRule * > FindRules(const ROOT::RFieldDescriptor *fieldDesc) const
Given the on-disk information from the page source, find all the I/O customization rules that apply t...
static constexpr const char * kPrefixInherited
Prefix used in the subfield names generated for base classes.
Definition RField.hxx:145
ROOT::DescriptorId_t LookupMember(const ROOT::RNTupleDescriptor &desc, std::string_view memberName, ROOT::DescriptorId_t classFieldId) const
Returns the id of member 'name' in the class field given by 'fieldId', or kInvalidDescriptorId if no ...
RRuleField(std::string_view name, std::string_view type, TClass *classp, ROOT::ENTupleStructure structure)
void PrepareStagingArea(const std::vector< const TSchemaRule * > &rules, const ROOT::RNTupleDescriptor &desc, const ROOT::RFieldDescriptor &classFieldId)
If there are rules with inputs (source members), create the staging area according to the TClass inst...
void AddReadCallbacksFromIORule(const TSchemaRule *rule, std::size_t subObjectOffset=0)
Register post-read callback corresponding to a ROOT I/O customization rules.
TClass * fClass
Set by derived class to the TClass instance representing the current in-memory layout.
Definition RField.hxx:148
void SetStagingClass(const std::string &className, unsigned int classVersion)
Sets fStagingClass according to the given name and version.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
RSetField(std::string_view fieldName, ESetType setType, std::unique_ptr< RFieldBase > itemField)
void operator()(void *objPtr, bool dtorOnly) final
The SoA field provides I/O for an in-memory SoA layout linked to an on-disk collection of the underly...
Definition RFieldSoA.hxx:54
std::unique_ptr< std::mutex > fLockSplitFields
protects the fSplitFields member.
Definition RFieldSoA.hxx:95
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
const std::type_info * GetPolymorphicTypeInfo() const
For polymorphic classes (that declare or inherit at least one virtual method), return the expected dy...
std::vector< RFieldBase * > fRecordMemberFields
Direct access to the member fields of the underlying record.
Definition RFieldSoA.hxx:75
size_t GetValueSize() const final
What sizeof(T) for this type returns.
std::vector< std::unique_ptr< RDeleter > > fRecordMemberDeleters
Definition RFieldSoA.hxx:79
std::unique_ptr< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
void CollectRecordMemberFields()
Called during construction, picks up the (nested) member fields of the underlying record type(s) and ...
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
void ReconstructSplitFields() const
std::vector< std::size_t > fSoAMemberOffsets
The offset of the RVec members in the SoA type in the same order as fRecordMemberFields.
Definition RFieldSoA.hxx:77
RSoAField(std::string_view fieldName, const RSoAField &source)
Used by CloneImpl.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
std::unique_ptr< std::vector< std::size_t > > fSplitOffsets
Definition RFieldSoA.hxx:94
std::uint32_t GetTypeVersion() const final
Indicates an evolution of the C++ type itself.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
size_t GetAlignment() const final
What alignof(T) for this type returns.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
std::unique_ptr< std::vector< std::unique_ptr< ROOT::RFieldBase > > > fSplitFields
For reading and writing, the RVecs of the SoA class do not have a dedicated field.
Definition RFieldSoA.hxx:93
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
std::uint32_t GetTypeChecksum() const final
Return the current TClass reported checksum of this class. Only valid if kTraitTypeChecksum is set.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
void GraftNestedMemberFields(const RSoAField &nestedSoA, std::size_t offsetInParent, const std::function< RFieldBase *(const std::string &)> &fnRecordFieldFinder)
For a nested SoA struct (either as a member of as a base class), use their fRecordMemberFields in thi...
void operator()(void *objPtr, bool dtorOnly) final
The field for a class using ROOT standard streaming.
Definition RField.hxx:245
ROOT::RExtraTypeInfoDescriptor GetExtraTypeInfo() const final
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
ROOT::Internal::RNTupleSerializer::StreamerInfoMap_t fStreamerInfos
streamer info records seen during writing
Definition RField.hxx:257
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::uint32_t GetTypeVersion() const final
Indicates an evolution of the C++ type itself.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::uint32_t GetTypeChecksum() const final
Return the current TClass reported checksum of this class. Only valid if kTraitTypeChecksum is set.
std::unique_ptr< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &source) final
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
RStreamerField(std::string_view fieldName, TClass *classp)
size_t GetAlignment() const final
What alignof(T) for this type returns.
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
size_t GetValueSize() const final
What sizeof(T) for this type returns.
Template specializations for C++ std::tuple.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
RTupleField(std::string_view fieldName, std::vector< std::unique_ptr< RFieldBase > > itemFields)
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
void operator()(void *objPtr, bool dtorOnly) final
Template specializations for C++ std::variant.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
static constexpr std::size_t kMaxVariants
std::vector< ROOT::Internal::RColumnIndex::ValueType > fNWritten
static std::uint8_t GetTag(const void *variantPtr, std::size_t tagOffset)
Extracts the index from an std::variant and transforms it into the 1-based index used for the switch ...
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
size_t fVariantOffset
In the std::variant memory layout, the actual union of types may start at an offset > 0.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
std::unique_ptr< RDeleter > GetDeleter() const final
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
size_t fTagOffset
In the std::variant memory layout, at which byte number is the index stored.
RVariantField(std::string_view name, const RVariantField &source)
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
static void SetTag(void *variantPtr, std::size_t tagOffset, std::uint8_t tag)
void CommitClusterImpl() final
ReadFuncPtr_t GetReadFunctionPointer() const
Get the pointer to the function to be run for the rule (if it is a read rule).
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition TBufferFile.h:47
@ kWrite
Definition TBuffer.h:73
@ kRead
Definition TBuffer.h:73
char * Buffer() const
Definition TBuffer.h:96
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
UInt_t GetCheckSum(ECheckSum code=kCurrentCheckSum) const
Call GetCheckSum with validity check.
Definition TClass.cxx:6693
Bool_t CanSplit() const
Return true if the data member of this TClass can be saved separately.
Definition TClass.cxx:2331
EState GetState() const
Definition TClass.h:504
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5529
size_t GetClassAlignment() const
Return the alignment requirement (in bytes) for objects of this class.
Definition TClass.cxx:5840
void BuildRealData(void *pointer=nullptr, Bool_t isTransient=kFALSE)
Build a full list of persistent data members.
Definition TClass.cxx:2043
const std::type_info * GetTypeInfo() const
Definition TClass.h:512
TList * GetListOfDataMembers(Bool_t load=kTRUE)
Return list containing the TDataMembers of a class.
Definition TClass.cxx:3833
TList * GetListOfRealData() const
Definition TClass.h:468
Int_t Size() const
Return size of object of this class.
Definition TClass.cxx:5871
TList * GetListOfBases()
Return list containing the TBaseClass(es) of a class.
Definition TClass.cxx:3699
TVirtualStreamerInfo * GetStreamerInfo(Int_t version=0, Bool_t isTransient=kFALSE) const
returns a pointer to the TVirtualStreamerInfo object for version If the object does not exist,...
Definition TClass.cxx:4716
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition TClass.cxx:2923
Int_t GetClassSize() const
Definition TClass.h:439
Long_t ClassProperty() const
Return the C++ property of this class, eg.
Definition TClass.cxx:2408
Long_t Property() const override
Returns the properties of the TClass as a bit field stored as a Long_t value.
Definition TClass.cxx:6268
@ kInterpreted
Definition TClass.h:129
@ kEmulated
Definition TClass.h:128
Version_t GetClassVersion() const
Definition TClass.h:434
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2999
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
The TEnum class implements the enum type.
Definition TEnum.h:33
EDataType GetUnderlyingType() const
Get the underlying integer type of the enum: enum E { kOne }; // ==> int enum F: long; // ==> long Re...
Definition TEnum.h:81
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
Definition TEnum.cxx:141
static TEnum * GetEnum(const std::type_info &ti, ESearchAction sa=kALoadAndInterpLookup)
Definition TEnum.cxx:181
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:789
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:42
@ kIsOnHeap
object is on heap
Definition TObject.h:90
@ kNotDeleted
object has not been deleted
Definition TObject.h:91
static TClass * Class()
@ kIsReferenced
if object is referenced by a TRef or TRefArray
Definition TObject.h:74
The TRealData class manages the effective list of all data members for a given class.
Definition TRealData.h:30
RAII helper class that ensures that PushProxy() / PopProxy() are called when entering / leaving a C++...
Defines a common interface to inspect/change the contents of an object that represents a collection.
@ kNeedDelete
The collection contains directly or indirectly (via other collection) some pointers that need explici...
virtual Next_t GetFunctionNext(Bool_t read=kTRUE)=0
Return a pointer to a function that can advance an iterator (see Next_t).
virtual DeleteTwoIterators_t GetFunctionDeleteTwoIterators(Bool_t read=kTRUE)=0
virtual TVirtualCollectionProxy * Generate() const =0
Returns a clean object of the actual class that derives from TVirtualCollectionProxy.
virtual CreateIterators_t GetFunctionCreateIterators(Bool_t read=kTRUE)=0
Return a pointer to a function that can create an iterator pair, where each iterator points to the be...
Abstract Interface class describing Streamer information for one class.
virtual TVirtualStreamerInfo * GenerateInfoForPair(const std::string &pairclassname, bool silent, size_t hint_pair_offset, size_t hint_pair_size)=0
Generate the TClass and TStreamerInfo for the requested pair.
virtual Int_t GetNumber() const =0
static TVirtualStreamerInfo * Factory()
Static function returning a pointer to a new TVirtualStreamerInfo object.
virtual TClass * GetClass() const =0
const Int_t n
Definition legend1.C:16
TRangeCast< T, false > TRangeStaticCast
TRangeStaticCast is an adapter class that allows the typed iteration through a TCollection.
void SetAllowFieldSubstitutions(RFieldZero &fieldZero, bool val)
Definition RField.cxx:35
std::tuple< unsigned char **, std::int32_t *, std::int32_t * > GetRVecDataMembers(void *rvecPtr)
Retrieve the addresses of the data members of a generic RVec from a pointer to the beginning of the R...
ROOT::RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
void CallConnectPageSourceOnField(RFieldBase &, ROOT::Internal::RPageSource &)
std::string GetRNTupleSoARecord(const TClass *cl)
Checks if the "rntuple.SoARecord" class attribute is set in the dictionary.
bool NeedsMetaNameAsAlias(const std::string &metaNormalizedName, std::string &renormalizedAlias, bool isArgInTemplatedUserClass=false)
Checks if the meta normalized name is different from the RNTuple normalized name in a way that would ...
std::string GetTypeTraceReport(const RFieldBase &field, const RNTupleDescriptor &desc)
Prints the hierarchy of types with their field names and field IDs for the given in-memory field and ...
std::unique_ptr< RFieldBase > CreateEmulatedEnumField(std::string_view fieldName, std::string_view emulatedFromType, std::string_view underlyingIntType)
ERNTupleSerializationMode GetRNTupleSerializationMode(const TClass *cl)
void EnsureTypedChildField(const RFieldBase &child, const RFieldBase &parent)
Makes sure that the type name is not empty. Used to prevent untyped fields as C++ inner types.
std::string GetRenormalizedTypeName(const std::string &metaNormalizedName)
Given a type name normalized by ROOT meta, renormalize it for RNTuple. E.g., insert std::prefix.
constexpr bool IsValidAlignment(std::size_t align) noexcept
Return true if align is a valid C++ alignment value: strictly positive and a power of two.
Definition BitUtils.hxx:36
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
@ kSTLvector
Definition ESTLType.h:30
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId
ENTupleStructure
The fields in the RNTuple data model tree can carry different structural information about the type s...
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.
std::unique_ptr< RFieldBase > fField
The field used to read the on-disk data.
Definition RField.hxx:141
std::size_t fOffset
offset in fStagingArea
Definition RField.hxx:142