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
18
19#include <cstddef> // std::size_t
20#include <memory>
21#include <string>
22#include <vector>
23
24namespace ROOT::Internal::RDF {
25
26namespace GraphDrawing {
27std::shared_ptr<GraphNode> AddDefinesToGraph(std::shared_ptr<GraphNode> node, const RColumnRegister &colRegister,
28 const std::vector<std::string> &prevNodeDefines,
29 std::unordered_map<void *, std::shared_ptr<GraphNode>> &visitedMap);
30} // namespace GraphDrawing
31
32class SnapshotHelperWithVariations;
33
34template <typename Helper, typename PrevNode>
36
37 // Template needed to avoid dependency on ActionHelpers.hxx
38 Helper fHelper;
39
40 /// Pointer to the previous node in this branch of the computation graph
41 std::vector<std::shared_ptr<PrevNode>> fPrevNodes;
42
43 /// Column readers per slot and per input column
44 std::vector<std::vector<RColumnReaderBase *>> fValues;
45
46 /// The nth flag signals whether the nth input column is a custom column or not.
47 std::vector<bool> fIsDefine;
48
49 /// Types of the columns to Snapshot
50 std::vector<const std::type_info *> fColTypeIDs;
51
52 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
53
54public:
55 RActionSnapshot(Helper &&h, const std::vector<std::string> &columns,
56 const std::vector<const std::type_info *> &colTypeIDs, std::shared_ptr<PrevNode> pd,
58 : RActionBase(pd->GetLoopManagerUnchecked(), columns, colRegister, pd->GetVariations()),
59 fHelper(std::move(h)),
60 fPrevNodes{std::move(pd)},
61 fValues(GetNSlots()),
62 fColTypeIDs(colTypeIDs)
63 {
64 fLoopManager->Register(this);
65
66 const auto nColumns = columns.size();
67 fIsDefine.reserve(nColumns);
68 for (auto i = 0u; i < nColumns; ++i)
69 fIsDefine.push_back(colRegister.IsDefineOrAlias(columns[i]));
70
71 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
72 if (const auto &variations = GetVariations(); !variations.empty()) {
73 // Get pointers to previous nodes of all systematics
74 fPrevNodes.reserve(1 + variations.size());
75 auto nominalFilter = fPrevNodes.front();
76 if (static_cast<RNodeBase *>(nominalFilter.get()) == fLoopManager) {
77 // just fill this with the RLoopManager N times
78 fPrevNodes.resize(1 + variations.size(), nominalFilter);
79 } else {
80 // create varied versions of the previous filter node
81 const auto &prevVariations = nominalFilter->GetVariations();
82 for (const auto &variation : variations) {
83 if (IsStrInVec(variation, prevVariations)) {
84 fPrevNodes.emplace_back(
85 std::static_pointer_cast<PrevNode>(nominalFilter->GetVariedFilter(variation)));
86 } else {
87 fPrevNodes.emplace_back(nominalFilter);
88 }
89 }
90 }
91 }
92 }
93 }
94
99
100 ~RActionSnapshot() final { fLoopManager->Deregister(this); }
101
102 /**
103 Retrieve a wrapper to the result of the action that knows how to merge
104 with others of the same type.
105 */
106 std::unique_ptr<ROOT::Detail::RDF::RMergeableValueBase> GetMergeableValue() const final
107 {
108 return fHelper.GetMergeableValue();
109 }
110
111 void Initialize() final { fHelper.Initialize(); }
112
113 void InitSlot(TTreeReader *r, unsigned int slot) final
114 {
115 fValues[slot] = GetUntypedColumnReaders(slot, r, RActionBase::GetColRegister(), *fLoopManager,
116 RActionBase::GetColumnNames(), fColTypeIDs);
117
118 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
119 // In case of systematic variations, append also the varied column readers to the values
120 // that get passed to the helpers
121 auto const &variations = GetVariations();
122 for (unsigned int variationIndex = 0; variationIndex < variations.size(); ++variationIndex) {
123 auto const &readers =
124 GetUntypedColumnReaders(slot, r, RActionBase::GetColRegister(), *fLoopManager,
125 RActionBase::GetColumnNames(), fColTypeIDs, variations[variationIndex]);
126 for (unsigned int i = 0; i < readers.size(); ++i) {
127 if (fValues[slot][i] != readers[i]) {
128 // The reader with variations differs from nominal, so this column needs to be added to the output
129 fValues[slot].push_back(readers[i]);
130 // Both the original and the varied column need to be registered for masking
131 fHelper.RegisterVariedColumn(slot, i, i, 0,
132 "nominal"); // (No harm flagging the nominal multiple times)
133 fHelper.RegisterVariedColumn(slot, fValues[slot].size() - 1, i, variationIndex + 1,
134 variations[variationIndex]);
135 }
136 }
137 }
138 }
139
140 fHelper.InitTask(r, slot);
141 }
142
143 void *GetValue(unsigned int slot, std::size_t readerIdx, Long64_t entry)
144 {
145 assert(slot < fValues.size());
146 assert(readerIdx < fValues[slot].size());
147 if (auto *val = fValues[slot][readerIdx]->template TryGet<void>(entry))
148 return val;
149
150 throw std::out_of_range{"RDataFrame: Action (" + fHelper.GetActionName() +
151 ") could not retrieve value for column '" + fColumnNames[readerIdx] + "' for entry " +
152 std::to_string(entry) +
153 ". You can use the DefaultValueFor operation to provide a default value, or "
154 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
155 }
156
157 void CallExec(unsigned int slot, Long64_t entry)
158 {
159 std::vector<void *> untypedValues;
160 auto nReaders = fValues[slot].size();
161 untypedValues.reserve(nReaders);
162 for (decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
163 untypedValues.push_back(GetValue(slot, readerIdx, entry));
164
165 fHelper.Exec(slot, untypedValues);
166 }
167
168 void Run(unsigned int slot, Long64_t entry) final
169 {
170 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
171 // check if entry passes all filters
172 std::vector<bool> filterPassed(fPrevNodes.size(), false);
173 for (unsigned int variation = 0; variation < fPrevNodes.size(); ++variation) {
174 filterPassed[variation] = fPrevNodes[variation]->CheckFilters(slot, entry);
175 }
176
177 // Currently, every event where any of nominal or variations pass gets written to the output.
178 // This logic could be extended for different use cases if the need arises.
179 if (std::any_of(filterPassed.begin(), filterPassed.end(), [](bool val) { return val; })) {
180 // TODO: Don't allocate
181 std::vector<void *> untypedValues;
182 auto nReaders = fValues[slot].size();
183 untypedValues.reserve(nReaders);
184 for (decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
185 untypedValues.push_back(GetValue(slot, readerIdx, entry));
186
187 fHelper.Exec(slot, untypedValues, filterPassed);
188 }
189 } else {
190 if (fPrevNodes.front()->CheckFilters(slot, entry))
191 CallExec(slot, entry);
192 }
193 }
194
196 {
197 for (auto const &node : fPrevNodes)
198 node->IncrChildrenCount();
199 }
200
201 /// Clean-up operations to be performed at the end of a task.
202 void FinalizeSlot(unsigned int slot) final
203 {
204 fValues[slot].clear();
205 fHelper.CallFinalizeTask(slot);
206 }
207
208 /// Clean-up and finalize the action result (e.g. merging slot-local results).
209 /// It invokes the helper's Finalize method.
211 {
212 fHelper.Finalize();
213 SetHasRun();
214 }
215
216 std::shared_ptr<GraphDrawing::GraphNode>
217 GetGraph(std::unordered_map<void *, std::shared_ptr<GraphDrawing::GraphNode>> &visitedMap) final
218 {
219 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
220 const auto nodeType = HasRun() ? GraphDrawing::ENodeType::kUsedAction : GraphDrawing::ENodeType::kAction;
221 auto thisNode = std::make_shared<GraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
222 visitedMap[(void *)this] = thisNode;
223
224 for (auto const &node : fPrevNodes) {
225 auto prevNode = node->GetGraph(visitedMap);
226 const auto &prevColumns = prevNode->GetDefinedColumns();
227 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
228
229 thisNode->AddDefinedColumns(GetColRegister().GenerateColumnNames());
230 upmostNode->SetPrevNode(prevNode);
231 }
232 return thisNode;
233 }
234
235 /// Forwards to the action helpers; will throw since PartialUpdate not supported for most snapshot helpers.
236 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
237
238 /// Will throw, since varied actions are unsupported. Instead, set a flag in RSnapshotOptions.
239 [[maybe_unused]] std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> && /*results*/) final
240 {
241 throw std::logic_error("RDataFrame::Snapshot: The snapshot action cannot be varied. Instead, switch on "
242 "variations in RSnapshotOptions.");
243 }
244
245 /**
246 * \brief Returns a new action with a cloned helper.
247 *
248 * \param[in] newResult The result to be filled by the new action (needed to clone the helper).
249 * \return A unique pointer to the new action.
250 */
251 std::unique_ptr<RActionBase> CloneAction(void *newResult) final
252 {
253 return std::make_unique<RActionSnapshot>(fHelper.CallMakeNew(newResult), GetColumnNames(), fColTypeIDs,
254 fPrevNodes.front(), GetColRegister());
255 }
256};
257
258} // namespace ROOT::Internal::RDF
259
260#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::vector< std::shared_ptr< PrevNode > > fPrevNodes
Pointer to the previous node in this branch of the computation graph.
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::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)
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....