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