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