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