Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RNTupleAttrReading.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleAttrReading.hxx
2/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
3/// \date 2026-04-01
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#ifndef ROOT7_RNTuple_Attr_Reading
8#define ROOT7_RNTuple_Attr_Reading
9
10#include <memory>
11#include <optional>
12#include <utility>
13#include <vector>
14
17#include <ROOT/RNTupleUtils.hxx>
18
19namespace ROOT {
20
21class REntry;
23class RNTupleModel;
24
25namespace Experimental {
26
28
29// clang-format off
30/**
31\class ROOT::Experimental::RNTupleAttrRange
32\ingroup NTuple
33\brief A range of main entries referred to by an attribute entry
34
35Each attribute entry contains a set of values referring to 0 or more contiguous entries in the main RNTuple.
36This class represents that contiguous range of entries.
37*/
38// clang-format on
39class RNTupleAttrRange final {
42
44
45public:
50
51 /// Creates an AttributeRange from [start, end), where `end` is one past the last valid entry of the range
52 /// (`FromStartEnd(0, 10)` will create a range whose last valid index is 9).
58
59 RNTupleAttrRange() = default;
60
61 /// Returns the first valid entry index in the range. Returns nullopt if the range has zero length.
62 std::optional<ROOT::NTupleSize_t> GetFirst() const { return fLength ? std::make_optional(fStart) : std::nullopt; }
63 /// Returns the beginning of the range. Note that this is *not* a valid index in the range if the range has zero
64 /// length.
66 /// Returns the last valid entry index in the range. Returns nullopt if the range has zero length.
67 std::optional<ROOT::NTupleSize_t> GetLast() const
68 {
69 return fLength ? std::make_optional(fStart + fLength - 1) : std::nullopt;
70 }
71 /// Returns one past the last valid index of the range, equal to `GetStart() + GetLength()`.
74
75 /// Returns the pair { firstEntryIdx, lastEntryIdx } (inclusive). Returns nullopt if the range has zero length.
76 std::optional<std::pair<ROOT::NTupleSize_t, ROOT::NTupleSize_t>> GetFirstLast() const
77 {
78 return fLength ? std::make_optional(std::make_pair(fStart, fStart + fLength - 1)) : std::nullopt;
79 }
80 /// Returns the pair { start, length }.
81 std::pair<ROOT::NTupleSize_t, ROOT::NTupleSize_t> GetStartLength() const { return {fStart, fLength}; }
82};
83
84// clang-format off
85/**
86\class ROOT::Experimental::RNTupleAttrSetReader
87\ingroup NTuple
88\brief Class used to read a RNTupleAttrSet in the context of a RNTupleReader
89
90An RNTupleAttrSetReader is created via RNTupleReader::OpenAttributeSet. Once created, it may outlive its parent Reader.
91Reading Attributes works similarly to reading regular RNTuple entries: you can either create entries or just use the
92AttrSetReader Model's default entry and load data into it via LoadEntry.
93
94~~ {.cpp}
95// Reading Attributes via RNTupleAttrSetReader
96// -------------------------------------------
97
98// Assuming `reader` is a RNTupleReader:
99auto attrSet = reader->OpenAttributeSet("MyAttrSet");
100
101// Just like how you would read a regular RNTuple, first get the pointer to the fields you want to read:
102auto &attrEntry = attrSet->GetModel().GetDefaultEntry();
103auto pAttr = attrEntry->GetPtr<std::string>("myAttr");
104
105// Then select which attributes you want to read. E.g. read all attributes linked to the entry at index 10:
106for (auto idx : attrSet->GetAttributes(10)) {
107 attrSet->LoadEntry(idx);
108 cout << "entry " << idx << " has attribute " << *pAttr << "\n";
109}
110~~
111*/
112// clang-format on
116
117 /// List containing pairs { entryRange, entryIndex }, used to quickly find out which entries in the Attribute
118 /// RNTuple contain entries that overlap a given range. The list is sorted by range start, i.e.
119 /// entryRange.first.Start().
120 std::vector<std::pair<RNTupleAttrRange, NTupleSize_t>> fEntryRanges;
121 /// The internal Reader used to read the AttributeSet RNTuple
122 std::unique_ptr<RNTupleReader> fReader;
123 /// The reconstructed user model
124 std::unique_ptr<ROOT::RNTupleModel> fUserModel;
125
126 RNTupleAttrSetReader(std::unique_ptr<RNTupleReader> reader, std::uint16_t vSchemaMajor);
127
128public:
134
135 /// Returns the read-only descriptor of this attribute set
137 /// Returns the read-only model of this attribute set
138 const ROOT::RNTupleModel &GetModel() const { return *fUserModel; }
139
140 /// Creates an entry suitable for use with LoadEntry.
141 /// This is a convenience method equivalent to GetModel().CreateEntry().
142 std::unique_ptr<REntry> CreateEntry();
143
144 /// Loads the attribute entry at position `index` into the default entry.
145 /// Returns the range of main RNTuple entries that the loaded set of attributes refers to.
147 /// Loads the attribute entry at position `index` into the given entry.
148 /// Returns the range of main RNTuple entries that the loaded set of attributes refers to.
150
151 /// Returns the number of all attribute entries in this attribute set.
152 std::size_t GetNEntries() const { return fEntryRanges.size(); }
153
154 /// Returns all the attributes in this Set. The returned attributes are sorted by entry range start.
156 /// Returns all the attributes whose range contains index `entryIndex`.
158 /// Returns all the attributes whose range fully contains `[startEntry, endEntry)`
160 /// Returns all the attributes whose range is fully contained in `[startEntry, endEntry)`
162};
163
164// clang-format off
165/**
166\class ROOT::Experimental::RNTupleAttrEntryIterable
167\ingroup NTuple
168\brief Iterable class used to loop over attribute entries.
169
170This class allows to perform range-for iteration on some set of attributes, typically returned by the
171RNTupleAttrSetReader::GetAttributes family of methods.
172
173See the documentation of RNTupleAttrSetReader for example usage.
174*/
175// clang-format on
177public:
182
183private:
185 std::optional<RFilter> fFilter;
186
187public:
188 class RIterator final {
189 private:
190 using Iter_t = decltype(std::declval<RNTupleAttrSetReader>().fEntryRanges.begin());
192 std::optional<RFilter> fFilter;
193
194 Iter_t SkipFiltered() const;
195 bool FullyContained(RNTupleAttrRange range) const;
196
197 public:
198 using iterator_category = std::forward_iterator_tag;
201 using difference_type = std::ptrdiff_t;
202 using pointer = const value_type *;
203 using reference = const value_type &;
204
205 RIterator(Iter_t iter, Iter_t end, std::optional<RFilter> filter) : fCur(iter), fEnd(end), fFilter(filter)
206 {
207 if (fFilter) {
208 if (fFilter->fRange.GetLength() == 0)
209 fCur = end;
210 else
211 fCur = SkipFiltered();
212 }
213 }
215 {
216 ++fCur;
217 fCur = SkipFiltered();
218 return *this;
219 }
221 {
222 iterator it = *this;
223 operator++();
224 return it;
225 }
226 reference operator*() { return fCur->second; }
227 bool operator!=(const iterator &rh) const { return !operator==(rh); }
228 bool operator==(const iterator &rh) const { return fCur == rh.fCur; }
229 };
230
231 explicit RNTupleAttrEntryIterable(RNTupleAttrSetReader &reader, std::optional<RFilter> filter = {})
232 : fReader(&reader), fFilter(filter)
233 {
234 }
235
236 RIterator begin() { return RIterator{fReader->fEntryRanges.begin(), fReader->fEntryRanges.end(), fFilter}; }
237 RIterator end() { return RIterator{fReader->fEntryRanges.end(), fReader->fEntryRanges.end(), fFilter}; }
238};
239
240} // namespace Experimental
241} // namespace ROOT
242
243#endif
start
Definition Rotated.cxx:223
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
decltype(std::declval< RNTupleAttrSetReader >().fEntryRanges.begin()) Iter_t
RIterator(Iter_t iter, Iter_t end, std::optional< RFilter > filter)
Iterable class used to loop over attribute entries.
RNTupleAttrEntryIterable(RNTupleAttrSetReader &reader, std::optional< RFilter > filter={})
A range of main entries referred to by an attribute entry.
static RNTupleAttrRange FromStartLength(ROOT::NTupleSize_t start, ROOT::NTupleSize_t length)
std::optional< std::pair< ROOT::NTupleSize_t, ROOT::NTupleSize_t > > GetFirstLast() const
Returns the pair { firstEntryIdx, lastEntryIdx } (inclusive). Returns nullopt if the range has zero l...
static RNTupleAttrRange FromStartEnd(ROOT::NTupleSize_t start, ROOT::NTupleSize_t end)
Creates an AttributeRange from [start, end), where end is one past the last valid entry of the range ...
std::pair< ROOT::NTupleSize_t, ROOT::NTupleSize_t > GetStartLength() const
Returns the pair { start, length }.
RNTupleAttrRange(ROOT::NTupleSize_t start, ROOT::NTupleSize_t length)
ROOT::NTupleSize_t GetStart() const
Returns the beginning of the range.
std::optional< ROOT::NTupleSize_t > GetLast() const
Returns the last valid entry index in the range. Returns nullopt if the range has zero length.
std::optional< ROOT::NTupleSize_t > GetFirst() const
Returns the first valid entry index in the range. Returns nullopt if the range has zero length.
ROOT::NTupleSize_t GetEnd() const
Returns one past the last valid index of the range, equal to GetStart() + GetLength().
Class used to read a RNTupleAttrSet in the context of a RNTupleReader.
RNTupleAttrEntryIterable GetAttributesInRange(NTupleSize_t startEntry, NTupleSize_t endEntry)
Returns all the attributes whose range is fully contained in [startEntry, endEntry).
RNTupleAttrSetReader(RNTupleAttrSetReader &&)=default
std::unique_ptr< ROOT::RNTupleModel > fUserModel
The reconstructed user model.
RNTupleAttrSetReader(const RNTupleAttrSetReader &)=delete
const ROOT::RNTupleDescriptor & GetDescriptor() const
Returns the read-only descriptor of this attribute set.
std::unique_ptr< RNTupleReader > fReader
The internal Reader used to read the AttributeSet RNTuple.
RNTupleAttrSetReader & operator=(const RNTupleAttrSetReader &)=delete
RNTupleAttrSetReader(std::unique_ptr< RNTupleReader > reader, std::uint16_t vSchemaMajor)
RNTupleAttrEntryIterable GetAttributes()
Returns all the attributes in this Set. The returned attributes are sorted by entry range start.
std::vector< std::pair< RNTupleAttrRange, NTupleSize_t > > fEntryRanges
List containing pairs { entryRange, entryIndex }, used to quickly find out which entries in the Attri...
std::size_t GetNEntries() const
Returns the number of all attribute entries in this attribute set.
std::unique_ptr< REntry > CreateEntry()
Creates an entry suitable for use with LoadEntry.
RNTupleAttrEntryIterable GetAttributesContainingRange(NTupleSize_t startEntry, NTupleSize_t endEntry)
Returns all the attributes whose range fully contains [startEntry, endEntry).
const ROOT::RNTupleModel & GetModel() const
Returns the read-only model of this attribute set.
RNTupleAttrSetReader & operator=(RNTupleAttrSetReader &&)=default
RNTupleAttrRange LoadEntry(NTupleSize_t index)
Loads the attribute entry at position index into the default entry.
The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
Definition REntry.hxx:54
The on-storage metadata of an RNTuple.
The RNTupleModel encapulates the schema of an RNTuple.
Reads RNTuple data from storage.
Namespace for ROOT features in testing.
Definition TROOT.h:100
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.