Logo ROOT   6.14/05
Reference Guide
df002_dataModel.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_dataframe
3 /// \notebook -draw
4 /// This tutorial shows the possibility to use data models which are more
5 /// complex than flat ntuples with RDataFrame
6 ///
7 /// \macro_code
8 ///
9 /// \date December 2016
10 /// \author Danilo Piparo
11 using FourVector = ROOT::Math::XYZTVector;
12 using FourVectorVec = std::vector<FourVector>;
13 using FourVectorRVec = ROOT::VecOps::RVec<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  const double M = 0.13957; // set pi+ mass
21  TRandom3 R(1);
22 
23  auto genTracks = [&](){
24  FourVectorVec tracks;
25  const auto nPart = R.Poisson(15);
26  tracks.reserve(nPart);
27  for (int j = 0; j < nPart; ++j) {
28  const auto px = R.Gaus(0, 10);
29  const auto py = R.Gaus(0, 10);
30  const auto pt = sqrt(px * px + py * py);
31  const auto eta = R.Uniform(-3, 3);
32  const auto phi = R.Uniform(0.0, 2 * TMath::Pi());
33  CylFourVector vcyl(pt, eta, phi);
34  // set energy
35  auto E = sqrt(vcyl.R() * vcyl.R() + M * M);
36  // fill track vector
37  tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
38  }
39  return tracks;
40  };
41 
42  ROOT::RDataFrame d(64);
43  d.Define("tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {"tracks"});
44 }
45 
46 int df002_dataModel()
47 {
48 
49  // We prepare an input tree to run on
50  auto fileName = "df002_dataModel.root";
51  auto treeName = "myTree";
52  fill_tree(fileName, treeName);
53 
54  // We read the tree from the file and create a RDataFrame, a class that
55  // allows us to interact with the data contained in the tree.
56  ROOT::RDataFrame d(treeName, fileName, {"tracks"});
57 
58  // ## Operating on branches which are collection of objects
59  // Here we deal with the simplest of the cuts: we decide to accept the event
60  // only if the number of tracks is greater than 5.
61  auto n_cut = [](const FourVectorRVec &tracks) { return tracks.size() > 8; };
62  auto nentries = d.Filter(n_cut, {"tracks"}).Count();
63 
64  std::cout << *nentries << " passed all filters" << std::endl;
65 
66  // Another possibility consists in creating a new column containing the
67  // quantity we are interested in.
68  // In this example, we will cut on the number of tracks and plot their
69  // transverse momentum.
70  auto getPt = [](const FourVectorRVec &tracks) {
71  return ROOT::VecOps::Map(tracks, [](const FourVector& v){return v.Pt();});
72  };
73 
74  // We do the same for the weights.
75  auto getPtWeights = [](const FourVectorRVec &tracks) {
76  return ROOT::VecOps::Map(tracks, [](const FourVector& v){ return 1. / v.Pt();});
77  };
78 
79  auto augmented_d = d.Define("tracks_n", [](const FourVectorRVec &tracks) { return (int)tracks.size(); })
80  .Filter([](int tracks_n) { return tracks_n > 2; }, {"tracks_n"})
81  .Define("tracks_pts", getPt)
82  .Define("tracks_pts_weights", getPtWeights);
83 
84  auto trN = augmented_d.Histo1D({"", "", 40, -.5, 39.5}, "tracks_n");
85  auto trPts = augmented_d.Histo1D("tracks_pts");
86  auto trWPts = augmented_d.Histo1D("tracks_pts", "tracks_pts_weights");
87 
88  TCanvas c1;
89  trN->Draw();
90  c1.Print("tracks_n.png");
91 
92  TCanvas c2;
93  trPts->Draw();
94  c2.Print("tracks_pt.png");
95 
96  TCanvas c3;
97  trWPts->Draw();
98  c3.Print("tracks_Wpt.png");
99 
100  return 0;
101 }
Random number generator class based on M.
Definition: TRandom3.h:27
auto Map(const RVec< T > &v, F &&f) -> RVec< decltype(f(v[0]))>
Create new collection applying a callable to the elements of the input collection.
Definition: RVec.hxx:627
return c1
Definition: legend1.C:41
DisplacementVector3D< CylindricalEta3D< double >, DefaultCoordinateSystemTag > RhoEtaPhiVector
3D Vector based on the eta based cylindrical coordinates rho, eta, phi in double precision.
Definition: Vector3Dfwd.h:50
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
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:38
RVec< T > Filter(const RVec< T > &v, F &&f)
Create a new collection with the elements passing the filter expressed by the predicate.
Definition: RVec.hxx:636
A "std::vector"-like collection of values implementing handy operation to analyse them...
Definition: RVec.hxx:146
TPaveText * pt
SVector< double, 2 > v
Definition: Dict.h:5
virtual void Print(const char *filename="") const
Save Pad contents in a file in one of various formats.
Definition: TPad.cxx:4617
constexpr Double_t E()
Base of natural log: .
Definition: TMath.h:97
ROOT&#39;s RDataFrame offers a high level interface for analyses of data stored in TTrees, CSV&#39;s and other data formats.
Definition: RDataFrame.hxx:42
The Canvas class.
Definition: TCanvas.h:31
#define d(i)
Definition: RSha256.hxx:102
return c2
Definition: legend2.C:14
int nentries
Definition: THbookFile.cxx:89
return c3
Definition: legend3.C:15