forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_read_data_handle.py
More file actions
157 lines (118 loc) · 5.28 KB
/
test_read_data_handle.py
File metadata and controls
157 lines (118 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pytest
import acts
import acts.examples
u = acts.UnitConstants
hepmc3 = pytest.importorskip("acts.examples.hepmc3")
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
class _Noop(acts.examples.IAlgorithm):
"""Minimal algorithm whose only purpose is to serve as a handle parent."""
def __init__(self):
acts.examples.IAlgorithm.__init__(self, "_Noop", acts.logging.WARNING)
def execute(self, context):
return acts.examples.ProcessCode.SUCCESS
def _make_sequencer(events=3):
"""Sequencer with a particle-gun → HepMC3-converter chain."""
rng = acts.examples.RandomNumbers(seed=42)
s = acts.examples.Sequencer(events=events, numThreads=1, logLevel=acts.logging.INFO)
evGen = acts.examples.EventGenerator(
level=acts.logging.WARNING,
generators=[
acts.examples.EventGenerator.Generator(
multiplicity=acts.examples.FixedMultiplicityGenerator(n=1),
vertex=acts.examples.GaussianVertexGenerator(
stddev=acts.Vector4(0, 0, 0, 0),
mean=acts.Vector4(0, 0, 0, 0),
),
particles=acts.examples.ParametricParticleGenerator(
p=(1 * u.GeV, 10 * u.GeV),
eta=(-2, 2),
numParticles=4,
),
)
],
outputEvent="particle_gun_event",
randomNumbers=rng,
)
s.addReader(evGen)
converter = hepmc3.HepMC3InputConverter(
level=acts.logging.WARNING,
inputEvent="particle_gun_event",
outputParticles="particles_generated",
outputVertices="vertices_generated",
mergePrimaries=False,
)
s.addAlgorithm(converter)
return s
# ---------------------------------------------------------------------------
# Unit-level tests (no sequencer run needed)
# ---------------------------------------------------------------------------
def test_unregistered_type_raises():
"""Constructing a handle for a type without a whiteboard registration fails."""
dummy = _Noop()
with pytest.raises(TypeError, match="not registered"):
acts.examples.ReadDataHandle(dummy, acts.examples.IAlgorithm, "test")
def test_not_initialized_raises():
"""Calling a handle before initialize() raises RuntimeError."""
dummy = _Noop()
handle = acts.examples.ReadDataHandle(
dummy, acts.examples.SimParticleContainer, "InputParticles"
)
wb = acts.examples.WhiteBoard(acts.logging.WARNING)
with pytest.raises(RuntimeError, match="not initialized"):
handle(wb)
def test_wrong_key_raises():
class WrongKeyInspector(acts.examples.IAlgorithm):
def __init__(self):
super().__init__(name="WrongKeyInspector", level=acts.logging.INFO)
self.particles = acts.examples.ReadDataHandle(
self, acts.examples.SimParticleContainer, "InputParticles"
)
self.particles.initialize("wrong_key")
def execute(self, context):
return acts.examples.ProcessCode.SUCCESS
s = _make_sequencer(events=3)
inspector = WrongKeyInspector()
with pytest.raises(KeyError, match="does not exist"):
wb = acts.examples.WhiteBoard(acts.logging.WARNING)
inspector.particles(wb)
with acts.logging.ScopedFailureThreshold(acts.logging.FATAL), pytest.raises(
RuntimeError,
match="Sequence configuration error: Missing data handle for key 'wrong_key'",
):
s.addAlgorithm(inspector)
# ---------------------------------------------------------------------------
# Integration test
# ---------------------------------------------------------------------------
def test_read_particles_via_handle():
"""A Python IAlgorithm reads SimParticleContainer from the whiteboard."""
class ParticleInspector(acts.examples.IAlgorithm):
def __init__(self):
super().__init__(name="ParticleInspector", level=acts.logging.INFO)
self.particles = acts.examples.ReadDataHandle(
self, acts.examples.SimParticleContainer, "InputParticles"
)
self.particles.initialize("particles_generated")
self.outputParticles = acts.examples.WriteDataHandle(
self, acts.examples.SimParticleContainer, "OutputParticles"
)
self.outputParticles.initialize("output_particles")
self.seen_events = 0
def execute(self, context):
particles = self.particles(context.eventStore)
assert isinstance(particles, acts.examples.SimParticleContainer)
self.logger.info("Found {} particles", len(particles))
for particle in particles:
print(particle)
assert not context.eventStore.exists("output_particles")
newParticles = acts.examples.SimParticleContainer()
self.outputParticles(context, newParticles)
assert context.eventStore.exists("output_particles")
self.seen_events += 1
return acts.examples.ProcessCode.SUCCESS
s = _make_sequencer(events=3)
inspector = ParticleInspector()
s.addAlgorithm(inspector)
s.run()
assert inspector.seen_events == 3