Use RVecs to plot the transverse momentum of selected particles.
This tutorial shows how VecOps can be used to slim down the programming model typically adopted in HEP for analysis. In this case we have a dataset containing the kinematic properties of particles stored in individual arrays. We want to plot the transverse momentum of these particles if the energy is greater than 100 MeV.
Author:
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, March 23, 2023 at 10:44 AM.
\macro_code \macro_image
\date March 2018 \authors Danilo Piparo (CERN), Andre Vieira Silva
import ROOT
filename = ROOT.gROOT.GetTutorialDir().Data() + "/dataframe/df017_vecOpsHEP.root"
treename = "myDataset"
def WithPyROOT(filename):
from math import sqrt
f = ROOT.TFile(filename)
h = ROOT.TH1F("pt", "With PyROOT", 16, 0, 4)
for event in f.myDataset:
for E, px, py in zip(event.E, event.px, event.py):
if (E > 100):
h.Fill(sqrt(px*px + py*py))
h.DrawCopy()
def WithRDataFrameVecOpsJit(treename, filename):
f = ROOT.RDataFrame(treename, filename)
h = f.Define("good_pt", "sqrt(px*px + py*py)[E>100]")\
.Histo1D(("pt", "With RDataFrame and RVec", 16, 0, 4), "good_pt")
h.DrawCopy()
Welcome to JupyROOT 6.29/01
We plot twice the same quantity, the key is to look into the implementation of the functions above.
c = ROOT.TCanvas()
c.Divide(2,1)
c.cd(1)
WithPyROOT(filename)
c.cd(2)
WithRDataFrameVecOpsJit(treename, filename)
c.SaveAs("df017_vecOpsHEP.png")
print("Saved figure to df017_vecOpsHEP.png")
Saved figure to df017_vecOpsHEP.png
Info in <TCanvas::Print>: png file df017_vecOpsHEP.png has been created
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()