Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleProcessorEntry.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleProcessor.hxx
2/// \ingroup NTuple
3/// \author Florine de Geus <florine.de.geus@cern.ch>
4/// \date 2025-06-25
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-2024, 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 ROOT_RNTupleProcessorEntry
17#define ROOT_RNTupleProcessorEntry
18
19#include <ROOT/RNTupleModel.hxx>
20#include <ROOT/RFieldBase.hxx>
21
22#include <cassert>
23#include <string>
24#include <string_view>
25#include <unordered_map>
26#include <vector>
27
28namespace ROOT {
29namespace Experimental {
30namespace Internal {
31/**
32\class ROOT::Experimental::RNTupleProcessorProvenance
33\ingroup NTuple
34\brief Identifies how a processor is composed.
35
36The processor provenance is used in RNTupleProcessorEntry to identify how an (auxiliary) field in a composed processor
37can be accessed.
38*/
39// clang-format on
41private:
42 std::string fProvenance{};
43
44public:
47
48 /////////////////////////////////////////////////////////////////////////////
49 /// \brief Get the full processor provenance, in the form of "x.y.z".
50 std::string Get() const { return fProvenance; }
51
52 /////////////////////////////////////////////////////////////////////////////
53 /// \brief Add a new processor to the provenance.
54 ///
55 /// \param[in] processorName Name of the processor to add.
56 ///
57 /// \return The updated provenance.
65
66 /////////////////////////////////////////////////////////////////////////////
67 /// \brief Check whether the provenance subsumes the provenance in `other`.
68 ///
69 /// \param[in] other The other provenance
71 {
72 return fProvenance.rfind(other.fProvenance) != std::string::npos;
73 }
74
75 /////////////////////////////////////////////////////////////////////////////
76 /// \brief Check whether the provided field name contains this provenance.
77 ///
78 /// \param[in] fieldName Field name to check.
79 bool IsPresentInFieldName(std::string_view fieldName) const
80 {
81 return !fProvenance.empty() && fieldName.find(fProvenance + ".") == 0;
82 }
83};
84
85// clang-format off
86/**
87\class ROOT::Experimental::Internal::RNTupleProcessorEntry
88\ingroup NTuple
89\brief Collection of values in an RNTupleProcessor, analogous to REntry, with checks and support for missing values.
90*/
91// clang-format on
93public:
94 // We don't use RFieldTokens here, because it (semantically) does not make sense for the entry to be fixed to the
95 // schema ID of a particular model.
96 using FieldIndex_t = std::uint64_t;
97
98private:
109
110 std::vector<RProcessorValue> fProcessorValues;
111 std::unordered_map<std::string, FieldIndex_t> fFieldName2Index;
112
113public:
114 /////////////////////////////////////////////////////////////////////////////
115 /// \brief Set the validity of a field, i.e. whether it is possible to read its value in the current entry.
116 ///
117 /// \param[in] fieldIdx The index of the field in the entry.
118 /// \param[in] isValid The new validity of the field.
120 {
122 fProcessorValues[fieldIdx].fIsValid = isValid;
123 }
124
125 /////////////////////////////////////////////////////////////////////////////
126 /// \brief Check whether a field is valid for reading.
127 ///
128 /// \param[in] fieldIdx The index of the field in the entry.
130 {
132 return fProcessorValues[fieldIdx].fIsValid;
133 }
134
135 /////////////////////////////////////////////////////////////////////////////
136 /// \brief Find the name of a field from its field index.
137 ///
138 /// \param[in] fieldIdx The index of the field in the entry.
139 ///
140 /// \warning This function has linear complexity, only use it for more helpful error messages!
141 const std::string &FindFieldName(FieldIndex_t fieldIdx) const
142 {
144
145 for (const auto &[fieldName, index] : fFieldName2Index) {
146 if (index == fieldIdx) {
147 return fieldName;
148 }
149 }
150 // Should never happen, but avoid compiler warning about "returning reference to local temporary object".
151 R__ASSERT(false);
152 static const std::string empty = "";
153 return empty;
154 }
155
156 /////////////////////////////////////////////////////////////////////////////
157 /// \brief Find the field index of the provided field in the entry.
158 ///
159 /// \param[in] fieldName The name of the field in the entry.
160 ///
161 /// \return A `std::optional` containing the field index if it was found.
162 std::optional<FieldIndex_t> FindFieldIndex(std::string_view fieldName) const
163 {
164 auto it = fFieldName2Index.find(std::string(fieldName));
165 if (it == fFieldName2Index.end()) {
166 return std::nullopt;
167 }
168 return it->second;
169 }
170
171 /////////////////////////////////////////////////////////////////////////////
172 /// \brief Add a new field to the entry.
173 ///
174 /// \param[in] fieldName Name of the field to add.
175 /// \param[in] field Reference to the field to add, used to to create its corresponding RValue.
176 /// \param[in] valuePtr Pointer to an object corresponding to the field's type to bind to its value. If this is a
177 /// `nullptr`, a pointer will be created.
178 /// \param[in] provenance Processor provenance of the field.
179 ///
180 /// \return The field index of the newly added field.
183 {
185 throw ROOT::RException(
186 R__FAIL("field \"" + field.GetQualifiedFieldName() + "\" is already present in the entry"));
187
188 auto value = field.CreateValue();
189 if (valuePtr)
190 value.BindRawPtr(valuePtr);
191 auto fieldIdx = fProcessorValues.size();
192 fFieldName2Index[std::string(fieldName)] = fieldIdx;
193 fProcessorValues.emplace_back(RProcessorValue(std::move(value), true, provenance));
194
195 return fieldIdx;
196 }
197
198 /////////////////////////////////////////////////////////////////////////////
199 /// \brief Update a field in the entry, preserving the value pointer.
200 ///
201 /// \param[in] fieldIdx Index of the field to update.
202 /// \param[in] field The new field to use in the entry.
204 {
206
207 auto currValuePtr = fProcessorValues[fieldIdx].fValue.GetPtr<void>();
208 auto value = field.CreateValue();
209 value.Bind(currValuePtr);
211 }
212
213 /////////////////////////////////////////////////////////////////////////////
214 /// \brief Bind a new value pointer to a field in the entry.
215 ///
216 /// \param[in] fieldIdx The index of the field in the entry.
217 /// \param[in] valuePtr Pointer to the value to bind to the field.
219 {
221 fProcessorValues[fieldIdx].fValue.BindRawPtr(valuePtr);
222 }
223
224 /////////////////////////////////////////////////////////////////////////////
225 /// \brief Read the field value corresponding to the given field index for the provided entry index.
226 ///
227 /// \param[in] fieldIdx The index of the field in the entry.
228 /// \param[in] entryIdx The entry number to read.
230 {
232
233 if (fProcessorValues[fieldIdx].fIsValid) {
234 fProcessorValues[fieldIdx].fValue.Read(entryIdx);
235 }
236 }
237
238 /////////////////////////////////////////////////////////////////////////////
239 /// \brief Get a pointer to the value for the field represented by the provided field index.
240 ///
241 /// \tparam T The type of the pointer.
242 ///
243 /// \param[in] fieldIdx The index of the field in the entry.
244 ///
245 /// \return A shared pointer of type `T` with the field's value.
246 template <typename T>
247 std::shared_ptr<T> GetPtr(FieldIndex_t fieldIdx) const
248 {
250
251 if (fProcessorValues[fieldIdx].fIsValid)
252 return fProcessorValues[fieldIdx].fValue.GetPtr<T>();
253
254 return nullptr;
255 }
256
257 /////////////////////////////////////////////////////////////////////////////
258 /// \brief Get a reference to a field in the entry.
259 ///
260 /// \param[in] fieldIdx The index of the field in the entry.
262 {
264 return fProcessorValues[fieldIdx].fValue.GetField();
265 }
266
267 /////////////////////////////////////////////////////////////////////////////
268 /// \brief Get the processor provenance of a field in the entry.
269 ///
270 /// \param[in] fieldIdx The index of the field in the entry.
272 {
274 return fProcessorValues[fieldIdx].fProcessorProvenance;
275 }
276
277 /////////////////////////////////////////////////////////////////////////////
278 /// \brief Get the name of a field in the entry, including processor name prefixes in the case of auxiliary fields.
279 ///
280 /// \param[in] fieldIdx The index of the field in the entry.
282 {
284 return fProcessorValues[fieldIdx].fProcessorProvenance.Get() + "." +
285 fProcessorValues[fieldIdx].fValue.GetField().GetQualifiedFieldName();
286 }
287
288 /////////////////////////////////////////////////////////////////////////////
289 /// \brief Get all field indices of this entry.
290 std::unordered_set<FieldIndex_t> GetFieldIndices() const
291 {
292 // Field indices are sequentially assigned, and the entry (currently) offers no way to remove fields, so we can
293 // just generate and return a set {0, ..., |fProcessorValues| - 1}.
294 std::unordered_set<FieldIndex_t> fieldIdxs(fProcessorValues.size());
295 std::generate_n(std::inserter(fieldIdxs, fieldIdxs.begin()), fProcessorValues.size(),
296 [i = 0]() mutable { return i++; });
297 return fieldIdxs;
298 }
299};
300} // namespace Internal
301} // namespace Experimental
302} // namespace ROOT
303
304#endif // ROOT_RNTupleProcessorEntry
#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 R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
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
Collection of values in an RNTupleProcessor, analogous to REntry, with checks and support for missing...
FieldIndex_t AddField(std::string_view fieldName, ROOT::RFieldBase &field, void *valuePtr, const RNTupleProcessorProvenance &provenance)
Add a new field to the entry.
std::unordered_set< FieldIndex_t > GetFieldIndices() const
Get all field indices of this entry.
std::string GetFieldName(FieldIndex_t fieldIdx) const
Get the name of a field in the entry, including processor name prefixes in the case of auxiliary fiel...
void BindRawPtr(FieldIndex_t fieldIdx, void *valuePtr)
Bind a new value pointer to a field in the entry.
void UpdateField(FieldIndex_t fieldIdx, ROOT::RFieldBase &field)
Update a field in the entry, preserving the value pointer.
const ROOT::RFieldBase & GetField(FieldIndex_t fieldIdx) const
Get a reference to a field in the entry.
std::optional< FieldIndex_t > FindFieldIndex(std::string_view fieldName) const
Find the field index of the provided field in the entry.
bool IsValidField(FieldIndex_t fieldIdx) const
Check whether a field is valid for reading.
const std::string & FindFieldName(FieldIndex_t fieldIdx) const
Find the name of a field from its field index.
std::unordered_map< std::string, FieldIndex_t > fFieldName2Index
void ReadValue(FieldIndex_t fieldIdx, ROOT::NTupleSize_t entryIdx)
Read the field value corresponding to the given field index for the provided entry index.
void SetFieldValidity(FieldIndex_t fieldIdx, bool isValid)
Set the validity of a field, i.e.
const RNTupleProcessorProvenance & GetFieldProvenance(FieldIndex_t fieldIdx) const
Get the processor provenance of a field in the entry.
std::shared_ptr< T > GetPtr(FieldIndex_t fieldIdx) const
Get a pointer to the value for the field represented by the provided field index.
std::string Get() const
Get the full processor provenance, in the form of "x.y.z".
bool Contains(const RNTupleProcessorProvenance &other) const
Check whether the provenance subsumes the provenance in other.
bool IsPresentInFieldName(std::string_view fieldName) const
Check whether the provided field name contains this provenance.
RNTupleProcessorProvenance Evolve(const std::string &processorName) const
Add a new processor to the provenance.
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
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.
const_iterator begin() const
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
RProcessorValue(ROOT::RFieldBase::RValue &&value, bool isValid, RNTupleProcessorProvenance provenance)