Belle II Software  release-08-01-10
dowhile.py
1 #!/usr/bin/env python3
2 
3 
10 
11 from basf2 import create_path, LogLevel, LogInfo, logging, set_random_seed, B2INFO, Module
12 import b2test_utils
13 from ROOT import Belle2, gRandom
14 
15 
16 class TestDoWhile(Module):
17  """Small test module to print something in Path.doWhile"""
18 
19  def __init__(self):
20  """Remember how many iterations we want"""
21  super().__init__()
22 
23  self.eventInfoeventInfo = Belle2.PyStoreObj("EventMetaData")
24 
25  def event(self):
26  """Print the current iteration for this event as well as the first five random numbers"""
27  weight = self.eventInfoeventInfo.getGeneratedWeight()
28  B2INFO(f"Current Iteration: {weight}")
29  rndm = ["%.4f" % gRandom.Rndm() for i in range(5)]
30  B2INFO(f"First 5 random numbers: {rndm}")
31  weight += 1
32  self.eventInfoeventInfo.setGeneratedWeight(weight)
33  self.return_value(weight > self.eventInfoeventInfo.getRun())
34 
35 
37 # fixed random seed
38 set_random_seed("something important")
39 # simplify logging output to just the type and the message
40 for level in LogLevel.values.values():
41  logging.set_info(level, LogInfo.LEVEL | LogInfo.MESSAGE)
42 # disable error summary, we don't need it for these short tests and it basically
43 # doubles the output
44 logging.enable_summary(False)
45 
46 path = create_path()
47 path.add_module('EventInfoSetter', evtNumList=[2, 2, 1], runList=[0, 3, 5], expList=[0, 1, 3])
48 path.add_module('EventInfoPrinter')
49 subpath = create_path()
50 test_module = subpath.add_module(TestDoWhile())
51 path.do_while(subpath, max_iterations=3)
53 
54 # make sure that conditions on the last module are not accepted
55 test_module.if_true(create_path())
56 b2test_utils.run_in_subprocess(target=path.do_while, path=subpath)
57 # and also make sure empty paths are rejected
58 b2test_utils.run_in_subprocess(target=path.do_while, path=create_path())
59 
60 # Try to loop over module not setting return value
61 path = create_path()
62 path.add_module('EventInfoSetter')
63 subpath = create_path()
64 subpath.add_module("EventInfoPrinter")
65 path.do_while(subpath)
67 
68 # Loop over a path with a condition leaving the loop should be rejected ... it
69 # just complicates things ...
70 path = create_path()
71 path.add_module("EventInfoSetter", evtNumList=5)
72 subpath = create_path()
73 subpath.add_module("EventInfoPrinter")
74 p1 = subpath.add_module("Prescale", prescale=0.9)
75 p1.if_true(create_path())
76 subpath.add_module("Prescale", prescale=0.2)
77 b2test_utils.run_in_subprocess(target=path.do_while, path=subpath)
78 
79 # Do a sub path with a prescale module for looping
80 path = create_path()
81 path.add_module('EventInfoSetter', evtNumList=5)
82 subpath = create_path()
83 subpath.add_module("EventInfoPrinter")
84 subpath.add_module("Prescale", prescale=0.2)
85 path.do_while(subpath)
87 
88 
90  # Ok, test pickling and loading the do_while(): Setting the PicklePath to a
91  # non-empty value means that running process(path) will create the pickle file
92  # and running process(None) will process the path in the pickle file
93  env.setPicklePath("testpath.pkl")
95  # lets add a new module just to make sure that the pickled path is executed,
96  # not just the path we have here
97  path.add_module("EventInfoPrinter")
98  # and then load and execute the pickled path
100  env.setPicklePath("")
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
eventInfo
use the event meta info to store the iterations
Definition: dowhile.py:23
def __init__(self)
Definition: dowhile.py:19
def event(self)
Definition: dowhile.py:25
def run_in_subprocess(*args, target, **kwargs)
Definition: __init__.py:221
def clean_working_directory()
Definition: __init__.py:189
def safe_process(*args, **kwargs)
Definition: __init__.py:236
static ExpRun getRun(std::map< ExpRun, std::pair< double, double >> runs, double t)
Get exp number + run number from time.
Definition: Splitter.cc:262