Skip to content

Commit 698dfc6

Browse files
GoodingJamiemdessole
authored andcommitted
[tutorials] Add Python tutorial for RDF h1 analysis
1 parent 556dba8 commit 698dfc6

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

tutorials/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ endif()
181181

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