Belle II Software  release-08-01-10
caf_top.py
1 
8 
9 """
10 Airflow script for TOP post-tracking calibration:
11  BS13d carrier shifts, module T0 and common T0.
12  Histograms for validation are also prepared here.
13 """
14 import basf2
15 from prompt import CalibrationSettings, INPUT_DATA_FILTERS
16 from caf.utils import IoV
17 from caf.strategies import SequentialBoundaries
18 from top_calibration import BS13d_calibration_cdst
19 from top_calibration import moduleT0_calibration_DeltaT, moduleT0_calibration_LL
20 from top_calibration import commonT0_calibration_BF
21 from top_calibration import offset_calibration
22 from top_calibration import photonYields_calibration
23 from top_calibration import calibration_validation
24 from prompt.calibrations.caf_top_pre import settings as top_pretracking
25 from prompt.utils import filter_by_max_files_per_run
26 
27 
28 settings = CalibrationSettings(
29  name="TOP post-tracking calibration",
30  expert_username="skohani",
31  description=__doc__,
32  input_data_formats=["cdst"],
33  input_data_names=["mumu_tight_or_highm_calib"],
34  input_data_filters={
35  "mumu_tight_or_highm_calib": [
36  INPUT_DATA_FILTERS["Data Tag"]["mumu_tight_or_highm_calib"],
37  INPUT_DATA_FILTERS["Run Type"]["physics"],
38  INPUT_DATA_FILTERS["Data Quality Tag"]["Good Or Recoverable"]]},
39  depends_on=[top_pretracking],
40  expert_config={
41  "max_files_per_run": 20,
42  "payload_boundaries": None,
43  "request_memory": "8 GB"
44  })
45 
46 
47 # Required function
48 def get_calibrations(input_data, **kwargs):
49  '''
50  Returns a list of calibration objects.
51  :input_data (dict): Contains every file name from the 'input_data_names' as a key.
52  :**kwargs: Configuration options to be sent in.
53  '''
54 
55  file_to_iov = input_data["mumu_tight_or_highm_calib"]
56  sample = 'dimuon'
57  expert_config = kwargs.get("expert_config")
58  max_files_per_run = expert_config["max_files_per_run"]
59  min_events_per_file = 1
60  # Applying the min event per file to remove empty root files
61  reduced_file_to_iov = filter_by_max_files_per_run(file_to_iov, max_files_per_run, min_events_per_file, random_select=True)
62  inputFiles = list(reduced_file_to_iov.keys())
63  basf2.B2INFO(f"Total number of files actually used as input = {len(inputFiles)}")
64  requested_iov = kwargs.get("requested_iov", None)
65  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
66 
67  # Run 2 calibration chain differs a bit from that of Run 1 (take Run 2 chain if requested_iov is not given)
68  if requested_iov.exp_low > 26 or requested_iov.exp_low <= 0:
69  basf2.B2INFO("Running Run 2 calibration chain for TOP")
70  cal = [moduleT0_calibration_DeltaT(inputFiles), # this cal cannot span across experiments
71  moduleT0_calibration_LL(inputFiles, sample), # this cal cannot span across experiments
72  commonT0_calibration_BF(inputFiles), # this is run-dep
73  offset_calibration(inputFiles), # this is run-dep
74  photonYields_calibration(inputFiles, sample), # this cal cannot span across experiments
75  calibration_validation(inputFiles, sample)] # this is run-dep
76  cal[0].save_payloads = False # don't save the rough moduleT0 result
77  cal[5].save_payloads = False # in fact it does not make any payloads, but produces histograms for validation
78  else:
79  basf2.B2INFO("Running Run 1 calibration chain for TOP")
80  cal = [BS13d_calibration_cdst(inputFiles), # this is run-dep
81  moduleT0_calibration_DeltaT(inputFiles), # this cal cannot span across experiments
82  moduleT0_calibration_LL(inputFiles, sample), # this cal cannot span across experiments
83  commonT0_calibration_BF(inputFiles), # this is run-dep
84  offset_calibration(inputFiles), # this is run-dep
85  calibration_validation(inputFiles, sample)] # this is run-dep
86  cal[1].save_payloads = False # don't save the rough moduleT0 result
87  cal[5].save_payloads = False # in fact it does not make any payloads, but produces histograms for validation
88 
89  for c in cal:
90  # If it's a SequentialBoundary calibration, check if there is any boundary in the config file
91  if c.strategies[0] == SequentialBoundaries:
92 
93  # Default boundaries. If there are no boundaries in the config file, this calibration will give a single IoV
94  payload_boundaries = [[output_iov.exp_low, output_iov.run_low]]
95 
96  # user-defined boundaries are set here.
97  if expert_config["payload_boundaries"] is not None:
98  payload_boundaries = expert_config["payload_boundaries"]
99 
100  # Set the actual boundaries.
101  for alg in c.algorithms:
102  alg.params = {"iov_coverage": output_iov, "payload_boundaries": payload_boundaries}
103 
104  # If it's not a SequentialBoundary calbration, just set the IoV coverage
105  else:
106  for alg in c.algorithms:
107  alg.params = {"iov_coverage": output_iov}
108 
109  for i in range(1, len(cal)):
110  cal[i].depends_on(cal[i - 1])
111 
112  return cal