17using FourVector = ROOT::Math::XYZTVector;
18using FourVectorVec = std::vector<FourVector>;
19using CylFourVector = ROOT::Math::RhoEtaPhiVector;
21// A simple helper function to fill a test tree: this makes the example
23void fill_tree(const char *filename, const char *treeName)
25 const double M = 0.13957; // set pi+ mass
28 auto genTracks = [&](){
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);
40 auto E =
sqrt(vcyl.R() * vcyl.R() + M * M);
42 tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
47 ROOT::RDataFrame
d(64);
48 d.Define(
"tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {
"tracks"});
52# We prepare an input tree to run on
53fileName = "df002_dataModel_py.root"
55ROOT.gInterpreter.Declare(fill_tree_code)
56ROOT.fill_tree(fileName, treeName)
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)
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();
69print("%s passed all filters" %nentries.GetValue())
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
77using namespace ROOT::VecOps;
78RVec<double> getPt(const RVec<FourVector> &tracks)
80 auto pt = [](const FourVector &v) {
return v.pt(); };
81 return Map(tracks, pt);
84ROOT.gInterpreter.Declare(getPt_code)
87using namespace ROOT::VecOps;
88RVec<double> getPtWeights(const RVec<FourVector> &tracks)
90 auto ptWeight = [](const FourVector &v) {
return 1. / v.Pt(); };
91 return Map(tracks, ptWeight);
94ROOT.gInterpreter.Declare(getPtWeights_code)
96augmented_d = d.Define('tracks_n', '(int)tracks.size()') \
98 .Define(
'tracks_pts',
'getPt( tracks )') \
99 .Define(
"tracks_pts_weights",
'getPtWeights( tracks )' )
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")
#define R(a, b, c, d, e, f, g, h, i)
double Pi()
Mathematical constants.
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.
RVec< T > Filter(const RVec< T > &v, F &&f)
Create a new collection with the elements passing the filter expressed by the predicate.