Logo ROOT   6.12/07
Reference Guide
tdf002_dataModel.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
3 /// \notebook -draw
4 /// This tutorial shows the possibility to use data models which are more
5 /// complex than flat ntuples with TDataFrame
6 ///
7 /// \macro_code
8 ///
9 /// \date December 2016
10 /// \author Danilo Piparo
11 
12 using FourVector = ROOT::Math::XYZTVector;
13 using FourVectors = std::vector<FourVector>;
14 using CylFourVector = ROOT::Math::RhoEtaPhiVector;
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  FourVectors tracks;
23  t.Branch("tracks", &tracks);
24 
25  const double M = 0.13957; // set pi+ mass
26  TRandom3 R(1);
27 
28  for (int i = 0; i < 50; ++i) {
29  auto nPart = R.Poisson(15);
30  tracks.clear();
31  tracks.reserve(nPart);
32  for (int j = 0; j < nPart; ++j) {
33  double px = R.Gaus(0, 10);
34  double py = R.Gaus(0, 10);
35  double pt = sqrt(px * px + py * py);
36  double eta = R.Uniform(-3, 3);
37  double phi = R.Uniform(0.0, 2 * TMath::Pi());
38  CylFourVector vcyl(pt, eta, phi);
39  // set energy
40  double E = sqrt(vcyl.R() * vcyl.R() + M * M);
41  FourVector q(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
42  // fill track vector
43  tracks.emplace_back(q);
44  }
45  t.Fill();
46  }
47 
48  t.Write();
49  f.Close();
50  return;
51 }
52 
53 int tdf002_dataModel()
54 {
55 
56  // We prepare an input tree to run on
57  auto fileName = "tdf002_dataModel.root";
58  auto treeName = "myTree";
59  fill_tree(fileName, treeName);
60 
61  // We read the tree from the file and create a TDataFrame, a class that
62  // allows us to interact with the data contained in the tree.
63  ROOT::Experimental::TDataFrame d(treeName, fileName, {"tracks"});
64 
65  // ## Operating on branches which are collection of objects
66  // Here we deal with the simplest of the cuts: we decide to accept the event
67  // only if the number of tracks is greater than 5.
68  auto n_cut = [](const FourVectors &tracks) { return tracks.size() > 8; };
69  auto nentries = d.Filter(n_cut, {"tracks"}).Count();
70 
71  std::cout << *nentries << " passed all filters" << std::endl;
72 
73  // Another possibility consists in creating a new column containing the
74  // quantity we are interested in.
75  // In this example, we will cut on the number of tracks and plot their
76  // transverse momentum.
77  auto getPt = [](const FourVectors &tracks) {
78  std::vector<double> pts;
79  pts.reserve(tracks.size());
80  for (auto &t : tracks)
81  pts.emplace_back(t.Pt());
82  return pts;
83  };
84 
85  // We do the same for the weights.
86  auto getPtWeights = [](const FourVectors &tracks) {
87  std::vector<double> ptsw;
88  ptsw.reserve(tracks.size());
89  for (auto &t : tracks)
90  ptsw.emplace_back(1. / t.Pt());
91  return ptsw;
92  };
93 
94  auto augmented_d = d.Define("tracks_n", [](const FourVectors &tracks) { return (int)tracks.size(); })
95  .Filter([](int tracks_n) { return tracks_n > 2; }, {"tracks_n"})
96  .Define("tracks_pts", getPt)
97  .Define("tracks_pts_weights", getPtWeights);
98 
99  auto trN = augmented_d.Histo1D({"", "", 40, -.5, 39.5}, "tracks_n");
100  auto trPts = augmented_d.Histo1D("tracks_pts");
101  auto trWPts = augmented_d.Histo1D("tracks_pts", "tracks_pts_weights");
102 
103  TCanvas c1;
104  trN->Draw();
105  c1.Print("tracks_n.png");
106 
107  TCanvas c2;
108  trPts->Draw();
109  c2.Print("tracks_pt.png");
110 
111  TCanvas c3;
112  trWPts->Draw();
113  c3.Print("tracks_Wpt.png");
114 
115  return 0;
116 }
Random number generator class based on M.
Definition: TRandom3.h:27
return c1
Definition: legend1.C:41
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
DisplacementVector3D< CylindricalEta3D< double >, DefaultCoordinateSystemTag > RhoEtaPhiVector
3D Vector based on the eta based cylindrical coordinates rho, eta, phi in double precision.
Definition: Vector3Dfwd.h:50
double sqrt(double)
LorentzVector< PxPyPzE4D< double > > XYZTVector
LorentzVector based on x,y,x,t (or px,py,pz,E) coordinates in double precision with metric (-...
Definition: Vector4Dfwd.h:33
constexpr Double_t Pi()
Definition: TMath.h:40
TPaveText * pt
virtual void Print(const char *filename="") const
Save Pad contents in a file in one of various formats.
Definition: TPad.cxx:4599
constexpr Double_t E()
Definition: TMath.h:74
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
int nentries
Definition: THbookFile.cxx:89
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:39
A TTree object has a header with a name and a title.
Definition: TTree.h:70
float * q
Definition: THbookFile.cxx:87
constexpr Double_t R()
Definition: TMath.h:213
return c3
Definition: legend3.C:15