ATLAS Offline Software
Loading...
Searching...
No Matches
HistDecorate.py
Go to the documentation of this file.
2#Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3#
4
5import ROOT
6
7Dic_etaStation_dbZ = {
8 1:[1,2],
9 2:[1,2,3],
10 3:[1,2],
11 4:[1,2,3],
12 5:[1,2],
13 6:[1,2],
14 7:[1,2],
15 8:[1,2]
16}
17
18
19xTitleOffset = {"TH1" : 0.9,
20 "TH2" : 0.9,
21 "TGraph" : 1.0,
22 "TEfficiency" : 1.0
23}
24yTitleOffset = {"TH1" : 1.6,
25 "TH2" : 1.0,
26 "TGraph" : 1.6,
27 "TEfficiency" : 1.6
28}
29zTitleOffset = {"TH1" : 0.,
30 "TH2" : 1.2,
31 "TGraph" : 0.,
32 "TEfficiency" : 0.
33}
34
35
36list_boxes_layers = {
37 1: [
38 ("1:3", "1:32"), ("7", "1:32"), ("9", "1:32"), ("19:20", "1:32"), ("30", "1:32"), ("32", "1:32"),("36:38", "1:32"),
39
40 ("4:12", "23:24"), ("4:12", "27:28"), ("27:35", "23:24"), ("27:35", "27:28"),
41
42 ("4", "3:4"), ("4", "7:8"), ("4", "11:12"), ("4", "15:16"), ("4", "19:20"), ("4", "31:32"),
43 ("35", "3:4"), ("35", "7:8"), ("35", "11:12"), ("35", "15:16"), ("35", "19:20"), ("35", "31:32"),
44
45 ("10", "1:2"), ("10", "5:6"), ("10", "9:10"), ("10", "13:14"), ("10", "17:18"), ("10", "21:22"), ("10", "29:30"),
46 ("14", "1:2"), ("14", "5:6"), ("14", "9:10"), ("14", "13:14"), ("14", "17:18"), ("14", "21:30"),
47 ("17", "1:2"), ("17", "13:14"), ("17", "17:18"), ("17", "21:22"), ("17", "25:26"), ("17", "29:30"),
48
49 ("29", "1:2"), ("29", "5:6"), ("29", "9:10"), ("29", "13:14"), ("29", "17:18"), ("29", "21:22"), ("29", "29:30"),
50 ("25", "1:2"), ("25", "5:6"), ("25", "9:10"), ("25", "13:14"), ("25", "17:18"), ("25", "21:30"),
51 ("22", "13:14"), ("22", "17:18"), ("22", "21:22"), ("22", "25:26"), ("22", "29:30"),
52 ],
53
54 2: [
55 ("1:3", "1:32"), ("7", "1:32"), ("9", "1:32"), ("19:20", "1:32"), ("30", "1:32"), ("32", "1:32"),("36:38", "1:32"),
56
57 ("4:12", "23:24"), ("4:12", "27:28"), ("27:35", "23:24"), ("27:35", "27:28"),
58
59 ("4", "3:4"), ("4", "7:8"), ("4", "11:12"), ("4", "15:16"), ("4", "19:20"), ("4", "31:32"),
60 ("35", "3:4"), ("35", "7:8"), ("35", "11:12"), ("35", "15:16"), ("35", "19:20"), ("35", "31:32"),
61
62 ("10", "1:2"), ("10", "5:6"), ("10", "9:10"), ("10", "13:14"), ("10", "17:18"), ("10", "21:22"), ("10", "29:30"),
63 ("14", "1:2"), ("14", "5:6"), ("14", "9:10"), ("14", "13:14"), ("14", "17:18"), ("14", "21:30"),
64 ("17", "1:2"), ("17", "13:14"), ("17", "17:18"), ("17", "21:22"), ("17", "25:26"), ("17", "29:30"),
65
66 ("29", "1:2"), ("29", "5:6"), ("29", "9:10"), ("29", "13:14"), ("29", "17:18"), ("29", "21:22"), ("29", "29:30"),
67 ("25", "1:2"), ("25", "5:6"), ("25", "9:10"), ("25", "13:14"), ("25", "17:18"), ("25", "21:30"),
68 ("22", "13:14"), ("22", "17:18"), ("22", "21:22"), ("22", "25:26"), ("22", "29:30"),
69 ],
70
71 3: [
72 ("1:4", "1:32"), ("7", "1:32"), ("19:20", "1:32"), ("32", "1:32"),("35:38", "1:32"),
73
74 ("5:11", "23:24"), ("5:11", "27:28"), ("28:34", "23:24"), ("28:34", "27:28"),
75
76 ("9:10", "1:2"), ("9:10", "5:6"), ("9:10", "9:10"), ("9:10", "13:14"), ("9:10", "17:18"), ("9:10", "21:22"), ("9:10", "29:30"),
77 ("29:30", "1:2"), ("29:30", "5:6"), ("29:30", "9:10"), ("29:30", "13:14"), ("29:30", "17:18"), ("29:30", "21:22"), ("29:30", "29:30"),
78
79 ("9", "25:26"), ("30", "25:26"),
80
81 ("14", "1:2"), ("14", "5:6"), ("14", "9:10"), ("14", "13:14"), ("14", "17:18"), ("14", "21:30"),
82 ("17", "1:2"), ("17", "13:14"), ("17", "17:18"), ("17", "21:22"), ("17", "25:26"), ("17", "29:30"),
83
84 ("25", "1:2"), ("25", "5:6"), ("25", "9:10"), ("25", "13:14"), ("25", "17:18"), ("25", "21:30"),
85 ("22", "13:14"), ("22", "17:18"), ("22", "21:22"), ("22", "25:26"), ("22", "29:30"),
86 ],
87
88 4: [
89 ("1:4", "1:32"), ("7", "1:32"), ("19:20", "1:32"), ("32", "1:32"),("35:38", "1:32"),
90
91 ("5:11", "23:24"), ("5:11", "27:28"), ("28:34", "23:24"), ("28:34", "27:28"),
92
93 ("9:10", "1:2"), ("9:10", "5:6"), ("9:10", "9:10"), ("9:10", "13:14"), ("9:10", "17:18"), ("9:10", "21:22"), ("9:10", "29:30"),
94 ("29:30", "1:2"), ("29:30", "5:6"), ("29:30", "9:10"), ("29:30", "13:14"), ("29:30", "17:18"), ("29:30", "21:22"), ("29:30", "29:30"),
95
96 ("9", "25:26"), ("30", "25:26"),
97
98 ("14", "1:2"), ("14", "5:6"), ("14", "9:10"), ("14", "13:14"), ("14", "17:18"), ("14", "21:30"),
99 ("17", "1:2"), ("17", "13:14"), ("17", "17:18"), ("17", "21:22"), ("17", "25:26"), ("17", "29:30"),
100
101 ("25", "1:2"), ("25", "5:6"), ("25", "9:10"), ("25", "13:14"), ("25", "17:18"), ("25", "21:30"),
102 ("22", "13:14"), ("22", "17:18"), ("22", "21:22"), ("22", "25:26"), ("22", "29:30"),
103 ],
104
105 5: [
106 ("1:4", "1:22"), ("19:20", "1:22"), ("35:38", "1:22"),
107 ("1:4", "29:32"), ("19:20", "29:32"), ("35:38", "29:32"),
108
109 ("9", "1:32"), ("14", "1:32"), ("25", "1:32"), ("30", "1:32"),
110
111 ("1", "23:28"), ("3", "23:28"), ("19", "23:28"), ("36", "23:28"), ("38", "23:28"),
112
113 ("5", "23:24"), ("10", "23:24"), ("15", "23:24"), ("24", "23:24"), ("29", "23:24"), ("34", "23:24"),
114 ("5", "27:28"), ("10", "27:28"), ("15", "27:28"), ("24", "27:28"), ("29", "27:28"), ("34", "27:28"),
115
116 ("4", "25:26"), ("20", "25:26"), ("35", "25:26"),
117 ],
118
119 6: [
120 ("1:4", "1:22"), ("19:20", "1:22"), ("35:38", "1:22"),
121 ("1:4", "29:32"), ("19:20", "29:32"), ("35:38", "29:32"),
122
123 ("9", "1:32"), ("14", "1:32"), ("25", "1:32"), ("30", "1:32"),
124
125 ("1", "23:28"), ("3", "23:28"), ("19", "23:28"), ("36", "23:28"), ("38", "23:28"),
126
127 ("5", "23:24"), ("10", "23:24"), ("15", "23:24"), ("24", "23:24"), ("29", "23:24"), ("34", "23:24"),
128 ("5", "27:28"), ("10", "27:28"), ("15", "27:28"), ("24", "27:28"), ("29", "27:28"), ("34", "27:28"),
129
130 ("4", "25:26"), ("20", "25:26"), ("35", "25:26"),
131 ],
132
133 7: [
134 ("1:38", "1:22"),
135 ("1:38", "29:32"),
136
137 ("14:25", "23:28"),
138 ("1:38", "25:26"),
139
140 ("1", "23:28"), ("3", "23:28"), ("5", "23:28"), ("34", "23:28"), ("36", "23:28"), ("38", "23:28"),
141
142 ("9:11", "23:28"), ("28:30", "23:28"),
143 ],
144
145 8: [
146 ("1:38", "1:22"),
147 ("1:38", "29:32"),
148
149 ("14:25", "23:28"),
150 ("1:38", "25:26"),
151
152 ("1", "23:28"), ("3", "23:28"), ("5", "23:28"), ("34", "23:28"), ("36", "23:28"), ("38", "23:28"),
153
154 ("9:11", "23:28"), ("28:30", "23:28"),
155 ],
156}
157
158
159# -----------------------------------------------------------------------
161 if isinstance(hist, (ROOT.TH1D, ROOT.TH1I, ROOT.TProfile, ROOT.TH1F)):
162 instance = "TH1"
163 elif isinstance(hist, (ROOT.TH2D, ROOT.TH2I, ROOT.TH2F, ROOT.TProfile2D)):
164 instance = "TH2"
165 elif isinstance(hist, (ROOT.TGraph, ROOT.TGraphErrors)):
166 instance = "TGraph"
167 elif isinstance(hist, ROOT.TEfficiency):
168 instance = "TEfficiency"
169
170 return instance
171
172# -----------------------------------------------------------------------
173def setTitleOffset(hist, instance=""):
174 if instance == "":
175 instance = classifyInstance(hist)
176
177 if instance == "TEfficiency":
178 hist.GetPaintedGraph().GetXaxis().SetTitleOffset(xTitleOffset[instance])
179 hist.GetPaintedGraph().GetYaxis().SetTitleOffset(yTitleOffset[instance])
180 else:
181 hist.GetXaxis().SetTitleOffset(xTitleOffset[instance])
182 hist.GetYaxis().SetTitleOffset(yTitleOffset[instance])
183 if instance == "TH2":
184 hist.GetZaxis().SetTitleOffset(zTitleOffset[instance])
185
186
187# -----------------------------------------------------------------------
189 y_min = 0.
190 y_max = 32.
191 dashlines_x = [-18., -16., -14., -12., -10., -9., -7., -5., -4., -2., 2., 4., 5., 7., 9., 10., 12., 14., 16., 18.]
192 solidlines_x = [-17., -15., -13., -11., -8., -6., -3., -1., 1., 3., 6., 8., 11., 13., 15., 17.]
193
194 for i_x in dashlines_x:
195 dash_line = ROOT.TLine(i_x, y_min, i_x, y_max)
196 dash_line.SetLineStyle(2)
197 dash_line.SetLineWidth(1)
198 dash_line.SetLineColor(ROOT.kBlack)
199
200 hist2d.GetListOfFunctions().Add(dash_line)
201
202 for i_x in solidlines_x:
203 solid_line = ROOT.TLine(i_x, y_min, i_x, y_max)
204 solid_line.SetLineStyle(1)
205 solid_line.SetLineWidth(1)
206 solid_line.SetLineColor(ROOT.kBlack)
207
208 hist2d.GetListOfFunctions().Add(solid_line)
209
210# -----------------------------------------------------------------------
211def set_horizonGrid(hist2d):
212 xmin = -19.
213 xmax = 19.
214 for i_sec in range(1, 16+1):
215 solid_line = ROOT.TLine(xmin, i_sec*2, xmax, i_sec*2)
216 solid_line.SetLineStyle(1)
217 solid_line.SetLineWidth(1)
218 solid_line.SetLineColor(ROOT.kBlack)
219 hist2d.GetListOfFunctions().Add(solid_line)
220
221 dash_line = ROOT.TLine(xmin, i_sec*2-1, xmax, i_sec*2-1)
222 dash_line .SetLineStyle(2)
223 dash_line .SetLineWidth(1)
224 dash_line .SetLineColor(ROOT.kBlack)
225 hist2d.GetListOfFunctions().Add(dash_line)
226
227# -----------------------------------------------------------------------
228def set_blankBox(hist_2d, layer):
229 if not (layer in list_boxes_layers):
230 print("HistDecorate::set_blankBox::ERROR - unknown layer index %d"%layer)
231 return
232
233 list_boxes = list_boxes_layers[layer]
234 for box_str in list_boxes:
235 box_pos = findBoxPos(hist_2d, box_str[0], box_str[1])
236
237 tbox =ROOT.TBox(box_pos[0], box_pos[1], box_pos[2], box_pos[3])
238 tbox.SetFillColor(ROOT.kGray)
239
240 hist_2d.GetListOfFunctions().Add(tbox)
241
242# -----------------------------------------------------------------------
243def findBoxPos(h2, binx_str, biny_str):
244 bins_X = [int(i) for i in binx_str.split(":")]
245 bins_Y = [int(i) for i in biny_str.split(":")]
246
247 Xpos = findAxisPos(h2, bins_X, axis = 1)
248 Ypos = findAxisPos(h2, bins_Y, axis = 2)
249
250 return (Xpos[0], Ypos[0], Xpos[1], Ypos[1])
251
252# -----------------------------------------------------------------------
253def findAxisPos(h2, bins, axis = 1):
254 low_bin = 0
255 up_bin = 0
256 if len(bins) == 2:
257 low_bin = bins[0]
258 up_bin = bins[1]
259 else:
260 low_bin = bins[0]
261 up_bin = bins[0]
262
263 if axis == 1:
264 pos_1 = h2.GetXaxis().GetBinLowEdge(low_bin)
265 pos_2 = h2.GetXaxis().GetBinUpEdge(up_bin)
266 elif axis == 2:
267 pos_1 = h2.GetYaxis().GetBinLowEdge(low_bin)
268 pos_2 = h2.GetYaxis().GetBinUpEdge(up_bin)
269 else:
270 print("HistDecorate::findAxisPos::ERROR - the axis index can only be 1 or 2")
271
272 return (pos_1, pos_2)
273
274
276 x_axis = hist2d.GetXaxis()
277 y_axis = hist2d.GetYaxis()
278
279 XBins_Labeled = { # bin : eta station
280 1 : '-8',
281 3 : '-7',
282 5 : '-6',
283 7 : '-5',
284 10: '-4',
285 12: '-3',
286 15: '-2',
287 17: '-1',
288 19: '0',
289 21: '1',
290 24: '2',
291 26: '3',
292 29: '4',
293 32: '5',
294 34: '6',
295 36: '7',
296 38: '8'
297 }
298
299 YBins_Labeled = { # bin : Sector name
300 1:"L1",
301 2:"S2",
302 3:"L3",
303 4:"S4",
304 5:"L5",
305 6:"S6",
306 7:"L7",
307 8:"S8",
308 9:"L9",
309 10:"S10",
310 11:"L11",
311 12:"FG12",
312 13:"L13",
313 14:"FG14",
314 15:"L15",
315 16:"S16"
316 }
317
318 for i_bin, i_label in XBins_Labeled.items():
319 x_axis.SetBinLabel(i_bin, i_label)
320
321 for i_bin, i_label in YBins_Labeled.items():
322 y_axis.SetBinLabel(i_bin*2-1, i_label)
323
324 hist2d.GetXaxis().SetTitleOffset(0.9)
325 hist2d.GetYaxis().SetTitleOffset(1.0)
326 hist2d.GetZaxis().SetTitleOffset(1.2)
327
328
329if __name__ == '__main__':
330 print ("HistDecorate: Hello, World !")
void print(char *figname, TCanvas *c1)
findAxisPos(h2, bins, axis=1)
classifyInstance(hist)
set_verticalGrid(hist2d)
set_blankBox(hist_2d, layer)
setAxisLabel_EtaPhi(hist2d)
set_horizonGrid(hist2d)
findBoxPos(h2, binx_str, biny_str)
setTitleOffset(hist, instance="")