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