Belle II Software development
create_reducedCDC_geometry_payloads.py
1#!/usr/bin/env python3
2
3
10
11"""
12Create a full set of consistent geometry payloads for reduced CDC geometry from XML files.
13"""
14
15import basf2
16import shutil
17import re
18import os
19import subprocess
20
21# only keep the payloads which are different from the default ones.
22interestedPayloads = ["GeoConfiguration", "CDCGeometry"]
23
24# remove existing local database
25shutil.rmtree("localdb", ignore_errors=True)
26
27# create new geometry with reduced CDC. For this we need to manually
28# remove the default CDC and add the reduced CDC instead. Only for exp=0.
29reducedCDCpath = basf2.create_path()
30reducedCDCpath.add_module("EventInfoSetter")
31reducedCDCpath.add_module("Gearbox")
32reducedCDCpath.add_module("Geometry", createPayloads=True, payloadIov=[0, 0, 0, -1],
33 excludedComponents=['CDC'], additionalComponents=['CDCReduced'])
34basf2.process(reducedCDCpath)
35
36# most of the components are identical so we avoid uploading two revisions
37# so we remove most of them. We only need separate payloads for the for
38# some of them
39replacementPayloads = ["GeoConfiguration", "CDCGeometry"]
40
41database_content = []
42line_match = re.compile(r"^dbstore/(.*?) ([0-9a-f]+) ([0-9\-,]*)$")
43keep = set()
44with open("localdb/database.txt") as dbfile:
45 for line in dbfile:
46 match = line_match.search(line)
47 name, revision, iov = match.groups()
48 # do we want to keep that payload at all?
49 if interestedPayloads and name not in interestedPayloads:
50 continue
51 # if so check whether we can unify the payloads
52 iov = tuple(int(e) for e in iov.split(','))
53 if iov[0] == 0:
54 if name in replacementPayloads:
55 keep.add((name, str(revision))) # we keep all revision one payloads somehow
56 database_content.append(f'dbstore/{name} {revision} 0,0,0,-1\n')
57 continue
58
59 # otherwise keep as it is ...
60 keep.add((name, str(revision)))
61 database_content.append(line)
62
63# and write new database file
64database_content.sort()
65with open("localdb/database.txt", "w") as dbfile:
66 dbfile.writelines(database_content)
67
68# Ok, finally remove all unneeded payload files ...
69for filename in os.scandir('localdb/'):
70 match = re.match(r"dbstore_(.*?)_rev_(\d*).root", filename.name)
71 if not match:
72 continue
73 if match and match.groups() not in keep:
74 print(f"Removing {filename.name}: not needed")
75 os.unlink(filename.path)
76 else:
77 print(f"Normalizing {filename.name} as '{match.group(1)}'")
78 subprocess.call(["b2file-normalize", "-i", "-n", match.group(1), filename.path])