CMS 3D CMS Logo

DisplayManager.py
Go to the documentation of this file.
1 from builtins import range
2 import ROOT
3 import copy
4 import math
5 
7  leg.SetBorderSize(0)
8  leg.SetFillColor(10)
9  leg.SetLineColor(0)
10  leg.SetFillStyle(0)
11 # leg.SetTextSize(0.035)
12 # leg.SetTextFont(42)
13 
14 def createRatioCanvas(name, errorBandFillColor=14, errorBandStyle=3354):
15  cv = ROOT.TCanvas(name.replace('.pdf', ''), name.replace('.pdf', ''), 10, 10, 700, 600)
16  ## this is the tricky part...
17  ## Divide with correct margins
18  cv.Divide(1, 2, 0.0, 0.0)
19  ## Set Pad sizes
20  cv.GetPad(1).SetPad(0.0, 0.32, 1., 1.0)
21  cv.GetPad(2).SetPad(0.0, 0.00, 1., 0.34)
22  cv.GetPad(1).SetFillStyle(4000)
23  cv.GetPad(2).SetFillStyle(4000)
24  ## Set pad margins 1
25  cv.cd(1)
26  ROOT.gPad.SetTopMargin(0.08)
27  ROOT.gPad.SetLeftMargin(0.12)
28  ROOT.gPad.SetBottomMargin(0.03)
29  ROOT.gPad.SetRightMargin(0.1)
30  ## Set pad margins 2
31  cv.cd(2)
32  ROOT.gPad.SetBottomMargin(0.35)
33  ROOT.gPad.SetLeftMargin(0.12)
34  ROOT.gPad.SetRightMargin(0.1)
35 
36  bogyHist = ROOT.TH1F("legendPseudoHist", "", 1, 1., 2.)
37  bogyHist.SetFillColor(errorBandFillColor)
38  bogyHist.SetFillStyle(errorBandStyle)
39  bogyHist.SetLineColor(0)
40 
41  cv.cd(1)
42  return cv
43 
44 def checkDifferences(histos):
45  rel_diff = 0
46  if len(histos)>0:
47  for ib in range(0, histos[0].GetNbinsX()):
48  for ih, h in enumerate(histos):
49  if not ih == 0:
50  if not histos[0].GetBinContent(ib+1) == 0:
51  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))
52  return math.sqrt(rel_diff)
53 
55  def __init__(self, name, ratio):
56  if ratio:
57  self.canvas = createRatioCanvas(name.replace('pdf', ''))
58  else:
59  self.canvas = ROOT.TCanvas(name.replace('.pdf', ''))
60  self.name = name
61  self.draw_ratio = ratio
62  self.histos = []
63  self.Legend = ROOT.TLegend(0.15, 0.79, 0.5, 0.89)
65  self.draw_ratioLegend = ROOT.TLegend(0.15, 0.79, 0.5, 0.89)
67  self.pullRange = 0.5
68  self.box = ROOT.TPave(0.93,0.85,0.98,0.90)
69  self.canvas.Print(self.name + '[')
70 
71  def __del__(self):
72  self.canvas.Print(self.name + ']')
73 
74  def Draw(self, histos, titles):
75  self.histos = histos
76  ymax = max(h.GetMaximum() for h in self.histos)
77  self.Legend.Clear()
78  self.draw_ratioLegend.Clear()
79  for i, h in enumerate(self.histos):
80  title = titles[i]
81  h.GetYaxis().SetRangeUser(0., ymax * 1.3)
82  self.Legend.AddEntry(h, title + ': ' + str(h.Integral()))
83  if i == 0:
84  h.Draw('HIST E')
85  else:
86  h.Draw('SAME HIST E')
87  self.Legend.Draw()
88  ## add an assessment on the agreement
89  rel_diff = checkDifferences(self.histos)
90  self.box.SetLineColor(1)
91  self.box.SetLineWidth(1)
92  self.box.SetShadowColor(0)
93  if rel_diff <= 0.01:
94  self.box.SetFillColor(416) ## kGreen
95  elif rel_diff <= 0.10:
96  self.box.SetFillColor(797) ## kOrange
97  else:
98  self.box.SetFillColor(632) ## kRed
99  self.box.ConvertNDCtoPad()
100  self.box.Draw()
101 
102  pull_histos = []
103  if self.draw_ratio:
104  self.canvas.cd(2)
105  for ihist in range(1, len(self.histos)):
106  histPull = copy.deepcopy(self.histos[ihist])
107  pull_histos.append(histPull)
108  histPull.Divide(self.histos[0])
109  histPull.UseCurrentStyle()
110  histPull.SetLineColor(self.histos[ihist].GetLineColor())
111  histPull.SetMarkerColor(self.histos[ihist].GetLineColor())
112  histPull.SetLineStyle(self.histos[ihist].GetLineStyle())
113  histPull.SetLineWidth(self.histos[ihist].GetLineWidth())
114  histPull.GetYaxis().SetRangeUser(-self.pullRange + 1., self.pullRange + 1.)
115  # defaultYtoPixel = 408. # height in pixels of default canvas
116  defaultYtoPixel = self.canvas.GetPad(1).YtoPixel(0.)
117  pad2YtoPixel = float(self.canvas.GetPad(2).YtoPixel(0))
118  pad2XaxisFactor = defaultYtoPixel / pad2YtoPixel
119  histPull.GetXaxis().SetLabelSize(self.histos[0].GetXaxis().GetLabelSize()*pad2XaxisFactor)
120  histPull.GetXaxis().SetLabelOffset(self.histos[0].GetXaxis().GetLabelOffset()*pad2XaxisFactor)
121  histPull.GetXaxis().SetTitleSize(self.histos[0].GetXaxis().GetTitleSize()*pad2XaxisFactor)
122  histPull.GetXaxis().SetTitleOffset(self.histos[0].GetXaxis().GetTitleOffset()/pad2XaxisFactor*2.5)
123  histPull.GetYaxis().SetLabelSize(self.histos[0].GetYaxis().GetLabelSize()*pad2XaxisFactor)
124  histPull.GetYaxis().SetLabelOffset(self.histos[0].GetYaxis().GetLabelOffset()*pad2XaxisFactor)
125  histPull.GetYaxis().SetTitleSize(self.histos[0].GetYaxis().GetTitleSize()*pad2XaxisFactor)
126  histPull.GetYaxis().SetTitleOffset(self.histos[0].GetYaxis().GetTitleOffset()/pad2XaxisFactor)
127  histPull.GetYaxis().CenterTitle()
128  histPull.GetXaxis().SetTickLength(histPull.GetXaxis().GetTickLength()*pad2XaxisFactor)
129  histPull.GetYaxis().SetNdivisions(306)
130  histPull.GetYaxis().SetTitle("Ratio to " + titles[0])
131  histPull.SetTitle('')
132  if ihist == 1:
133  histPull.Draw("ep")
134  else:
135  histPull.Draw("same ep")
136  self.draw_ratioLegend.AddEntry(histPull, titles[ihist])
137  # This is a little bit ugly though ...
138  for i, h in enumerate(self.histos):
139  h.GetXaxis().SetLabelSize(0)
140  self.canvas.cd(1)
141  self.canvas.Update()
142  self.canvas.Print(self.name)
def __init__(self, name, ratio)
def checkDifferences(histos)
def createRatioCanvas(name, errorBandFillColor=14, errorBandStyle=3354)
def applyLegendSettings(leg)
def Draw(self, histos, titles)
#define str(s)