Logo ROOT  
Reference Guide
df002_dataModel.C File Reference

Detailed Description

Show how to work with non-flat data models, e.g. vectors of tracks.

View in nbviewer Open in SWAN This tutorial shows the possibility to use data models which are more complex than flat ntuples with RDataFrame

using FourVector = ROOT::Math::XYZTVector;
using FourVectorVec = std::vector<FourVector>;
using FourVectorRVec = ROOT::VecOps::RVec<FourVector>;
using CylFourVector = ROOT::Math::RhoEtaPhiVector;
// A simple helper function to fill a test tree: this makes the example
// stand-alone.
void fill_tree(const char *filename, const char *treeName)
const double M = 0.13957; // set pi+ mass
TRandom3 R(1);
auto genTracks = [&](){
FourVectorVec tracks;
const auto nPart = R.Poisson(15);
for (int j = 0; j < nPart; ++j) {
const auto px = R.Gaus(0, 10);
const auto py = R.Gaus(0, 10);
const auto pt = sqrt(px * px + py * py);
const auto eta = R.Uniform(-3, 3);
const auto phi = R.Uniform(0.0, 2 * TMath::Pi());
CylFourVector vcyl(pt, eta, phi);
// set energy
auto E = sqrt(vcyl.R() * vcyl.R() + M * M);
// fill track vector
tracks.emplace_back(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
return tracks;
d.Define("tracks", genTracks).Snapshot<FourVectorVec>(treeName, filename, {"tracks"});
// We prepare an input tree to run on
auto fileName = "df002_dataModel.root";
auto treeName = "myTree";
fill_tree(fileName, treeName);
// We read the tree from the file and create a RDataFrame, a class that
// allows us to interact with the data contained in the tree.
ROOT::RDataFrame d(treeName, fileName, {"tracks"});
// ## Operating on branches which are collection of objects
// Here we deal with the simplest of the cuts: we decide to accept the event
// only if the number of tracks is greater than 5.
auto n_cut = [](const FourVectorRVec &tracks) { return tracks.size() > 8; };
auto nentries = d.Filter(n_cut, {"tracks"}).Count();
std::cout << *nentries << " passed all filters" << std::endl;
// Another possibility consists in creating a new column containing the
// quantity we are interested in.
// In this example, we will cut on the number of tracks and plot their
// transverse momentum.
auto getPt = [](const FourVectorRVec &tracks) {
return ROOT::VecOps::Map(tracks, [](const FourVector& v){return v.Pt();});
// We do the same for the weights.
auto getPtWeights = [](const FourVectorRVec &tracks) {
return ROOT::VecOps::Map(tracks, [](const FourVector& v){ return 1. / v.Pt();});
auto augmented_d = d.Define("tracks_n", [](const FourVectorRVec &tracks) { return (int)tracks.size(); })
.Filter([](int tracks_n) { return tracks_n > 2; }, {"tracks_n"})
.Define("tracks_pts", getPt)
.Define("tracks_pts_weights", getPtWeights);
auto trN = augmented_d.Histo1D({"", "", 40, -.5, 39.5}, "tracks_n");
auto trPts = augmented_d.Histo1D("tracks_pts");
auto trWPts = augmented_d.Histo1D("tracks_pts", "tracks_pts_weights");
auto c1 = new TCanvas();
auto c2 = new TCanvas();
auto c3 = new TCanvas();
return 0;
December 2016
Danilo Piparo

Definition in file df002_dataModel.C.

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:60
Definition: df002_dataModel.py:1
int nentries
Definition: THbookFile.cxx:91
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42
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
@ v
Definition: rootcling_impl.cxx:3635
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:128
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:43
constexpr Double_t Pi()
Definition: TMath.h:43
Definition: TRandom3.h:27
double sqrt(double)
void tracks()
Definition: tracks.C:49
Definition: TCanvas.h:23
#define d(i)
Definition: RSha256.hxx:120
return c2
Definition: legend2.C:14
TPaveText * pt
Definition: entrylist_figure1.C:7
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
constexpr Double_t E()
Base of natural log:
Definition: TMath.h:102
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition: RVec.hxx:55
return c1
Definition: legend1.C:41