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 class RArrayAsVectorField; // to get access to the RVectorDeleter
204 friend std::unique_ptr<RFieldBase> Internal::CreateEmulatedVectorField(std::string_view fieldName,
205 std::unique_ptr<RFieldBase> itemField,
206 std::string_view emulatedFromType);
207
208 class RVectorDeleter : public RDeleter {
209 private:
210 std::size_t fItemSize = 0;
211 std::unique_ptr<RDeleter> fItemDeleter;
212
213 public:
214 RVectorDeleter() = default;
215 RVectorDeleter(std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
216 : fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
217 {
218 }
219 void operator()(void *objPtr, bool dtorOnly) final;
220 };
221
222 std::size_t fItemSize;
224 std::unique_ptr<RDeleter> fItemDeleter;
225
226 // Ensures that the std::vector pointed to by vec has at least nItems valid elements.
227 static void ResizeVector(void *vec, std::size_t nItems, std::size_t itemSize, const RFieldBase &itemField,
229
230protected:
231 /// Creates a possibly-untyped VectorField.
232 /// If `emulatedFromType` is not nullopt, the field is untyped. If the string is empty, it is a "regular"
233 /// untyped vector field; otherwise, it was created as an emulated field from the given type name.
234 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
235 std::optional<std::string_view> emulatedFromType);
236
237 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
238
239 const RColumnRepresentations &GetColumnRepresentations() const final;
240 void GenerateColumns() final;
241 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
242
243 void ConstructValue(void *where) const final { new (where) std::vector<char>(); }
244 std::unique_ptr<RDeleter> GetDeleter() const final;
245
246 std::size_t AppendImpl(const void *from) final;
247 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
248
249 std::unique_ptr<RFieldBase> BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final;
250 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
251
252 void CommitClusterImpl() final { fNWritten = 0; }
253
254public:
255 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
258 ~RVectorField() override = default;
259
260 static std::unique_ptr<RVectorField>
261 CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
262
263 std::vector<RValue> SplitValue(const RValue &value) const final;
264 size_t GetValueSize() const final { return sizeof(std::vector<char>); }
265 size_t GetAlignment() const final { return std::alignment_of<std::vector<char>>(); }
266 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
267};
268
269template <typename ItemT>
270class RField<std::vector<ItemT>> final : public RVectorField {
271public:
272 static std::string TypeName() { return "std::vector<" + RField<ItemT>::TypeName() + ">"; }
273 explicit RField(std::string_view name) : RVectorField(name, std::make_unique<RField<ItemT>>("_0")) {}
274 RField(RField &&other) = default;
275 RField &operator=(RField &&other) = default;
277};
278
279// `std::vector<bool>` is a template specialization and needs special treatment
280template <>
281class RField<std::vector<bool>> final : public RFieldBase {
282private:
283 ROOT::Internal::RColumnIndex fNWritten{0};
284 /// If schema-evolved from an std::array, fOnDiskNRepetition is > 0 and there will be no
285 /// principal column.
286 std::size_t fOnDiskNRepetitions = 0;
287
288protected:
289 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
290 {
291 return std::make_unique<RField>(newName);
292 }
293
294 const RColumnRepresentations &GetColumnRepresentations() const final;
295 void GenerateColumns() final;
296 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
297
298 void ConstructValue(void *where) const final { new (where) std::vector<bool>(); }
299 std::unique_ptr<RDeleter> GetDeleter() const final { return std::make_unique<RTypedDeleter<std::vector<bool>>>(); }
300
301 std::size_t AppendImpl(const void *from) final;
302 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
304
305 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
306
307 void CommitClusterImpl() final { fNWritten = 0; }
308
309public:
310 static std::string TypeName() { return "std::vector<bool>"; }
311 explicit RField(std::string_view name);
312 RField(RField &&other) = default;
313 RField &operator=(RField &&other) = default;
315
316 std::vector<RValue> SplitValue(const RValue &value) const final;
317
318 size_t GetValueSize() const final { return sizeof(std::vector<bool>); }
319 size_t GetAlignment() const final { return std::alignment_of<std::vector<bool>>(); }
321};
322
323////////////////////////////////////////////////////////////////////////////////
324/// Additional classes related to sequence containers
325////////////////////////////////////////////////////////////////////////////////
326
327/**
328\class ROOT::RArrayAsRVecField
329\brief A field for fixed-size arrays that are represented as RVecs in memory.
330\ingroup NTuple
331This class is used only for reading. In particular, it helps exposing
332arbitrarily-nested `std::array` on-disk fields as RVecs for usage in RDataFrame.
333*/
335private:
336 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
337 std::size_t fItemSize; /// The size of a child field's item
338 std::size_t fArrayLength; /// The length of the arrays in this field
339 std::size_t fValueSize; /// The size of a value of this field, i.e. an RVec
340
341protected:
342 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
343
344 void GenerateColumns() final { throw RException(R__FAIL("RArrayAsRVec fields must only be used for reading")); }
345 using RFieldBase::GenerateColumns;
346
347 void ConstructValue(void *where) const final;
348 /// Returns an RRVecField::RRVecDeleter
349 std::unique_ptr<RDeleter> GetDeleter() const final;
350
351 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
352 void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final;
353
354 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
355
356public:
357 /**
358 Constructor of the field. The `itemField` argument represents the inner
359 item of the on-disk array, i.e. for an `std::array<float>` it is the `float`
360 field and not the `std::array` itself.
361 */
362 RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
368
369 std::size_t GetValueSize() const final { return fValueSize; }
370 std::size_t GetAlignment() const final;
371
372 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
373 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
374};
375
376/**
377\class ROOT::RArrayAsVectorField
378\brief A field for fixed-size arrays that are represented as std::vector in memory.
379\ingroup NTuple
380This class is used only for reading. In particular, it helps for schema evolution of fixed-size arrays into vectors.
381*/
383private:
384 std::unique_ptr<RDeleter> fItemDeleter; /// Sub field deleter or nullptr for simple fields
385 std::size_t fItemSize; /// The size of a child field's item
386 std::size_t fArrayLength; /// The length of the arrays in this field
387
388protected:
389 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
390
391 void GenerateColumns() final;
392 using RFieldBase::GenerateColumns;
393
394 void ConstructValue(void *where) const final { new (where) std::vector<char>(); }
395 /// Returns an RVectorField::RVectorDeleter
396 std::unique_ptr<RDeleter> GetDeleter() const final;
397
398 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
399 void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final;
400
401 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
402
403public:
404 /// The `itemField` argument represents the inner item of the on-disk array,
405 /// i.e. for an `std::array<float>` it is the `float`
406 RArrayAsVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
412
413 size_t GetValueSize() const final { return sizeof(std::vector<char>); }
414 size_t GetAlignment() const final { return std::alignment_of<std::vector<char>>(); }
415
416 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
417 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
418};
419
420} // namespace ROOT
421
422#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.
A field for fixed-size arrays that are represented as std::vector in memory.
std::unique_ptr< RDeleter > fItemDeleter
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
std::size_t fArrayLength
The size of a child field's item.
std::size_t fItemSize
Sub field deleter or nullptr for simple fields.
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
void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) 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:589
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().