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