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