Logo ROOT   6.16/01
Reference Guide
RFilter.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 09/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2018, 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_RFILTER
12#define ROOT_RFILTER
13
17#include "ROOT/RDF/Utils.hxx"
21#include "ROOT/TypeTraits.hxx"
22#include "RtypesCore.h"
23
24#include <algorithm>
25#include <memory>
26#include <string>
27#include <vector>
28
29namespace ROOT {
30
31namespace Internal {
32namespace RDF {
33using namespace ROOT::Detail::RDF;
34
35// fwd decl for RFilter
36namespace GraphDrawing {
37std::shared_ptr<GraphNode> CreateFilterNode(const RFilterBase *filterPtr);
38
39bool CheckIfDefaultOrDSColumn(const std::string &name, const std::shared_ptr<RCustomColumnBase> &column);
40
41std::shared_ptr<GraphNode>
42CreateDefineNode(const std::string &columnName, const RDFDetail::RCustomColumnBase *columnPtr);
43} // ns GraphDrawing
44
45} // ns RDF
46} // ns Internal
47
48namespace Detail {
49namespace RDF {
50using namespace ROOT::TypeTraits;
52
53template <typename FilterF, typename PrevDataFrame>
54class RFilter final : public RFilterBase {
55 using ColumnTypes_t = typename CallableTraits<FilterF>::arg_types;
57
58 FilterF fFilter;
60 const std::shared_ptr<PrevDataFrame> fPrevDataPtr;
61 PrevDataFrame &fPrevData;
62 std::vector<RDFInternal::RDFValueTuple_t<ColumnTypes_t>> fValues;
63
64public:
65 RFilter(FilterF &&f, const ColumnNames_t &bl, std::shared_ptr<PrevDataFrame> pd,
68 fFilter(std::forward<FilterF>(f)), fBranches(bl), fPrevDataPtr(std::move(pd)), fPrevData(*fPrevDataPtr),
69 fValues(fNSlots) { }
70
71 RFilter(const RFilter &) = delete;
72 RFilter &operator=(const RFilter &) = delete;
73 // must call Deregister here, before fPrevDataFrame is destroyed,
74 // otherwise if fPrevDataFrame is fLoopManager we get a use after delete
76
77 bool CheckFilters(unsigned int slot, Long64_t entry) final
78 {
79 if (entry != fLastCheckedEntry[slot]) {
80 if (!fPrevData.CheckFilters(slot, entry)) {
81 // a filter upstream returned false, cache the result
82 fLastResult[slot] = false;
83 } else {
84 // evaluate this filter, cache the result
85 auto passed = CheckFilterHelper(slot, entry, TypeInd_t());
86 passed ? ++fAccepted[slot] : ++fRejected[slot];
87 fLastResult[slot] = passed;
88 }
89 fLastCheckedEntry[slot] = entry;
90 }
91 return fLastResult[slot];
92 }
93
94 template <std::size_t... S>
96 {
97 // silence "unused parameter" warnings in gcc
98 (void)slot;
99 (void)entry;
100 return fFilter(std::get<S>(fValues[slot]).Get(entry)...);
101 }
102
103 void InitSlot(TTreeReader *r, unsigned int slot) final
104 {
105 for (auto &bookedBranch : fCustomColumns.GetColumns())
106 bookedBranch.second->InitSlot(r, slot);
108 }
109
110 // recursive chain of `Report`s
111 void Report(ROOT::RDF::RCutFlowReport &rep) const final { PartialReport(rep); }
112
114 {
115 fPrevData.PartialReport(rep);
116 FillReport(rep);
117 }
118
119 void StopProcessing() final
120 {
123 fPrevData.StopProcessing();
124 }
125
126 void IncrChildrenCount() final
127 {
128 ++fNChildren;
129 // propagate "children activation" upstream. named filters do the propagation via `TriggerChildrenCount`.
130 if (fNChildren == 1 && fName.empty())
131 fPrevData.IncrChildrenCount();
132 }
133
135 {
136 R__ASSERT(!fName.empty()); // this method is to only be called on named filters
137 fPrevData.IncrChildrenCount();
138 }
139
140 virtual void ClearValueReaders(unsigned int slot) final
141 {
143 }
144
145 void AddFilterName(std::vector<std::string> &filters)
146 {
147 fPrevData.AddFilterName(filters);
148 auto name = (HasName() ? fName : "Unnamed Filter");
149 filters.push_back(name);
150 }
151
152 virtual void ClearTask(unsigned int slot) final
153 {
154 for (auto &column : fCustomColumns.GetColumns()) {
155 column.second->ClearValueReaders(slot);
156 }
157
158 ClearValueReaders(slot);
159 }
160
161 std::shared_ptr<RDFGraphDrawing::GraphNode> GetGraph()
162 {
163 // Recursively call for the previous node.
164 auto prevNode = fPrevData.GetGraph();
165 auto prevColumns = prevNode->GetDefinedColumns();
166
167 auto thisNode = RDFGraphDrawing::CreateFilterNode(this);
168
169 /* If the returned node is not new, there is no need to perform any other operation.
170 * This is a likely scenario when building the entire graph in which branches share
171 * some nodes. */
172 if (!thisNode->GetIsNew()) {
173 return thisNode;
174 }
175
176 auto evaluatedNode = thisNode;
177 /* Each column that this node has but the previous hadn't has been defined in between,
178 * so it has to be built and appended. */
179
180 for (auto &column : fCustomColumns.GetColumns()) {
181 // Even if treated as custom columns by the Dataframe, datasource columns must not be in the graph.
182 if (RDFGraphDrawing::CheckIfDefaultOrDSColumn(column.first, column.second))
183 continue;
184 if (std::find(prevColumns.begin(), prevColumns.end(), column.first) == prevColumns.end()) {
185 auto defineNode = RDFGraphDrawing::CreateDefineNode(column.first, column.second.get());
186 evaluatedNode->SetPrevNode(defineNode);
187 evaluatedNode = defineNode;
188 }
189 }
190
191 // Keep track of the columns defined up to this point.
192 thisNode->AddDefinedColumns(fCustomColumns.GetNames());
193
194 evaluatedNode->SetPrevNode(prevNode);
195 return thisNode;
196 }
197};
198
199} // ns RDF
200} // ns Detail
201} // ns ROOT
202
203#endif // ROOT_RFILTER
ROOT::R::TRInterface & r
Definition: Object.C:4
#define f(i)
Definition: RSha256.hxx:104
long long Long64_t
Definition: RtypesCore.h:69
#define R__ASSERT(e)
Definition: TError.h:96
const char * filters[]
typedef void((*Func_t)())
const unsigned int fNSlots
Number of thread slots used by this node, inherited from parent node.
Definition: RFilterBase.hxx:43
RDFInternal::RBookedCustomColumns fCustomColumns
Definition: RFilterBase.hxx:45
std::vector< ULong64_t > fRejected
Definition: RFilterBase.hxx:41
virtual void FillReport(ROOT::RDF::RCutFlowReport &) const
Definition: RFilterBase.cxx:35
std::vector< int > fLastResult
Definition: RFilterBase.hxx:39
std::vector< ULong64_t > fAccepted
Definition: RFilterBase.hxx:40
std::vector< Long64_t > fLastCheckedEntry
Definition: RFilterBase.hxx:38
const ColumnNames_t fBranches
Definition: RFilter.hxx:59
void StopProcessing() final
Definition: RFilter.hxx:119
virtual void ClearTask(unsigned int slot) final
Definition: RFilter.hxx:152
void PartialReport(ROOT::RDF::RCutFlowReport &rep) const final
Definition: RFilter.hxx:113
bool CheckFilterHelper(unsigned int slot, Long64_t entry, std::index_sequence< S... >)
Definition: RFilter.hxx:95
void Report(ROOT::RDF::RCutFlowReport &rep) const final
Definition: RFilter.hxx:111
const std::shared_ptr< PrevDataFrame > fPrevDataPtr
Definition: RFilter.hxx:60
void AddFilterName(std::vector< std::string > &filters)
Definition: RFilter.hxx:145
virtual void ClearValueReaders(unsigned int slot) final
Definition: RFilter.hxx:140
void IncrChildrenCount() final
Definition: RFilter.hxx:126
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition: RFilter.hxx:103
RFilter & operator=(const RFilter &)=delete
RFilter(FilterF &&f, const ColumnNames_t &bl, std::shared_ptr< PrevDataFrame > pd, const RDFInternal::RBookedCustomColumns &customColumns, std::string_view name="")
Definition: RFilter.hxx:65
void TriggerChildrenCount() final
Definition: RFilter.hxx:134
typename CallableTraits< FilterF >::arg_types ColumnTypes_t
Definition: RFilter.hxx:55
bool CheckFilters(unsigned int slot, Long64_t entry) final
Definition: RFilter.hxx:77
RFilter(const RFilter &)=delete
std::make_index_sequence< ColumnTypes_t::list_size > TypeInd_t
Definition: RFilter.hxx:56
std::vector< RDFInternal::RDFValueTuple_t< ColumnTypes_t > > fValues
Definition: RFilter.hxx:62
PrevDataFrame & fPrevData
Definition: RFilter.hxx:61
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph()
Definition: RFilter.hxx:161
void Deregister(RDFInternal::RActionBase *actionPtr)
virtual RLoopManager * GetLoopManagerUnchecked()
Definition: RNodeBase.hxx:64
unsigned int fNStopsReceived
Number of times that a children node signaled to stop processing entries.
Definition: RNodeBase.hxx:45
unsigned int fNChildren
Number of nodes of the functional graph hanging from this object.
Definition: RNodeBase.hxx:44
RLoopManager * fLoopManager
Definition: RNodeBase.hxx:43
Encapsulates the columns defined by the user.
ColumnNames_t GetNames() const
Returns the list of the names of the defined columns.
RCustomColumnBasePtrMap_t GetColumns() const
Returns the list of the pointers to the defined columns.
A simple, robust and fast interface to read values from ROOT colmnar datasets such as TTree,...
Definition: TTreeReader.h:44
bool CheckIfDefaultOrDSColumn(const std::string &name, const std::shared_ptr< ROOT::Detail::RDF::RCustomColumnBase > &column)
std::shared_ptr< GraphNode > CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RCustomColumnBase *columnPtr)
std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr)
void InitRDFValues(unsigned int slot, RDFValueTuple &valueTuple, TTreeReader *r, const ColumnNames_t &bn, const RBookedCustomColumns &customCols, std::index_sequence< S... >)
Initialize a tuple of RColumnValues.
Definition: NodesUtils.hxx:54
unsigned int GetNSlots()
Definition: RDFUtils.cxx:246
void ResetRDFValueTuple(std::vector< RTypeErasedColumnValue > &values, std::index_sequence< S... >, ROOT::TypeTraits::TypeList< ColTypes... >)
This overload is specialized to act on RTypeErasedColumnValues instead of RColumnValues.
Definition: RAction.hxx:90
ROOT type_traits extensions.
Definition: TypeTraits.hxx:23
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
ROOT::Detail::RDF::ColumnNames_t ColumnNames_t
Definition: RDataFrame.cxx:790
RooArgSet S(const RooAbsArg &v1)
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:544
STL namespace.
basic_string_view< char > string_view
Definition: RStringView.hxx:35
make_integer_sequence< size_t, _Np > make_index_sequence