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 ColType>
110 auto GetValueChecked(unsigned int slot, std::size_t readerIdx, Long64_t entry) -> ColType &
111 {
112 if (auto *val = fValues[slot][readerIdx]->template TryGet<ColType>(entry))
113 return *val;
114
115 throw std::out_of_range{"RDataFrame: Filter could not retrieve value for column '" + fColumnNames[readerIdx] +
116 "' for entry " + std::to_string(entry) +
117 ". You can use the DefaultValueFor operation to provide a default value, or "
118 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
119 }
120
121 template <typename... ColTypes, std::size_t... S>
122 bool CheckFilterHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>)
123 {
124 return fFilter(GetValueChecked<ColTypes>(slot, S, entry)...);
125 // avoid unused parameter warnings (gcc 12.1)
126 (void)slot;
127 (void)entry;
128 }
129
130 void InitSlot(TTreeReader *r, unsigned int slot) final
131 {
132 RDFInternal::RColumnReadersInfo info{fColumnNames, fColRegister, fIsDefine.data(), *fLoopManager};
133 fValues[slot] = RDFInternal::GetColumnReaders(slot, r, ColumnTypes_t{}, info, fVariation);
134 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
135 }
136
137 // recursive chain of `Report`s
138 void Report(ROOT::RDF::RCutFlowReport &rep) const final { PartialReport(rep); }
139
141 {
142 fPrevNode.PartialReport(rep);
143 FillReport(rep);
144 }
145
146 void StopProcessing() final
147 {
148 ++fNStopsReceived;
149 if (fNStopsReceived == fNChildren)
150 fPrevNode.StopProcessing();
151 }
152
153 void IncrChildrenCount() final
154 {
155 ++fNChildren;
156 // propagate "children activation" upstream. named filters do the propagation via `TriggerChildrenCount`.
157 if (fNChildren == 1 && fName.empty())
158 fPrevNode.IncrChildrenCount();
159 }
160
162 {
163 assert(!fName.empty()); // this method is to only be called on named filters
164 fPrevNode.IncrChildrenCount();
165 }
166
167 void AddFilterName(std::vector<std::string> &filters) final
168 {
169 fPrevNode.AddFilterName(filters);
170 auto name = (HasName() ? fName : "Unnamed Filter");
171 filters.push_back(name);
172 }
173
174 /// Clean-up operations to be performed at the end of a task.
175 void FinalizeSlot(unsigned int slot) final { fValues[slot].fill(nullptr); }
176
177 std::shared_ptr<RDFGraphDrawing::GraphNode>
178 GetGraph(std::unordered_map<void *, std::shared_ptr<RDFGraphDrawing::GraphNode>> &visitedMap) final
179 {
180 // Recursively call for the previous node.
181 auto prevNode = fPrevNode.GetGraph(visitedMap);
182 const auto &prevColumns = prevNode->GetDefinedColumns();
183
184 auto thisNode = RDFGraphDrawing::CreateFilterNode(this, visitedMap);
185
186 /* If the returned node is not new, there is no need to perform any other operation.
187 * This is a likely scenario when building the entire graph in which branches share
188 * some nodes. */
189 if (!thisNode->IsNew()) {
190 return thisNode;
191 }
192
193 auto upmostNode = AddDefinesToGraph(thisNode, fColRegister, prevColumns, visitedMap);
194
195 // Keep track of the columns defined up to this point.
196 thisNode->AddDefinedColumns(fColRegister.GenerateColumnNames());
197
198 upmostNode->SetPrevNode(prevNode);
199 return thisNode;
200 }
201
202 /// Return a clone of this Filter that works with values in the variationName "universe".
203 std::shared_ptr<RNodeBase> GetVariedFilter(const std::string &variationName) final
204 {
205 // Only the nominal filter should be asked to produce varied filters
206 assert(fVariation == "nominal");
207 // nobody should ask for a varied filter for the nominal variation: they can just
208 // use the nominal filter!
209 assert(variationName != "nominal");
210 // nobody should ask for a varied filter for a variation on which this filter does not depend:
211 // they can just use the nominal filter.
212 assert(RDFInternal::IsStrInVec(variationName, fVariations));
213
214 auto it = fVariedFilters.find(variationName);
215 if (it != fVariedFilters.end())
216 return it->second;
217
218 auto prevNode = fPrevNodePtr;
219 if (static_cast<RNodeBase *>(fPrevNodePtr.get()) != static_cast<RNodeBase *>(fLoopManager) &&
220 RDFInternal::IsStrInVec(variationName, prevNode->GetVariations()))
221 prevNode = std::static_pointer_cast<PrevNode_t>(prevNode->GetVariedFilter(variationName));
222
223 // the varied filters get a copy of the callable object.
224 // TODO document this
225 auto variedFilter = std::unique_ptr<RFilterBase>(
226 new RFilter(fFilter, fColumnNames, std::move(prevNode), fColRegister, fName, variationName));
227 auto e = fVariedFilters.insert({variationName, std::move(variedFilter)});
228 return e.first->second;
229 }
230};
231
232} // ns RDF
233} // ns Detail
234} // ns ROOT
235
236#endif // ROOT_RFILTER
#define f(i)
Definition RSha256.hxx:104
#define e(i)
Definition RSha256.hxx:103
long long Long64_t
Definition RtypesCore.h:69
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:130
auto GetValueChecked(unsigned int slot, std::size_t readerIdx, Long64_t entry) -> ColType &
Definition RFilter.hxx:110
bool CheckFilterHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >)
Definition RFilter.hxx:122
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RFilter.hxx:175
void Report(ROOT::RDF::RCutFlowReport &rep) const final
Definition RFilter.hxx:138
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:153
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:146
void AddFilterName(std::vector< std::string > &filters) final
Definition RFilter.hxx:167
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:203
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< RDFGraphDrawing::GraphNode > > &visitedMap) final
Definition RFilter.hxx:178
typename CallableTraits< FilterF >::arg_types ColumnTypes_t
Definition RFilter.hxx:58
void TriggerChildrenCount() final
Definition RFilter.hxx:161
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:140
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:46
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:439
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
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.