Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
15 #include "ROOT/RDF/RRangeBase.hxx"
16 #include "RtypesCore.h"
17 
18 #include <memory>
19 
20 namespace ROOT {
21 
22 // fwd decl
23 namespace Internal {
24 namespace RDF {
25 namespace GraphDrawing {
26 std::shared_ptr<GraphNode> CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr);
27 } // ns GraphDrawing
28 } // ns RDF
29 } // ns Internal
30 
31 namespace Detail {
32 namespace RDF {
33 namespace RDFGraphDrawing = ROOT::Internal::RDF::GraphDrawing;
34 
35 template <typename PrevData>
36 class RRange final : public RRangeBase {
37  const std::shared_ptr<PrevData> fPrevDataPtr;
38  PrevData &fPrevData;
39 
40 public:
41  RRange(unsigned int start, unsigned int stop, unsigned int stride, std::shared_ptr<PrevData> pd)
42  : RRangeBase(pd->GetLoopManagerUnchecked(), start, stop, stride, pd->GetLoopManagerUnchecked()->GetNSlots()),
43  fPrevDataPtr(std::move(pd)), fPrevData(*fPrevDataPtr) {}
44 
45  RRange(const RRange &) = delete;
46  RRange &operator=(const RRange &) = delete;
47  // must call Deregister here, before fPrevDataFrame is destroyed,
48  // otherwise if fPrevDataFrame is fLoopManager we get a use after delete
49  ~RRange() { fLoopManager->Deregister(this); }
50 
51  /// Ranges act as filters when it comes to selecting entries that downstream nodes should process
52  bool CheckFilters(unsigned int slot, Long64_t entry) final
53  {
54  if (entry != fLastCheckedEntry) {
55  if (fHasStopped)
56  return false;
57  if (!fPrevData.CheckFilters(slot, entry)) {
58  // a filter upstream returned false, cache the result
59  fLastResult = false;
60  } else {
61  // apply range filter logic, cache the result
62  ++fNProcessedEntries;
63  if (fNProcessedEntries <= fStart || (fStop > 0 && fNProcessedEntries > fStop) ||
64  (fStride != 1 && fNProcessedEntries % fStride != 0))
65  fLastResult = false;
66  else
67  fLastResult = true;
68  if (fNProcessedEntries == fStop) {
69  fHasStopped = true;
70  fPrevData.StopProcessing();
71  }
72  }
73  fLastCheckedEntry = entry;
74  }
75  return fLastResult;
76  }
77 
78  // recursive chain of `Report`s
79  // RRange simply forwards these calls to the previous node
80  void Report(ROOT::RDF::RCutFlowReport &rep) const final { fPrevData.PartialReport(rep); }
81 
82  void PartialReport(ROOT::RDF::RCutFlowReport &rep) const final { fPrevData.PartialReport(rep); }
83 
84  void StopProcessing() final
85  {
86  ++fNStopsReceived;
87  if (fNStopsReceived == fNChildren && !fHasStopped)
88  fPrevData.StopProcessing();
89  }
90 
91  void IncrChildrenCount() final
92  {
93  ++fNChildren;
94  // propagate "children activation" upstream
95  if (fNChildren == 1)
96  fPrevData.IncrChildrenCount();
97  }
98 
99  /// This function must be defined by all nodes, but only the filters will add their name
100  void AddFilterName(std::vector<std::string> &filters) { fPrevData.AddFilterName(filters); }
101  std::shared_ptr<RDFGraphDrawing::GraphNode> GetGraph()
102  {
103  // TODO: Ranges node have no information about custom columns, hence it is not possible now
104  // if defines have been used before.
105  auto prevNode = fPrevData.GetGraph();
106  auto prevColumns = prevNode->GetDefinedColumns();
107 
108  auto thisNode = RDFGraphDrawing::CreateRangeNode(this);
109 
110  /* If the returned node is not new, there is no need to perform any other operation.
111  * This is a likely scenario when building the entire graph in which branches share
112  * some nodes. */
113  if (!thisNode->GetIsNew()) {
114  return thisNode;
115  }
116  thisNode->SetPrevNode(prevNode);
117 
118  // If there have been some defines before it, this node won't detect them.
119  thisNode->AddDefinedColumns(prevColumns);
120 
121  return thisNode;
122  }
123 };
124 
125 } // namespace RDF
126 } // namespace Detail
127 } // namespace ROOT
128 
129 #endif // ROOT_RDFRANGE