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