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