Logo ROOT   6.18/05
Reference Guide
df009_FromScratchVSTTree.C File Reference

Detailed Description

View in nbviewer Open in SWAN This tutorial illustrates how simpler it can be to use a RDataFrame to create a dataset with respect to the usage of the TTree interfaces.

// ##This is the classic way of creating a ROOT dataset
// The steps are:
// - Create a file
// - Create a tree associated to the file
// - Define the variables to write in the entries
// - Define the branches associated to those variables
// - Write the event loop to set the right value to the variables
// - Call TTree::Fill to save the value of the variables
// - Write the TTree
// - Close the file
void classicWay()
{
TFile f("df009_FromScratchVSTTree_classic.root", "RECREATE");
TTree t("treeName", "treeName");
double b1;
int b2;
t.Branch("b1", &b1);
t.Branch("b2", &b2);
for (int i = 0; i < 10; ++i) {
b1 = i;
b2 = i * i;
t.Fill();
}
t.Write();
f.Close();
}
// ##This is the RDF way of creating a ROOT dataset
// Few lines are needed to achieve the same result.
// Parallel creation of the TTree is not supported in the
// classic method.
// In this case the steps are:
// - Create an empty RDataFrame
// - If needed, define variables for the functions used to fill the branches
// - Create new columns expressing their content with lambdas, functors, functions or strings
// - Invoke the Snapshot action
//
// Parallelism is not the only advantage. Starting from an existing dataset and
// filter it, enrich it with new columns, leave aside some other columns and
// write a new dataset becomes very easy to do.
void RDFWay()
{
auto b = 0.;
tdf.Define("b1", [&b]() { return b++; })
.Define("b2", "(int) b1 * b1") // This can even be a string
.Snapshot("treeName", "df009_FromScratchVSTTree_tdf.root");
}
void df009_FromScratchVSTTree()
{
classicWay();
RDFWay();
}
#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:48
A TTree represents a columnar dataset.
Definition: TTree.h:71
Date
August 2017
Author
Danilo Piparo

Definition in file df009_FromScratchVSTTree.C.