157def plotVariations(ao, outdir, label, threshold=0.05, xLabel=None, title=None):
158 if ao.dim() != 2: return
159 tgraphs = {}
160 tgraphs['all'] = [r.TGraphAsymmErrors(), r.TGraphAsymmErrors(), r.TGraphAsymmErrors(), r.TGraphAsymmErrors()]
161 maxErr = 0
162
163 nbins = ao.numPoints()
164 corr = ao.annotation('ErrorBreakdown') if '1.7' in yoda.version() else yaml.load(ao.annotation('ErrorBreakdown'))
165
166 if len(corr) == 0: return
167 systList = corr[0].keys()
168
169 all_up = [0 for i in range(nbins)]
170 all_dn = [0 for i in range(nbins)]
171 for sname in systList:
172 tgu = r.TGraphAsymmErrors()
173 tgu_line = r.TGraphAsymmErrors()
174 tgd = r.TGraphAsymmErrors()
175 tgd_line = r.TGraphAsymmErrors()
176 pt = 0
177 draw = 0
178 for i in range(nbins):
179 nominalx = ao.points()[i].x()
180 nominaly = ao.points()[i].y()
181 xerr = abs(nominalx - ao.points()[i].xMin())
182 tgu.SetPoint(pt, nominalx, 1)
183 tgd.SetPoint(pt, nominalx, 1)
184 if nominaly == 0: continue
185 eup = 1 - (((corr[i][sname]['up'])) + nominaly) / nominaly
186 edn = 1 - (((corr[i][sname]['dn'])) + nominaly) / nominaly
187 all_up[i] = (all_up[i] ** 2 + eup ** 2) ** 0.5
188 all_dn[i] = (all_dn[i] ** 2 + edn ** 2) ** 0.5
189 if (abs(eup) > threshold): draw = 1
190 if (abs(edn) > threshold): draw = 1
191 if (abs(edn) > threshold): draw = 1
192 if abs(all_up[i]) > maxErr: maxErr = abs(all_up[i])
193 tgu.SetPointError(pt, xerr, xerr, 0, eup)
194 tgd.SetPointError(pt, xerr, xerr, 0, edn)
195 tgu_line.SetPoint(pt, nominalx, 1 + eup)
196 tgd_line.SetPoint(pt, nominalx, 1 + edn)
197 tgu_line.SetPointError(pt, xerr, xerr, 0, 0)
198 tgd_line.SetPointError(pt, xerr, xerr, 0, 0)
199 tgraphs['all'][0].SetPoint(pt, nominalx, nominaly / nominaly)
200 tgraphs['all'][1].SetPoint(pt, nominalx, nominaly / nominaly)
201 tgraphs['all'][2].SetPoint(pt, nominalx, 1 - all_dn[i])
202 tgraphs['all'][3].SetPoint(pt, nominalx, 1 + all_up[i])
203 tgraphs['all'][0].SetPointError(pt, xerr, xerr, 0, -1 * all_dn[i])
204 tgraphs['all'][1].SetPointError(pt, xerr, xerr, 0, all_up[i])
205 tgraphs['all'][2].SetPointError(pt, xerr, xerr, 0, 0)
206 tgraphs['all'][3].SetPointError(pt, xerr, xerr, 0, 0)
207 pt += 1
208 if (draw): tgraphs[sname] = [tgd, tgu, tgd_line, tgu_line]
209 r.gROOT.SetBatch()
210 tc = r.TCanvas("c", "c", 500, 500)
211 same = 0
212 itg = 0
213 leg = r.TLegend(.13, .6, .9, .8)
214 leg.SetNColumns(2)
215 leg.SetBorderSize(0)
216 leg.SetFillColor(0)
217 leg.SetFillStyle(0)
218 leg.SetTextFont(42)
219 leg.SetTextSize(0.025)
220
221 for tgname, tg in sorted(tgraphs.items(), key=lambda x: abs(x[1][1].GetErrorYhigh(0)), reverse=True):
222 itg += 1
223 isCorr = not (('stat' in tgname) or ('all' in tgname))
224 if 'nominal' == tgname: continue
225 if 'mc' == tgname: continue
226 tg[0].SetFillColorAlpha(0 + itg, 0.3)
227 tg[1].SetFillColorAlpha(0 + itg, 0.3)
228 tg[0].SetMarkerColor(1)
229 tg[2].SetMarkerStyle(23)
230 tg[2].SetMarkerColor(0 + itg)
231 tg[2].SetLineColor(0 + itg)
232 tg[3].SetMarkerStyle(22)
233 tg[3].SetMarkerColor(0 + itg)
234 tg[3].SetLineColor(0 + itg)
235 leg.AddEntry(tg[0], tgname, "f")
236 if same:
237 tg[0].Draw("p2 same")
238 tg[1].Draw("p2 same")
239 if isCorr: tg[2].Draw("P same")
240 if isCorr: tg[3].Draw("P same ")
241 else:
242 tg[0].SetMaximum((1. + maxErr) * 1.33)
243 tg[0].SetMinimum(1. - maxErr)
244 tg[0].Draw("AP2")
245 tg[1].Draw("P2 same ")
246 if isCorr: tg[2].Draw("P same")
247 if isCorr: tg[3].Draw("P same")
248 same = 1
249 leg.Draw()
250 lat = r.TLatex()
251 leg.SetTextFont(42)
252 lat.SetNDC()
253 lat.SetTextSize(0.05)
254 lat.DrawLatex(0.1, 0.91, "#it{ATLAS} #bf{Internal}")
255 lat.SetTextSize(0.035)
256 lat.DrawLatex(0.13, 0.83, "#bf{All variations >%1.1f%% anywhere in range:}" % (threshold * 100))
257 if xLabel:
258 lat.SetTextAlign(33)
259 lat.SetTextSize(0.04)
260 lat.DrawLatex(0.9, 0.05,
"#bf{%s}" % xLabel.replace(
"$",
"").
replace(
"\\text",
""))
261 lat.SetTextAngle(90)
262 lat.DrawLatex(0.02, 0.9, "#bf{Relative Uncertainty}")
263 outfile = "%s/%s-variations" % (outdir, label)
264 tc.SaveAs(outfile + ".pdf")
265 tc.SaveAs(outfile + ".png")
266
267