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