Belle II Software  release-05-01-25
caf_ignoring_runs_sequential.py
1 # This steering file shows off how you can ignore runs from the input data of a
2 # Calibration. And how the SequentialRunByRun strategy deals with gaps in the input data
3 
4 import basf2 as b2
5 
6 import os
7 import sys
8 
9 from ROOT.Belle2 import TestCalibrationAlgorithm
10 
11 from caf.framework import Calibration, CAF
12 from caf.utils import ExpRun, IoV
13 from caf.strategies import SequentialRunByRun
14 from caf.backends import Local
15 
16 b2.set_log_level(b2.LogLevel.INFO)
17 
18 
19 def main(argv):
20  if len(argv) == 1:
21  data_dir = argv[0]
22  else:
23  print("Usage: python3 caf_ignoring_runs.py <data directory>")
24  sys.exit(1)
25 
26 
29  input_files_test = [os.path.join(os.path.abspath(data_dir), '*.root')]
30 
31 
33 
34  # Make a bunch of test calibrations
35  col_test = b2.register_module('CaTest')
36  # Specific parameter to our test collector, proportional to the probability of algorithm requesting iteration.
37  col_test.param('spread', 15)
38 
39  alg_test = TestCalibrationAlgorithm()
40  # We want to see what happens when c_NotEnoughData is returned
41  alg_test.setMinEntries(10000)
42 
43  cal_test = Calibration(name='TestCalibration',
44  collector=col_test,
45  algorithms=alg_test,
46  input_files=input_files_test)
47 
48  # We don't want to run collector jobs on these runs (if possible) and we don't want the algorithm to use data from
49  # these runs. However, the algorithm strategy may also merge IoVs across the gaps left by the ignored runs.
50  # What the strategy does with missing runs depends on the AlgorithmStrategy and any configuration you have made.
51  cal_test.ignored_runs = [ExpRun(0, 2), ExpRun(0, 3)]
52 
53  # We use a different algorithm strategy for every algorithm (only one) in this Calibration
54  cal_test.strategies = SequentialRunByRun
55 
56  # This parameter is used by SequentialRunByRun to define a total IoV that the combination of all IoVs defines.
57  # It must be strictly larger than the IoV of all input data, OR the IoV in cal_fw.run(iov=...) if defined.
58  # This allows us to define a larger total IoV so that the first payload IoV MUST START at the ExpRun(exp_low, run_low)
59  # and the final IoV MUST END at ExpRun(exp_high, run_high) e.g.
60  #
61  # Here we define iov_coverage = IoV(0, 1, 0, 15). This means that the first payload created will be of the form
62  # IoV(0, 1, ?, ?) where we don't yet know what the highest (exp,run) will be as it depends on the data.
63  # The last payload we create must have the form IoV(?, ?, 0, 15) but we can't yet know the lowest (exp,run)
64  #
65  # It is entirely possible that in the final database there will be only one payload of IoV(0, 1, 0, 15), or
66  # several such as [IoV(0, 1, 0, 5), IoV(0, 6, 0, 10), IoV(0, 11, 0, 15)]
67  cal_test.algorithms[0].params["iov_coverage"] = IoV(0, 1, 0, 15)
68 
69  # This parameter defines the step size of the SequentialRunByRun strategy i.e. how many runs of data we add
70  # each time we execute.
71  # So initially an execution of the algorithm will use this many runs-worth of data (if possible).
72  # If c_NotEnoughData is returned on this execution, we will add the next 'step_size' number of runs and execute again.
73  # Then repeat until we get c_Iterate or c_OK.
74  #
75  # The default value is 1.
76  cal_test.algorithms[0].params["step_size"] = 1
77 
78  cal_test.max_files_per_collector_job = 1
79 
80 
82  cal_fw = CAF()
83 
84  # You could alternatively set the same ignored_runs for every Calibration by setting it here.
85  # Note that setting cal_test.ignored_runs will override the value set from here.
86 
87  # cal_fw = CAF(calibration_defaults={'ignored_runs':[ExpRun(0,2), ExpRun(0, 3)])
88 
89  cal_fw.add_calibration(cal_test)
90  cal_fw.backend = Local(max_processes=4)
91 
92  # The iov value here allows you to set an IoV that all your input files/executed runs must overlap.
93  # Any input files not overlapping this IoV will be ignored.
94  # HOWEVER, if you have an input file containing multiple runs the file IoV may overlap this IoV. But it may still
95  # contain runs outside of it.
96  # In this case the CAF will still use the file in collector jobs, BUT any runs collected will not be used in the
97  # algorithm step if they exist outside of this IoV.
98  #
99  # To explicitly prevent certain runs from within this IoV being used, you should use the ignored_runs
100  # attribute of the Calibration. Or make sure that the input data files do not contain data from those runs at all.
101  cal_fw.run(iov=IoV(0, 3, 0, 11))
102  print("End of CAF processing.")
103 
104 
105 if __name__ == "__main__":
106  main(sys.argv[1:])
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Calibration
Definition: Calibration.py:1