Belle II Software development
create_geometry_payloads_with_FarBeamLine.py
1#!/usr/bin/env python3
2
3
10
11"""
12Create a full set of consistent geometry payloads for nominal geometry, Run 0 (phase 2)
13geometry, and Run 1 (early phase 3) geometry from XML files.
14
15Optionally one can give a list of payload names to keep only a subset of payloads
16"""
17
18import basf2
19import b2test_utils
20import shutil
21import sys
22import re
23import os
24import subprocess
25
26# if we have extra arguments only keep the payloads given as arguments
27interested = sys.argv[1:]
28
29# remove existing local database
30shutil.rmtree("localdb", ignore_errors=True)
31
32# create phase3 geometry: This is the default in Belle2.xml
33phase3 = basf2.Path()
34phase3.add_module("EventInfoSetter")
35phase3.add_module("Gearbox")
36phase3.add_module("Geometry", createPayloads=True, payloadIov=[0, 0, 0, -1], additionalComponents=['FarBeamLine'])
38
39# create post LS1 geometry: Identical to the default one
40postLS1 = basf2.Path()
41postLS1.add_module("EventInfoSetter")
42postLS1.add_module("Gearbox")
43postLS1.add_module("Geometry", createPayloads=True, payloadIov=[1004, 0, 1004, -1], additionalComponents=['FarBeamLine'])
45
46# create phase2 geometry. For this we need to manually add all the phase2
47# detectors and remove the phase3-only detectors.
48phase2_detectors = "BeamPipe PXD SVD ServiceGapsMaterial MICROTPC PINDIODE BEAMABORT HE3TUBE CLAWS FANGS PLUME QCSMONITOR".split()
49phase2 = basf2.create_path()
50phase2.add_module("EventInfoSetter")
51phase2.add_module("Gearbox")
52phase2.add_module("Geometry", createPayloads=True, payloadIov=[1002, 0, 1002, -1],
53 excludedComponents=["BeamPipe", "PXD", "SVD", "VXDService", "ServiceGapsMaterial"],
54 additionalComponents=[e + "-phase2" for e in phase2_detectors])
56
57# create Run 1 (early phase3) geometry: same as Run 2 (phase 3) but
58# different PXD, ServiceGapsMaterial, BeamPipe, Cryostat, FarBeamLine
59# configuration
60run1 = basf2.Path()
61run1.add_module("EventInfoSetter")
62run1.add_module("Gearbox")
63run1.add_module(
64 "Geometry",
65 createPayloads=True,
66 payloadIov=[
67 1003,
68 0,
69 1003,
70 -1],
71 excludedComponents=[
72 'PXD',
73 'ServiceGapsMaterial',
74 'BeamPipe',
75 'Cryostat',
76 'FarBeamLine'],
77 additionalComponents=[
78 'PXD-Run1',
79 'ServiceGapsMaterial-Run1',
80 'BeamPipe-Run1',
81 'Cryostat-Run1',
82 'FarBeamLine-Run1'])
84
85# most of the components are identical so we avoid uploading two
86# revisions so we remove most of them. We only need separate payloads for the
87# for some of them
88phase2_comp_list = ["GeoConfiguration", "PXDGeometryPar", "SVDGeometryPar", "BeamPipeGeo", "ServiceGapsMaterialsPar"]
89# once we have a different ServiceGapsMaterial configuration for phase 3 we
90# want to put that here as well
91run1_comp_list = [
92 "GeoConfiguration",
93 "PXDGeometryPar",
94 "BeamabortGeo",
95 "BeamPipeGeo",
96 "CryostatGeo",
97 "FarBeamLineGeo",
98 "ServiceGapsMaterialsPar"]
99database_content = []
100line_match = re.compile(r"^dbstore/(.*?) ([0-9a-f]+) ([0-9\-,]*)$")
101keep = set()
102with open("localdb/database.txt") as dbfile:
103 for line in dbfile:
104 match = line_match.search(line)
105 name, revision, iov = match.groups()
106 # do we want to keep that payload at all?
107 if interested and name not in interested:
108 continue
109 # if so check whether we can unify the payloads
110 iov = tuple(int(e) for e in iov.split(','))
111 if iov[0] == 0:
112 if name in phase2_comp_list:
113 keep.add((name, str(revision))) # we keep all revision one payloads somehow
114 database_content.append(f'dbstore/{name} {revision} 0,0,0,-1\n')
115 if name not in run1_comp_list:
116 database_content.append(f'dbstore/{name} {revision} 1003,0,1003,-1\n')
117 # luckily nothing we have in Run 1 (early phase 3) is identical between
118 # Run 0 (phase 2) and Run 1 so need for extra checks if in Run 1 but
119 # not Run 0
120 continue
121 elif iov[0] == 1002 and name not in phase2_comp_list:
122 continue
123 elif iov[0] == 1003 and name not in run1_comp_list:
124 continue
125
126 # otherwise keep as it is ...
127 keep.add((name, str(revision)))
128 database_content.append(line)
129
130# and write new database file
131database_content.sort()
132with open("localdb/database.txt", "w") as dbfile:
133 dbfile.writelines(database_content)
134
135# Ok, finally remove all unneeded payload files ...
136for filename in os.scandir('localdb/'):
137 match = re.match(r"dbstore_(.*?)_rev_(\d*).root", filename.name)
138 if not match:
139 continue
140 if match and match.groups() not in keep:
141 print(f"Removing {filename.name}: not needed")
142 os.unlink(filename.path)
143 else:
144 print(f"Normalizing {filename.name} as '{match.group(1)}'")
145 subprocess.call(["b2file-normalize", "-i", "-n", match.group(1), filename.path])
safe_process(*args, **kwargs)
Definition __init__.py:246