223 def terminate(self):
224 """ Termination function. """
225 self.vertex_k_av = self.vertex_k_av * (1.0 / len(self.vertex))
226 self.momentum_av = self.momentum_av * (1.0 / len(self.vertex))
227
228 cov_mat = numpy.zeros((4, 4))
229 cov_mat_err = numpy.zeros((4, 4))
230 corr_mat = numpy.zeros((4, 4))
231 corr_mat_err = numpy.zeros((4, 4))
232 for i in range(len(self.vertex)):
233 cov_mat[0][0] = cov_mat[0][0] + \
234 (self.vertex[i].X() - self.vertex_k_av.X()) * \
235 (self.vertex[i].X() - self.vertex_k_av.X())
236 cov_mat[0][1] = cov_mat[0][1] + \
237 (self.vertex[i].X() - self.vertex_k_av.X()) * \
238 (self.vertex[i].Y() - self.vertex_k_av.Y())
239 cov_mat[0][2] = cov_mat[0][2] + \
240 (self.vertex[i].X() - self.vertex_k_av.X()) * \
241 (self.vertex[i].Z() - self.vertex_k_av.Z())
242 cov_mat[0][3] = cov_mat[0][3] + \
243 (self.vertex[i].X() - self.vertex_k_av.X()) * \
244 (self.momentum[i] - self.momentum_av)
245 cov_mat[1][1] = cov_mat[1][1] + \
246 (self.vertex[i].Y() - self.vertex_k_av.Y()) * \
247 (self.vertex[i].Y() - self.vertex_k_av.Y())
248 cov_mat[1][2] = cov_mat[1][2] + \
249 (self.vertex[i].Y() - self.vertex_k_av.Y()) * \
250 (self.vertex[i].Z() - self.vertex_k_av.Z())
251 cov_mat[1][3] = cov_mat[1][3] + \
252 (self.vertex[i].Y() - self.vertex_k_av.Y()) * \
253 (self.momentum[i] - self.momentum_av)
254 cov_mat[2][2] = cov_mat[2][2] + \
255 (self.vertex[i].Z() - self.vertex_k_av.Z()) * \
256 (self.vertex[i].Z() - self.vertex_k_av.Z())
257 cov_mat[2][3] = cov_mat[2][3] + \
258 (self.vertex[i].Z() - self.vertex_k_av.Z()) * \
259 (self.momentum[i] - self.momentum_av)
260 cov_mat[3][3] = cov_mat[3][3] + \
261 (self.momentum[i] - self.momentum_av) * \
262 (self.momentum[i] - self.momentum_av)
263 for i in range(0, 4):
264 for j in range(i, 4):
265 cov_mat[i][j] = cov_mat[i][j] / (len(self.vertex) - 1)
266 for i in range(len(self.vertex)):
267 cov_mat_err[0][0] = cov_mat_err[0][0] + \
268 pow((self.vertex[i].X() - self.vertex_k_av.X()) *
269 (self.vertex[i].X() - self.vertex_k_av.X()) - cov_mat[0][0], 2)
270 cov_mat_err[0][1] = cov_mat_err[0][1] + \
271 pow((self.vertex[i].X() - self.vertex_k_av.X()) *
272 (self.vertex[i].Y() - self.vertex_k_av.Y()) - cov_mat[0][1], 2)
273 cov_mat_err[0][2] = cov_mat_err[0][2] + \
274 pow((self.vertex[i].X() - self.vertex_k_av.X()) *
275 (self.vertex[i].Z() - self.vertex_k_av.Z()) - cov_mat[0][2], 2)
276 cov_mat_err[0][3] = cov_mat_err[0][3] + \
277 pow((self.vertex[i].X() - self.vertex_k_av.X()) *
278 (self.momentum[i] - self.momentum_av) - cov_mat[0][3], 2)
279 cov_mat_err[1][1] = cov_mat_err[1][1] + \
280 pow((self.vertex[i].Y() - self.vertex_k_av.Y()) *
281 (self.vertex[i].Y() - self.vertex_k_av.Y()) - cov_mat[1][1], 2)
282 cov_mat_err[1][2] = cov_mat_err[1][2] + \
283 pow((self.vertex[i].Y() - self.vertex_k_av.Y()) *
284 (self.vertex[i].Z() - self.vertex_k_av.Z()) - cov_mat[1][2], 2)
285 cov_mat_err[1][3] = cov_mat_err[1][3] + \
286 pow((self.vertex[i].Y() - self.vertex_k_av.Y()) *
287 (self.momentum[i] - self.momentum_av) - cov_mat[1][3], 2)
288 cov_mat_err[2][2] = cov_mat_err[2][2] + \
289 pow((self.vertex[i].Z() - self.vertex_k_av.Z()) *
290 (self.vertex[i].Z() - self.vertex_k_av.Z()) - cov_mat[2][2], 2)
291 cov_mat_err[2][3] = cov_mat_err[2][3] + \
292 pow((self.vertex[i].Z() - self.vertex_k_av.Z()) *
293 (self.momentum[i] - self.momentum_av) - cov_mat[2][3], 2)
294 cov_mat_err[3][3] = cov_mat_err[3][3] + \
295 pow((self.momentum[i] - self.momentum_av) *
296 (self.momentum[i] - self.momentum_av) - cov_mat[3][3], 2)
297 for i in range(0, 4):
298 for j in range(i, 4):
299 cov_mat_err[i][j] = \
300 math.sqrt(cov_mat_err[i][j] /
301 ((len(self.vertex) - 2) * len(self.vertex)))
302 self.hist_covmat.SetBinContent(1, cov_mat[0][0])
303 self.hist_covmat.SetBinContent(2, cov_mat[0][1])
304 self.hist_covmat.SetBinContent(3, cov_mat[0][2])
305 self.hist_covmat.SetBinContent(4, cov_mat[1][1])
306 self.hist_covmat.SetBinContent(5, cov_mat[1][2])
307 self.hist_covmat.SetBinContent(6, cov_mat[2][2])
308 self.hist_covmat.SetBinError(1, cov_mat_err[0][0])
309 self.hist_covmat.SetBinError(2, cov_mat_err[0][1])
310 self.hist_covmat.SetBinError(3, cov_mat_err[0][2])
311 self.hist_covmat.SetBinError(4, cov_mat_err[1][1])
312 self.hist_covmat.SetBinError(5, cov_mat_err[1][2])
313 self.hist_covmat.SetBinError(6, cov_mat_err[2][2])
314 for i in range(0, 4):
315 for j in range(i, 4):
316 corr_mat[i][j] = cov_mat[i][j] / \
317 math.sqrt(cov_mat[i][i]) / math.sqrt(cov_mat[j][j])
318
319 corr_mat_err[i][j] = cov_mat_err[i][j] / cov_mat[i][j] * \
320 corr_mat[i][j]
321 self.hist_corrmat.SetBinContent(1, corr_mat[0][0])
322 self.hist_corrmat.SetBinContent(2, corr_mat[0][1])
323 self.hist_corrmat.SetBinContent(3, corr_mat[0][2])
324 self.hist_corrmat.SetBinContent(4, corr_mat[0][3])
325 self.hist_corrmat.SetBinContent(5, corr_mat[1][1])
326 self.hist_corrmat.SetBinContent(6, corr_mat[1][2])
327 self.hist_corrmat.SetBinContent(7, corr_mat[1][3])
328 self.hist_corrmat.SetBinContent(8, corr_mat[2][2])
329 self.hist_corrmat.SetBinContent(9, corr_mat[2][3])
330 self.hist_corrmat.SetBinContent(10, corr_mat[3][3])
331 self.hist_corrmat.SetBinError(1, corr_mat_err[0][0])
332 self.hist_corrmat.SetBinError(2, corr_mat_err[0][1])
333 self.hist_corrmat.SetBinError(3, corr_mat_err[0][2])
334 self.hist_corrmat.SetBinError(4, corr_mat_err[0][3])
335 self.hist_corrmat.SetBinError(5, corr_mat_err[1][1])
336 self.hist_corrmat.SetBinError(6, corr_mat_err[1][2])
337 self.hist_corrmat.SetBinError(7, corr_mat_err[1][3])
338 self.hist_corrmat.SetBinError(8, corr_mat_err[2][2])
339 self.hist_corrmat.SetBinError(9, corr_mat_err[2][3])
340 self.hist_corrmat.SetBinError(10, corr_mat_err[3][3])
341 self.output_file.cd()
342 self.hist_nkl.Write()
343 self.hist_xres.Write()
344 self.hist_yres.Write()
345 self.hist_zres.Write()
346 self.hist_tres.Write()
347 self.hist_pres.Write()
348 self.hist_ptres.Write()
349 self.hist_ppres.Write()
350 self.hist_covmat.Write()
351 self.hist_corrmat.Write()
352 self.output_file.Close()