Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RActionCustomLoader.hxx
Go to the documentation of this file.
1// Author: Vincenzo Eduardo Padulano CERN 10/2026
2// Author: Silia Taider CERN 10/2026
3
4#ifndef ROOT_RActionCustomLoader
5#define ROOT_RActionCustomLoader
6
13
14#include <cstddef> // std::size_t
15#include <memory>
16#include <string>
17#include <vector>
18
19namespace ROOT::Internal::RDF {
20
21template <typename Helper, typename PrevNode>
22class R__CLING_PTRCHECK(off) RActionCustomLoader final : public RActionBase {
23
24 // Template needed to avoid dependency on ActionHelpers.hxx
25 Helper fHelper;
26
27 // If the PrevNode is a RJittedFilter, our collection of previous nodes will have to use the RFilterBase type:
28 // we'll have a RJittedFilter for the nominal case, but the others will be concrete filters.
29 using PrevNodeCommon_t = std::conditional_t<std::is_same_v<PrevNode, ROOT::Detail::RDF::RJittedFilter>,
31 /// Previous nodes in the computation graph. First element is nominal, others are varied.
32 std::vector<std::shared_ptr<PrevNodeCommon_t>> fPrevNodes;
33
34 /// Column readers per slot and per input column
35 std::vector<std::vector<RColumnReaderBase *>> fValues;
36
37 // Types of input columns
38 std::vector<const std::type_info *> fColTypeIDs;
39
40 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
41
42public:
43 RActionCustomLoader(Helper &&h, const std::vector<std::string> &columns,
44 const std::vector<const std::type_info *> &colTypeIDs, std::shared_ptr<PrevNode> pd,
45 const RColumnRegister &colRegister)
46 : RActionBase(pd->GetLoopManagerUnchecked(), columns, colRegister, pd->GetVariations()),
47 fHelper(std::move(h)),
48 fPrevNodes{std::static_pointer_cast<PrevNodeCommon_t>(pd)},
49 fValues(GetNSlots()),
50 fColTypeIDs(colTypeIDs)
51 {
52 fLoopManager->Register(this);
53 }
54
59
60 ~RActionCustomLoader() final { fLoopManager->Deregister(this); }
61
62 /**
63 Retrieve a wrapper to the result of the action that knows how to merge
64 with others of the same type.
65 */
66 std::unique_ptr<ROOT::Detail::RDF::RMergeableValueBase> GetMergeableValue() const final
67 {
68 return fHelper.GetMergeableValue();
69 }
70
71 void Initialize() final { fHelper.Initialize(); }
72
73 void InitSlot(TTreeReader *r, unsigned int slot) final
74 {
75 fValues[slot] = GetUntypedColumnReaders(slot, r, RActionBase::GetColRegister(), *fLoopManager,
76 RActionBase::GetColumnNames(), fColTypeIDs);
77 fHelper.InitTask(r, slot);
78 }
79
80 void *GetValue(unsigned int slot, std::size_t readerIdx, Long64_t entry)
81 {
82 assert(slot < fValues.size());
83 assert(readerIdx < fValues[slot].size());
84 if (auto *val = fValues[slot][readerIdx]->template TryGet<void>(entry))
85 return val;
86
87 throw std::out_of_range{"RDataFrame: Action (" + fHelper.GetActionName() +
88 ") could not retrieve value for column '" + fColumnNames[readerIdx] + "' for entry " +
89 std::to_string(entry) +
90 ". You can use the DefaultValueFor operation to provide a default value, or "
91 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
92 }
93
94 void CallExec(unsigned int slot, Long64_t entry)
95 {
96 std::vector<void *> untypedValues;
97 auto nReaders = fValues[slot].size();
98 untypedValues.reserve(nReaders);
99 for (decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
100 untypedValues.push_back(GetValue(slot, readerIdx, entry));
101
102 fHelper.Exec(slot, untypedValues);
103 }
104
105 void Run(unsigned int slot, Long64_t entry) final
106 {
107
108 if (fPrevNodes.front()->CheckFilters(slot, entry))
109 CallExec(slot, entry);
110 }
111
113 {
114 for (auto const &node : fPrevNodes)
115 node->IncrChildrenCount();
116 }
117
118 /// Clean-up operations to be performed at the end of a task.
119 void FinalizeSlot(unsigned int slot) final
120 {
121 fValues[slot].clear();
122 fHelper.CallFinalizeTask(slot);
123 }
124
125 /// Clean-up and finalize the action result (e.g. merging slot-local results).
126 /// It invokes the helper's Finalize method.
127 void Finalize() final
128 {
129 fHelper.Finalize();
130 SetHasRun();
131 }
132
133 std::shared_ptr<GraphDrawing::GraphNode>
134 GetGraph(std::unordered_map<void *, std::shared_ptr<GraphDrawing::GraphNode>> &visitedMap) final
135 {
136 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
137 const auto nodeType = HasRun() ? GraphDrawing::ENodeType::kUsedAction : GraphDrawing::ENodeType::kAction;
138 auto thisNode = std::make_shared<GraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
139 visitedMap[(void *)this] = thisNode;
140
141 for (auto const &node : fPrevNodes) {
142 auto prevNode = node->GetGraph(visitedMap);
143 const auto &prevColumns = prevNode->GetDefinedColumns();
144 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
145
146 thisNode->AddDefinedColumns(GetColRegister().GenerateColumnNames());
147 upmostNode->SetPrevNode(prevNode);
148 }
149 return thisNode;
150 }
151
152 /// Forwards to the action helpers; will throw since PartialUpdate not supported for most snapshot helpers.
153 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
154
155 [[maybe_unused]] std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> && /*results*/) final
156 {
157 throw std::runtime_error("not implemented.");
158 }
159
160 /**
161 * \brief Returns a new action with a cloned helper.
162 *
163 * \param[in] newResult The result to be filled by the new action (needed to clone the helper).
164 * \return A unique pointer to the new action.
165 */
166 std::unique_ptr<RActionBase> CloneAction(void *newResult) final
167 {
168 return std::make_unique<RActionCustomLoader>(fHelper.CallMakeNew(newResult), GetColumnNames(), fColTypeIDs,
169 std::static_pointer_cast<PrevNode>(fPrevNodes.front()),
170 GetColRegister());
171 }
172};
173
174} // namespace ROOT::Internal::RDF
175
176#endif // ROOT_RActionCustomLoader
#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:84
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
RActionCustomLoader(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::conditional_t< std::is_same_v< PrevNode, ROOT::Detail::RDF::RJittedFilter >, ROOT::Detail::RDF::RFilterBase, PrevNode > PrevNodeCommon_t
void Finalize() final
Clean-up and finalize the action result (e.g.
std::vector< std::vector< RColumnReaderBase * > > fValues
Column readers per slot and per input column.
std::vector< std::shared_ptr< PrevNodeCommon_t > > fPrevNodes
Previous nodes in the computation graph. First element is nominal, others are varied.
void * PartialUpdate(unsigned int slot) final
Forwards to the action helpers; will throw since PartialUpdate not supported for most snapshot helper...
void * GetValue(unsigned int slot, std::size_t readerIdx, Long64_t entry)
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&) final
RActionCustomLoader & operator=(const RActionCustomLoader &)=delete
RActionCustomLoader(RActionCustomLoader &&)=delete
void InitSlot(TTreeReader *r, unsigned int slot) final
void CallExec(unsigned int slot, Long64_t entry)
std::shared_ptr< GraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< GraphDrawing::GraphNode > > &visitedMap) final
ROOT::RDF::SampleCallback_t GetSampleCallback() final
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.
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.
void Run(unsigned int slot, Long64_t entry) final
RActionCustomLoader(const RActionCustomLoader &)=delete
std::vector< const std::type_info * > fColTypeIDs
RActionCustomLoader & operator=(RActionCustomLoader &&)=delete
A binder for user-defined columns, variations and aliases.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
unsigned int GetNSlots()
Definition RDFUtils.cxx:411
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....