forked from marksan87/hcalraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.py
executable file
·216 lines (180 loc) · 6.37 KB
/
diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python2
from configuration import patterns as conf
import configuration.hw
import optparse
import sys
def mapping(file=None, skip=[]):
out = {}
misc = []
for line in file:
if any([item in line for item in skip]):
continue
if line.startswith(conf.lineStart):
fields = line.split(":")
if len(fields) <= 1:
sys.exit("Problem processing this line (length %d < 2):\n%s" % (len(fields), line))
else:
be = fields[0].replace(conf.lineStart, "").split()
be = (be[0], int(be[1]), int(be[2]))
fe = " ".join(fields[1:])
out[be] = tuple(fe.split())
elif line != "\n":
misc.append(line)
return out, misc
def diffs(ref={}, cabled={}):
extra = {}
missing = {}
different = {}
same = {}
for be, fe in sorted(ref.iteritems()):
if be not in cabled:
missing[be] = fe
continue
if fe != cabled[be]:
different[be] = (fe, cabled[be])
else:
same[be] = fe
for be, fe in cabled.iteritems():
if be not in ref:
extra[be] = fe
return extra, missing, different, same
def pretty(be=None, fe=None):
if be:
dcc, sp, fi = be
if dcc.startswith("u"):
crate = dcc[1:]
slot = sp
s = " %2s %2s %2s: " % (crate, slot, fi)
else:
crate = conf.expectedCrate(int(dcc))
exp = configuration.hw.expectedVmeHtr(int(dcc), int(sp))
htr = "%2d%1s" % (exp["Slot"], exp["Top"])
s = "%2s(%2d) %2s(%3s) %2s: " % (dcc, crate, sp, htr, fi)
else:
s = ""
try:
s += " %6s %2s %2s" % fe
except TypeError:
s += " ".join(fe)
return s
def fiberCount(feCoords=[]):
out = {}
for coords in feCoords:
rbx = coords[0]
if rbx in out:
out[rbx] += 1
else:
out[rbx] = 1
return out
def printTable(rbxes={}, header="", zero=" ", onlyDet=None, excludeDet=None):
if header:
header = "| %s |" % header
print "-"*len(header)
print header
print "-"*len(header)
rbxList = conf.rbxes()
boxes = sorted(list(set([rbx[-2:] for rbx in rbxList])))
dets = sorted(list(set([rbx[:-2] for rbx in rbxList])))
nSpaces = 5
header = " ".join(["RBX".ljust(nSpaces)] + boxes)
print header
print "-" * len(header)
for det in dets:
if onlyDet and (onlyDet not in det):
continue
if excludeDet and (excludeDet in det):
continue
row = []
for box in boxes:
key = det + box
nFibers = rbxes.get(key)
if nFibers is None:
row.append(zero)
else:
row.append("%2d" % nFibers)
print " ".join([det.ljust(nSpaces)] + row)
print
def report(extra=None, missing=None, different=None, same=None, options=None):
nMissingMin = options.nMissingMin
nMissingMax = options.nMissingMax
printTable(fiberCount(same.values()),
header="Fibers matching reference",
onlyDet=options.only,
excludeDet=options.exclude)
nMissing = fiberCount(missing.values())
printTable(nMissing,
header="Fibers in reference, but either not acquired or having FE link errors",
onlyDet=options.only,
excludeDet=options.exclude)
header = "| RBXes with (%d <= n missing fibers <= %d) |" % (nMissingMin, nMissingMax)
print "-" * len(header)
print header
print "-" * len(header)
print "DCC(CR) SP(HTR) FI: ref. RBX RM FI"
count = 0
for iMissing in range(nMissingMin, 1 + nMissingMax):
count += nMissing.values().count(iMissing)
if not count:
print "None"
for be, ref in sorted(missing.iteritems()):
rbx = ref[0]
if nMissingMin <= nMissing[rbx] <= nMissingMax:
print pretty(be=be, fe=ref)
print
print "-----------------------------------"
print "| Fibers differing from reference |"
print "-----------------------------------"
if different:
print "DCC(CR) SP(HTR) FI: ref. RBX RM FI | cabled"
for be, (ref, cabled) in sorted(different.iteritems()):
print pretty(be=be, fe=ref) + " | " + pretty(fe=cabled)
else:
print "None"
print
print "--------------------------------------------------------"
print "| Fibers acquired and valid, but absent from reference |"
print "--------------------------------------------------------"
if extra:
print "DCC(CR) SP(HTR) FI: cabled"
for be, fe in sorted(extra.iteritems()):
print pretty(be=be, fe=fe)
else:
print "None"
def go(fileName="", options=None):
assert fileName
with open(fileName) as f:
ref, refMisc = mapping(f)
assert not refMisc, "refMisc='%s'" % refMisc
cabled, misc = mapping(sys.stdin, skip=["Xrd", "TClassTable", "nologin"])
print "".join(misc)
report(*diffs(ref, cabled), options=options)
def opts():
parser = optparse.OptionParser(usage="usage: %prog [options] reference_file.txt")
parser.add_option("--n-missing-min",
dest="nMissingMin",
default="1",
metavar="n",
type="int",
help="maximum number of missing fibers per RBX to print in detailed table (default is 1)")
parser.add_option("--n-missing-max",
dest="nMissingMax",
default="2",
metavar="n",
type="int",
help="maximum number of missing fibers per RBX to print in detailed table (default is 2)")
parser.add_option("--only",
dest="only",
default=None,
help="suppress row in table if this string is absent from RBX name")
parser.add_option("--exclude",
dest="exclude",
default=None,
help="suppress row in table if this string is present in RBX name")
options, args = parser.parse_args()
if not args:
parser.print_help()
sys.exit(1)
return options, args[0]
if __name__ == "__main__":
options, ref = opts()
go(ref, options)