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