Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldFundamental.hxx
Go to the documentation of this file.
1/// \file ROOT/RField/Fundamental.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-09
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RField_Fundamental
17#define ROOT7_RField_Fundamental
18
19#ifndef ROOT7_RField
20#error "Please include RField.hxx!"
21#endif
22
23#include <ROOT/RColumn.hxx>
24#include <ROOT/RFieldBase.hxx>
25#include <ROOT/RNTupleUtil.hxx>
26
27#include <cstddef>
28#include <memory>
29#include <string>
30#include <string_view>
31#include <type_traits>
32
33namespace ROOT {
34namespace Experimental {
35
36namespace Detail {
37class RFieldVisitor;
38} // namespace Detail
39
40////////////////////////////////////////////////////////////////////////////////
41/// Template specializations for concrete C++ fundamental types
42////////////////////////////////////////////////////////////////////////////////
43
44template <>
45class RField<void> : public RFieldBase {
46public:
47 static std::string TypeName() { return "void"; }
48 // RField<void> should never be constructed.
49 RField() = delete;
50 RField(const RField &) = delete;
51 RField &operator=(const RField &) = delete;
52};
53
54////////////////////////////////////////////////////////////////////////////////
55/// Template specializations for integral types
56////////////////////////////////////////////////////////////////////////////////
57
58// bool and char are somewhat special, handle them first
59
60extern template class RSimpleField<bool>;
61
62template <>
63class RField<bool> final : public RSimpleField<bool> {
64protected:
65 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
66 {
67 return std::make_unique<RField>(newName);
68 }
69
70 const RColumnRepresentations &GetColumnRepresentations() const final;
71
72public:
73 static std::string TypeName() { return "bool"; }
74 explicit RField(std::string_view name) : RSimpleField(name, TypeName()) {}
75 RField(RField &&other) = default;
76 RField &operator=(RField &&other) = default;
77 ~RField() override = default;
78
79 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
80};
81
82extern template class RSimpleField<char>;
83
84template <>
85class RField<char> final : public RSimpleField<char> {
86protected:
87 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
88 {
89 return std::make_unique<RField>(newName);
90 }
91
92 const RColumnRepresentations &GetColumnRepresentations() const final;
93
94public:
95 static std::string TypeName() { return "char"; }
96 explicit RField(std::string_view name) : RSimpleField(name, TypeName()) {}
97 RField(RField &&other) = default;
98 RField &operator=(RField &&other) = default;
99 ~RField() override = default;
100
101 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
102};
103
104// For other integral types, we introduce an intermediate RIntegralField. It is specialized for fixed-width integer
105// types (from std::[u]int8_t to std::[u]int64_t). RField<T> for integral types T is specialized by mapping to the
106// corresponding fixed-width integer type (see RIntegralTypeMap).
107
108template <typename T>
110 // Instantiating this base template definition should never happen and is an error!
111 RIntegralField() = delete;
112};
113
114extern template class RSimpleField<std::int8_t>;
115
116template <>
118protected:
119 const RColumnRepresentations &GetColumnRepresentations() const final;
120
121public:
122 static std::string TypeName() { return "std::int8_t"; }
123 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
124 RIntegralField(RIntegralField &&other) = default;
125 RIntegralField &operator=(RIntegralField &&other) = default;
126 ~RIntegralField() override = default;
127
128 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
129};
130
131extern template class RSimpleField<std::uint8_t>;
132
133template <>
134class RIntegralField<std::uint8_t> : public RSimpleField<std::uint8_t> {
135protected:
136 const RColumnRepresentations &GetColumnRepresentations() const final;
137
138public:
139 static std::string TypeName() { return "std::uint8_t"; }
140 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
141 RIntegralField(RIntegralField &&other) = default;
142 RIntegralField &operator=(RIntegralField &&other) = default;
143 ~RIntegralField() override = default;
144
145 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
146};
147
148extern template class RSimpleField<std::int16_t>;
149
150template <>
151class RIntegralField<std::int16_t> : public RSimpleField<std::int16_t> {
152protected:
153 const RColumnRepresentations &GetColumnRepresentations() const final;
154
155public:
156 static std::string TypeName() { return "std::int16_t"; }
157 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
158 RIntegralField(RIntegralField &&other) = default;
159 RIntegralField &operator=(RIntegralField &&other) = default;
160 ~RIntegralField() override = default;
161
162 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
163};
164
165extern template class RSimpleField<std::uint16_t>;
166
167template <>
168class RIntegralField<std::uint16_t> : public RSimpleField<std::uint16_t> {
169protected:
170 const RColumnRepresentations &GetColumnRepresentations() const final;
171
172public:
173 static std::string TypeName() { return "std::uint16_t"; }
174 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
175 RIntegralField(RIntegralField &&other) = default;
176 RIntegralField &operator=(RIntegralField &&other) = default;
177 ~RIntegralField() override = default;
178
179 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
180};
181
182extern template class RSimpleField<std::int32_t>;
183
184template <>
185class RIntegralField<std::int32_t> : public RSimpleField<std::int32_t> {
186protected:
187 const RColumnRepresentations &GetColumnRepresentations() const final;
188
189public:
190 static std::string TypeName() { return "std::int32_t"; }
191 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
192 RIntegralField(RIntegralField &&other) = default;
193 RIntegralField &operator=(RIntegralField &&other) = default;
194 ~RIntegralField() override = default;
195
196 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
197};
198
199extern template class RSimpleField<std::uint32_t>;
200
201template <>
202class RIntegralField<std::uint32_t> : public RSimpleField<std::uint32_t> {
203protected:
204 const RColumnRepresentations &GetColumnRepresentations() const final;
205
206public:
207 static std::string TypeName() { return "std::uint32_t"; }
208 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
209 RIntegralField(RIntegralField &&other) = default;
210 RIntegralField &operator=(RIntegralField &&other) = default;
211 ~RIntegralField() override = default;
212
213 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
214};
215
216extern template class RSimpleField<std::int64_t>;
217
218template <>
219class RIntegralField<std::int64_t> : public RSimpleField<std::int64_t> {
220protected:
221 const RColumnRepresentations &GetColumnRepresentations() const final;
222
223public:
224 static std::string TypeName() { return "std::int64_t"; }
225 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
226 RIntegralField(RIntegralField &&other) = default;
227 RIntegralField &operator=(RIntegralField &&other) = default;
228 ~RIntegralField() override = default;
229
230 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
231};
232
233extern template class RSimpleField<std::uint64_t>;
234
235template <>
236class RIntegralField<std::uint64_t> : public RSimpleField<std::uint64_t> {
237protected:
238 const RColumnRepresentations &GetColumnRepresentations() const final;
239
240public:
241 static std::string TypeName() { return "std::uint64_t"; }
242 explicit RIntegralField(std::string_view name) : RSimpleField(name, TypeName()) {}
243 RIntegralField(RIntegralField &&other) = default;
244 RIntegralField &operator=(RIntegralField &&other) = default;
245 ~RIntegralField() override = default;
246
247 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
248};
249
250namespace Internal {
251// Map standard integer types to fixed width equivalents.
252template <typename T>
254 using type = T;
255};
256
257// RField<char> has its own specialization, we should not need a specialization of RIntegralTypeMap.
258// From https://en.cppreference.com/w/cpp/language/types:
259// char has the same representation and alignment as either signed char or
260// unsigned char, but is always a distinct type.
261template <>
263 static_assert(sizeof(signed char) == sizeof(std::int8_t));
264 using type = std::int8_t;
265};
266template <>
268 static_assert(sizeof(unsigned char) == sizeof(std::uint8_t));
269 using type = std::uint8_t;
270};
271template <>
273 static_assert(sizeof(short) == sizeof(std::int16_t));
274 using type = std::int16_t;
275};
276template <>
278 static_assert(sizeof(unsigned short) == sizeof(std::uint16_t));
279 using type = std::uint16_t;
280};
281template <>
283 static_assert(sizeof(int) == sizeof(std::int32_t));
284 using type = std::int32_t;
285};
286template <>
288 static_assert(sizeof(unsigned int) == sizeof(std::uint32_t));
289 using type = std::uint32_t;
290};
291template <>
293 static_assert(sizeof(long) == sizeof(std::int32_t) || sizeof(long) == sizeof(std::int64_t));
294 using type = std::conditional_t<sizeof(long) == sizeof(std::int32_t), std::int32_t, std::int64_t>;
295};
296template <>
297struct RIntegralTypeMap<unsigned long> {
298 static_assert(sizeof(unsigned long) == sizeof(std::uint32_t) || sizeof(unsigned long) == sizeof(std::uint64_t));
299 using type = std::conditional_t<sizeof(unsigned long) == sizeof(std::uint32_t), std::uint32_t, std::uint64_t>;
300};
301template <>
302struct RIntegralTypeMap<long long> {
303 static_assert(sizeof(long long) == sizeof(std::int64_t));
304 using type = std::int64_t;
305};
306template <>
307struct RIntegralTypeMap<unsigned long long> {
308 static_assert(sizeof(unsigned long long) == sizeof(std::uint64_t));
309 using type = std::uint64_t;
310};
311} // namespace Internal
312
313template <typename T>
314class RField<T, typename std::enable_if<std::is_integral_v<T>>::type> final
315 : public RIntegralField<typename Internal::RIntegralTypeMap<T>::type> {
316 using MappedType = typename Internal::RIntegralTypeMap<T>::type;
317 static_assert(sizeof(T) == sizeof(MappedType), "invalid size of mapped type");
318 static_assert(std::is_signed_v<T> == std::is_signed_v<MappedType>, "invalid signedness of mapped type");
319 using BaseType = RIntegralField<MappedType>;
320
321protected:
322 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
323 {
324 return std::make_unique<RField>(newName);
325 }
326
327public:
328 RField(std::string_view name) : RIntegralField<MappedType>(name) {}
329 RField(RField &&other) = default;
330 RField &operator=(RField &&other) = default;
331 ~RField() override = default;
332
333 T *Map(NTupleSize_t globalIndex) { return reinterpret_cast<T *>(this->BaseType::Map(globalIndex)); }
334 T *Map(RClusterIndex clusterIndex) { return reinterpret_cast<T *>(this->BaseType::Map(clusterIndex)); }
335 T *MapV(NTupleSize_t globalIndex, NTupleSize_t &nItems)
336 {
337 return reinterpret_cast<T *>(this->BaseType::MapV(globalIndex, nItems));
338 }
339 T *MapV(RClusterIndex clusterIndex, NTupleSize_t &nItems)
340 {
341 return reinterpret_cast<T *>(this->BaseType::MapV(clusterIndex, nItems));
342 }
343};
344
345////////////////////////////////////////////////////////////////////////////////
346/// Template specializations for floating-point types
347////////////////////////////////////////////////////////////////////////////////
348
349extern template class RSimpleField<float>;
350
351template <>
352class RField<float> final : public RSimpleField<float> {
353 std::size_t fBitWidth = sizeof(float) * 8;
354 double fValueMin = std::numeric_limits<float>::min();
355 double fValueMax = std::numeric_limits<float>::max();
356
357protected:
358 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
359 {
360 auto cloned = std::make_unique<RField<float>>(newName);
361 cloned->fBitWidth = fBitWidth;
362 cloned->fValueMin = fValueMin;
363 cloned->fValueMax = fValueMax;
364 return cloned;
365 }
366
367 const RColumnRepresentations &GetColumnRepresentations() const final;
368
369 void GenerateColumns() final;
370 void GenerateColumns(const RNTupleDescriptor &desc) final;
371
372public:
373 static std::string TypeName() { return "float"; }
374 explicit RField(std::string_view name) : RSimpleField(name, TypeName()) {}
375 RField(RField &&other) = default;
376 RField &operator=(RField &&other) = default;
377 ~RField() override = default;
378
379 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
380
381 /// Sets this field to use a half precision representation, occupying half as much storage space (16 bits) on disk.
382 /// This is mutually exclusive with `SetTruncated` and `SetQuantized`.
383 void SetHalfPrecision();
384 /// Set the precision of this field to `nBits`. The remaining (32 - `nBits`) bits will be truncated
385 /// from the number's mantissa. `nBits` must be $10 <= nBits <= 31$ (this means that at least 1 bit
386 /// of mantissa is always preserved). Note that this effectively rounds the number towards 0.
387 /// This is mutually exclusive with `SetHalfPrecision` and `SetQuantized`.
388 /// \note Calling `SetTruncated(16)` effectively makes this field a `bfloat16` on disk.
389 void SetTruncated(std::size_t nBits);
390 /// Sets this field to use a quantized integer representation using `nBits` per value.
391 /// This call promises that this field will only contain values contained in `[minValue, maxValue]` inclusive.
392 /// If a value outside this range is assigned to this field, the behavior is undefined.
393 /// This is mutually exclusive with `SetTruncated` and `SetHalfPrecision`.
394 void SetQuantized(float minValue, float maxValue, std::size_t nBits);
395};
396
397extern template class RSimpleField<double>;
398
399template <>
401protected:
402 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final
403 {
404 return std::make_unique<RField>(newName);
405 }
406
407 const RColumnRepresentations &GetColumnRepresentations() const final;
408
409public:
410 static std::string TypeName() { return "double"; }
411 explicit RField(std::string_view name) : RSimpleField(name, TypeName()) {}
412 RField(RField &&other) = default;
413 RField &operator=(RField &&other) = default;
414 ~RField() override = default;
415
416 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
417
418 // Set the column representation to 32 bit floating point and the type alias to Double32_t
419 void SetDouble32();
420};
421} // namespace Experimental
422} // namespace ROOT
423
424#endif
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
Binding & operator=(OUT(*fun)(void))
Abstract base class for classes implementing the visitor design pattern.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
Definition RField.cxx:1867
A field translates read and write calls from/to underlying columns to/from tree values.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
RField(RField &&other)=default
RField & operator=(RField &&other)=default
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
RField(RField &&other)=default
RField & operator=(RField &&other)=default
RField & operator=(RField &&other)=default
RField(RField &&other)=default
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
RField(RField &&other)=default
RField & operator=(RField &&other)=default
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
RField(const RField &)=delete
RField & operator=(const RField &)=delete
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:239
RField & operator=(RField &&other)=default
~RField() override=default
The on-storage meta-data of an ntuple.
auto Map(Args &&... args)
Create new collection applying a callable to the elements of the input collection.
Definition RVec.hxx:2150
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
double T(double x)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::conditional_t< sizeof(long)==sizeof(std::int32_t), std::int32_t, std::int64_t > type
std::conditional_t< sizeof(unsigned long)==sizeof(std::uint32_t), std::uint32_t, std::uint64_t > type