Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df002_dataModel.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook -draw
4/// Show how to work with non-flat data models, e.g. vectors of tracks.
5///
6/// This tutorial shows the possibility to use data models which are more
7/// complex than flat ntuples with RDataFrame.
8///
9/// \macro_code
10/// \macro_image
11///
12/// \date December 2016
13/// \author Danilo Piparo (CERN)
14
15using FourVector = ROOT::Math::XYZTVector;
16using FourVectorVec = std::vector<FourVector>;
17using FourVectorRVec = ROOT::VecOps::RVec<FourVector>;
18using CylFourVector = ROOT::Math::RhoEtaPhiVector;
19
20// A simple helper function to fill a test tree: this makes the example
21// stand-alone.
22void fill_tree(const char *filename, const char *treeName)
23{
24 const double M = 0.13957; // set pi+ mass
25 TRandom3 R(1);
26
27 auto genTracks = [&](){
28 FourVectorVec tracks;
29 const auto nPart = R.Poisson(15);
30 tracks.reserve(nPart);
31 for (int j = 0; j < nPart; ++j) {
32 const auto px = R.Gaus(0, 10);
33 const auto py = R.Gaus(0, 10);
34 const auto pt = sqrt(px * px + py * py);
35 const auto eta = R.Uniform(-3, 3);
36 const auto phi = R.Uniform(0.0, 2 * TMath::Pi());
37 CylFourVector vcyl(pt, eta, phi);
38 // set energy
39 auto E = sqrt(vcyl.R() * vcyl.R() + M * M);
40 // fill track vector
41 tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
42 }
43 return tracks;
44 };
45
47 d.Define("tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {"tracks"});
48}
49
51{
52
53 // We prepare an input tree to run on
54 auto fileName = "df002_dataModel.root";
55 auto treeName = "myTree";
56 fill_tree(fileName, treeName);
57
58 // We read the tree from the file and create a RDataFrame, a class that
59 // allows us to interact with the data contained in the tree.
60 ROOT::RDataFrame d(treeName, fileName, {"tracks"});
61
62 // ## Operating on branches which are collections of objects
63 // Here we deal with the simplest of the cuts: we decide to accept the event
64 // only if the number of tracks is greater than 8.
65 auto n_cut = [](const FourVectorRVec &tracks) { return tracks.size() > 8; };
66 auto nentries = d.Filter(n_cut, {"tracks"}).Count();
67
68 std::cout << *nentries << " passed all filters" << std::endl;
69
70 // Another possibility consists in creating a new column containing the
71 // quantity we are interested in.
72 // In this example, we will cut on the number of tracks and plot their
73 // transverse momentum.
74 auto getPt = [](const FourVectorRVec &tracks) {
75 return ROOT::VecOps::Map(tracks, [](const FourVector& v){return v.Pt();});
76 };
77
78 // We do the same for the weights.
79 auto getPtWeights = [](const FourVectorRVec &tracks) {
80 return ROOT::VecOps::Map(tracks, [](const FourVector& v){ return 1. / v.Pt();});
81 };
82
83 auto augmented_d = d.Define("tracks_n", [](const FourVectorRVec &tracks) { return (int)tracks.size(); })
84 .Filter([](int tracks_n) { return tracks_n > 2; }, {"tracks_n"})
85 .Define("tracks_pts", getPt)
86 .Define("tracks_pts_weights", getPtWeights);
87
88 auto trN = augmented_d.Histo1D({"", "", 40, -.5, 39.5}, "tracks_n");
89 auto trPts = augmented_d.Histo1D("tracks_pts");
90 auto trWPts = augmented_d.Histo1D("tracks_pts", "tracks_pts_weights");
91
92 auto c1 = new TCanvas();
93 trN->DrawCopy();
94
95 auto c2 = new TCanvas();
96 trPts->DrawCopy();
97
98 auto c3 = new TCanvas();
99 trWPts->DrawCopy();
100
101 return 0;
102}
#define d(i)
Definition RSha256.hxx:102
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
int nentries
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1492
The Canvas class.
Definition TCanvas.h:23
Random number generator class based on M.
Definition TRandom3.h:27
TPaveText * pt
auto Map(Args &&... args)
Create new collection applying a callable to the elements of the input collection.
Definition RVec.hxx:2113
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:2145
return c1
Definition legend1.C:41
return c2
Definition legend2.C:14
return c3
Definition legend3.C:15
DisplacementVector3D< CylindricalEta3D< double >, DefaultCoordinateSystemTag > RhoEtaPhiVector
3D Vector based on the eta based cylindrical coordinates rho, eta, phi in double precision.
Definition Vector3Dfwd.h:62
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
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:46
constexpr Double_t E()
Base of natural log: .
Definition TMath.h:93
constexpr Double_t Pi()
Definition TMath.h:37
void tracks()
Definition tracks.C:49