Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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"
20#include "ROOT/TypeTraits.hxx"
21#include "RtypesCore.h"
22
23#include <algorithm>
24#include <cassert>
25#include <memory>
26#include <string>
27#include <unordered_map>
28#include <utility> // std::index_sequence
29#include <vector>
30
31namespace ROOT {
32
33namespace Internal {
34namespace RDF {
35using namespace ROOT::Detail::RDF;
36
37// fwd decl for RFilter
38namespace GraphDrawing {
39std::shared_ptr<GraphNode>
40CreateFilterNode(const RFilterBase *filterPtr, std::unordered_map<void *, std::shared_ptr<GraphNode>> &visitedMap);
41
42std::shared_ptr<GraphNode> AddDefinesToGraph(std::shared_ptr<GraphNode> node, const RColumnRegister &colRegister,
43 const std::vector<std::string> &prevNodeDefines,
44 std::unordered_map<void *, std::shared_ptr<GraphNode>> &visitedMap);
45} // ns GraphDrawing
46
47} // ns RDF
48} // ns Internal
49
50namespace Detail {
51namespace RDF {
52using namespace ROOT::TypeTraits;
54class RJittedFilter;
55
56template <typename FilterF, typename PrevNodeRaw>
57class R__CLING_PTRCHECK(off) RFilter final : public RFilterBase {
59 using TypeInd_t = std::make_index_sequence<ColumnTypes_t::list_size>;
60 // If the PrevNode is a RJittedFilter, treat it as a more generic RFilterBase: when dealing with systematic
61 // variations we'll have a RJittedFilter node for the nominal case but other "universes" will use concrete filters,
62 // so we normalize the "previous node type" to the base type RFilterBase.
63 using PrevNode_t = std::conditional_t<std::is_same<PrevNodeRaw, RJittedFilter>::value, RFilterBase, PrevNodeRaw>;
64
65 FilterF fFilter;
66 /// Column readers per slot and per input column
67 std::vector<std::array<RColumnReaderBase *, ColumnTypes_t::list_size>> fValues;
68 const std::shared_ptr<PrevNode_t> fPrevNodePtr;
70
71public:
72 RFilter(FilterF f, const ROOT::RDF::ColumnNames_t &columns, std::shared_ptr<PrevNode_t> pd,
73 const RDFInternal::RColumnRegister &colRegister, std::string_view name = "",
74 const std::string &variationName = "nominal")
75 : RFilterBase(pd->GetLoopManagerUnchecked(), name, pd->GetLoopManagerUnchecked()->GetNSlots(), colRegister,
76 columns, pd->GetVariations(), variationName),
77 fFilter(std::move(f)), fValues(pd->GetLoopManagerUnchecked()->GetNSlots()), fPrevNodePtr(std::move(pd)),
78 fPrevNode(*fPrevNodePtr)
79 {
80 fLoopManager->Register(this);
81 }
82
83 RFilter(const RFilter &) = delete;
84 RFilter &operator=(const RFilter &) = delete;
86 // must Deregister objects from the RLoopManager here, before the fPrevNode data member is destroyed:
87 // otherwise if fPrevNode is the RLoopManager, it will be destroyed before the calls to Deregister happen.
88 fLoopManager->Deregister(this);
89 }
90
91 bool CheckFilters(unsigned int slot, Long64_t entry) final
92 {
93 if (entry != fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()]) {
94 if (!fPrevNode.CheckFilters(slot, entry)) {
95 // a filter upstream returned false, cache the result
96 fLastResult[slot * RDFInternal::CacheLineStep<int>()] = false;
97 } else {
98 // evaluate this filter, cache the result
99 auto passed = CheckFilterHelper(slot, entry, ColumnTypes_t{}, TypeInd_t{});
100 passed ? ++fAccepted[slot * RDFInternal::CacheLineStep<ULong64_t>()]
101 : ++fRejected[slot * RDFInternal::CacheLineStep<ULong64_t>()];
102 fLastResult[slot * RDFInternal::CacheLineStep<int>()] = passed;
103 }
104 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = entry;
105 }
106 return fLastResult[slot * RDFInternal::CacheLineStep<int>()];
107 }
108
109 template <typename... ColTypes, std::size_t... S>
110 bool CheckFilterHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>)
111 {
112 return fFilter(fValues[slot][S]->template Get<ColTypes>(entry)...);
113 // avoid unused parameter warnings (gcc 12.1)
114 (void)slot;
115 (void)entry;
116 }
117
118 void InitSlot(TTreeReader *r, unsigned int slot) final
119 {
120 RDFInternal::RColumnReadersInfo info{fColumnNames, fColRegister, fIsDefine.data(), *fLoopManager};
121 fValues[slot] = RDFInternal::GetColumnReaders(slot, r, ColumnTypes_t{}, info, fVariation);
122 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
123 }
124
125 // recursive chain of `Report`s
126 void Report(ROOT::RDF::RCutFlowReport &rep) const final { PartialReport(rep); }
127
129 {
130 fPrevNode.PartialReport(rep);
131 FillReport(rep);
132 }
133
134 void StopProcessing() final
135 {
136 ++fNStopsReceived;
137 if (fNStopsReceived == fNChildren)
138 fPrevNode.StopProcessing();
139 }
140
141 void IncrChildrenCount() final
142 {
143 ++fNChildren;
144 // propagate "children activation" upstream. named filters do the propagation via `TriggerChildrenCount`.
145 if (fNChildren == 1 && fName.empty())
146 fPrevNode.IncrChildrenCount();
147 }
148
150 {
151 assert(!fName.empty()); // this method is to only be called on named filters
152 fPrevNode.IncrChildrenCount();
153 }
154
155 void AddFilterName(std::vector<std::string> &filters) final
156 {
157 fPrevNode.AddFilterName(filters);
158 auto name = (HasName() ? fName : "Unnamed Filter");
159 filters.push_back(name);
160 }
161
162 /// Clean-up operations to be performed at the end of a task.
163 void FinalizeSlot(unsigned int slot) final { fValues[slot].fill(nullptr); }
164
165 std::shared_ptr<RDFGraphDrawing::GraphNode>
166 GetGraph(std::unordered_map<void *, std::shared_ptr<RDFGraphDrawing::GraphNode>> &visitedMap) final
167 {
168 // Recursively call for the previous node.
169 auto prevNode = fPrevNode.GetGraph(visitedMap);
170 const auto &prevColumns = prevNode->GetDefinedColumns();
171
172 auto thisNode = RDFGraphDrawing::CreateFilterNode(this, visitedMap);
173
174 /* If the returned node is not new, there is no need to perform any other operation.
175 * This is a likely scenario when building the entire graph in which branches share
176 * some nodes. */
177 if (!thisNode->IsNew()) {
178 return thisNode;
179 }
180
181 auto upmostNode = AddDefinesToGraph(thisNode, fColRegister, prevColumns, visitedMap);
182
183 // Keep track of the columns defined up to this point.
184 thisNode->AddDefinedColumns(fColRegister.GetNames());
185
186 upmostNode->SetPrevNode(prevNode);
187 return thisNode;
188 }
189
190 /// Return a clone of this Filter that works with values in the variationName "universe".
191 std::shared_ptr<RNodeBase> GetVariedFilter(const std::string &variationName) final
192 {
193 // Only the nominal filter should be asked to produce varied filters
194 assert(fVariation == "nominal");
195 // nobody should ask for a varied filter for the nominal variation: they can just
196 // use the nominal filter!
197 assert(variationName != "nominal");
198 // nobody should ask for a varied filter for a variation on which this filter does not depend:
199 // they can just use the nominal filter.
200 assert(RDFInternal::IsStrInVec(variationName, fVariations));
201
202 auto it = fVariedFilters.find(variationName);
203 if (it != fVariedFilters.end())
204 return it->second;
205
206 auto prevNode = fPrevNodePtr;
207 if (static_cast<RNodeBase *>(fPrevNodePtr.get()) != static_cast<RNodeBase *>(fLoopManager) &&
208 RDFInternal::IsStrInVec(variationName, prevNode->GetVariations()))
209 prevNode = std::static_pointer_cast<PrevNode_t>(prevNode->GetVariedFilter(variationName));
210
211 // the varied filters get a copy of the callable object.
212 // TODO document this
213 auto variedFilter = std::unique_ptr<RFilterBase>(
214 new RFilter(fFilter, fColumnNames, std::move(prevNode), fColRegister, fName, variationName));
215 auto e = fVariedFilters.insert({variationName, std::move(variedFilter)});
216 return e.first->second;
217 }
218};
219
220} // ns RDF
221} // ns Detail
222} // ns ROOT
223
224#endif // ROOT_RFILTER
#define f(i)
Definition RSha256.hxx:104
#define e(i)
Definition RSha256.hxx:103
long long Long64_t
Definition RtypesCore.h:80
const char * filters[]
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
char name[80]
Definition TGX11.cxx:110
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition RFilter.hxx:118
bool CheckFilterHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >)
Definition RFilter.hxx:110
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RFilter.hxx:163
void Report(ROOT::RDF::RCutFlowReport &rep) const final
Definition RFilter.hxx:126
RFilter(FilterF f, const ROOT::RDF::ColumnNames_t &columns, std::shared_ptr< PrevNode_t > pd, const RDFInternal::RColumnRegister &colRegister, std::string_view name="", const std::string &variationName="nominal")
Definition RFilter.hxx:72
bool CheckFilters(unsigned int slot, Long64_t entry) final
Definition RFilter.hxx:91
RFilter & operator=(const RFilter &)=delete
void IncrChildrenCount() final
Definition RFilter.hxx:141
const std::shared_ptr< PrevNode_t > fPrevNodePtr
Definition RFilter.hxx:68
std::conditional_t< std::is_same< PrevNodeRaw, RJittedFilter >::value, RFilterBase, PrevNodeRaw > PrevNode_t
Definition RFilter.hxx:63
RFilter(const RFilter &)=delete
std::make_index_sequence< ColumnTypes_t::list_size > TypeInd_t
Definition RFilter.hxx:59
void StopProcessing() final
Definition RFilter.hxx:134
void AddFilterName(std::vector< std::string > &filters) final
Definition RFilter.hxx:155
std::shared_ptr< RNodeBase > GetVariedFilter(const std::string &variationName) final
Return a clone of this Filter that works with values in the variationName "universe".
Definition RFilter.hxx:191
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< RDFGraphDrawing::GraphNode > > &visitedMap) final
Definition RFilter.hxx:166
typename CallableTraits< FilterF >::arg_types ColumnTypes_t
Definition RFilter.hxx:58
void TriggerChildrenCount() final
Definition RFilter.hxx:149
std::vector< std::array< RColumnReaderBase *, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition RFilter.hxx:67
void PartialReport(ROOT::RDF::RCutFlowReport &rep) const final
Definition RFilter.hxx:128
A wrapper around a concrete RFilter, which forwards all calls to it RJittedFilter is the type of the ...
Base class for non-leaf nodes of the computational graph.
Definition RNodeBase.hxx:43
A binder for user-defined columns, variations and aliases.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:44
std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr, std::unordered_map< void *, std::shared_ptr< GraphNode > > &visitedMap)
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)
bool IsStrInVec(const std::string &str, const std::vector< std::string > &vec)
Definition RDFUtils.cxx:417
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
ROOT type_traits extensions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
Extract types from the signature of a callable object. See CallableTraits.
This type aggregates some of the arguments passed to GetColumnReaders.
Lightweight storage for a collection of types.