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... ColTypes, std::size_t... S>
102 void CallExec(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>)
103 {
104 fHelper.Exec(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
105 (void)entry; // avoid unused parameter warning (gcc 12.1)
106 }
107
108 void Run(unsigned int slot, Long64_t entry) final
109 {
110 // check if entry passes all filters
111 if (fPrevNode.CheckFilters(slot, entry))
112 CallExec(slot, entry, ColumnTypes_t{}, TypeInd_t{});
113 }
114
115 void TriggerChildrenCount() final { fPrevNode.IncrChildrenCount(); }
116
117 /// Clean-up operations to be performed at the end of a task.
118 void FinalizeSlot(unsigned int slot) final
119 {
120 fValues[slot].fill(nullptr);
121 fHelper.CallFinalizeTask(slot);
122 }
123
124 /// Clean-up and finalize the action result (e.g. merging slot-local results).
125 /// It invokes the helper's Finalize method.
126 void Finalize() final
127 {
128 fHelper.Finalize();
129 SetHasRun();
130 }
131
132 std::shared_ptr<RDFGraphDrawing::GraphNode>
133 GetGraph(std::unordered_map<void *, std::shared_ptr<RDFGraphDrawing::GraphNode>> &visitedMap) final
134 {
135 auto prevNode = fPrevNode.GetGraph(visitedMap);
136 const auto &prevColumns = prevNode->GetDefinedColumns();
137
138 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
139 const auto nodeType = HasRun() ? RDFGraphDrawing::ENodeType::kUsedAction : RDFGraphDrawing::ENodeType::kAction;
140 auto thisNode =
141 std::make_shared<RDFGraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
142 visitedMap[(void *)this] = thisNode;
143
144 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
145
146 thisNode->AddDefinedColumns(GetColRegister().GetNames());
147 upmostNode->SetPrevNode(prevNode);
148 return thisNode;
149 }
150
151 /// This method is invoked to update a partial result during the event loop, right before passing the result to a
152 /// user-defined callback registered via RResultPtr::RegisterCallback
153 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
154
155 std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> &&results) final
156 {
157 const auto nVariations = GetVariations().size();
158 assert(results.size() == nVariations);
159
160 std::vector<Helper> helpers;
161 helpers.reserve(nVariations);
162
163 for (auto &&res : results)
164 helpers.emplace_back(fHelper.CallMakeNew(res));
165
166 return std::unique_ptr<RActionBase>(new RVariedAction<Helper, PrevNode, ColumnTypes_t>{
167 std::move(helpers), GetColumnNames(), fPrevNodePtr, GetColRegister()});
168 }
169
170 /**
171 * \brief Returns a new action with a cloned helper.
172 *
173 * \param[in] newResult The result to be filled by the new action (needed to clone the helper).
174 * \return A unique pointer to the new action.
175 */
176 std::unique_ptr<RActionBase> CloneAction(void *newResult) final
177 {
178 return std::make_unique<RAction>(fHelper.CallMakeNew(newResult), GetColumnNames(), fPrevNodePtr,
179 GetColRegister());
180 }
181
182private:
183 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
184};
185
186} // namespace RDF
187} // namespace Internal
188} // namespace ROOT
189
190#endif // ROOT_RACTION
#define h(i)
Definition RSha256.hxx:106
long long Long64_t
Definition RtypesCore.h:80
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:133
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RAction.hxx:118
void Run(unsigned int slot, Long64_t entry) final
Definition RAction.hxx:108
std::unique_ptr< RActionBase > CloneAction(void *newResult) final
Returns a new action with a cloned helper.
Definition RAction.hxx:176
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:126
RAction & operator=(const RAction &)=delete
void TriggerChildrenCount() final
Definition RAction.hxx:115
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:153
void CallExec(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >)
Definition RAction.hxx:102
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:183
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&results) final
Definition RAction.hxx:155
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:44
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:283
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....
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
This type aggregates some of the arguments passed to GetColumnReaders.
Lightweight storage for a collection of types.