ROOT
v6-34
Reference Guide
Loading...
Searching...
No Matches
df002_dataModel.py
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 May 2017
13
## \author Danilo Piparo (CERN)
14
15
import
ROOT
16
17
# A simple helper function to fill a test tree: this makes the example stand-alone.
18
fill_tree_code =
'''
19
using FourVector = ROOT::Math::XYZTVector;
20
using FourVectorVec = std::vector<FourVector>;
21
using CylFourVector = ROOT::Math::RhoEtaPhiVector;
22
23
// A simple helper function to fill a test tree: this makes the example
24
// stand-alone.
25
void fill_tree(const char *filename, const char *treeName)
26
{
27
const double M = 0.13957; // set pi+ mass
28
TRandom3 R(1);
29
30
auto genTracks = [&](){
31
FourVectorVec tracks;
32
const auto nPart = R.Poisson(15);
33
tracks.reserve(nPart);
34
for (int j = 0; j < nPart; ++j) {
35
const auto px = R.Gaus(0, 10);
36
const auto py = R.Gaus(0, 10);
37
const auto pt = sqrt(px * px + py * py);
38
const auto eta = R.Uniform(-3, 3);
39
const auto phi = R.Uniform(0.0, 2 * TMath::Pi());
40
CylFourVector vcyl(pt, eta, phi);
41
// set energy
42
auto E = sqrt(vcyl.R() * vcyl.R() + M * M);
43
// fill track vector
44
tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
45
}
46
return tracks;
47
};
48
49
ROOT::RDataFrame d(64);
50
d.Define("tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {"tracks"});
51
}
52
'''
53
54
# We prepare an input tree to run on
55
fileName =
"df002_dataModel_py.root"
56
treeName =
"myTree"
57
ROOT.gInterpreter.Declare
(fill_tree_code)
58
ROOT.fill_tree
(fileName, treeName)
59
60
# We read the tree from the file and create a RDataFrame, a class that
61
# allows us to interact with the data contained in the tree.
62
d =
ROOT.RDataFrame
(treeName, fileName)
63
64
# Operating on branches which are collections of objects
65
# Here we deal with the simplest of the cuts: we decide to accept the event
66
# only if the number of tracks is greater than 8.
67
n_cut =
'tracks.size() > 8'
68
nentries =
d.Filter
(n_cut).Count();
69
70
print(
"%s events passed all filters"
%
nentries.GetValue
())
71
72
# Another possibility consists in creating a new column containing the
73
# quantity we are interested in.
74
# In this example, we will cut on the number of tracks and plot their
75
# transverse momentum.
76
77
getPt_code =
'''
78
using namespace ROOT::VecOps;
79
ROOT::RVecD getPt(const RVec<FourVector> &tracks)
80
{
81
auto pt = [](const FourVector &v) { return v.pt(); };
82
return Map(tracks, pt);
83
}
84
'''
85
ROOT.gInterpreter.Declare
(getPt_code)
86
87
getPtWeights_code =
'''
88
using namespace ROOT::VecOps;
89
ROOT::RVecD getPtWeights(const RVec<FourVector> &tracks)
90
{
91
auto ptWeight = [](const FourVector &v) { return 1. / v.Pt(); };
92
return Map(tracks, ptWeight);
93
};
94
'''
95
ROOT.gInterpreter.Declare
(getPtWeights_code)
96
97
augmented_d =
d.Define
(
'tracks_n'
,
'(int)tracks.size()'
) \
98
.Filter(
'tracks_n > 2'
) \
99
.Define(
'tracks_pts'
,
'getPt( tracks )'
) \
100
.Define(
"tracks_pts_weights"
,
'getPtWeights( tracks )'
)
101
102
# The histogram is initialised with a tuple containing the parameters of the
103
# histogram
104
trN =
augmented_d.Histo1D
((
""
,
""
, 40, -.5, 39.5),
"tracks_n"
)
105
trPts =
augmented_d.Histo1D
(
"tracks_pts"
)
106
trWPts =
augmented_d.Histo1D
(
"tracks_pts"
,
"tracks_pts_weights"
)
107
108
c1 =
ROOT.TCanvas
()
109
trN.Draw
()
110
c1.SaveAs
(
"df002_trN.png"
)
111
112
c2 =
ROOT.TCanvas
()
113
trPts.Draw
()
114
c2.SaveAs
(
"df002_trPts.png"
)
115
116
c3 =
ROOT.TCanvas
()
117
trWPts.Draw
()
118
c2.SaveAs
(
"df002_trWPts.png"
)
119
120
print(
"Saved figures to df002_*.png"
)
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
ROOT::RDataFrame
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
Definition
RDataFrame.hxx:41
tutorials
dataframe
df002_dataModel.py
ROOT v6-34 - Reference Guide Generated on Fri Jan 24 2025 14:44:17 (GVA Time) using Doxygen 1.10.0