ATLAS Offline Software
parseMapping.py
Go to the documentation of this file.
1 #Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 import sys
4 import re
5 
6 def parsePosition(header_line):
7  #negative signs can appear on the second and last numbers
8  coords=re.match(r'^\s*{\s*(\d+),\s*(-?\d+),\s*(\d+),\s*(\d+),\s*(-?\d+)\s*},?', header_line)
9  if coords is None:
10  return None
11  return [int(x) for x in coords.groups()]
12 
13 #new format is a list with hash and position, [2027, 2, 2, 39, 0]
14 def convertToNewFormat(oldFormat):
15  hash_positionStrings=oldFormat.split()[1:]
16  theHash = int(hash_positionStrings[0])
17  thePositions = [int(x) for x in hash_positionStrings[1].split(',')]
18  result = [theHash]
19  return result + thePositions
20 
21 
22 #old format is a single string, ' : 2027 2,2,39,0'
23 #note: no comma between hash and position coordinates, the hash is r-justified
24 def convertToOldFormat(newFormat):
25  newFormatPosition = str(newFormat[1:])
26  joinedPosition = "".join(newFormatPosition)[1:-1]
27  spaceStripped = re.sub(r'[\s+]', '', joinedPosition)
28  newFormatHash = str(newFormat[0])
29  return " : "+newFormatHash.rjust(4)+" "+spaceStripped
30 
31 
32 def create_mapping0(fname):
33  golden_modules = []
34  module_file = open(fname, 'r')
35  module_data = module_file.read()
36  module_lines = module_data.splitlines()
37 
38  for modules in module_lines:
39  modulename = ""
40  modules = modules.split()
41  if len(modules) != 0:
42  if modules[0] == "else" and modules[1] == "if":
43  geographicalID = modules[2].split('"')
44  modulename = geographicalID[1]
45  elif modules[0] == "if":
46  geographicalID = modules[1].split('"')
47  modulename = geographicalID[1]
48  if modulename:
49  golden_modules.append(modulename)
50 
51  golden_hash = []
52  for modules in module_lines:
53  modulehash = ""
54  goldens = modules.split('=')
55  modules = modules.split()
56  if len(modules) != 0:
57  if modules[0] == "else" and modules[1] == "if":
58  golden_hashID = " : " + goldens[3].split(';')[0]
59  golden_bec = goldens[4].split(';')[0].strip()
60  golden_layer = goldens[5].split(';')[0].strip()
61  golden_phi = goldens[6].split(';')[0].strip()
62  golden_eta = goldens[7].split(';')[0].strip()
63  modulehash = golden_hashID + " " + golden_bec + "," + golden_layer + "," + golden_phi + "," + golden_eta
64  elif modules[0] == "if":
65  golden_hashIds = modules[4].split(';')[0]
66  golden_bec = modules[5].split('=')[1].split(';')[0]
67  golden_layer = modules[6].split('=')[1].split(';')[0]
68  golden_phi = modules[8].split(';')[0]
69  golden_eta = modules[10].split(';')[0].strip()
70  if len(golden_hashIds) == 1:
71  golden_hashID = " : " + golden_hashIds
72  elif len(golden_hashIds) == 2:
73  golden_hashID = " : " + golden_hashIds
74  elif len(golden_hashIds) == 3:
75  golden_hashID = " : " + golden_hashIds
76  elif len(golden_hashIds) == 4:
77  golden_hashID = " : " + golden_hashIds
78  modulehash = golden_hashID + " " + golden_bec + "," + golden_layer + "," + golden_phi + "," + golden_eta
79  if modulehash:
80  golden_hash.append(modulehash)
81  return dict(zip(golden_modules, golden_hash))
82 
83 
84 
85 #create new mapping from c++ header file of format March 2013
86 #Two arrays in the file, first one is array of strings
87 #Second one is array of the struct 'positions'
88 def create_mapping1(fname):
89 #The following is truly horrible; it parses the .h file to read module names.
90  module_names=[]
91  module_file = open(fname, 'r')
92  module_data = module_file.read()
93  module_lines = module_data.splitlines()
94  found_names=False
95  for modules in module_lines:
96  modulename = ""
97  modules = modules.split()
98  if len(modules) != 0:
99  if found_names:
100  if "};" in modules: #end of list in code
101  break
102  geographicalID = modules[0].split('"')
103  modulename = geographicalID[1]
104  module_names.append(modulename)
105  if "names" in modules: #beginning of list in code
106  found_names=True
107  #
108  found_positions = False
109  module_positions=[]
110  for position_line in module_lines:
111  if len(position_line) != 0:
112  if found_positions:
113  if "}};" in position_line: #end of list in code
114  break
115  position = parsePosition(position_line)
116  if position is None:
117  print('No match')
118  module_positions.append(position)
119  if "values" in position_line: #beginning of list in code
120  found_positions=True
121  return dict(zip(module_names,module_positions))
122 
123 def create_mapping(fname):
124  result=dict()
125  module_file = open(fname, 'r')
126  module_data = module_file.read()
127  module_lines = module_data.splitlines()
128  for module in module_lines:
129  if '#' in module:
130  continue
131  components = module.split(',')
132  thisModuleName = components[0]
133  thisModulePosition = [int(x) for x in components[1:]]
134  result[thisModuleName] = thisModulePosition
135  return result
136 
137 
138 if __name__ == "__main__":
139  #pre-2023 format of the header, which was in 'pixels' and 'lbl' directories
140  #name_position_dict0 = create_mapping0('oldPixelMapping.h')
141  name_position_dict1 = create_mapping1('pixelMapping.h')
142  name_position_dict = create_mapping('mapping.csv')
143  #print(name_position_dict)
144  print ("#name, hash, bec, layer, phi, eta")
145  for n in name_position_dict1:
146  v = convertToOldFormat(name_position_dict[n])
147  #left as an example
148  #v1 = convertToOldFormat(name_position_dict1[n])
149  v0 = convertToOldFormat(name_position_dict1[n])
150  if v!=v0:
151  print (v0,v)
152 
153  '''
154  Examples of extraction to lists
155  ===============================
156  myList = [*name_position_dict]
157  print(myList)
158  names=[]
159  hashes=[]
160  for (k,v) in name_position_dict.items():
161  names.append(k)
162  hashes.append(v[0])
163  print(names)
164  print(hashes)
165  '''
166  sys.exit(0)
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
parseMapping.create_mapping1
def create_mapping1(fname)
Definition: parseMapping.py:88
parseMapping.convertToOldFormat
def convertToOldFormat(newFormat)
Definition: parseMapping.py:24
parseMapping.convertToNewFormat
def convertToNewFormat(oldFormat)
Definition: parseMapping.py:14
parseMapping.parsePosition
def parsePosition(header_line)
Definition: parseMapping.py:6
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
parseMapping.create_mapping0
def create_mapping0(fname)
Definition: parseMapping.py:32
parseMapping.create_mapping
def create_mapping(fname)
Definition: parseMapping.py:123
Trk::split
@ split
Definition: LayerMaterialProperties.h:38