Belle II Software  release-08-01-10
create_magneticfield_payloads_gearbox.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2
12 import shutil
13 import multiprocessing
14 
15 
16 def 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
22  <LogLevel.FATAL>` error occurs during processing.
23 
24  It will return the exitcode of the child process which should be 0 in case of no error
25  """
26  process = multiprocessing.Process(target=basf2.process, args=args, kwargs=kwargs)
27  process.start()
28  process.join()
29  return process.exitcode
30 
31 
32 # remove existing local database
33 shutil.rmtree("localdb", ignore_errors=True)
34 
35 path = basf2.Path()
36 path.add_module("EventInfoSetter")
37 path.add_module("Gearbox")
38 path.add_module("Geometry", createPayloads=True, payloadIov=[0, 0, -1, -1])
39 for field in ["", "Phase2", "Phase2QCSoff"]:
40  basf2.set_module_parameters(path, "Geometry", components=[f"MagneticField{field}"])
41  basf2.prepend_testing_payloads(f"localdb/MagneticField{field}.txt")
42  safe_process(path)
43  txtfile = open(f"localdb/MagneticField{field}.txt").readlines()
44  with open(f"localdb/MagneticField{field}.txt", "w") as newfile:
45  for line in txtfile:
46  if line.find("Magnetic") >= 0:
47  newfile.write(line)