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