Logo ROOT   6.10/09
Reference Guide
tdf007_snapshot.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
3 /// \notebook
4 /// This tutorial shows how to write out datasets in ROOT formatusing the TDataFrame
5 /// \macro_code
6 ///
7 /// \date April 2017
8 /// \author Danilo Piparo
9 
10 #include "TFile.h"
11 #include "TH1F.h"
12 #include "TTree.h"
13 
14 #include "ROOT/TDataFrame.hxx"
15 
16 // A simple helper function to fill a test tree: this makes the example
17 // stand-alone.
18 void fill_tree(const char *filename, const char *treeName)
19 {
20  TFile f(filename, "RECREATE");
21  TTree t(treeName, treeName);
22  int b1;
23  float b2;
24  t.Branch("b1", &b1);
25  t.Branch("b2", &b2);
26  for (int i = 0; i < 10000; ++i) {
27  b1 = i;
28  b2 = i * i;
29  t.Fill();
30  }
31  t.Write();
32  f.Close();
33  return;
34 }
35 
36 int tdf007_snapshot()
37 {
38  // We prepare an input tree to run on
39  auto fileName = "tdf007_snapshot.root";
40  auto outFileName = "tdf007_snapshot_output.root";
41  auto outFileNameAllColumns = "tdf007_snapshot_output_allColumns.root";
42  auto treeName = "myTree";
43  fill_tree(fileName, treeName);
44 
45  // We read the tree from the file and create a TDataFrame.
46  ROOT::Experimental::TDataFrame d(treeName, fileName);
47 
48  // ## Select entries
49  // We now select some entries in the dataset
50  auto d_cut = d.Filter("b1 % 2 == 0");
51  // ## Enrich the dataset
52  // Build some temporary columns: we'll write them out
53  auto d2 = d_cut.Define("b1_square", "b1 * b1")
54  .Define("b2_vector",
55  [](float b2) {
56  std::vector<float> v;
57  for (int i = 0; i < 3; i++) v.push_back(b2*i);
58  return v;
59  },
60  {"b2"});
61 
62  // ## Write it to disk in ROOT format
63  // We now write to disk a new dataset with one of the variables originally
64  // present in the tree and the new variables.
65  // The user can explicitly specify the types of the columns as template
66  // arguments of the Snapshot method, otherwise they will be automatically
67  // inferred.
68  d2.Snapshot(treeName, outFileName, {"b1", "b1_square", "b2_vector"});
69 
70  // Open the new file and list the columns of the tree
71  TFile f1(outFileName);
72  TTree *t;
73  f1.GetObject(treeName, t);
74  std::cout << "These are the columns b1, b1_square and b2_vector:" << std::endl;
75  for (auto branch : *t->GetListOfBranches()) {
76  std::cout << "Branch: " << branch->GetName() << std::endl;
77  }
78  f1.Close();
79 
80  // We are not forced to write the full set of column names. We can also
81  // specify a regular expression for that. In case nothing is specified, all
82  // columns are persistified.
83  d2.Snapshot(treeName, outFileNameAllColumns);
84 
85  // Open the new file and list the columns of the tree
86  TFile f2(outFileNameAllColumns);
87  f2.GetObject(treeName, t);
88  std::cout << "These are all the columns available to this tdf:" << std::endl;
89  for (auto branch : *t->GetListOfBranches()) {
90  std::cout << "Branch: " << branch->GetName() << std::endl;
91  }
92  f2.Close();
93 
94  // We can also get a fresh TDataFrame out of the snapshot and restart the
95  // analysis chain from it. The default columns are the one selected.
96  // Notice also how we can decide to be more explicit with the types of the
97  // columns.
98  auto snapshot_tdf = d2.Snapshot<int>(treeName, outFileName, {"b1_square"});
99  auto h = snapshot_tdf.Histo1D();
100  auto c = new TCanvas();
101  h->Draw();
102 
103  return 0;
104 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual TObjArray * GetListOfBranches()
Definition: TTree.h:405
SVector< double, 2 > v
Definition: Dict.h:5
The Canvas class.
Definition: TCanvas.h:31
double f(double x)
double f2(const double *x)
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
TF1 * f1
Definition: legend1.C:11
A TTree object has a header with a name and a title.
Definition: TTree.h:78