Logo ROOT  
Reference Guide
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 std::unordered_map<void *, std::shared_ptr<GraphNode>> &visitedMap);
40} // namespace GraphDrawing
41
42// clang-format off
43/**
44 * \class ROOT::Internal::RDF::RAction
45 * \ingroup dataframe
46 * \brief A RDataFrame node that produces a result
47 * \tparam Helper The action helper type, which implements the concrete action logic (e.g. FillHelper, SnapshotHelper)
48 * \tparam PrevNode The type of the parent node in the computation graph
49 * \tparam ColumnTypes_t A TypeList with the types of the input columns
50 *
51 */
52// clang-format on
53template <typename Helper, typename PrevNode, typename ColumnTypes_t = typename Helper::ColumnTypes_t>
54class R__CLING_PTRCHECK(off) RAction : public RActionBase {
55 using TypeInd_t = std::make_index_sequence<ColumnTypes_t::list_size>;
56
57 Helper fHelper;
58 const std::shared_ptr<PrevNode> fPrevNodePtr;
59 PrevNode &fPrevNode;
60 /// Column readers per slot and per input column
61 std::vector<std::array<RColumnReaderBase *, ColumnTypes_t::list_size>> fValues;
62
63 /// The nth flag signals whether the nth input column is a custom column or not.
64 std::array<bool, ColumnTypes_t::list_size> fIsDefine;
65
66public:
67 RAction(Helper &&h, const ColumnNames_t &columns, std::shared_ptr<PrevNode> pd, const RColumnRegister &colRegister)
68 : RActionBase(pd->GetLoopManagerUnchecked(), columns, colRegister, pd->GetVariations()),
69 fHelper(std::forward<Helper>(h)), fPrevNodePtr(std::move(pd)), fPrevNode(*fPrevNodePtr), fValues(GetNSlots())
70 {
71 fLoopManager->Register(this);
72
73 const auto nColumns = columns.size();
74 for (auto i = 0u; i < nColumns; ++i)
75 fIsDefine[i] = colRegister.IsDefineOrAlias(columns[i]);
76 }
77
78 RAction(const RAction &) = delete;
79 RAction &operator=(const RAction &) = delete;
80
81 ~RAction() { fLoopManager->Deregister(this); }
82
83 /**
84 Retrieve a wrapper to the result of the action that knows how to merge
85 with others of the same type.
86 */
87 std::unique_ptr<RDFDetail::RMergeableValueBase> GetMergeableValue() const final
88 {
89 return fHelper.GetMergeableValue();
90 }
91
92 void Initialize() final { fHelper.Initialize(); }
93
94 void InitSlot(TTreeReader *r, unsigned int slot) final
95 {
97 fIsDefine.data(), *fLoopManager};
98 fValues[slot] = RDFInternal::GetColumnReaders(slot, r, ColumnTypes_t{}, info);
99 fHelper.InitTask(r, slot);
100 }
101
102 template <typename... ColTypes, std::size_t... S>
103 void CallExec(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>)
104 {
105 fHelper.Exec(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
106 (void)entry; // avoid unused parameter warning (gcc 12.1)
107 }
108
109 void Run(unsigned int slot, Long64_t entry) final
110 {
111 // check if entry passes all filters
112 if (fPrevNode.CheckFilters(slot, entry))
113 CallExec(slot, entry, ColumnTypes_t{}, TypeInd_t{});
114 }
115
116 void TriggerChildrenCount() final { fPrevNode.IncrChildrenCount(); }
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].fill(nullptr);
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<RDFGraphDrawing::GraphNode>
134 GetGraph(std::unordered_map<void *, std::shared_ptr<RDFGraphDrawing::GraphNode>> &visitedMap) final
135 {
136 auto prevNode = fPrevNode.GetGraph(visitedMap);
137 const auto &prevColumns = prevNode->GetDefinedColumns();
138
139 // Action nodes do not need to go through CreateFilterNode: they are never common nodes between multiple branches
140 const auto nodeType = HasRun() ? RDFGraphDrawing::ENodeType::kUsedAction : RDFGraphDrawing::ENodeType::kAction;
141 auto thisNode =
142 std::make_shared<RDFGraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
143 visitedMap[(void *)this] = thisNode;
144
145 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
146
147 thisNode->AddDefinedColumns(GetColRegister().GetNames());
148 upmostNode->SetPrevNode(prevNode);
149 return thisNode;
150 }
151
152 /// This method is invoked to update a partial result during the event loop, right before passing the result to a
153 /// user-defined callback registered via RResultPtr::RegisterCallback
154 void *PartialUpdate(unsigned int slot) final { return fHelper.CallPartialUpdate(slot); }
155
156 std::unique_ptr<RActionBase> MakeVariedAction(std::vector<void *> &&results) final
157 {
158 const auto nVariations = GetVariations().size();
159 assert(results.size() == nVariations);
160
161 std::vector<Helper> helpers;
162 helpers.reserve(nVariations);
163
164 for (auto &&res : results)
165 helpers.emplace_back(fHelper.CallMakeNew(res));
166
167 return std::unique_ptr<RActionBase>(new RVariedAction<Helper, PrevNode, ColumnTypes_t>{
168 std::move(helpers), GetColumnNames(), fPrevNodePtr, GetColRegister()});
169 }
170
171private:
172
173 ROOT::RDF::SampleCallback_t GetSampleCallback() final { return fHelper.GetSampleCallback(); }
174};
175
176} // namespace RDF
177} // namespace Internal
178} // namespace ROOT
179
180#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
const ColumnNames_t & GetColumnNames() const
Definition: RActionBase.hxx:62
RColumnRegister & GetColRegister()
Definition: RActionBase.hxx:63
A RDataFrame node that produces a result.
Definition: RAction.hxx:54
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< RDFGraphDrawing::GraphNode > > &visitedMap) final
Definition: RAction.hxx:134
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition: RAction.hxx:119
void Run(unsigned int slot, Long64_t entry) final
Definition: RAction.hxx:109
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition: RAction.hxx:94
std::make_index_sequence< ColumnTypes_t::list_size > TypeInd_t
Definition: RAction.hxx:55
void Finalize() final
Clean-up and finalize the action result (e.g.
Definition: RAction.hxx:127
RAction & operator=(const RAction &)=delete
void TriggerChildrenCount() final
Definition: RAction.hxx:116
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:87
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:154
void CallExec(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >)
Definition: RAction.hxx:103
RAction(Helper &&h, const ColumnNames_t &columns, std::shared_ptr< PrevNode > pd, const RColumnRegister &colRegister)
Definition: RAction.hxx:67
const std::shared_ptr< PrevNode > fPrevNodePtr
Definition: RAction.hxx:58
ROOT::RDF::SampleCallback_t GetSampleCallback() final
Definition: RAction.hxx:173
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&results) final
Definition: RAction.hxx:156
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:64
std::vector< std::array< RColumnReaderBase *, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition: RAction.hxx:61
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 (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 RDFInternal::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.
void(off) SmallVectorTemplateBase< T
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 a RDataFrame computation graph via e....
Definition: RSampleInfo.hxx:84
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
RooArgSet S(Args_t &&... args)
Definition: RooArgSet.h:240
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:546
This type aggregates some of the arguments passed to GetColumnReaders.
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25