Logo ROOT   6.18/05
Reference Guide
RNTupleView.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleView.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-05
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/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RNTupleView
17#define ROOT7_RNTupleView
18
19#include <ROOT/RField.hxx>
20#include <ROOT/RNTupleUtil.hxx>
21#include <ROOT/RStringView.hxx>
22
23#include <iterator>
24#include <memory>
25#include <utility>
26
27namespace ROOT {
28namespace Experimental {
29
30
31// clang-format off
32/**
33\class ROOT::Experimental::RNTupleViewRange
34\ingroup NTuple
35\brief Used to loop over indexes (entries or collections) between start and end
36*/
37// clang-format on
39private:
42public:
43 class RIterator : public std::iterator<std::forward_iterator_tag, NTupleSize_t> {
44 private:
47 public:
48 RIterator() = default;
49 explicit RIterator(NTupleSize_t index) : fIndex(index) {}
50 ~RIterator() = default;
51
52 iterator operator++(int) /* postfix */ { auto r = *this; fIndex++; return r; }
53 iterator& operator++() /* prefix */ { fIndex++; return *this; }
54 reference operator* () { return fIndex; }
55 pointer operator->() { return &fIndex; }
56 bool operator==(const iterator& rh) const { return fIndex == rh.fIndex; }
57 bool operator!=(const iterator& rh) const { return fIndex != rh.fIndex; }
58 };
59
62 RIterator end() { return RIterator(fEnd); }
63};
64
65
66// clang-format off
67/**
68\class ROOT::Experimental::RNTupleView
69\ingroup NTuple
70\brief An RNTupleView provides read-only access to a single field of the ntuple
71
72(NB(jblomer): The ntuple view is very close to TTreeReader. Do we simply want to teach TTreeReader to deal with
73RNTuple?)
74
75The view owns a field and its underlying columns in order to fill an ntuple value object with data. Data can be
76accessed by index. For top level fields, the index refers to the entry number. Fields that are part of
77nested collections have global index numbers that are derived from their parent indexes.
78
79The RNTupleView object is an iterable. That means, all field values in the tree can be sequentially read from begin()
80to end().
81
82For simple types, template specializations let the reading become a pure mapping into a page buffer.
83*/
84// clang-format on
85template <typename T>
87 friend class RNTupleReader;
89
90protected:
94 : fField(fieldName), fValue(fField.GenerateValue())
95 {
96 fField.ConnectColumns(pageSource);
97 for (auto& f : fField) {
98 f.ConnectColumns(pageSource);
99 }
100 }
101
102public:
103 RNTupleView(const RNTupleView& other) = delete;
104 RNTupleView(RNTupleView&& other) = default;
105 RNTupleView& operator=(const RNTupleView& other) = delete;
106 RNTupleView& operator=(RNTupleView&& other) = default;
107 ~RNTupleView() { fField.DestroyValue(fValue); }
108
109 const T& operator()(NTupleSize_t index) {
110 fField.Read(index, &fValue);
111 return *fValue.Get<T>();
112 }
113};
114
115// Template specializations in order to directly map simple types into the page pool
116
117template <>
118class RNTupleView<float> {
119 friend class RNTupleReader;
121
122protected:
124 RNTupleView(std::string_view fieldName, Detail::RPageSource* pageSource) : fField(fieldName) {
125 fField.ConnectColumns(pageSource);
126 }
127
128public:
129 RNTupleView(const RNTupleView& other) = delete;
130 RNTupleView(RNTupleView&& other) = default;
131 RNTupleView& operator=(const RNTupleView& other) = delete;
132 RNTupleView& operator=(RNTupleView&& other) = default;
133 ~RNTupleView() = default;
134
135 float operator()(NTupleSize_t index) { return *fField.Map(index); }
136};
137
138
139// clang-format off
140/**
141\class ROOT::Experimental::RNTupleViewCollection
142\ingroup NTuple
143\brief A view for a collection, that can itself generate new ntuple views for its nested fields.
144*/
145// clang-format on
146class RNTupleViewCollection : public RNTupleView<ClusterSize_t> {
147 friend class RNTupleReader;
148
149private:
150 std::string fCollectionName;
152
154 : RNTupleView<ClusterSize_t>(fieldName, source)
155 , fCollectionName(fieldName)
156 , fSource(source)
157 {}
158
160 std::string prefix(fCollectionName);
162 return prefix + std::string(name);
163 }
164
165public:
171
173 ClusterSize_t size;
174 NTupleSize_t idxStart;
175 fField.GetCollectionInfo(index, &idxStart, &size);
176 return RNTupleViewRange(idxStart, idxStart + size);
177 }
178 template <typename T>
181 return RNTupleViewCollection(GetSubName(fieldName), fSource);
182 }
183
185 ClusterSize_t size;
186 NTupleSize_t idxStart;
187 fField.GetCollectionInfo(index, &idxStart, &size);
188 return size;
189 }
190};
191
192} // namespace Experimental
193} // namespace ROOT
194
195#endif
ROOT::R::TRInterface & r
Definition: Object.C:4
#define f(i)
Definition: RSha256.hxx:104
char name[80]
Definition: TGX11.cxx:109
static constexpr char kCollectionSeparator
Field names convey the level of subfields; sub fields (nested collections) are separated by a dot.
Definition: RField.hxx:100
Abstract interface to read data from a tree.
void GetCollectionInfo(NTupleSize_t index, NTupleSize_t *idxStart, ClusterSize_t *size)
Special help for offset fields.
Definition: RField.hxx:385
Classes with dictionaries that can be inspected by TClass.
Definition: RField.hxx:294
An RNTuple that is used to read data from storage.
Definition: RNTuple.hxx:94
A view for a collection, that can itself generate new ntuple views for its nested fields.
RNTupleViewCollection GetViewCollection(std::string_view fieldName)
std::string GetSubName(std::string_view name)
RNTupleViewRange GetViewRange(NTupleSize_t index)
RNTupleViewCollection(RNTupleViewCollection &&other)=default
RNTupleViewCollection & operator=(RNTupleViewCollection &&other)=default
RNTupleViewCollection & operator=(const RNTupleViewCollection &other)=delete
ClusterSize_t operator()(NTupleSize_t index)
RNTupleView< T > GetView(std::string_view fieldName)
RNTupleViewCollection(const RNTupleViewCollection &other)=delete
RNTupleViewCollection(std::string_view fieldName, Detail::RPageSource *source)
bool operator!=(const iterator &rh) const
Definition: RNTupleView.hxx:57
bool operator==(const iterator &rh) const
Definition: RNTupleView.hxx:56
Used to loop over indexes (entries or collections) between start and end.
Definition: RNTupleView.hxx:38
RNTupleViewRange(NTupleSize_t start, NTupleSize_t end)
Definition: RNTupleView.hxx:60
float operator()(NTupleSize_t index)
RNTupleView(const RNTupleView &other)=delete
RNTupleView(RNTupleView &&other)=default
RNTupleView(std::string_view fieldName, Detail::RPageSource *pageSource)
RNTupleView & operator=(RNTupleView &&other)=default
RNTupleView & operator=(const RNTupleView &other)=delete
An RNTupleView provides read-only access to a single field of the ntuple.
Definition: RNTupleView.hxx:86
Detail::RFieldValue fValue
Definition: RNTupleView.hxx:92
RNTupleView & operator=(RNTupleView &&other)=default
RNTupleView & operator=(const RNTupleView &other)=delete
const T & operator()(NTupleSize_t index)
RNTupleView(RNTupleView &&other)=default
RNTupleView(const RNTupleView &other)=delete
RNTupleView(std::string_view fieldName, Detail::RPageSource *pageSource)
Definition: RNTupleView.hxx:93
basic_string_view< char > string_view
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
Definition: RNTupleUtil.hxx:44
constexpr NTupleSize_t kInvalidNTupleIndex
Definition: RNTupleUtil.hxx:45
double T(double x)
Definition: ChebyshevPol.h:34
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
Wrap the 32bit integer in a struct in order to avoid template specialization clash with std::uint32_t...
Definition: RNTupleUtil.hxx:47