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