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/RFieldBase.hxx>
20
21#include <cassert>
22#include <string>
23#include <string_view>
24#include <unordered_map>
25#include <vector>
26
27namespace ROOT {
28namespace Experimental {
29namespace Internal {
30/**
31\class ROOT::Experimental::RNTupleProcessorProvenance
32\ingroup NTuple
33\brief Identifies how a processor is composed.
34
35The processor provenance is used in RNTupleProcessorEntry to identify how an (auxiliary) field in a composed processor
36can be accessed.
37*/
38// clang-format on
40private:
41 std::string fProvenance{};
42
43public:
45 RNTupleProcessorProvenance(const std::string &provenance) : fProvenance(provenance) {}
46
47 /////////////////////////////////////////////////////////////////////////////
48 /// \brief Get the full processor provenance, in the form of "x.y.z".
49 std::string Get() const { return fProvenance; }
50
51 /////////////////////////////////////////////////////////////////////////////
52 /// \brief Add a new processor to the provenance.
53 ///
54 /// \param[in] processorName Name of the processor to add.
55 ///
56 /// \return The updated provenance.
57 RNTupleProcessorProvenance Evolve(const std::string &processorName) const
58 {
59 if (fProvenance.empty())
60 return RNTupleProcessorProvenance(processorName);
61
62 return RNTupleProcessorProvenance(fProvenance + "." + processorName);
63 }
64
65 /////////////////////////////////////////////////////////////////////////////
66 /// \brief Check whether the provenance subsumes the provenance in `other`.
67 ///
68 /// \param[in] other The other provenance
69 bool Contains(const RNTupleProcessorProvenance &other) const
70 {
71 return fProvenance.rfind(other.fProvenance) != std::string::npos;
72 }
73
74 /////////////////////////////////////////////////////////////////////////////
75 /// \brief Check whether the provided field name contains this provenance.
76 ///
77 /// \param[in] fieldName Field name to check.
78 bool IsPresentInFieldName(std::string_view fieldName) const
79 {
80 return !fProvenance.empty() && fieldName.find(fProvenance + ".") == 0;
81 }
82};
83
84// clang-format off
85/**
86\class ROOT::Experimental::Internal::RNTupleProcessorEntry
87\ingroup NTuple
88\brief Collection of values in an RNTupleProcessor, analogous to REntry, with checks and support for missing values.
89*/
90// clang-format on
92public:
93 // We don't use RFieldTokens here, because it (semantically) does not make sense for the entry to be fixed to the
94 // schema ID of a particular model.
95 using FieldIndex_t = std::uint64_t;
96
97private:
99 std::unique_ptr<ROOT::RFieldBase> fField;
104
105 RProcessorValue(std::unique_ptr<ROOT::RFieldBase> field, std::string_view qualifiedFieldName,
107 : fField(std::move(field)),
108 fQualifiedFieldName(qualifiedFieldName),
109 fValue(std::move(value)),
110 fIsValid(isValid),
111 fProcessorProvenance(provenance)
112 {
113 }
114 };
115
116 std::vector<RProcessorValue> fProcessorValues;
117 // Maps from the field name to all type alternatives for that field that have been added to the entry.
118 std::unordered_map<std::string, std::vector<FieldIndex_t>> fFieldName2Index;
119
120public:
121 /////////////////////////////////////////////////////////////////////////////
122 /// \brief Clear all fields from the entry.
123 void Clear()
124 {
125 fProcessorValues.clear();
126 fFieldName2Index.clear();
127 }
128
129 /////////////////////////////////////////////////////////////////////////////
130 /// \brief Set the validity of a field, i.e. whether it is possible to read its value in the current entry.
131 ///
132 /// \param[in] fieldIdx The index of the field in the entry.
133 /// \param[in] isValid The new validity of the field.
134 void SetFieldValidity(FieldIndex_t fieldIdx, bool isValid)
135 {
136 assert(fieldIdx < fProcessorValues.size());
137 fProcessorValues[fieldIdx].fIsValid = isValid;
138 }
139
140 /////////////////////////////////////////////////////////////////////////////
141 /// \brief Check whether a field is valid for reading.
142 ///
143 /// \param[in] fieldIdx The index of the field in the entry.
144 bool IsValidField(FieldIndex_t fieldIdx) const
145 {
146 assert(fieldIdx < fProcessorValues.size());
147 return fProcessorValues[fieldIdx].fIsValid;
148 }
149
150 /////////////////////////////////////////////////////////////////////////////
151 /// \brief Find the name of a field from its field index.
152 ///
153 /// \param[in] fieldIdx The index of the field in the entry.
154 ///
155 /// \warning This function has linear complexity, only use it for more helpful error messages!
156 const std::string &FindFieldName(FieldIndex_t fieldIdx) const;
157
158 /////////////////////////////////////////////////////////////////////////////
159 /// \brief Find the field index of the provided field in the entry.
160 ///
161 /// \param[in] canonicalFieldName The name of the field in the entry, including its processor name prefixes and
162 /// parent field names, if applicable.
163 ///
164 /// \return A `std::optional` containing the field index if it was found.
165 std::optional<FieldIndex_t> FindFieldIndex(std::string_view canonicalFieldName, std::string_view typeName) const;
166
167 /////////////////////////////////////////////////////////////////////////////
168 /// \brief Add a new field to the entry.
169 ///
170 /// \param[in] qualifiedFieldName Name of the field to add, including its parent field if applicable.
171 /// \param[in] field Reference to the field to add, used to to create its corresponding RValue.
172 /// \param[in] valuePtr Pointer to an object corresponding to the field's type to bind to its value. If this is a
173 /// `nullptr`, a pointer will be created.
174 /// \param[in] provenance Processor provenance of the field.
175 ///
176 /// \return The field index of the newly added field.
177 FieldIndex_t AddField(const std::string &qualifiedFieldName, std::unique_ptr<ROOT::RFieldBase> field, void *valuePtr,
178 const RNTupleProcessorProvenance &provenance);
179
180 /////////////////////////////////////////////////////////////////////////////
181 /// \brief Update a field in the entry, preserving the value pointer.
182 ///
183 /// \param[in] fieldIdx Index of the field to update.
184 /// \param[in] field The new field to use in the entry.
185 void UpdateField(FieldIndex_t fieldIdx, std::unique_ptr<ROOT::RFieldBase> field);
186
187 /////////////////////////////////////////////////////////////////////////////
188 /// \brief Bind a new value pointer to a field in the entry.
189 ///
190 /// \param[in] fieldIdx The index of the field in the entry.
191 /// \param[in] valuePtr Pointer to the value to bind to the field.
192 void BindRawPtr(FieldIndex_t fieldIdx, void *valuePtr);
193
194 /////////////////////////////////////////////////////////////////////////////
195 /// \brief Read the field value corresponding to the given field index for the provided entry index.
196 ///
197 /// \param[in] fieldIdx The index of the field in the entry.
198 /// \param[in] entryIdx The entry number to read.
199 void ReadValue(FieldIndex_t fieldIdx, ROOT::NTupleSize_t entryIdx);
200
202 {
203 assert(fieldIdx < fProcessorValues.size());
204 return fProcessorValues[fieldIdx].fValue;
205 }
206
207 /////////////////////////////////////////////////////////////////////////////
208 /// \brief Get the processor provenance of a field in the entry.
209 ///
210 /// \param[in] fieldIdx The index of the field in the entry.
212 {
213 assert(fieldIdx < fProcessorValues.size());
214 return fProcessorValues[fieldIdx].fProcessorProvenance;
215 }
216
217 /////////////////////////////////////////////////////////////////////////////
218 /// \brief Get the name of a field in the entry, including its parent fields.
219 ///
220 /// \param[in] fieldIdx The index of the field in the entry.
221 std::string GetQualifiedFieldName(FieldIndex_t fieldIdx) const
222 {
223 assert(fieldIdx < fProcessorValues.size());
224 return fProcessorValues[fieldIdx].fQualifiedFieldName;
225 }
226
227 /////////////////////////////////////////////////////////////////////////////
228 /// \brief Get all field indices of this entry.
229 std::unordered_set<FieldIndex_t> GetFieldIndices() const;
230};
231} // namespace Internal
232} // namespace Experimental
233} // namespace ROOT
234
235#endif // ROOT_RNTupleProcessorEntry
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...
std::unordered_set< FieldIndex_t > GetFieldIndices() const
Get all field indices of this entry.
std::optional< FieldIndex_t > FindFieldIndex(std::string_view canonicalFieldName, std::string_view typeName) const
Find the field index of the provided field in the entry.
std::unordered_map< std::string, std::vector< FieldIndex_t > > fFieldName2Index
void BindRawPtr(FieldIndex_t fieldIdx, void *valuePtr)
Bind a new value pointer to a field in the entry.
std::string GetQualifiedFieldName(FieldIndex_t fieldIdx) const
Get the name of a field in the entry, including its parent fields.
const ROOT::RFieldBase::RValue & GetValue(FieldIndex_t fieldIdx) const
bool IsValidField(FieldIndex_t fieldIdx) const
Check whether a field is valid for reading.
void UpdateField(FieldIndex_t fieldIdx, std::unique_ptr< ROOT::RFieldBase > field)
Update a field in the entry, preserving the value pointer.
const std::string & FindFieldName(FieldIndex_t fieldIdx) const
Find the name of a field from its field index.
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.
FieldIndex_t AddField(const std::string &qualifiedFieldName, std::unique_ptr< ROOT::RFieldBase > field, void *valuePtr, const RNTupleProcessorProvenance &provenance)
Add a new field to the entry.
const RNTupleProcessorProvenance & GetFieldProvenance(FieldIndex_t fieldIdx) const
Get the processor provenance of a field in the entry.
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.
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
Namespace for ROOT features in testing.
Definition TROOT.h:100
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
RProcessorValue(std::unique_ptr< ROOT::RFieldBase > field, std::string_view qualifiedFieldName, ROOT::RFieldBase::RValue &&value, bool isValid, RNTupleProcessorProvenance provenance)