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