Skip to content

Commit 5713f46

Browse files
GoodingJamiemdessole
authored andcommitted
[tutorials] Add Python tutorial for RDF h1 analysis
1 parent ba7619b commit 5713f46

File tree

3 files changed

+126
-2
lines changed

3 files changed

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

tutorials/analysis/dataframe/index.md

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

141141
\anchor df_alltutorials
142-
@}
142+
@}

0 commit comments

Comments
 (0)