Belle II Software  release-05-02-19
calculateModuleT0FromLaser.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # ---------------------------------------------------------------------------------------
5 # Script to exctract an approximate ModuleT0 constant using the laser data.
6 # The input of the file is the root file containing the tree with all the
7 # localT0 constants produced by the TOPLaserCalibratorModule
8 # Usage: basf2 checkCalibrationOnCalpulseRun.py <reference slot (1-16)>
9 # <reference channel (0-511)> <input_file.root> <output_file.root>
10 #
11 # Contributors: Umberto Tamponi (tamponi@to.infn.it)
12 #
13 # ---------------------------------------------------------------------------------------
14 
15 from basf2 import *
16 from ROOT import Belle2, TTree, TFile, TH1F
17 import sys
18 import math
19 from array import array
20 
21 if len(sys.argv) is not 4:
22  print('usage: basf2', argvs[0], ' <reference slot (1-16)> <reference channel (0-511)> <input_file.root> <output_file.root>')
23  sys.exit()
24 
25 refSlot = int(sys.argv[1]) - 1 # 1-based to 0-based
26 refChan = int(sys.argv[2])
27 
28 laserT0 = [0.] * 16
29 laserT0Err = [0.] * 16
30 
31 inputFile = TFile(str(sys.argv[3]))
32 inputTree = inputFile.Get('chT0')
33 
34 for entry in inputTree:
35  if int(entry.channel) == refChan:
36  laserT0[int(entry.slot) - 1] = float(entry.fittedTime)
37  laserT0Err[int(entry.slot) - 1] = float(entry.fittedTimeError)
38 inputFile.Close()
39 
40 outFile = TFile(str(sys.argv[4]), 'recreate')
41 outTree = TTree('moduleT0laser', 'Module T0 constants using the laser')
42 moduleT0LaserConst = array('f', [0.])
43 moduleT0LaserErr = array('f', [0.])
44 outTree.Branch('moduleT0LaserConst', moduleT0LaserConst, ' moduleT0LaserConst/F')
45 outTree.Branch('moduleT0LaserErr', moduleT0LaserErr, ' moduleT0LaserErr/F')
46 
47 print('------------------------------------------------------------------')
48 print('Results of TOP module-by-module sychronization using the Laser data')
49 print(' ')
50 for iSlot in range(0, 16):
51  moduleT0LaserConst = 0.
52  moduleT0LaserErr = 0.
53  if laserT0[iSlot] != 0:
54  moduleT0LaserConst = laserT0[iSlot] - laserT0[refSlot]
55  moduleT0LaserErr = math.sqrt(
56  laserT0Err[iSlot] *
57  laserT0Err[iSlot] +
58  laserT0Err[refSlot] *
59  laserT0Err[refSlot] +
60  0.055 *
61  0.055 +
62  0.025 *
63  0.025)
64  print('Slot ' + "%02d" % (iSlot + 1) + ': constant = ' + "%+06.3f" % (round(moduleT0LaserConst, 3)) +
65  ' ns; Error = ' + "%05.3f" % (round(moduleT0LaserErr, 3)) + ' ns')
66  outTree.Fill()
67 print(' ')
68 print('------------------------------------------------------------------')
69 
70 outTree.Write()
71 outFile.Close()