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