CMS 3D CMS Logo

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