Belle II Software  release-05-02-19
utils.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 """\
5 Utilities for PXD calibration and performance study
6 Author: qingyuan.liu@desy.de
7 """
8 
9 import ROOT
10 from ROOT import Belle2
11 
12 
13 # Helper functions for Belle2.VxdID
14 def get_name(self, separator="_"):
15  return f"{self.getLayerNumber()}{separator}{self.getLadderNumber()}{separator}{self.getSensorNumber()}"
16 
17 
18 def get_pxdid(self):
19  return self.getLayerNumber() * 1000 + self.getLadderNumber() * 10 + self.getSensorNumber()
20 
21 
22 # Helper function for TGraph
23 def graph_append(self, x, y):
24  self.SetPoint(self.GetN(), x, y)
25 
26 
27 # Extend Belle2.VxdID
28 Belle2.VxdID.get_name = get_name
29 Belle2.VxdID.get_pxdid = get_pxdid
30 # Extend ROOT classes
31 ROOT.TGraph.append = graph_append
32 
33 # Sensor id list
34 sensor_labels = [
35  "1.1.1", "1.1.2",
36  "1.2.1", "1.2.2",
37  "1.3.1", "1.3.2",
38  "1.4.1", "1.4.2",
39  "1.5.1", "1.5.2",
40  "1.6.1", "1.6.2",
41  "1.7.1", "1.7.2",
42  "1.8.1", "1.8.2",
43  "2.4.1", "2.4.2",
44  "2.5.1", "2.5.2"]
45 sensorID_list = tuple([Belle2.VxdID(label) for label in sensor_labels])
46 nsensors = len(sensorID_list)
47 
48 # Sensor matrix
49 nUCells = 250
50 nVCells = 768
51 nPixels = 192000
52 
53 # Colors and styles for ROOT
54 colors = tuple([
55  ROOT.kRed + 1, ROOT.kOrange + 1, ROOT.kYellow - 3, ROOT.kSpring + 5, ROOT.kGreen + 3,
56  ROOT.kCyan - 6, ROOT.kAzure - 5, ROOT.kAzure - 6, ROOT.kBlue + 3, ROOT.kViolet - 1])
57 
58 plot_configs = {
59  "LineWidth": 4,
60  "yTitleOffset": 0.9
61 }
62 
63 
64 def get_sensor_graphs(ytitle=""):
65  """
66  Create TGraphs and related TLegends
67  Parameters:
68  ytitle (str): Title of the y-axis
69  Return:
70  A dictionary using sensorID as key and TGraph as value. A special key "TLegends"
71  is used for the list of TLegend objects for drawing.
72  """
73  graphs = {}
74  legs = []
75  # Create TLegends
76  xlegl = [0.60, 0.57, 0.78, 0.75]
77  xlegr = [0.72, 0.65, 0.90, 0.83]
78  for i in range(4):
79  """
80  Display format (content in a TLegend is bracketed):
81  (marker1 /) (marker2 label1/label2) (marker3 /) (markter4 label3/label4)
82  """
83  legs.append(ROOT.TLegend(xlegl[i], 0.62, xlegr[i], 0.85))
84  legs[i].SetFillStyle(0)
85  legs[i].SetBorderSize(0)
86  legs[i].SetTextFont(43)
87  legs[i].SetTextSize(18)
88  # Create TGraphs and set TLegend for each sensor
89  for i, sensorID in enumerate(sensorID_list):
90  agraph = ROOT.TGraph()
91  # agraph.SetTitle(sensorID.get_name())
92  agraph.SetName(sensorID.get_name())
93  agraph.GetXaxis().SetTitle('run #')
94  agraph.GetYaxis().SetTitle(ytitle)
95  agraph.GetYaxis().SetTitleOffset(0.9)
96  agraph.SetLineColor(colors[int(i / 2)])
97  agraph.SetLineWidth(4)
98  agraph.SetMarkerColor(colors[int(i / 2)])
99  agraph.SetMarkerStyle(26 if i % 2 else 24)
100  graphs[sensorID.getID()] = agraph
101  # Set TLegend
102  i1 = 0
103  i2 = 1
104  if (i > 9):
105  i1 = 2
106  i2 = 3
107  if (i % 2):
108  legs[i1].AddEntry(agraph, sensor_labels[i - 1] + ' / ' + sensor_labels[i], 'p')
109  else:
110  legs[i2].AddEntry(agraph, ' / ', 'p')
111  graphs["TLegends"] = legs
112 
113  return graphs
114 
115 
116 def get_sensor_maps(
117  name="MaskedPixels", title="Masked Pixels", ztitle="isMasked", run=0,
118  nUCells=nUCells, nVCells=nVCells, sensorID_list=sensorID_list
119 ):
120  """
121  Create TH2F objects for saving pixel map
122  Parameters:
123  name (str): Name of a histogram
124  title (str): Title
125  ztitle (str): Label for z-axis
126  run (int): run number of the map, to be appended into the title
127  sensorID_list (list): List of sensorID objects for which histograms are created.
128  Return:
129  A dictionary using sensorID as key and TH2F as value.
130  """
131  hists = {}
132  for sensorID in sensorID_list:
133  hname = name + "_{}_run_{:d}".format(sensorID.get_name(), run)
134  htitle = title + " Sensor={:d} Run={:d}".format(sensorID.get_pxdid(), run)
135  h2 = ROOT.TH2F(hname, htitle, nUCells, 0, nUCells, nVCells, 0, nVCells)
136  h2.GetXaxis().SetTitle("uCell")
137  h2.GetYaxis().SetTitle("vCell")
138  h2.GetZaxis().SetTitle(ztitle)
139  h2.SetStats(0)
140  hists[sensorID.getID()] = h2
141  return hists
142 
143 
144 # latex
145 latex_l = ROOT.TLatex() # left aligned
146 latex_l.SetTextFont(43)
147 latex_l.SetNDC()
148 latex_l.SetTextSize(24)
149 latex_ls = ROOT.TLatex() # left aligned small
150 latex_ls.SetTextFont(43)
151 latex_ls.SetNDC()
152 latex_ls.SetTextSize(19)
153 latex_r = ROOT.TLatex() # right aligned
154 latex_r.SetTextFont(43)
155 latex_r.SetNDC()
156 latex_r.SetTextSize(24)
157 latex_r.SetTextAlign(31)
158 
159 
160 def style():
161  """
162  ROOT style
163  Author: maiko.takahashi@desy.de
164  """
165  # root style option
166 
167  # canvas
168  ROOT.gStyle.SetCanvasBorderMode(0)
169  ROOT.gStyle.SetCanvasBorderSize(10)
170  ROOT.gStyle.SetCanvasColor(0)
171  ROOT.gStyle.SetCanvasDefH(450)
172  ROOT.gStyle.SetCanvasDefW(500)
173  ROOT.gStyle.SetCanvasDefX(10)
174  ROOT.gStyle.SetCanvasDefY(10)
175  # pad
176  ROOT.gStyle.SetPadBorderMode(0)
177  ROOT.gStyle.SetPadBorderSize(10)
178  ROOT.gStyle.SetPadColor(0)
179  ROOT.gStyle.SetPadBottomMargin(0.16)
180  ROOT.gStyle.SetPadTopMargin(0.10)
181  ROOT.gStyle.SetPadLeftMargin(0.17)
182  ROOT.gStyle.SetPadRightMargin(0.19)
183  ROOT.gStyle.SetPadGridX(1)
184  ROOT.gStyle.SetPadGridY(1)
185  ROOT.gStyle.SetGridStyle(2)
186  ROOT.gStyle.SetGridColor(ROOT.kGray + 1)
187  # frame
188  ROOT.gStyle.SetFrameFillStyle(0)
189  ROOT.gStyle.SetFrameFillColor(0)
190  ROOT.gStyle.SetFrameLineColor(1)
191  ROOT.gStyle.SetFrameLineStyle(0)
192  ROOT.gStyle.SetFrameLineWidth(2)
193  ROOT.gStyle.SetFrameBorderMode(0)
194  ROOT.gStyle.SetFrameBorderSize(10)
195  # axis
196  ROOT.gStyle.SetLabelFont(42, "xyz")
197  ROOT.gStyle.SetLabelOffset(0.015, "xyz")
198  ROOT.gStyle.SetLabelSize(0.048, "xyz")
199  ROOT.gStyle.SetNdivisions(505, "xyz")
200  ROOT.gStyle.SetTitleFont(42, "xyz")
201  ROOT.gStyle.SetTitleSize(0.050, "xyz")
202  ROOT.gStyle.SetTitleOffset(1.3, "x")
203  ROOT.gStyle.SetTitleOffset(1.2, "y")
204  ROOT.gStyle.SetTitleOffset(1.4, "z")
205  ROOT.gStyle.SetPadTickX(1)
206  ROOT.gStyle.SetPadTickY(1)
207  # title
208  ROOT.gStyle.SetTitleBorderSize(0)
209  ROOT.gStyle.SetTitleFillColor(10)
210  ROOT.gStyle.SetTitleAlign(12)
211  ROOT.gStyle.SetTitleFontSize(0.045)
212  ROOT.gStyle.SetTitleX(0.560)
213  ROOT.gStyle.SetTitleY(0.860)
214  ROOT.gStyle.SetTitleFont(42, "")
215  # stat
216  ROOT.gStyle.SetStatBorderSize(0)
217  ROOT.gStyle.SetStatColor(10)
218  ROOT.gStyle.SetStatFont(42)
219  ROOT.gStyle.SetStatX(0.94)
220  ROOT.gStyle.SetStatY(0.91)
221  # histo style
222  ROOT.gStyle.SetOptTitle(0)
223  ROOT.gStyle.SetOptStat(0)
224  ROOT.gStyle.SetHistLineWidth(3)
225  ROOT.TH2.SetDefaultSumw2()
226 
227 
228 if __name__ == '__main__':
229  style()
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43