Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RAction.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 09/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2020, 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_RACTION
12#define ROOT_RACTION
13
18#include "ROOT/RDF/Utils.hxx" // ColumnNames_t, IsInternalColumn
21
22#include <array>
23#include <cstddef> // std::size_t
24#include <memory>
25#include <string>
26#include <vector>
27
28namespace ROOT {
29namespace Internal {
30namespace RDF {
31
34
35namespace GraphDrawing {
36std::shared_ptr<GraphNode> AddDefinesToGraph(std::shared_ptr<GraphNode> node, const RColumnRegister &colRegister,
37 const std::vector<std::string> &prevNodeDefines,
38 std::unordered_map<void *, std::shared_ptr<GraphNode>> &visitedMap);
39} // namespace GraphDrawing
40
41// clang-format off
42/**
43 * \class ROOT::Internal::RDF::RAction
44 * \ingroup dataframe
45 * \brief A RDataFrame node that produces a result
46 * \tparam Helper The action helper type, which implements the concrete action logic (e.g. FillHelper, SnapshotHelper)
47 * \tparam PrevNode The type of the parent node in the computation graph
48 * \tparam ColumnTypes_t A TypeList with the types of the input columns
49 *
50 */
51// clang-format on
52template <typename Helper, typename PrevNode, typename ColumnTypes_t = typename Helper::ColumnTypes_t>
53class R__CLING_PTRCHECK(off) RAction : public RActionBase {
54 using TypeInd_t = std::make_index_sequence<ColumnTypes_t::list_size>;
55
56 Helper fHelper;
57 const std::shared_ptr<PrevNode> fPrevNodePtr;
58 PrevNode &fPrevNode;
59 /// Column readers per slot and per input column
60 std::vector<std::array<RColumnReaderBase *, ColumnTypes_t::list_size>> fValues;
61
62 /// The nth flag signals whether the nth input column is a custom column or not.
63 std::array<bool, ColumnTypes_t::list_size> fIsDefine;
64
65public:
66 RAction(Helper &&h, const ColumnNames_t &columns, std::shared_ptr<PrevNode> pd, const RColumnRegister &colRegister)
67 : RActionBase(pd->GetLoopManagerUnchecked(), columns, colRegister, pd->GetVariations()),
68 fHelper(std::forward<Helper>(h)), fPrevNodePtr(std::move(pd)), fPrevNode(*fPrevNodePtr), fValues(GetNSlots())
69 {
70 fLoopManager->Register(this);
71
72 const auto nColumns = columns.size();
73 for (auto i = 0u; i < nColumns; ++i)
74 fIsDefine[i] = colRegister.IsDefineOrAlias(columns[i]);
75 }
76
77 RAction(const RAction &) = delete;
78 RAction &operator=(const RAction &) = delete;
79
80 ~RAction() { fLoopManager->Deregister(this); }
81
82 /**
83 Retrieve a wrapper to the result of the action that knows how to merge
84 with others of the same type.
85 */
86 std::unique_ptr<RDFDetail::RMergeableValueBase> GetMergeableValue() const final
87 {
88 return fHelper.GetMergeableValue();
89 }
90
91 void Initialize() final { fHelper.Initialize(); }
92
93 void InitSlot(TTreeReader *r, unsigned int slot) final
94 {
95 RColumnReadersInfo info{RActionBase::GetColumnNames(), RActionBase::GetColRegister(), fIsDefine.data(),
96 *fLoopManager};
97 fValues[slot] = GetColumnReaders(slot, r, ColumnTypes_t{}, info);
98 fHelper.InitTask(r, slot);
99 }
100
101 template <typename ColType>
102 auto GetValueChecked(unsigned int slot, std::size_t readerIdx, Long64_t entry) -> ColType &
103 {
104 if (auto *val = fValues[slot][readerIdx]->template TryGet<ColType>(entry))
105 return *val;
106
107 throw std::out_of_range{"RDataFrame: Action (" + fHelper.GetActionName() +
108 ") could not retrieve value for column '" + fColumnNames[readerIdx] + "' for entry " +
109 std::to_string(entry) +
110 ". You can use the DefaultValueFor operation to provide a default value, or "
111 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
112 }
113
114 template <typename... ColTypes, std::size_t... S>
115 void CallExec(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>)
116 {
117 ROOT::Internal::RDF::CallGuaranteedOrder{[&](auto &&...args) { return fHelper.Exec(slot, args...); },
118 GetValueChecked<ColTypes>(slot, S, entry)...};
119 (void)entry; // avoid unused parameter warning (gcc 12.1)
120 }
121
122 void Run(unsigned int slot, Long64_t entry) final
123 {
124 // check if entry passes all filters
125 if (fPrevNode.CheckFilters(slot, entry))
126 CallExec(slot, entry, ColumnTypes_t{}, TypeInd_t{});
127 }
128
129 void TriggerChildrenCount() final { fPrevNode.IncrChildrenCount(); }
130
131 /// Clean-up operations to be performed at the end of a task.
132 void FinalizeSlot(unsigned int slot) final
133 {
134 fValues[slot].fill(nullptr);
135 fHelper.CallFinalizeTask(slot);
136 }
137
138 /// Clean-up and finalize the action result (e.g. merging slot-local results).
139 /// It invokes the helper's Finalize method.
140 void Finalize() final
141 {
142 fHelper.Finalize();
143 SetHasRun();
144 }
145
146 std::shared_ptr<RDFGraphDrawing::GraphNode>
147 GetGraph(std::unordered_map<void *, std::shared_ptr<RDFGraphDrawing::GraphNode>> &visitedMap) final
148 {
149 auto prevNode = fPrevNode.GetGraph(visitedMap);
150 const auto &prevColumns = prevNode->GetDefinedColumns();
151
152 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
153 const auto nodeType = HasRun() ? RDFGraphDrawing::ENodeType::kUsedAction : RDFGraphDrawing::ENodeType::kAction;
154 auto thisNode =
155 std::make_shared<RDFGraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
156 visitedMap[(void *)this] = thisNode;
157
158 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
159
160 thisNode->AddDefinedColumns(GetColRegister().GenerateColumnNames());
161 upmostNode->SetPrevNode(prevNode);
162 return thisNode;
163 }
164
165 /// This method is invoked to update a partial result during the event loop, right before passing the result to a
166 /// user-defined callback registered via RResultPtr::RegisterCallback
167 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
168
169 std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> &&results) final
170 {
171 const auto nVariations = GetVariations().size();
172 assert(results.size() == nVariations);
173
174 std::vector<Helper> helpers;
175 helpers.reserve(nVariations);
176
177 for (auto &&res : results)
178 helpers.emplace_back(fHelper.CallMakeNew(res));
179
180 return std::unique_ptr<RActionBase>(new RVariedAction<Helper, PrevNode, ColumnTypes_t>{
181 std::move(helpers), GetColumnNames(), fPrevNodePtr, GetColRegister()});
182 }
183
184 /**
185 * \brief Returns a new action with a cloned helper.
186 *
187 * \param[in] newResult The result to be filled by the new action (needed to clone the helper).
188 * \return A unique pointer to the new action.
189 */
190 std::unique_ptr<RActionBase> CloneAction(void *newResult) final
191 {
192 return std::make_unique<RAction>(fHelper.CallMakeNew(newResult), GetColumnNames(), fPrevNodePtr,
193 GetColRegister());
194 }
195
196private:
197 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
198};
199
200} // namespace RDF
201} // namespace Internal
202} // namespace ROOT
203
204#endif // ROOT_RACTION
#define h(i)
Definition RSha256.hxx:106
long long Long64_t
Definition RtypesCore.h:69
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
A RDataFrame node that produces a result.
Definition RAction.hxx:53
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< RDFGraphDrawing::GraphNode > > &visitedMap) final
Definition RAction.hxx:147
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RAction.hxx:132
void Run(unsigned int slot, Long64_t entry) final
Definition RAction.hxx:122
std::unique_ptr< RActionBase > CloneAction(void *newResult) final
Returns a new action with a cloned helper.
Definition RAction.hxx:190
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition RAction.hxx:93
std::make_index_sequence< ColumnTypes_t::list_size > TypeInd_t
Definition RAction.hxx:54
void Finalize() final
Clean-up and finalize the action result (e.g.
Definition RAction.hxx:140
RAction & operator=(const RAction &)=delete
void TriggerChildrenCount() final
Definition RAction.hxx:129
std::unique_ptr< RDFDetail::RMergeableValueBase > GetMergeableValue() const final
Retrieve a wrapper to the result of the action that knows how to merge with others of the same type.
Definition RAction.hxx:86
RAction(const RAction &)=delete
void * PartialUpdate(unsigned int slot) final
This method is invoked to update a partial result during the event loop, right before passing the res...
Definition RAction.hxx:167
void CallExec(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >)
Definition RAction.hxx:115
RAction(Helper &&h, const ColumnNames_t &columns, std::shared_ptr< PrevNode > pd, const RColumnRegister &colRegister)
Definition RAction.hxx:66
const std::shared_ptr< PrevNode > fPrevNodePtr
Definition RAction.hxx:57
ROOT::RDF::SampleCallback_t GetSampleCallback() final
Definition RAction.hxx:197
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&results) final
Definition RAction.hxx:169
auto GetValueChecked(unsigned int slot, std::size_t readerIdx, Long64_t entry) -> ColType &
Definition RAction.hxx:102
std::array< bool, ColumnTypes_t::list_size > fIsDefine
The nth flag signals whether the nth input column is a custom column or not.
Definition RAction.hxx:63
std::vector< std::array< RColumnReaderBase *, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition RAction.hxx:60
A binder for user-defined columns, variations and aliases.
bool IsDefineOrAlias(std::string_view name) const
Check if the provided name is tracked in the names list.
Just like an RAction, but it has N action helpers and N previous nodes (N is the number of variations...
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:301
std::array< RDFDetail::RColumnReaderBase *, sizeof...(ColTypes)> GetColumnReaders(unsigned int slot, TTreeReader *r, TypeList< ColTypes... >, const RColumnReadersInfo &colInfo, const std::string &variationName="nominal")
Create a group of column readers, one per type in the parameter pack.
std::vector< std::string > ColumnNames_t
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....
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Struct to wrap the call to a function with a guaranteed order of execution of its arguments.
Definition Utils.hxx:313
This type aggregates some of the arguments passed to GetColumnReaders.
Lightweight storage for a collection of types.