Belle II Software  release-08-01-10
CoGCalibration_utils_tbindependent.py
1 # !/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 
26 
27 
28 import basf2 as b2
29 from ROOT import Belle2, TFile, TH1F, TH2D
30 from ROOT import TF1, gDirectory, gROOT
31 # import numpy
32 from ROOT.Belle2 import SVDCoGCalibrationFunction
33 
34 
35 svd_recoDigits = "SVDRecoDigitsFromTracks"
36 cdc_Time0 = "EventT0"
37 svd_Clusters = "SVDClustersFromTracks"
38 
39 gROOT.SetBatch(True)
40 
41 # mode = True
42 
43 
45  """
46  Python class used for checking SVD CoG Calibration stored in a localDB,
47  creating a localDB with the correction and a root file to check the corrections
48  """
49 
51  """
52  Function that allows to set if apply the CDC latency correction
53 
54  parameters:
55  mode (bool):
56  - if True -> not apply correction
57  - if False -> apply correction
58  """
59 
60  self.notApplyCDCLatencyCorrectionnotApplyCDCLatencyCorrection = mode
61  print("Not Correct for CDC latency: " + str(mode) + " " + str(self.notApplyCDCLatencyCorrectionnotApplyCDCLatencyCorrection))
62 
63  def fillLists(self, svdRecoDigits_rel_Clusters, svdClusters_rel_RecoTracks_cl):
64  """
65  Function that fill the lists needed for the CoG corrections
66 
67  parameters:
68  svdRecoDigits_rel_Clusters (SVDRecoDigit): reco digits related to clusters
69  svdClusters_rel_RecoTracks_cl (SVDCluster): cluster related to tracks
70  """
71 
72  timeCluster = svdClusters_rel_RecoTracks_cl.getClsTime()
73  snrCluster = svdClusters_rel_RecoTracks_cl.getSNR()
74  layerCluster = svdClusters_rel_RecoTracks_cl.getSensorID().getLayerNumber()
75  layerIndex = layerCluster - 3
76  sensorCluster = svdClusters_rel_RecoTracks_cl.getSensorID().getSensorNumber()
77  sensorIndex = sensorCluster - 1
78  ladderCluster = svdClusters_rel_RecoTracks_cl.getSensorID().getLadderNumber()
79  ladderIndex = ladderCluster - 1
80  sideCluster = svdClusters_rel_RecoTracks_cl.isUCluster()
81  if sideCluster:
82  sideIndex = 1
83  else:
84  sideIndex = 0
85 
86  hasTimezero = self.cdcEventT0cdcEventT0.hasEventT0()
87  # print("Time: " + str(hasTimezero))
88  if hasTimezero:
89  svdEventInfo = Belle2.PyStoreObj("SVDEventInfo")
90  TBClusters = svdEventInfo.getModeByte().getTriggerBin()
91  TBIndex = ord(TBClusters)
92  tZero = self.cdcEventT0cdcEventT0.getEventT0()
93  # tZero_err = self.cdcEventT0.getEventT0Uncertainty()
94  # tZero_err = 5.1
95  tZeroSync = tZero - 4000./509 * (3 - TBIndex)
96  et0 = self.EventT0HistEventT0Hist
97  et0.Fill(tZeroSync)
98  # print(str(tZero_err))
99 
100  # print(self.resList[sideIndex])
101  resHist = self.resListresList[layerIndex][ladderIndex][sensorIndex][sideIndex]
102  resHist.Fill(timeCluster - tZeroSync)
103  spHist = self.spListspList[layerIndex][ladderIndex][sensorIndex][sideIndex]
104  # spHist.Fill(timeCluster, 1.3*timeCluster - 50 + random.gauss(0,10))
105  spHist.Fill(timeCluster, tZeroSync)
106  cogHist = self.cogListcogList[layerIndex][ladderIndex][sensorIndex][sideIndex]
107  cogHist.Fill(timeCluster)
108  cdcHist = self.cdcListcdcList[layerIndex][ladderIndex][sensorIndex][sideIndex]
109  cdcHist.Fill(tZeroSync)
110  snrHist = self.snrListsnrList[layerIndex][ladderIndex][sensorIndex][sideIndex]
111  snrHist.Fill(snrCluster)
112 
113  self.nListnList[layerIndex][ladderIndex][sensorIndex][sideIndex] += 1
114 
115  self.sumCOGListsumCOGList[layerIndex][ladderIndex][sensorIndex][sideIndex] += timeCluster
116  self.sumCOGList2sumCOGList2[layerIndex][ladderIndex][sensorIndex][sideIndex] += timeCluster * timeCluster
117  self.sumCOGList3sumCOGList3[layerIndex][ladderIndex][sensorIndex][sideIndex] += timeCluster * timeCluster * timeCluster
118  self.sumCOGList4sumCOGList4[layerIndex][ladderIndex][sensorIndex][sideIndex] += timeCluster * \
119  timeCluster * timeCluster * timeCluster
120  self.sumCOGList5sumCOGList5[layerIndex][ladderIndex][sensorIndex][sideIndex] += timeCluster * \
121  timeCluster * timeCluster * timeCluster * timeCluster
122  self.sumCOGList6sumCOGList6[layerIndex][ladderIndex][sensorIndex][sideIndex] += timeCluster * \
123  timeCluster * timeCluster * timeCluster * timeCluster * timeCluster
124 
125  self.sumCDCListsumCDCList[layerIndex][ladderIndex][sensorIndex][sideIndex] += tZeroSync
126  self.sumCDCCOGListsumCDCCOGList[layerIndex][ladderIndex][sensorIndex][sideIndex] += tZeroSync * timeCluster
127  self.sumCDCCOGList2sumCDCCOGList2[layerIndex][ladderIndex][sensorIndex][sideIndex] += tZeroSync * timeCluster * timeCluster
128  self.sumCDCCOGList3sumCDCCOGList3[layerIndex][ladderIndex][sensorIndex][sideIndex] += tZeroSync * \
129  timeCluster * timeCluster * timeCluster
130 
131 
132  self.NTOTNTOT = self.NTOTNTOT + 1
133 
134  def set_localdb(self, localDB):
135  """
136  Function that allows to set the localDB
137 
138  parameters:
139  localDB (str): Name of the localDB used
140  """
141 
142  self.localdblocaldb = localDB
143 
144  def initialize(self):
145  """
146  Initialize object (histograms, lists, ...) used by the class
147  """
148 
149 
150  self.outputFileNameoutputFileName = "CoGCorrectionMonitor_" + self.localdblocaldb + ".root"
151 
152 
154  self.resListresList = []
155 
156  self.spListspList = []
157 
158  self.cogListcogList = []
159 
160  self.cdcListcdcList = []
161 
162  self.snrListsnrList = []
163 
164  self.nListnList = []
165 
166  self.sumCOGListsumCOGList = []
167 
168  self.sumCOGList2sumCOGList2 = []
169 
170  self.sumCOGList3sumCOGList3 = []
171 
172  self.sumCOGList4sumCOGList4 = []
173 
174  self.sumCOGList5sumCOGList5 = []
175 
176  self.sumCOGList6sumCOGList6 = []
177 
178  self.sumCDCListsumCDCList = []
179 
180  self.sumCDCCOGListsumCDCCOGList = []
181 
182  self.sumCDCCOGList2sumCDCCOGList2 = []
183 
184  self.sumCDCCOGList3sumCDCCOGList3 = []
185 
187 
188  self.EvtEvt = 0
189  for layer in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
190  layerList0 = []
191  layerList1 = []
192  layerList2 = []
193  layerList3 = []
194  layerList4 = []
195  layerList5 = []
196  layerList6 = []
197  layerList7 = []
198  layerList8 = []
199 
200  layerList9 = []
201  layerList10 = []
202  layerList11 = []
203  layerList12 = []
204  layerList13 = []
205  layerList14 = []
206  layerList15 = []
207  layerList16 = []
208  layerList17 = []
209 
210  self.resListresList.append(layerList0)
211  self.spListspList.append(layerList1)
212  self.cogListcogList.append(layerList2)
213  self.cdcListcdcList.append(layerList3)
214  self.snrListsnrList.append(layerList4)
215 
216  self.nListnList.append(layerList8)
217  self.sumCOGListsumCOGList.append(layerList7)
218  self.sumCOGList2sumCOGList2.append(layerList9)
219  self.sumCOGList3sumCOGList3.append(layerList10)
220  self.sumCOGList4sumCOGList4.append(layerList11)
221  self.sumCOGList5sumCOGList5.append(layerList12)
222  self.sumCOGList6sumCOGList6.append(layerList13)
223 
224  self.sumCDCListsumCDCList.append(layerList14)
225  self.sumCDCCOGListsumCDCCOGList.append(layerList15)
226  self.sumCDCCOGList2sumCDCCOGList2.append(layerList16)
227  self.sumCDCCOGList3sumCDCCOGList3.append(layerList17)
228 
229  # layerNumber = layer.getLayerNumber()
230  for ladder in geoCache.getLadders(layer):
231  ladderList0 = []
232  ladderList1 = []
233  ladderList2 = []
234  ladderList3 = []
235  ladderList4 = []
236  ladderList5 = []
237  ladderList6 = []
238  ladderList7 = []
239  ladderList8 = []
240 
241  ladderList9 = []
242  ladderList10 = []
243  ladderList11 = []
244  ladderList12 = []
245  ladderList13 = []
246  ladderList14 = []
247  ladderList15 = []
248  ladderList16 = []
249  ladderList17 = []
250 
251  layerList0.append(ladderList0)
252  layerList1.append(ladderList1)
253  layerList2.append(ladderList2)
254  layerList3.append(ladderList3)
255  layerList4.append(ladderList4)
256  layerList5.append(ladderList5)
257  layerList6.append(ladderList6)
258  layerList7.append(ladderList7)
259  layerList8.append(ladderList8)
260 
261  layerList9.append(ladderList9)
262  layerList10.append(ladderList10)
263  layerList11.append(ladderList11)
264  layerList12.append(ladderList12)
265  layerList13.append(ladderList13)
266  layerList14.append(ladderList14)
267  layerList15.append(ladderList15)
268  layerList16.append(ladderList16)
269  layerList17.append(ladderList17)
270  # ladderNumber = ladder.getLadderNumber()
271  for sensor in geoCache.getSensors(ladder):
272  sensorList0 = []
273  sensorList1 = []
274  sensorList2 = []
275  sensorList3 = []
276  sensorList4 = []
277  sensorList5 = []
278  sensorList6 = []
279  sensorList7 = []
280  sensorList8 = []
281 
282  sensorList9 = []
283  sensorList10 = []
284  sensorList11 = []
285  sensorList12 = []
286  sensorList13 = []
287  sensorList14 = []
288  sensorList15 = []
289  sensorList16 = []
290  sensorList17 = []
291 
292  ladderList0.append(sensorList0)
293  ladderList1.append(sensorList1)
294  ladderList2.append(sensorList2)
295  ladderList3.append(sensorList3)
296  ladderList4.append(sensorList4)
297  ladderList5.append(sensorList5)
298  ladderList6.append(sensorList6)
299  ladderList7.append(sensorList7)
300  ladderList8.append(sensorList8)
301 
302  ladderList9.append(sensorList9)
303  ladderList10.append(sensorList10)
304  ladderList11.append(sensorList11)
305  ladderList12.append(sensorList12)
306  ladderList13.append(sensorList13)
307  ladderList14.append(sensorList14)
308  ladderList15.append(sensorList15)
309  ladderList16.append(sensorList16)
310  ladderList17.append(sensorList17)
311  # sensorNumber = sensor.getSensorNumber()
312  '''
313  for side in range(2):
314  sideList0 = []
315  sideList1 = []
316  sideList2 = []
317  sideList3 = []
318  sideList4 = []
319  sideList5 = []
320  sideList6 = []
321  sideList7 = []
322  sideList8 = []
323  sensorList0.append(sideList0)
324  sensorList1.append(sideList1)
325  sensorList2.append(sideList2)
326  sensorList3.append(sideList3)
327  sensorList4.append(sideList4)
328  sensorList5.append(sideList5)
329  sensorList6.append(sideList6)
330  sensorList7.append(sideList7)
331  sensorList8.append(sideList8)
332  '''
333  for i in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
334  layerN = i.getLayerNumber()
335  li = layerN - 3
336  for j in geoCache.getLadders(i):
337  ladderN = j.getLadderNumber()
338  ldi = ladderN - 1
339  for k in geoCache.getSensors(j):
340  sensorN = k.getSensorNumber()
341  si = sensorN - 1
342  for s in range(2):
343  self.resListresList[li][ldi][si].append(
344  TH1F("res" + "_" + str(k) + "." + str(s), " ", 200, -100, 100))
345  self.spListspList[li][ldi][si].append(
346  TH2D("sp" + "_" + str(k) + "." + str(s), " ", 300, -150, 150, 300, -150, 150))
347  self.cogListcogList[li][ldi][si].append(
348  TH1F("cog" + "_" + str(k) + "." + str(s), " ", 200, -100, 100))
349  self.cdcListcdcList[li][ldi][si].append(
350  TH1F("cdc" + "_" + str(k) + "." + str(s), " ", 200, -100, 100))
351  self.snrListsnrList[li][ldi][si].append(
352  TH1F("snr" + "_" + str(k) + "." + str(s), " ", 100, 0, 100))
353  self.nListnList[li][ldi][si].append(0)
354  self.sumCOGListsumCOGList[li][ldi][si].append(0)
355  self.sumCOGList2sumCOGList2[li][ldi][si].append(0)
356  self.sumCOGList3sumCOGList3[li][ldi][si].append(0)
357  self.sumCOGList4sumCOGList4[li][ldi][si].append(0)
358  self.sumCOGList5sumCOGList5[li][ldi][si].append(0)
359  self.sumCOGList6sumCOGList6[li][ldi][si].append(0)
360  self.sumCDCListsumCDCList[li][ldi][si].append(0)
361  self.sumCDCCOGListsumCDCCOGList[li][ldi][si].append(0)
362  self.sumCDCCOGList2sumCDCCOGList2[li][ldi][si].append(0)
363  self.sumCDCCOGList3sumCDCCOGList3[li][ldi][si].append(0)
364 
365 
366  self.EventT0HistEventT0Hist = TH1F("EventT0", " ", 200, -100, 100)
367 
368  self.gausgaus = TF1("gaus", 'gaus(0)', -150, 100)
369 
370  self.polpol = TF1("pol", "[0] + [1]*x + [2]*x*x + [3]*x*x*x", -150, 150)
371 
372  self.NTOTNTOT = 0
373 
374  def event(self):
375  """
376  Function that allows to cicle on the events
377  """
378  # timeClusterU = 0
379  # timeClusterV = 0
380  # sideIndex = 0
381  # TBIndexU = 0
382  # TBIndexV = 0
383 
384  self.EvtEvt = self.EvtEvt + 1
385 
386 
387  self.cdcEventT0cdcEventT0 = Belle2.PyStoreObj(cdc_Time0)
388  svdCluster_list = Belle2.PyStoreArray(svd_Clusters)
389  # svdRecoDigit_list = Belle2.PyStoreArray(svd_recoDigits)
390 
391  for svdCluster in svdCluster_list:
392  svdRecoDigit = svdCluster.getRelatedTo(svd_recoDigits)
393  self.fillListsfillLists(svdRecoDigit, svdCluster)
394 
395  def terminate(self):
396  """
397  Terminates te class and produces the output rootfile
398  """
399 
400  tfile = TFile(self.outputFileNameoutputFileName, 'recreate')
402 
404 
405  timeCal = SVDCoGCalibrationFunction()
406  par = [0, 1, 0, 0]
407  # TCOGMEAN = 0
408  T0MEAN = 0
409 
410  '''
411  geoCache = Belle2.VXD.GeoCache.getInstance()
412  for layer in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
413  layerNumber = layer.getLayerNumber()
414  li = layerNumber - 3
415  for ladder in geoCache.getLadders(layer):
416  ladderNumber = ladder.getLadderNumber()
417  ldi = ladderNumber - 1
418  for sensor in geoCache.getSensors(ladder):
419  sensorNumber = sensor.getSensorNumber()
420  si = sensorNumber - 1
421  for side in range(2):
422  for tb in range(4):
423  n = self.nList[li][ldi][si][side][tb]
424  NTOT += n
425  '''
427  gDirectory.mkdir("plots")
428  gDirectory.cd("plots")
429  for layer in geoCache.getLayers(Belle2.VXD.SensorInfoBase.SVD):
430  layerNumber = layer.getLayerNumber()
431  li = layerNumber - 3
432  gDirectory.mkdir("layer" + str(layer))
433  gDirectory.cd("layer" + str(layer))
434  for ladder in geoCache.getLadders(layer):
435  ladderNumber = ladder.getLadderNumber()
436  ldi = ladderNumber - 1
437  for sensor in geoCache.getSensors(ladder):
438  sensorNumber = sensor.getSensorNumber()
439  si = sensorNumber - 1
440  for side in range(2):
441  # Resolution distribution Histograms with Gaussian Fit
442  res = self.resListresList[li][ldi][si][side]
443  # fitResult = int(TFitResultPtr(res.Fit(self.gaus, "R")))
444 
445  res.Write()
446  # COG Distribution Histograms
447  cog = self.cogListcogList[li][ldi][si][side]
448  cog.Write()
449  # CDC EventT0 Distribution Histograms
450  cdc = self.cdcListcdcList[li][ldi][si][side]
451  cdc.Write()
452  # SNR Distribution Histograms
453  snr = self.snrListsnrList[li][ldi][si][side]
454  # snrMean = snr.GetMean()
455  snr.Write()
456  # ScatterPlot Histograms with Linear Fit
457  sp = self.spListspList[li][ldi][si][side]
458  # covscalebias = sp.GetCovariance()
459  pfxsp = sp.ProfileX()
460  self.polpol.SetParameters(-50, 1.5, 0.001, 0.00001)
461  pfxsp.Fit(self.polpol, "R")
462  par[0] = self.polpol.GetParameter(0)
463  par[1] = self.polpol.GetParameter(1)
464  par[2] = self.polpol.GetParameter(2)
465  par[3] = self.polpol.GetParameter(3)
466  sp.Write()
467  pfxsp.Write()
468 
469  # T0MEAN = self.EventT0Hist.GetMean()
470  T0MEAN = cdc.GetMean()
471  '''
472  print(
473  "Mean of the CoG corrected distribution: " +
474  str(TCOGMEAN) +
475  " Mean of the T0 distribution: " +
476  str(T0MEAN))
477  '''
478  timeCal.set_current(1)
479  timeCal.set_pol3parameters(par[0] - T0MEAN, par[1], par[2], par[3])
480  print("setting CoG calibration for " + str(layerNumber) + "." + str(ladderNumber) + "." + str(sensorNumber))
481  payload.set(layerNumber, ladderNumber, sensorNumber, bool(side), 1, timeCal)
482  gDirectory.cd("../")
483 
484  gDirectory.cd("../")
485  self.EventT0HistEventT0Hist.Write()
486 
487  Belle2.Database.Instance().storeData(Belle2.SVDCoGTimeCalibrations.name, payload, iov)
488 
489  tfile.Close()
static IntervalOfValidity always()
Function that returns an interval of validity that is always valid, c.f.
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
base class for calibrations classes
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:214
notApplyCDCLatencyCorrection
parameter that allows to apply or not the CDC latency correction
def fillLists(self, svdRecoDigits_rel_Clusters, svdClusters_rel_RecoTracks_cl)
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42