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 {
92 std::unique_ptr<ROOT::RFieldBase> field;
93 {
94 const auto &desc = pageSource.GetSharedDescriptorGuard().GetRef();
95 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
96 if constexpr (std::is_void_v<T>) {
97 if (typeName.empty())
98 field = fieldDesc.CreateField(desc);
99 else
100 field = ROOT::RFieldBase::Create(fieldDesc.GetFieldName(), std::string(typeName)).Unwrap();
101 } else {
102 field = std::make_unique<ROOT::RField<T>>(fieldDesc.GetFieldName());
103 }
104 }
105 field->SetOnDiskId(fieldId);
107 return field;
108 }
109
110 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
111 : fField(std::move(field)), fFieldRange(range), fValue(fField->CreateValue())
112 {
113 }
114
115 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<T> objPtr)
116 : fField(std::move(field)), fFieldRange(range), fValue(fField->BindValue(objPtr))
117 {
118 }
119
120 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, T *rawPtr)
121 : fField(std::move(field)),
123 fValue(fField->BindValue(ROOT::Internal::MakeAliasedSharedPtr(rawPtr)))
124 {
125 }
126
127public:
132 ~RNTupleViewBase() = default;
133
134 const ROOT::RFieldBase &GetField() const { return *fField; }
136
137 const ROOT::RFieldBase::RValue &GetValue() const { return fValue; }
138 /// Returns the global field range of this view.
139 /// This may differ from the RNTuple's entry range in case of subfields and can be used to iterate
140 /// over all the concatenated elements of the subfield without caring which entry they belong to.
141 /// Throws an RException if the underlying field of this view is empty, i.e. if it's a class or
142 /// record field with no associated columns.
144 {
145 if (!fFieldRange.IsValid()) {
146 throw RException(R__FAIL("field iteration over empty fields is unsupported: " + fField->GetFieldName()));
147 }
148 return fFieldRange;
149 }
150
151 void Bind(std::shared_ptr<T> objPtr) { fValue.Bind(objPtr); }
154};
155
156// clang-format off
157/**
158\class ROOT::RNTupleView
159\ingroup NTuple
160\brief An RNTupleView for a known type. See RNTupleViewBase.
161*/
162// clang-format on
163template <typename T>
164class RNTupleView : public RNTupleViewBase<T> {
167
168protected:
169 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
170 : RNTupleViewBase<T>(std::move(field), range)
171 {
172 }
173
174 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<T> objPtr)
176 {
177 }
178
179 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, T *rawPtr)
181 {
182 }
183
184 const T &GetValueRef() const
185 {
186 // We created the RValue and know its type, avoid extra checks.
187 void *ptr = RNTupleViewBase<T>::fValue.template GetPtr<void>().get();
188 return *static_cast<T *>(ptr);
189 }
190
191public:
192 RNTupleView(const RNTupleView &other) = delete;
196 ~RNTupleView() = default;
197
198 /// Reads the value of this view for the entry with the provided `globalIndex`.
204
205 /// Reads the value of this view for the entry with the provided `localIndex`.
206 /// See RNTupleLocalIndex for more details.
212};
213
214// clang-format off
215/**
216\class ROOT::RNTupleView
217\ingroup NTuple
218\brief An RNTupleView that can be used when the type is unknown at compile time. See RNTupleViewBase.
219*/
220// clang-format on
221template <>
222class RNTupleView<void> final : public RNTupleViewBase<void> {
225
226protected:
227 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
228 : RNTupleViewBase<void>(std::move(field), range)
229 {
230 }
231
232 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<void> objPtr)
233 : RNTupleViewBase<void>(std::move(field), range, objPtr)
234 {
235 }
236
237 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, void *rawPtr)
238 : RNTupleViewBase<void>(std::move(field), range, rawPtr)
239 {
240 }
241
242public:
243 RNTupleView(const RNTupleView &other) = delete;
247 ~RNTupleView() = default;
248
249 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
251 /// \see RNTupleView::operator()(RNTupleLocalIndex)
253};
254
255// clang-format off
256/**
257\class ROOT::RNTupleDirectAccessView
258\ingroup NTuple
259\brief A view variant that provides direct access to the I/O buffers. Only works for mappable fields.
260*/
261// clang-format on
262template <typename T>
266
267protected:
270
272 {
273 const auto &desc = pageSource.GetSharedDescriptorGuard().GetRef();
274 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
275 if (!Internal::IsMatchingFieldType<T>(fieldDesc.GetTypeName())) {
276 throw RException(R__FAIL("type mismatch for field " + fieldDesc.GetFieldName() + ": " +
277 fieldDesc.GetTypeName() + " vs. " + ROOT::RField<T>::TypeName()));
278 }
279 ROOT::RField<T> field(fieldDesc.GetFieldName());
280 field.SetOnDiskId(fieldId);
282 return field;
283 }
284
289
290public:
296
297 const ROOT::RFieldBase &GetField() const { return fField; }
298 /// \see RNTupleView::GetFieldRange()
300
301 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
303 /// \see RNTupleView::operator()(RNTupleLocalIndex)
305};
306
307// clang-format off
308/**
309\class ROOT::RNTupleCollectionView
310\ingroup NTuple
311\brief A view for a collection, that can itself generate new ntuple views for its nested fields.
312*/
313// clang-format on
316
317private:
321
329
331 {
332 std::string fieldName;
333 {
334 const auto &desc = source->GetSharedDescriptorGuard().GetRef();
335 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
336 if (fieldDesc.GetStructure() != ROOT::ENTupleStructure::kCollection) {
337 throw RException(
338 R__FAIL("invalid attemt to create collection view on non-collection field " + fieldDesc.GetFieldName()));
339 }
340 fieldName = fieldDesc.GetFieldName();
341 }
343 }
344
346 {
348 auto fieldId = descGuard->FindFieldId(fieldName, fField.GetOnDiskId());
350 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in collection '" +
351 descGuard->GetQualifiedFieldName(fField.GetOnDiskId()) + "'"));
352 }
353 return fieldId;
354 }
355
356 std::uint64_t GetCardinalityValue() const
357 {
358 // We created the RValue and know its type, avoid extra checks.
359 void *ptr = fValue.GetPtr<void>().get();
360 return *static_cast<RNTupleCardinality<std::uint64_t> *>(ptr);
361 }
362
363public:
369
378
387
388 /// Provides access to an individual (sub)field.
389 ///
390 /// Raises an exception if there is no field with the given name.
391 ///
392 /// \sa ROOT::RNTupleReader::GetView(std::string_view)
393 template <typename T>
400
401 /// Provides direct access to the I/O buffers of a **mappable** (sub)field.
402 ///
403 /// Raises an exception if there is no field with the given name.
404 /// Attempting to access the values of a direct-access view for non-mappable fields will yield compilation errors.
405 ///
406 /// \sa ROOT::RNTupleReader::DirectAccessView(std::string_view)
407 template <typename T>
414
415 /// Provides access to a collection field, that can itself generate new RNTupleViews for its nested fields.
416 ///
417 /// Raises an exception if:
418 /// * there is no field with the given name or,
419 /// * the field is not a collection
420 ///
421 /// \sa ROOT::RNTupleReader::GetCollectionView(std::string_view)
426
427 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
433
434 /// \see RNTupleView::operator()(RNTupleLocalIndex)
436 {
438 return GetCardinalityValue();
439 }
440};
441
442} // namespace ROOT
443
444#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.
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:287
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
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.
std::uint64_t operator()(RNTupleLocalIndex localIndex)
ROOT::RNTupleLocalRange GetCollectionRange(RNTupleLocalIndex localIndex)
RNTupleCollectionView(RNTupleCollectionView &&other)=default
RNTupleCollectionView & operator=(RNTupleCollectionView &&other)=default
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 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)
Namespace for new ROOT classes and functions.
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