Belle II Software  release-08-01-10
CoGCalibration_utils_checkCalibration.py
1 # !/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 #
13 # util module to check SVD CoG Calibration stored in a localDB
14 #
15 # called by SVDCoGTimeCalibrationCheck script in
16 # svd/scripts/caf/SVDCoGTimeCalibrationCheck.py
17 #
18 
19 
20 import basf2 as b2
21 from ROOT import (Belle2, TFile, TFitResultPtr, TH1F, TH2D, TTree, TF1,
22  gDirectory, gROOT)
23 # import numpy
24 import math
25 
26 import numpy as np
27 
28 cdc_Time0 = "EventT0"
29 svd_Clusters = "SVDClustersFromTracks"
30 svd_EventInfo = "SVDEventInfo"
31 # svd_EventMD = "EventMetaData"
32 
33 gROOT.SetBatch(True)
34 
35 # mode = True
36 
37 
39  """
40  Python class used for checking SVD CoG Calibration stored in a localDB
41  """
42 
43  def fillLists(self, svdClusterFromTracks):
44  """
45  Function that fill the lists needed for the check of the calibration
46 
47  parameters:
48  svdClusterFromTracks (SVDCluster): Cluster related to tracks
49  """
50  timeCluster = svdClusterFromTracks.getClsTime()
51  snrCluster = svdClusterFromTracks.getSNR()
52  sizeCluster = svdClusterFromTracks.getSize()
53  chargeCluster = svdClusterFromTracks.getCharge()
54  layerCluster = svdClusterFromTracks.getSensorID().getLayerNumber()
55  layerIndex = layerCluster - 3
56  sensorCluster = svdClusterFromTracks.getSensorID().getSensorNumber()
57  sensorIndex = sensorCluster - 1
58  ladderCluster = svdClusterFromTracks.getSensorID().getLadderNumber()
59  ladderIndex = ladderCluster - 1
60  sideCluster = svdClusterFromTracks.isUCluster()
61  sideIndex = 0
62  if sideCluster:
63  sideIndex = 1
64 
65  hasTimezero = self.cdcEventT0cdcEventT0.hasEventT0()
66  if hasTimezero:
67  tZero = self.cdcEventT0cdcEventT0.getEventT0()
68  tZeroError = self.cdcEventT0cdcEventT0.getEventT0Uncertainty()
69 
70  # filling histograms
71  resHist = self.resListresList[layerIndex][ladderIndex][sensorIndex][sideIndex]
72  resHist.Fill(timeCluster - tZero)
73  spHist = self.spListspList[layerIndex][ladderIndex][sensorIndex][sideIndex]
74  spHist.Fill(timeCluster, tZero)
75  cogHist = self.cogListcogList[layerIndex][ladderIndex][sensorIndex][sideIndex]
76  cogHist.Fill(timeCluster)
77  cdcHist = self.cdcListcdcList[layerIndex][ladderIndex][sensorIndex][sideIndex]
78  cdcHist.Fill(tZero)
79  snrHist = self.snrListsnrList[layerIndex][ladderIndex][sensorIndex][sideIndex]
80  snrHist.Fill(snrCluster)
81  sizeHist = self.sizeListsizeList[layerIndex][ladderIndex][sensorIndex][sideIndex]
82  sizeHist.Fill(sizeCluster)
83  chargeHist = self.chargeListchargeList[layerIndex][ladderIndex][sensorIndex][sideIndex]
84  chargeHist.Fill(chargeCluster/1000.)
85  cdcErrorHist = self.cdcErrorListcdcErrorList[layerIndex][ladderIndex][sensorIndex][sideIndex]
86  cdcErrorHist.Fill(tZeroError)
87 
88 
89  self.NTOTNTOT = self.NTOTNTOT + 1
90 
91  def set_localdb(self, localDB):
92  """
93  Function that allows to set the localDB
94 
95  parameters:
96  localDB (str): Name of the localDB used
97  """
98 
99  self.localdblocaldb = localDB
100 
101  def set_run_number(self, run):
102  """
103  Function that allows to save the run number
104 
105  parameters:
106  run (int): run number
107  """
108 
109  self.runnumberrunnumber = run
110 
111  def set_exp_number(self, exp):
112  """
113  Function that allows to save the experiment number
114 
115  parameters:
116  exp (int): experiment number
117  """
118 
119  self.expnumberexpnumber = exp
120 
121  def initialize(self):
122  """
123  Initialize object (histograms, lists, ...) used by the class
124  """
125 
126 
127  self.outputFileNameoutputFileName = "../caf/tree/SVDCoGCalibrationCheck_Exp" + \
128  str(self.expnumberexpnumber) + "_Run" + str(self.runnumberrunnumber) + ".root"
129 
131  self.resListresList = []
132 
133  self.spListspList = []
134 
135  self.cogListcogList = []
136 
137  self.cdcListcdcList = []
138 
139  self.snrListsnrList = []
140 
141  self.sizeListsizeList = []
142 
143  self.chargeListchargeList = []
144 
145  self.cdcErrorListcdcErrorList = []
146 
148 
149 
150  self.EvtEvt = 0
151  for layer in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
152  layerList0 = []
153  layerList1 = []
154  layerList2 = []
155  layerList3 = []
156  layerList4 = []
157  layerList5 = []
158  layerList6 = []
159  layerList7 = []
160 
161  self.resListresList.append(layerList0)
162  self.spListspList.append(layerList1)
163  self.cogListcogList.append(layerList2)
164  self.cdcListcdcList.append(layerList3)
165  self.snrListsnrList.append(layerList4)
166  self.sizeListsizeList.append(layerList5)
167  self.chargeListchargeList.append(layerList6)
168  self.cdcErrorListcdcErrorList.append(layerList7)
169 
170  for ladder in geoCache.getLadders(layer):
171  ladderList0 = []
172  ladderList1 = []
173  ladderList2 = []
174  ladderList3 = []
175  ladderList4 = []
176  ladderList5 = []
177  ladderList6 = []
178  ladderList7 = []
179 
180  layerList0.append(ladderList0)
181  layerList1.append(ladderList1)
182  layerList2.append(ladderList2)
183  layerList3.append(ladderList3)
184  layerList4.append(ladderList4)
185  layerList5.append(ladderList5)
186  layerList6.append(ladderList6)
187  layerList7.append(ladderList7)
188 
189  for sensor in geoCache.getSensors(ladder):
190  sensorList0 = []
191  sensorList1 = []
192  sensorList2 = []
193  sensorList3 = []
194  sensorList4 = []
195  sensorList5 = []
196  sensorList6 = []
197  sensorList7 = []
198 
199  ladderList0.append(sensorList0)
200  ladderList1.append(sensorList1)
201  ladderList2.append(sensorList2)
202  ladderList3.append(sensorList3)
203  ladderList4.append(sensorList4)
204  ladderList5.append(sensorList5)
205  ladderList6.append(sensorList6)
206  ladderList7.append(sensorList7)
207 
208  for i in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
209  layerN = i.getLayerNumber()
210  li = layerN - 3
211  for j in geoCache.getLadders(i):
212  ladderN = j.getLadderNumber()
213  ldi = ladderN - 1
214  for k in geoCache.getSensors(j):
215  sensorN = k.getSensorNumber()
216  si = sensorN - 1
217  for s in range(2):
218  self.resListresList[li][ldi][si].append(TH1F("res" + "_" + str(layerN) + "." +
219  str(ladderN) + "." + str(sensorN) + "." +
220  str(s), " ", 200, -100, 100))
221  self.spListspList[li][ldi][si].append(TH2D("sp" + "_" + str(layerN) + "." +
222  str(ladderN) + "." + str(sensorN) + "." +
223  str(s), " ", 300, -150, 150, 300, -150, 150))
224  self.cogListcogList[li][ldi][si].append(TH1F("cog" + "_" + str(layerN) + "." +
225  str(ladderN) + "." + str(sensorN) + "." + str(s),
226  " ", 200, -100, 100))
227  self.cdcListcdcList[li][ldi][si].append(TH1F("cdc" + "_" + str(layerN) + "." +
228  str(ladderN) + "." + str(sensorN) + "." + str(s),
229  " ", 200, -100, 100))
230  self.snrListsnrList[li][ldi][si].append(TH1F("snr" + "_" + str(layerN) + "." +
231  str(ladderN) + "." + str(sensorN) + "." + str(s), " ", 100, 0, 100))
232  self.sizeListsizeList[li][ldi][si].append(TH1F("size" + "_" + str(layerN) + "." +
233  str(ladderN) + "." + str(sensorN) + "." + str(s), " ", 20, 0, 20))
234  self.chargeListchargeList[li][ldi][si].append(TH1F("charge" + "_" + str(layerN) + "." +
235  str(ladderN) + "." + str(sensorN) + "." + str(s),
236  " ", 100, 0, 200))
237  self.cdcErrorListcdcErrorList[li][ldi][si].append(TH1F("cdcError" + "_" + str(layerN) + "." +
238  str(ladderN) + "." + str(sensorN) + "." + str(s),
239  " ", 200, 0, 50))
240 
241 
242  self.EventT0HistEventT0Hist = TH1F("EventT0", " ", 160, -40, 40)
243 
244 
245  self.gausgaus = TF1("gaus", 'gaus(0)', -150, 150)
246 
247  self.pol1pol1 = TF1("pol1", "[0] + [1]*x", -30, 30)
248 
249  self.pol3pol3 = TF1("pol3", "[0] + [1]*x + [2]*x*x + [3]*x*x*x", -50, 50)
250 
251  self.NTOTNTOT = 0
252 
253  def event(self):
254  """
255  Function that allows to cicle on the events
256  """
257  self.EvtEvt = self.EvtEvt + 1
258 
259  # fill EventT0 histogram
260 
261  self.cdcEventT0cdcEventT0 = Belle2.PyStoreObj(cdc_Time0)
262  if self.cdcEventT0cdcEventT0.hasEventT0():
263  et0 = self.EventT0HistEventT0Hist
264  et0.Fill(self.cdcEventT0cdcEventT0.getEventT0())
265 
266  # fill plots
267  svdCluster_list = Belle2.PyStoreArray(svd_Clusters)
268  svd_evtInfo = Belle2.PyStoreObj(svd_EventInfo)
269  clsTB = svd_evtInfo.getModeByte().getTriggerBin()
270 
271  self.TBTB = ord(clsTB)
272 
273  for svdCluster in svdCluster_list:
274  self.fillListsfillLists(svdCluster)
275 
276  def terminate(self):
277  """
278  Terminates te class and produces the output rootfile
279  """
280 
281  layerNumberTree = np.zeros(1, dtype=int)
282  ladderNumberTree = np.zeros(1, dtype=int)
283  sensorNumberTree = np.zeros(1, dtype=int)
284  sideTree = np.zeros(1, dtype=int)
285  mean = np.zeros(1, dtype=float)
286  meanerr = np.zeros(1, dtype=float)
287  width = np.zeros(1, dtype=float)
288  widtherr = np.zeros(1, dtype=float)
289  cogmean = np.zeros(1, dtype=float)
290  cogmeanerr = np.zeros(1, dtype=float)
291  runnumber = np.zeros(1, dtype=int)
292  runnumber[0] = int(self.runnumberrunnumber)
293  expnumber = np.zeros(1, dtype=int)
294  expnumber[0] = int(self.expnumberexpnumber)
295  evttime = np.zeros(1, dtype=float)
296  jitter = np.zeros(1, dtype=float)
297  cdcErrorMean = np.zeros(1, dtype=float)
298  clsSize = np.zeros(1, dtype=float)
299  clsCharge = np.zeros(1, dtype=float)
300  clsSNR = np.zeros(1, dtype=float)
301 
302  tfile = TFile(self.outputFileNameoutputFileName, 'recreate')
303  tfile.cd()
304  tree = TTree("cog", "tree")
305  tree.Branch("Layer", layerNumberTree, "Layer/I")
306  tree.Branch("Ladder", ladderNumberTree, "Ladder/I")
307  tree.Branch("Sensor", sensorNumberTree, "Sensor/I")
308  tree.Branch("Side", sideTree, "Side/I")
309  tree.Branch("ResMean", mean, "ResMean/D")
310  tree.Branch("ResMeanErr", meanerr, "ResMeanErr/D")
311  tree.Branch("ResWidth", width, "ResWidth/D")
312  tree.Branch("ResWidthErr", widtherr, "ResWidthErr/D")
313  tree.Branch("CoGMean", cogmean, "CoGMean/D")
314  tree.Branch("CoGMeanErr", cogmeanerr, "CoGMeanErr/D")
315  tree.Branch("RunNumber", runnumber, "RunNumber/I")
316  tree.Branch("ExpNumber", expnumber, "ExpNumber/I")
317  tree.Branch("EvtT0Mean", evttime, "EvtT0Mean/D")
318  tree.Branch("Jitter", jitter, "Jitter/D")
319  tree.Branch("EvtT0ErrorMean", cdcErrorMean, "EvtT0ErrorMean/D")
320  tree.Branch("ClsSizeMean", clsSize, "ClsSizeMean/D")
321  tree.Branch("ClsChargeMean", clsCharge, "ClsChargeMean/D")
322  tree.Branch("ClsSNRMean", clsSNR, "ClsSNRMean/D")
323 
324  # par = [0, 1]
325 
326  # tfileHist.cd()
328  gDirectory.mkdir("plots")
329  gDirectory.cd("plots")
330  for layer in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
331  layerNumber = layer.getLayerNumber()
332  layerNumberTree[0] = layerNumber
333  li = layerNumber - 3
334  gDirectory.mkdir("layer" + str(layerNumber))
335  gDirectory.cd("layer" + str(layerNumber))
336  for ladder in geoCache.getLadders(layer):
337  ladderNumber = ladder.getLadderNumber()
338  ladderNumberTree[0] = ladderNumber
339  ldi = ladderNumber - 1
340  for sensor in geoCache.getSensors(ladder):
341  sensorNumber = sensor.getSensorNumber()
342  sensorNumberTree[0] = sensorNumber
343  si = sensorNumber - 1
344  for side in range(2):
345  sideTree[0] = side
346  # Resolution distribution Histograms with Gaussian Fit
347  res = self.resListresList[li][ldi][si][side]
348  res.GetXaxis().SetTitle("cluster time - EventT0 (ns)")
349  fitResult = int(TFitResultPtr(res.Fit(self.gausgaus, "R")))
350  res.Write()
351  # COG Distribution Histograms
352  cog = self.cogListcogList[li][ldi][si][side]
353  cog.GetXaxis().SetTitle("cluster time (ns)")
354  cog.Write()
355  # CDC EventT0 Distribution Histograms
356  cdc = self.cdcListcdcList[li][ldi][si][side]
357  cdc.GetXaxis().SetTitle("EventT0 (ns)")
358  cdc.Write()
359  cdcError = self.cdcErrorListcdcErrorList[li][ldi][si][side]
360  cdcError.GetXaxis().SetTitle("EventT0 Error (ns)")
361  cdcError.Write()
362  # SNR Distribution Histograms
363  snr = self.snrListsnrList[li][ldi][si][side]
364  clsSNR[0] = snr.GetMean()
365  snr.GetXaxis().SetTitle("cluster SNR")
366  snr.Write()
367  # Size Distribution Histograms
368  size = self.sizeListsizeList[li][ldi][si][side]
369  clsSize[0] = size.GetMean()
370  size.GetXaxis().SetTitle("cluster Size")
371  size.Write()
372  # Charge Distribution Histograms
373  charge = self.chargeListchargeList[li][ldi][si][side]
374  clsCharge[0] = charge.GetMean()
375  charge.GetXaxis().SetTitle("cluster Charge")
376  charge.Write()
377  # ScatterPlot Histograms with Linear Fit
378  sp = self.spListspList[li][ldi][si][side]
379  # covscalebias = sp.GetCovariance()
380  pfxsp = sp.ProfileX()
381  sp.GetXaxis().SetTitle("cluster time (ns)")
382  sp.GetYaxis().SetTitle("EventT0 (ns)")
383  sp.Write()
384  pfxsp.Write()
385 
386  mean[0] = self.gausgaus.GetParameter(1)
387  meanerr[0] = self.gausgaus.GetParError(1)
388  width[0] = self.gausgaus.GetParameter(2)
389  widtherr[0] = self.gausgaus.GetParError(2)
390  cogmean[0] = cog.GetMean()
391  if cog.GetEntries() == 0:
392  cogmeanerr[0] = -1
393  else:
394  cogmeanerr[0] = cog.GetRMS()/math.sqrt(cog.GetEntries())
395  evttime[0] = cdc.GetMean()
396  if cdc.GetEntries() == 0:
397  jitter[0] = -1
398  else:
399  jitter[0] = cdc.GetRMS()
400  cdcErrorMean[0] = cdcError.GetMean()
401 
402  if fitResult == -1:
403  mean[0] = -100.0
404  meanerr[0] = -100.0
405  width[0] = -100.0
406  widtherr[0] = -100.0
407 
408  tree.Fill()
409 
410  gDirectory.cd("../")
411 
412  gDirectory.cd("../")
413  self.EventT0HistEventT0Hist.Write()
414  tree.Write()
415  tfile.Purge()
416  tfile.Close()
417  del tfile
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:214