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;
73
74public:
75 RArrayField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
79
81 size_t GetLength() const { return fArrayLength; }
83 size_t GetAlignment() const final { return fSubfields[0]->GetAlignment(); }
85};
86
87template <typename ItemT, std::size_t N>
88class RField<std::array<ItemT, N>> : public RArrayField {
89public:
90 static std::string TypeName() { return "std::array<" + RField<ItemT>::TypeName() + "," + std::to_string(N) + ">"; }
91 explicit RField(std::string_view name) : RArrayField(name, std::make_unique<RField<ItemT>>("_0"), N) {}
92 RField(RField &&other) = default;
93 RField &operator=(RField &&other) = default;
94 ~RField() override = default;
95};
96
97template <typename ItemT, std::size_t N>
98class RField<ItemT[N]> final : public RField<std::array<ItemT, N>> {
99public:
100 explicit RField(std::string_view name) : RField<std::array<ItemT, N>>(name) {}
101 RField(RField &&other) = default;
104};
105
106////////////////////////////////////////////////////////////////////////////////
107/// Template specializations for ROOT's RVec
108////////////////////////////////////////////////////////////////////////////////
109
110/// The type-erased field for a RVec<Type>
112public:
113 /// the RRVecDeleter is also used by RArrayAsRVecField and therefore declared public
114 class RRVecDeleter : public RDeleter {
115 private:
116 std::size_t fItemAlignment;
117 std::size_t fItemSize = 0;
118 std::unique_ptr<RDeleter> fItemDeleter;
119
120 public:
121 explicit RRVecDeleter(std::size_t itemAlignment) : fItemAlignment(itemAlignment) {}
122 RRVecDeleter(std::size_t itemAlignment, std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
123 : fItemAlignment(itemAlignment), fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
124 {
125 }
126 void operator()(void *objPtr, bool dtorOnly) final;
127 };
128
129 std::unique_ptr<RDeleter> fItemDeleter;
130
131protected:
132 std::size_t fItemSize;
134 std::size_t fValueSize;
135
136 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
138 void GenerateColumns() final;
140
141 void ConstructValue(void *where) const final;
142 std::unique_ptr<RDeleter> GetDeleter() const final;
143
144 std::size_t AppendImpl(const void *from) final;
147
148 void CommitClusterImpl() final { fNWritten = 0; }
149
150public:
151 RRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
152 RRVecField(RRVecField &&) = default;
154 RRVecField(const RRVecField &) = delete;
156 ~RRVecField() override = default;
157
158 std::vector<RValue> SplitValue(const RValue &value) const final;
159 size_t GetValueSize() const final;
160 size_t GetAlignment() const final;
162 void
167 void
172};
173
174template <typename ItemT>
175class RField<ROOT::VecOps::RVec<ItemT>> final : public RRVecField {
176public:
177 RField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
179 {
180 }
181
182 explicit RField(std::string_view name) : RField(name, std::make_unique<RField<ItemT>>("_0")) {}
183 RField(RField &&other) = default;
186
187 static std::string TypeName() { return "ROOT::VecOps::RVec<" + RField<ItemT>::TypeName() + ">"; }
188};
189
190////////////////////////////////////////////////////////////////////////////////
191/// Template specializations for C++ std::vector
192////////////////////////////////////////////////////////////////////////////////
193
194/// The generic field for a (nested) `std::vector<Type>` except for `std::vector<bool>`
195/// The field can be constructed as untyped collection through CreateUntyped().
196class RVectorField : public RFieldBase {
197 friend std::unique_ptr<RFieldBase> Internal::CreateEmulatedVectorField(std::string_view fieldName,
198 std::unique_ptr<RFieldBase> itemField,
199 std::string_view emulatedFromType);
200
201 class RVectorDeleter : public RDeleter {
202 private:
203 std::size_t fItemSize = 0;
204 std::unique_ptr<RDeleter> fItemDeleter;
205
206 public:
207 RVectorDeleter() = default;
208 RVectorDeleter(std::size_t itemSize, std::unique_ptr<RDeleter> itemDeleter)
209 : fItemSize(itemSize), fItemDeleter(std::move(itemDeleter))
210 {
211 }
212 void operator()(void *objPtr, bool dtorOnly) final;
213 };
214
215 std::size_t fItemSize;
217 std::unique_ptr<RDeleter> fItemDeleter;
218
219protected:
220 /// Creates a possibly-untyped VectorField.
221 /// If `emulatedFromType` is not nullopt, the field is untyped. If the string is empty, it is a "regular"
222 /// untyped vector field; otherwise, it was created as an emulated field from the given type name.
223 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
224 std::optional<std::string_view> emulatedFromType);
225
226 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
227
228 const RColumnRepresentations &GetColumnRepresentations() const final;
229 void GenerateColumns() final;
230 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
231
232 void ConstructValue(void *where) const final { new (where) std::vector<char>(); }
233 std::unique_ptr<RDeleter> GetDeleter() const final;
234
235 std::size_t AppendImpl(const void *from) final;
236 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final;
237
238 void CommitClusterImpl() final { fNWritten = 0; }
239
240public:
241 RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
244 ~RVectorField() override = default;
245
246 static std::unique_ptr<RVectorField>
247 CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField);
248
249 std::vector<RValue> SplitValue(const RValue &value) const final;
250 size_t GetValueSize() const final { return sizeof(std::vector<char>); }
251 size_t GetAlignment() const final { return std::alignment_of<std::vector<char>>(); }
252 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
253 void
258 void
263};
264
265template <typename ItemT>
266class RField<std::vector<ItemT>> final : public RVectorField {
267public:
268 static std::string TypeName() { return "std::vector<" + RField<ItemT>::TypeName() + ">"; }
269 explicit RField(std::string_view name) : RVectorField(name, std::make_unique<RField<ItemT>>("_0")) {}
270 RField(RField &&other) = default;
271 RField &operator=(RField &&other) = default;
273};
274
275// `std::vector<bool>` is a template specialization and needs special treatment
276template <>
277class RField<std::vector<bool>> final : public RFieldBase {
278private:
279 ROOT::Internal::RColumnIndex fNWritten{0};
280
281protected:
282 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
283 {
284 return std::make_unique<RField>(newName);
285 }
286
287 const RColumnRepresentations &GetColumnRepresentations() const final;
288 void GenerateColumns() final;
289 void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final;
290
291 void ConstructValue(void *where) const final { new (where) std::vector<bool>(); }
292 std::unique_ptr<RDeleter> GetDeleter() const final { return std::make_unique<RTypedDeleter<std::vector<bool>>>(); }
293
294 std::size_t AppendImpl(const void *from) final;
295 void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) 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 void
312 GetCollectionInfo(ROOT::NTupleSize_t globalIndex, RNTupleLocalIndex *collectionStart, ROOT::NTupleSize_t *size) const
313 {
315 }
316 void
317 GetCollectionInfo(RNTupleLocalIndex localIndex, RNTupleLocalIndex *collectionStart, ROOT::NTupleSize_t *size) const
318 {
320 }
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
354public:
355 /**
356 Constructor of the field. The `itemField` argument represents the inner
357 item of the on-disk array, i.e. for an `std::array<float>` it is the `float`
358 field and not the `std::array` itself.
359 */
360 RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField, std::size_t arrayLength);
366
367 std::size_t GetValueSize() const final { return fValueSize; }
368 std::size_t GetAlignment() const final;
369
370 std::vector<RFieldBase::RValue> SplitValue(const RFieldBase::RValue &value) const final;
371 void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
372};
373
374} // namespace ROOT
375
376#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
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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.
void GetCollectionInfo(const ROOT::NTupleSize_t globalIndex, RNTupleLocalIndex *collectionStart, ROOT::NTupleSize_t *collectionSize)
For offset columns only, look at the two adjacent values that define a collection's coordinates.
Definition RColumn.hxx:283
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:199
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:184
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.
ROOT::Internal::RColumn * fPrincipalColumn
All fields that have columns have a distinct main column.
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:287
~RField() final=default
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:289
RField(std::string_view name)
Definition RField.hxx:290
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
void GetCollectionInfo(RNTupleLocalIndex localIndex, RNTupleLocalIndex *collectionStart, ROOT::NTupleSize_t *size) const
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
void GetCollectionInfo(ROOT::NTupleSize_t globalIndex, RNTupleLocalIndex *collectionStart, ROOT::NTupleSize_t *size) const
void GetCollectionInfo(RNTupleLocalIndex localIndex, RNTupleLocalIndex *collectionStart, ROOT::NTupleSize_t *size) const
~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:1525
std::unique_ptr< RFieldBase > CreateEmulatedVectorField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, std::string_view emulatedFromType)
Definition RField.cxx:521
Namespace for new ROOT classes and functions.
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().