Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REntry.hxx
Go to the documentation of this file.
1/// \file ROOT/REntry.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-07-19
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_REntry
17#define ROOT7_REntry
18
19#include <ROOT/RError.hxx>
20#include <ROOT/RField.hxx>
21#include <string_view>
22
23#include <TError.h>
24
25#include <algorithm>
26#include <iterator>
27#include <memory>
28#include <type_traits>
29#include <utility>
30#include <vector>
31#include <unordered_map>
32
33namespace ROOT {
34namespace Experimental {
35
36// clang-format off
37/**
38\class ROOT::Experimental::REntry
39\ingroup NTuple
40\brief The REntry is a collection of values in an ntuple corresponding to a complete row in the data set
41
42The entry provides a memory-managed binder for a set of values. Through shared pointers, the memory locations
43that are associated to values are managed.
44*/
45// clang-format on
46class REntry {
48 friend class RNTupleModel;
49 friend class RNTupleReader;
50 friend class RNTupleFillContext;
51
52public:
53 /// The field token identifies a top-level field in this entry. It can be used for fast indexing in REntry's
54 /// methods, e.g. BindValue. The field token can also be created by the model.
56 friend class REntry;
57 friend class RNTupleModel;
58
59 std::size_t fIndex; ///< the index in fValues that belongs to the top-level field
60 std::uint64_t fModelId; ///< Safety check to prevent tokens from other models being used
61 RFieldToken(std::size_t index, std::uint64_t modelId) : fIndex(index), fModelId(modelId) {}
62 };
63
64private:
65 /// The entry must be linked to a specific model (or one if its clones), identified by a model ID
66 std::uint64_t fModelId = 0;
67 /// Corresponds to the top-level fields of the linked model
68 std::vector<RFieldBase::RValue> fValues;
69 /// For fast lookup of token IDs given a top-level field name
70 std::unordered_map<std::string, std::size_t> fFieldName2Token;
71
72 // Creation of entries is done by the RNTupleModel class
73
74 REntry() = default;
75 explicit REntry(std::uint64_t modelId) : fModelId(modelId) {}
76
78 {
79 fFieldName2Token[value.GetField().GetFieldName()] = fValues.size();
80 fValues.emplace_back(std::move(value));
81 }
82
83 /// While building the entry, adds a new value to the list and return the value's shared pointer
84 template <typename T, typename... ArgsT>
85 std::shared_ptr<T> AddValue(RField<T> &field, ArgsT &&...args)
86 {
87 fFieldName2Token[field.GetFieldName()] = fValues.size();
88 auto ptr = std::make_shared<T>(std::forward<ArgsT>(args)...);
89 fValues.emplace_back(field.BindValue(ptr));
90 return ptr;
91 }
92
94 {
95 for (auto &v : fValues) {
96 v.Read(index);
97 }
98 }
99
100 std::size_t Append()
101 {
102 std::size_t bytesWritten = 0;
103 for (auto &v : fValues) {
104 bytesWritten += v.Append();
105 }
106 return bytesWritten;
107 }
108
110 {
111 if (fModelId != token.fModelId) {
112 throw RException(R__FAIL("invalid token for this entry, "
113 "make sure to use a token from the same model as this entry."));
114 }
115 }
116
117 template <typename T>
118 void EnsureMatchingType(RFieldToken token [[maybe_unused]]) const
119 {
120 if constexpr (!std::is_void_v<T>) {
121 const auto &v = fValues[token.fIndex];
122 if (v.GetField().GetTypeName() != RField<T>::TypeName()) {
123 throw RException(R__FAIL("type mismatch for field " + v.GetField().GetFieldName() + ": " +
124 v.GetField().GetTypeName() + " vs. " + RField<T>::TypeName()));
125 }
126 }
127 }
128
129public:
130 using ConstIterator_t = decltype(fValues)::const_iterator;
131
132 REntry(const REntry &other) = delete;
133 REntry &operator=(const REntry &other) = delete;
134 REntry(REntry &&other) = default;
135 REntry &operator=(REntry &&other) = default;
136 ~REntry() = default;
137
138 /// The ordinal of the top-level field fieldName; can be used in other methods to address the corresponding value
139 RFieldToken GetToken(std::string_view fieldName) const
140 {
141 auto it = fFieldName2Token.find(std::string(fieldName));
142 if (it == fFieldName2Token.end()) {
143 throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
144 }
145 return RFieldToken(it->second, fModelId);
146 }
147
149 {
150 EnsureMatchingModel(token);
151 fValues[token.fIndex].EmplaceNew();
152 }
153
154 void EmplaceNewValue(std::string_view fieldName) { EmplaceNewValue(GetToken(fieldName)); }
155
156 template <typename T>
157 void BindValue(RFieldToken token, std::shared_ptr<T> objPtr)
158 {
159 EnsureMatchingModel(token);
160 EnsureMatchingType<T>(token);
161 fValues[token.fIndex].Bind(objPtr);
162 }
163
164 template <typename T>
165 void BindValue(std::string_view fieldName, std::shared_ptr<T> objPtr)
166 {
167 BindValue<T>(GetToken(fieldName), objPtr);
168 }
169
170 template <typename T>
171 void BindRawPtr(RFieldToken token, T *rawPtr)
172 {
173 EnsureMatchingModel(token);
174 EnsureMatchingType<T>(token);
175 fValues[token.fIndex].BindRawPtr(rawPtr);
176 }
177
178 template <typename T>
179 void BindRawPtr(std::string_view fieldName, T *rawPtr)
180 {
181 BindRawPtr<void>(GetToken(fieldName), rawPtr);
182 }
183
184 template <typename T>
185 std::shared_ptr<T> GetPtr(RFieldToken token) const
186 {
187 EnsureMatchingModel(token);
188 EnsureMatchingType<T>(token);
189 return std::static_pointer_cast<T>(fValues[token.fIndex].GetPtr<void>());
190 }
191
192 template <typename T>
193 std::shared_ptr<T> GetPtr(std::string_view fieldName) const
194 {
195 return GetPtr<T>(GetToken(fieldName));
196 }
197
198 std::uint64_t GetModelId() const { return fModelId; }
199
200 ConstIterator_t begin() const { return fValues.cbegin(); }
201 ConstIterator_t end() const { return fValues.cend(); }
202};
203
204} // namespace Experimental
205} // namespace ROOT
206
207#endif
#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:290
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
The field token identifies a top-level field in this entry.
Definition REntry.hxx:55
std::uint64_t fModelId
Safety check to prevent tokens from other models being used.
Definition REntry.hxx:60
std::size_t fIndex
the index in fValues that belongs to the top-level field
Definition REntry.hxx:59
RFieldToken(std::size_t index, std::uint64_t modelId)
Definition REntry.hxx:61
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:46
void EnsureMatchingType(RFieldToken token) const
Definition REntry.hxx:118
std::uint64_t fModelId
The entry must be linked to a specific model (or one if its clones), identified by a model ID.
Definition REntry.hxx:66
void BindValue(RFieldToken token, std::shared_ptr< T > objPtr)
Definition REntry.hxx:157
REntry & operator=(REntry &&other)=default
decltype(fValues)::const_iterator ConstIterator_t
Definition REntry.hxx:130
void Read(NTupleSize_t index)
Definition REntry.hxx:93
ConstIterator_t begin() const
Definition REntry.hxx:200
REntry & operator=(const REntry &other)=delete
REntry(std::uint64_t modelId)
Definition REntry.hxx:75
void EmplaceNewValue(std::string_view fieldName)
Definition REntry.hxx:154
REntry(REntry &&other)=default
std::uint64_t GetModelId() const
Definition REntry.hxx:198
void EmplaceNewValue(RFieldToken token)
Definition REntry.hxx:148
std::shared_ptr< T > AddValue(RField< T > &field, ArgsT &&...args)
While building the entry, adds a new value to the list and return the value's shared pointer.
Definition REntry.hxx:85
void BindRawPtr(RFieldToken token, T *rawPtr)
Definition REntry.hxx:171
std::unordered_map< std::string, std::size_t > fFieldName2Token
For fast lookup of token IDs given a top-level field name.
Definition REntry.hxx:70
void BindValue(std::string_view fieldName, std::shared_ptr< T > objPtr)
Definition REntry.hxx:165
RFieldToken GetToken(std::string_view fieldName) const
The ordinal of the top-level field fieldName; can be used in other methods to address the correspondi...
Definition REntry.hxx:139
void AddValue(RFieldBase::RValue &&value)
Definition REntry.hxx:77
std::vector< RFieldBase::RValue > fValues
Corresponds to the top-level fields of the linked model.
Definition REntry.hxx:68
void BindRawPtr(std::string_view fieldName, T *rawPtr)
Definition REntry.hxx:179
ConstIterator_t end() const
Definition REntry.hxx:201
void EnsureMatchingModel(RFieldToken token) const
Definition REntry.hxx:109
REntry(const REntry &other)=delete
std::shared_ptr< T > GetPtr(std::string_view fieldName) const
Definition REntry.hxx:193
std::shared_ptr< T > GetPtr(RFieldToken token) const
Definition REntry.hxx:185
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
Definition RField.hxx:191
std::string GetFieldName() const
Definition RField.hxx:677
RValue BindValue(std::shared_ptr< void > objPtr)
Creates a value from a memory location with an already constructed object.
Definition RField.hxx:664
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:1605
static std::string TypeName()
Definition RField.hxx:1618
A virtual ntuple used for writing untyped collections that can be used to some extent like an RNTuple...
A context for filling entries (data) into clusters of an RNTuple.
The RNTupleModel encapulates the schema of an ntuple.
An RNTuple that is used to read data from storage.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...