DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
fddetdataformats
scripts
DAPHNEEth_binding_test.py
Go to the documentation of this file.
1
#!/usr/bin/env python3
2
3
import
sys
4
from
fddetdataformats
import
DAPHNEEthFrame, DAPHNEEthHeader
5
6
7
NUM_CHANNELS = 1
8
ADCS_PER_CHANNEL = 1024
9
ADC_MAX = (1 << 14) - 1
10
11
12
def
test_construction_and_size
() -> int:
13
frame = DAPHNEEthFrame()
14
if
frame
is
None
:
15
print(
"FAIL: DAPHNEEthFrame default construction returned None"
)
16
return
1
17
size = DAPHNEEthFrame.sizeof()
18
if
size <= 0:
19
print(f
"FAIL: DAPHNEEthFrame.sizeof() returned {size}, expected > 0"
)
20
return
1
21
print(f
"PASS: DAPHNEEthFrame construction and sizeof={size}"
)
22
return
0
23
24
25
def
test_static_members
() -> int:
26
if
DAPHNEEthFrame.version != 1:
27
print(f
"FAIL: DAPHNEEthFrame.version expected 1, got {DAPHNEEthFrame.version}"
)
28
return
1
29
if
DAPHNEEthFrame.s_num_adcs != ADCS_PER_CHANNEL:
30
print(
31
"FAIL: DAPHNEEthFrame.s_num_adcs expected "
32
f
"{ADCS_PER_CHANNEL}, got {DAPHNEEthFrame.s_num_adcs}"
33
)
34
return
1
35
if
DAPHNEEthFrame.s_bits_per_adc != 14:
36
print(f
"FAIL: DAPHNEEthFrame.s_bits_per_adc expected 14, got {DAPHNEEthFrame.s_bits_per_adc}"
)
37
return
1
38
if
DAPHNEEthFrame.s_expected_bytes != DAPHNEEthFrame.sizeof():
39
print(f
"FAIL: DAPHNEEthFrame.s_expected_bytes {DAPHNEEthFrame.s_expected_bytes} != sizeof {DAPHNEEthFrame.sizeof()}"
)
40
return
1
41
if
DAPHNEEthHeader.s_expected_bytes <= 0:
42
print(f
"FAIL: DAPHNEEthHeader.s_expected_bytes invalid: {DAPHNEEthHeader.s_expected_bytes}"
)
43
return
1
44
print(
"PASS: DAPHNEEthFrame/DAPHNEEthHeader static members"
)
45
return
0
46
47
48
def
test_bytes_roundtrip
() -> int:
49
frame = DAPHNEEthFrame()
50
frame.set_timestamp(0x112233445566)
51
frame.set_channel(9)
52
raw = frame.get_bytes()
53
if
len(raw) != DAPHNEEthFrame.sizeof():
54
print(f
"FAIL: get_bytes() length {len(raw)} != sizeof() {DAPHNEEthFrame.sizeof()}"
)
55
return
1
56
57
clone = DAPHNEEthFrame(raw)
58
if
clone.get_timestamp() != 0x112233445566:
59
print(f
"FAIL: bytes round-trip timestamp mismatch: got {clone.get_timestamp():#x}"
)
60
return
1
61
if
clone.get_channel() != 9:
62
print(f
"FAIL: bytes round-trip channel mismatch: got {clone.get_channel()}"
)
63
return
1
64
65
print(
"PASS: DAPHNEEthFrame bytes constructor roundtrip"
)
66
return
0
67
68
69
def
test_less_than_operator
() -> int:
70
earlier = DAPHNEEthFrame()
71
later = DAPHNEEthFrame()
72
earlier.set_timestamp(100)
73
later.set_timestamp(200)
74
if
not
(earlier < later):
75
print(
"FAIL: __lt__ expected earlier < later"
)
76
return
1
77
if
later < earlier:
78
print(
"FAIL: __lt__ expected later !< earlier"
)
79
return
1
80
print(
"PASS: __lt__ ordering by timestamp/channel tuple"
)
81
return
0
82
83
84
def
test_timestamp
() -> int:
85
frame = DAPHNEEthFrame()
86
test_ts = 0xAABBCCDDEEFF
87
frame.set_timestamp(test_ts)
88
got = frame.get_timestamp()
89
if
got != test_ts:
90
print(f
"FAIL: set/get_timestamp: set {test_ts:#x}, got {got:#x}"
)
91
return
1
92
print(
"PASS: set_timestamp / get_timestamp"
)
93
return
0
94
95
96
def
test_channel
() -> int:
97
frame = DAPHNEEthFrame()
98
for
val
in
(0, 1, 127, 255):
99
frame.set_channel(val)
100
got = frame.get_channel()
101
if
got != val:
102
print(f
"FAIL: set_channel({val}), get_channel() returned {got}"
)
103
return
1
104
print(
"PASS: set_channel / get_channel"
)
105
return
0
106
107
108
def
test_daqheader_accessible
() -> int:
109
frame = DAPHNEEthFrame()
110
hdr = frame.daq_header
111
if
hdr
is
None
:
112
print(
"FAIL: daq_header returned None"
)
113
return
1
114
hdr_alias = frame.get_daqheader()
115
if
hdr_alias
is
None
:
116
print(
"FAIL: get_daqheader() returned None"
)
117
return
1
118
print(
"PASS: daq_header accessible"
)
119
return
0
120
121
122
def
test_header_properties
() -> int:
123
frame = DAPHNEEthFrame()
124
hdr = frame.header
125
hdr_alias = frame.get_daphneheader()
126
127
if
hdr_alias
is
None
:
128
print(
"FAIL: get_daphneheader() returned None"
)
129
return
1
130
131
hdr.channel = 5
132
hdr.version = 1
133
hdr.trigger_sample_value = 123
134
hdr.threshold = 456
135
hdr.baseline = 789
136
hdr.w1 = 11
137
hdr.w2 = 22
138
hdr.w3 = 33
139
hdr.w4 = 44
140
hdr.w5 = 55
141
hdr.w6 = 66
142
143
if
hdr.channel != 5
or
frame.get_channel() != 5:
144
print(
"FAIL: header.channel mismatch"
)
145
return
1
146
if
hdr.version != 1:
147
print(
"FAIL: header.version mismatch"
)
148
return
1
149
if
hdr.trigger_sample_value != 123:
150
print(
"FAIL: header.trigger_sample_value mismatch"
)
151
return
1
152
if
hdr.threshold != 456:
153
print(
"FAIL: header.threshold mismatch"
)
154
return
1
155
if
hdr.baseline != 789:
156
print(
"FAIL: header.baseline mismatch"
)
157
return
1
158
if
(hdr.w1, hdr.w2, hdr.w3, hdr.w4, hdr.w5, hdr.w6) != (11, 22, 33, 44, 55, 66):
159
print(
"FAIL: w1..w6 mismatch"
)
160
return
1
161
if
hdr_alias.w6 != 66:
162
print(f
"FAIL: get_daphneheader alias mismatch, got {hdr_alias.w6}"
)
163
return
1
164
165
print(
"PASS: DAPHNEEthHeader properties"
)
166
return
0
167
168
169
def
test_set_geoid
() -> int:
170
frame = DAPHNEEthFrame()
171
crate_id = 5
172
slot_id = 3
173
stream_id = 12
174
hdr = frame.daq_header
175
hdr.crate_id = crate_id
176
hdr.slot_id = slot_id
177
hdr.stream_id = stream_id
178
if
hdr.crate_id != crate_id:
179
print(f
"FAIL: crate_id mismatch, got {hdr.crate_id}"
)
180
return
1
181
if
hdr.slot_id != slot_id:
182
print(f
"FAIL: slot_id mismatch, got {hdr.slot_id}"
)
183
return
1
184
if
hdr.stream_id != stream_id:
185
print(f
"FAIL: stream_id mismatch, got {hdr.stream_id}"
)
186
return
1
187
print(
"PASS: DAQEthHeader geoid fields can be set directly"
)
188
return
0
189
190
191
def
test_adc_single
() -> int:
192
frame = DAPHNEEthFrame()
193
frame.set_adc(0, 321)
194
if
frame.get_adc(0) != 321:
195
print(f
"FAIL: set_adc(0,321) -> get_adc(0) returned {frame.get_adc(0)}"
)
196
return
1
197
print(
"PASS: single ADC set/get (adc index 0)"
)
198
return
0
199
200
201
def
test_adc_max_value
() -> int:
202
frame = DAPHNEEthFrame()
203
frame.set_adc(0, ADC_MAX)
204
got = frame.get_adc(0)
205
if
got != ADC_MAX:
206
print(f
"FAIL: max ADC value {ADC_MAX}: got {got}"
)
207
return
1
208
print(f
"PASS: max ADC value ({ADC_MAX}) preserved"
)
209
return
0
210
211
212
def
test_adc_independence
() -> int:
213
frame = DAPHNEEthFrame()
214
test_cases = [
215
(0, 100),
216
(1, 200),
217
(2, 300),
218
(ADCS_PER_CHANNEL - 1, 9999),
219
]
220
for
adc_idx, val
in
test_cases:
221
frame.set_adc(adc_idx, val)
222
for
adc_idx, expected
in
test_cases:
223
got = frame.get_adc(adc_idx)
224
if
got != expected:
225
print(f
"FAIL: ADC independence: index={adc_idx}: expected {expected}, got {got}"
)
226
return
1
227
print(
"PASS: ADC cell independence across multiple indices"
)
228
return
0
229
230
231
def
test_adc_all_channels_one_sample
() -> int:
232
frame = DAPHNEEthFrame()
233
for
adc_idx
in
range(ADCS_PER_CHANNEL):
234
frame.set_adc(adc_idx, adc_idx * 100 % (ADC_MAX + 1))
235
for
adc_idx
in
range(ADCS_PER_CHANNEL):
236
expected = adc_idx * 100 % (ADC_MAX + 1)
237
got = frame.get_adc(adc_idx)
238
if
got != expected:
239
print(f
"FAIL: all-adcs test: adc index {adc_idx}: expected {expected}, got {got}"
)
240
return
1
241
print(f
"PASS: all {ADCS_PER_CHANNEL} ADC values read back correctly"
)
242
return
0
243
244
245
def
test_adc_out_of_range
() -> int:
246
frame = DAPHNEEthFrame()
247
try
:
248
frame.get_adc(ADCS_PER_CHANNEL)
249
print(
"FAIL: get_adc with out-of-range index should have raised an exception"
)
250
return
1
251
except
Exception:
252
pass
253
print(
"PASS: out-of-range ADC index raises exception as expected"
)
254
return
0
255
256
def
main
() -> int:
257
tests = [
258
test_construction_and_size,
259
test_static_members,
260
test_bytes_roundtrip,
261
test_less_than_operator,
262
test_timestamp,
263
test_channel,
264
test_daqheader_accessible,
265
test_set_geoid,
266
test_header_properties,
267
test_adc_single,
268
test_adc_max_value,
269
test_adc_independence,
270
test_adc_all_channels_one_sample,
271
test_adc_out_of_range,
272
]
273
274
failures = 0
275
for
test
in
tests:
276
failures += test()
277
278
print()
279
if
failures == 0:
280
print(
"All DAPHNEEthFrame Python binding tests passed"
)
281
else
:
282
print(f
"{failures} DAPHNEEthFrame Python binding test(s) FAILED"
)
283
return
failures
284
285
286
if
__name__ ==
"__main__"
:
287
sys.exit(
main
())
DAPHNEEth_binding_test.test_construction_and_size
int test_construction_and_size()
Definition
DAPHNEEth_binding_test.py:12
DAPHNEEth_binding_test.test_adc_out_of_range
int test_adc_out_of_range()
Definition
DAPHNEEth_binding_test.py:245
DAPHNEEth_binding_test.test_static_members
int test_static_members()
Definition
DAPHNEEth_binding_test.py:25
DAPHNEEth_binding_test.test_adc_single
int test_adc_single()
Definition
DAPHNEEth_binding_test.py:191
DAPHNEEth_binding_test.test_adc_independence
int test_adc_independence()
Definition
DAPHNEEth_binding_test.py:212
DAPHNEEth_binding_test.test_set_geoid
int test_set_geoid()
Definition
DAPHNEEth_binding_test.py:169
DAPHNEEth_binding_test.test_less_than_operator
int test_less_than_operator()
Definition
DAPHNEEth_binding_test.py:69
DAPHNEEth_binding_test.test_daqheader_accessible
int test_daqheader_accessible()
Definition
DAPHNEEth_binding_test.py:108
DAPHNEEth_binding_test.test_header_properties
int test_header_properties()
Definition
DAPHNEEth_binding_test.py:122
DAPHNEEth_binding_test.test_adc_all_channels_one_sample
int test_adc_all_channels_one_sample()
Definition
DAPHNEEth_binding_test.py:231
DAPHNEEth_binding_test.main
int main()
Definition
DAPHNEEth_binding_test.py:256
DAPHNEEth_binding_test.test_bytes_roundtrip
int test_bytes_roundtrip()
Definition
DAPHNEEth_binding_test.py:48
DAPHNEEth_binding_test.test_timestamp
int test_timestamp()
Definition
DAPHNEEth_binding_test.py:84
DAPHNEEth_binding_test.test_channel
int test_channel()
Definition
DAPHNEEth_binding_test.py:96
DAPHNEEth_binding_test.test_adc_max_value
int test_adc_max_value()
Definition
DAPHNEEth_binding_test.py:201
Generated on
for DUNE-DAQ by
1.17.0