Logo ROOT   6.12/07
Reference Guide
tdf009_FromScratchVSTTree.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
3 /// \notebook -nodraw
4 /// This tutorial illustrates how simpler it can be to use a
5 /// TDataFrame 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
23 void classicWay()
24 {
25  TFile f("tdf009_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 TDF 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 TDataFrame
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.
53 void TDFWay()
54 {
56  auto b = 0.;
57  tdf.Define("b1", [&b]() { return b++; })
58  .Define("b2", "(int) b1 * b1") // This can even be a string
59  .Snapshot("treeName", "tdf009_FromScratchVSTTree_tdf.root");
60 }
61 
62 void tdf009_FromScratchVSTTree()
63 {
64 
65  classicWay();
66  TDFWay();
67 }
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:39
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
A TTree object has a header with a name and a title.
Definition: TTree.h:70