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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
#!/usr/bin/env python3
import ROOT
import sys
import getopt
from ROOT import TBox, TLatex, TClass
from copy import deepcopy
ROOT.gROOT.SetBatch() # don't pop up canvases
# ROOT.gROOT.SetStyle('Plain') # white background
#####################################################
### GLOBAL VARS
#
rocsInCol = 2
rocsInRow = 8;
rocXLen = 1.0 / rocsInRow
maxRocIdx = rocsInCol * rocsInRow - 1 # 0...15
onlineMaxLadder = [6, 14, 22, 32]
onlineMaxBlade = [11, 17]
maxOnlineModule = 4
maxOnlineDisk = 3
#
useNumberAsPartName = False # does it accept numbers or mO, pI etc.
inputFileName = "input.dat" #default file name
hRes, vRes = 1920, 1080
useFileSuffix = False
colorCoded = False
pixelAlive = False
### GLOBAL VARS
class HistogramManager:
def __init__(self):
self.barrelHists = []
self.forwardHists = []
# barrel histograms
i = 1
for maxLadder in onlineMaxLadder:
nBinsX = (maxOnlineModule * 2 + 1) * rocsInRow
nBinsY = (maxLadder * 2 + 1) * rocsInCol
name = "PXBarrel_Layer" + str(i);
title = "PXBarrel_Layer" + str(i);
histObj = ROOT.TH2F(name, title, nBinsX, -(maxOnlineModule + 0.5), maxOnlineModule + 0.5, nBinsY, -(maxLadder + 0.5), maxLadder + 0.5)
histObj.GetXaxis().SetTitle("OnlineSignedModule");
histObj.GetYaxis().SetTitle("OnlineSignedLadder");
histObj.SetOption("COLZ")
histObj.SetStats(0)
histObj.SetTitle("OnlineSignedModule")
self.barrelHists.append(deepcopy(histObj))
i = i + 1
# forward histograms
i = 1
for maxBlade in onlineMaxBlade:
nBinsX = (maxOnlineDisk * 2 + 1) * rocsInRow
nBinsY = (maxBlade * 2 + 1) * 2 * rocsInCol
name = "PXForward_Ring" + str(i);
title = "PXForward_Ring" + str(i);
histObj = ROOT.TH2F(name, title, nBinsX, -(maxOnlineDisk + 0.5), maxOnlineDisk + 0.5, nBinsY, -(maxBlade + 0.5), maxBlade + 0.5)
histObj.GetXaxis().SetTitle("OnlineSignedDisk");
histObj.GetYaxis().SetTitle("OnlineSignedBladePanel");
histObj.SetOption("COLZ")
histObj.SetStats(0)
self.forwardHists.append(deepcopy(histObj))
i = i + 1
def fillHistograms(self, barrelObjs, forwardObjs):
for b in barrelObjs:
coord = b.GetXYCoords()
histRef = self.barrelHists[b.layer - 1]
binNum = histRef.FindBin(coord[0], coord[1])
if colorCoded:
binContent = TranslateReasonStringBPix(b.reason)
elif pixelAlive:
binContent = float(b.reason)
else:
binContent = b.roc + 1
histRef.SetBinContent(binNum, binContent)
# self.barrelHists[b.layer - 1].Fill(coord[0], coord[1], b.roc + 1)
for f in forwardObjs:
coord = f.GetXYCoords()
histRef = self.forwardHists[f.ring - 1]
binNum = histRef.FindBin(coord[0], coord[1])
if colorCoded:
binContent = TranslateReasonStringFPix(f.reason)
else:
binContent = f.roc + 1
histRef.SetBinContent(binNum, binContent)
# self.forwardHists[f.ring - 1].Fill(coord[0], coord[1], f.roc + 1)
def drawLine(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1):
lineObj.SetLineWidth(width)
lineObj.SetLineStyle(style)
lineObj.SetLineColor(color)
lineObj.DrawLine(x1, y1, x2, y2)
def drawRectangle(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1):
self.drawLine(lineObj, x1, x2, y2, y2, width, style, color)
self.drawLine(lineObj, x2, x2, y2, y1, width, style, color)
self.drawLine(lineObj, x2, x1, y1, y1, width, style, color)
self.drawLine(lineObj, x1, x1, y1, y2, width, style, color)
def prettifyCanvas(self, hist):
nBinsX = hist.GetXaxis().GetNbins()
xMin = hist.GetXaxis().GetXmin()
xMax = hist.GetXaxis().GetXmax()
nBinsY = hist.GetYaxis().GetNbins()
yMin = hist.GetYaxis().GetXmin()
yMax = hist.GetYaxis().GetXmax()
xLen = int(xMax)
yLen = int(yMax)
name = hist.GetName()[0:3]
isBarrel = True if name != "PXF" else False
print((name, isBarrel))
xBaseStep = 1
xRange = (nBinsX - 1) // (rocsInRow * 2) + 1
yBaseStep = (yMax - yMin) / nBinsY
yRange = (nBinsY - 1) // (2) + 1
if not isBarrel:
yBaseStep = yBaseStep * 2
yRange = yRange // 2
# horizontal
x1 = xMin
x2 = xMin + xLen
y1 = yMin
y2 = yMin
lineObj = ROOT.TLine()
lineObj.SetBit(ROOT.kCanDelete)
for i in range(yRange):
w = 1 if i % 2 else 2
self.drawLine(lineObj, x1, x2, y1, y2, w)
self.drawLine(lineObj, x1, x2, -y1, -y2, w)
self.drawLine(lineObj, -x1, -x2, -y1, -y2,w )
self.drawLine(lineObj, -x1, -x2, y1, y2, w)
y1 = y1 + yBaseStep
y2 = y2 + yBaseStep
# vertical
x1 = xMin
x2 = xMin
y1 = yMin
y2 = yMin + yLen
for i in range(xRange):
self.drawLine(lineObj, x1, x2, y1, y2, style = 9)
self.drawLine(lineObj, x1, x2, -y1, -y2, style = 9)
self.drawLine(lineObj, -x1, -x2, -y1, -y2, style = 9)
self.drawLine(lineObj, -x1, -x2, y1, y2, style = 9)
x1 = x1 + xBaseStep
x2 = x2 + xBaseStep
# mark zero ROC
zeroModuleHeight = yBaseStep if isBarrel else yBaseStep * 0.5 # because there are two panels height of roc is smaller
yRange = int(yMax) if isBarrel else int(yMax) * 2
x1_base = 0 + xMin
x2_base = xBaseStep / float(rocsInRow) + xMin
y1_base = zeroModuleHeight + yMin
y2_base = 2 * zeroModuleHeight + yMin
for i in range(yRange):
y1 = y1_base + i * (zeroModuleHeight * 2) - (zeroModuleHeight if i % 2 else 0)
y2 = y2_base + i * (zeroModuleHeight * 2) - (zeroModuleHeight if i % 2 else 0)
#negative ladders/blades
for j in range(int(xMax)):
x1 = x1_base + j * xBaseStep
x2 = x2_base + j * xBaseStep
if yMax == 6.5 and x1 <0:
y1 = y1_base + i * (zeroModuleHeight * 2) + (zeroModuleHeight if i % 2 else 0)
y2 = y2_base + i * (zeroModuleHeight * 2) + (zeroModuleHeight if i % 2 else 0)
self.drawRectangle(lineObj,(xBaseStep+x1-(x2-x1)),(xBaseStep+x2-(x2-x1)), y1+(y1-y2), y2+(y1-y2), color=8)
x1, x2 = -x1, -x2
yPosChange = -zeroModuleHeight if i % 2 else zeroModuleHeight
self.drawRectangle(lineObj, x1, x2, y1 - yPosChange-2*(zeroModuleHeight if i % 2 else 0), y2 - yPosChange-2*(zeroModuleHeight if i % 2 else 0), color=8)
else:
self.drawRectangle(lineObj, x1, x2, y1, y2, color=8)
x1, x2 = -x1, -x2
yPosChange = -zeroModuleHeight if i % 2 else zeroModuleHeight
self.drawRectangle(lineObj, x1, x2, y1 - yPosChange, y2 - yPosChange, color=8)
# positive ladders/blades
y1 = y1 - yMin + yBaseStep
y2 = y2 - yMin + yBaseStep
for j in range(int(xMax)):
x1 = x1_base + j * xBaseStep
x2 = x2_base + j * xBaseStep
if yMax== 6.5 and x1 <0:
self.drawRectangle(lineObj, xBaseStep+x1-(x2-x1), xBaseStep+x2-(x2-x1), y1+(y1-y2), y2+(y1-y2), color=8)
x1, x2 = -x1, -x2
self.drawRectangle(lineObj, x1, x2, y1 - yPosChange- 2*(zeroModuleHeight if i % 2 else 0), y2 - yPosChange-2*(zeroModuleHeight if i % 2 else 0), color=8)
else:
self.drawRectangle(lineObj, x1, x2, y1, y2, color=8)
x1, x2 = -x1, -x2
self.drawRectangle(lineObj, x1, x2, y1 - yPosChange, y2 - yPosChange, color=8)
# hist.GetZaxis().SetRangeUser(-0.5,15.5)
def saveHistograms(self):
for hists in [self.barrelHists, self.forwardHists]:
for hist in hists:
# if hist.GetEntries():
c1 = ROOT.TCanvas(hist.GetName(), hist.GetName(), hRes, vRes)
if colorCoded:
hist.GetZaxis().SetRangeUser(0,5)
ROOT.gStyle.SetPalette(55)
elif pixelAlive:
hist.GetZaxis().SetRangeUser(0,4160)
ROOT.gStyle.SetPalette(70)
hist.Draw()
txt = TLatex()
txt.SetNDC()
txt.SetTextFont(1)
txt.SetTextColor(1)
txt.SetTextAlign(22)
txt.SetTextAngle(0)
txt.SetTextSize(0.05)
txt.DrawLatex(0.5, 0.95, hist.GetName())
xMin = hist.GetXaxis().GetXmin()
yMin = hist.GetYaxis().GetXmin()
box1 = TBox(xMin*1.1,yMin*1.25,xMin*1,yMin*1.15);
box1.SetFillColor(kRed+3)
box1.Draw()
txt.SetTextSize(0.035)
txt.DrawLatex(0.25, 0.077, "Dead At Beginning")
box2 = TBox(xMin*0.45,yMin*1.25,xMin*0.35,yMin*1.15);
box2.SetFillColor(kAzure+2)
box2.Draw()
txt.SetTextSize(0.035)
txt.DrawLatex(0.47, 0.077, "Dead At End")
self.prettifyCanvas(hist)
colorString = ""
if colorCoded:
colorString = "_coded"
elif pixelAlive:
colorString = "_pixelalive"
if useFileSuffix:
c1.Print(hist.GetName() + colorString + "_" + inputFileName[:-4] + ".png")
else:
c1.Print(hist.GetName() + colorString + ".png")
#####################################################
class Barrel:
def __init__ (self, part, sector, layer, ladder, module, roc, reason="unknown"):
self.part = part
self.sector = sector
self.layer = layer
self.ladder = ladder
self.module = module
self.roc = roc
self.isCoverted = False
self.reason = reason
def __str__(self):
return str([self.part, self.sector, self.layer, self.ladder, self.module])
def convertParts(self):
if not self.isCoverted:
self.ladder = -self.ladder if self.part % 2 else self.ladder
self.module = -self.module if self.part <= 2 else self.module
isConverted = True
def GetXYCoords(self):
xBase = -0.625 + ((maxRocIdx - self.roc if self.roc >= rocsInRow else self.roc) + 1) * rocXLen
flipY = False
if self.module < 0:
if self.ladder < 0:
if abs(self.ladder) % 2:
flipY = True
else:
if self.ladder % 2 == 0:
flipY = True
else:
if self.ladder < 0:
if abs(self.ladder) % 2 == 0:
flipY = True
else:
if self.ladder % 2:
flipY = True
tmpRoc = maxRocIdx - self.roc if flipY else self.roc;
yBase = -0.5 * (tmpRoc // rocsInRow)
x = self.module + (xBase if self.module < 0 else -xBase - rocXLen)
y = self.ladder + yBase
#print("roc=%d\t: (%f;%f)"%(self.roc, x, y))
return x, y
class Forward:
def __init__ (self, part, disk, blade, panel, ring, roc, reason="unknown"):
self.part = part
self.disk = disk
self.blade = blade
self.panel = panel
self.ring = ring
self.roc = roc
self.reason = reason
self.isCoverted = False
def __str__(self):
return str([self.part, self.disk, self.blade, self.panel, self.ring])
def convertParts(self):
if not self.isCoverted:
self.blade = -self.blade if self.part % 2 else self.blade
self.disk = -self.disk if self.part <= 2 else self.disk
self.isCoverted = True
def GetXYCoords(self):
xBase = -0.625 + ((maxRocIdx - self.roc if self.roc >= rocsInRow else self.roc) + 1) * rocXLen
x = self.disk + (xBase if self.disk < 0 else -xBase - rocXLen)
flipY = (self.panel == 2 if self.disk < 0 else self.panel == 1)
tmpRoc = maxRocIdx - self.roc if flipY else self.roc;
yBase = -0.25 - 0.25 * (tmpRoc // rocsInRow) + 0.5 * (self.panel - 1)
y = self.blade + yBase
# print("roc=%d\t: (%f;%f)"%(self.roc, x, y))
return x, y
def TranslatePartString(thePartStr):
if thePartStr == "mO":
return 1
elif thePartStr == "mI":
return 2
elif thePartStr == "pO":
return 3
elif thePartStr == "pI":
return 4
else:
print("Unrecognized part <%s>, the script is likely to crash..." % (thePartStr))
def TranslateReasonStringBPix(theReasonStr):
if theReasonStr == "unknown":
return 1
elif theReasonStr == "notprogrammable":
return 1
elif theReasonStr == "vcthr":
return 2
elif theReasonStr == "pixelalive":
return 2
elif theReasonStr == "iana":
return 2
elif theReasonStr == "calib":
return 2
elif theReasonStr== "fedphases":
return 4
elif theReasonStr == "tbmdelay":
return 1
elif theReasonStr == "power":
return 5
else:
return 1
print("Unrecognized part <%s>, the script is likely to crash..." % (theReasonStr))
def TranslateReasonStringFPix(theReasonStr):
if theReasonStr == "flaky":
return 1
elif theReasonStr == "power": #check github for the real reason
return 5
elif theReasonStr == "tbmdelay": #
return 1
elif theReasonStr == "unknown":
return 2
else:
return 2
print("Unrecognized part <%s>, the script is likely to crash..." % (theReasonStr))
def GetOnlineBarrelCharacteristics(detElements, roc, reason="unknown"):
onlinePart = int(detElements[1][1:]) if useNumberAsPartName else TranslatePartString(detElements[1][1:])
onlineSector = int(detElements[2][3:])
onlineLayer = int(detElements[3][3:])
if detElements[4][-1] == "H" or detElements[4][-1] == "F":
onlineLadder = int(detElements[4][3:-1])
else:
onlineLadder = int(detElements[4][3:])
onlineModule = int(detElements[5][3:])
return Barrel(*[onlinePart, onlineSector, onlineLayer, onlineLadder, onlineModule, roc, reason])
def GetOnlineForwardCharacteristics(detElements, roc, reason="unknown"):
onlinePart = int(detElements[1][1:]) if useNumberAsPartName else TranslatePartString(detElements[1][1:])
onlineDisk = int(detElements[2][1:])
onlineBlade = int(detElements[3][3:])
onlinePanel = int(detElements[4][3:])
onlineRing = int(detElements[5][3:])
return Forward(*[onlinePart, onlineDisk, onlineBlade, onlinePanel, onlineRing, roc, reason])
def GetAffectedRocs(rocString):
rocString = str(rocString)
iComma=rocString.find(",")
listOfRocs = []
if iComma!=-1:
listOfRocs.extend(GetAffectedRocs(rocString[0:iComma]))
listOfRocs.extend(GetAffectedRocs(rocString[iComma+1:len(rocString)]))
else:
iHyphen=rocString.find("-")
if iHyphen!=-1:
start=int(rocString[0:iHyphen])
end=int(rocString[iHyphen+1:len(rocString)])+1
listOfRocs.extend(range(start,end))
else:
return [int(rocString)]
return listOfRocs
#####################################################
histMan = HistogramManager()
barrelObjs = []
forwardObjs = []
if len(sys.argv) > 1:
inputFileName = sys.argv[1]
print(inputFileName)
if len(sys.argv) > 2:
opts, args = getopt.getopt(sys.argv[2:], "bscp", ["help", "output="])
for o, a in opts:
if o == "-b":
useNumberAsPartName = False
if o == "-s":
useFileSuffix = True
if o == "-c":
colorCoded = True
if o == "-p":
pixelAlive = True
i = 1
with open (inputFileName, "r") as inputFile:
for item in inputFile:
# print("Processing record #%d" % (i))
inputs = item.split(" ")
if len(inputs) >= 2: # but take only first 2 elements (ignore others like '\n')
detElements = inputs[0].split("_")
if detElements[3]=='LYR1' and (detElements[1]=='BmI' or detElements[1]=='BmO'):
rocs = []
roc = GetAffectedRocs(inputs[1])
for roc_rotate in roc:
if int(str(roc_rotate)) <= 7:
rocs.append(int(str(roc_rotate))+8)
elif int(str(roc_rotate)) >= 8:
rocs.append(int(str(roc_rotate)) -8)
else:
rocs = GetAffectedRocs(inputs[1])
# rocs = GetAffectedRocs(inputs[1]) #int(inputs[1]) #- 1 #shifts 0..16 rocNum to 0..15
if len(inputs) == 3:
reason = str(inputs[2]).lower().strip()
else:
reason="unknown"
for roc in rocs:
if detElements[0][0] == "B":
barrelObj = GetOnlineBarrelCharacteristics(detElements, roc, reason)
#print(barrelObj)
barrelObj.convertParts()
# print(barrelObj)
barrelObjs.append(barrelObj)
elif detElements[0][0] == "F":
forwardObj = GetOnlineForwardCharacteristics(detElements, roc, reason)
# print(forwardObj)
forwardObj.convertParts()
# print(forwardObj)
forwardObjs.append(forwardObj)
else:
print("Not recognized part type")
i = i + 1
histMan.fillHistograms(barrelObjs, forwardObjs)
histMan.saveHistograms()
|