Logo ROOT  
Reference Guide
df002_dataModel.py
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## \macro_image
9##
10## \date May 2017
11## \author Danilo Piparo
12
13import ROOT
14
15# A simple helper function to fill a test tree: this makes the example stand-alone.
16fill_tree_code = '''
17using FourVector = ROOT::Math::XYZTVector;
18using FourVectorVec = std::vector<FourVector>;
19using CylFourVector = ROOT::Math::RhoEtaPhiVector;
20
21// A simple helper function to fill a test tree: this makes the example
22// stand-alone.
23void fill_tree(const char *filename, const char *treeName)
24{
25 const double M = 0.13957; // set pi+ mass
26 TRandom3 R(1);
27
28 auto genTracks = [&](){
29 FourVectorVec tracks;
30 const auto nPart = R.Poisson(15);
31 tracks.reserve(nPart);
32 for (int j = 0; j < nPart; ++j) {
33 const auto px = R.Gaus(0, 10);
34 const auto py = R.Gaus(0, 10);
35 const auto pt = sqrt(px * px + py * py);
36 const auto eta = R.Uniform(-3, 3);
37 const auto phi = R.Uniform(0.0, 2 * TMath::Pi());
38 CylFourVector vcyl(pt, eta, phi);
39 // set energy
40 auto E = sqrt(vcyl.R() * vcyl.R() + M * M);
41 // fill track vector
42 tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
43 }
44 return tracks;
45 };
46
47 ROOT::RDataFrame d(64);
48 d.Define("tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {"tracks"});
49}
50'''
51
52# We prepare an input tree to run on
53fileName = "df002_dataModel_py.root"
54treeName = "myTree"
55ROOT.gInterpreter.Declare(fill_tree_code)
56ROOT.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.
60RDF = ROOT.ROOT.RDataFrame
61d = RDF(treeName, fileName)
62
63# Operating on branches which are collection of objects
64# Here we deal with the simplest of the cuts: we decide to accept the event
65# only if the number of tracks is greater than 5.
66n_cut = 'tracks.size() > 8'
67nentries = d.Filter(n_cut).Count();
68
69print("%s passed all filters" %nentries.GetValue())
70
71# Another possibility consists in creating a new column containing the
72# quantity we are interested in.
73# In this example, we will cut on the number of tracks and plot their
74# transverse momentum.
75
76getPt_code ='''
77using namespace ROOT::VecOps;
78RVec<double> getPt(const RVec<FourVector> &tracks)
79{
80 auto pt = [](const FourVector &v) { return v.pt(); };
81 return Map(tracks, pt);
82}
83'''
84ROOT.gInterpreter.Declare(getPt_code)
85
86getPtWeights_code ='''
87using namespace ROOT::VecOps;
88RVec<double> getPtWeights(const RVec<FourVector> &tracks)
89{
90 auto ptWeight = [](const FourVector &v) { return 1. / v.Pt(); };
91 return Map(tracks, ptWeight);
92};
93'''
94ROOT.gInterpreter.Declare(getPtWeights_code)
95
96augmented_d = d.Define('tracks_n', '(int)tracks.size()') \
97 .Filter('tracks_n > 2') \
98 .Define('tracks_pts', 'getPt( tracks )') \
99 .Define("tracks_pts_weights", 'getPtWeights( tracks )' )
100
101# The histogram is initialised with a tuple containing the parameters of the
102# histogram
103trN = augmented_d.Histo1D(("", "", 40, -.5, 39.5), "tracks_n")
104trPts = augmented_d.Histo1D("tracks_pts")
105trWPts = augmented_d.Histo1D("tracks_pts", "tracks_pts_weights")
106
107c1 = ROOT.TCanvas()
108trN.Draw()
109
110c2 = ROOT.TCanvas()
111trPts.Draw()
112
113c3 = ROOT.TCanvas()
114trWPts.Draw()
#define d(i)
Definition: RSha256.hxx:102
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
double sqrt(double)
double Pi()
Mathematical constants.
Definition: Math.h:88
auto Map(Args &&... args) -> decltype(ROOT::Detail::VecOps::MapFromTuple(std::forward_as_tuple(args...), std::make_index_sequence< sizeof...(args) - 1 >()))
Create new collection applying a callable to the elements of the input collection.
Definition: RVec.hxx:909
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:938