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