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
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-09
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_RField_SequenceContainer
15#define ROOT_RField_SequenceContainer
16
17#ifndef ROOT_RField
18#error "Please include RField.hxx!"
19#endif
20
21#include <ROOT/RFieldBase.hxx>
22#include <ROOT/RNTupleUtil.hxx>
23#include <ROOT/RVec.hxx>
24
25#include <array>
26#include <memory>
27#include <vector>
28
29namespace ROOT {
30
31namespace Detail {
32class RFieldVisitor;
33} // namespace Detail
34
35////////////////////////////////////////////////////////////////////////////////
36/// Template specializations for C++ std::array and C-style arrays
37////////////////////////////////////////////////////////////////////////////////
38
39/// The generic field for fixed size arrays, which do not need an offset column
40class RArrayField : public RFieldBase {
41private:
42 class RArrayDeleter : public RDeleter {
43 private:
44 std::size_t fItemSize = 0;
45 std::size_t fArrayLength = 0;
46 std::unique_ptr<RDeleter> fItemDeleter;
47
48 public:
49 RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::unique_ptr<RDeleter> itemDeleter)
51 {
52 }
53 void operator()(void *objPtr, bool dtorOnly) final;
54 };
55
56 std::size_t fItemSize;
57 std::size_t fArrayLength;
58
59protected:
60 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
61
62 void ConstructValue(void *where) const final;
63 std::unique_ptr<RDeleter> GetDeleter() const final;
64
65 std::size_t AppendImpl(const void *from) final;
68
69public:
70 RArrayField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
74
76 size_t GetLength() const { return fArrayLength; }
78 size_t GetAlignment() const final { return fSubfields[0]->GetAlignment(); }
80};
81
82template <typename ItemT, std::size_t N>
83class RField<std::array<ItemT, N>> : public RArrayField {
84public:
85 static std::string TypeName() { return "std::array<" + RField<ItemT>::TypeName() + "," + std::to_string(N) + ">"; }
86 explicit RField(std::string_view name) : RArrayField(name, std::make_unique<RField<ItemT>>("_0"), N) {}
87 RField(RField &&other) = default;
88 RField &operator=(RField &&other) = default;
89 ~RField() override = default;
90};
91
92template <typename ItemT, std::size_t N>
93class RField<ItemT[N]> final : public RField<std::array<ItemT, N>> {
94public:
95 explicit RField(std::string_view name) : RField<std::array<ItemT, N>>(name) {}
96 RField(RField &&other) = default;
99};
100
101////////////////////////////////////////////////////////////////////////////////
102/// Template specializations for ROOT's RVec
103////////////////////////////////////////////////////////////////////////////////
104
105/// The type-erased field for a RVec<Type>
107public:
108 /// the RRVecDeleter is also used by RArrayAsRVecField and therefore declared public
109 class RRVecDeleter : public RDeleter {
110 private:
111 std::size_t fItemAlignment;
112 std::size_t fItemSize = 0;
113 std::unique_ptr<RDeleter> fItemDeleter;
114
115 public:
116 explicit RRVecDeleter(std::size_t itemAlignment) : fItemAlignment(itemAlignment) {}
117 RRVecDeleter(std::size_t itemAlignment, std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
118 : fItemAlignment(itemAlignment), fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
119 {
120 }
121 void operator()(void *objPtr, bool dtorOnly) final;
122 };
123
124 std::unique_ptr<RDeleter> fItemDeleter;
125
126protected:
127 std::size_t fItemSize;
129 std::size_t fValueSize;
130
131 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
133 void GenerateColumns() final;
135
136 void ConstructValue(void *where) const final;
137 std::unique_ptr<RDeleter> GetDeleter() const final;
138
139 std::size_t AppendImpl(const void *from) final;
142
143 void CommitClusterImpl() final { fNWritten = 0; }
144
145public:
146 RRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
147 RRVecField(RRVecField &&) = default;
149 RRVecField(const RRVecField &) = delete;
151 ~RRVecField() override = default;
152
153 std::vector<RValue> SplitValue(const RValue &value) const final;
154 size_t GetValueSize() const final;
155 size_t GetAlignment() const final;
157};
158
161public:
162 RField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
164 {
165 }
166
167 explicit RField(std::string_view name) : RField(name, std::make_unique<RField<ItemT>>("_0")) {}
168 RField(RField &&other) = default;
171
172 static std::string TypeName() { return "ROOT::VecOps::RVec<" + RField<ItemT>::TypeName() + ">"; }
173};
174
175////////////////////////////////////////////////////////////////////////////////
176/// Template specializations for C++ std::vector
177////////////////////////////////////////////////////////////////////////////////
178
179/// The generic field for a (nested) `std::vector<Type>` except for `std::vector<bool>`
180/// The field can be constructed as untyped collection through CreateUntyped().
181class RVectorField : public RFieldBase {
182private:
183 class RVectorDeleter : public RDeleter {
184 private:
185 std::size_t fItemSize = 0;
186 std::unique_ptr<RDeleter> fItemDeleter;
187
188 public:
189 RVectorDeleter() = default;
190 RVectorDeleter(std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
191 : fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
192 {
193 }
194 void operator()(void *objPtr, bool dtorOnly) final;
195 };
196
197 std::size_t fItemSize;
199 std::unique_ptr<RDeleter> fItemDeleter;
200
201protected:
202 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, bool isUntyped);
203
204 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
205
206 const RColumnRepresentations &GetColumnRepresentations() const final;
207 void GenerateColumns() final;
208 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
209
210 void ConstructValue(void *where) const final { new (where) std::vector<char>(); }
211 std::unique_ptr<RDeleter> GetDeleter() const final;
212
213 std::size_t AppendImpl(const void *from) final;
214 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
215
216 void CommitClusterImpl() final { fNWritten = 0; }
217
218public:
219 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
222 ~RVectorField() override = default;
223
224 static std::unique_ptr<RVectorField>
225 CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
226
227 std::vector<RValue> SplitValue(const RValue &value) const final;
228 size_t GetValueSize() const final { return sizeof(std::vector<char>); }
229 size_t GetAlignment() const final { return std::alignment_of<std::vector<char>>(); }
230 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
231};
232
233template <typename ItemT>
234class RField<std::vector<ItemT>> final : public RVectorField {
235public:
236 static std::string TypeName() { return "std::vector<" + RField<ItemT>::TypeName() + ">"; }
237 explicit RField(std::string_view name) : RVectorField(name, std::make_unique<RField<ItemT>>("_0")) {}
238 RField(RField &&other) = default;
239 RField &operator=(RField &&other) = default;
241};
242
243// `std::vector<bool>` is a template specialization and needs special treatment
244template <>
245class RField<std::vector<bool>> final : public RFieldBase {
246private:
247 ROOT::Internal::RColumnIndex fNWritten{0};
248
249protected:
250 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
251 {
252 return std::make_unique<RField>(newName);
253 }
254
255 const RColumnRepresentations &GetColumnRepresentations() const final;
256 void GenerateColumns() final;
257 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
258
259 void ConstructValue(void *where) const final { new (where) std::vector<bool>(); }
260 std::unique_ptr<RDeleter> GetDeleter() const final { return std::make_unique<RTypedDeleter<std::vector<bool>>>(); }
261
262 std::size_t AppendImpl(const void *from) final;
263 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
264
265 void CommitClusterImpl() final { fNWritten = 0; }
266
267public:
268 static std::string TypeName() { return "std::vector<bool>"; }
269 explicit RField(std::string_view name);
270 RField(RField &&other) = default;
271 RField &operator=(RField &&other) = default;
273
274 std::vector<RValue> SplitValue(const RValue &value) const final;
275
276 size_t GetValueSize() const final { return sizeof(std::vector<bool>); }
277 size_t GetAlignment() const final { return std::alignment_of<std::vector<bool>>(); }
279};
280
281////////////////////////////////////////////////////////////////////////////////
282/// Additional classes related to sequence containers
283////////////////////////////////////////////////////////////////////////////////
284
285/**
286\class ROOT::RArrayAsRVecField
287\brief A field for fixed-size arrays that are represented as RVecs in memory.
288\ingroup NTuple
289This class is used only for reading. In particular, it helps exposing
290arbitrarily-nested `std::array` on-disk fields as RVecs for usage in RDataFrame.
291*/
293private:
294 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
295 std::size_t fItemSize; /// The size of a child field's item
296 std::size_t fArrayLength; /// The length of the arrays in this field
297 std::size_t fValueSize; /// The size of a value of this field, i.e. an RVec
298
299protected:
300 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
301
302 void GenerateColumns() final { R__ASSERT(false && "RArrayAsRVec fields must only be used for reading"); }
303 using RFieldBase::GenerateColumns;
304
305 void ConstructValue(void *where) const final;
306 /// Returns an RRVecField::RRVecDeleter
307 std::unique_ptr<RDeleter> GetDeleter() const final;
308
309 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
310 void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final;
311
312public:
313 /**
314 Constructor of the field. The `itemField` argument represents the inner
315 item of the on-disk array, i.e. for an `std::array<float>` it is the `float`
316 field and not the `std::array` itself.
317 */
318 RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
324
325 std::size_t GetValueSize() const final { return fValueSize; }
326 std::size_t GetAlignment() const final;
327
328 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
329 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
330};
331
332} // namespace ROOT
333
334#endif
size_t fValueSize
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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.
The in-memory representation of a 32bit or 64bit on-disk index column.
Additional classes related to sequence containers.
std::unique_ptr< RDeleter > fItemDeleter
std::size_t fArrayLength
The size of a child field's item.
std::size_t fItemSize
Sub field deleter or nullptr for simple fields.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
std::size_t fValueSize
The length of the arrays in this field.
RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::unique_ptr< RDeleter > itemDeleter)
std::unique_ptr< RDeleter > fItemDeleter
void operator()(void *objPtr, bool dtorOnly) final
Template specializations for C++ std::array and C-style arrays.
std::unique_ptr< RDeleter > GetDeleter() const final
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
Definition RField.hxx:197
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
std::unique_ptr< RDeleter > GetDeleter() const final
Definition RField.hxx:182
The list of column representations a field can have.
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.
std::vector< std::unique_ptr< RFieldBase > > fSubfields
Collections and classes own subfields.
virtual const RColumnRepresentations & GetColumnRepresentations() const
Implementations in derived classes should return a static RColumnRepresentations object.
virtual void GenerateColumns()
Implementations in derived classes should create the backing columns corresponding to the field type ...
virtual void CommitClusterImpl()
RFieldBase(std::string_view name, std::string_view type, ROOT::ENTupleStructure structure, bool isSimple, std::size_t nRepetitions=0)
The constructor creates the underlying column objects and connects them to either a sink or a source.
virtual std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec)
General implementation of bulk read.
RField(RField &&other)=default
~RField() final=default
RField & operator=(RField &&other)=default
RField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField)
RField & operator=(RField &&other)=default
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:285
~RField() final=default
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:287
RField(std::string_view name)
Definition RField.hxx:288
The on-storage metadata of an RNTuple.
Addresses a column element or field item relative to a particular cluster, instead of a global 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)
std::unique_ptr< RDeleter > fItemDeleter
RRVecDeleter(std::size_t itemAlignment)
Template specializations for ROOT's RVec.
RRVecField & operator=(RRVecField &&)=default
~RRVecField() override=default
RRVecField(const RRVecField &)=delete
ROOT::Internal::RColumnIndex fNWritten
std::unique_ptr< RDeleter > fItemDeleter
RRVecField(RRVecField &&)=default
RRVecField & operator=(RRVecField &)=delete
RVectorDeleter(std::size_t itemSize, std::unique_ptr< RDeleter > itemDeleter)
Template specializations for C++ std::vector.
RVectorField(RVectorField &&other)=default
~RVectorField() override=default
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
std::unique_ptr< RDeleter > fItemDeleter
ROOT::Internal::RColumnIndex fNWritten
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
RVectorField & operator=(RVectorField &&other)=default
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1530
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
Input parameter to RFieldBase::ReadBulk() and RFieldBase::ReadBulkImpl().