Belle II Software light-2406-ragdoll
module_paths.py
1#!/usr/bin/env python3
2
3
10
11import basf2 # also test non-polluting import
12from ROOT import Belle2
13
14basf2.set_random_seed("something important")
15
16
17class SelectOddEvents(basf2.Module):
18
19 """For events with an odd event number, set module return value to False"""
20
21 def event(self):
22 """reimplementation of Module::event()."""
23
24 evtmetadata = Belle2.PyStoreObj('EventMetaData')
25 if not evtmetadata:
26 basf2.B2ERROR('No EventMetaData found')
27 else:
28 event = evtmetadata.obj().getEvent()
29 basf2.B2INFO('Setting return value to ' + str(event % 2 == 0))
30 self.return_value(event % 2 == 0)
31
32 def terminate(self):
33 """reimplementation of Module::terminate()."""
34
35 basf2.B2INFO('terminating SelectOddEvents')
36
37
38class ReturnFalse(basf2.Module):
39
40 """Always return false"""
41
42 def event(self):
43 """reimplementation of Module::event()."""
44 self.return_value(False)
45
46
47class PrintName(basf2.Module):
48
49 """Print name in event"""
50
51 def __init__(self, name):
52 """constructor."""
53
54 super().__init__()
55 self.set_name(name)
56
57 def event(self):
58 """reimplementation of Module::event()."""
59 # error level to check that this doesn't prevent execution
60 basf2.B2ERROR("In module " + self.name())
61
62
63main = basf2.create_path()
64
65# register necessary modules
66
67emptypath = basf2.create_path()
68emptypath.add_path(basf2.create_path())
69main.add_path(emptypath)
70
71# generate three events
72main.add_module('EventInfoSetter', expList=[0, 1], runList=[1, 2], evtNumList=[2, 1])
73
74anotherpath = basf2.create_path()
75main.add_path(anotherpath) # added here, filled later
76main.add_module('PrintCollections')
77
78subsubpath = basf2.create_path()
79subsubpath.add_module('Progress')
80subsubpath.add_path(emptypath)
81
82# fill anotherpath now
83module_with_condition = SelectOddEvents()
84anotherpath.add_module(module_with_condition)
85anotherpath.add_module('EventInfoPrinter')
86
87# check printing of paths, should be:
88# [] -> eventinfosetter -> [SelectOddEvents -> eventinfo]
89# -> printcollections
90print(main)
91
92# when the module returns true/1 (even events), we jump to Progress instead:
93# [] -> eventinfosetter -> [SelectOddEvents -> [Progress -> []]]
94module_with_condition.if_true(subsubpath)
95# this is equivalent to:
96# module_with_condition.if_value('<1', subsubpath)
97
98# test continuing after conditional path
99returnfalse1 = ReturnFalse()
100returnfalse1_condition_path = basf2.create_path()
101returnfalse1_condition_path.add_module(PrintName('ReturnFalse1Condition'))
102returnfalse1.if_false(returnfalse1_condition_path, basf2.AfterConditionPath.CONTINUE)
103main.add_module(returnfalse1)
104
105# test more complicated conditions (in this case, it is never met)
106returnfalse2 = ReturnFalse()
107returnfalse2_condition_path = basf2.create_path()
108returnfalse2_condition_path.add_module(PrintName('ReturnFalse2Condition'))
109returnfalse2.if_true(returnfalse2_condition_path)
110
111main.add_module(PrintName("final"))
112
113basf2.process(main)
114
115basf2.B2INFO("second process() call follows...")
116basf2.process(main)
117
118print(main)
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def __init__(self, name)
Definition: module_paths.py:51