Belle II Software development
eclCompareMaps.py
1#!/usr/bin/env python3
2
3
10
11"""
12This scripts compares different versions of ECL channel maps,
13both text files and database payloads
14
15Return code is
16 0-ECL channel maps are identical
17 1-ECL channel maps are different
18 255-wrong script arguments
19"""
20
21from ROOT import TFile
22import sys
23
24
26
27ECL_CHANNELS_IN_SHAPER = 16
28ECL_BARREL_CRATES = 36
29ECL_FWD_CRATES = 8
30
31
32
33
34def main():
35 if len(sys.argv) < 3:
36 print()
37 print(f'Usage: {sys.argv[0]} file1 file2')
38 print(' file1 Path to text file or root file with ECL channel map')
39 print(' file2 Path to text file or root file with ECL channel map')
40 print()
41 exit(255)
42
43 paths = sys.argv[1], sys.argv[2]
44 ch_maps = []
45 ch_maps.append(loadFile(paths[0]))
46 ch_maps.append(loadFile(paths[1]))
47
48 # True if there are different rows in two maps
49 diff = False
50 # Header for the diff table
51 header = ' | crate | shaper | channel | cell_id |'
52
53 def rowToString(row):
54 return '| %5d | %6d | %7d | %7d |' % row
55
56 for partial_ch_maps in zip(*ch_maps):
57 for row1, row2 in zip(*partial_ch_maps):
58 if row1 != row2:
59 print()
60 if not diff:
61 print(header)
62 diff = True
63 print('file1:', rowToString(row1))
64 print('file2:', rowToString(row2))
65 if not diff:
66 print()
67 print('%s and %s contain identical channel maps.' % paths)
68 print()
69 exit(0)
70 else:
71 print(header)
72 print()
73 exit(1)
74
75
76
77# Load ECL channel map from file (either *.txt or *.root)
78
79
80def loadFile(path):
81 if path.endswith('.txt'):
82 return loadTextFile(path)
83 elif path.endswith('.root'):
84 return loadRootFile(path)
85 else:
86 print()
87 print(f'Error: The script only supports *.txt and *.root. Input file: {path}')
88 print()
89 exit(255)
90
91# Load ECL channel map from text file
92
93
94def loadTextFile(path):
95 map_bar = []
96 map_fwd = []
97 map_bwd = []
98 with open(path) as f:
99 for line in f.readlines():
100 # Skip comments
101 if line[0] == '#':
102 continue
103 items = [int(float(x)) for x in line.split()]
104 # Skip empty lines
105 if len(items) < 1:
106 continue
107
108 crate, shaper, channel, phi, theta, cell_id = items
109 # Discard phiID and thetaID because they don't depend on channel map
110 added_items = (crate, shaper, channel, cell_id)
111
112 if crate <= 36:
113 map_bar.append(added_items)
114 elif crate <= 44:
115 map_fwd.append(added_items)
116 else:
117 map_bwd.append(added_items)
118 return map_bar, map_fwd, map_bwd
119
120# Load ECL channel map from ROOT file
121
122
123def loadRootFile(path):
124 map_bar = []
125 map_fwd = []
126 map_bwd = []
127 root_file = TFile(path, 'read')
128 ecl_ch_map = root_file.Get('ECLChannelMap')
129
130 # Based on code from ECLChannelMapper
131 channel = crate = shaper = 1
132 array_index = 0
133 max_shapers = 12
134 while crate <= 52:
135 cell_id = -1
136 if crate <= 36:
137 cell_id = ecl_ch_map.getMappingBAR()[array_index]
138 map_bar.append((crate, shaper, channel, cell_id))
139 elif crate <= 44:
140 cell_id = ecl_ch_map.getMappingFWD()[array_index]
141 map_fwd.append((crate, shaper, channel, cell_id))
142 else:
143 cell_id = ecl_ch_map.getMappingBWD()[array_index]
144 map_bwd.append((crate, shaper, channel, cell_id))
145
146 array_index += 1
147 channel += 1
148 if channel > ECL_CHANNELS_IN_SHAPER:
149 channel = 1
150 shaper += 1
151 if shaper > max_shapers:
152 shaper = 1
153 if crate == ECL_BARREL_CRATES:
154 array_index = 0
155 max_shapers = 10
156 elif crate == ECL_BARREL_CRATES + ECL_FWD_CRATES:
157 array_index = 0
158 max_shapers = 8
159 crate += 1
160
161 return map_bar, map_fwd, map_bwd
162
163
164
165
166if __name__ == '__main__':
167 main()
Definition: main.py:1