Belle II Software  release-08-01-10
dqm.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 from array import array
12 from ROOT import TCanvas, TF1, TFile
13 from ROOT import TGraph, TH2D, TLine
14 from ROOT import kFullCircle
15 import argparse
16 
17 nWires = [160, 160, 160, 160, 160, 160, 160, 160,
18  160, 160, 160, 160, 160, 160,
19  192, 192, 192, 192, 192, 192,
20  224, 224, 224, 224, 224, 224,
21  256, 256, 256, 256, 256, 256,
22  288, 288, 288, 288, 288, 288,
23  320, 320, 320, 320, 320, 320,
24  352, 352, 352, 352, 352, 352,
25  384, 384, 384, 384, 384, 384]
26 
27 nWiresSum = []
28 sum = 0
29 for w in nWires:
30  sum = sum + w
31  nWiresSum.append(sum)
32 
33 nWiresSL = [160 * 8, 160 * 6, 192 * 6, 224 * 6, 256 * 6, 288 * 6,
34  320 * 6, 352 * 6, 384 * 6]
35 
36 rfClock = 1.017774
37 refT0 = 4905.0 * rfClock
38 refMPV = 80
39 
40 
41 class DQM():
42 
43  """
44  DQM class.
45  """
46 
47  def __init__(self, fname='output.root'):
48  """
49  call constructor of base class, required.
50  """
51 
52  super(DQM, self).__init__()
53 
54 
55  self.ff = TFile(fname)
56 
57  self.typetype = 'adc'
58 
59  self.histADChistADC = [self.ff.Get('h' + str(100000 + i))
60  for i in range(14336)]
61 
62  self.histTDChistTDC = [self.ff.Get('h' + str(200000 + i))
63  for i in range(14336)]
64 
65  self.histHithistHit = [self.ff.Get('h' + str(400000 + i))
66  for i in range(56)]
67 
68  self.lineslines = []
69  for h in self.histTDChistTDC:
70  y = h.GetMaximum()
71  line = TLine(refT0, 0, refT0, y)
72  line.SetLineColor(2)
73  self.lineslines.append(line)
74 
75 
76  self.lineMPVslineMPVs = []
77  for h in self.histADChistADC:
78  y = h.GetMaximum()
79  line = TLine(refMPV, 0, refMPV, y)
80  line.SetLineColor(2)
81  self.lineMPVslineMPVs.append(line)
82  # self.histADCinLayer = [self.f.Get('h' + str(500000 + i))
83  # for i in range(56)]
84 
85 
86  self.sigsig = TF1("sig", "landau", 0, 200)
87  # self.sig = TF1("sig", "landau+expo(3)", 0, 200)
88  # self.sig.SetParameters(300.0,60.0,20.0,1000,-1.0)
89 
90  self.ft0ft0 = TF1("ft0", "[0]+[1]*(exp([2]*([3]-x))/(1+exp(-([4]-x)/[5])))", 2000, 4000)
91  self.ft0ft0.SetParameters(0, 10, 0, 0, 3800, 3.)
92  self.ft0ft0.FixParameter(0, 0.)
93 
94  self.m_fitStatusm_fitStatus = False
95 
96  self.canvascanvas = TCanvas("canvas", "canvas", 800, 800)
97 
98  self.hidhid = 0
99 
100  self.ndivndiv = 1
101 
102  self.parpar = [-1.0 for i in range(14336)]
103 
104  self.parADCinLayerparADCinLayer = [-1.0 for i in range(56)]
105 
106  self.parT0parT0 = [0.0 for i in range(14336)]
107 
108  self.h2h2 = TH2D("h2d", "MPV of all ADCs", 15000,
109  -500, 14500, 100, 0, 200)
110 
111  self.h2h2.SetStats(0)
112 
113  self.h2h2.GetXaxis().SetTitle('Cell ID')
114 
115  self.h2h2.GetYaxis().SetTitle('MPV')
116 
117  self.h2ADCvsLayerh2ADCvsLayer = TH2D("h2dl", "MPV for every Layer", 56,
118  0, 56, 100, 0, 200)
119 
120  self.h2ADCvsLayerh2ADCvsLayer.SetStats(0)
121 
122  self.h2ADCvsLayerh2ADCvsLayer.GetXaxis().SetTitle('Layer')
123 
124  self.h2ADCvsLayerh2ADCvsLayer.GetYaxis().SetTitle('MPV (adc count)')
125 
126  self.xx = array("d", [i for i in range(14336)])
127 
128  self.l_arrayl_array = array("d", [i for i in range(56)])
129 
130  self.graphgraph = TGraph(len(self.xx))
131 
132  self.graphgraph.SetMarkerStyle(kFullCircle)
133 
134  self.graphgraph.SetMarkerSize(0.3)
135 
136 
137  self.graph2graph2 = TGraph(len(self.l_arrayl_array))
138 
139  self.graph2graph2.SetMarkerStyle(kFullCircle)
140 
141  self.graph2graph2.SetMarkerSize(1.0)
142 
143  self.lineline = [TLine(0, 0, 0, 200)]
144 
145  x0 = 0
146  for i in range(9):
147  x0 = x0 + nWiresSL[i]
148  line = TLine(x0, 0, x0, 200)
149  self.lineline.append(line)
150  for l_i in self.lineline:
151  l_i.SetLineColorAlpha(8, 0.5)
152  l_i.SetLineStyle(2)
153 
154  def fitADC(self, xmin=8, xmax=200):
155  """
156  Fit all ADC histgrams.
157  """
158  if self.m_fitStatusm_fitStatus is True:
159  print("Fitting for ADC has been already applied.")
160  else:
161 
162  for (i, h) in enumerate(self.histADChistADC):
163  if h.GetEntries() > 0:
164  h.Fit(self.sigsig, "Q", "", xmin, xmax)
165  self.parpar[i] = self.sigsig.GetParameter(1)
166  self.m_fitStatusm_fitStatus = True
167 
168  def getHistID(self, lay=0, wire=0):
169  """
170  Get Hist ID from (layer, wire) ID.
171  Return values : hist ID (cell ID)
172  """
173  hid = nWiresSum[lay - 1] + wire if lay > 0 else wire
174  return hid
175 
176  def getLayerWireID(self, hid=0):
177  """
178  Get (layer, wire) ID from cell ID.
179  Return values : [layer, wire]
180  """
181  if hid < nWiresSum[0]:
182  return [0, hid]
183  else:
184  layer = 0
185  wire = 0
186  for (i, wsum) in enumerate(nWiresSum):
187  diff = hid - wsum
188  if diff > 0 and diff < nWires[i]:
189  layer = i + 1
190  wire = diff
191  break
192  return [layer, wire]
193 
194  def drawADC(self, lay=0, w=0):
195  """
196  Draw ADC histgrams w.r.t (lay,wire).
197  """
198  i = self.getHistIDgetHistID(lay, w)
199  for j in range(self.ndivndiv):
200  self.hidhid = i + j
201  self.canvascanvas.cd(j + 1)
202  self.histADChistADC[self.hidhid].Draw()
203  self.lineMPVslineMPVs[self.hidhid].Draw()
204 
205  self.typetype = 'adc'
206  self.canvascanvas.Update()
207 
208  def drawTDC(self, lay=0, w=0):
209  """
210  Draw TDC histgrams w.r.t (lay,wire).
211  """
212  i = self.getHistIDgetHistID(lay, w)
213  for j in range(self.ndivndiv):
214  self.hidhid = i + j
215  self.canvascanvas.cd(j + 1)
216  self.histTDChistTDC[self.hidhid].Draw()
217  self.lineslines[self.hidhid].Draw()
218 
219  self.canvascanvas.Update()
220  self.typetype = 'tdc'
221 
222  def drawHit(self, lay=0):
223  """
224  Draw Hit histgrams w.r.t layer ID.
225  """
226  for j in range(self.ndivndiv):
227  self.hidhid = lay + j
228  if self.hidhid > 55:
229  break
230  self.canvascanvas.cd(j + 1)
231  self.histHithistHit[self.hidhid].Draw()
232  self.canvascanvas.Update()
233  self.typetype = 'hit'
234 
235  def drawTDCByCell(self, i):
236  """
237  Draw TDC histgrams w.r.t cell ID (0-14336).
238  """
239  for j in range(self.ndivndiv):
240  self.hidhid = i + j
241  self.canvascanvas.cd(j + 1)
242  self.histTDChistTDC[self.hidhid].Draw()
243  self.lineslines[self.hidhid].Draw()
244 
245  self.typetype = 'tdc'
246  self.canvascanvas.Update()
247 
248  def drawADCByCell(self, i):
249  """
250  Draw ADC histgrams w.r.t cell ID (0-14336).
251  """
252  for j in range(self.ndivndiv):
253  self.hidhid = i + j
254  self.canvascanvas.cd(j + 1)
255  self.histADChistADC[self.hidhid].Draw()
256  self.lineMPVslineMPVs[self.hidhid].Draw()
257 
258  self.typetype = 'adc'
259  self.canvascanvas.Update()
260 
261  def next(self):
262  """
263  Show next plots.
264  """
265  self.hidhid = self.hidhid + 1
266  if self.typetype == 'adc':
267  self.canvascanvas.Clear('d')
268  self.drawADCByCelldrawADCByCell(self.hidhid)
269  elif self.typetype == 'tdc':
270  self.canvascanvas.Clear('d')
271  self.drawTDCByCelldrawTDCByCell(self.hidhid)
272  elif self.typetype == 'hit':
273  self.canvascanvas.Clear('d')
274  self.drawHitdrawHit(self.hidhid)
275  else:
276  print('Undefined type: ' + type)
277 
278  def div(self, i=1, j=1):
279  """
280  Divide Tcanvas by (i,j).
281  """
282  self.canvascanvas.Clear()
283  self.canvascanvas.Divide(i, j)
284  self.ndivndiv = i * j
285 
286  def mpv(self):
287  """
288  Show the MPV of ADC distribution
289  for all wires.
290  """
291  self.canvascanvas.Clear()
292 
293  for (i, p) in enumerate(self.parpar):
294  self.graphgraph.SetPoint(i, self.xx[i], p)
295 
296  self.h2h2.Draw()
297  for line in self.lineline:
298  line.Draw()
299  self.graphgraph.Draw("P")
300  self.canvascanvas.Update()
301 
302  def xrange(self, xmin=-500, xmax=14500):
303  """
304  Set x range of MPV plot.
305  """
306  self.h2h2.GetXaxis().SetRangeUser(xmin, xmax)
307 
308  def xrangeHit(self, xmin=0, xmax=350):
309  """
310  Set x range of Hit distributions.
311  """
312  for h in self.histHithistHit:
313  h.GetXaxis().SetRangeUser(xmin, xmax)
314 
315  def xrangeTDC(self, xmin=0, xmax=4000):
316  """
317  Set x range of TDC distributions.
318  """
319  for h in self.histTDChistTDC:
320  h.GetXaxis().SetRangeUser(xmin, xmax)
321 
322  def xrangeADC(self, xmin=0, xmax=200):
323  """
324  Set x range of ADC distributions.
325  """
326  for h in self.histADChistADC:
327  h.GetXaxis().SetRangeUser(xmin, xmax)
328 
329  def print(self, fname='test.png'):
330  """
331  Print the current plot.
332  """
333  self.canvascanvas.Print(fname)
334 
335  def ne(self):
336  """
337  Alias to next()
338  """
339  self.nextnext()
340 
341  def dh(self, lay):
342  """
343  Alias to drawHit()
344  """
345  self.drawHitdrawHit(lay)
346 
347  def da(self, lay, w):
348  """
349  Alias to drawADC()
350  """
351  self.drawADCdrawADC(lay, w)
352 
353  def dt(self, lay, w):
354  """
355  Alias to drawTDC()
356  """
357  self.drawTDCdrawTDC(lay, w)
358 
359 
360 if __name__ == "__main__":
361  # Make the parameters accessible form the outside.
362 
363  parser = argparse.ArgumentParser()
364  parser.add_argument('input', help='Input file to be processed (unpacked CDC data).')
365  args = parser.parse_args()
366  dqm = DQM(args.input)
Definition: dqm.py:41
def print(self, fname='test.png')
Definition: dqm.py:329
def div(self, i=1, j=1)
Definition: dqm.py:278
lines
Line for guide of TDC T0.
Definition: dqm.py:68
def xrangeADC(self, xmin=0, xmax=200)
Definition: dqm.py:322
def dh(self, lay)
Definition: dqm.py:341
def xrange(self, xmin=-500, xmax=14500)
Definition: dqm.py:302
def drawHit(self, lay=0)
Definition: dqm.py:222
line
Marker style.
Definition: dqm.py:143
def fitADC(self, xmin=8, xmax=200)
Definition: dqm.py:154
def da(self, lay, w)
Definition: dqm.py:347
def xrangeTDC(self, xmin=0, xmax=4000)
Definition: dqm.py:315
f
input file name
Definition: dqm.py:55
def dt(self, lay, w)
Definition: dqm.py:353
parT0
T0 values.
Definition: dqm.py:106
histHit
Hit distribution histograms.
Definition: dqm.py:65
histADC
ADC histograms.
Definition: dqm.py:59
m_fitStatus
Fit status True if fitting has been finished.
Definition: dqm.py:94
type
plot type (adc/tdc/hit)
Definition: dqm.py:57
h2ADCvsLayer
Set stats.
Definition: dqm.py:117
histTDC
TDC histograms.
Definition: dqm.py:62
par
MPV values.
Definition: dqm.py:102
graph
TGraph.
Definition: dqm.py:130
x
Set stats.
Definition: dqm.py:126
def getHistID(self, lay=0, wire=0)
Definition: dqm.py:168
sig
Fitting function for ADC histogram.
Definition: dqm.py:86
def next(self)
Definition: dqm.py:261
def ne(self)
Definition: dqm.py:335
hid
histogram ID
Definition: dqm.py:98
graph2
Marker style.
Definition: dqm.py:137
ft0
Fitting function for TDC histogram.
Definition: dqm.py:90
def mpv(self)
Definition: dqm.py:286
def drawTDCByCell(self, i)
Definition: dqm.py:235
def xrangeHit(self, xmin=0, xmax=350)
Definition: dqm.py:308
lineMPVs
Line for guide of ADC MPV.
Definition: dqm.py:76
def drawTDC(self, lay=0, w=0)
Definition: dqm.py:208
h2
histogram
Definition: dqm.py:108
def __init__(self, fname='output.root')
Definition: dqm.py:47
parADCinLayer
MPV values w.r.t.
Definition: dqm.py:104
ndiv
Number of division for canvas.
Definition: dqm.py:100
def drawADCByCell(self, i)
Definition: dqm.py:248
def drawADC(self, lay=0, w=0)
Definition: dqm.py:194
canvas
canvas
Definition: dqm.py:96
def getLayerWireID(self, hid=0)
Definition: dqm.py:176
l_array
array layer
Definition: dqm.py:128