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