Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleProcessor.cxx
Go to the documentation of this file.
1/// \file RNTupleProcessor.cxx
2/// \author Florine de Geus <florine.de.geus@cern.ch>
3/// \date 2024-03-26
4/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
5/// is welcome!
6
7/*************************************************************************
8 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
9 * All rights reserved. *
10 * *
11 * For the licensing terms see $ROOTSYS/LICENSE. *
12 * For the list of contributors see $ROOTSYS/README/CREDITS. *
13 *************************************************************************/
14
16
17#include <ROOT/RFieldBase.hxx>
18#include <ROOT/RNTuple.hxx>
20#include <ROOT/StringUtils.hxx>
21
22#include <TDirectory.h>
23
24#include <iomanip>
25
28{
29 processor.AddAllFieldsToEntry(RNTupleProcessorProvenance(), includeSubfields);
30 return processor.fEntry.get();
31}
32
33//------------------------------------------------------------------------------
34
35std::unique_ptr<ROOT::RNTupleProcessor>
37{
38 return std::unique_ptr<RNTupleSingleProcessor>(new RNTupleSingleProcessor(std::move(ntuple), options));
39}
40
41std::unique_ptr<ROOT::RNTupleProcessor>
42ROOT::RNTupleProcessor::CreateChain(std::vector<RNTupleInputSpec> ntuples, const RNTupleProcessorOptions &options)
43{
44 if (ntuples.empty())
45 throw RException(R__FAIL("at least one RNTuple must be provided"));
46
47 std::vector<std::unique_ptr<RNTupleProcessor>> innerProcessors;
48 innerProcessors.reserve(ntuples.size());
49
50 for (auto &ntuple : ntuples) {
51 innerProcessors.emplace_back(Create(std::move(ntuple)));
52 }
53
54 return CreateChain(std::move(innerProcessors), options);
55}
56
57std::unique_ptr<ROOT::RNTupleProcessor>
58ROOT::RNTupleProcessor::CreateChain(std::vector<std::unique_ptr<RNTupleProcessor>> innerProcessors,
59 const RNTupleProcessorOptions &options)
60{
61 if (innerProcessors.empty())
62 throw RException(R__FAIL("at least one inner processor must be provided"));
63
64 return std::unique_ptr<RNTupleChainProcessor>(new RNTupleChainProcessor(std::move(innerProcessors), options));
65}
66
67std::unique_ptr<ROOT::RNTupleProcessor>
69 const std::vector<std::string> &joinFields,
70 const RNTupleProcessorOptions &options)
71{
72 if (joinFields.size() > 4) {
73 throw RException(R__FAIL("a maximum of four join fields is allowed"));
74 }
75
76 if (std::unordered_set(joinFields.begin(), joinFields.end()).size() < joinFields.size()) {
77 throw RException(R__FAIL("join fields must be unique"));
78 }
79
80 std::unique_ptr<RNTupleProcessor> primaryProcessor = RNTupleProcessor::Create(std::move(primaryNTuple), options);
81
82 std::unique_ptr<RNTupleProcessor> auxProcessor = RNTupleProcessor::Create(std::move(auxNTuple));
83
84 return CreateRNTupleJoinProcessor(std::move(primaryProcessor), std::move(auxProcessor), joinFields, options);
85}
86
87std::unique_ptr<ROOT::RNTupleProcessor>
88ROOT::Experimental::CreateRNTupleJoinProcessor(std::unique_ptr<RNTupleProcessor> primaryProcessor,
89 std::unique_ptr<RNTupleProcessor> auxProcessor,
90 const std::vector<std::string> &joinFields,
91 const RNTupleProcessorOptions &options)
92{
93 if (joinFields.size() > 4) {
94 throw RException(R__FAIL("a maximum of four join fields is allowed"));
95 }
96
97 if (std::unordered_set(joinFields.begin(), joinFields.end()).size() < joinFields.size()) {
98 throw RException(R__FAIL("join fields must be unique"));
99 }
100
101 return std::unique_ptr<RNTupleJoinProcessor>(
102 new RNTupleJoinProcessor(std::move(primaryProcessor), std::move(auxProcessor), joinFields, options));
103}
104
105//------------------------------------------------------------------------------
106
108 : RNTupleProcessor(options), fNTupleSpec(std::move(ntuple))
109{
110 if (fOptions.GetProcessorName().empty()) {
112 }
113}
114
115namespace {
116std::unique_ptr<ROOT::Internal::RPageSource> CreatePageSource(const ROOT::RNTupleInputSpec &inputSpec)
117{
118 if (const std::string *storagePath = std::get_if<std::string>(&inputSpec.GetStorage()))
119 return ROOT::Internal::RPageSource::Create(inputSpec.GetNTupleName(), *storagePath);
120
121 auto dir = std::get<TDirectory *>(inputSpec.GetStorage());
122 auto ntuple = std::unique_ptr<ROOT::RNTuple>(dir->Get<ROOT::RNTuple>(inputSpec.GetNTupleName().c_str()));
124}
125} // anonymous namespace
126
127void ROOT::RNTupleSingleProcessor::Initialize(std::shared_ptr<Internal::RNTupleProcessorEntry> entry)
128{
129 // The processor has already been initialized.
130 if (IsInitialized() && fPageSource)
131 return;
132
133 if (!entry)
134 fEntry = std::make_shared<Internal::RNTupleProcessorEntry>();
135 else
136 fEntry = std::move(entry);
137
138 fPageSource = CreatePageSource(fNTupleSpec);
139 fPageSource->Attach();
140
141 fNEntries = fPageSource->GetNEntries();
142}
143
145{
146 Initialize();
147 auto desc = fPageSource->GetSharedDescriptorGuard();
148 auto fieldZeroId = desc->GetFieldZeroId();
149
150 // TODO handle subfields
151 return desc->FindFieldId(fieldName, fieldZeroId) != ROOT::kInvalidDescriptorId;
152}
153
154std::unique_ptr<ROOT::RFieldBase>
155ROOT::RNTupleSingleProcessor::CreateAndConnectField(const std::string &qualifiedFieldName, const std::string &typeName)
156{
157 assert(fPageSource);
158
159 // Strip the "R_rntproc_join_" prefix (for join fields) from the field name, if present.
160 const std::string onDiskFieldName =
161 qualifiedFieldName.find("R_rntproc_join_") == 0 ? qualifiedFieldName.substr(15) : qualifiedFieldName;
162
163 auto descGuard = fPageSource->GetSharedDescriptorGuard();
164 const auto &desc = descGuard.GetConstRef();
165
166 const auto onDiskFieldId = desc.FindFieldId(onDiskFieldName);
167
168 if (onDiskFieldId == kInvalidDescriptorId) {
169 return nullptr;
170 }
171
172 std::unique_ptr<ROOT::RFieldBase> field;
173 if (typeName.empty()) {
174 const auto &fieldDesc = desc.GetFieldDescriptor(onDiskFieldId);
175 field = fieldDesc.CreateField(desc);
176 } else {
177 // Strip the parent field name prefix(es), if present.
178 std::string subfieldName = onDiskFieldName;
179 auto posDot = onDiskFieldName.find_last_of('.');
180 if (posDot != std::string::npos)
181 subfieldName = onDiskFieldName.substr(posDot + 1);
182
183 field = ROOT::RFieldBase::Create(subfieldName, typeName).Unwrap();
184 }
185
186 field->SetOnDiskId(onDiskFieldId);
187 return ConnectField(std::move(field));
188}
189
190std::unique_ptr<ROOT::RFieldBase> ROOT::RNTupleSingleProcessor::ConnectField(std::unique_ptr<ROOT::RFieldBase> field)
191{
192 ROOT::RFieldZero fieldZero;
194 fieldZero.Attach(std::move(field));
195 ROOT::Internal::CallConnectPageSourceOnField(fieldZero, *fPageSource);
196 return std::move(fieldZero.ReleaseSubfields()[0]);
197}
198
200ROOT::RNTupleSingleProcessor::AddFieldToEntry(const std::string &canonicalFieldName, const std::string &typeName,
201 void *valuePtr, const Internal::RNTupleProcessorProvenance &provenance)
202{
203 auto fieldIdx = fEntry->FindFieldIndex(canonicalFieldName, typeName);
204 if (!fieldIdx) {
205 // Strip the processor name prefix(es), if present.
206 std::string qualifiedFieldName = canonicalFieldName;
207 if (provenance.IsPresentInFieldName(qualifiedFieldName)) {
208 qualifiedFieldName = qualifiedFieldName.substr(provenance.Get().size() + 1);
209 }
210
211 auto field = CreateAndConnectField(qualifiedFieldName, typeName);
212
213 if (!field) {
214 throw RException(R__FAIL("cannot register field with name \"" + qualifiedFieldName +
215 "\" because it is not present in the on-disk information of the RNTuple(s) this "
216 "processor is created from"));
217 }
218
219 fieldIdx = fEntry->AddField(qualifiedFieldName, std::move(field), valuePtr, provenance);
220 }
221
222 return *fieldIdx;
223}
224
226ROOT::RNTupleSingleProcessor::AddFieldToEntry(std::unique_ptr<ROOT::RFieldBase> field,
227 const std::string &canonicalFieldName, void *valuePtr,
228 const Internal::RNTupleProcessorProvenance &provenance)
229{
230 auto fieldIdx = fEntry->FindFieldIndex(canonicalFieldName, field->GetTypeName());
231 if (!fieldIdx) {
232 // Strip the processor name prefix(es), if present.
233 std::string qualifiedFieldName = canonicalFieldName;
234 if (provenance.IsPresentInFieldName(qualifiedFieldName)) {
235 qualifiedFieldName = qualifiedFieldName.substr(provenance.Get().size() + 1);
236 }
237
238 field = ConnectField(std::move(field));
239
240 if (!field) {
241 throw RException(R__FAIL("cannot register field with name \"" + qualifiedFieldName +
242 "\" because it is not present in the on-disk information of the RNTuple(s) this "
243 "processor is created from"));
244 }
245
246 fieldIdx = fEntry->AddField(qualifiedFieldName, std::move(field), valuePtr, provenance);
247 }
248
249 return *fieldIdx;
250}
251
253 bool includeSubfields)
254{
255 Initialize();
256 auto descGuard = fPageSource->GetSharedDescriptorGuard();
257 const auto &desc = descGuard.GetConstRef();
258 auto fnAddSubfields = [this, &desc, &provenance](const ROOT::RFieldDescriptor &field, auto &fn) -> void {
259 std::string fieldName = desc.GetQualifiedFieldName(field.GetId());
260 if (auto prefix = provenance.Get(); !prefix.empty())
261 fieldName = prefix + "." + fieldName;
262
263 AddFieldToEntry(fieldName, field.GetTypeName(), nullptr, provenance);
264 for (const auto &subfield : desc.GetFieldIterable(field.GetId())) {
265 fn(subfield, fn);
266 }
267 };
268
269 for (const auto &field : desc.GetTopLevelFields()) {
270 if (includeSubfields) {
271 fnAddSubfields(field, fnAddSubfields);
272 } else {
273 std::string fieldName = desc.GetQualifiedFieldName(field.GetId());
274 if (auto prefix = provenance.Get(); !prefix.empty())
275 fieldName = prefix + "." + fieldName;
276
277 AddFieldToEntry(fieldName, field.GetTypeName(), nullptr, provenance);
278 }
279 }
280}
281
283{
284 if (globalEntryNumber >= fNEntries || !fEntry)
286 else
287 mapping.SetEntryNumber(globalEntryNumber);
288}
289
291{
292 assert(mapping.GetEntryNumber() != kInvalidNTupleIndex);
293
294 for (auto fieldIdx : fFieldIdxs) {
295 fEntry->ReadValue(fieldIdx, mapping.GetEntryNumber());
296 }
297
298 fNEntriesProcessed++;
299}
300
301namespace {
302void ResetColumnRepresentative(ROOT::RFieldBase &field)
303{
305 for (auto &subfield : field.GetMutableSubfields()) {
306 ResetColumnRepresentative(*subfield);
307 }
308}
309
310void SetOnDiskId(ROOT::RFieldBase &field, ROOT::DescriptorId_t onDiskId, const ROOT::RNTupleDescriptor &desc)
311{
312 field.SetOnDiskId(onDiskId);
313
314 for (auto &subfield : field.GetMutableSubfields()) {
315 onDiskId = desc.FindFieldId(subfield->GetQualifiedFieldName());
316 SetOnDiskId(*subfield, onDiskId, desc);
317 }
318}
319} // anonymous namespace
320
322 const std::unordered_set<Internal::RNTupleProcessorEntry::FieldIndex_t> &fieldIdxs,
323 const Internal::RNTupleProcessorProvenance & /* provenance */, bool updateFields)
324{
325 Initialize(fEntry);
326
327 fFieldIdxs = fieldIdxs;
328
329 if (updateFields) {
330 auto descGuard = fPageSource->GetSharedDescriptorGuard();
331 const auto &desc = descGuard.GetConstRef();
332
333 for (const auto &fieldIdx : fFieldIdxs) {
334 const auto &currField = fEntry->GetValue(fieldIdx).GetField();
335
336 auto onDiskId = desc.FindFieldId(currField.GetQualifiedFieldName());
337 bool needsClone = onDiskId != ROOT::kInvalidDescriptorId &&
338 currField.GetTypeName() == desc.GetFieldDescriptor(onDiskId).GetTypeName();
339 if (const auto fieldOriginIdx = fEntry->GetFieldOriginIdx(fieldIdx);
340 fieldOriginIdx != ROOT::kInvalidDescriptorId) {
341 const auto &originField = fEntry->GetField(fieldOriginIdx);
342 onDiskId = desc.FindFieldId(originField.GetQualifiedFieldName());
343 needsClone = true;
344 }
345
346 std::unique_ptr<ROOT::RFieldBase> newField;
347 if (needsClone) {
348 // We have enough information to clone the existing field.
349 newField = currField.Clone(currField.GetFieldName());
350 // The clone's column representatives (+ its subfields') need to be reset before it can be connected to the
351 // new page source.
352 ResetColumnRepresentative(*newField);
353 SetOnDiskId(*newField, onDiskId, desc);
354 newField = ConnectField(std::move(newField));
355 } else {
356 // We need to create the new field from scratch (e.g. because it is a subfield without a parent field
357 // attached).
358 newField = CreateAndConnectField(fEntry->GetQualifiedFieldName(fieldIdx), currField.GetTypeName());
359 }
360
361 fEntry->UpdateField(fieldIdx, std::move(newField));
362 }
363 }
364}
365
367{
368 if (fPageSource) {
369 fEntry->ResetFields(fFieldIdxs);
370 fPageSource.reset();
371 }
372}
373
380
382{
383 static constexpr int width = 32;
384
385 std::string ntupleNameTrunc = fNTupleSpec.fNTupleName.substr(0, width - 4);
386 if (ntupleNameTrunc.size() < fNTupleSpec.fNTupleName.size())
387 ntupleNameTrunc = fNTupleSpec.fNTupleName.substr(0, width - 6) + "..";
388
389 output << "+" << std::setfill('-') << std::setw(width - 1) << "+\n";
390 output << std::setfill(' ') << "| " << ntupleNameTrunc << std::setw(width - 2 - ntupleNameTrunc.size()) << " |\n";
391
392 if (const std::string *storage = std::get_if<std::string>(&fNTupleSpec.fStorage)) {
393 std::string storageTrunc = storage->substr(0, width - 5);
394 if (storageTrunc.size() < storage->size())
395 storageTrunc = storage->substr(0, width - 8) + "...";
396
397 output << std::setfill(' ') << "| " << storageTrunc << std::setw(width - 2 - storageTrunc.size()) << " |\n";
398 } else {
399 output << "| " << std::setw(width - 2) << " |\n";
400 }
401
402 output << "+" << std::setfill('-') << std::setw(width - 1) << "+\n";
403}
404
405//------------------------------------------------------------------------------
406
407ROOT::RNTupleChainProcessor::RNTupleChainProcessor(std::vector<std::unique_ptr<RNTupleProcessor>> processors,
408 const RNTupleProcessorOptions &options)
409 : RNTupleProcessor(options), fInnerProcessors(std::move(processors))
410{
411 if (fOptions.GetProcessorName().empty()) {
412 // `CreateChain` ensures there is at least one inner processor.
414 }
415
417}
418
419void ROOT::RNTupleChainProcessor::Initialize(std::shared_ptr<Internal::RNTupleProcessorEntry> entry)
420{
421 if (IsInitialized())
422 return;
423
424 if (!entry)
425 fEntry = std::make_shared<Internal::RNTupleProcessorEntry>();
426 else
427 fEntry = std::move(entry);
428
429 fInnerProcessors[0]->Initialize(fEntry);
430}
431
433{
434 if (fNEntries == kInvalidNTupleIndex) {
435 fNEntries = 0;
436
437 for (unsigned i = 0; i < fInnerProcessors.size(); ++i) {
438 if (fInnerNEntries[i] == kInvalidNTupleIndex) {
439 fInnerProcessors[i]->Initialize(fEntry);
440 fInnerNEntries[i] = fInnerProcessors[i]->GetNEntries();
441 }
442
443 fNEntries += fInnerNEntries[i];
444 }
445 }
446
447 return fNEntries;
448}
449
451 const std::unordered_set<Internal::RNTupleProcessorEntry::FieldIndex_t> &fieldIdxs,
452 const Internal::RNTupleProcessorProvenance &provenance, bool /* updateFields */)
453{
454 Initialize();
455 fFieldIdxs = fieldIdxs;
456 fProvenance = provenance;
457 ConnectInnerProcessor(fCurrentProcessorNumber);
458}
459
461{
462 for (const auto &innerProc : fInnerProcessors) {
463 innerProc->Disconnect();
464 }
465}
466
468{
469 if (fCurrentProcessorNumber != processorNumber) {
470 fInnerProcessors[fCurrentProcessorNumber]->Disconnect();
471 fCurrentProcessorNumber = processorNumber;
472 }
473
474 auto &innerProc = fInnerProcessors[processorNumber];
475 innerProc->Initialize(fEntry);
476 innerProc->Connect(fFieldIdxs, fProvenance, /*updateFields=*/true);
477}
478
480ROOT::RNTupleChainProcessor::AddFieldToEntry(const std::string &fieldName, const std::string &typeName, void *valuePtr,
481 const Internal::RNTupleProcessorProvenance &provenance)
482{
483 return fInnerProcessors[fCurrentProcessorNumber]->AddFieldToEntry(fieldName, typeName, valuePtr, provenance);
484}
485
487ROOT::RNTupleChainProcessor::AddFieldToEntry(std::unique_ptr<ROOT::RFieldBase> field, const std::string &fieldName,
488 void *valuePtr, const Internal::RNTupleProcessorProvenance &provenance)
489{
490 return fInnerProcessors[fCurrentProcessorNumber]->AddFieldToEntry(std::move(field), fieldName, valuePtr, provenance);
491}
492
494 bool includeSubfields)
495{
496 Initialize();
497 fInnerProcessors[0]->AddAllFieldsToEntry(provenance, includeSubfields);
498}
499
501{
502 std::size_t currProcessorIdx = mapping.GetInnerProcessorIdx();
503
504 // If the requested entry number is lower than the current entry number, we have to again localise the correct local
505 // entry number starting from the first processor in the chain. Otherwise, we can continue looking from the inner
506 // processor that is currently connected, which is much faster when the chain consists of many inner processors.
507 if (fLastLoadedEntry != ROOT::kInvalidNTupleIndex && globalEntryNumber < fLastLoadedEntry) {
508 currProcessorIdx = 0;
509 ConnectInnerProcessor(currProcessorIdx);
510 }
511
512 ROOT::NTupleSize_t entriesSeen = 0;
513 for (unsigned i = 0; i < currProcessorIdx; ++i) {
514 if (fInnerNEntries[i] == kInvalidNTupleIndex) {
515 fInnerNEntries[i] = fInnerProcessors[i]->GetNEntries();
516 }
517
518 if (fInnerNEntries[i] >= globalEntryNumber) {
519 currProcessorIdx = 0;
520 ConnectInnerProcessor(currProcessorIdx);
521 break;
522 }
523 entriesSeen += fInnerNEntries[i];
524 }
525
526 ROOT::NTupleSize_t localEntryNumber = globalEntryNumber - entriesSeen;
527 fInnerProcessors[currProcessorIdx]->SetEntryMapping(localEntryNumber, mapping.GetInnerProcessorMapping());
528
529 // As long as the entry fails to load from the current processor, we decrement the local entry number with the number
530 // of entries in this processor and try with the next processor until we find the correct local entry number.
532 if (fInnerNEntries[currProcessorIdx] == kInvalidNTupleIndex) {
533 fInnerNEntries[currProcessorIdx] = fInnerProcessors[currProcessorIdx]->GetNEntries();
534 }
535
536 localEntryNumber -= fInnerNEntries[currProcessorIdx];
537
538 // The provided global entry number is larger than the number of available entries.
539 if (++currProcessorIdx >= fInnerProcessors.size()) {
541 return;
542 }
543
544 ConnectInnerProcessor(currProcessorIdx);
545 fInnerProcessors[currProcessorIdx]->SetEntryMapping(localEntryNumber, mapping.GetInnerProcessorMapping());
546 }
547
548 mapping.SetEntryNumber(globalEntryNumber);
549 mapping.SetInnerProcessorIdx(currProcessorIdx);
550}
551
553{
554 assert(mapping.GetEntryNumber() != kInvalidNTupleIndex);
555
556 if (fCurrentProcessorNumber != mapping.GetInnerProcessorIdx())
557 ConnectInnerProcessor(mapping.GetInnerProcessorIdx());
558 fInnerProcessors[mapping.GetInnerProcessorIdx()]->LoadEntry(mapping.GetInnerProcessorMapping());
559 fLastLoadedEntry = mapping.GetEntryNumber();
560 fNEntriesProcessed++;
561}
562
564 ROOT::NTupleSize_t entryOffset)
565{
566 for (unsigned i = 0; i < fInnerProcessors.size(); ++i) {
567 const auto &innerProc = fInnerProcessors[i];
568 // TODO can this be done (more) lazily? I.e. only when a match cannot be found in the current inner proc?
569 innerProc->Initialize(fEntry);
570 innerProc->AddEntriesToJoinTable(joinTable, entryOffset);
571 entryOffset += innerProc->GetNEntries();
572 }
573}
574
576{
577 for (const auto &innerProc : fInnerProcessors) {
578 innerProc->PrintStructure(output);
579 }
580}
581
582//------------------------------------------------------------------------------
583
584ROOT::Experimental::RNTupleJoinProcessor::RNTupleJoinProcessor(std::unique_ptr<RNTupleProcessor> primaryProcessor,
585 std::unique_ptr<RNTupleProcessor> auxProcessor,
586 const std::vector<std::string> &joinFields,
587 const RNTupleProcessorOptions &options)
588 : RNTupleProcessor(options),
589 fPrimaryProcessor(std::move(primaryProcessor)),
590 fAuxiliaryProcessor(std::move(auxProcessor)),
591 fJoinFieldNames(joinFields)
592{
593 if (fOptions.GetProcessorName().empty()) {
594 fOptions.SetProcessorName(fPrimaryProcessor->fOptions.GetProcessorName());
595 }
596}
597
598void ROOT::Experimental::RNTupleJoinProcessor::Initialize(std::shared_ptr<ROOT::Internal::RNTupleProcessorEntry> entry)
599{
600 if (IsInitialized())
601 return;
602
603 if (!entry)
604 fEntry = std::make_shared<ROOT::Internal::RNTupleProcessorEntry>();
605 else
606 fEntry = std::move(entry);
607
608 fPrimaryProcessor->Initialize(fEntry);
609 fAuxiliaryProcessor->Initialize(fEntry);
610
611 if (!fJoinFieldNames.empty()) {
612 for (const auto &joinField : fJoinFieldNames) {
613 if (!fPrimaryProcessor->CanReadFieldFromDisk(joinField)) {
614 throw RException(R__FAIL("could not find join field \"" + joinField + "\" in primary processor \"" +
615 fPrimaryProcessor->fOptions.GetProcessorName() + "\""));
616 }
617 if (!fAuxiliaryProcessor->CanReadFieldFromDisk(joinField)) {
618 throw RException(R__FAIL("could not find join field \"" + joinField + "\" in auxiliary processor \"" +
619 fAuxiliaryProcessor->fOptions.GetProcessorName() + "\""));
620 }
621
622 // We prepend the name of the primary processor in this case to prevent reading from the wrong join field in
623 // composed join operations.
624 auto fieldIdx =
625 AddFieldToEntry(fOptions.GetProcessorName() + ".R_rntproc_join_" + joinField, "std::uint64_t", nullptr,
626 ROOT::Internal::RNTupleProcessorProvenance(fOptions.GetProcessorName()));
627 fJoinFieldIdxs.insert(fieldIdx);
628 }
629
630 fJoinTable = Experimental::Internal::RNTupleJoinTable::Create(fJoinFieldNames);
631 }
632}
633
635 const std::unordered_set<ROOT::Internal::RNTupleProcessorEntry::FieldIndex_t> &fieldIdxs,
636 const ROOT::Internal::RNTupleProcessorProvenance &provenance, bool updateFields)
637{
638 Initialize();
639
640 auto auxProvenance = provenance.Evolve(fAuxiliaryProcessor->fOptions.GetProcessorName());
641 for (const auto &fieldIdx : fieldIdxs) {
642 const auto &fieldProvenance = fEntry->GetFieldProvenance(fieldIdx);
643 if (fieldProvenance.Contains(auxProvenance))
644 fAuxiliaryFieldIdxs.insert(fieldIdx);
645 else
646 fFieldIdxs.insert(fieldIdx);
647 }
648
649 fPrimaryProcessor->Connect(fFieldIdxs, provenance, updateFields);
650 fAuxiliaryProcessor->Connect(fAuxiliaryFieldIdxs, auxProvenance, updateFields);
651}
652
654{
655 fPrimaryProcessor->Disconnect();
656 fAuxiliaryProcessor->Disconnect();
657}
658
660ROOT::Experimental::RNTupleJoinProcessor::AddFieldToEntry(const std::string &fieldName, const std::string &typeName,
661 void *valuePtr,
663{
664 auto auxProvenance = provenance.Evolve(fAuxiliaryProcessor->fOptions.GetProcessorName());
665 if (auxProvenance.IsPresentInFieldName(fieldName)) {
666 // If the primaryProcessor has a field with the name of the auxProcessor (either as a "proper" field or because
667 // the primary processor itself is a join where its auxProcessor bears the same name as the current auxProcessor),
668 // there will be name conflicts, so error out.
669 if (fPrimaryProcessor->CanReadFieldFromDisk(fieldName)) {
670 throw RException(R__FAIL("ambiguous field name: \"" + fieldName +
671 "\" is present in the primary RNTupleProcessor \"" +
672 fPrimaryProcessor->fOptions.GetProcessorName() +
673 "\", but may also refer to a field in the auxiliary RNTupleProcessor named \"" +
674 fAuxiliaryProcessor->fOptions.GetProcessorName() +
675 "\". To avoid this ambiguity, rename the auxiliary RNTupleProcessor."));
676 }
677
678 auto fieldIdx = fAuxiliaryProcessor->AddFieldToEntry(fieldName, typeName, valuePtr, auxProvenance);
679 if (fieldIdx)
680 fAuxiliaryFieldIdxs.insert(fieldIdx);
681 return fieldIdx;
682 } else {
683 auto fieldIdx = fPrimaryProcessor->AddFieldToEntry(fieldName, typeName, valuePtr, provenance);
684 if (fieldIdx)
685 fFieldIdxs.insert(fieldIdx);
686 return fieldIdx;
687 }
688}
689
691ROOT::Experimental::RNTupleJoinProcessor::AddFieldToEntry(std::unique_ptr<ROOT::RFieldBase> field,
692 const std::string &fieldName, void *valuePtr,
694{
695 auto auxProvenance = provenance.Evolve(fAuxiliaryProcessor->GetOptions().GetProcessorName());
696 if (auxProvenance.IsPresentInFieldName(fieldName)) {
697 // If the primaryProcessor has a field with the name of the auxProcessor (either as a "proper" field or because
698 // the primary processor itself is a join where its auxProcessor bears the same name as the current auxProcessor),
699 // there will be name conflicts, so error out.
700 if (fPrimaryProcessor->CanReadFieldFromDisk(fieldName)) {
701 throw RException(R__FAIL("ambiguous field name: \"" + fieldName +
702 "\" is present in the primary RNTupleProcessor \"" +
703 fPrimaryProcessor->GetOptions().GetProcessorName() +
704 "\", but may also refer to a field in the auxiliary RNTupleProcessor named \"" +
705 fAuxiliaryProcessor->GetOptions().GetProcessorName() +
706 "\". To avoid this ambiguity, rename the auxiliary RNTupleProcessor."));
707 }
708
709 auto fieldIdx = fAuxiliaryProcessor->AddFieldToEntry(std::move(field), fieldName, valuePtr, auxProvenance);
710 fAuxiliaryFieldIdxs.insert(fieldIdx);
711 return fieldIdx;
712 } else {
713 auto fieldIdx = fPrimaryProcessor->AddFieldToEntry(std::move(field), fieldName, valuePtr, provenance);
714 if (fieldIdx)
715 fFieldIdxs.insert(fieldIdx);
716 return fieldIdx;
717 }
718}
719
721 const ROOT::Internal::RNTupleProcessorProvenance &provenance, bool includeSubfields)
722{
723 Initialize();
724 fPrimaryProcessor->AddAllFieldsToEntry(provenance, includeSubfields);
725 auto auxProvenance = provenance.Evolve(fAuxiliaryProcessor->GetOptions().GetProcessorName());
726 fAuxiliaryProcessor->AddAllFieldsToEntry(auxProvenance, includeSubfields);
727}
728
730{
731 for (const auto &fieldIdx : fAuxiliaryFieldIdxs) {
732 fEntry->SetFieldValidity(fieldIdx, isValid);
733 }
734}
735
737 REntryMapping &mapping)
738{
739 fPrimaryProcessor->SetEntryMapping(globalEntryNumber, mapping.GetPrimaryProcessorMapping());
742 for (auto fieldIdx : fFieldIdxs) {
743 fEntry->SetFieldValidity(fieldIdx, false);
744 }
745 SetAuxiliaryFieldValidity(false);
746 return;
747 }
748 // We only set the entry mapping of the auxiliary ntuple here if the join is aligned. For unaligne joins, we need to
749 // first get the correct entry number from the join table, which requires reading join field data from disk. Because
750 // we want to do this only when the entry is actually loaded to avoid unnecessary reads, we postpone this to the
751 // actual `LoadEntry` call.
752 if (!fJoinTable) {
753 fAuxiliaryProcessor->SetEntryMapping(globalEntryNumber, mapping.GetAuxiliaryProcessorMapping());
754 }
755 mapping.SetEntryNumber(globalEntryNumber);
756}
757
759{
760 assert(mapping.GetEntryNumber() != kInvalidNTupleIndex);
761
762 fPrimaryProcessor->LoadEntry(mapping.GetPrimaryProcessorMapping());
763 fNEntriesProcessed++;
764
765 if (!fJoinTable) {
766 fAuxiliaryProcessor->LoadEntry(mapping.GetAuxiliaryProcessorMapping());
767 return;
768 }
769
770 if (!fJoinTableIsBuilt) {
771 fAuxiliaryProcessor->AddEntriesToJoinTable(*fJoinTable);
772 fJoinTableIsBuilt = true;
773 }
774
775 // Collect the values of the join fields for this entry.
776 std::vector<ROOT::Experimental::Internal::RNTupleJoinTable::JoinValue_t> values;
777 values.reserve(fJoinFieldIdxs.size());
778 for (const auto &fieldIdx : fJoinFieldIdxs) {
779 auto val = fEntry->GetValue(fieldIdx).GetRef<ROOT::Experimental::Internal::RNTupleJoinTable::JoinValue_t>();
780 values.push_back(val);
781 }
782
783 // Find the entry index corresponding to the join field values for each auxiliary processor and load the
784 // corresponding entry.
785 const auto entryIdx = fJoinTable->GetEntryIndex(values);
786 REntryMapping auxMapping;
787 fAuxiliaryProcessor->SetEntryMapping(entryIdx, auxMapping);
788
789 if (entryIdx == kInvalidNTupleIndex) {
790 SetAuxiliaryFieldValidity(false);
791 } else {
792 SetAuxiliaryFieldValidity(true);
793 fAuxiliaryProcessor->LoadEntry(auxMapping);
794 }
795}
796
798{
799 if (fNEntries == kInvalidNTupleIndex)
800 fNEntries = fPrimaryProcessor->GetNEntries();
801 return fNEntries;
802}
803
806{
807 fPrimaryProcessor->AddEntriesToJoinTable(joinTable, entryOffset);
808}
809
811{
812 std::ostringstream primaryStructureStr;
813 fPrimaryProcessor->PrintStructure(primaryStructureStr);
814 const auto primaryStructure = ROOT::Split(primaryStructureStr.str(), "\n", /*skipEmpty=*/true);
815 const auto primaryStructureWidth = primaryStructure.front().size();
816
817 std::ostringstream auxStructureStr;
818 fAuxiliaryProcessor->PrintStructure(auxStructureStr);
819 const auto auxStructure = ROOT::Split(auxStructureStr.str(), "\n", /*skipEmpty=*/true);
820
821 const auto maxLength = std::max(primaryStructure.size(), auxStructure.size());
822 for (unsigned i = 0; i < maxLength; i++) {
823 if (i < primaryStructure.size())
824 output << primaryStructure[i];
825 else
826 output << std::setw(primaryStructureWidth) << "";
827
828 if (i < auxStructure.size())
829 output << " " << auxStructure[i];
830
831 output << "\n";
832 }
833}
#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
Option_t Option_t width
Builds a join table on one or several fields of an RNTuple so it can be joined onto other RNTuples.
static std::unique_ptr< RNTupleJoinTable > Create(const std::vector< std::string > &joinFieldNames)
Create an RNTupleJoinTable from an existing RNTuple.
RNTupleJoinTable & Add(ROOT::Internal::RPageSource &pageSource, PartitionKey_t partitionKey=kDefaultPartitionKey, ROOT::NTupleSize_t entryOffset=0)
Add an entry mapping to the join table.
static constexpr PartitionKey_t kDefaultPartitionKey
Processor specialization for horizontally combined (joined) RNTupleProcessors.
void PrintStructureImpl(std::ostream &output) const final
Processor-specific implementation for printing its structure, called by PrintStructure().
void SetEntryMapping(ROOT::NTupleSize_t globalEntryNumber, REntryMapping &mapping) final
Set the local entry mapping (for nested processors), according to a global entry number.
void AddAllFieldsToEntry(const ROOT::Internal::RNTupleProcessorProvenance &provenance, bool includeSubfields) final
Add all known fields (from on-disk information) to the entry.
void AddEntriesToJoinTable(Internal::RNTupleJoinTable &joinTable, ROOT::NTupleSize_t entryOffset=0) final
Add the entry mappings for this processor to the provided join table.
void Disconnect() final
Disconnect the processor from associated physical storage.
ROOT::NTupleSize_t GetNEntries() final
Get the total number of entries in this processor.
void SetAuxiliaryFieldValidity(bool validity)
Set the validity for all fields in the auxiliary processor at once.
void Initialize(std::shared_ptr< ROOT::Internal::RNTupleProcessorEntry > entry=nullptr) final
Initialize the processor by creating an (initially empty) fEntry, or setting an existing one.
void Connect(const std::unordered_set< ROOT::Internal::RNTupleProcessorEntry::FieldIndex_t > &fieldIdxs, const ROOT::Internal::RNTupleProcessorProvenance &provenance=ROOT::Internal::RNTupleProcessorProvenance(), bool updateFields=false) final
Connect the provided fields indices in the entry to their on-disk fields.
std::unique_ptr< RNTupleProcessor > fPrimaryProcessor
void LoadEntryImpl(const REntryMapping &mapping) final
Load the entry identified by the entry mapping.
ROOT::Internal::RNTupleProcessorEntry::FieldIndex_t AddFieldToEntry(const std::string &fieldName, const std::string &typeName, void *valuePtr=nullptr, const ROOT::Internal::RNTupleProcessorProvenance &provenance=ROOT::Internal::RNTupleProcessorProvenance()) final
Add a field to the entry.
Collection of values in an RNTupleProcessor, analogous to REntry, with checks and support for missing...
std::string Get() const
Get the full processor provenance, in the form of "x.y.z".
RNTupleProcessorProvenance Evolve(const std::string &processorName) const
Add a new processor to the provenance.
bool IsPresentInFieldName(std::string_view fieldName) const
Check whether the provided field name contains this provenance.
static std::unique_ptr< RPageSourceFile > CreateFromAnchor(const RNTuple &anchor, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Used from the RNTuple class to build a datasource if the anchor is already available.
static std::unique_ptr< RPageSource > Create(std::string_view ntupleName, std::string_view location, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Guess the concrete derived page source from the file name (location)
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
std::vector< ColumnRepresentation_t > Selection_t
A list of column representations.
A field translates read and write calls from/to underlying columns to/from tree values.
void SetColumnRepresentatives(const RColumnRepresentations::Selection_t &representatives)
Fixes a column representative.
void SetOnDiskId(ROOT::DescriptorId_t id)
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.
Metadata stored for every field of an RNTuple.
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
void Attach(std::unique_ptr< RFieldBase > child)
A public version of the Attach method that allows piece-wise construction of the zero field.
Definition RField.cxx:40
Processor specialization for vertically combined (chained) RNTupleProcessors.
std::vector< std::unique_ptr< RNTupleProcessor > > fInnerProcessors
void AddAllFieldsToEntry(const Internal::RNTupleProcessorProvenance &provenance, bool includeSubfields) final
Add all known fields (from on-disk information) to the entry.
void ConnectInnerProcessor(std::size_t processorNumber)
Update the entry to reflect any missing fields in the current inner processor.
void Initialize(std::shared_ptr< ROOT::Internal::RNTupleProcessorEntry > entry=nullptr) final
Initialize the processor by creating an (initially empty) fEntry, or setting an existing one.
std::vector< ROOT::NTupleSize_t > fInnerNEntries
void Connect(const std::unordered_set< Internal::RNTupleProcessorEntry::FieldIndex_t > &fieldIdxs, const Internal::RNTupleProcessorProvenance &provenance=Internal::RNTupleProcessorProvenance(), bool updateFields=false) final
Connect the provided fields indices in the entry to their on-disk fields.
Internal::RNTupleProcessorEntry::FieldIndex_t AddFieldToEntry(const std::string &fieldName, const std::string &typeName, void *valuePtr=nullptr, const Internal::RNTupleProcessorProvenance &provenance=Internal::RNTupleProcessorProvenance()) final
Add a field to the entry.
void LoadEntryImpl(const REntryMapping &mapping) final
Load the entry identified by the entry mapping.
void SetEntryMapping(ROOT::NTupleSize_t globalEntryNumber, REntryMapping &mapping) final
Set the local entry mapping (for nested processors), according to a global entry number.
void AddEntriesToJoinTable(Experimental::Internal::RNTupleJoinTable &joinTable, ROOT::NTupleSize_t entryOffset=0) final
Add the entry mappings for this processor to the provided join table.
void PrintStructureImpl(std::ostream &output) const final
Processor-specific implementation for printing its structure, called by PrintStructure().
void Disconnect() final
Disconnect the processor from associated physical storage.
ROOT::NTupleSize_t GetNEntries() final
Get the total number of entries in this processor.
The on-storage metadata of an RNTuple.
const RFieldDescriptor & GetFieldDescriptor(ROOT::DescriptorId_t fieldId) const
ROOT::DescriptorId_t FindFieldId(std::string_view fieldName, ROOT::DescriptorId_t parentId) const
const std::string & GetNTupleName() const
const Storage_t & GetStorage() const
const std::string & GetProcessorName() const
Get the processor's name.
void SetProcessorName(std::string_view name)
Set the processor's name.
Maps a processor's global (outer) entry index to its inner ones.
void SetEntryNumber(ROOT::NTupleSize_t entryNumber)
ROOT::NTupleSize_t GetEntryNumber() const
Interface for iterating over entries of vertically ("chained") and/or horizontally ("joined") combine...
virtual void AddAllFieldsToEntry(const Internal::RNTupleProcessorProvenance &provenance, bool includeSubfields)=0
Add all known fields (from on-disk information) to the entry.
static std::unique_ptr< RNTupleProcessor > CreateChain(std::vector< RNTupleInputSpec > ntuples, const RNTupleProcessorOptions &opts=RNTupleProcessorOptions())
Create an RNTupleProcessor for a chain (i.e., a vertical combination) of RNTuples.
static std::unique_ptr< RNTupleProcessor > Create(RNTupleInputSpec ntuple, const RNTupleProcessorOptions &opts=RNTupleProcessorOptions())
Create an RNTupleProcessor for a single RNTuple.
RNTupleProcessorOptions fOptions
friend class Experimental::RNTupleJoinProcessor
std::shared_ptr< ROOT::Internal::RNTupleProcessorEntry > fEntry
void SetEntryMapping(ROOT::NTupleSize_t globalEntryNumber, REntryMapping &mapping) final
Set the local entry mapping (for nested processors), according to a global entry number.
void PrintStructureImpl(std::ostream &output) const final
Processor-specific implementation for printing its structure, called by PrintStructure().
void LoadEntryImpl(const REntryMapping &mapping) final
Load the entry identified by the entry mapping.
std::unique_ptr< ROOT::RFieldBase > ConnectField(std::unique_ptr< ROOT::RFieldBase > field)
Connect an existing field to the processor's page source.
bool CanReadFieldFromDisk(std::string_view fieldName) final
Check if a field exists on-disk and can be read by the processor.
void Initialize(std::shared_ptr< ROOT::Internal::RNTupleProcessorEntry > entry=nullptr) final
Initialize the processor by creating an (initially empty) fEntry, or setting an existing one.
Internal::RNTupleProcessorEntry::FieldIndex_t AddFieldToEntry(const std::string &fieldName, const std::string &typeName, void *valuePtr=nullptr, const Internal::RNTupleProcessorProvenance &provenance=Internal::RNTupleProcessorProvenance()) final
Add a field to the entry.
void AddAllFieldsToEntry(const Internal::RNTupleProcessorProvenance &provenance, bool includeSubfields) final
Add all known fields (from on-disk information) to the entry.
void AddEntriesToJoinTable(Experimental::Internal::RNTupleJoinTable &joinTable, ROOT::NTupleSize_t entryOffset=0) final
Add the entry mappings for this processor to the provided join table.
std::unique_ptr< ROOT::RFieldBase > CreateAndConnectField(const std::string &qualifiedFieldName, const std::string &typeName)
Create a new field and connect it to the processor's page source.
void Connect(const std::unordered_set< Internal::RNTupleProcessorEntry::FieldIndex_t > &fieldIdxs, const Internal::RNTupleProcessorProvenance &provenance=Internal::RNTupleProcessorProvenance(), bool updateFields=false) final
Connect the provided fields indices in the entry to their on-disk fields.
void Disconnect() final
Disconnect the processor from associated physical storage.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
std::unique_ptr< RNTupleProcessor > CreateRNTupleJoinProcessor(RNTupleInputSpec primaryNTuple, RNTupleInputSpec auxNTuple, const std::vector< std::string > &joinFields, const RNTupleProcessorOptions &opts=RNTupleProcessorOptions())
Create an RNTupleProcessor for a join (i.e., a horizontal combination) of RNTuples.
void SetAllowFieldSubstitutions(RFieldZero &fieldZero, bool val)
Definition RField.cxx:35
void CallConnectPageSourceOnField(RFieldBase &, ROOT::Internal::RPageSource &)
const RNTupleProcessorEntry * LoadFullRNTupleProcessorEntry(ROOT::RNTupleProcessor &processor, bool includeSubfields)
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr NTupleSize_t kInvalidNTupleIndex
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId