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