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>
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 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>
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
142 loopManager->Jit();
143
144 auto actionPtr = resultPtr.fActionPtr;
145 return FromGraphLeafToDot(actionPtr->GetGraph());
146 }
147
148public:
149 ////////////////////////////////////////////////////////////////////////////
150 /// \brief Functor. Initializes the static members and delegates the work to the right override.
151 /// \tparam NodeType the RNode from which the graph has to be drawn
152 template <typename NodeType>
153 std::string operator()(NodeType &node)
154 {
155 // First all static data structures are cleaned, to avoid undefined behaviours if more than one Represent is
156 // called
161 // The Represent can now start on a clean environment
162 return RepresentGraph(node);
163 }
164};
165
166} // namespace GraphDrawing
167} // namespace RDF
168} // namespace Internal
169} // namespace ROOT
170
171#endif
The head node of a RDF computation graph.
void Jit()
Add RDF nodes that require just-in-time compilation to the computation graph.
std::map< const ROOT::Detail::RDF::RFilterBase *, std::weak_ptr< GraphNode > > FiltersNodesMap_t
Definition: GraphUtils.hxx:68
std::string operator()(NodeType &node)
Functor.
Definition: GraphUtils.hxx:153
static FiltersNodesMap_t & GetStaticFiltersMap()
Stores the filters defined and which node in the graph defined them.
Definition: GraphUtils.hxx:81
std::string RepresentGraph(const RResultPtr< T > &resultPtr)
Starting from an action, prints the branch it belongs to.
Definition: GraphUtils.hxx:138
static DefinesNodesMap_t & GetStaticColumnsMap()
Stores the columns defined and which node in the graph defined them.
Definition: GraphUtils.hxx:73
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
Definition: GraphUtils.hxx:69
std::string RepresentGraph(RInterface< Proxied, DataSource > &rInterface)
Starting from a Filter or Range, prints the branch it belongs to.
Definition: GraphUtils.hxx:127
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
Definition: GraphUtils.hxx:67
static RangesNodesMap_t & GetStaticRangesMap()
Stores the ranges defined and which node in the graph defined them.
Definition: GraphUtils.hxx:89
static void ClearCounter()
Resets the counter.
Definition: GraphNode.hxx:71
The public interface to the RDataFrame federation of classes.
Definition: RInterface.hxx:97
RLoopManager * GetLoopManager() const
const std::shared_ptr< Proxied > & GetProxiedPtr() const
Smart pointer for the return type of actions.
Definition: RResultPtr.hxx:95
RDFDetail::RLoopManager * fLoopManager
Non-owning pointer to the RLoopManager at the root of this computation graph.
Definition: RResultPtr.hxx:142
std::shared_ptr< RDFInternal::RActionBase > fActionPtr
Owning pointer to the action that will produce this result.
Definition: RResultPtr.hxx:146
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTree,...
Definition: RDataFrame.hxx:40
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 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...