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