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/RNTupleTypes.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
35namespace Internal {
36std::unique_ptr<RFieldBase> CreateEmulatedVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
37 std::string_view emulatedFromType);
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Template specializations for C++ std::array and C-style arrays
42////////////////////////////////////////////////////////////////////////////////
43
44/// The generic field for fixed size arrays, which do not need an offset column
45class RArrayField : public RFieldBase {
46private:
47 class RArrayDeleter : public RDeleter {
48 private:
49 std::size_t fItemSize = 0;
50 std::size_t fArrayLength = 0;
51 std::unique_ptr<RDeleter> fItemDeleter;
52
53 public:
54 RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::unique_ptr<RDeleter> itemDeleter)
56 {
57 }
58 void operator()(void *objPtr, bool dtorOnly) final;
59 };
60
61 std::size_t fItemSize;
62 std::size_t fArrayLength;
63
64protected:
65 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
66
67 void ConstructValue(void *where) const final;
68 std::unique_ptr<RDeleter> GetDeleter() const final;
69
70 std::size_t AppendImpl(const void *from) final;
74
76
77public:
78 RArrayField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
82
84 size_t GetLength() const { return fArrayLength; }
86 size_t GetAlignment() const final { return fSubfields[0]->GetAlignment(); }
88};
89
90template <typename ItemT, std::size_t N>
91class RField<std::array<ItemT, N>> : public RArrayField {
92public:
93 static std::string TypeName() { return "std::array<" + RField<ItemT>::TypeName() + "," + std::to_string(N) + ">"; }
94 explicit RField(std::string_view name) : RArrayField(name, std::make_unique<RField<ItemT>>("_0"), N) {}
95 RField(RField &&other) = default;
96 RField &operator=(RField &&other) = default;
97 ~RField() override = default;
98};
99
100template <typename ItemT, std::size_t N>
101class RField<ItemT[N]> final : public RField<std::array<ItemT, N>> {
102public:
103 explicit RField(std::string_view name) : RField<std::array<ItemT, N>>(name) {}
104 RField(RField &&other) = default;
107};
108
109////////////////////////////////////////////////////////////////////////////////
110/// Template specializations for ROOT's RVec
111////////////////////////////////////////////////////////////////////////////////
112
113/// The type-erased field for a RVec<Type>
115 friend class RArrayAsRVecField; // to use the RRVecDeleter and to call ResizeRVec()
116
117 // Ensures that the RVec pointed to by rvec has at least nItems valid elements
118 // Returns the possibly new "begin pointer" of the RVec, i.e. the pointer to the data area.
119 static unsigned char *
120 ResizeRVec(void *rvec, std::size_t nItems, std::size_t itemSize, const RFieldBase *itemField, RDeleter *itemDeleter);
121
122 class RRVecDeleter : public RDeleter {
123 private:
124 std::size_t fItemAlignment;
125 std::size_t fItemSize = 0;
126 std::unique_ptr<RDeleter> fItemDeleter;
127
128 public:
129 explicit RRVecDeleter(std::size_t itemAlignment) : fItemAlignment(itemAlignment) {}
130 RRVecDeleter(std::size_t itemAlignment, std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
131 : fItemAlignment(itemAlignment), fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
132 {
133 }
134 void operator()(void *objPtr, bool dtorOnly) final;
135 };
136
137 std::unique_ptr<RDeleter> fItemDeleter;
138
139protected:
140 std::size_t fItemSize;
142 std::size_t fValueSize;
143
144 // For bulk read optimzation
145 std::size_t fBulkNRepetition = 1;
146 /// May be a direct PoD subfield or a sub-subfield of a fixed-size array of PoD
147 RFieldBase *fBulkSubfield = nullptr;
148
149 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
151 void GenerateColumns() final;
153
154 void ConstructValue(void *where) const final;
155 std::unique_ptr<RDeleter> GetDeleter() const final;
156
157 std::size_t AppendImpl(const void *from) final;
160
163
164 void CommitClusterImpl() final { fNWritten = 0; }
165
166public:
167 RRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
168 RRVecField(RRVecField &&) = default;
170 RRVecField(const RRVecField &) = delete;
172 ~RRVecField() override = default;
173
174 std::vector<RValue> SplitValue(const RValue &value) const final;
175 size_t GetValueSize() const final;
176 size_t GetAlignment() const final;
178};
179
182public:
183 RField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
185 {
186 }
187
188 explicit RField(std::string_view name) : RField(name, std::make_unique<RField<ItemT>>("_0")) {}
189 RField(RField &&other) = default;
192
193 static std::string TypeName() { return "ROOT::VecOps::RVec<" + RField<ItemT>::TypeName() + ">"; }
194};
195
196////////////////////////////////////////////////////////////////////////////////
197/// Template specializations for C++ std::vector
198////////////////////////////////////////////////////////////////////////////////
199
200/// The generic field for a (nested) `std::vector<Type>` except for `std::vector<bool>`
201/// The field can be constructed as untyped collection through CreateUntyped().
202class RVectorField : public RFieldBase {
203 friend std::unique_ptr<RFieldBase> Internal::CreateEmulatedVectorField(std::string_view fieldName,
204 std::unique_ptr<RFieldBase> itemField,
205 std::string_view emulatedFromType);
206
207 class RVectorDeleter : public RDeleter {
208 private:
209 std::size_t fItemSize = 0;
210 std::unique_ptr<RDeleter> fItemDeleter;
211
212 public:
213 RVectorDeleter() = default;
214 RVectorDeleter(std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
215 : fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
216 {
217 }
218 void operator()(void *objPtr, bool dtorOnly) final;
219 };
220
221 std::size_t fItemSize;
223 std::unique_ptr<RDeleter> fItemDeleter;
224
225protected:
226 /// Creates a possibly-untyped VectorField.
227 /// If `emulatedFromType` is not nullopt, the field is untyped. If the string is empty, it is a "regular"
228 /// untyped vector field; otherwise, it was created as an emulated field from the given type name.
229 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
230 std::optional<std::string_view> emulatedFromType);
231
232 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
233
234 const RColumnRepresentations &GetColumnRepresentations() const final;
235 void GenerateColumns() final;
236 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
237
238 void ConstructValue(void *where) const final { new (where) std::vector<char>(); }
239 std::unique_ptr<RDeleter> GetDeleter() const final;
240
241 std::size_t AppendImpl(const void *from) final;
242 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
243
244 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
245
246 void CommitClusterImpl() final { fNWritten = 0; }
247
248public:
249 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
252 ~RVectorField() override = default;
253
254 static std::unique_ptr<RVectorField>
255 CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
256
257 std::vector<RValue> SplitValue(const RValue &value) const final;
258 size_t GetValueSize() const final { return sizeof(std::vector<char>); }
259 size_t GetAlignment() const final { return std::alignment_of<std::vector<char>>(); }
260 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
261};
262
263template <typename ItemT>
264class RField<std::vector<ItemT>> final : public RVectorField {
265public:
266 static std::string TypeName() { return "std::vector<" + RField<ItemT>::TypeName() + ">"; }
267 explicit RField(std::string_view name) : RVectorField(name, std::make_unique<RField<ItemT>>("_0")) {}
268 RField(RField &&other) = default;
269 RField &operator=(RField &&other) = default;
271};
272
273// `std::vector<bool>` is a template specialization and needs special treatment
274template <>
275class RField<std::vector<bool>> final : public RFieldBase {
276private:
277 ROOT::Internal::RColumnIndex fNWritten{0};
278
279protected:
280 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
281 {
282 return std::make_unique<RField>(newName);
283 }
284
285 const RColumnRepresentations &GetColumnRepresentations() const final;
286 void GenerateColumns() final;
287 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
288
289 void ConstructValue(void *where) const final { new (where) std::vector<bool>(); }
290 std::unique_ptr<RDeleter> GetDeleter() const final { return std::make_unique<RTypedDeleter<std::vector<bool>>>(); }
291
292 std::size_t AppendImpl(const void *from) final;
293 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
294
295 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
296
297 void CommitClusterImpl() final { fNWritten = 0; }
298
299public:
300 static std::string TypeName() { return "std::vector<bool>"; }
301 explicit RField(std::string_view name);
302 RField(RField &&other) = default;
303 RField &operator=(RField &&other) = default;
305
306 std::vector<RValue> SplitValue(const RValue &value) const final;
307
308 size_t GetValueSize() const final { return sizeof(std::vector<bool>); }
309 size_t GetAlignment() const final { return std::alignment_of<std::vector<bool>>(); }
311};
312
313////////////////////////////////////////////////////////////////////////////////
314/// Additional classes related to sequence containers
315////////////////////////////////////////////////////////////////////////////////
316
317/**
318\class ROOT::RArrayAsRVecField
319\brief A field for fixed-size arrays that are represented as RVecs in memory.
320\ingroup NTuple
321This class is used only for reading. In particular, it helps exposing
322arbitrarily-nested `std::array` on-disk fields as RVecs for usage in RDataFrame.
323*/
325private:
326 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
327 std::size_t fItemSize; /// The size of a child field's item
328 std::size_t fArrayLength; /// The length of the arrays in this field
329 std::size_t fValueSize; /// The size of a value of this field, i.e. an RVec
330
331protected:
332 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
333
334 void GenerateColumns() final { throw RException(R__FAIL("RArrayAsRVec fields must only be used for reading")); }
335 using RFieldBase::GenerateColumns;
336
337 void ConstructValue(void *where) const final;
338 /// Returns an RRVecField::RRVecDeleter
339 std::unique_ptr<RDeleter> GetDeleter() const final;
340
341 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
342 void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final;
343
344 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
345
346public:
347 /**
348 Constructor of the field. The `itemField` argument represents the inner
349 item of the on-disk array, i.e. for an `std::array<float>` it is the `float`
350 field and not the `std::array` itself.
351 */
352 RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
358
359 std::size_t GetValueSize() const final { return fValueSize; }
360 std::size_t GetAlignment() const final;
361
362 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
363 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
364};
365
366} // namespace ROOT
367
368#endif
size_t fValueSize
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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.
Abstract interface to read data from an ntuple.
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.
std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec) final
General implementation of bulk read.
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...
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
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< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
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.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
Definition RField.hxx:222
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:205
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
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:313
~RField() final=default
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:315
RField(std::string_view name)
Definition RField.hxx:316
The on-storage metadata of an RNTuple.
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
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:1526
std::unique_ptr< RFieldBase > CreateEmulatedVectorField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, std::string_view emulatedFromType)
Definition RField.cxx:586
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().