Belle II Software  release-06-02-00
cdst_calibrateModuleT0.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # Calibrate module T0 with Bhabha (or dimuon) events using likelihood method
14 # Note: this method works correctly only if initial calibration is reasonably good
15 #
16 # usage: basf2 cdst_calibrateModuleT0.py experiment runFirst runLast
17 # job: bsub -q l "basf2 cdst_calibrateModuleT0.py experiment runFirst runLast"
18 #
19 # note: runLast is inclusive
20 # ---------------------------------------------------------------------------------------
21 
22 import basf2 as b2
23 from ROOT import Belle2
24 import sys
25 import glob
26 import os
27 
28 # ----- those need to be adjusted before running --------------------------------------
29 #
30 sampleType = 'bhabha' # sample type: 'bhabha' or 'dimuon'
31 data_dir = '/group/belle2/dataprod/Data/release-03-02-02/DB00000635/proc00000009_nofilter'
32 skim_dir = 'skim/hlt_bhabha/cdst/sub00'
33 globalTag = 'data_reprocessing_prompt' # base global tag (fall-back)
34 stagingTags = ['staging_data_reprocessing'] # list of global tags with new calibration
35 localDB = [] # list of local databases with new calibration
36 output_dir = 'moduleT0' # main output folder
37 #
38 # -------------------------------------------------------------------------------------
39 
40 # Argument parsing
41 argvs = sys.argv
42 if len(argvs) < 4:
43  print("usage: basf2", argvs[0], "experiment runFirst runLast")
44  sys.exit()
45 experiment = int(argvs[1])
46 run_first = int(argvs[2])
47 run_last = int(argvs[3])
48 
49 expNo = 'e' + '{:0=4d}'.format(experiment)
50 
51 # Make list of files
52 files = []
53 for run in range(run_first, run_last + 1):
54  runNo = 'r' + '{:0=5d}'.format(run)
55  for typ in ['4S', 'Continuum', 'Scan']:
56  folder = data_dir + '/' + expNo + '/' + typ + '/' + runNo + '/' + skim_dir
57  files += glob.glob(folder + '/cdst.*.root')
58 if len(files) == 0:
59  b2.B2ERROR('No cdst files found')
60  sys.exit()
61 
62 # Output folder
63 method = 'LL'
64 output_folder = output_dir + '/' + expNo + '/' + sampleType + '/' + method
65 if not os.path.isdir(output_folder):
66  os.makedirs(output_folder)
67  print('New folder created: ' + output_folder)
68 
69 # Output file name
70 fileName = output_folder + '/moduleT0-' + expNo + '-'
71 run1 = 'r' + '{:0=5d}'.format(run_first)
72 run2 = 'r' + '{:0=5d}'.format(run_last)
73 fileName += run1 + '_to_' + run2 + '.root'
74 print('Output file:', fileName)
75 
76 
77 class Mask_BS13d(b2.Module):
78  ''' exclude (mask-out) BS 13d '''
79 
80  def event(self):
81  ''' event processing '''
82 
83  for digit in Belle2.PyStoreArray('TOPDigits'):
84  if digit.getModuleID() == 13 and digit.getBoardstackNumber() == 3:
85  digit.setHitQuality(Belle2.TOPDigit.c_Junk)
86 
87 
88 # Database
89 b2.use_central_database(globalTag)
90 for tag in stagingTags:
91  b2.use_central_database(tag)
92 for db in localDB:
93  if os.path.isfile(db):
94  b2.use_local_database(db, invertLogging=True)
95  else:
96  b2.B2ERROR(db + ": local database not found")
97  sys.exit()
98 
99 # Create path
100 main = b2.create_path()
101 
102 # Input (cdst files)
103 main.add_module('RootInput', inputFileNames=files)
104 
105 # Initialize TOP geometry parameters
106 main.add_module('TOPGeometryParInitializer')
107 
108 # Time Recalibrator
109 main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
110 
111 # Channel masking
112 main.add_module('TOPChannelMasker')
113 
114 # Exclude BS13d
115 main.add_module(Mask_BS13d())
116 
117 # Bunch finder
118 main.add_module('TOPBunchFinder', usePIDLikelihoods=True, subtractRunningOffset=False)
119 
120 # Module T0 calibration
121 main.add_module('TOPModuleT0Calibrator', sample=sampleType, outputFileName=fileName)
122 
123 # Print progress
124 progress = b2.register_module('Progress')
125 main.add_module(progress)
126 
127 # Process events
128 b2.process(main)
129 
130 # Print statistics
131 print(b2.statistics)
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56