Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df034_SaveGraph.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook -nodraw
4/// Basic SaveGraph usage.
5///
6/// This tutorial shows how to use the SaveGraph action.
7/// SaveGraph inspects the sequence of RDataFrame actions.
8///
9/// \macro_code
10/// \macro_output
11///
12/// \date January 2022
13/// \author Ivan Kabadzhov (CERN)
14
15// First, an RDataFrame computation graph is created with Defines, Filters and methods such as Mean, Count, etc.
16// After that, SaveGraph can be called either on the root RDataFrame object or on a specific node of the computation
17// graph: in the first case, the graph returned will span the full computation graph, in the second case it will show
18// only the branch of the computation graph that the node belongs to.
19// If a filename is passed as second argument, the graph is saved to that file, otherwise it is returned as a string.
20
21R__LOAD_LIBRARY(ROOTDataFrame) // only required for ROOT installations without runtime_cxxmodules
22
23void df034_SaveGraph()
24{
25 ROOT::RDataFrame rd1(1);
26 auto rd2 = rd1.Define("Root_def1", "1").Filter("Root_def1 < 2", "Main_Filter").Define("Root_def2", "1");
27
28 auto branch1 = rd2.Define("Branch_1_def", "1");
29 auto branch2 = rd2.Define("Branch_2_def", "1");
30
31 ROOT::RDF::RResultPtr<double> branch1_1 = branch1.Filter("Branch_1_def < 2", "Filter_1")
32 .Define("Branch_1_1_def", "1")
33 .Filter("1 == Branch_1_1_def % 2", "Filter_1_1")
34 .Mean("Branch_1_1_def");
35
37 branch1.Define("Branch_1_2_def", "1").Filter("Branch_1_2_def < 2", "Filter_1_2").Count();
38
39 ROOT::RDF::RResultPtr<double> branch2_1 = branch2.Filter("Branch_2_def < 2", "Filter_2")
40 .Define("Branch_2_1_def", "1")
41 .Define("Branch_2_2_def", "1")
42 .Filter("1 == Branch_2_1_def % 2", "Filter_2_1")
43 .Max("Branch_2_1_def");
44
45 ROOT::RDF::RResultPtr<unsigned long long> branch2_2 = branch2.Count();
46
47 std::cout << ROOT::RDF::SaveGraph(branch1_1);
48 ROOT::RDF::SaveGraph(rd1, /*output_file=*/"rdf_savegraph_tutorial.dot");
49 // SaveGraph produces content in the standard DOT file format
50 // (https://en.wikipedia.org/wiki/DOT_%28graph_description_language%29): it can be converted to e.g. an image file
51 // using standard tools such as the `dot` CLI program.
52 gSystem->Exec("dot -Tpng rdf_savegraph_tutorial.dot -o rdf_savegraph_tutorial.png");
53}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:491
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
Smart pointer for the return type of actions.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:640
std::string SaveGraph(NodeType node)
Create a graphviz representation of the dataframe computation graph, return it as a string.