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,
37 const RDFInternal::RColumnRegister &colRegister,
38 const std::vector<std::string> &prevNodeDefines);
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
57 const std::shared_ptr<PrevNode> fPrevNodePtr;
58 PrevNode &fPrevNode;
59 /// Column readers per slot and per input column
60 std::vector<std::array<std::unique_ptr<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->Book(this);
71
72 const auto nColumns = columns.size();
73 const auto &customCols = GetColRegister();
74 for (auto i = 0u; i < nColumns; ++i)
75 fIsDefine[i] = customCols.HasName(columns[i]);
76 }
77
78 RAction(const RAction &) = delete;
79 RAction &operator=(const RAction &) = delete;
81 {
82 // must Deregister objects from the RLoopManager here, before the fPrevNode data member is destroyed:
83 // otherwise if fPrevNode is the RLoopManager, it will be destroyed before the calls to Deregister happen.
84 RActionBase::GetColRegister().Clear(); // triggers RDefine deregistration
85 fLoopManager->Deregister(this);
86 }
87
88 /**
89 Retrieve a wrapper to the result of the action that knows how to merge
90 with others of the same type.
91 */
92 std::unique_ptr<RDFDetail::RMergeableValueBase> GetMergeableValue() const final
93 {
94 return fHelper.GetMergeableValue();
95 }
96
97 void Initialize() final { fHelper.Initialize(); }
98
99 void InitSlot(TTreeReader *r, unsigned int slot) final
100 {
101 RDFInternal::RColumnReadersInfo info{RActionBase::GetColumnNames(), RActionBase::GetColRegister(),
102 fIsDefine.data(), fLoopManager->GetDSValuePtrs(),
103 fLoopManager->GetDataSource()};
104 fValues[slot] = RDFInternal::MakeColumnReaders(slot, r, ColumnTypes_t{}, info);
105 fHelper.InitTask(r, slot);
106 }
107
108 template <typename... ColTypes, std::size_t... S>
109 void CallExec(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>)
110 {
111 fHelper.Exec(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
112 (void)entry; // avoid "unused parameter" warnings
113 }
114
115 void Run(unsigned int slot, Long64_t entry) final
116 {
117 // check if entry passes all filters
118 if (fPrevNode.CheckFilters(slot, entry))
119 CallExec(slot, entry, ColumnTypes_t{}, TypeInd_t{});
120 }
121
122 void TriggerChildrenCount() final { fPrevNode.IncrChildrenCount(); }
123
124 /// Clean-up operations to be performed at the end of a task.
125 void FinalizeSlot(unsigned int slot) final
126 {
127 for (auto &v : fValues[slot])
128 v.reset();
129 fHelper.CallFinalizeTask(slot);
130 }
131
132 /// Clean-up and finalize the action result (e.g. merging slot-local results).
133 /// It invokes the helper's Finalize method.
134 void Finalize() final
135 {
136 fHelper.Finalize();
137 SetHasRun();
138 }
139
140 std::shared_ptr<RDFGraphDrawing::GraphNode> GetGraph() final
141 {
142 auto prevNode = fPrevNode.GetGraph();
143 auto prevColumns = prevNode->GetDefinedColumns();
144
145 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
146 auto thisNode = std::make_shared<RDFGraphDrawing::GraphNode>(fHelper.GetActionName());
147
148 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns);
149
150 thisNode->AddDefinedColumns(GetColRegister().GetNames());
151 thisNode->SetAction(HasRun());
152 upmostNode->SetPrevNode(prevNode);
153 return thisNode;
154 }
155
156 /// This method is invoked to update a partial result during the event loop, right before passing the result to a
157 /// user-defined callback registered via RResultPtr::RegisterCallback
158 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
159
160 std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> &&results) final
161 {
162 const auto nVariations = GetVariations().size();
163 assert(results.size() == nVariations);
164
165 std::vector<Helper> helpers;
166 helpers.reserve(nVariations);
167
168 for (auto &&res : results)
169 helpers.emplace_back(fHelper.CallMakeNew(res));
170
171 return std::unique_ptr<RActionBase>(new RVariedAction<Helper, PrevNode, ColumnTypes_t>{
172 std::move(helpers), GetColumnNames(), fPrevNodePtr, GetColRegister()});
173 }
174
175private:
176
177 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
178};
179
180} // namespace RDF
181} // namespace Internal
182} // namespace ROOT
183
184#endif // ROOT_RACTION
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
ROOT::R::TRInterface & r
Definition Object.C:4
#define h(i)
Definition RSha256.hxx:106
long long Long64_t
Definition RtypesCore.h:80
A RDataFrame node that produces a result.
Definition RAction.hxx:53
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RAction.hxx:125
void Run(unsigned int slot, Long64_t entry) final
Definition RAction.hxx:115
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition RAction.hxx:99
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:134
RAction & operator=(const RAction &)=delete
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph() final
Definition RAction.hxx:140
void TriggerChildrenCount() final
Definition RAction.hxx:122
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:92
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:158
void CallExec(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >)
Definition RAction.hxx:109
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:177
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&results) final
Definition RAction.hxx:160
std::vector< std::array< std::unique_ptr< RColumnReaderBase >, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition RAction.hxx:60
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
A binder for user-defined columns and aliases.
Just like an RAction, but it has N action helpers (one per variation + nominal) and N previous nodes.
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)
Add the Defines that have been added between this node and the previous to the graph.
unsigned int GetNSlots()
Definition RDFUtils.cxx:285
std::array< std::unique_ptr< RDFDetail::RColumnReaderBase >, sizeof...(ColTypes)> MakeColumnReaders(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
Definition Utils.hxx:35
std::function< void(unsigned int, const ROOT::RDF::RSampleInfo &)> SampleCallback_t
The type of a data-block callback, registered with a RDataFrame computation graph via e....
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
This type aggregates some of the arguments passed to MakeColumnReaders.
Lightweight storage for a collection of types.