Belle II Software  release-05-02-19
caf_pxd.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 airflow script for PXD hot/dead pixel masking.
5 Author: qingyuan.liu@desy.de
6 """
7 
8 import basf2
9 from pxd.calibration import hot_pixel_mask_calibration
10 from prompt.utils import filter_by_max_files_per_run, filter_by_max_events_per_run
11 from prompt import CalibrationSettings, input_data_filters
12 from caf.utils import IoV
13 from itertools import groupby
14 from itertools import chain
15 from math import ceil
16 
17 
18 settings = CalibrationSettings(name="PXD hot/dead pixel calibration",
19  expert_username="qyliu",
20  description=__doc__,
21  input_data_formats=["raw"],
22  input_data_names=["beamorphysics", "cosmic"],
23  input_data_filters={
24  "beamorphysics": [
25  input_data_filters["Data Tag"]["bhabha_all_calib"],
26  input_data_filters["Data Tag"]["gamma_gamma_calib"],
27  input_data_filters["Data Tag"]["hadron_calib"],
28  input_data_filters["Data Tag"]["offip_calib"],
29  input_data_filters["Data Tag"]["cosmic_calib"],
30  input_data_filters["Beam Energy"]["4S"],
31  input_data_filters["Beam Energy"]["Continuum"],
32  input_data_filters["Beam Energy"]["Scan"],
33  input_data_filters["Run Type"]["physics"],
34  input_data_filters["Data Quality Tag"]["Good Or Recoverable"]],
35  "cosmic": [input_data_filters["Run Type"]["cosmic"]]},
36  expert_config={
37  "max_events_per_run": 400000,
38  "max_files_per_run": 20, # only valid when max_events/run <= 0
39  },
40  depends_on=[])
41 
42 
43 def get_calibrations(input_data, **kwargs):
44  """
45  Parameters:
46  input_data (dict): Should contain every name from the 'input_data_names' variable as a key.
47  Each value is a dictionary with {"/path/to/file_e1_r5.root": IoV(1,5,1,5), ...}. Useful for
48  assigning to calibration.files_to_iov
49 
50  **kwargs: Configuration options to be sent in. Since this may change we use kwargs as a way to help prevent
51  backwards compatibility problems. But you could use the correct arguments in b2caf-prompt-run for this
52  release explicitly if you want to.
53 
54  Currently only kwargs["output_iov"] is used. This is the output IoV range that your payloads should
55  correspond to. Generally your highest ExpRun payload should be open ended e.g. IoV(3,4,-1,-1)
56 
57  Returns:
58  list(caf.framework.Calibration): All of the calibration objects we want to assign to the CAF process
59  """
60 
61  # Set up config options
62  requested_iov = kwargs.get("requested_iov", None)
63  expert_config = kwargs.get("expert_config")
64  cal_kwargs = expert_config.get("kwargs", {})
65  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
66  expert_config = kwargs.get("expert_config")
67  max_events_per_run = expert_config["max_events_per_run"]
68  max_files_per_run = expert_config["max_files_per_run"]
69  min_files_per_chunk = 10
70  min_events_per_file = 1000 # avoid empty files
71 
72  # Read input_data
73  file_to_iov_physics = input_data["beamorphysics"]
74  file_to_iov_cosmics = input_data["cosmic"]
75 
76  # Reduce data and create calibration instances for different data categories
77  cal_list = []
78  if max_events_per_run <= 0:
79  basf2.B2INFO(f"Reducing to a maximum of {max_files_per_run} files per run.")
80  reduced_file_to_iov_physics = filter_by_max_files_per_run(file_to_iov_physics,
81  max_files_per_run, min_events_per_file)
82  reduced_file_to_iov_cosmics = filter_by_max_files_per_run(file_to_iov_cosmics,
83  max_files_per_run, min_events_per_file)
84  else:
85  basf2.B2INFO(f"Reducing to a maximum of {max_events_per_run} events per run.")
86  reduced_file_to_iov_physics = filter_by_max_events_per_run(file_to_iov_physics,
87  max_events_per_run, random_select=True)
88  reduced_file_to_iov_cosmics = filter_by_max_events_per_run(file_to_iov_cosmics,
89  max_events_per_run, random_select=True)
90 
91  # Create run chunks based on exp no. and run type
92  iov_set_physics = set(reduced_file_to_iov_physics.values())
93  iov_set_cosmics = set(reduced_file_to_iov_cosmics.values())
94 
95  iov_list_cosmics = list(sorted(iov_set_cosmics))
96  # iov_list_physics = list(sorted(iov_set_physics))
97  iov_list_all = list(sorted(iov_set_cosmics | iov_set_physics))
98 
99  exp_set = set([iov.exp_low for iov in iov_list_all])
100  chunks_exp = []
101  for exp in sorted(exp_set):
102  chunks_exp += [list(g) for k, g in groupby(iov_list_all, lambda x: x.exp_low == exp) if k]
103 
104  chunks_phy = []
105  chunks_cosmic = []
106  for chunk_exp in chunks_exp:
107  chunks_phy += [list(g) for k, g in groupby(chunk_exp, lambda x: x in iov_list_cosmics) if not k]
108  chunks_cosmic += [list(g) for k, g in groupby(chunk_exp, lambda x: x in iov_list_cosmics) if k]
109 
110  # Create calibrations
111 
112  # Physics or beam run
113  chunk_list = chunks_phy
114  input_data = reduced_file_to_iov_physics
115  iCal = 0
116  for ichunk, chunk in enumerate(chunk_list):
117  first_iov = IoV(chunk[0].exp_low, chunk[0].run_low, -1, -1)
118  last_iov = IoV(chunk[-1].exp_low, chunk[-1].run_low, -1, -1)
119  if last_iov < output_iov: # All the chunk iovs are earlier than the requested
120  continue
121  else:
122  input_files = list(chain.from_iterable([list(g) for k, g in groupby(
123  input_data, lambda x: input_data[x] in chunk) if k]))
124  # Check the minimum number of files in the physics/beam run chunk
125  if len(input_files) < min_files_per_chunk:
126  continue
127  # From the second chunk within the requested range, we have the iov defined by the first run
128  specific_iov = first_iov if iCal > 0 else output_iov
129  basf2.B2INFO(f"Total number of files actually used as input = {len(input_files)} for the output {specific_iov}")
130  cal = hot_pixel_mask_calibration(
131  cal_name="{}_PXDHotPixelMaskCalibration_BeamorPhysics".format(iCal + 1),
132  input_files=input_files,
133  **cal_kwargs)
134  cal.algorithms[0].params = {"iov_coverage": specific_iov}
135  cal_list.append(cal)
136  iCal += 1
137 
138  # Cosmic run
139  nCal_phy = iCal
140  chunk_list = chunks_cosmic
141  input_data = reduced_file_to_iov_cosmics
142  for ichunk, chunk in enumerate(chunk_list):
143  first_iov = IoV(chunk[0].exp_low, chunk[0].run_low, chunk[-1].exp_high, chunk[-1].run_high)
144  last_iov = IoV(chunk[-1].exp_low, chunk[-1].run_low, chunk[-1].exp_high, chunk[-1].run_high)
145  if last_iov < output_iov: # All the chunk iovs are earlier than the requested
146  continue
147  # From the first chunk within the requested range, we have the iov defined by the first run
148  if iCal == nCal_phy:
149  specific_iov = max(first_iov, IoV(
150  requested_iov.exp_low, requested_iov.run_low, chunk[-1].exp_high, chunk[-1].run_high))
151  else:
152  specific_iov = first_iov
153  input_files = list(chain.from_iterable([list(g) for k, g in groupby(
154  input_data, lambda x: input_data[x] in chunk) if k]))
155  basf2.B2INFO(f"Total number of files actually used as input = {len(input_files)} for the output {specific_iov}")
156  cal = hot_pixel_mask_calibration(
157  cal_name="{}_PXDHotPixelMaskCalibration_Cosmic".format(iCal + 1),
158  input_files=input_files,
159  run_type='cosmic',
160  **cal_kwargs)
161  cal.algorithms[0].params = {"iov_coverage": specific_iov} # Not valid when using SimpleRunByRun strategy
162  cal_list.append(cal)
163  iCal += 1
164 
165  # The number of calibrations depends on the 'chunking' above. We would like to make sure that the total number of
166  # batch jobs submitted is approximately constant and reasonable, no matter how many files and chunks are used.
167  # So we define 1000 total jobs and split this between the calibrations depending on the fraction of total input files
168  # in the calibrations.
169 
170  total_jobs = 1000
171  total_input_files = len(reduced_file_to_iov_physics) + len(reduced_file_to_iov_cosmics)
172 
173  for cal in cal_list:
174  fraction_of_input_files = len(cal.input_files)/total_input_files
175  # Assign the max collector jobs to be roughly the same fraction of total jobs
176  cal.max_collector_jobs = ceil(fraction_of_input_files * total_jobs)
177  basf2.B2INFO(f"{cal.name} will submit a maximum of {cal.max_collector_jobs} batch jobs")
178 
179  return cal_list
pxd.calibration
Definition: __init__.py:1
prompt.utils
Definition: utils.py:1