22 from ROOT
import Belle2
23 from ROOT
import TFile, TH1F, TH2F, TF1, TMatrixDSym
24 from array
import array
33 data_dir =
'/group/belle2/dataprod/Data/release-03-02-02/DB00000635/proc00000009_nofilter'
34 skim_dir =
'skim/hlt_bhabha/cdst/sub00/'
35 globalTag =
'data_reprocessing_prompt'
36 stagingTags = [
'staging_data_reprocessing']
39 output_dir =
'moduleT0'
46 print(
"usage: basf2", argvs[0],
"experiment runFirst runLast")
48 experiment = int(argvs[1])
49 run_first = int(argvs[2])
50 run_last = int(argvs[3])
52 expNo =
'e' +
'{:0=4d}'.format(experiment)
56 for run
in range(run_first, run_last + 1):
57 runNo =
'r' +
'{:0=5d}'.format(run)
58 for typ
in [
'4S',
'Continuum',
'Scan']:
59 folder = data_dir +
'/' + expNo +
'/' + typ +
'/' + runNo +
'/' + skim_dir
60 files += glob.glob(folder +
'/cdst.*.root')
62 b2.B2ERROR(
'No cdst files found')
67 output_folder = output_dir +
'/' + expNo +
'/' + sampleType +
'/' + method
68 if not os.path.isdir(output_folder):
69 os.makedirs(output_folder)
70 print(
'New folder created: ' + output_folder)
73 fileName = output_folder +
'/moduleT0-' + expNo +
'-'
74 run1 =
'r' +
'{:0=5d}'.format(run_first)
75 run2 =
'r' +
'{:0=5d}'.format(run_last)
76 fileName += run1 +
'_to_' + run2 +
'.root'
77 print(
'Output file:', fileName)
81 ''' exclude (mask-out) BS 13d '''
84 ''' event processing '''
87 if digit.getModuleID() == 13
and digit.getBoardstackNumber() == 3:
88 digit.setHitQuality(Belle2.TOPDigit.c_Junk)
92 ''' module T0 calibrator using chi2 minimization of time differences between slots '''
95 ''' initialize and book histograms '''
106 self.
h_slotPairsh_slotPairs = TH2F(
"slots",
"slot pairs: number of entries",
107 16, 0.5, 16.5, 16, 0.5, 16.5)
108 self.
h_slotPairsh_slotPairs.SetXTitle(
"first slot number")
109 self.
h_slotPairsh_slotPairs.SetYTitle(
"second slot number")
113 "slot pairs: #chi^{2} /NDF of " +
114 "successfully fitted time differences",
115 16, 0.5, 16.5, 16, 0.5, 16.5)
120 self.
h_relModuleT0h_relModuleT0 = TH1F(
"relModuleT0",
"Module T0 relative to calibration",
123 self.
h_relModuleT0h_relModuleT0.SetYTitle(
"module T0 residual [ns]")
126 self.
h_moduleT0h_moduleT0 = TH1F(
"moduleT0",
"Module T0", 16, 0.5, 16.5)
127 self.
h_moduleT0h_moduleT0.SetXTitle(
"slot number")
128 self.
h_moduleT0h_moduleT0.SetYTitle(
"module T0 [ns]")
131 ''' sorting timeZeros according to slot number '''
134 py_list = [x
for x
in unsortedPyStoreArray]
144 ''' event processor: fill histograms for events with two entries in timeZeros '''
147 if timeZeros.getEntries() != 2:
152 slot1 = tZeros[0].getModuleID()
153 slot2 = tZeros[1].getModuleID()
157 if slot2
not in self.
h_deltaT0h_deltaT0[slot1]:
158 name =
'deltaT0_' + str(slot1) +
'-' + str(slot2)
159 title =
'time difference: slot ' + str(slot1) +
' - slot ' + str(slot2)
160 self.
h_deltaT0h_deltaT0[slot1][slot2] = TH1F(name, title, 200, -5, 5)
161 self.
h_deltaT0h_deltaT0[slot1][slot2].SetXTitle(
'time difference [ns]')
162 dt = tZeros[0].getTime() - tZeros[1].getTime()
163 self.
h_deltaT0h_deltaT0[slot1][slot2].Fill(dt)
166 ''' perform a fit of time difference distribution '''
168 xmin = h.GetXaxis().GetXmin()
169 xmax = h.GetXaxis().GetXmax()
170 fun = TF1(
"fun",
"[0]*exp(-0.5*((x-[1])/[2])**2)+[3]", xmin, xmax)
171 fun.SetParameter(0, h.GetMaximum())
172 fun.SetParameter(1, h.GetBinCenter(h.GetMaximumBin()))
173 fun.SetParameter(2, 0.2)
174 fun.SetParameter(3, 0)
175 return h.Fit(fun,
"LERSQ")
178 ''' fit all histograms and store the results into lists '''
189 for slot1
in range(1, 10):
192 for slot2
in range(slot1 + 7, slot1 + 10):
193 if slot2
not in self.
h_deltaT0h_deltaT0[slot1]:
195 h = self.
h_deltaT0h_deltaT0[slot1][slot2]
196 self.
h_outh_out.append(h)
197 if h.GetEntries() > minEntries:
200 chi2 = result.Chi2() / result.Ndf()
202 self.
delT0delT0.append(result.Parameter(1))
203 self.
sigmasigma.append(result.ParError(1))
204 a = [0
for i
in range(16)]
211 Minimization procedure. For the method see NIM A 586 (2008) 174-179, sect. 2.2.
215 self.
delT0delT0.append(0.0)
216 self.
sigmasigma.append(0.001)
217 a = [1
for i
in range(16)]
219 m = len(self.
delT0delT0)
229 B[i][j] += self.
AA[k][i] * self.
AA[k][j] / self.
sigmasigma[k]**2
232 det = array(
'd', [0])
235 b2.B2ERROR(
"Matrix inversion failed")
239 b = [0.0
for i
in range(16)]
242 b[i] += self.
AA[k][i] * self.
delT0delT0[k] / self.
sigmasigma[k]**2
245 x = [0
for i
in range(16)]
246 e = [0
for i
in range(16)]
249 x[i] += B[i][j] * b[j]
250 e[i] = math.sqrt(B[i][i])
257 s += self.
AA[k][i] * x[i]
258 chi2 += ((s - self.
delT0delT0[k]) / self.
sigmasigma[k])**2
260 chi_ndf =
'chi^2/NDF = ' + str(round(chi2, 2)) +
'/' + str(ndf)
269 T0 = [0.0
for i
in range(16)]
282 self.
h_moduleT0h_moduleT0.SetBinContent(i+1, x[i] + T0[i] - average)
283 self.
h_moduleT0h_moduleT0.SetBinError(i+1, e[i])
285 b2.B2RESULT(
"Module T0 calibration constants successfully determined, " + chi_ndf)
289 ''' fit histograms, minimize and write results to file '''
298 file = TFile.Open(fileName,
'recreate')
306 for h
in self.
h_outh_out:
310 b2.B2RESULT(
"Output written to " + fileName)
314 b2.use_central_database(globalTag)
315 for tag
in stagingTags:
316 b2.use_central_database(tag)
318 if os.path.isfile(db):
319 b2.use_local_database(db, invertLogging=
True)
321 b2.B2ERROR(db +
": local database not found")
325 main = b2.create_path()
328 main.add_module(
'RootInput', inputFileNames=files)
331 main.add_module(
'TOPGeometryParInitializer')
334 main.add_module(
'TOPTimeRecalibrator', subtractBunchTime=
False)
337 main.add_module(
'TOPChannelMasker')
343 main.add_module(
'TOPBunchFinder', usePIDLikelihoods=
True, subtractRunningOffset=
False)
349 main.add_module(
'Progress')
Class to access a DBObjPtr from Python.
a (simplified) python wrapper for StoreArray.
A
matrix denoting which slot pair was involved
h_slotPairs_acc
histogram to store Chi2/ndf of successfully fitted time differences
def sortedTimeZeros(self, unsortedPyStoreArray)
def fitHistograms(self, minEntries)
h_relModuleT0
histogram to store minimization results (relative module T0)
h_moduleT0
histogram to store final results (module T0)
sigma
uncertainties of fitted time differences
h_out
list of histograms selected for output
h_deltaT0
histograms of time difference between two slots
h_slotPairs
histogram of number of entries per slot pairs
delT0
fitted time differences
db_moduleT0
module T0 from database