Belle II Software  release-05-02-19
example_simple.py
1 # -*- coding: utf-8 -*-
2 
3 """A simple example calibration that takes one input data list from raw data and performs
4 a single calibration."""
5 
6 from prompt import CalibrationSettings, input_data_filters
7 
8 
15 
16 
17 settings = CalibrationSettings(name="Example Simple",
18  expert_username="ddossett",
19  description=__doc__,
20  input_data_formats=["raw"],
21  input_data_names=["physics"],
22  input_data_filters={"physics": [f"NOT {input_data_filters['Magnet']['On']}",
23  input_data_filters["Data Tag"]["hadron_calib"],
24  input_data_filters["Data Quality Tag"]["Good"],
25  input_data_filters["Beam Energy"]["4S"],
26  input_data_filters["Run Type"]["physics"]]},
27  depends_on=[],
28  expert_config={})
29 
30 
31 
32 
40 
41 
42 def get_calibrations(input_data, **kwargs):
43  """
44  Parameters:
45  input_data (dict): Should contain every name from the 'input_data_names' variable as a key.
46  Each value is a dictionary with {"/path/to/file_e1_r5.root": IoV(1,5,1,5), ...}. Useful for
47  assigning to calibration.files_to_iov
48 
49  **kwargs: Configuration options to be sent in. Since this may change we use kwargs as a way to help prevent
50  backwards compatibility problems. But you could use the correct arguments in b2caf-prompt-run for this
51  release explicitly if you want to.
52 
53  Currently only kwargs["requested_iov"] and kwargs["expert_config"] are used.
54 
55  "requested_iov" is the IoV range of the bucket and your payloads should correspond to this range.
56  However your highest payload IoV should be open ended e.g. IoV(3,4,-1,-1)
57 
58  "expert_config" is the input configuration. It takes default values from your `CalibrationSettings` but these are
59  overwritten by values from the 'expert_config' key in your input `caf_config.json` file when running ``b2caf-prompt-run``.
60 
61  Returns:
62  list(caf.framework.Calibration): All of the calibration objects we want to assign to the CAF process
63  """
64  import basf2
65  # Set up config options
66 
67  # In this script we want to use one sources of input data.
68  # Get the input files from the input_data variable
69  file_to_iov_physics = input_data["physics"]
70 
71  # We might have requested an enormous amount of data across a run range.
72  # There's a LOT more files than runs!
73  # Lets set some limits because this calibration doesn't need that much to run.
74  max_files_per_run = 2
75 
76  # If you are using Raw data there's a chance that input files could have zero events.
77  # This causes a B2FATAL in basf2 RootInput so the collector job will fail.
78  # Currently we don't have a good way of filtering this on the automated side, so we can check here.
79  min_events_per_file = 1
80 
81  # We filter out any more than 2 files per run. The input data files are sorted alphabetically by b2caf-prompt-run
82  # already. This procedure respects that ordering
83  from prompt.utils import filter_by_max_files_per_run
84 
85  reduced_file_to_iov_physics = filter_by_max_files_per_run(file_to_iov_physics, max_files_per_run, min_events_per_file)
86  input_files_physics = list(reduced_file_to_iov_physics.keys())
87  basf2.B2INFO(f"Total number of files actually used as input = {len(input_files_physics)}")
88 
89  # Get the overall IoV we our process should cover. Includes the end values that we may want to ignore since our output
90  # IoV should be open ended. We could also use this as part of the input data selection in some way.
91  requested_iov = kwargs.get("requested_iov", None)
92 
93  # Get the expert configurations if you have something you might configure from them. It should always be available
94  # expert_config = kwargs.get("expert_config")
95 
96  from caf.utils import IoV
97  # The actual value our output IoV payload should have. Notice that we've set it open ended.
98  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
99 
100 
102 
103  from ROOT.Belle2 import TestCalibrationAlgorithm
104 
105  alg_test = TestCalibrationAlgorithm()
106 
107 
109 
110  from caf.framework import Calibration
111 
112  cal_test = Calibration("TestCalibration_physics",
113  collector="CaTest",
114  algorithms=[alg_test],
115  input_files=input_files_physics
116  )
117  # Do this for the default AlgorithmStrategy to force the output payload IoV
118  # It may be different if you are using another strategy like SequentialRunByRun
119  for algorithm in cal_test.algorithms:
120  algorithm.params = {"apply_iov": output_iov}
121 
122  # Most other options like database chain and backend args will be overwritten by b2caf-prompt-run.
123  # So we don't bother setting them.
124 
125  # You must return all calibrations you want to run in the prompt process, even if it's only one
126  return [cal_test]
127 
128 
prompt.utils
Definition: utils.py:1
Calibration
Definition: Calibration.py:1