Belle II Software light-2406-ragdoll
dowhile.py
1#!/usr/bin/env python3
2
3
10
11from basf2 import create_path, LogLevel, LogInfo, logging, set_random_seed, B2INFO, Module
12import b2test_utils
13from ROOT import Belle2, gRandom
14
15
16class 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.eventInfo = 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.eventInfo.getGeneratedWeight()
28 B2INFO(f"Current Iteration: {weight}")
29 rndm = [f"{gRandom.Rndm():.4f}" for i in range(5)]
30 B2INFO(f"First 5 random numbers: {rndm}")
31 weight += 1
32 self.eventInfo.setGeneratedWeight(weight)
33 self.return_value(weight > self.eventInfo.getRun())
34
35
37# fixed random seed
38set_random_seed("something important")
39# simplify logging output to just the type and the message
40for 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
44logging.enable_summary(False)
45
46path = create_path()
47path.add_module('EventInfoSetter', evtNumList=[2, 2, 1], runList=[0, 3, 5], expList=[0, 1, 3])
48path.add_module('EventInfoPrinter')
49subpath = create_path()
50test_module = subpath.add_module(TestDoWhile())
51path.do_while(subpath, max_iterations=3)
53
54# make sure that conditions on the last module are not accepted
55test_module.if_true(create_path())
56b2test_utils.run_in_subprocess(target=path.do_while, path=subpath)
57# and also make sure empty paths are rejected
58b2test_utils.run_in_subprocess(target=path.do_while, path=create_path())
59
60# Try to loop over module not setting return value
61path = create_path()
62path.add_module('EventInfoSetter')
63subpath = create_path()
64subpath.add_module("EventInfoPrinter")
65path.do_while(subpath)
67
68# Loop over a path with a condition leaving the loop should be rejected ... it
69# just complicates things ...
70path = create_path()
71path.add_module("EventInfoSetter", evtNumList=5)
72subpath = create_path()
73subpath.add_module("EventInfoPrinter")
74p1 = subpath.add_module("Prescale", prescale=0.9)
75p1.if_true(create_path())
76subpath.add_module("Prescale", prescale=0.2)
77b2test_utils.run_in_subprocess(target=path.do_while, path=subpath)
78
79# Do a sub path with a prescale module for looping
80path = create_path()
81path.add_module('EventInfoSetter', evtNumList=5)
82subpath = create_path()
83subpath.add_module("EventInfoPrinter")
84subpath.add_module("Prescale", prescale=0.2)
85path.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