16 using FourVector = ROOT::Math::XYZTVector; 17 using FourVectorVec = std::vector<FourVector>; 18 using CylFourVector = ROOT::Math::RhoEtaPhiVector; 20 // A simple helper function to fill a test tree: this makes the example 22 void fill_tree(const char *filename, const char *treeName) 24 const double M = 0.13957; // set pi+ mass 27 auto genTracks = [&](){ 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); 39 auto E = sqrt(vcyl.R() * vcyl.R() + M * M); 41 tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E); 46 ROOT::RDataFrame d(64); 47 d.Define("tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {"tracks"}); 52 fileName =
"df002_dataModel_py.root" 54 ROOT.gInterpreter.Declare(fill_tree_code)
55 ROOT.fill_tree(fileName, treeName)
59 RDF = ROOT.ROOT.RDataFrame
60 d = RDF(treeName, fileName)
65 n_cut =
'tracks.size() > 8' 66 nentries = d.Filter(n_cut).Count();
68 print(
"%s passed all filters" %nentries.GetValue())
76 using namespace ROOT::VecOps; 77 RVec<double> getPt(const RVec<FourVector> &tracks) 79 auto pt = [](const FourVector &v) { return v.pt(); }; 80 return Map(tracks, pt); 83 ROOT.gInterpreter.Declare(getPt_code)
85 getPtWeights_code =
''' 86 using namespace ROOT::VecOps; 87 RVec<double> getPtWeights(const RVec<FourVector> &tracks) 89 auto ptWeight = [](const FourVector &v) { return 1. / v.Pt(); }; 90 return Map(tracks, ptWeight); 93 ROOT.gInterpreter.Declare(getPtWeights_code)
95 augmented_d = d.Define(
'tracks_n',
'(int)tracks.size()') \
97 .Define(
'tracks_pts',
'getPt( tracks )') \
98 .Define(
"tracks_pts_weights",
'getPtWeights( tracks )' )
102 trN = augmented_d.Histo1D((
"",
"", 40, -.5, 39.5),
"tracks_n")
103 trPts = augmented_d.Histo1D(
"tracks_pts")
104 trWPts = augmented_d.Histo1D(
"tracks_pts",
"tracks_pts_weights")
108 c1.Print(
"tracks_n.png")
112 c2.Print(
"tracks_pt.png")
116 c3.Print(
"tracks_Wpt.png")
RVec< T > Filter(const RVec< T > &v, F &&f)
Create a new collection with the elements passing the filter expressed by the predicate.