Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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/RNTupleUtil.hxx>
21#include <string_view>
22
23#include <iterator>
24#include <memory>
25#include <type_traits>
26#include <utility>
27#include <unordered_map>
28
29namespace ROOT {
30
31class RNTupleReader;
32
33namespace Internal {
34
35/// Helper to get the iteration space of the given field that needs to be connected to the given page source.
36/// The indexes are given by the number of elements of the principal column of the field or, if none exists,
37/// by the number of elements of the first principal column found in the subfields searched by BFS.
38/// If the field hierarchy is empty on columns, the returned field range is invalid (start and end set to
39/// kInvalidNTupleIndex). An attempt to use such a field range in RNTupleViewBase::GetFieldRange will throw.
41
42} // namespace Internal
43
44// clang-format off
45/**
46\class ROOT::RNTupleViewBase
47\ingroup NTuple
48\brief An RNTupleView provides read-only access to a single field of an RNTuple
49
50\tparam T The type of the object that will be read by the view; can be void if unknown at compile time.
51
52The view owns a field and its underlying columns in order to fill an RField::RValue object with data. Data can be
53accessed by index. For top-level fields, the index refers to the entry number. Fields that are part of
54nested collections have global index numbers that are derived from their parent indexes (\see GetFieldRange()).
55
56View can only be created by a reader or by a collection view.
57
58**Example: read an RNTuple's field with a view**
59~~~ {.cpp}
60auto reader = RNTupleReader::Open("myNtuple", "myntuple.root");
61auto viewFoo = reader->GetView<float>("foo");
62for (auto idx : reader->GetEntryRange()) {
63 float foo = viewFoo(idx); // read field "foo" of the `idx`-th entry
64 std::cout << foo << "\n";
65}
66~~~
67
68**Example: read an RNTuple's collection subfield with a view**
69~~~ {.cpp}
70auto reader = RNTupleReader::Open("myNtuple", "myntuple.root");
71// Assuming "v" is a std::vector<int>:
72auto view = reader->GetView<int>("v._0");
73// Effectively flattens all fields "v" in all entries and reads their elements.
74for (auto idx : view.GetFieldRange()) {
75 int x = view(idx);
76 std::cout << x << "\n";
77}
78~~~
79*/
80// clang-format on
81template <typename T>
83protected:
84 std::unique_ptr<ROOT::RFieldBase> fField;
87
88 static std::unique_ptr<ROOT::RFieldBase>
90 {
91 std::unique_ptr<ROOT::RFieldBase> field;
92 {
93 const auto &desc = pageSource.GetSharedDescriptorGuard().GetRef();
94 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
95 if constexpr (std::is_void_v<T>) {
96 if (typeName.empty())
97 field = fieldDesc.CreateField(desc);
98 else
99 field = ROOT::RFieldBase::Create(fieldDesc.GetFieldName(), std::string(typeName)).Unwrap();
100 } else {
101 field = std::make_unique<ROOT::RField<T>>(fieldDesc.GetFieldName());
102 }
103 }
104 field->SetOnDiskId(fieldId);
106 return field;
107 }
108
109 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
110 : fField(std::move(field)), fFieldRange(range), fValue(fField->CreateValue())
111 {
112 }
113
114 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<T> objPtr)
115 : fField(std::move(field)), fFieldRange(range), fValue(fField->BindValue(objPtr))
116 {
117 }
118
119 RNTupleViewBase(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, T *rawPtr)
120 : fField(std::move(field)),
122 fValue(fField->BindValue(ROOT::Internal::MakeAliasedSharedPtr(rawPtr)))
123 {
124 }
125
126public:
131 ~RNTupleViewBase() = default;
132
133 const ROOT::RFieldBase &GetField() const { return *fField; }
134 ROOT::RFieldBase::RBulk CreateBulk() { return fField->CreateBulk(); }
135
136 const ROOT::RFieldBase::RValue &GetValue() const { return fValue; }
137 /// Returns the global field range of this view.
138 /// This may differ from the RNTuple's entry range in case of subfields and can be used to iterate
139 /// over all the concatenated elements of the subfield without caring which entry they belong to.
140 /// Throws an RException if the underlying field of this view is empty, i.e. if it's a class or
141 /// record field with no associated columns.
143 {
144 if (!fFieldRange.IsValid()) {
145 throw RException(R__FAIL("field iteration over empty fields is unsupported: " + fField->GetFieldName()));
146 }
147 return fFieldRange;
148 }
149
150 void Bind(std::shared_ptr<T> objPtr) { fValue.Bind(objPtr); }
153};
154
155// clang-format off
156/**
157\class ROOT::RNTupleView
158\ingroup NTuple
159\brief An RNTupleView for a known type. See RNTupleViewBase.
160*/
161// clang-format on
162template <typename T>
163class RNTupleView : public RNTupleViewBase<T> {
166
167protected:
168 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
169 : RNTupleViewBase<T>(std::move(field), range)
170 {
171 }
172
173 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<T> objPtr)
175 {
176 }
177
178 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, T *rawPtr)
180 {
181 }
182
183public:
184 RNTupleView(const RNTupleView &other) = delete;
188 ~RNTupleView() = default;
189
190 /// Reads the value of this view for the entry with the provided `globalIndex`.
196
197 /// Reads the value of this view for the entry with the provided `localIndex`.
198 /// See RNTupleLocalIndex for more details.
204};
205
206// clang-format off
207/**
208\class ROOT::RNTupleView
209\ingroup NTuple
210\brief An RNTupleView that can be used when the type is unknown at compile time. See RNTupleViewBase.
211*/
212// clang-format on
213template <>
214class RNTupleView<void> final : public RNTupleViewBase<void> {
217
218protected:
219 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range)
220 : RNTupleViewBase<void>(std::move(field), range)
221 {
222 }
223
224 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, std::shared_ptr<void> objPtr)
225 : RNTupleViewBase<void>(std::move(field), range, objPtr)
226 {
227 }
228
229 RNTupleView(std::unique_ptr<ROOT::RFieldBase> field, ROOT::RNTupleGlobalRange range, void *rawPtr)
230 : RNTupleViewBase<void>(std::move(field), range, rawPtr)
231 {
232 }
233
234public:
235 RNTupleView(const RNTupleView &other) = delete;
239 ~RNTupleView() = default;
240
241 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
243 /// \see RNTupleView::operator()(RNTupleLocalIndex)
245};
246
247// clang-format off
248/**
249\class ROOT::RNTupleDirectAccessView
250\ingroup NTuple
251\brief A view variant that provides direct access to the I/O buffers. Only works for mappable fields.
252*/
253// clang-format on
254template <typename T>
258
259protected:
262
264 {
265 const auto &desc = pageSource.GetSharedDescriptorGuard().GetRef();
266 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
267 if (fieldDesc.GetTypeName() != ROOT::RField<T>::TypeName()) {
268 throw RException(R__FAIL("type mismatch for field " + fieldDesc.GetFieldName() + ": " +
269 fieldDesc.GetTypeName() + " vs. " + ROOT::RField<T>::TypeName()));
270 }
271 ROOT::RField<T> field(fieldDesc.GetFieldName());
272 field.SetOnDiskId(fieldId);
274 return field;
275 }
276
281
282public:
288
289 const ROOT::RFieldBase &GetField() const { return fField; }
290 /// \see RNTupleView::GetFieldRange()
292
293 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
295 /// \see RNTupleView::operator()(RNTupleLocalIndex)
297};
298
299// clang-format off
300/**
301\class ROOT::RNTupleCollectionView
302\ingroup NTuple
303\brief A view for a collection, that can itself generate new ntuple views for its nested fields.
304*/
305// clang-format on
308
309private:
313
321
323 {
324 std::string fieldName;
325 {
326 const auto &desc = source->GetSharedDescriptorGuard().GetRef();
327 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
328 if (fieldDesc.GetStructure() != ROOT::ENTupleStructure::kCollection) {
329 throw RException(
330 R__FAIL("invalid attemt to create collection view on non-collection field " + fieldDesc.GetFieldName()));
331 }
332 fieldName = fieldDesc.GetFieldName();
333 }
335 }
336
338 {
340 auto fieldId = descGuard->FindFieldId(fieldName, fField.GetOnDiskId());
342 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in collection '" +
343 descGuard->GetQualifiedFieldName(fField.GetOnDiskId()) + "'"));
344 }
345 return fieldId;
346 }
347
348public:
354
363
372
373 /// Provides access to an individual (sub)field.
374 ///
375 /// Raises an exception if there is no field with the given name.
376 ///
377 /// \sa ROOT::RNTupleReader::GetView(std::string_view)
378 template <typename T>
385
386 /// Provides direct access to the I/O buffers of a **mappable** (sub)field.
387 ///
388 /// Raises an exception if there is no field with the given name.
389 /// Attempting to access the values of a direct-access view for non-mappable fields will yield compilation errors.
390 ///
391 /// \sa ROOT::RNTupleReader::DirectAccessView(std::string_view)
392 template <typename T>
399
400 /// Provides access to a collection field, that can itself generate new RNTupleViews for its nested fields.
401 ///
402 /// Raises an exception if:
403 /// * there is no field with the given name or,
404 /// * the field is not a collection
405 ///
406 /// \sa ROOT::RNTupleReader::GetCollectionView(std::string_view)
411
412 /// \see RNTupleView::operator()(ROOT::NTupleSize_t)
414 {
416 return fValue.GetRef<std::uint64_t>();
417 }
418
419 /// \see RNTupleView::operator()(RNTupleLocalIndex)
421 {
423 return fValue.GetRef<std::uint64_t>();
424 }
425};
426
427} // namespace ROOT
428
429#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:299
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)
void BindRawPtr(void *rawPtr)
const T & GetRef() const
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:283
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
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::RBulk 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 & 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)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
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