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