Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
df017_vecOpsHEP.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_dataframe
3## \notebook -draw
4## Use RVecs to plot the transverse momentum of selected particles.
5##
6## This tutorial shows how VecOps can be used to slim down the programming
7## model typically adopted in HEP for analysis.
8## In this case we have a dataset containing the kinematic properties of
9## particles stored in individual arrays.
10## We want to plot the transverse momentum of these particles if the energy is
11## greater than 100 MeV.
12
13## \macro_code
14## \macro_image
15##
16## \date March 2018
17## \authors Danilo Piparo (CERN), Andre Vieira Silva
18
19import ROOT
20import numpy as np
21
22filename = ROOT.gROOT.GetTutorialDir().Data() + "/analysis/dataframe/df017_vecOpsHEP.root"
23treename = "myDataset"
24
25def WithPyROOT(filename):
26 from math import sqrt
27 f = ROOT.TFile(filename)
28 h = ROOT.TH1F("pt", "With PyROOT", 16, 0, 4)
29 for event in f[treename]:
30 h.Fill(
32 [
33 sqrt(px * px + py * py)
34 for E, px, py in zip(event.E, event.px, event.py)
35 if E > 100
36 ]
37 )
38 )
40
41def WithRDataFrameVecOpsJit(treename, filename):
42 f = ROOT.RDataFrame(treename, filename)
43 h = f.Define("good_pt", "sqrt(px*px + py*py)[E>100]")\
44 .Histo1D(("pt", "With RDataFrame and RVec", 16, 0, 4), "good_pt")
46
47## We plot twice the same quantity, the key is to look into the implementation
48## of the functions above.
49c = ROOT.TCanvas()
50c.Divide(2,1)
51c.cd(1)
52WithPyROOT(filename)
53c.cd(2)
54WithRDataFrameVecOpsJit(treename, filename)
55c.SaveAs("df017_vecOpsHEP.png")
56
57print("Saved figure to df017_vecOpsHEP.png")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...