Belle II Software  release-08-01-10
caf_klm_channel_status.py
1 
8 
9 """
10 Calibration of KLM channel status. It provides calibration constants for the KLMChannelStatus
11 database object.
12 """
13 
14 import collections
15 
16 import basf2
17 from caf.utils import ExpRun, IoV
18 from prompt import CalibrationSettings, INPUT_DATA_FILTERS
19 from prompt.utils import events_in_basf2_file
20 
21 
28 
29 
30 settings = CalibrationSettings(
31  name='KLM channel status',
32  expert_username='sayan97',
33  description=__doc__,
34  input_data_formats=['raw'],
35  input_data_names=['raw_beam', 'raw_cosmic', 'raw_physics'],
36  input_data_filters={
37  'raw_beam': [INPUT_DATA_FILTERS['Run Type']['beam'],
38  INPUT_DATA_FILTERS['Data Quality Tag']['Good Or Recoverable']],
39  'raw_cosmic': [INPUT_DATA_FILTERS['Run Type']['cosmic'],
40  INPUT_DATA_FILTERS['Data Quality Tag']['Good Or Recoverable']],
41  'raw_physics': [INPUT_DATA_FILTERS['Run Type']['physics'],
42  f"NOT {INPUT_DATA_FILTERS['Data Tag']['random_calib']}",
43  INPUT_DATA_FILTERS['Data Tag']['bhabha_all_calib'],
44  INPUT_DATA_FILTERS['Data Tag']['gamma_gamma_calib'],
45  INPUT_DATA_FILTERS['Data Tag']['hadron_calib'],
46  INPUT_DATA_FILTERS['Data Tag']['mumu_tight_or_highm_calib'],
47  INPUT_DATA_FILTERS['Data Tag']['radmumu_calib'],
48  INPUT_DATA_FILTERS['Data Quality Tag']['Good Or Recoverable']]
49  },
50  depends_on=[])
51 
52 
53 
54 
55 def select_input_files(file_to_iov):
56  """
57  Parameters:
58  files_to_iov (dict): Dictionary {run : IOV}.
59  reduced_file_to_iov (dict): Selected data.
60  """
61  run_to_files = collections.defaultdict(list)
62  for input_file, file_iov in file_to_iov.items():
63  run = ExpRun(exp=file_iov.exp_low, run=file_iov.run_low)
64  # Reject files without events.
65  if events_in_basf2_file(input_file) > 0:
66  run_to_files[run].append(input_file)
67  reduced_file_to_iov = collections.OrderedDict()
68  for run, files in run_to_files.items():
69  for input_file in files:
70  reduced_file_to_iov[input_file] = IoV(*run, *run)
71  return reduced_file_to_iov
72 
73 
82 
83 
84 def get_calibrations(input_data, **kwargs):
85  """
86  Parameters:
87  input_data (dict): Should contain every name from the 'input_data_names' variable as a key.
88  Each value is a dictionary with {"/path/to/file_e1_r5.root": IoV(1,5,1,5), ...}. Useful for
89  assigning to calibration.files_to_iov
90 
91  **kwargs: Configuration options to be sent in. Since this may change we use kwargs as a way to help prevent
92  backwards compatibility problems. But you could use the correct arguments in b2caf-prompt-run for this
93  release explicitly if you want to.
94 
95  Currently only kwargs["requested_iov"] is used. This is the output IoV range that your payloads should
96  correspond to. Generally your highest ExpRun payload should be open ended e.g. IoV(3,4,-1,-1)
97 
98  Returns:
99  list(caf.framework.Calibration): All of the calibration objects we want to assign to the CAF process
100  """
101  # Set up config options
102 
103  # In this script we want to use one sources of input data.
104  # Get the input files from the input_data variable
105  file_to_iov_raw_beam = input_data['raw_beam']
106  file_to_iov_raw_cosmic = input_data['raw_cosmic']
107  file_to_iov_raw_physics = input_data['raw_physics']
108 
109  # Select input files (all data are necessary, only removes empty files).
110  reduced_file_to_iov_raw_beam = select_input_files(file_to_iov_raw_beam)
111  reduced_file_to_iov_raw_cosmic = select_input_files(file_to_iov_raw_cosmic)
112  reduced_file_to_iov_raw_physics = select_input_files(file_to_iov_raw_physics)
113 
114  # Merge all input data.
115  input_files_raw = list(reduced_file_to_iov_raw_beam.keys())
116  input_files_raw.extend(list(reduced_file_to_iov_raw_cosmic.keys()))
117  input_files_raw.extend(list(reduced_file_to_iov_raw_physics.keys()))
118  input_files_raw.sort()
119  basf2.B2INFO(f'Total number of raw-data files used as input = {len(input_files_raw)}')
120 
121  if not input_files_raw:
122  raise Exception('No valid input files found!')
123 
124  # Get the overall IoV we our process should cover. Includes the end values that we may want to ignore since our output
125  # IoV should be open ended. We could also use this as part of the input data selection in some way.
126  requested_iov = kwargs['requested_iov']
127 
128  from caf.utils import IoV
129  # The actual value our output IoV payload should have. Notice that we've set it open ended.
130  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
131 
132 
134 
135  from ROOT.Belle2 import KLMChannelStatusAlgorithm
136 
137  alg = KLMChannelStatusAlgorithm()
138 
139 
141 
142  from caf.framework import Calibration, Collection
143 
144  cal_klm = Calibration('KLMChannelStatus')
145 
146 
148 
149  from klm_calibration_utils import get_channel_status_pre_collector_path
150 
151  if input_files_raw:
152  coll_raw = get_collector('raw')
153  rec_path_raw = get_channel_status_pre_collector_path()
154 
155  collection_raw = Collection(collector=coll_raw,
156  input_files=input_files_raw,
157  pre_collector_path=rec_path_raw)
158 
159  cal_klm.add_collection(name='raw', collection=collection_raw)
160 
161 
163 
164  cal_klm.algorithms = [alg]
165 
166  from klm_channel_status import KLMChannelStatus
167 
168  for algorithm in cal_klm.algorithms:
169  algorithm.strategy = KLMChannelStatus
170  algorithm.params = {'iov_coverage': output_iov}
171 
172  # You must return all calibrations you want to run in the prompt process, even if it's only one
173  return [cal_klm]
174 
175 
176 
177 
178 def get_collector(input_data_name):
179  """
180  Return the correct KLMChannelStatusCollector module setup for each data type.
181  Placed here so it can be different for prompt compared to standard.
182  """
183 
184  if input_data_name == 'raw':
185  return basf2.register_module('KLMChannelStatusCollector')
186  raise Exception("Unknown input data name used when setting up collector")