Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RActionSnapshot.hxx
Go to the documentation of this file.
1// Author: Vincenzo Eduardo Padulano CERN 06/2025
2
3/*************************************************************************
4 * Copyright (C) 1995-2025, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_RACTIONSNAPSHOT
12#define ROOT_RACTIONSNAPSHOT
13
20
21#include <cstddef> // std::size_t
22#include <memory>
23#include <string>
24#include <vector>
25
26namespace ROOT::Internal::RDF {
27
28namespace GraphDrawing {
29std::shared_ptr<GraphNode> AddDefinesToGraph(std::shared_ptr<GraphNode> node, const RColumnRegister &colRegister,
30 const std::vector<std::string> &prevNodeDefines,
31 std::unordered_map<void *, std::shared_ptr<GraphNode>> &visitedMap);
32} // namespace GraphDrawing
33
34class SnapshotHelperWithVariations;
35
36template <typename Helper, typename PrevNode>
38
39 // Template needed to avoid dependency on ActionHelpers.hxx
40 Helper fHelper;
41
42 // If the PrevNode is a RJittedFilter, our collection of previous nodes will have to use the RFilterBase type:
43 // we'll have a RJittedFilter for the nominal case, but the others will be concrete filters.
44 using PrevNodeCommon_t = std::conditional_t<std::is_same_v<PrevNode, ROOT::Detail::RDF::RJittedFilter>,
46 /// Previous nodes in the computation graph. First element is nominal, others are varied.
47 std::vector<std::shared_ptr<PrevNodeCommon_t>> fPrevNodes;
48
49 /// Column readers per slot and per input column
50 std::vector<std::vector<RColumnReaderBase *>> fValues;
51
52 /// The nth flag signals whether the nth input column is a custom column or not.
53 std::vector<bool> fIsDefine;
54
55 /// Types of the columns to Snapshot
56 std::vector<const std::type_info *> fColTypeIDs;
57
58 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
59
61 {
62 // This method only makes sense if we're appending the varied filters to the list after the nominal
63 assert(fPrevNodes.size() == 1);
64 const auto &currentVariations = GetVariations();
65
66 // If this node hangs from the RLoopManager itself, just use that as the upstream node for each variation
67 auto nominalPrevNode = fPrevNodes.front();
68 if (static_cast<ROOT::Detail::RDF::RNodeBase *>(nominalPrevNode.get()) == fLoopManager) {
69 fPrevNodes.resize(1 + currentVariations.size(), nominalPrevNode);
70 return;
71 }
72
73 // Otherwise, append one varied filter per variation
74 const auto &prevVariations = nominalPrevNode->GetVariations();
75 fPrevNodes.reserve(1 + currentVariations.size());
76
77 for (const auto &variation : currentVariations) {
78 if (IsStrInVec(variation, prevVariations)) {
79 fPrevNodes.emplace_back(
80 std::static_pointer_cast<PrevNodeCommon_t>(nominalPrevNode->GetVariedFilter(variation)));
81 } else {
82 fPrevNodes.push_back(nominalPrevNode);
83 }
84 }
85 }
86
87public:
88 RActionSnapshot(Helper &&h, const std::vector<std::string> &columns,
89 const std::vector<const std::type_info *> &colTypeIDs, std::shared_ptr<PrevNode> pd,
91 : RActionBase(pd->GetLoopManagerUnchecked(), columns, colRegister, pd->GetVariations()),
92 fHelper(std::move(h)),
93 fPrevNodes{std::static_pointer_cast<PrevNodeCommon_t>(pd)},
94 fValues(GetNSlots()),
95 fColTypeIDs(colTypeIDs)
96 {
97 fLoopManager->Register(this);
98
99 const auto nColumns = columns.size();
100 fIsDefine.reserve(nColumns);
101 for (auto i = 0u; i < nColumns; ++i)
102 fIsDefine.push_back(colRegister.IsDefineOrAlias(columns[i]));
103
104 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
105 // Need to populate parts of the computation graph for which we have empty shells, e.g. RJittedFilters and
106 // varied Defines
107 if (!GetVariations().empty())
108 fLoopManager->Jit();
109
110 AppendVariedPrevNodes();
111
112 for (auto i = 0u; i < nColumns; ++i) {
113 if (fIsDefine[i]) {
114 auto define = colRegister.GetDefine(columns[i]);
115 define->MakeVariations(GetVariations());
116 }
117 }
118 }
119 }
120
125
126 ~RActionSnapshot() final { fLoopManager->Deregister(this); }
127
128 /**
129 Retrieve a wrapper to the result of the action that knows how to merge
130 with others of the same type.
131 */
132 std::unique_ptr<ROOT::Detail::RDF::RMergeableValueBase> GetMergeableValue() const final
133 {
134 return fHelper.GetMergeableValue();
135 }
136
137 void Initialize() final { fHelper.Initialize(); }
138
139 void InitSlot(TTreeReader *r, unsigned int slot) final
140 {
141 fValues[slot] = GetUntypedColumnReaders(slot, r, RActionBase::GetColRegister(), *fLoopManager,
142 RActionBase::GetColumnNames(), fColTypeIDs);
143
144 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
145 // In case of systematic variations, append also the varied column readers to the values
146 // that get passed to the helpers
147 auto const &variations = GetVariations();
148 for (unsigned int variationIndex = 0; variationIndex < variations.size(); ++variationIndex) {
149 auto const &readers =
150 GetUntypedColumnReaders(slot, r, RActionBase::GetColRegister(), *fLoopManager,
151 RActionBase::GetColumnNames(), fColTypeIDs, variations[variationIndex]);
152 for (unsigned int i = 0; i < readers.size(); ++i) {
153 if (fValues[slot][i] != readers[i]) {
154 // The reader with variations differs from nominal, so this column needs to be added to the output
155 fValues[slot].push_back(readers[i]);
156 // Both the original and the varied column need to be registered for masking
157 fHelper.RegisterVariedColumn(slot, i, i, 0,
158 "nominal"); // (No harm flagging the nominal multiple times)
159 fHelper.RegisterVariedColumn(slot, fValues[slot].size() - 1, i, variationIndex + 1,
160 variations[variationIndex]);
161 }
162 }
163 }
164 }
165
166 fHelper.InitTask(r, slot);
167 }
168
169 void *GetValue(unsigned int slot, std::size_t readerIdx, Long64_t entry)
170 {
171 assert(slot < fValues.size());
172 assert(readerIdx < fValues[slot].size());
173 if (auto *val = fValues[slot][readerIdx]->template TryGet<void>(entry))
174 return val;
175
176 throw std::out_of_range{"RDataFrame: Action (" + fHelper.GetActionName() +
177 ") could not retrieve value for column '" + fColumnNames[readerIdx] + "' for entry " +
178 std::to_string(entry) +
179 ". You can use the DefaultValueFor operation to provide a default value, or "
180 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
181 }
182
183 void CallExec(unsigned int slot, Long64_t entry)
184 {
185 std::vector<void *> untypedValues;
186 auto nReaders = fValues[slot].size();
187 untypedValues.reserve(nReaders);
188 for (decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
189 untypedValues.push_back(GetValue(slot, readerIdx, entry));
190
191 fHelper.Exec(slot, untypedValues);
192 }
193
194 void Run(unsigned int slot, Long64_t entry) final
195 {
196 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
197 // check if entry passes all filters
198 std::vector<bool> filterPassed(fPrevNodes.size(), false);
199 for (unsigned int variation = 0; variation < fPrevNodes.size(); ++variation) {
200 filterPassed[variation] = fPrevNodes[variation]->CheckFilters(slot, entry);
201 }
202
203 // Currently, every event where any of nominal or variations pass gets written to the output.
204 // This logic could be extended for different use cases if the need arises.
205 if (std::any_of(filterPassed.begin(), filterPassed.end(), [](bool val) { return val; })) {
206 // TODO: Don't allocate
207 std::vector<void *> untypedValues;
208 auto nReaders = fValues[slot].size();
209 untypedValues.reserve(nReaders);
210 for (decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
211 untypedValues.push_back(GetValue(slot, readerIdx, entry));
212
213 fHelper.Exec(slot, untypedValues, filterPassed);
214 }
215 } else {
216 if (fPrevNodes.front()->CheckFilters(slot, entry))
217 CallExec(slot, entry);
218 }
219 }
220
222 {
223 for (auto const &node : fPrevNodes)
224 node->IncrChildrenCount();
225 }
226
227 /// Clean-up operations to be performed at the end of a task.
228 void FinalizeSlot(unsigned int slot) final
229 {
230 fValues[slot].clear();
231 fHelper.CallFinalizeTask(slot);
232 }
233
234 /// Clean-up and finalize the action result (e.g. merging slot-local results).
235 /// It invokes the helper's Finalize method.
237 {
238 fHelper.Finalize();
239 SetHasRun();
240 }
241
242 std::shared_ptr<GraphDrawing::GraphNode>
243 GetGraph(std::unordered_map<void *, std::shared_ptr<GraphDrawing::GraphNode>> &visitedMap) final
244 {
245 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
246 const auto nodeType = HasRun() ? GraphDrawing::ENodeType::kUsedAction : GraphDrawing::ENodeType::kAction;
247 auto thisNode = std::make_shared<GraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
248 visitedMap[(void *)this] = thisNode;
249
250 for (auto const &node : fPrevNodes) {
251 auto prevNode = node->GetGraph(visitedMap);
252 const auto &prevColumns = prevNode->GetDefinedColumns();
253 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
254
255 thisNode->AddDefinedColumns(GetColRegister().GenerateColumnNames());
256 upmostNode->SetPrevNode(prevNode);
257 }
258 return thisNode;
259 }
260
261 /// Forwards to the action helpers; will throw since PartialUpdate not supported for most snapshot helpers.
262 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
263
264 /// Will throw, since varied actions are unsupported. Instead, set a flag in RSnapshotOptions.
265 [[maybe_unused]] std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> && /*results*/) final
266 {
267 throw std::logic_error("RDataFrame::Snapshot: The snapshot action cannot be varied. Instead, switch on "
268 "variations in RSnapshotOptions.");
269 }
270
271 /**
272 * \brief Returns a new action with a cloned helper.
273 *
274 * \param[in] newResult The result to be filled by the new action (needed to clone the helper).
275 * \return A unique pointer to the new action.
276 */
277 std::unique_ptr<RActionBase> CloneAction(void *newResult) final
278 {
279 return std::make_unique<RActionSnapshot>(fHelper.CallMakeNew(newResult), GetColumnNames(), fColTypeIDs,
280 std::static_pointer_cast<PrevNode>(fPrevNodes.front()),
281 GetColRegister());
282 }
283};
284
285} // namespace ROOT::Internal::RDF
286
287#endif // ROOT_RACTIONSNAPSHOT
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 r
Base class for non-leaf nodes of the computational graph.
Definition RNodeBase.hxx:43
RActionSnapshot & operator=(const RActionSnapshot &)=delete
void CallExec(unsigned int slot, Long64_t entry)
ROOT::RDF::SampleCallback_t GetSampleCallback() final
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&) final
Will throw, since varied actions are unsupported. Instead, set a flag in RSnapshotOptions.
std::vector< bool > fIsDefine
The nth flag signals whether the nth input column is a custom column or not.
std::vector< std::vector< RColumnReaderBase * > > fValues
Column readers per slot and per input column.
void InitSlot(TTreeReader *r, unsigned int slot) final
void * PartialUpdate(unsigned int slot) final
Forwards to the action helpers; will throw since PartialUpdate not supported for most snapshot helper...
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
std::conditional_t< std::is_same_v< PrevNode, ROOT::Detail::RDF::RJittedFilter >, ROOT::Detail::RDF::RFilterBase, PrevNode > PrevNodeCommon_t
std::unique_ptr< RActionBase > CloneAction(void *newResult) final
Returns a new action with a cloned helper.
RActionSnapshot(Helper &&h, const std::vector< std::string > &columns, const std::vector< const std::type_info * > &colTypeIDs, std::shared_ptr< PrevNode > pd, const RColumnRegister &colRegister)
std::vector< std::shared_ptr< PrevNodeCommon_t > > fPrevNodes
Previous nodes in the computation graph. First element is nominal, others are varied.
void * GetValue(unsigned int slot, std::size_t readerIdx, Long64_t entry)
RActionSnapshot(RActionSnapshot &&)=delete
std::vector< const std::type_info * > fColTypeIDs
Types of the columns to Snapshot.
void Finalize() final
Clean-up and finalize the action result (e.g.
std::unique_ptr< ROOT::Detail::RDF::RMergeableValueBase > GetMergeableValue() const final
Retrieve a wrapper to the result of the action that knows how to merge with others of the same type.
std::shared_ptr< GraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< GraphDrawing::GraphNode > > &visitedMap) final
RActionSnapshot(const RActionSnapshot &)=delete
RActionSnapshot & operator=(RActionSnapshot &&)=delete
void Run(unsigned int slot, Long64_t entry) final
A binder for user-defined columns, variations and aliases.
const_iterator begin() const
const_iterator end() const
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
std::shared_ptr< GraphNode > AddDefinesToGraph(std::shared_ptr< GraphNode > node, const RColumnRegister &colRegister, const std::vector< std::string > &prevNodeDefines, std::unordered_map< void *, std::shared_ptr< GraphNode > > &visitedMap)
unsigned int GetNSlots()
Definition RDFUtils.cxx:384
std::vector< RDFDetail::RColumnReaderBase * > GetUntypedColumnReaders(unsigned int slot, TTreeReader *treeReader, ROOT::Internal::RDF::RColumnRegister &colRegister, ROOT::Detail::RDF::RLoopManager &lm, const std::vector< std::string > &colNames, const std::vector< const std::type_info * > &colTypeIDs, const std::string &variationName="nominal")
std::function< void(unsigned int, const ROOT::RDF::RSampleInfo &)> SampleCallback_t
The type of a data-block callback, registered with an RDataFrame computation graph via e....