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