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