Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df009_FromScratchVSTTree.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Compare creation of a ROOT dataset with RDataFrame and TTree.

This tutorial illustrates how much 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,
// filtering it, enriching it with new columns, leaving aside some other columns, and
// writing a new dataset become very easy to do.
void RDFWay()
{
auto b = 0.;
df.Define("b1", [&b]() { return b++; })
.Define("b2", "(int) b1 * b1") // This can even be a string
.Snapshot("treeName", "df009_FromScratchVSTTree_df.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 modern, high-level interface for analysis of data stored in TTree ,...
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
A TTree represents a columnar dataset.
Definition TTree.h:79
Date
August 2017
Author
Danilo Piparo (CERN)

Definition in file df009_FromScratchVSTTree.C.