Skip to content

Commit 6ddcc0d

Browse files
committed
Fix drawing in df104 tutorial
1. Disable automatic drawing when fitting 2. Draw data histogram only when all settings are done 3. Use TH1I instead of TF1 for axis drawing in lower pad Created canvas can be stored and read back properly from ROOT file
1 parent a3a8503 commit 6ddcc0d

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

tutorials/dataframe/df104_HiggsToTwoPhotons.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,38 +113,40 @@
113113
lower_pad.Draw()
114114

115115
# Fit signal + background model to data
116-
upper_pad.cd()
117116
fit = ROOT.TF1("fit", "([0]+[1]*x+[2]*x^2+[3]*x^3)+[4]*exp(-0.5*((x-[5])/[6])^2)", 105, 160)
118117
fit.FixParameter(5, 125.0)
119118
fit.FixParameter(4, 119.1)
120119
fit.FixParameter(6, 2.39)
121120
fit.SetLineColor(2)
122121
fit.SetLineStyle(1)
123122
fit.SetLineWidth(2)
124-
data.Fit("fit", "", "E SAME", 105, 160)
125-
fit.Draw("SAME")
126-
127-
# Draw background
128-
bkg = ROOT.TF1("bkg", "([0]+[1]*x+[2]*x^2+[3]*x^3)", 105, 160)
129-
for i in range(4):
130-
bkg.SetParameter(i, fit.GetParameter(i))
131-
bkg.SetLineColor(4)
132-
bkg.SetLineStyle(2)
133-
bkg.SetLineWidth(2)
134-
bkg.Draw("SAME")
123+
data.Fit("fit", "0", "", 105, 160)
135124

136125
# Draw data
126+
upper_pad.cd()
137127
data.SetMarkerStyle(20)
138128
data.SetMarkerSize(1.2)
139129
data.SetLineWidth(2)
140130
data.SetLineColor(ROOT.kBlack)
141-
data.Draw("E SAME")
142131
data.SetMinimum(1e-3)
143132
data.SetMaximum(8e3)
144133
data.GetYaxis().SetLabelSize(0.045)
145134
data.GetYaxis().SetTitleSize(0.05)
146135
data.SetStats(0)
147136
data.SetTitle("")
137+
data.Draw("E")
138+
139+
# Draw fit
140+
fit.Draw("SAME")
141+
142+
# Draw background
143+
bkg = ROOT.TF1("bkg", "([0]+[1]*x+[2]*x^2+[3]*x^3)", 105, 160)
144+
for i in range(4):
145+
bkg.SetParameter(i, fit.GetParameter(i))
146+
bkg.SetLineColor(4)
147+
bkg.SetLineStyle(2)
148+
bkg.SetLineWidth(2)
149+
bkg.Draw("SAME")
148150

149151
# Scale simulated events with luminosity * cross-section / sum of weights
150152
# and merge to single Higgs signal
@@ -158,7 +160,7 @@
158160
# Draw ratio
159161
lower_pad.cd()
160162

161-
ratiobkg = ROOT.TF1("zero", "0", 105, 160)
163+
ratiobkg = ROOT.TH1I("zero", "", 100, 105, 160)
162164
ratiobkg.SetLineColor(4)
163165
ratiobkg.SetLineStyle(2)
164166
ratiobkg.SetLineWidth(2)
@@ -175,7 +177,7 @@
175177
ratiobkg.GetYaxis().SetNdivisions(503, False)
176178
ratiobkg.GetYaxis().ChangeLabel(-1, -1, 0)
177179
ratiobkg.GetXaxis().SetTitle("m_{#gamma#gamma} [GeV]")
178-
ratiobkg.Draw()
180+
ratiobkg.Draw("AXIS")
179181

180182
ratiosig = ROOT.TH1F("ratiosig", "ratiosig", 5500, 105, 160)
181183
ratiosig.Eval(fit)
@@ -187,9 +189,9 @@
187189

188190
ratiodata = data.Clone()
189191
ratiodata.Add(bkg, -1)
190-
ratiodata.Draw("E SAME")
191192
for i in range(1, data.GetNbinsX()):
192193
ratiodata.SetBinError(i, data.GetBinError(i))
194+
ratiodata.Draw("E SAME")
193195

194196
# Add legend
195197
upper_pad.cd()
@@ -203,7 +205,7 @@
203205
legend.AddEntry(bkg, "Background", "l")
204206
legend.AddEntry(fit, "Signal + Bkg.", "l")
205207
legend.AddEntry(higgs, "Signal", "l")
206-
legend.Draw("SAME")
208+
legend.Draw()
207209

208210
# Add ATLAS label
209211
text = ROOT.TLatex()

0 commit comments

Comments
 (0)