File indexing completed on 2023-03-17 11:21:43
0001 from builtins import range
0002 import ROOT
0003 import copy
0004 import math
0005
0006 def applyLegendSettings(leg):
0007 leg.SetBorderSize(0)
0008 leg.SetFillColor(10)
0009 leg.SetLineColor(0)
0010 leg.SetFillStyle(0)
0011
0012
0013
0014 def createRatioCanvas(name, errorBandFillColor=14, errorBandStyle=3354):
0015 cv = ROOT.TCanvas(name.replace('.pdf', ''), name.replace('.pdf', ''), 10, 10, 700, 600)
0016
0017
0018 cv.Divide(1, 2, 0.0, 0.0)
0019
0020 cv.GetPad(1).SetPad(0.0, 0.32, 1., 1.0)
0021 cv.GetPad(2).SetPad(0.0, 0.00, 1., 0.34)
0022 cv.GetPad(1).SetFillStyle(4000)
0023 cv.GetPad(2).SetFillStyle(4000)
0024
0025 cv.cd(1)
0026 ROOT.gPad.SetTopMargin(0.08)
0027 ROOT.gPad.SetLeftMargin(0.12)
0028 ROOT.gPad.SetBottomMargin(0.03)
0029 ROOT.gPad.SetRightMargin(0.1)
0030
0031 cv.cd(2)
0032 ROOT.gPad.SetBottomMargin(0.35)
0033 ROOT.gPad.SetLeftMargin(0.12)
0034 ROOT.gPad.SetRightMargin(0.1)
0035
0036 bogyHist = ROOT.TH1F("legendPseudoHist", "", 1, 1., 2.)
0037 bogyHist.SetFillColor(errorBandFillColor)
0038 bogyHist.SetFillStyle(errorBandStyle)
0039 bogyHist.SetLineColor(0)
0040
0041 cv.cd(1)
0042 return cv
0043
0044 def checkDifferences(histos):
0045 rel_diff = 0
0046 if len(histos)>0:
0047 for ib in range(0, histos[0].GetNbinsX()):
0048 for ih, h in enumerate(histos):
0049 if not ih == 0:
0050 if not histos[0].GetBinContent(ib+1) == 0:
0051 rel_diff+=((h.GetBinContent(ib+1)-histos[0].GetBinContent(ib+1))/histos[0].GetBinContent(ib+1))*((h.GetBinContent(ib+1)-histos[0].GetBinContent(ib+1))/histos[0].GetBinContent(ib+1))
0052 return math.sqrt(rel_diff)
0053
0054 class DisplayManager(object):
0055 def __init__(self, name, ratio):
0056 if ratio:
0057 self.canvas = createRatioCanvas(name.replace('pdf', ''))
0058 else:
0059 self.canvas = ROOT.TCanvas(name.replace('.pdf', ''))
0060 self.name = name
0061 self.draw_ratio = ratio
0062 self.histos = []
0063 self.Legend = ROOT.TLegend(0.15, 0.79, 0.5, 0.89)
0064 applyLegendSettings(self.Legend)
0065 self.draw_ratioLegend = ROOT.TLegend(0.15, 0.79, 0.5, 0.89)
0066 applyLegendSettings(self.draw_ratioLegend)
0067 self.pullRange = 0.5
0068 self.box = ROOT.TPave(0.93,0.85,0.98,0.90)
0069 self.canvas.Print(self.name + '[')
0070
0071 def __del__(self):
0072 self.canvas.Print(self.name + ']')
0073
0074 def Draw(self, histos, titles):
0075 self.histos = histos
0076 ymax = max(h.GetMaximum() for h in self.histos)
0077 self.Legend.Clear()
0078 self.draw_ratioLegend.Clear()
0079 for i, h in enumerate(self.histos):
0080 title = titles[i]
0081 h.GetYaxis().SetRangeUser(0., ymax * 1.3)
0082 self.Legend.AddEntry(h, title + ': ' + str(h.Integral()))
0083 if i == 0:
0084 h.Draw('HIST E')
0085 else:
0086 h.Draw('SAME HIST E')
0087 self.Legend.Draw()
0088
0089 rel_diff = checkDifferences(self.histos)
0090 self.box.SetLineColor(1)
0091 self.box.SetLineWidth(1)
0092 self.box.SetShadowColor(0)
0093 if rel_diff <= 0.01:
0094 self.box.SetFillColor(416)
0095 elif rel_diff <= 0.10:
0096 self.box.SetFillColor(797)
0097 else:
0098 self.box.SetFillColor(632)
0099 self.box.ConvertNDCtoPad()
0100 self.box.Draw()
0101
0102 pull_histos = []
0103 if self.draw_ratio:
0104 self.canvas.cd(2)
0105 for ihist in range(1, len(self.histos)):
0106 histPull = copy.deepcopy(self.histos[ihist])
0107 pull_histos.append(histPull)
0108 histPull.Divide(self.histos[0])
0109 histPull.UseCurrentStyle()
0110 histPull.SetLineColor(self.histos[ihist].GetLineColor())
0111 histPull.SetMarkerColor(self.histos[ihist].GetLineColor())
0112 histPull.SetLineStyle(self.histos[ihist].GetLineStyle())
0113 histPull.SetLineWidth(self.histos[ihist].GetLineWidth())
0114 histPull.GetYaxis().SetRangeUser(-self.pullRange + 1., self.pullRange + 1.)
0115
0116 defaultYtoPixel = self.canvas.GetPad(1).YtoPixel(0.)
0117 pad2YtoPixel = float(self.canvas.GetPad(2).YtoPixel(0))
0118 pad2XaxisFactor = defaultYtoPixel / pad2YtoPixel
0119 histPull.GetXaxis().SetLabelSize(self.histos[0].GetXaxis().GetLabelSize()*pad2XaxisFactor)
0120 histPull.GetXaxis().SetLabelOffset(self.histos[0].GetXaxis().GetLabelOffset()*pad2XaxisFactor)
0121 histPull.GetXaxis().SetTitleSize(self.histos[0].GetXaxis().GetTitleSize()*pad2XaxisFactor)
0122 histPull.GetXaxis().SetTitleOffset(self.histos[0].GetXaxis().GetTitleOffset()/pad2XaxisFactor*2.5)
0123 histPull.GetYaxis().SetLabelSize(self.histos[0].GetYaxis().GetLabelSize()*pad2XaxisFactor)
0124 histPull.GetYaxis().SetLabelOffset(self.histos[0].GetYaxis().GetLabelOffset()*pad2XaxisFactor)
0125 histPull.GetYaxis().SetTitleSize(self.histos[0].GetYaxis().GetTitleSize()*pad2XaxisFactor)
0126 histPull.GetYaxis().SetTitleOffset(self.histos[0].GetYaxis().GetTitleOffset()/pad2XaxisFactor)
0127 histPull.GetYaxis().CenterTitle()
0128 histPull.GetXaxis().SetTickLength(histPull.GetXaxis().GetTickLength()*pad2XaxisFactor)
0129 histPull.GetYaxis().SetNdivisions(306)
0130 histPull.GetYaxis().SetTitle("Ratio to " + titles[0])
0131 histPull.SetTitle('')
0132 if ihist == 1:
0133 histPull.Draw("ep")
0134 else:
0135 histPull.Draw("same ep")
0136 self.draw_ratioLegend.AddEntry(histPull, titles[ihist])
0137
0138 for i, h in enumerate(self.histos):
0139 h.GetXaxis().SetLabelSize(0)
0140 self.canvas.cd(1)
0141 self.canvas.Update()
0142 self.canvas.Print(self.name)