Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleView.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleView.hxx
2/// \ingroup NTuple
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-05
5
6/*************************************************************************
7 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#ifndef ROOT_RNTupleView
15#define ROOT_RNTupleView
16
17#include <ROOT/RError.hxx>
18#include <ROOT/RField.hxx>
19#include <ROOT/RNTupleRange.hxx>
20#include <ROOT/RNTupleTypes.hxx>
21#include <ROOT/RNTupleUtils.hxx>
22#include <string_view>
23
24#include <iterator>
25#include <memory>
26#include <type_traits>
27#include <utility>
28#include <unordered_map>
29
30namespace ROOT {
31
32class RNTupleReader;
33
34namespace Internal {
35
36/// Helper to get the iteration space of the given field that needs to be connected to the given page source.
37/// The indexes are given by the number of elements of the principal column of the field or, if none exists,
38/// by the number of elements of the first principal column found in the subfields searched by BFS.
39/// If the field hierarchy is empty on columns, the returned field range is invalid (start and end set to
40/// kInvalidNTupleIndex). An attempt to use such a field range in RNTupleViewBase::GetFieldRange will throw.
42
43} // namespace Internal
44
45// clang-format off
46/**
47\class ROOT::RNTupleViewBase
48\ingroup NTuple
49\brief An RNTupleView provides read-only access to a single field of an RNTuple
50
51\tparam T The type of the object that will be read by the view; can be void if unknown at compile time.
52
53The view owns a field and its underlying columns in order to fill an RField::RValue object with data. Data can be
54accessed by index. For top-level fields, the index refers to the entry number. Fields that are part of
55nested collections have global index numbers that are derived from their parent indexes (\see GetFieldRange()).
56
57View can only be created by a reader or by a collection view.
58
59**Example: read an RNTuple's field with a view**
60~~~ {.cpp}
61auto reader = RNTupleReader::Open("myNtuple", "myntuple.root");
62auto viewFoo = reader->GetView<float>("foo");
63for (auto idx : reader->GetEntryRange()) {
64 float foo = viewFoo(idx); // read field "foo" of the `idx`-th entry
65 std::cout << foo << "\n";
66}
67~~~
68
69**Example: read an RNTuple's collection subfield with a view**
70~~~ {.cpp}
71auto reader = RNTupleReader::Open("myNtuple", "myntuple.root");
72// Assuming "v" is a std::vector<int>:
73auto view = reader->GetView<int>("v._0");
74// Effectively flattens all fields "v" in all entries and reads their elements.
75for (auto idx : view.GetFieldRange()) {
76 int x = view(idx);
77 std::cout << x << "\n";
78}
79~~~
80*/
81// clang-format on
82template <typename T>
84protected:
85 std::unique_ptr<ROOT::RFieldBase> fField;
88
89 static std::unique_ptr<ROOT::RFieldBase>
91 {
94 std::unique_ptr<ROOT::RFieldBase> field;
95 {
96 const auto &desc = pageSource.GetSharedDescriptorGuard().GetRef();
97 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
98 if constexpr (std::is_void_v<T>) {
99 if (typeName.empty())
100 field = fieldDesc.CreateField(desc);
101 else
102 field = ROOT::RFieldBase::Create(fieldDesc.GetFieldName(), std::string(typeName)).Unwrap();
103 } else {
104 field = std::make_unique<ROOT::RField<T>>(fieldDesc.GetFieldName());
105 }
106 }
107 field->SetOnDiskId(fieldId);
108 fieldZero.Attach(std::move(field));
110 return std::move(fieldZero.ReleaseSubfields()[0]);
111 }
112
113 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
114 : fField(std::move(field)), fFieldRange(range), fValue(fField->CreateValue())
115 {
116 }
117
118 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<T> objPtr)
119 : fField(std::move(field)), fFieldRange(range), fValue(fField->BindValue(objPtr))
120 {
121 }
122
123 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, T *rawPtr)
124 : fField(std::move(field)),
126 fValue(fField->BindValue(ROOT::Internal::MakeAliasedSharedPtr(rawPtr)))
127 {
128 }
129
130public:
135 ~RNTupleViewBase() = default;
136
137 const ROOT::RFieldBase &GetField() const { return *fField; }
139
140 const ROOT::RFieldBase::RValue &GetValue() const { return fValue; }
141 /// Returns the global field range of this view.
142 /// This may differ from the RNTuple's entry range in case of subfields and can be used to iterate
143 /// over all the concatenated elements of the subfield without caring which entry they belong to.
144 /// Throws an RException if the underlying field of this view is empty, i.e. if it's a class or
145 /// record field with no associated columns.
147 {
148 if (!fFieldRange.IsValid()) {
149 throw RException(R__FAIL("field iteration over empty fields is unsupported: " + fField->GetFieldName()));
150 }
151 return fFieldRange;
152 }
153
154 void Bind(std::shared_ptr<T> objPtr) { fValue.Bind(objPtr); }
157};
158
159// clang-format off
160/**
161\class ROOT::RNTupleView
162\ingroup NTuple
163\brief An RNTupleView for a known type. See RNTupleViewBase.
164*/
165// clang-format on
166template <typename T>
167class RNTupleView : public RNTupleViewBase<T> {
170
171protected:
172 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
173 : RNTupleViewBase<T>(std::move(field), range)
174 {
175 }
176
177 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<T> objPtr)
179 {
180 }
181
182 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, T *rawPtr)
184 {
185 }
186
187 const T &GetValueRef() const
188 {
189 // We created the RValue and know its type, avoid extra checks.
190 void *ptr = RNTupleViewBase<T>::fValue.template GetPtr<void>().get();
191 return *static_cast<T *>(ptr);
192 }
193
194public:
195 RNTupleView(const RNTupleView &other) = delete;
199 ~RNTupleView() = default;
200
201 /// Reads the value of this view for the entry with the provided `globalIndex`.
207
208 /// Reads the value of this view for the entry with the provided `localIndex`.
209 /// See RNTupleLocalIndex for more details.
215};
216
217// clang-format off
218/**
219\class ROOT::RNTupleView
220\ingroup NTuple
221\brief An RNTupleView that can be used when the type is unknown at compile time. See RNTupleViewBase.
222*/
223// clang-format on
224template <>
225class RNTupleView<void> final : public RNTupleViewBase<void> {
228
229protected:
230 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
231 : RNTupleViewBase<void>(std::move(field), range)
232 {
233 }
234
235 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<void> objPtr)
236 : RNTupleViewBase<void>(std::move(field), range, objPtr)
237 {
238 }
239
240 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, void *rawPtr)
241 : RNTupleViewBase<void>(std::move(field), range, rawPtr)
242 {
243 }
244
245public:
246 RNTupleView(const RNTupleView &other) = delete;
250 ~RNTupleView() = default;
251
252 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
254 /// \see RNTupleView::operator()(RNTupleLocalIndex)
256};
257
258// clang-format off
259/**
260\class ROOT::RNTupleDirectAccessView
261\ingroup NTuple
262\brief A view variant that provides direct access to the I/O buffers. Only works for mappable fields.
263*/
264// clang-format on
265template <typename T>
269
270protected:
273
275 {
276 const auto &desc = pageSource.GetSharedDescriptorGuard().GetRef();
277 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
278 if (!Internal::IsMatchingFieldType<T>(fieldDesc.GetTypeName())) {
279 throw RException(R__FAIL("type mismatch for field " + fieldDesc.GetFieldName() + ": " +
280 fieldDesc.GetTypeName() + " vs. " + ROOT::RField<T>::TypeName()));
281 }
282 ROOT::RField<T> field(fieldDesc.GetFieldName());
283 field.SetOnDiskId(fieldId);
285 return field;
286 }
287
292
293public:
299
300 const ROOT::RFieldBase &GetField() const { return fField; }
301 /// \see RNTupleView::GetFieldRange()
303
304 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
306 /// \see RNTupleView::operator()(RNTupleLocalIndex)
308};
309
310// clang-format off
311/**
312\class ROOT::RNTupleCollectionView
313\ingroup NTuple
314\brief A view for a collection, that can itself generate new ntuple views for its nested fields.
315*/
316// clang-format on
319
320private:
324
332
334 {
335 std::string fieldName;
336 {
337 const auto &desc = source->GetSharedDescriptorGuard().GetRef();
338 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
339 if (fieldDesc.GetStructure() != ROOT::ENTupleStructure::kCollection) {
340 throw RException(
341 R__FAIL("invalid attemt to create collection view on non-collection field " + fieldDesc.GetFieldName()));
342 }
343 fieldName = fieldDesc.GetFieldName();
344 }
346 }
347
349 {
351 auto fieldId = descGuard->FindFieldId(fieldName, fField.GetOnDiskId());
353 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in collection '" +
354 descGuard->GetQualifiedFieldName(fField.GetOnDiskId()) + "'"));
355 }
356 return fieldId;
357 }
358
359 std::uint64_t GetCardinalityValue() const
360 {
361 // We created the RValue and know its type, avoid extra checks.
362 void *ptr = fValue.GetPtr<void>().get();
363 return *static_cast<RNTupleCardinality<std::uint64_t> *>(ptr);
364 }
365
366public:
374 {
375 if (this == &other)
376 return *this;
377 std::swap(fSource, other.fSource);
378 std::swap(fField, other.fField);
379 fValue = fField.CreateValue();
380 return *this;
381 }
383
392
401
402 /// Provides access to an individual (sub)field.
403 ///
404 /// Raises an exception if there is no field with the given name.
405 ///
406 /// \sa ROOT::RNTupleReader::GetView(std::string_view)
407 template <typename T>
414
415 /// Provides direct access to the I/O buffers of a **mappable** (sub)field.
416 ///
417 /// Raises an exception if there is no field with the given name.
418 /// Attempting to access the values of a direct-access view for non-mappable fields will yield compilation errors.
419 ///
420 /// \sa ROOT::RNTupleReader::DirectAccessView(std::string_view)
421 template <typename T>
428
429 /// Provides access to a collection field, that can itself generate new RNTupleViews for its nested fields.
430 ///
431 /// Raises an exception if:
432 /// * there is no field with the given name or,
433 /// * the field is not a collection
434 ///
435 /// \sa ROOT::RNTupleReader::GetCollectionView(std::string_view)
440
441 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
447
448 /// \see RNTupleView::operator()(RNTupleLocalIndex)
450 {
452 return GetCardinalityValue();
453 }
454};
455
456} // namespace ROOT
457
458#endif
#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:300
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Abstract interface to read data from an ntuple.
const RSharedDescriptorGuard GetSharedDescriptorGuard() const
Takes the read lock for the descriptor.
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
Points to an array of objects with RNTuple I/O support, used for bulk reading.
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
void Read(ROOT::NTupleSize_t globalIndex)
void EmplaceNew()
Replace the current object pointer by a pointer to a new object constructed by the field.
void Bind(std::shared_ptr< void > objPtr)
std::shared_ptr< T > GetPtr() const
void BindRawPtr(void *rawPtr)
A field translates read and write calls from/to underlying columns to/from tree values.
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.
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:59
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:313
A view for a collection, that can itself generate new ntuple views for its nested fields.
ROOT::DescriptorId_t GetFieldId(std::string_view fieldName)
RNTupleCollectionView & operator=(const RNTupleCollectionView &other)=delete
std::uint64_t GetCardinalityValue() const
RNTupleView< T > GetView(std::string_view fieldName)
Provides access to an individual (sub)field.
RNTupleCollectionView(const RNTupleCollectionView &other)=delete
RNTupleCollectionView(ROOT::DescriptorId_t fieldId, const std::string &fieldName, ROOT::Internal::RPageSource *source)
ROOT::Internal::RPageSource * fSource
ROOT::RNTupleLocalRange GetCollectionRange(ROOT::NTupleSize_t globalIndex)
std::uint64_t operator()(ROOT::NTupleSize_t globalIndex)
RNTupleCollectionView GetCollectionView(std::string_view fieldName)
Provides access to a collection field, that can itself generate new RNTupleViews for its nested field...
ROOT::RField< RNTupleCardinality< std::uint64_t > > fField
RNTupleCollectionView & operator=(RNTupleCollectionView &&other)
ROOT::RFieldBase::RValue fValue
static RNTupleCollectionView Create(ROOT::DescriptorId_t fieldId, ROOT::Internal::RPageSource *source)
RNTupleDirectAccessView< T > GetDirectAccessView(std::string_view fieldName)
Provides direct access to the I/O buffers of a mappable (sub)field.
RNTupleCollectionView(RNTupleCollectionView &&other)
std::uint64_t operator()(RNTupleLocalIndex localIndex)
ROOT::RNTupleLocalRange GetCollectionRange(RNTupleLocalIndex localIndex)
A view variant that provides direct access to the I/O buffers.
ROOT::RNTupleGlobalRange GetFieldRange() const
RNTupleDirectAccessView & operator=(RNTupleDirectAccessView &&other)=default
RNTupleDirectAccessView(ROOT::RField< T > field, ROOT::RNTupleGlobalRange range)
RNTupleDirectAccessView & operator=(const RNTupleDirectAccessView &other)=delete
static ROOT::RField< T > CreateField(ROOT::DescriptorId_t fieldId, ROOT::Internal::RPageSource &pageSource)
ROOT::RNTupleGlobalRange fFieldRange
RNTupleDirectAccessView(const RNTupleDirectAccessView &other)=delete
const T & operator()(RNTupleLocalIndex localIndex)
const T & operator()(ROOT::NTupleSize_t globalIndex)
RNTupleDirectAccessView(RNTupleDirectAccessView &&other)=default
const ROOT::RFieldBase & GetField() const
Used to loop over indexes (entries or collections) between start and end.
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
Used to loop over entries of collections in a single cluster.
Reads RNTuple data from storage.
An RNTupleView provides read-only access to a single field of an RNTuple.
const ROOT::RFieldBase & GetField() const
const ROOT::RFieldBase::RValue & GetValue() const
void BindRawPtr(T *rawPtr)
RNTupleViewBase & operator=(const RNTupleViewBase &other)=delete
static std::unique_ptr< ROOT::RFieldBase > CreateField(ROOT::DescriptorId_t fieldId, Internal::RPageSource &pageSource, std::string_view typeName="")
std::unique_ptr< ROOT::RFieldBase > fField
RNTupleViewBase(RNTupleViewBase &&other)=default
ROOT::RNTupleGlobalRange GetFieldRange() const
Returns the global field range of this view.
ROOT::RFieldBase::RBulkValues CreateBulk()
ROOT::RNTupleGlobalRange fFieldRange
ROOT::RFieldBase::RValue fValue
RNTupleViewBase & operator=(RNTupleViewBase &&other)=default
~RNTupleViewBase()=default
RNTupleViewBase(const RNTupleViewBase &other)=delete
RNTupleViewBase(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range)
void Bind(std::shared_ptr< T > objPtr)
RNTupleViewBase(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range, std::shared_ptr< T > objPtr)
RNTupleViewBase(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range, T *rawPtr)
RNTupleView(const RNTupleView &other)=delete
void operator()(ROOT::NTupleSize_t globalIndex)
RNTupleView(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range, void *rawPtr)
RNTupleView & operator=(const RNTupleView &other)=delete
RNTupleView(RNTupleView &&other)=default
RNTupleView(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range)
void operator()(RNTupleLocalIndex localIndex)
RNTupleView & operator=(RNTupleView &&other)=default
RNTupleView(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range, std::shared_ptr< void > objPtr)
An RNTupleView for a known type.
RNTupleView & operator=(const RNTupleView &other)=delete
RNTupleView(RNTupleView &&other)=default
RNTupleView(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range, T *rawPtr)
RNTupleView(const RNTupleView &other)=delete
const T & GetValueRef() const
const T & operator()(RNTupleLocalIndex localIndex)
Reads the value of this view for the entry with the provided localIndex.
RNTupleView & operator=(RNTupleView &&other)=default
RNTupleView(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range)
const T & operator()(ROOT::NTupleSize_t globalIndex)
Reads the value of this view for the entry with the provided globalIndex.
RNTupleView(std::unique_ptr< ROOT::RFieldBase > field, ROOT::RNTupleGlobalRange range, std::shared_ptr< T > objPtr)
~RNTupleView()=default
void SetAllowFieldSubstitutions(RFieldZero &fieldZero, bool val)
Definition RField.cxx:34
void CallConnectPageSourceOnField(RFieldBase &, ROOT::Internal::RPageSource &)
ROOT::RNTupleGlobalRange GetFieldRange(const ROOT::RFieldBase &field, const ROOT::Internal::RPageSource &pageSource)
Helper to get the iteration space of the given field that needs to be connected to the given page sou...
auto MakeAliasedSharedPtr(T *rawPtr)
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId