Logo ROOT  
Reference Guide
df009_FromScratchVSTTree.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook -nodraw
4/// This tutorial illustrates how simpler it can be to use a
5/// RDataFrame to create a dataset with respect to the usage
6/// of the TTree interfaces.
7///
8/// \macro_code
9///
10/// \date August 2017
11/// \author Danilo Piparo
12
13// ##This is the classic way of creating a ROOT dataset
14// The steps are:
15// - Create a file
16// - Create a tree associated to the file
17// - Define the variables to write in the entries
18// - Define the branches associated to those variables
19// - Write the event loop to set the right value to the variables
20// - Call TTree::Fill to save the value of the variables
21// - Write the TTree
22// - Close the file
23void classicWay()
24{
25 TFile f("df009_FromScratchVSTTree_classic.root", "RECREATE");
26 TTree t("treeName", "treeName");
27 double b1;
28 int b2;
29 t.Branch("b1", &b1);
30 t.Branch("b2", &b2);
31 for (int i = 0; i < 10; ++i) {
32 b1 = i;
33 b2 = i * i;
34 t.Fill();
35 }
36 t.Write();
37 f.Close();
38}
39
40// ##This is the RDF way of creating a ROOT dataset
41// Few lines are needed to achieve the same result.
42// Parallel creation of the TTree is not supported in the
43// classic method.
44// In this case the steps are:
45// - Create an empty RDataFrame
46// - If needed, define variables for the functions used to fill the branches
47// - Create new columns expressing their content with lambdas, functors, functions or strings
48// - Invoke the Snapshot action
49//
50// Parallelism is not the only advantage. Starting from an existing dataset and
51// filter it, enrich it with new columns, leave aside some other columns and
52// write a new dataset becomes very easy to do.
53void RDFWay()
54{
55 ROOT::RDataFrame df(10);
56 auto b = 0.;
57 df.Define("b1", [&b]() { return b++; })
58 .Define("b2", "(int) b1 * b1") // This can even be a string
59 .Snapshot("treeName", "df009_FromScratchVSTTree_df.root");
60}
61
62void df009_FromScratchVSTTree()
63{
64
65 classicWay();
66 RDFWay();
67}
#define b(i)
Definition: RSha256.hxx:100
#define f(i)
Definition: RSha256.hxx:104
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
A TTree represents a columnar dataset.
Definition: TTree.h:78