Belle II Software  release-08-01-10
module_paths.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2 # also test non-polluting import
12 from ROOT import Belle2
13 
14 basf2.set_random_seed("something important")
15 
16 
17 class 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 
38 class 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 
47 class 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 
63 main = basf2.create_path()
64 
65 # register necessary modules
66 
67 emptypath = basf2.create_path()
68 emptypath.add_path(basf2.create_path())
69 main.add_path(emptypath)
70 
71 # generate three events
72 main.add_module('EventInfoSetter', expList=[0, 1], runList=[1, 2], evtNumList=[2, 1])
73 
74 anotherpath = basf2.create_path()
75 main.add_path(anotherpath) # added here, filled later
76 main.add_module('PrintCollections')
77 
78 subsubpath = basf2.create_path()
79 subsubpath.add_module('Progress')
80 subsubpath.add_path(emptypath)
81 
82 # fill anotherpath now
83 module_with_condition = SelectOddEvents()
84 anotherpath.add_module(module_with_condition)
85 anotherpath.add_module('EventInfoPrinter')
86 
87 # check printing of paths, should be:
88 # [] -> eventinfosetter -> [SelectOddEvents -> eventinfo]
89 # -> printcollections
90 print(main)
91 
92 # when the module returns true/1 (even events), we jump to Progress instead:
93 # [] -> eventinfosetter -> [SelectOddEvents -> [Progress -> []]]
94 module_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
99 returnfalse1 = ReturnFalse()
100 returnfalse1_condition_path = basf2.create_path()
101 returnfalse1_condition_path.add_module(PrintName('ReturnFalse1Condition'))
102 returnfalse1.if_false(returnfalse1_condition_path, basf2.AfterConditionPath.CONTINUE)
103 main.add_module(returnfalse1)
104 
105 # test more complicated conditions (in this case, it is never met)
106 returnfalse2 = ReturnFalse()
107 returnfalse2_condition_path = basf2.create_path()
108 returnfalse2_condition_path.add_module(PrintName('ReturnFalse2Condition'))
109 returnfalse2.if_true(returnfalse2_condition_path)
110 
111 main.add_module(PrintName("final"))
112 
113 basf2.process(main)
114 
115 basf2.B2INFO("second process() call follows...")
116 basf2.process(main)
117 
118 print(main)
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def __init__(self, name)
Definition: module_paths.py:51