Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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>
23
24#include <iostream>
25
26namespace ROOT {
27namespace Detail {
28namespace RDF {
29class RDefineBase;
30class RFilterBase;
31class RRangeBase;
32} // namespace RDF
33} // namespace Detail
34
35namespace Internal {
36namespace RDF {
37class RBookedDefines;
38
39namespace GraphDrawing {
40std::shared_ptr<GraphNode>
41CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr);
42
43std::shared_ptr<GraphNode> CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr);
44
45std::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
50std::shared_ptr<GraphNode> AddDefinesToGraph(std::shared_ptr<GraphNode> node,
51 const RDFInternal::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
66private:
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
154public:
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
The head node of a RDF computation graph.
std::map< const ROOT::Detail::RDF::RFilterBase *, std::weak_ptr< GraphNode > > FiltersNodesMap_t
std::string operator()(NodeType &node)
Functor.
static FiltersNodesMap_t & GetStaticFiltersMap()
Stores the filters defined and which node in the graph defined them.
std::string RepresentGraph(const RResultPtr< T > &resultPtr)
Starting from an action, prints the branch it belongs to.
static DefinesNodesMap_t & GetStaticColumnsMap()
Stores the columns defined and which node in the graph defined them.
friend std::shared_ptr< GraphNode > CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr)
Invoked by the RNodes to create a Range graph node.
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.
std::string FromGraphLeafToDot(std::shared_ptr< GraphNode > leaf)
Starting from any leaf (Action, Filter, Range) it draws the dot representation of the branch.
std::string FromGraphActionsToDot(std::vector< std::shared_ptr< GraphNode > > leaves)
Starting by an array of leaves, it draws the entire graph.
std::map< const ROOT::Detail::RDF::RRangeBase *, std::weak_ptr< GraphNode > > RangesNodesMap_t
std::string RepresentGraph(RInterface< Proxied, DataSource > &rInterface)
Starting from a Filter or Range, prints the branch it belongs to.
std::string RepresentGraph(ROOT::RDataFrame &rDataFrame)
Starting from the root node, prints the entire graph.
friend std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr)
Invoked by the RNodes to create a Filter graph node.
std::map< const ROOT::Detail::RDF::RDefineBase *, std::weak_ptr< GraphNode > > DefinesNodesMap_t
static RangesNodesMap_t & GetStaticRangesMap()
Stores the ranges defined and which node in the graph defined them.
static void ClearCounter()
Resets the counter.
Definition GraphNode.hxx:71
Encapsulates the columns defined by the user.
Smart pointer for the return type of actions.
RDFDetail::RLoopManager * fLoopManager
Non-owning pointer to the RLoopManager at the root of this computation graph.
std::shared_ptr< RDFInternal::RActionBase > fActionPtr
Owning pointer to the action that will produce this result.
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
std::shared_ptr< GraphNode > CreateDefineNode(const std::string &columnName, const ROOT::Detail::RDF::RDefineBase *columnPtr)
std::shared_ptr< GraphNode > CreateRangeNode(const ROOT::Detail::RDF::RRangeBase *rangePtr)
std::shared_ptr< GraphNode > AddDefinesToGraph(std::shared_ptr< GraphNode > node, const RDFInternal::RBookedDefines &defines, const std::vector< std::string > &prevNodeDefines)
Add the Defines that have been added between this node and the previous to the graph.
std::shared_ptr< GraphNode > CreateFilterNode(const ROOT::Detail::RDF::RFilterBase *filterPtr)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...