Belle II Software development
caf_multiple_options.py
1
12
13import basf2 as b2
14
15import os
16import sys
17
18from caf.framework import Calibration, CAF
19from caf import backends
20
21b2.set_log_level(b2.LogLevel.INFO)
22
23
24def main(argv):
25 if len(argv) == 1:
26 data_dir = argv[0]
27 else:
28 print("Usage: python3 caf_multiple_options.py <data directory>")
29 sys.exit(1)
30
31
35 input_files_test = [os.path.join(os.path.abspath(data_dir), '*.root')]
36
37
39
40 # Define a function to be performed before the algorithm.execute(iov)
41 def pre_alg_test(algorithm, iteration):
42 """
43 Just to show that the function is correctly applied
44 """
45 b2.set_log_level(b2.LogLevel.DEBUG)
46 b2.B2INFO(f"Running Test Algorithm Setup For Iteration {iteration}")
47 b2.B2INFO(f"Can access the {algorithm.__cppname__} class from Calibration().pre_algorithms.")
48
49 from ROOT import Belle2 # noqa: make the Belle2 namespace available
50 from ROOT.Belle2 import TestCalibrationAlgorithm
51
52 # Make a bunch of test calibrations
53 calibrations = []
54 for i in range(1, 5):
55 col_test = b2.register_module('CaTest')
56 col_test.set_name(f'Test{i}') # Sets the prefix of the collected data in the datastore.
57 # Allows us to execute algorithm over all input data, in one big IoV.
58 col_test.param('granularity', 'all')
59 # Specific parameter to our test collector, proportional to the probability of algorithm requesting iteration.
60 col_test.param('spread', 15)
61
62 alg_test = TestCalibrationAlgorithm()
63 # Since we're using several instances of the same test algorithm here, we still want the database entries to have
64 # different names. TestCalibrationAlgorithm outputs to the database using the prefix name so we change it
65 # slightly for each calibration. Not something you'd usually have to do.
66 alg_test.setPrefix(f'Test{i}') # Must be the same as colllector prefix
67 alg_test.setDebugHisto(True)
68
69 cal_test = Calibration(name=f'TestCalibration{i}',
70 collector=col_test,
71 algorithms=alg_test,
72 input_files=input_files_test)
73 cal_test.pre_algorithms = pre_alg_test
74
75 cal_test.max_files_per_collector_job = 1
76 cal_test.max_iterations = 5 # Each calibration will end iteration after this many attempts (if reached)
77 # If you have some local databases or want to override the default global tag for this calibration you can do that
78 # with these functions
79# cal_test.use_local_database("mylocaldb/database.txt")
80# cal_test.use_central_database("BelleII_GlobalTag_Tutorial")
81 calibrations.append(cal_test)
82
83 # Define dependencies. In this case the classic diamond e.g.
84 # 2
85 # / \
86 # 1 4
87 # \ /
88 # 3
89 calibrations[1].depends_on(calibrations[0])
90 calibrations[2].depends_on(calibrations[0])
91 calibrations[3].depends_on(calibrations[2])
92 calibrations[3].depends_on(calibrations[1])
93
94
96 cal_fw = CAF()
97 # Add in our list of calibrations
98 for cal in calibrations:
99 cal_fw.add_calibration(cal)
100 # Subjobs from collector jobs being split over input files can be parallelized.
101 # Also Calibrations 1 and 2, can be run at the same time.
102 # If you have 4 cores this backend will run them whenever one of the 4 processes becomes available
103 # For larger data or more calibrations, consider using the LSF or PBS batch system backends at your site
104 cal_fw.backend = backends.Local(max_processes=4)
105 cal_fw.output_dir = 'cal_test_results' # Can change where your calibration runs
106 # Start her up!
107 cal_fw.run()
108 print("End of CAF processing.")
109
110
111if __name__ == "__main__":
112 main(sys.argv[1:])
Definition: main.py:1