Skip to content

Commit ef8cd49

Browse files
committed
[ntuple] allow creating an AttrSetReader from RNTupleReader
1 parent 047a4e8 commit ef8cd49

6 files changed

Lines changed: 45 additions & 0 deletions

File tree

tree/ntuple/inc/ROOT/RNTupleReader.hxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ public:
527527
/// ~~~
528528
void EnableMetrics() { fMetrics.Enable(); }
529529
const Experimental::Detail::RNTupleMetrics &GetMetrics() const { return fMetrics; }
530+
531+
std::unique_ptr<Experimental::RNTupleAttrSetReader> OpenAttributeSet(std::string_view attrSetName);
530532
}; // class RNTupleReader
531533

532534
} // namespace ROOT

tree/ntuple/inc/ROOT/RPageStorage.hxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,10 @@ public:
854854
/// Forces the loading of ROOT StreamerInfo from the underlying file. This currently only has an effect for
855855
/// TFile-backed sources.
856856
virtual void LoadStreamerInfo() = 0;
857+
858+
/// Reads an attribute set at the given location and returns a page source for it.
859+
virtual std::unique_ptr<ROOT::Internal::RPageSource>
860+
ReadAttributeSet(ROOT::RNTupleLocator anchorLocator, std::uint64_t anchorUncompLen);
857861
}; // class RPageSource
858862

859863
} // namespace Internal

tree/ntuple/inc/ROOT/RPageStorageFile.hxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ public:
200200
LoadClusters(std::span<ROOT::Internal::RCluster::RKey> clusterKeys) final;
201201

202202
void LoadStreamerInfo() final;
203+
204+
std::unique_ptr<ROOT::Internal::RPageSource>
205+
ReadAttributeSet(ROOT::RNTupleLocator anchorLocator, std::uint64_t anchorUncompLen) final;
203206
}; // class RPageSourceFile
204207

205208
} // namespace Internal

tree/ntuple/src/RNTupleReader.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <ROOT/RField.hxx>
1717
#include <ROOT/RFieldVisitor.hxx>
18+
#include <ROOT/RNTupleAttrReading.hxx>
1819
#include <ROOT/RNTupleImtTaskScheduler.hxx>
1920
#include <ROOT/RNTuple.hxx>
2021
#include <ROOT/RNTupleModel.hxx>
@@ -366,3 +367,20 @@ ROOT::DescriptorId_t ROOT::RNTupleReader::RetrieveFieldId(std::string_view field
366367
}
367368
return fieldId;
368369
}
370+
371+
std::unique_ptr<ROOT::Experimental::RNTupleAttrSetReader>
372+
ROOT::RNTupleReader::OpenAttributeSet(std::string_view attrSetName)
373+
{
374+
auto attrSets = GetDescriptor().GetAttrSetIterable();
375+
const auto it =
376+
std::find_if(attrSets.begin(), attrSets.end(), [&](const auto &d) { return d.GetName() == attrSetName; });
377+
if (it == attrSets.end())
378+
throw ROOT::RException(R__FAIL(std::string("No such attribute set: ") + std::string(attrSetName)));
379+
380+
auto attrSource = fSource->ReadAttributeSet(it->GetAnchorLocator(), it->GetAnchorLength());
381+
auto newReader = std::unique_ptr<RNTupleReader>(new RNTupleReader(std::move(attrSource), RNTupleReadOptions{}));
382+
R__ASSERT(newReader);
383+
auto attrSetReader = std::unique_ptr<ROOT::Experimental::RNTupleAttrSetReader>(
384+
new ROOT::Experimental::RNTupleAttrSetReader(std::move(newReader)));
385+
return attrSetReader;
386+
}

tree/ntuple/src/RPageStorage.cxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,3 +1331,9 @@ void ROOT::Internal::RPagePersistentSink::EnableDefaultMetrics(const std::string
13311331
*fMetrics.MakeCounter<RNTupleTickCounter<RNTupleAtomicCounter> *>("timeCpuZip", "ns",
13321332
"CPU time spent compressing")});
13331333
}
1334+
1335+
std::unique_ptr<ROOT::Internal::RPageSource>
1336+
ROOT::Internal::RPageSource::ReadAttributeSet(ROOT::RNTupleLocator /*locator*/, std::uint64_t /*uncompLen*/)
1337+
{
1338+
throw ROOT::RException(R__FAIL("reading attribute sets is not implemented for this Page Source"));
1339+
}

tree/ntuple/src/RPageStorageFile.cxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,3 +744,15 @@ void ROOT::Internal::RPageSourceFile::LoadStreamerInfo()
744744
{
745745
fReader.LoadStreamerInfo();
746746
}
747+
748+
std::unique_ptr<ROOT::Internal::RPageSource>
749+
ROOT::Internal::RPageSourceFile::ReadAttributeSet(ROOT::RNTupleLocator anchorLocator, std::uint64_t anchorUncompLen)
750+
{
751+
assert(anchorLocator.GetType() == RNTupleLocator::kTypeFile);
752+
753+
const auto anchorPos = anchorLocator.GetPosition<std::uint64_t>();
754+
auto attrAnchor =
755+
fReader.GetNTupleProperAtOffset(anchorPos, anchorLocator.GetNBytesOnStorage(), anchorUncompLen).Unwrap();
756+
auto attrSource = OpenWithDifferentAnchor(attrAnchor);
757+
return attrSource;
758+
}

0 commit comments

Comments
 (0)