Belle II Software development
test_randomness.py
1
8
9'''
10We generate, simulate and reconstruct few events with two different processes
11and we check the two processes give exactly the same output.
12'''
13
14
15import difflib
16import subprocess
17
18import basf2 as b2
19import b2test_utils as b2tu
20
21
22def _unified_diff(expected, actual):
23 '''
24 Helper function. Returns a string containing the unified diff of two multiline strings.
25 '''
26 expected = expected.splitlines(1)
27 actual = actual.splitlines(1)
28 diff = difflib.unified_diff(expected, actual)
29 return ''.join(diff)
30
31
32if __name__ == '__main__':
33
34 steering = b2.find_file('reconstruction/tests/test_randomness.py_noexec')
35 command = f'basf2 {steering} -n 10 --random-seed aSeed'
36
37 b2.B2INFO('Executing first job...')
38 with b2tu.clean_working_directory():
39 job1 = subprocess.run(command.split(), check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
40
41 b2.B2INFO('Executing second job...')
42 with b2tu.clean_working_directory():
43 job2 = subprocess.run(command.split(), check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
44
45 diff = _unified_diff(job1.stdout, job2.stdout)
46 if diff:
47 b2.B2ERROR('!!! The two outputs are different !!!')
48 print(diff)
49 b2.B2FATAL('!!! basf2 produces irreproducible results !!!')