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