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