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