Logo ROOT  
Reference Guide
GraphUtils.hxx
Go to the documentation of this file.
1 // Author: Enrico Guiraud, Danilo Piparo, CERN, Massimo Tumolo Politecnico di Torino 08/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_GRAPHUTILS
12 #define ROOT_GRAPHUTILS
13 
14 #include <string>
15 #include <sstream>
16 #include <vector>
17 #include <map>
18 #include <memory>
19 #include <type_traits>
20 #include <ROOT/RDataFrame.hxx>
21 #include <ROOT/RDF/RInterface.hxx>
22 #include <ROOT/RDF/GraphNode.hxx>
23 
24 #include <iostream>
25 
26 namespace ROOT {
27 namespace Detail {
28 namespace RDF {
29 class RDefineBase;
30 class RFilterBase;
31 class RRangeBase;
32 } // namespace RDF
33 } // namespace Detail
34 
35 namespace Internal {
36 namespace RDF {
37 class RBookedDefines;
38 
39 namespace GraphDrawing {
40 std::shared_ptr<GraphNode>
41 CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr);
42 
43 std::shared_ptr<GraphNode> CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr);
44 
45 std::shared_ptr<GraphNode> CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr);
46 
47 
48 /// Add the Defines that have been added between this node and the previous to the graph.
49 /// Return the new "upmost" node, i.e. the last of the Defines added if any, otherwise the node itself
50 std::shared_ptr<GraphNode> AddDefinesToGraph(std::shared_ptr<GraphNode> node,
51  const RBookedDefines &defines,
52  const std::vector<std::string> &prevNodeDefines);
53 
54 // clang-format off
55 /**
56 \class ROOT::Internal::RDF::GraphCreatorHelper
57 \ingroup dataframe
58 \brief Helper class that provides the operation graph nodes.
59 
60  This class is the single point from which graph nodes can be retrieved. Every time an object is created,
61  it clears the static members and starts again.
62  By asking this class to create a node, it will return an existing node if already created, otherwise a new one.
63 */
64 // clang-format on
66 private:
67  using DefinesNodesMap_t = std::map<const ROOT::Detail::RDF::RDefineBase *, std::weak_ptr<GraphNode>>;
68  using FiltersNodesMap_t = std::map<const ROOT::Detail::RDF::RFilterBase *, std::weak_ptr<GraphNode>>;
69  using RangesNodesMap_t = std::map<const ROOT::Detail::RDF::RRangeBase *, std::weak_ptr<GraphNode>>;
70 
71  ////////////////////////////////////////////////////////////////////////////
72  /// \brief Stores the columns defined and which node in the graph defined them.
74  {
75  static DefinesNodesMap_t sMap;
76  return sMap;
77  };
78 
79  ////////////////////////////////////////////////////////////////////////////
80  /// \brief Stores the filters defined and which node in the graph defined them.
82  {
83  static FiltersNodesMap_t sMap;
84  return sMap;
85  }
86 
87  ////////////////////////////////////////////////////////////////////////////
88  /// \brief Stores the ranges defined and which node in the graph defined them.
90  {
91  static RangesNodesMap_t sMap;
92  return sMap;
93  }
94 
95  ////////////////////////////////////////////////////////////////////////////
96  /// \brief Invoked by the RNodes to create a define graph node.
97  friend std::shared_ptr<GraphNode>
98  CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr);
99 
100  ////////////////////////////////////////////////////////////////////////////
101  /// \brief Invoked by the RNodes to create a Filter graph node.
102  friend std::shared_ptr<GraphNode> CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr);
103 
104  ////////////////////////////////////////////////////////////////////////////
105  /// \brief Invoked by the RNodes to create a Range graph node.
106  friend std::shared_ptr<GraphNode> CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr);
107 
108  ////////////////////////////////////////////////////////////////////////////
109  /// \brief Starting from any leaf (Action, Filter, Range) it draws the dot representation of the branch.
110  std::string FromGraphLeafToDot(std::shared_ptr<GraphNode> leaf);
111 
112  ////////////////////////////////////////////////////////////////////////////
113  /// \brief Starting by an array of leaves, it draws the entire graph.
114  std::string FromGraphActionsToDot(std::vector<std::shared_ptr<GraphNode>> leaves);
115 
116  ////////////////////////////////////////////////////////////////////////////
117  /// \brief Starting from the root node, prints the entire graph.
118  std::string RepresentGraph(ROOT::RDataFrame &rDataFrame);
119 
120  ////////////////////////////////////////////////////////////////////////////
121  /// \brief Starting from the root node, prints the entire graph.
122  std::string RepresentGraph(RLoopManager *rLoopManager);
123 
124  ////////////////////////////////////////////////////////////////////////////
125  /// \brief Starting from a Filter or Range, prints the branch it belongs to
126  template <typename Proxied, typename DataSource>
127  std::string RepresentGraph(RInterface<Proxied, DataSource> &rInterface)
128  {
129  auto loopManager = rInterface.GetLoopManager();
130  loopManager->Jit();
131 
132  return FromGraphLeafToDot(rInterface.GetProxiedPtr()->GetGraph());
133  }
134 
135  ////////////////////////////////////////////////////////////////////////////
136  /// \brief Starting from an action, prints the branch it belongs to
137  template <typename T>
138  std::string RepresentGraph(const RResultPtr<T> &resultPtr)
139  {
140  auto loopManager = resultPtr.fLoopManager;
141  if (!loopManager)
142  throw std::runtime_error("Something went wrong");
143 
144  if (std::is_same<T, RInterface<RLoopManager, void>>::value) {
145  return RepresentGraph(loopManager);
146  }
147 
148  loopManager->Jit();
149 
150  auto actionPtr = resultPtr.fActionPtr;
151  return FromGraphLeafToDot(actionPtr->GetGraph());
152  }
153 
154 public:
155  ////////////////////////////////////////////////////////////////////////////
156  /// \brief Functor. Initializes the static members and delegates the work to the right override.
157  /// \tparam NodeType the RNode from which the graph has to be drawn
158  template <typename NodeType>
159  std::string operator()(NodeType &node)
160  {
161  // First all static data structures are cleaned, to avoid undefined behaviours if more than one Represent is
162  // called
167  // The Represent can now start on a clean environment
168  return RepresentGraph(node);
169  }
170 };
171 
172 } // namespace GraphDrawing
173 } // namespace RDF
174 } // namespace Internal
175 } // namespace ROOT
176 
177 #endif
ROOT::Detail::RDF::RFilterBase
Definition: RFilterBase.hxx:36
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::CreateFilterNode
friend std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr)
Invoked by the RNodes to create a Filter graph node.
Definition: RDFGraphUtils.cxx:108
ROOT::Internal::RDF::GraphDrawing::CreateDefineNode
std::shared_ptr< GraphNode > CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr)
Definition: RDFGraphUtils.cxx:90
ROOT::RDF::RResultPtr::fLoopManager
RDFDetail::RLoopManager * fLoopManager
Non-owning pointer to the RLoopManager at the root of this computation graph.
Definition: RResultPtr.hxx:142
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::FromGraphLeafToDot
std::string FromGraphLeafToDot(std::shared_ptr< GraphNode > leaf)
Starting from any leaf (Action, Filter, Range) it draws the dot representation of the branch.
Definition: RDFGraphUtils.cxx:21
ROOT::Internal::RDF::GraphDrawing::GraphNode::ClearCounter
static void ClearCounter()
Resets the counter.
Definition: GraphNode.hxx:71
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::RepresentGraph
std::string RepresentGraph(RInterface< Proxied, DataSource > &rInterface)
Starting from a Filter or Range, prints the branch it belongs to.
Definition: GraphUtils.hxx:127
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::GetStaticFiltersMap
static FiltersNodesMap_t & GetStaticFiltersMap()
Stores the filters defined and which node in the graph defined them.
Definition: GraphUtils.hxx:81
ROOT::Detail::RDF::RRangeBase
Definition: RRangeBase.hxx:32
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::CreateDefineNode
friend std::shared_ptr< GraphNode > CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr)
Invoked by the RNodes to create a define graph node.
Definition: RDFGraphUtils.cxx:90
ROOT::RDF::RResultPtr::fActionPtr
std::shared_ptr< RDFInternal::RActionBase > fActionPtr
Owning pointer to the action that will produce this result.
Definition: RResultPtr.hxx:146
RInterface.hxx
ROOT::RDataFrame
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTree,...
Definition: RDataFrame.hxx:42
ROOT::RDF::RResultPtr
Smart pointer for the return type of actions.
Definition: RResultPtr.hxx:95
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::DefinesNodesMap_t
std::map< const ROOT::Detail::RDF::RDefineBase *, std::weak_ptr< GraphNode > > DefinesNodesMap_t
Definition: GraphUtils.hxx:67
RDataFrame.hxx
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper
Definition: GraphUtils.hxx:65
ROOT::Internal::RDF::GraphDrawing::AddDefinesToGraph
std::shared_ptr< GraphNode > AddDefinesToGraph(std::shared_ptr< GraphNode > node, const RBookedDefines &defines, const std::vector< std::string > &prevNodeDefines)
Add the Defines that have been added between this node and the previous to the graph.
Definition: RDFGraphUtils.cxx:143
ROOT::Detail::RDF::RDefineBase
Definition: RDefineBase.hxx:34
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::operator()
std::string operator()(NodeType &node)
Functor.
Definition: GraphUtils.hxx:159
ROOT::Internal::RDF::GraphDrawing::CreateFilterNode
std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr)
Definition: RDFGraphUtils.cxx:108
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::GetStaticRangesMap
static RangesNodesMap_t & GetStaticRangesMap()
Stores the ranges defined and which node in the graph defined them.
Definition: GraphUtils.hxx:89
ROOT::Internal::RDF::GraphDrawing::CreateRangeNode
std::shared_ptr< GraphNode > CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr)
Definition: RDFGraphUtils.cxx:126
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::GetStaticColumnsMap
static DefinesNodesMap_t & GetStaticColumnsMap()
Stores the columns defined and which node in the graph defined them.
Definition: GraphUtils.hxx:73
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::CreateRangeNode
friend std::shared_ptr< GraphNode > CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr)
Invoked by the RNodes to create a Range graph node.
Definition: RDFGraphUtils.cxx:126
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::FromGraphActionsToDot
std::string FromGraphActionsToDot(std::vector< std::shared_ptr< GraphNode >> leaves)
Starting by an array of leaves, it draws the entire graph.
Definition: RDFGraphUtils.cxx:41
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::RangesNodesMap_t
std::map< const ROOT::Detail::RDF::RRangeBase *, std::weak_ptr< GraphNode > > RangesNodesMap_t
Definition: GraphUtils.hxx:69
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::RepresentGraph
std::string RepresentGraph(const RResultPtr< T > &resultPtr)
Starting from an action, prints the branch it belongs to.
Definition: GraphUtils.hxx:138
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::FiltersNodesMap_t
std::map< const ROOT::Detail::RDF::RFilterBase *, std::weak_ptr< GraphNode > > FiltersNodesMap_t
Definition: GraphUtils.hxx:68
ROOT
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: EExecutionPolicy.hxx:4
GraphNode.hxx
ROOT::Detail::RDF::RLoopManager
The head node of a RDF computation graph.
Definition: RLoopManager.hxx:58
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::RepresentGraph
std::string RepresentGraph(ROOT::RDataFrame &rDataFrame)
Starting from the root node, prints the entire graph.
Definition: RDFGraphUtils.cxx:64