Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RRange.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_RDFRANGE
12#define ROOT_RDFRANGE
13
16#include "ROOT/RDF/Utils.hxx"
17#include "RtypesCore.h"
18
19#include <cassert>
20#include <memory>
21
22namespace ROOT {
23
24// fwd decl
25namespace Internal {
26namespace RDF {
27namespace GraphDrawing {
28std::shared_ptr<GraphNode> CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr);
29} // ns GraphDrawing
30} // ns RDF
31} // ns Internal
32
33namespace Detail {
34namespace RDF {
36class RJittedFilter;
37
38template <typename PrevNodeRaw>
39class RRange final : public RRangeBase {
40 // If the PrevNode is a RJittedFilter, treat it as a more generic RFilterBase: when dealing with systematic
41 // variations we'll have a RJittedFilter node for the nominal case but other "universes" will use concrete filters,
42 // so we normalize the "previous node type" to the base type RFilterBase.
43 using PrevNode_t = std::conditional_t<std::is_same<PrevNodeRaw, RJittedFilter>::value, RFilterBase, PrevNodeRaw>;
44 const std::shared_ptr<PrevNode_t> fPrevNodePtr;
46
47public:
48 RRange(unsigned int start, unsigned int stop, unsigned int stride, std::shared_ptr<PrevNode_t> pd)
49 : RRangeBase(pd->GetLoopManagerUnchecked(), start, stop, stride, pd->GetLoopManagerUnchecked()->GetNSlots(),
50 pd->GetVariations()),
51 fPrevNodePtr(std::move(pd)), fPrevNode(*fPrevNodePtr)
52 {
53 fLoopManager->Book(this);
54 }
55
56 RRange(const RRange &) = delete;
57 RRange &operator=(const RRange &) = delete;
58 // must call Deregister here, before fPrevNode is destroyed,
59 // otherwise if fPrevNode is fLoopManager we get a use after delete
61
62 /// Ranges act as filters when it comes to selecting entries that downstream nodes should process
63 bool CheckFilters(unsigned int slot, Long64_t entry) final
64 {
65 if (entry != fLastCheckedEntry) {
66 if (fHasStopped)
67 return false;
68 if (!fPrevNode.CheckFilters(slot, entry)) {
69 // a filter upstream returned false, cache the result
70 fLastResult = false;
71 } else {
72 // apply range filter logic, cache the result
74 (fStride != 1 && (fNProcessedEntries - fStart) % fStride != 0))
75 fLastResult = false;
76 else
77 fLastResult = true;
80 fHasStopped = true;
81 fPrevNode.StopProcessing();
82 }
83 }
84 fLastCheckedEntry = entry;
85 }
86 return fLastResult;
87 }
88
89 // recursive chain of `Report`s
90 // RRange simply forwards these calls to the previous node
91 void Report(ROOT::RDF::RCutFlowReport &rep) const final { fPrevNode.PartialReport(rep); }
92
93 void PartialReport(ROOT::RDF::RCutFlowReport &rep) const final { fPrevNode.PartialReport(rep); }
94
95 void StopProcessing() final
96 {
99 fPrevNode.StopProcessing();
100 }
101
102 void IncrChildrenCount() final
103 {
104 ++fNChildren;
105 // propagate "children activation" upstream
106 if (fNChildren == 1)
107 fPrevNode.IncrChildrenCount();
108 }
109
110 /// This function must be defined by all nodes, but only the filters will add their name
111 void AddFilterName(std::vector<std::string> &filters) { fPrevNode.AddFilterName(filters); }
112 std::shared_ptr<RDFGraphDrawing::GraphNode> GetGraph()
113 {
114 // TODO: Ranges node have no information about custom columns, hence it is not possible now
115 // if defines have been used before.
116 auto prevNode = fPrevNode.GetGraph();
117 auto prevColumns = prevNode->GetDefinedColumns();
118
119 auto thisNode = RDFGraphDrawing::CreateRangeNode(this);
120
121 /* If the returned node is not new, there is no need to perform any other operation.
122 * This is a likely scenario when building the entire graph in which branches share
123 * some nodes. */
124 if (!thisNode->GetIsNew()) {
125 return thisNode;
126 }
127 thisNode->SetPrevNode(prevNode);
128
129 // If there have been some defines between the last Filter and this Range node we won't detect them:
130 // Ranges don't keep track of Defines (they have no RColumnRegister data member).
131 // Let's pretend that the Defines of this node are the same as the node above, so that in the graph
132 // the Defines will just appear below the Range instead (no functional change).
133 thisNode->AddDefinedColumns(prevColumns);
134
135 return thisNode;
136 }
137
138 std::shared_ptr<RNodeBase> GetVariedFilter(const std::string &variationName) final
139 {
140 // nobody should ask for a varied filter for the nominal variation: they can just
141 // use the nominal filter!
142 assert(variationName != "nominal");
143 // nobody should ask for a varied filter for a variation on which this filter does not depend:
144 // they can just use the nominal filter.
145 assert(RDFInternal::IsStrInVec(variationName, fVariations));
146
147 auto it = fVariedRanges.find(variationName);
148 if (it != fVariedRanges.end())
149 return it->second;
150
151 auto prevNode = fPrevNodePtr;
152 if (static_cast<RNodeBase *>(fPrevNodePtr.get()) != static_cast<RNodeBase *>(fLoopManager) &&
153 RDFInternal::IsStrInVec(variationName, prevNode->GetVariations()))
154 prevNode = std::static_pointer_cast<PrevNode_t>(prevNode->GetVariedFilter(variationName));
155
156 auto variedRange = std::unique_ptr<RRangeBase>(new RRange(fStart, fStop, fStride, std::move(prevNode)));
157 auto e = fVariedRanges.insert({variationName, std::move(variedRange)});
158 return e.first->second;
159 }
160};
161
162} // namespace RDF
163} // namespace Detail
164} // namespace ROOT
165
166#endif // ROOT_RDFRANGE
#define e(i)
Definition RSha256.hxx:103
long long Long64_t
Definition RtypesCore.h:80
const char * filters[]
void Book(RDFInternal::RActionBase *actionPtr)
void Deregister(RDFInternal::RActionBase *actionPtr)
Base class for non-leaf nodes of the computational graph.
Definition RNodeBase.hxx:42
const std::vector< std::string > & GetVariations() const
Definition RNodeBase.hxx:72
virtual RLoopManager * GetLoopManagerUnchecked()
Definition RNodeBase.hxx:70
unsigned int fNStopsReceived
Number of times that a children node signaled to stop processing entries.
Definition RNodeBase.hxx:46
unsigned int fNChildren
Number of nodes of the functional graph hanging from this object.
Definition RNodeBase.hxx:45
std::vector< std::string > fVariations
List of systematic variations that affect this node.
Definition RNodeBase.hxx:47
std::unordered_map< std::string, std::shared_ptr< RRangeBase > > fVariedRanges
bool fHasStopped
True if the end of the range has been reached.
bool CheckFilters(unsigned int slot, Long64_t entry) final
Ranges act as filters when it comes to selecting entries that downstream nodes should process.
Definition RRange.hxx:63
const std::shared_ptr< PrevNode_t > fPrevNodePtr
Definition RRange.hxx:44
PrevNode_t & fPrevNode
Definition RRange.hxx:45
RRange & operator=(const RRange &)=delete
std::conditional_t< std::is_same< PrevNodeRaw, RJittedFilter >::value, RFilterBase, PrevNodeRaw > PrevNode_t
Definition RRange.hxx:43
void StopProcessing() final
Definition RRange.hxx:95
void IncrChildrenCount() final
Definition RRange.hxx:102
void AddFilterName(std::vector< std::string > &filters)
This function must be defined by all nodes, but only the filters will add their name.
Definition RRange.hxx:111
std::shared_ptr< RDFGraphDrawing::GraphNode > GetGraph()
Definition RRange.hxx:112
void Report(ROOT::RDF::RCutFlowReport &rep) const final
Definition RRange.hxx:91
RRange(const RRange &)=delete
RRange(unsigned int start, unsigned int stop, unsigned int stride, std::shared_ptr< PrevNode_t > pd)
Definition RRange.hxx:48
std::shared_ptr< RNodeBase > GetVariedFilter(const std::string &variationName) final
Return a clone of this node that acts as a Filter working with values in the variationName "universe"...
Definition RRange.hxx:138
void PartialReport(ROOT::RDF::RCutFlowReport &rep) const final
Definition RRange.hxx:93
std::shared_ptr< GraphNode > CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr)
bool IsStrInVec(const std::string &str, const std::vector< std::string > &vec)
Definition RDFUtils.cxx:419
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...