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 namespace GraphDrawing {
38 std::shared_ptr<GraphNode>
39 CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr);
40 
41 std::shared_ptr<GraphNode> CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr);
42 
43 std::shared_ptr<GraphNode> CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr);
44 
45 // clang-format off
46 /**
47 \class ROOT::Internal::RDF::GraphCreatorHelper
48 \ingroup dataframe
49 \brief Helper class that provides the operation graph nodes.
50 
51  This class is the single point from which graph nodes can be retrieved. Every time an object is created,
52  it clears the static members and starts again.
53  By asking this class to create a node, it will return an existing node if already created, otherwise a new one.
54 */
55 // clang-format on
57 private:
58  using DefinesNodesMap_t = std::map<const ROOT::Detail::RDF::RDefineBase *, std::weak_ptr<GraphNode>>;
59  using FiltersNodesMap_t = std::map<const ROOT::Detail::RDF::RFilterBase *, std::weak_ptr<GraphNode>>;
60  using RangesNodesMap_t = std::map<const ROOT::Detail::RDF::RRangeBase *, std::weak_ptr<GraphNode>>;
61 
62  ////////////////////////////////////////////////////////////////////////////
63  /// \brief Stores the columns defined and which node in the graph defined them.
65  {
66  static DefinesNodesMap_t sMap;
67  return sMap;
68  };
69 
70  ////////////////////////////////////////////////////////////////////////////
71  /// \brief Stores the filters defined and which node in the graph defined them.
73  {
74  static FiltersNodesMap_t sMap;
75  return sMap;
76  }
77 
78  ////////////////////////////////////////////////////////////////////////////
79  /// \brief Stores the ranges defined and which node in the graph defined them.
81  {
82  static RangesNodesMap_t sMap;
83  return sMap;
84  }
85 
86  ////////////////////////////////////////////////////////////////////////////
87  /// \brief Invoked by the RNodes to create a define graph node.
88  friend std::shared_ptr<GraphNode>
89  CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr);
90 
91  ////////////////////////////////////////////////////////////////////////////
92  /// \brief Invoked by the RNodes to create a Filter graph node.
93  friend std::shared_ptr<GraphNode> CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr);
94 
95  ////////////////////////////////////////////////////////////////////////////
96  /// \brief Invoked by the RNodes to create a Range graph node.
97  friend std::shared_ptr<GraphNode> CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr);
98 
99  ////////////////////////////////////////////////////////////////////////////
100  /// \brief Starting from any leaf (Action, Filter, Range) it draws the dot representation of the branch.
101  std::string FromGraphLeafToDot(std::shared_ptr<GraphNode> leaf);
102 
103  ////////////////////////////////////////////////////////////////////////////
104  /// \brief Starting by an array of leaves, it draws the entire graph.
105  std::string FromGraphActionsToDot(std::vector<std::shared_ptr<GraphNode>> leaves);
106 
107  ////////////////////////////////////////////////////////////////////////////
108  /// \brief Starting from the root node, prints the entire graph.
109  std::string RepresentGraph(ROOT::RDataFrame &rDataFrame);
110 
111  ////////////////////////////////////////////////////////////////////////////
112  /// \brief Starting from the root node, prints the entire graph.
113  std::string RepresentGraph(RLoopManager *rLoopManager);
114 
115  ////////////////////////////////////////////////////////////////////////////
116  /// \brief Starting from a Filter or Range, prints the branch it belongs to
117  template <typename Proxied, typename DataSource>
118  std::string RepresentGraph(RInterface<Proxied, DataSource> &rInterface)
119  {
120  auto loopManager = rInterface.GetLoopManager();
121  loopManager->Jit();
122 
123  return FromGraphLeafToDot(rInterface.GetProxiedPtr()->GetGraph());
124  }
125 
126  ////////////////////////////////////////////////////////////////////////////
127  /// \brief Starting from an action, prints the branch it belongs to
128  template <typename T>
129  std::string RepresentGraph(const RResultPtr<T> &resultPtr)
130  {
131  auto loopManager = resultPtr.fLoopManager;
132  if (!loopManager)
133  throw std::runtime_error("Something went wrong");
134 
135  if (std::is_same<T, RInterface<RLoopManager, void>>::value) {
136  return RepresentGraph(loopManager);
137  }
138 
139  loopManager->Jit();
140 
141  auto actionPtr = resultPtr.fActionPtr;
142  return FromGraphLeafToDot(actionPtr->GetGraph());
143  }
144 
145 public:
146  ////////////////////////////////////////////////////////////////////////////
147  /// \brief Functor. Initializes the static members and delegates the work to the right override.
148  /// \tparam NodeType the RNode from which the graph has to be drawn
149  template <typename NodeType>
150  std::string operator()(NodeType &node)
151  {
152  // First all static data structures are cleaned, to avoid undefined behaviours if more than one Represent is
153  // called
158  // The Represent can now start on a clean environment
159  return RepresentGraph(node);
160  }
161 };
162 
163 } // namespace GraphDrawing
164 } // namespace RDF
165 } // namespace Internal
166 } // namespace ROOT
167 
168 #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:92
ROOT::Internal::RDF::GraphDrawing::CreateDefineNode
std::shared_ptr< GraphNode > CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr)
Definition: RDFGraphUtils.cxx:74
ROOT::RDF::RResultPtr::fLoopManager
RDFDetail::RLoopManager * fLoopManager
Non-owning pointer to the RLoopManager at the root of this computation graph.
Definition: RResultPtr.hxx:124
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:8
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:118
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:72
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:74
ROOT::RDF::RResultPtr::fActionPtr
std::shared_ptr< RDFInternal::RActionBase > fActionPtr
Owning pointer to the action that will produce this result.
Definition: RResultPtr.hxx:128
RInterface.hxx
ROOT::RDataFrame
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42
ROOT::RDF::RResultPtr
Smart pointer for the return type of actions.
Definition: RResultPtr.hxx:79
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::DefinesNodesMap_t
std::map< const ROOT::Detail::RDF::RDefineBase *, std::weak_ptr< GraphNode > > DefinesNodesMap_t
Definition: GraphUtils.hxx:58
RDataFrame.hxx
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper
Definition: GraphUtils.hxx:56
ROOT::Detail::RDF::RDefineBase
Definition: RDefineBase.hxx:31
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::operator()
std::string operator()(NodeType &node)
Functor.
Definition: GraphUtils.hxx:150
ROOT::Internal::RDF::GraphDrawing::CreateFilterNode
std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr)
Definition: RDFGraphUtils.cxx:92
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:80
ROOT::Internal::RDF::GraphDrawing::CreateRangeNode
std::shared_ptr< GraphNode > CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr)
Definition: RDFGraphUtils.cxx:110
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:64
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:110
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:28
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::RangesNodesMap_t
std::map< const ROOT::Detail::RDF::RRangeBase *, std::weak_ptr< GraphNode > > RangesNodesMap_t
Definition: GraphUtils.hxx:60
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:129
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::FiltersNodesMap_t
std::map< const ROOT::Detail::RDF::RFilterBase *, std::weak_ptr< GraphNode > > FiltersNodesMap_t
Definition: GraphUtils.hxx:59
ROOT
VSD Structures.
Definition: StringConv.hxx:21
GraphNode.hxx
ROOT::Detail::RDF::RLoopManager
The head node of a RDF computation graph.
Definition: RLoopManager.hxx:57
ROOT::Internal::RDF::GraphDrawing::GraphCreatorHelper::RepresentGraph
std::string RepresentGraph(ROOT::RDataFrame &rDataFrame)
Starting from the root node, prints the entire graph.
Definition: RDFGraphUtils.cxx:51