Belle II Software development
foreach.py
1#!/usr/bin/env python3
2
3
10
11import sys
12import os
13import basf2
14from ROOT import Belle2
15from b2test_utils import skip_test_if_light
16
17skip_test_if_light() # light builds don't contain the particlegun
18basf2.set_random_seed("something important")
19
20path = basf2.Path()
21path.add_module('EventInfoSetter', evtNumList=[5, 1], runList=[0, 1], expList=[0, 1])
22pgun = path.add_module('ParticleGun', nTracks=3)
23
24
25class TestModule(basf2.Module):
26
27 """print some debug info"""
28
29 def event(self):
30 """reimplementation of Module::event()."""
31
32 part = Belle2.PyStoreObj('MCParticle')
33 basf2.B2INFO("MCPart: " + str(part.obj().getIndex()))
34
35 def beginRun(self):
36 """reimplementation of Module::beginRun()."""
37 basf2.B2INFO("TestModule: beginRun()")
38
39
40for use_pp in [False, True]:
41 if os.fork() == 0:
42 subeventpath = basf2.Path()
43 testmod = TestModule()
44 if use_pp:
45 testmod.set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
46 else:
47 subeventpath.add_module('EventInfoPrinter')
48 subeventpath.add_module(testmod)
49 # read: for each $objName in $arrayName run over $path
50 path.for_each('MCParticle', 'MCParticles', subeventpath)
51 path.add_module('PrintCollections', printForEvent=0)
52 if use_pp:
53 basf2.set_nprocesses(2)
54 basf2.logging.log_level = basf2.LogLevel.WARNING # suppress output
55 basf2.process(path)
56 else:
57 print(path)
58 basf2.process(path)
59 # print(statistics)
60 # initialize/terminate once
61 assert basf2.statistics.get(pgun).calls(basf2.statistics.INIT) == 1
62 assert basf2.statistics.get(testmod).calls(basf2.statistics.INIT) == 1
63 if not use_pp:
64 # for pp, term basf2.statistics are wrong, begin/end run are complicated
65 assert basf2.statistics.get(pgun).calls(basf2.statistics.TERM) == 1
66 assert basf2.statistics.get(testmod).calls(basf2.statistics.TERM) == 1
67 # 2 runs
68 assert basf2.statistics.get(pgun).calls(basf2.statistics.BEGIN_RUN) == 2
69 assert basf2.statistics.get(testmod).calls(basf2.statistics.BEGIN_RUN) == 2
70 assert basf2.statistics.get(pgun).calls(basf2.statistics.END_RUN) == 2
71 assert basf2.statistics.get(testmod).calls(basf2.statistics.END_RUN) == 2
72 # 6 events, a 3 particles
73 assert basf2.statistics.get(pgun).calls(basf2.statistics.EVENT) == 6
74 assert basf2.statistics.get(testmod).calls(basf2.statistics.EVENT) == 3 * 6
75
76 sys.exit(0)
77 retbytes = os.wait()[1]
78 assert retbytes == 0
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def beginRun(self)
Definition: foreach.py:35
def event(self)
Definition: foreach.py:29