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.
Author: Danilo Piparo (CERN)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, March 23, 2023 at 10:44 AM.
The steps are:
%%cpp -d
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();
}
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:
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.
%%cpp -d
void RDFWay()
{
ROOT::RDataFrame df(10);
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");
}
input_line_44:6:36: error: expected ';' after expression .Define("b2", "(int) b1 * b1") This can even be a string ^ ; input_line_44:6:38: error: unknown type name 'This' .Define("b2", "(int) b1 * b1") This can even be a string ^ input_line_44:6:46: error: expected ';' at end of declaration .Define("b2", "(int) b1 * b1") This can even be a string ^ ;
classicWay();
RDFWay();
input_line_46:2:3: error: use of undeclared identifier 'RDFWay' (RDFWay()) ^ Error in <HandleInterpreterException>: Error evaluating expression (RDFWay()) Execution of your code was aborted.