Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldSequenceContainer.hxx
Go to the documentation of this file.
1/// \file ROOT/RField/SequenceContainer.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-09
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_RField_SequenceContainer
17#define ROOT7_RField_SequenceContainer
18
19#ifndef ROOT7_RField
20#error "Please include RField.hxx!"
21#endif
22
23#include <ROOT/RFieldBase.hxx>
24#include <ROOT/RNTupleUtil.hxx>
25#include <ROOT/RVec.hxx>
26
27#include <array>
28#include <memory>
29#include <vector>
30
31namespace ROOT {
32namespace Experimental {
33
34namespace Detail {
35class RFieldVisitor;
36} // namespace Detail
37
38////////////////////////////////////////////////////////////////////////////////
39/// Template specializations for C++ std::array and C-style arrays
40////////////////////////////////////////////////////////////////////////////////
41
42/// The generic field for fixed size arrays, which do not need an offset column
43class RArrayField : public RFieldBase {
44private:
45 class RArrayDeleter : public RDeleter {
46 private:
47 std::size_t fItemSize = 0;
48 std::size_t fArrayLength = 0;
49 std::unique_ptr<RDeleter> fItemDeleter;
50
51 public:
52 RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::unique_ptr<RDeleter> itemDeleter)
53 : fItemSize(itemSize), fArrayLength(arrayLength), fItemDeleter(std::move(itemDeleter))
54 {
55 }
56 void operator()(void *objPtr, bool dtorOnly) final;
57 };
58
59 std::size_t fItemSize;
60 std::size_t fArrayLength;
61
62protected:
63 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
64
65 void ConstructValue(void *where) const final;
66 std::unique_ptr<RDeleter> GetDeleter() const final;
67
68 std::size_t AppendImpl(const void *from) final;
69 void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final;
70 void ReadInClusterImpl(RClusterIndex clusterIndex, void *to) final;
71
72public:
73 RArrayField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
74 RArrayField(RArrayField &&other) = default;
75 RArrayField &operator=(RArrayField &&other) = default;
76 ~RArrayField() override = default;
77
78 std::vector<RValue> SplitValue(const RValue &value) const final;
79 size_t GetLength() const { return fArrayLength; }
80 size_t GetValueSize() const final { return fItemSize * fArrayLength; }
81 size_t GetAlignment() const final { return fSubFields[0]->GetAlignment(); }
82 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
83};
84
85template <typename ItemT, std::size_t N>
86class RField<std::array<ItemT, N>> : public RArrayField {
87public:
88 static std::string TypeName() { return "std::array<" + RField<ItemT>::TypeName() + "," + std::to_string(N) + ">"; }
89 explicit RField(std::string_view name) : RArrayField(name, std::make_unique<RField<ItemT>>("_0"), N) {}
90 RField(RField &&other) = default;
91 RField &operator=(RField &&other) = default;
92 ~RField() override = default;
93};
94
95template <typename ItemT, std::size_t N>
96class RField<ItemT[N]> final : public RField<std::array<ItemT, N>> {
97public:
98 explicit RField(std::string_view name) : RField<std::array<ItemT, N>>(name) {}
99 RField(RField &&other) = default;
100 RField &operator=(RField &&other) = default;
101 ~RField() final = default;
102};
103
104////////////////////////////////////////////////////////////////////////////////
105/// Template specializations for ROOT's RVec
106////////////////////////////////////////////////////////////////////////////////
107
108/// The type-erased field for a RVec<Type>
109class RRVecField : public RFieldBase {
110public:
111 /// the RRVecDeleter is also used by RArrayAsRVecField and therefore declared public
112 class RRVecDeleter : public RDeleter {
113 private:
114 std::size_t fItemAlignment;
115 std::size_t fItemSize = 0;
116 std::unique_ptr<RDeleter> fItemDeleter;
117
118 public:
119 explicit RRVecDeleter(std::size_t itemAlignment) : fItemAlignment(itemAlignment) {}
120 RRVecDeleter(std::size_t itemAlignment, std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
121 : fItemAlignment(itemAlignment), fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
122 {
123 }
124 void operator()(void *objPtr, bool dtorOnly) final;
125 };
126
127 std::unique_ptr<RDeleter> fItemDeleter;
128
129protected:
130 std::size_t fItemSize;
132 std::size_t fValueSize;
133
134 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
136 void GenerateColumns() final;
137 void GenerateColumns(const RNTupleDescriptor &desc) final;
138
139 void ConstructValue(void *where) const final;
140 std::unique_ptr<RDeleter> GetDeleter() const final;
141
142 std::size_t AppendImpl(const void *from) final;
143 void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final;
144 std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec) final;
145
146 void CommitClusterImpl() final { fNWritten = 0; }
147
148public:
149 RRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
150 RRVecField(RRVecField &&) = default;
152 RRVecField(const RRVecField &) = delete;
154 ~RRVecField() override = default;
155
156 std::vector<RValue> SplitValue(const RValue &value) const final;
157 size_t GetValueSize() const final;
158 size_t GetAlignment() const final;
159 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
160 void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
161 {
162 fPrincipalColumn->GetCollectionInfo(globalIndex, collectionStart, size);
163 }
164 void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
165 {
166 fPrincipalColumn->GetCollectionInfo(clusterIndex, collectionStart, size);
167 }
168};
169
170template <typename ItemT>
171class RField<ROOT::VecOps::RVec<ItemT>> final : public RRVecField {
172public:
173 RField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
174 : RRVecField(fieldName, std::move(itemField))
175 {
176 }
177
178 explicit RField(std::string_view name) : RField(name, std::make_unique<RField<ItemT>>("_0")) {}
179 RField(RField &&other) = default;
180 RField &operator=(RField &&other) = default;
181 ~RField() final = default;
182
183 static std::string TypeName() { return "ROOT::VecOps::RVec<" + RField<ItemT>::TypeName() + ">"; }
184};
185
186////////////////////////////////////////////////////////////////////////////////
187/// Template specializations for C++ std::vector
188////////////////////////////////////////////////////////////////////////////////
189
190/// The generic field for a (nested) std::vector<Type> except for std::vector<bool>
191/// The field can be constructed as untyped collection through CreateUntyped().
192class RVectorField : public RFieldBase {
193private:
194 class RVectorDeleter : public RDeleter {
195 private:
196 std::size_t fItemSize = 0;
197 std::unique_ptr<RDeleter> fItemDeleter;
198
199 public:
200 RVectorDeleter() = default;
201 RVectorDeleter(std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
202 : fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
203 {
204 }
205 void operator()(void *objPtr, bool dtorOnly) final;
206 };
207
208 std::size_t fItemSize;
210 std::unique_ptr<RDeleter> fItemDeleter;
211
212protected:
213 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, bool isUntyped);
214
215 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
216
217 const RColumnRepresentations &GetColumnRepresentations() const final;
218 void GenerateColumns() final;
219 void GenerateColumns(const RNTupleDescriptor &desc) final;
220
221 void ConstructValue(void *where) const final { new (where) std::vector<char>(); }
222 std::unique_ptr<RDeleter> GetDeleter() const final;
223
224 std::size_t AppendImpl(const void *from) final;
225 void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final;
226
227 void CommitClusterImpl() final { fNWritten = 0; }
228
229public:
230 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
231 RVectorField(RVectorField &&other) = default;
233 ~RVectorField() override = default;
234
235 static std::unique_ptr<RVectorField>
236 CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
237
238 std::vector<RValue> SplitValue(const RValue &value) const final;
239 size_t GetValueSize() const final { return sizeof(std::vector<char>); }
240 size_t GetAlignment() const final { return std::alignment_of<std::vector<char>>(); }
241 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
242 void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
243 {
244 fPrincipalColumn->GetCollectionInfo(globalIndex, collectionStart, size);
245 }
246 void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
247 {
248 fPrincipalColumn->GetCollectionInfo(clusterIndex, collectionStart, size);
249 }
250};
251
252template <typename ItemT>
253class RField<std::vector<ItemT>> final : public RVectorField {
254public:
255 static std::string TypeName() { return "std::vector<" + RField<ItemT>::TypeName() + ">"; }
256 explicit RField(std::string_view name) : RVectorField(name, std::make_unique<RField<ItemT>>("_0")) {}
257 RField(RField &&other) = default;
258 RField &operator=(RField &&other) = default;
259 ~RField() final = default;
260};
261
262// std::vector<bool> is a template specialization and needs special treatment
263template <>
264class RField<std::vector<bool>> final : public RFieldBase {
265private:
266 ClusterSize_t fNWritten{0};
267
268protected:
269 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
270 {
271 return std::make_unique<RField>(newName);
272 }
273
274 const RColumnRepresentations &GetColumnRepresentations() const final;
275 void GenerateColumns() final;
276 void GenerateColumns(const RNTupleDescriptor &desc) final;
277
278 void ConstructValue(void *where) const final { new (where) std::vector<bool>(); }
279 std::unique_ptr<RDeleter> GetDeleter() const final { return std::make_unique<RTypedDeleter<std::vector<bool>>>(); }
280
281 std::size_t AppendImpl(const void *from) final;
282 void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final;
283
284 void CommitClusterImpl() final { fNWritten = 0; }
285
286public:
287 static std::string TypeName() { return "std::vector<bool>"; }
288 explicit RField(std::string_view name);
289 RField(RField &&other) = default;
290 RField &operator=(RField &&other) = default;
291 ~RField() final = default;
292
293 std::vector<RValue> SplitValue(const RValue &value) const final;
294
295 size_t GetValueSize() const final { return sizeof(std::vector<bool>); }
296 size_t GetAlignment() const final { return std::alignment_of<std::vector<bool>>(); }
297 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
298 void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
299 {
300 fPrincipalColumn->GetCollectionInfo(globalIndex, collectionStart, size);
301 }
302 void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
303 {
304 fPrincipalColumn->GetCollectionInfo(clusterIndex, collectionStart, size);
305 }
306};
307
308////////////////////////////////////////////////////////////////////////////////
309/// Additional classes related to sequence containers
310////////////////////////////////////////////////////////////////////////////////
311
312/**
313\class ROOT::Experimental::RArrayAsRVecField
314\brief A field for fixed-size arrays that are represented as RVecs in memory.
315\ingroup NTuple
316This class is used only for reading. In particular, it helps exposing
317arbitrarily-nested std::array on-disk fields as RVecs for usage in RDataFrame.
318*/
319class RArrayAsRVecField final : public RFieldBase {
320private:
321 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
322 std::size_t fItemSize; /// The size of a child field's item
323 std::size_t fArrayLength; /// The length of the arrays in this field
324 std::size_t fValueSize; /// The size of a value of this field, i.e. an RVec
325
326protected:
327 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
328
329 void GenerateColumns() final { R__ASSERT(false && "RArrayAsRVec fields must only be used for reading"); }
330 using RFieldBase::GenerateColumns;
331
332 void ConstructValue(void *where) const final;
333 /// Returns an RRVecField::RRVecDeleter
334 std::unique_ptr<RDeleter> GetDeleter() const final;
335
336 void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final;
337 void ReadInClusterImpl(RClusterIndex clusterIndex, void *to) final;
338
339public:
340 /**
341 Constructor of the field. the \p itemField argument represents the inner
342 item of the on-disk array, i.e. for an `std::array<float>` it is the `float`
343 field and not the `std::array` itself.
344 */
345 RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
346 RArrayAsRVecField(const RArrayAsRVecField &other) = delete;
347 RArrayAsRVecField &operator=(const RArrayAsRVecField &other) = delete;
349 RArrayAsRVecField &operator=(RArrayAsRVecField &&other) = default;
350 ~RArrayAsRVecField() final = default;
351
352 std::size_t GetValueSize() const final { return fValueSize; }
353 std::size_t GetAlignment() const final;
354
355 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
356 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
357};
358
359} // namespace Experimental
360} // namespace ROOT
361
362#endif
size_t fValueSize
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
TRObject operator()(const T1 &t1) const
Abstract base class for classes implementing the visitor design pattern.
void GetCollectionInfo(const NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *collectionSize)
For offset columns only, look at the two adjacent values that define a collection's coordinates.
Definition RColumn.hxx:275
Additional classes related to sequence containers.
std::size_t fArrayLength
The size of a child field's item.
std::size_t fValueSize
The length of the arrays in this field.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponsing to the field type ...
std::size_t fItemSize
Sub field deleter or nullptr for simple fields.
void operator()(void *objPtr, bool dtorOnly) final
Definition RField.cxx:3176
RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::unique_ptr< RDeleter > itemDeleter)
Template specializations for C++ std::array and C-style arrays.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given a value for this field.
Definition RField.cxx:3194
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
Definition RField.cxx:3165
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
Definition RField.cxx:3138
void AcceptVisitor(Detail::RFieldVisitor &visitor) const final
Definition RField.cxx:3206
void ReadInClusterImpl(RClusterIndex clusterIndex, void *to) final
Definition RField.cxx:3156
std::unique_ptr< RDeleter > GetDeleter() const final
Definition RField.cxx:3186
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
Definition RField.cxx:3132
void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final
Definition RField.cxx:3148
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
Definition RField.cxx:1981
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
Definition RField.cxx:1907
void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final
Definition RField.cxx:1916
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
Definition RField.cxx:1902
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
Definition RField.cxx:1957
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
Definition RField.hxx:153
void AcceptVisitor(Detail::RFieldVisitor &visitor) const final
Definition RField.cxx:1996
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given a value for this field.
Definition RField.cxx:1969
std::unique_ptr< RDeleter > GetDeleter() const final
Definition RField.hxx:138
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
Some fields have multiple possible column representations, e.g.
A functor to release the memory acquired by CreateValue (memory and constructor).
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
A field translates read and write calls from/to underlying columns to/from tree values.
virtual void GenerateColumns()
Implementations in derived classes should create the backing columns corresponsing to the field type ...
virtual std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec)
General implementation of bulk read.
Definition RField.cxx:941
std::vector< std::unique_ptr< RFieldBase > > fSubFields
Collections and classes own sub fields.
Internal::RColumn * fPrincipalColumn
All fields that have columns have a distinct main column.
virtual const RColumnRepresentations & GetColumnRepresentations() const
Implementations in derived classes should return a static RColumnRepresentations object.
Definition RField.cxx:908
RField & operator=(RField &&other)=default
RField(RField &&other)=default
RField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField)
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:241
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:243
The on-storage meta-data of an ntuple.
the RRVecDeleter is also used by RArrayAsRVecField and therefore declared public
RRVecDeleter(std::size_t itemAlignment, std::size_t itemSize, std::unique_ptr< RDeleter > itemDeleter)
Template specializations for ROOT's RVec.
void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
std::unique_ptr< RDeleter > fItemDeleter
RRVecField(RRVecField &&)=default
~RRVecField() override=default
RRVecField(const RRVecField &)=delete
RRVecField & operator=(RRVecField &&)=default
RRVecField & operator=(RRVecField &)=delete
RVectorDeleter(std::size_t itemSize, std::unique_ptr< RDeleter > itemDeleter)
Template specializations for C++ std::vector.
~RVectorField() override=default
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
RVectorField & operator=(RVectorField &&other)=default
void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
RVectorField(RVectorField &&other)=default
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1529
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
RClusterSize ClusterSize_t
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Wrap the integer in a struct in order to avoid template specialization clash with std::uint64_t.