Belle II Software  release-08-01-10
run_postTracking_calibration.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # CAF calibration script: post-tracking calibration
14 # (BS13d carrier shifts, module T0 and common T0)
15 #
16 # data type: cdst (bhabha or dimuon)
17 #
18 # usage: basf2 run_postTracking_calibration.py expNo runFirst runLast [sample method]
19 # sample = bhabha/dimuon (D = bhabha)
20 # method = BF/LL (D = BF)
21 # ---------------------------------------------------------------------------------------
22 
23 import sys
24 import glob
25 from caf import backends
26 from caf.framework import CAF
27 from basf2 import B2ERROR
28 from top_calibration import BS13d_calibration_cdst
29 from top_calibration import moduleT0_calibration_DeltaT, moduleT0_calibration_LL
30 from top_calibration import commonT0_calibration_BF, commonT0_calibration_LL
31 from top_calibration import offset_calibration
32 
33 # ----- those parameters need to be adjusted before running -----------------------------
34 #
35 globalTags = ['Reco_master_patch', 'data_reprocessing_proc10'] # highest priority first
36 localDBs = [] # highest priority first, local DB's have higher priority than global tags
37 data_dir = '/group/belle2/dataprod/Data/OfficialReco/proc10/'
38 bhabha_skim_dir = 'skim/hlt_bhabha/cdst/sub00'
39 dimuon_skim_dir = 'offskim/offskim_mumutop/cdst/sub00'
40 main_output_dir = 'top_calibration'
41 default_sample = 'bhabha'
42 default_method = 'BF' # default method for common T0
43 time_offset = 0 # [ns], set to -66.8 for proc9 or older (processed w/ release-3)
44 new_cdst_format = False # set to True for input in new cdst format
45 #
46 # ---------------------------------------------------------------------------------------
47 
48 # Argument parsing
49 argvs = sys.argv
50 if len(argvs) < 4:
51  print("usage: basf2", argvs[0], "experiment runFirst runLast [sample method]")
52  print(" sample = bhabha/dimuon (D = bhabha)")
53  print(" method = BF/LL (D = BF)")
54  sys.exit()
55 experiment = int(argvs[1])
56 run_first = int(argvs[2])
57 run_last = int(argvs[3])
58 sample = default_sample
59 method = default_method
60 
61 if len(argvs) == 5:
62  sample = argvs[4]
63 elif len(argvs) > 5:
64  sample = argvs[4]
65  method = argvs[5]
66 
67 if sample == 'bhabha':
68  skim_dir = bhabha_skim_dir
69 elif sample == 'dimuon':
70  skim_dir = dimuon_skim_dir
71 else:
72  B2ERROR("Invalid sample name: " + sample)
73  sys.exit()
74 
75 # Make list of files
76 inputFiles = []
77 expNo = 'e' + '{:0=4d}'.format(experiment)
78 for run in range(run_first, run_last + 1):
79  runNo = 'r' + '{:0=5d}'.format(run)
80  filename = f"{data_dir}/{expNo}/*/{runNo}/{skim_dir}/cdst*.root"
81  inputFiles += glob.glob(filename)
82 
83 if len(inputFiles) == 0:
84  B2ERROR('No cdst files found in ' + data_dir + ' for exp=' + str(experiment) +
85  ' runFirst=' + str(run_first) + ' runLast=' + str(run_last) +
86  ' (skim_dir=' + skim_dir + ')')
87  sys.exit()
88 
89 # Output folder name
90 run_range = 'r' + '{:0=5d}'.format(run_first) + '-' + '{:0=5d}'.format(run_last)
91 output_dir = f"{main_output_dir}/postTracking-{sample}-{method}-{expNo}-{run_range}"
92 
93 # Define calibrations
94 cal1 = BS13d_calibration_cdst(inputFiles, time_offset, globalTags, localDBs, new_cdst_format)
95 cal2 = moduleT0_calibration_DeltaT(inputFiles, globalTags, localDBs, new_cdst_format)
96 cal3 = moduleT0_calibration_LL(inputFiles, sample, globalTags, localDBs, new_cdst_format)
97 if method == 'BF':
98  cal4 = commonT0_calibration_BF(inputFiles, globalTags, localDBs, new_cdst_format)
99 elif method == 'LL':
100  cal4 = commonT0_calibration_LL(inputFiles, sample, globalTags, localDBs, new_cdst_format)
101 else:
102  B2ERROR('Invalid method name: ' + method)
103  sys.exit()
104 cal5 = offset_calibration(inputFiles, globalTags, localDBs, new_cdst_format)
105 
106 cal1.backend_args = {"queue": "s"}
107 cal2.backend_args = {"queue": "s"}
108 cal3.backend_args = {"queue": "s"}
109 cal4.backend_args = {"queue": "s"}
110 cal5.backend_args = {"queue": "s"}
111 
112 # Dependencies
113 cal2.depends_on(cal1)
114 cal3.depends_on(cal2)
115 cal4.depends_on(cal3)
116 cal5.depends_on(cal4)
117 
118 # Add calibrations to CAF
119 cal_fw = CAF()
120 cal_fw.add_calibration(cal1)
121 cal_fw.add_calibration(cal2)
122 cal_fw.add_calibration(cal3)
123 cal_fw.add_calibration(cal4)
124 cal_fw.add_calibration(cal5)
125 cal_fw.output_dir = output_dir
126 cal_fw.backend = backends.LSF()
127 
128 # Run calibration
129 cal_fw.run()