Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldSequenceContainer.hxx
Go to the documentation of this file.
1/// \file ROOT/RField/SequenceContainer.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2018-10-09
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT_RField_SequenceContainer
14#define ROOT_RField_SequenceContainer
15
16#ifndef ROOT_RField
17#error "Please include RField.hxx!"
18#endif
19
20#include <ROOT/RFieldBase.hxx>
21#include <ROOT/RNTupleTypes.hxx>
22#include <ROOT/RVec.hxx>
23
24#include <array>
25#include <memory>
26#include <vector>
27
28namespace ROOT {
29
30namespace Detail {
31class RFieldVisitor;
32} // namespace Detail
33
34namespace Experimental {
35class RSoAField;
36}
37
38namespace Internal {
39std::unique_ptr<RFieldBase> CreateEmulatedVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
40 std::string_view emulatedFromType);
41}
42
43////////////////////////////////////////////////////////////////////////////////
44/// Template specializations for C++ std::array and C-style arrays
45////////////////////////////////////////////////////////////////////////////////
46
47/// The generic field for fixed size arrays, which do not need an offset column
48class RArrayField : public RFieldBase {
49private:
50 class RArrayDeleter : public RDeleter {
51 private:
52 std::size_t fItemSize = 0;
53 std::size_t fArrayLength = 0;
54 std::unique_ptr<RDeleter> fItemDeleter;
55
56 public:
57 RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::size_t alignment,
58 std::unique_ptr<RDeleter> itemDeleter)
60 {
61 }
62 void operator()(void *objPtr, bool dtorOnly) final;
63 };
64
65 std::size_t fItemSize;
66 std::size_t fArrayLength;
67
68protected:
69 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
70
71 void ConstructValue(void *where) const final;
72 std::unique_ptr<RDeleter> GetDeleter() const final;
73
74 std::size_t AppendImpl(const void *from) final;
78
80
81public:
82 RArrayField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
86
88 size_t GetLength() const { return fArrayLength; }
90 size_t GetAlignment() const final { return fSubfields[0]->GetAlignment(); }
92};
93
94template <typename ItemT, std::size_t N>
95class RField<std::array<ItemT, N>> : public RArrayField {
96public:
97 static std::string TypeName() { return "std::array<" + RField<ItemT>::TypeName() + "," + std::to_string(N) + ">"; }
98 explicit RField(std::string_view name) : RArrayField(name, std::make_unique<RField<ItemT>>("_0"), N) {}
99 RField(RField &&other) = default;
100 RField &operator=(RField &&other) = default;
101 ~RField() override = default;
102};
103
104template <typename ItemT, std::size_t N>
105class RField<ItemT[N]> final : public RField<std::array<ItemT, N>> {
106public:
107 explicit RField(std::string_view name) : RField<std::array<ItemT, N>>(name) {}
108 RField(RField &&other) = default;
111};
112
113////////////////////////////////////////////////////////////////////////////////
114/// Template specializations for ROOT's RVec
115////////////////////////////////////////////////////////////////////////////////
116
117/// The type-erased field for a RVec<Type>
119 friend class RArrayAsRVecField; // to use the RRVecDeleter and to call ResizeRVec()
120 friend class ROOT::Experimental::RSoAField; // to call ResizeRVec()
121
122 // Ensures that the RVec pointed to by rvec has at least nItems valid elements
123 // Returns the possibly new "begin pointer" of the RVec, i.e. the pointer to the data area.
124 static unsigned char *
125 ResizeRVec(void *rvec, std::size_t nItems, std::size_t itemSize, const RFieldBase *itemField, RDeleter *itemDeleter);
126
127 class RRVecDeleter : public RDeleter {
128 private:
129 std::size_t fItemAlignment;
130 std::size_t fItemSize = 0;
131 std::unique_ptr<RDeleter> fItemDeleter;
132
133 public:
134 explicit RRVecDeleter(std::size_t itemAlignment)
135 : RDeleter(ROOT::Internal::EvalRVecAlignment(itemAlignment)), fItemAlignment(itemAlignment)
136 {
137 }
138 RRVecDeleter(std::size_t itemAlignment, std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
140 fItemAlignment(itemAlignment),
141 fItemSize(itemSize),
142 fItemDeleter(std::move(itemDeleter))
143 {
144 }
145 void operator()(void *objPtr, bool dtorOnly) final;
146 };
147
148 std::unique_ptr<RDeleter> fItemDeleter;
149
150protected:
151 std::size_t fItemSize;
153 std::size_t fValueSize;
154
155 // For bulk read optimzation
156 std::size_t fBulkNRepetition = 1;
157 /// May be a direct PoD subfield or a sub-subfield of a fixed-size array of PoD
158 RFieldBase *fBulkSubfield = nullptr;
159
160 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
162 void GenerateColumns() final;
164
165 void ConstructValue(void *where) const final;
166 std::unique_ptr<RDeleter> GetDeleter() const final;
167
168 std::size_t AppendImpl(const void *from) final;
171
174
175 void CommitClusterImpl() final { fNWritten = 0; }
176
177public:
178 RRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
179 RRVecField(RRVecField &&) = default;
181 RRVecField(const RRVecField &) = delete;
183 ~RRVecField() override = default;
184
185 std::vector<RValue> SplitValue(const RValue &value) const final;
186 size_t GetValueSize() const final;
187 size_t GetAlignment() const final;
189};
190
193public:
194 RField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
196 {
197 }
198
199 explicit RField(std::string_view name) : RField(name, std::make_unique<RField<ItemT>>("_0")) {}
200 RField(RField &&other) = default;
203
204 static std::string TypeName() { return "ROOT::VecOps::RVec<" + RField<ItemT>::TypeName() + ">"; }
205};
206
207////////////////////////////////////////////////////////////////////////////////
208/// Template specializations for C++ std::vector
209////////////////////////////////////////////////////////////////////////////////
210
211/// The generic field for a (nested) `std::vector<Type>` except for `std::vector<bool>`
212/// The field can be constructed as untyped collection through CreateUntyped().
213class RVectorField : public RFieldBase {
214 friend class RArrayAsVectorField; // to get access to the RVectorDeleter
215 friend std::unique_ptr<RFieldBase> Internal::CreateEmulatedVectorField(std::string_view fieldName,
216 std::unique_ptr<RFieldBase> itemField,
217 std::string_view emulatedFromType);
218
219 class RVectorDeleter : public RDeleter {
220 private:
221 std::size_t fItemSize = 0;
222 std::size_t fItemAlignment = 0;
223 std::unique_ptr<RDeleter> fItemDeleter;
224
225 public:
226 explicit RVectorDeleter(std::size_t itemAlignment);
227 RVectorDeleter(std::size_t itemSize, std::size_t itemAlignment, std::unique_ptr<RDeleter> itemDeleter);
228 void operator()(void *objPtr, bool dtorOnly) final;
229 };
230
231 std::size_t fItemSize;
233 std::unique_ptr<RDeleter> fItemDeleter;
234
235 // Ensures that the std::vector pointed to by vec has at least nItems valid elements.
236 static void ResizeVector(void *vec, std::size_t nItems, std::size_t itemSize, const RFieldBase &itemField,
238
239protected:
240 /// Creates a possibly-untyped VectorField.
241 /// If `emulatedFromType` is not nullopt, the field is untyped. If the string is empty, it is a "regular"
242 /// untyped vector field; otherwise, it was created as an emulated field from the given type name.
243 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
244 std::optional<std::string_view> emulatedFromType);
245
246 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
247
248 const RColumnRepresentations &GetColumnRepresentations() const final;
249 void GenerateColumns() final;
250 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
251
252 void ConstructValue(void *where) const final;
253 std::unique_ptr<RDeleter> GetDeleter() const final;
254
255 std::size_t AppendImpl(const void *from) final;
256 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
257
258 std::unique_ptr<RFieldBase> BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final;
259 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
260
261 void CommitClusterImpl() final { fNWritten = 0; }
262
263public:
264 /// Maximum alignment of the vector's value type
265 static constexpr std::size_t kMaxItemAlignment = 4096;
266
267 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
270 ~RVectorField() override = default;
271
272 static std::unique_ptr<RVectorField>
273 CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
274
275 std::vector<RValue> SplitValue(const RValue &value) const final;
276 std::size_t GetValueSize() const final;
277 std::size_t GetAlignment() const final;
278 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
279};
280
282class RField<std::vector<ItemT>> final : public RVectorField {
283public:
284 static std::string TypeName() { return "std::vector<" + RField<ItemT>::TypeName() + ">"; }
285 explicit RField(std::string_view name) : RVectorField(name, std::make_unique<RField<ItemT>>("_0")) {}
286 RField(RField &&other) = default;
287 RField &operator=(RField &&other) = default;
288 ~RField() final = default;
289};
290
291// `std::vector<bool>` is a template specialization and needs special treatment
292template <>
293class RField<std::vector<bool>> final : public RFieldBase {
294private:
295 ROOT::Internal::RColumnIndex fNWritten{0};
296 /// If schema-evolved from an std::array, fOnDiskNRepetition is > 0 and there will be no
297 /// principal column.
298 std::size_t fOnDiskNRepetitions = 0;
299
300protected:
301 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
302 {
303 return std::make_unique<RField>(newName);
304 }
305
306 const RColumnRepresentations &GetColumnRepresentations() const final;
307 void GenerateColumns() final;
308 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
309
310 void ConstructValue(void *where) const final { new (where) std::vector<bool>(); }
311 std::unique_ptr<RDeleter> GetDeleter() const final { return std::make_unique<RTypedDeleter<std::vector<bool>>>(); }
312
313 std::size_t AppendImpl(const void *from) final;
314 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
315 void ReadInClusterImpl(ROOT::RNTupleLocalIndex localIndex, void *to) final;
316
317 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
318
319 void CommitClusterImpl() final { fNWritten = 0; }
320
321public:
322 static std::string TypeName() { return "std::vector<bool>"; }
323 explicit RField(std::string_view name);
324 RField(RField &&other) = default;
325 RField &operator=(RField &&other) = default;
326 ~RField() final = default;
327
328 std::vector<RValue> SplitValue(const RValue &value) const final;
329
330 std::size_t GetValueSize() const final { return sizeof(std::vector<bool>); }
331 std::size_t GetAlignment() const final { return alignof(std::vector<bool>); }
332 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
333};
334
335////////////////////////////////////////////////////////////////////////////////
336/// Additional classes related to sequence containers
337////////////////////////////////////////////////////////////////////////////////
338
339/**
340\class ROOT::RArrayAsRVecField
341\brief A field for fixed-size arrays that are represented as RVecs in memory.
342\ingroup NTuple
343This class is used only for reading. In particular, it helps exposing
344arbitrarily-nested `std::array` on-disk fields as RVecs for usage in RDataFrame.
345*/
347private:
348 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
349 std::size_t fItemSize; /// The size of a child field's item
350 std::size_t fArrayLength; /// The length of the arrays in this field
351 std::size_t fValueSize; /// The size of a value of this field, i.e. an RVec
352
353protected:
354 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
355
356 void GenerateColumns() final { throw RException(R__FAIL("RArrayAsRVec fields must only be used for reading")); }
357 using RFieldBase::GenerateColumns;
358
359 void ConstructValue(void *where) const final;
360 /// Returns an RRVecField::RRVecDeleter
361 std::unique_ptr<RDeleter> GetDeleter() const final;
362
363 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
364 void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final;
365
366 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
367
368public:
369 /**
370 Constructor of the field. The `itemField` argument represents the inner
371 item of the on-disk array, i.e. for an `std::array<float>` it is the `float`
372 field and not the `std::array` itself.
373 */
374 RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
380
381 std::size_t GetValueSize() const final { return fValueSize; }
382 std::size_t GetAlignment() const final;
383
384 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
385 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
386};
387
388/**
389\class ROOT::RArrayAsVectorField
390\brief A field for fixed-size arrays that are represented as std::vector in memory.
391\ingroup NTuple
392This class is used only for reading. In particular, it helps for schema evolution of fixed-size arrays into vectors.
393*/
395private:
396 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
397 std::size_t fItemSize; /// The size of a child field's item
398 std::size_t fArrayLength; /// The length of the arrays in this field
399
400protected:
401 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
402
403 void GenerateColumns() final;
404 using RFieldBase::GenerateColumns;
405
406 void ConstructValue(void *where) const final;
407 /// Returns an RVectorField::RVectorDeleter
408 std::unique_ptr<RDeleter> GetDeleter() const final;
409
410 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
411 void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final;
412
413 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
414
415public:
416 /// The `itemField` argument represents the inner item of the on-disk array,
417 /// i.e. for an `std::array<float>` it is the `float`
418 RArrayAsVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
424
425 std::size_t GetValueSize() const final;
426 std::size_t GetAlignment() const final;
427
428 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
429 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
430};
431
432} // namespace ROOT
433
434#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:299
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:148
TCanvas * alignment()
Definition alignment.C:1
Abstract base class for classes implementing the visitor design pattern.
The SoA field provides I/O for an in-memory SoA layout linked to an on-disk collection of the underly...
Definition RFieldSoA.hxx:55
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.
A field for fixed-size arrays that are represented as std::vector in memory.
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.
std::unique_ptr< RDeleter > fItemDeleter
void operator()(void *objPtr, bool dtorOnly) final
RArrayDeleter(std::size_t itemSize, std::size_t arrayLength, std::size_t alignment, std::unique_ptr< RDeleter > itemDeleter)
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
What sizeof(T) for this type returns.
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
What alignof(T) for this type returns.
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.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
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
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:78
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()
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:319
~RField() final=default
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:321
RField(std::string_view name)
Definition RField.hxx:322
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
Template specializations for C++ std::vector.
RVectorField(RVectorField &&other)=default
~RVectorField() override=default
std::unique_ptr< RDeleter > fItemDeleter
ROOT::Internal::RColumnIndex fNWritten
RVectorField & operator=(RVectorField &&other)=default
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1530
std::unique_ptr< RFieldBase > CreateEmulatedVectorField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, std::string_view emulatedFromType)
Definition RField.cxx:598
std::size_t EvalRVecAlignment(std::size_t alignOfSubfield)
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().