Belle II Software  release-05-01-25
iov_conditional.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 iov_conditional - Functions to Execute Paths Depending on Experiment Phases
5 ===========================================================================
6 
7 This module contains some convenience functions to execute different paths
8 depending on the experiment run number. This allows to write one common steering
9 file for phases 2 and 3.
10 
11 Warning:
12  All Modules will be initialized on startup of the framework. This might
13  cause problems if the modules initialize some common global state. For
14  example adding :b2:mod:`Geometry` modules with different parameters will not
15  work as expected.
16 """
17 import basf2
18 
19 PHASE2_IOVS = [(1, 0, 4, -1), (1002, 0, 1002, -1)]
20 
21 
22 def make_conditional_at(path, iov_list, path_when_in_iov, path_when_not_in_iov=None):
23  """
24  Branch the current path execution based on the given iov list. If the
25  current exp/run is met by one of the IoV conditions, execute the path given
26  in ``path_when_in_iov`` and continue the normal path afterwards. Otherwise
27  execute the ``path_when_not_in_iov`` when given.
28 
29  Basically, this function is just a wrapper around adding the
30  :b2:mod:`IoVDependentCondition` module to the path.
31 
32  See Also:
33  If you just need to distinguish between phase 2/3 please use
34  `phase_2_conditional()`
35 
36  Example:
37 
38  >>> make_conditional_at(path, iov_list=[(0, 0, 0, -1)],
39  ... path_when_in_iov=exp_0_path,
40  ... path_when_not_in_iov=not_exp_0_path)
41 
42  will branch the path for all events with experiment number 0 to the modules
43  in ``exp_0_path`` and into ``not_exp_0_path`` in all other cases.
44 
45  Parameters:
46  path (basf2.Path): Branch the execution of the given path.
47  iov_list (list(tuple)): Under which IoV conditions should the
48  ``path_when_in_iov`` be executed. It should be a list in the form
49  ``[(min exp, min run, max exp, max run), ...]``
50  path_when_in_iov (basf2.Path): Which branch to execute, if one of the IoV conditions is met.
51  path_when_not_in_iov (basf2.Path): If given, execute this path in all cases, none IoV condition is met.
52  """
53  condition_module = path.add_module("IoVDependentCondition", iovList=iov_list)
54  condition_module.if_true(path_when_in_iov, basf2.AfterConditionPath.CONTINUE)
55  if path_when_not_in_iov:
56  condition_module.if_false(path_when_not_in_iov, basf2.AfterConditionPath.CONTINUE)
57 
58 
59 def phase_2_conditional(path, phase2_path, phase3_path=None):
60  """
61  Handy shortcut for phase 2/3 conditional module execution.
62 
63  See Also:
64  `make_conditional_at()`
65 
66  Parameters:
67  path (basf2.Path): Branch the execution of the given path.
68  phase2_path (basf2.Path): Call this path only when in phase 2.
69  phase3_path (basf2.Path): When given, call this path only when in phase 3.
70  """
71  make_conditional_at(path=path, iov_list=PHASE2_IOVS,
72  path_when_in_iov=phase2_path, path_when_not_in_iov=phase3_path)