Belle II Software development
create_magneticfield_payloads_gearbox.py
1#!/usr/bin/env python3
2
3
10
11import basf2
12import shutil
13import multiprocessing
14
15
16def safe_process(*args, **kwargs):
17 """Run `basf2.process` with the given path in a child process using
18 `multiprocessing.Process`
19
20 This avoids side effects (`safe_process` can be safely called multiple times)
21 and doesn't kill this script even if a segmentation violation or a `FATAL <LogLevel.FATAL>` error occurs during processing.
22
23 It will return the exitcode of the child process which should be 0 in case of no error
24 """
25 process = multiprocessing.Process(target=basf2.process, args=args, kwargs=kwargs)
26 process.start()
27 process.join()
28 return process.exitcode
29
30
31# remove existing local database
32shutil.rmtree("localdb", ignore_errors=True)
33
34path = basf2.Path()
35path.add_module("EventInfoSetter")
36path.add_module("Gearbox")
37path.add_module("Geometry", createPayloads=True, payloadIov=[0, 0, -1, -1])
38for field in ["", "Phase2", "Phase2QCSoff"]:
39 basf2.set_module_parameters(path, "Geometry", components=[f"MagneticField{field}"])
40 basf2.prepend_testing_payloads(f"localdb/MagneticField{field}.txt")
41 safe_process(path)
42 txtfile = open(f"localdb/MagneticField{field}.txt").readlines()
43 with open(f"localdb/MagneticField{field}.txt", "w") as newfile:
44 for line in txtfile:
45 if line.find("Magnetic") >= 0:
46 newfile.write(line)
47