Skip to content

Commit fd16f53

Browse files
committed
[tutorials] Add Python tutorial for RDF h1 analysis
1 parent 800af77 commit fd16f53

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
import ROOT
3+
import math
4+
5+
ROOT.EnableImplicitMT(4)
6+
7+
# Declare filters on RVec objects and JIT with Numba
8+
@ROOT.Numba.Declare(["int", "int", "RVecI"], "bool")
9+
def ik_ipi_nhitrp_cut(ik, ipi, nhitrp):
10+
return nhitrp[ik - 1] * nhitrp[ipi - 1] > 1
11+
12+
@ROOT.Numba.Declare(["int", "RVecF", "RVecF"], "bool")
13+
def ik_rstart_rend_cut(ik, rstart, rend):
14+
return rend[ik - 1] - rstart[ik - 1] > 22
15+
16+
@ROOT.Numba.Declare(["int", "RVecF", "RVecF"], "bool")
17+
def ipi_rstart_rend_cut(ipi, rstart, rend):
18+
return rend[ipi - 1] - rstart[ipi - 1] > 22
19+
20+
@ROOT.Numba.Declare(["int", "RVecF"], "bool")
21+
def ik_nlhk_cut(ik, nlhk):
22+
return nlhk[ik - 1] > 0.1
23+
24+
@ROOT.Numba.Declare(["int", "RVecF"], "bool")
25+
def ipi_nlhpi_cut(ipi, nlhpi):
26+
return nlhpi[ipi - 1] > 0.1
27+
28+
@ROOT.Numba.Declare(["int", "RVecF"], "bool")
29+
def ipis_nlhpi_cut(ipis, nlhpi):
30+
return nlhpi[ipis - 1] > 0.1
31+
32+
def select(rdf):
33+
return rdf.Filter("TMath::Abs(md0_d - 1.8646) < 0.04")\
34+
.Filter("ptds_d > 2.5")\
35+
.Filter("TMath::Abs(etads_d) < 1.5")\
36+
.Filter("Numba::ik_ipi_nhitrp_cut(ik, ipi, nhitrp)")\
37+
.Filter("Numba::ik_rstart_rend_cut(ik, rstart, rend)")\
38+
.Filter("Numba::ipi_rstart_rend_cut(ipi, rstart, rend)")\
39+
.Filter("Numba::ik_nlhk_cut(ik, nlhk)")\
40+
.Filter("Numba::ipi_nlhpi_cut(ipi, nlhpi)")\
41+
.Filter("Numba::ipis_nlhpi_cut(ipis, nlhpi)")\
42+
43+
dxbin = (0.17 - 0.13) / 40 # Bin-width
44+
45+
def fdm5(xx, par):
46+
x = xx[0]
47+
if x <= 0.13957:
48+
return 0
49+
xp3 = (x - par[3]) ** 2
50+
res = dxbin * (par[0] * (x - 0.13957) ** par[1]) + par[2] / 2.5066 / par[4] * math.exp(-xp3 / 2 / par[4] ** 2)
51+
return res
52+
53+
def fdm2(xx, par):
54+
sigma = 0.0012
55+
x = xx[0]
56+
if x <= 0.13957:
57+
return 0
58+
xp3 = (x - 0.1454) ** 2
59+
res = dxbin * (par[0] * (x - 0.13957) ** 0.25) + par[1] / 2.5066 / sigma * math.exp(-xp3 / 2 / sigma **2)
60+
return res
61+
62+
def FitAndPlotHdmd(hdmd: ROOT.TH1):
63+
ROOT.gStyle.SetOptFit()
64+
c1 = ROOT.TCanvas("c1", "h1analysis analysis", 10, 10, 800, 600)
65+
66+
hdmd.GetXaxis().SetTitleOffset(1.4)
67+
68+
hdraw = hdmd.DrawClone()
69+
70+
# Fit histogram hdmd with function f5 using the loglikelihood option
71+
f5 = ROOT.TF1("f5", fdm5, 0.139, 0.17, 5)
72+
f5.SetParameters(1000000, .25, 2000, .1454, .001)
73+
hdraw.Fit("f5", "lr")
74+
75+
return
76+
77+
78+
def FitAndPlotH2(h2: ROOT.TH2):
79+
80+
# Create the canvas for tau d0
81+
c2 = ROOT.TCanvas("c2", "tauD0", 100, 100, 800, 600)
82+
83+
c2.SetGrid()
84+
c2.SetBottomMargin(0.15)
85+
86+
# Project slices of 2-d histogram h2 along X , then fit each slice
87+
# with function f2 and make a histogram for each fit parameter
88+
# Note that the generated histograms are added to the list of objects
89+
# in the current directory.
90+
91+
f2 = ROOT.TF1("f2", fdm2, 0.139, 0.17, 2)
92+
f2.SetParameters(10000, 10)
93+
h2.FitSlicesX(f2, 0, -1, 1, "qln")
94+
95+
# See TH2::FitSlicesX documentation why h2_1 name is used
96+
h2_1 = ROOT.gDirectory.Get("h2_1")
97+
h2_1.SetDirectory(ROOT.nullptr)
98+
h2_1.GetXaxis().SetTitle("#tau [ps]")
99+
h2_1.SetMarkerStyle(21)
100+
h2_1.Draw()
101+
102+
c2.Update()
103+
104+
line = ROOT.Tline(0, 0, 0, c2.GetUymax())
105+
line.Draw()
106+
107+
return
108+
109+
110+
chain = ROOT.TChain("h42")
111+
chain.Add("root://eospublic.cern.ch//eos/root-eos/h1/dstarmb.root")
112+
chain.Add("root://eospublic.cern.ch//eos/root-eos/h1/dstarp1a.root")
113+
chain.Add("root://eospublic.cern.ch//eos/root-eos/h1/dstarp1b.root")
114+
chain.Add("root://eospublic.cern.ch//eos/root-eos/h1/dstarp2.root")
115+
116+
df = ROOT.RDataFrame(chain)
117+
selected = select(df)
118+
# Note: The title syntax is "<Title>;<Label x axis>;<Label y axis>"
119+
hdmdARP = selected.Histo1D(("hdmd", "Dm_d;m_{K#pi#pi} - m_{K#pi}[GeV/c^{2}]", 40, 0.13, 0.17), "dm_d")
120+
selected_added_branch = selected.Define("h2_y", "rpd0_t / 0.029979f * 1.8646f / ptd0_d")
121+
h2ARP = selected_added_branch.Histo2D(("h2", "ptD0 vs Dm_d", 30, 0.135, 0.165, 30, -3, 6), "dm_d", "h2_y")
122+
123+
FitAndPlotHdmd(hdmdARP)
124+
FitAndPlotH2(h2ARP)

tutorials/analysis/dataframe/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ With RDataFrame advanced analyses can be executed on large amounts of data. Thes
129129
| **Tutorial** || **Description** |
130130
|------|--------|-----------------|
131131
| df017_vecOpsHEP.C | df017_vecOpsHEP.py | Use RVecs to plot the transverse momentum of selected particles. |
132-
| df101_h1Analysis.C | | Express ROOT's standard H1 analysis. |
132+
| df101_h1Analysis.C | df101_h1Analysis.py | Express ROOT's standard H1 analysis. |
133133
| df102_NanoAODDimuonAnalysis.C | df102_NanoAODDimuonAnalysis.py | Process NanoAOD files. |
134134
| df103_NanoAODHiggsAnalysis.C | df103_NanoAODHiggsAnalysis.py | An example of complex analysis: reconstructing the Higgs boson. |
135135
| | df104_HiggsToTwoPhotons.py | The Higgs to two photons analysis from the ATLAS Open Data 2020 release. |
@@ -138,4 +138,4 @@ With RDataFrame advanced analyses can be executed on large amounts of data. Thes
138138
| | df107_SingleTopAnalysis.py | A single top analysis using the ATLAS Open Data release of 2020. |
139139

140140
\anchor df_alltutorials
141-
@}
141+
@}

0 commit comments

Comments
 (0)