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