CMS 3D CMS Logo

PixelMapPlotter.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from __future__ import print_function
4 import ROOT
5 import sys
6 import getopt
7 from ROOT import TBox, TLatex, TClass
8 
9 from copy import deepcopy
10 
11 ROOT.gROOT.SetBatch() # don't pop up canvases
12 # ROOT.gROOT.SetStyle('Plain') # white background
13 
14 
17 rocsInCol = 2
18 rocsInRow = 8;
19 rocXLen = 1.0 / rocsInRow
20 
21 maxRocIdx = rocsInCol * rocsInRow - 1 # 0...15
22 
23 onlineMaxLadder = [6, 14, 22, 32]
24 onlineMaxBlade = [11, 17]
25 
26 maxOnlineModule = 4
27 maxOnlineDisk = 3
28 #
29 useNumberAsPartName = False # does it accept numbers or mO, pI etc.
30 inputFileName = "input.dat" #default file name
31 hRes, vRes = 1920, 1080
32 useFileSuffix = False
33 colorCoded = False
34 pixelAlive = False
35 
36 
38  def __init__(self):
39  self.barrelHists = []
40  self.forwardHists = []
41 
42  # barrel histograms
43  i = 1
44  for maxLadder in onlineMaxLadder:
45  nBinsX = (maxOnlineModule * 2 + 1) * rocsInRow
46  nBinsY = (maxLadder * 2 + 1) * rocsInCol
47 
48  name = "PXBarrel_Layer" + str(i);
49  title = "PXBarrel_Layer" + str(i);
50 
51  histObj = ROOT.TH2F(name, title, nBinsX, -(maxOnlineModule + 0.5), maxOnlineModule + 0.5, nBinsY, -(maxLadder + 0.5), maxLadder + 0.5)
52  histObj.GetXaxis().SetTitle("OnlineSignedModule");
53  histObj.GetYaxis().SetTitle("OnlineSignedLadder");
54  histObj.SetOption("COLZ")
55  histObj.SetStats(0)
56  histObj.SetTitle("OnlineSignedModule")
57 
58  self.barrelHists.append(deepcopy(histObj))
59  i = i + 1
60 
61  # forward histograms
62  i = 1
63  for maxBlade in onlineMaxBlade:
64  nBinsX = (maxOnlineDisk * 2 + 1) * rocsInRow
65  nBinsY = (maxBlade * 2 + 1) * 2 * rocsInCol
66 
67  name = "PXForward_Ring" + str(i);
68  title = "PXForward_Ring" + str(i);
69 
70  histObj = ROOT.TH2F(name, title, nBinsX, -(maxOnlineDisk + 0.5), maxOnlineDisk + 0.5, nBinsY, -(maxBlade + 0.5), maxBlade + 0.5)
71  histObj.GetXaxis().SetTitle("OnlineSignedDisk");
72  histObj.GetYaxis().SetTitle("OnlineSignedBladePanel");
73  histObj.SetOption("COLZ")
74  histObj.SetStats(0)
75 
76 
77  self.forwardHists.append(deepcopy(histObj))
78  i = i + 1
79 
80  def fillHistograms(self, barrelObjs, forwardObjs):
81  for b in barrelObjs:
82  coord = b.GetXYCoords()
83  histRef = self.barrelHists[b.layer - 1]
84 
85  binNum = histRef.FindBin(coord[0], coord[1])
86  if colorCoded:
87  binContent = TranslateReasonStringBPix(b.reason)
88  elif pixelAlive:
89  binContent = float(b.reason)
90  else:
91  binContent = b.roc + 1
92 
93  histRef.SetBinContent(binNum, binContent)
94  # self.barrelHists[b.layer - 1].Fill(coord[0], coord[1], b.roc + 1)
95 
96  for f in forwardObjs:
97  coord = f.GetXYCoords()
98  histRef = self.forwardHists[f.ring - 1]
99 
100  binNum = histRef.FindBin(coord[0], coord[1])
101 
102  if colorCoded:
103  binContent = TranslateReasonStringFPix(f.reason)
104  else:
105  binContent = f.roc + 1
106  histRef.SetBinContent(binNum, binContent)
107 
108  # self.forwardHists[f.ring - 1].Fill(coord[0], coord[1], f.roc + 1)
109 
110  def drawLine(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1):
111  lineObj.SetLineWidth(width)
112  lineObj.SetLineStyle(style)
113  lineObj.SetLineColor(color)
114 
115  lineObj.DrawLine(x1, y1, x2, y2)
116 
117  def drawRectangle(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1):
118  self.drawLine(lineObj, x1, x2, y2, y2, width, style, color)
119  self.drawLine(lineObj, x2, x2, y2, y1, width, style, color)
120  self.drawLine(lineObj, x2, x1, y1, y1, width, style, color)
121  self.drawLine(lineObj, x1, x1, y1, y2, width, style, color)
122 
123  def prettifyCanvas(self, hist):
124  nBinsX = hist.GetXaxis().GetNbins()
125  xMin = hist.GetXaxis().GetXmin()
126  xMax = hist.GetXaxis().GetXmax()
127  nBinsY = hist.GetYaxis().GetNbins()
128  yMin = hist.GetYaxis().GetXmin()
129  yMax = hist.GetYaxis().GetXmax()
130 
131  xLen = int(xMax)
132  yLen = int(yMax)
133 
134  name = hist.GetName()[0:3]
135  isBarrel = True if name != "PXF" else False
136  print((name, isBarrel))
137 
138  xBaseStep = 1
139  xRange = (nBinsX - 1) // (rocsInRow * 2) + 1
140  yBaseStep = (yMax - yMin) / nBinsY
141  yRange = (nBinsY - 1) // (2) + 1
142  if not isBarrel:
143  yBaseStep = yBaseStep * 2
144  yRange = yRange // 2
145 
146  # horizontal
147  x1 = xMin
148  x2 = xMin + xLen
149  y1 = yMin
150  y2 = yMin
151 
152  lineObj = ROOT.TLine()
153  lineObj.SetBit(ROOT.kCanDelete)
154 
155  for i in range(yRange):
156  w = 1 if i % 2 else 2
157  self.drawLine(lineObj, x1, x2, y1, y2, w)
158  self.drawLine(lineObj, x1, x2, -y1, -y2, w)
159  self.drawLine(lineObj, -x1, -x2, -y1, -y2,w )
160  self.drawLine(lineObj, -x1, -x2, y1, y2, w)
161 
162  y1 = y1 + yBaseStep
163  y2 = y2 + yBaseStep
164 
165  # vertical
166  x1 = xMin
167  x2 = xMin
168  y1 = yMin
169  y2 = yMin + yLen
170 
171  for i in range(xRange):
172  self.drawLine(lineObj, x1, x2, y1, y2, style = 9)
173  self.drawLine(lineObj, x1, x2, -y1, -y2, style = 9)
174  self.drawLine(lineObj, -x1, -x2, -y1, -y2, style = 9)
175  self.drawLine(lineObj, -x1, -x2, y1, y2, style = 9)
176 
177  x1 = x1 + xBaseStep
178  x2 = x2 + xBaseStep
179 
180  # mark zero ROC
181  zeroModuleHeight = yBaseStep if isBarrel else yBaseStep * 0.5 # because there are two panels height of roc is smaller
182 
183  yRange = int(yMax) if isBarrel else int(yMax) * 2
184 
185  x1_base = 0 + xMin
186  x2_base = xBaseStep / float(rocsInRow) + xMin
187  y1_base = zeroModuleHeight + yMin
188  y2_base = 2 * zeroModuleHeight + yMin
189 
190  for i in range(yRange):
191  y1 = y1_base + i * (zeroModuleHeight * 2) - (zeroModuleHeight if i % 2 else 0)
192  y2 = y2_base + i * (zeroModuleHeight * 2) - (zeroModuleHeight if i % 2 else 0)
193 
194  #negative ladders/blades
195  for j in range(int(xMax)):
196  x1 = x1_base + j * xBaseStep
197  x2 = x2_base + j * xBaseStep
198  if yMax == 6.5 and x1 <0:
199  y1 = y1_base + i * (zeroModuleHeight * 2) + (zeroModuleHeight if i % 2 else 0)
200  y2 = y2_base + i * (zeroModuleHeight * 2) + (zeroModuleHeight if i % 2 else 0)
201  self.drawRectangle(lineObj,(xBaseStep+x1-(x2-x1)),(xBaseStep+x2-(x2-x1)), y1+(y1-y2), y2+(y1-y2), color=8)
202  x1, x2 = -x1, -x2
203  yPosChange = -zeroModuleHeight if i % 2 else zeroModuleHeight
204  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)
205  else:
206  self.drawRectangle(lineObj, x1, x2, y1, y2, color=8)
207 
208  x1, x2 = -x1, -x2
209  yPosChange = -zeroModuleHeight if i % 2 else zeroModuleHeight
210  self.drawRectangle(lineObj, x1, x2, y1 - yPosChange, y2 - yPosChange, color=8)
211 
212 
213  # positive ladders/blades
214  y1 = y1 - yMin + yBaseStep
215  y2 = y2 - yMin + yBaseStep
216 
217  for j in range(int(xMax)):
218  x1 = x1_base + j * xBaseStep
219  x2 = x2_base + j * xBaseStep
220 
221  if yMax== 6.5 and x1 <0:
222  self.drawRectangle(lineObj, xBaseStep+x1-(x2-x1), xBaseStep+x2-(x2-x1), y1+(y1-y2), y2+(y1-y2), color=8)
223  x1, x2 = -x1, -x2
224  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)
225  else:
226  self.drawRectangle(lineObj, x1, x2, y1, y2, color=8)
227  x1, x2 = -x1, -x2
228  self.drawRectangle(lineObj, x1, x2, y1 - yPosChange, y2 - yPosChange, color=8)
229 
230 # hist.GetZaxis().SetRangeUser(-0.5,15.5)
231 
232  def saveHistograms(self):
233  for hists in [self.barrelHists, self.forwardHists]:
234  for hist in hists:
235  # if hist.GetEntries():
236  c1 = ROOT.TCanvas(hist.GetName(), hist.GetName(), hRes, vRes)
237  if colorCoded:
238  hist.GetZaxis().SetRangeUser(0,5)
239  ROOT.gStyle.SetPalette(55)
240  elif pixelAlive:
241  hist.GetZaxis().SetRangeUser(0,4160)
242  ROOT.gStyle.SetPalette(70)
243  hist.Draw()
244 
245  txt = TLatex()
246  txt.SetNDC()
247  txt.SetTextFont(1)
248  txt.SetTextColor(1)
249  txt.SetTextAlign(22)
250  txt.SetTextAngle(0)
251  txt.SetTextSize(0.05)
252  txt.DrawLatex(0.5, 0.95, hist.GetName())
253 
254  xMin = hist.GetXaxis().GetXmin()
255 
256  yMin = hist.GetYaxis().GetXmin()
257 
258  box1 = TBox(xMin*1.1,yMin*1.25,xMin*1,yMin*1.15);
259  box1.SetFillColor(kRed+3)
260  box1.Draw()
261  txt.SetTextSize(0.035)
262  txt.DrawLatex(0.25, 0.077, "Dead At Beginning")
263 
264 
265  box2 = TBox(xMin*0.45,yMin*1.25,xMin*0.35,yMin*1.15);
266  box2.SetFillColor(kAzure+2)
267  box2.Draw()
268  txt.SetTextSize(0.035)
269  txt.DrawLatex(0.47, 0.077, "Dead At End")
270 
271 
272  self.prettifyCanvas(hist)
273  colorString = ""
274  if colorCoded:
275  colorString = "_coded"
276  elif pixelAlive:
277  colorString = "_pixelalive"
278  if useFileSuffix:
279  c1.Print(hist.GetName() + colorString + "_" + inputFileName[:-4] + ".png")
280  else:
281  c1.Print(hist.GetName() + colorString + ".png")
282 
283 
284 
285 class Barrel:
286  def __init__ (self, part, sector, layer, ladder, module, roc, reason="unknown"):
287  self.part = part
288  self.sector = sector
289  self.layer = layer
290  self.ladder = ladder
291  self.module = module
292  self.roc = roc
293  self.isCoverted = False
294  self.reason = reason
295  def __str__(self):
296  return str([self.part, self.sector, self.layer, self.ladder, self.module])
297  def convertParts(self):
298  if not self.isCoverted:
299  self.ladder = -self.ladder if self.part % 2 else self.ladder
300  self.module = -self.module if self.part <= 2 else self.module
301  isConverted = True
302  def GetXYCoords(self):
303 
304  xBase = -0.625 + ((maxRocIdx - self.roc if self.roc >= rocsInRow else self.roc) + 1) * rocXLen
305 
306  flipY = False
307  if self.module < 0:
308  if self.ladder < 0:
309  if abs(self.ladder) % 2:
310  flipY = True
311  else:
312  if self.ladder % 2 == 0:
313  flipY = True
314  else:
315  if self.ladder < 0:
316  if abs(self.ladder) % 2 == 0:
317  flipY = True
318  else:
319  if self.ladder % 2:
320  flipY = True
321 
322  tmpRoc = maxRocIdx - self.roc if flipY else self.roc;
323 
324  yBase = -0.5 * (tmpRoc // rocsInRow)
325 
326  x = self.module + (xBase if self.module < 0 else -xBase - rocXLen)
327  y = self.ladder + yBase
328 
329  #print("roc=%d\t: (%f;%f)"%(self.roc, x, y))
330 
331  return x, y
332 
333 class Forward:
334  def __init__ (self, part, disk, blade, panel, ring, roc, reason="unknown"):
335  self.part = part
336  self.disk = disk
337  self.blade = blade
338  self.panel = panel
339  self.ring = ring
340  self.roc = roc
341  self.reason = reason
342  self.isCoverted = False
343  def __str__(self):
344  return str([self.part, self.disk, self.blade, self.panel, self.ring])
345  def convertParts(self):
346  if not self.isCoverted:
347  self.blade = -self.blade if self.part % 2 else self.blade
348  self.disk = -self.disk if self.part <= 2 else self.disk
349  self.isCoverted = True
350  def GetXYCoords(self):
351 
352  xBase = -0.625 + ((maxRocIdx - self.roc if self.roc >= rocsInRow else self.roc) + 1) * rocXLen
353 
354  x = self.disk + (xBase if self.disk < 0 else -xBase - rocXLen)
355 
356  flipY = (self.panel == 2 if self.disk < 0 else self.panel == 1)
357 
358  tmpRoc = maxRocIdx - self.roc if flipY else self.roc;
359 
360  yBase = -0.25 - 0.25 * (tmpRoc // rocsInRow) + 0.5 * (self.panel - 1)
361 
362  y = self.blade + yBase
363 
364  # print("roc=%d\t: (%f;%f)"%(self.roc, x, y))
365  return x, y
366 
367 def TranslatePartString(thePartStr):
368  if thePartStr == "mO":
369  return 1
370  elif thePartStr == "mI":
371  return 2
372  elif thePartStr == "pO":
373  return 3
374  elif thePartStr == "pI":
375  return 4
376  else:
377  print("Unrecognized part <%s>, the script is likely to crash..." % (thePartStr))
378 
379 def TranslateReasonStringBPix(theReasonStr):
380  if theReasonStr == "unknown":
381  return 1
382  elif theReasonStr == "notprogrammable":
383  return 1
384  elif theReasonStr == "vcthr":
385  return 2
386  elif theReasonStr == "pixelalive":
387  return 2
388  elif theReasonStr == "iana":
389  return 2
390  elif theReasonStr == "calib":
391  return 2
392  elif theReasonStr== "fedphases":
393  return 4
394  elif theReasonStr == "tbmdelay":
395  return 1
396  elif theReasonStr == "power":
397  return 5
398  else:
399  return 1
400  print("Unrecognized part <%s>, the script is likely to crash..." % (theReasonStr))
401 
402 def TranslateReasonStringFPix(theReasonStr):
403  if theReasonStr == "flaky":
404  return 1
405  elif theReasonStr == "power": #check github for the real reason
406  return 5
407  elif theReasonStr == "tbmdelay": #
408  return 1
409  elif theReasonStr == "unknown":
410  return 2
411  else:
412  return 2
413  print("Unrecognized part <%s>, the script is likely to crash..." % (theReasonStr))
414 
415 
416 def GetOnlineBarrelCharacteristics(detElements, roc, reason="unknown"):
417  onlinePart = int(detElements[1][1:]) if useNumberAsPartName else TranslatePartString(detElements[1][1:])
418  onlineSector = int(detElements[2][3:])
419  onlineLayer = int(detElements[3][3:])
420 
421  if detElements[4][-1] == "H" or detElements[4][-1] == "F":
422  onlineLadder = int(detElements[4][3:-1])
423  else:
424  onlineLadder = int(detElements[4][3:])
425 
426  onlineModule = int(detElements[5][3:])
427 
428  return Barrel(*[onlinePart, onlineSector, onlineLayer, onlineLadder, onlineModule, roc, reason])
429 
430 def GetOnlineForwardCharacteristics(detElements, roc, reason="unknown"):
431  onlinePart = int(detElements[1][1:]) if useNumberAsPartName else TranslatePartString(detElements[1][1:])
432  onlineDisk = int(detElements[2][1:])
433  onlineBlade = int(detElements[3][3:])
434  onlinePanel = int(detElements[4][3:])
435  onlineRing = int(detElements[5][3:])
436 
437  return Forward(*[onlinePart, onlineDisk, onlineBlade, onlinePanel, onlineRing, roc, reason])
438 
439 
440 def GetAffectedRocs(rocString):
441 
442  rocString = str(rocString)
443  iComma=rocString.find(",")
444  listOfRocs = []
445 
446  if iComma!=-1:
447  listOfRocs.extend(GetAffectedRocs(rocString[0:iComma]))
448  listOfRocs.extend(GetAffectedRocs(rocString[iComma+1:len(rocString)]))
449  else:
450  iHyphen=rocString.find("-")
451  if iHyphen!=-1:
452  start=int(rocString[0:iHyphen])
453  end=int(rocString[iHyphen+1:len(rocString)])+1
454  listOfRocs.extend(range(start,end))
455  else:
456  return [int(rocString)]
457 
458  return listOfRocs
459 
460 
461 
462 
463 
464 histMan = HistogramManager()
465 barrelObjs = []
466 forwardObjs = []
467 
468 if len(sys.argv) > 1:
469  inputFileName = sys.argv[1]
470  print(inputFileName)
471 
472  if len(sys.argv) > 2:
473  opts, args = getopt.getopt(sys.argv[2:], "bscp", ["help", "output="])
474  for o, a in opts:
475  if o == "-b":
476  useNumberAsPartName = False
477  if o == "-s":
478  useFileSuffix = True
479  if o == "-c":
480  colorCoded = True
481  if o == "-p":
482  pixelAlive = True
483 
484 i = 1
485 with open (inputFileName, "r") as inputFile:
486 
487  for item in inputFile:
488 # print("Processing record #%d" % (i))
489 
490  inputs = item.split(" ")
491 
492  if len(inputs) >= 2: # but take only first 2 elements (ignore others like '\n')
493 
494  detElements = inputs[0].split("_")
495  if detElements[3]=='LYR1' and (detElements[1]=='BmI' or detElements[1]=='BmO'):
496 
497  rocs = []
498  roc = GetAffectedRocs(inputs[1])
499  for roc_rotate in roc:
500  if int(str(roc_rotate)) <= 7:
501  rocs.append(int(str(roc_rotate))+8)
502  elif int(str(roc_rotate)) >= 8:
503  rocs.append(int(str(roc_rotate)) -8)
504  else:
505  rocs = GetAffectedRocs(inputs[1])
506 # rocs = GetAffectedRocs(inputs[1]) #int(inputs[1]) #- 1 #shifts 0..16 rocNum to 0..15
507 
508  if len(inputs) == 3:
509  reason = str(inputs[2]).lower().strip()
510  else:
511  reason="unknown"
512 
513  for roc in rocs:
514  if detElements[0][0] == "B":
515  barrelObj = GetOnlineBarrelCharacteristics(detElements, roc, reason)
516  #print(barrelObj)
517  barrelObj.convertParts()
518  # print(barrelObj)
519  barrelObjs.append(barrelObj)
520  elif detElements[0][0] == "F":
521  forwardObj = GetOnlineForwardCharacteristics(detElements, roc, reason)
522  # print(forwardObj)
523  forwardObj.convertParts()
524  # print(forwardObj)
525  forwardObjs.append(forwardObj)
526  else:
527  print("Not recognized part type")
528 
529  i = i + 1
530 
531 histMan.fillHistograms(barrelObjs, forwardObjs)
532 histMan.saveHistograms()
def TranslateReasonStringFPix(theReasonStr)
def GetOnlineForwardCharacteristics(detElements, roc, reason="unknown")
def GetOnlineBarrelCharacteristics(detElements, roc, reason="unknown")
def TranslateReasonStringBPix(theReasonStr)
def drawLine(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1)
def fillHistograms(self, barrelObjs, forwardObjs)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def __init__(self, part, disk, blade, panel, ring, roc, reason="unknown")
def GetAffectedRocs(rocString)
def __init__(self, part, sector, layer, ladder, module, roc, reason="unknown")
def drawRectangle(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1)
def TranslatePartString(thePartStr)
#define str(s)