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