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