Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:33:04

0001 #!/usr/bin/env python3 
0002 
0003 from __future__ import print_function
0004 import ROOT
0005 import sys
0006 import getopt
0007 from ROOT import *
0008 
0009 from copy import deepcopy
0010 
0011 ROOT.gROOT.SetBatch()        # don't pop up canvases
0012 # ROOT.gROOT.SetStyle('Plain') # white background
0013 
0014 #####################################################
0015 ### GLOBAL VARS
0016 #
0017 rocsInCol = 2
0018 rocsInRow = 8;
0019 rocXLen = 1.0 / rocsInRow
0020 
0021 maxRocIdx = rocsInCol * rocsInRow - 1 # 0...15
0022 
0023 onlineMaxLadder = [6, 14, 22, 32]
0024 onlineMaxBlade = [11, 17]
0025 
0026 maxOnlineModule = 4
0027 maxOnlineDisk = 3
0028 #
0029 useNumberAsPartName = False  # does it accept numbers or mO, pI etc.
0030 inputFileName = "input.dat" #default file name
0031 hRes, vRes = 1920, 1080
0032 useFileSuffix = False
0033 colorCoded = False
0034 pixelAlive = False
0035 ### GLOBAL VARS
0036 
0037 class HistogramManager:
0038   def __init__(self):
0039     self.barrelHists = []
0040     self.forwardHists = []
0041     
0042     # barrel histograms
0043     i = 1
0044     for maxLadder in onlineMaxLadder:
0045       nBinsX = (maxOnlineModule * 2 + 1) * rocsInRow
0046       nBinsY = (maxLadder * 2 + 1) * rocsInCol
0047 
0048       name = "PXBarrel_Layer" + str(i);
0049       title = "PXBarrel_Layer" + str(i);
0050 
0051       histObj = ROOT.TH2F(name, title, nBinsX, -(maxOnlineModule + 0.5), maxOnlineModule + 0.5, nBinsY, -(maxLadder + 0.5), maxLadder + 0.5)
0052       histObj.GetXaxis().SetTitle("OnlineSignedModule");
0053       histObj.GetYaxis().SetTitle("OnlineSignedLadder");
0054       histObj.SetOption("COLZ")
0055       histObj.SetStats(0)
0056       histObj.SetTitle("OnlineSignedModule")
0057 
0058       self.barrelHists.append(deepcopy(histObj))
0059       i = i + 1
0060 
0061     # forward histograms
0062     i = 1
0063     for maxBlade in onlineMaxBlade:
0064       nBinsX = (maxOnlineDisk * 2 + 1) * rocsInRow
0065       nBinsY = (maxBlade * 2 + 1) * 2 * rocsInCol
0066 
0067       name = "PXForward_Ring" + str(i);
0068       title = "PXForward_Ring" + str(i);
0069 
0070       histObj = ROOT.TH2F(name, title, nBinsX, -(maxOnlineDisk + 0.5), maxOnlineDisk + 0.5, nBinsY, -(maxBlade + 0.5), maxBlade + 0.5)
0071       histObj.GetXaxis().SetTitle("OnlineSignedDisk");
0072       histObj.GetYaxis().SetTitle("OnlineSignedBladePanel");
0073       histObj.SetOption("COLZ")
0074       histObj.SetStats(0)
0075       
0076 
0077       self.forwardHists.append(deepcopy(histObj))
0078       i = i + 1
0079 
0080   def fillHistograms(self, barrelObjs, forwardObjs):
0081     for b in barrelObjs:
0082       coord = b.GetXYCoords()
0083       histRef = self.barrelHists[b.layer - 1]
0084       
0085       binNum = histRef.FindBin(coord[0], coord[1])
0086       if colorCoded:
0087         binContent = TranslateReasonStringBPix(b.reason)
0088       elif pixelAlive:
0089         binContent = float(b.reason)
0090       else:
0091         binContent = b.roc + 1
0092 
0093       histRef.SetBinContent(binNum, binContent)
0094       # self.barrelHists[b.layer - 1].Fill(coord[0], coord[1], b.roc + 1) 
0095 
0096     for f in forwardObjs:
0097       coord = f.GetXYCoords()
0098       histRef = self.forwardHists[f.ring - 1]
0099       
0100       binNum = histRef.FindBin(coord[0], coord[1])
0101 
0102       if colorCoded:
0103         binContent = TranslateReasonStringFPix(f.reason)
0104       else:
0105         binContent = f.roc + 1
0106       histRef.SetBinContent(binNum, binContent)      
0107 
0108       # self.forwardHists[f.ring - 1].Fill(coord[0], coord[1], f.roc + 1)
0109   
0110   def drawLine(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1):  
0111     lineObj.SetLineWidth(width)
0112     lineObj.SetLineStyle(style)
0113     lineObj.SetLineColor(color)
0114 
0115     lineObj.DrawLine(x1, y1, x2, y2)
0116 
0117   def drawRectangle(self, lineObj, x1, x2, y1, y2, width=2, style=1, color=1):
0118     self.drawLine(lineObj, x1, x2, y2, y2, width, style, color)
0119     self.drawLine(lineObj, x2, x2, y2, y1, width, style, color)
0120     self.drawLine(lineObj, x2, x1, y1, y1, width, style, color)
0121     self.drawLine(lineObj, x1, x1, y1, y2, width, style, color)
0122 
0123   def prettifyCanvas(self, hist):
0124     nBinsX = hist.GetXaxis().GetNbins()
0125     xMin = hist.GetXaxis().GetXmin()
0126     xMax = hist.GetXaxis().GetXmax()
0127     nBinsY = hist.GetYaxis().GetNbins()
0128     yMin = hist.GetYaxis().GetXmin()
0129     yMax = hist.GetYaxis().GetXmax()
0130 
0131     xLen = int(xMax)
0132     yLen = int(yMax)
0133 
0134     name = hist.GetName()[0:3]
0135     isBarrel = True if name != "PXF" else False
0136     print((name, isBarrel))
0137 
0138     xBaseStep = 1
0139     xRange = (nBinsX - 1) // (rocsInRow * 2) + 1
0140     yBaseStep = (yMax - yMin) / nBinsY
0141     yRange = (nBinsY - 1) // (2) + 1
0142     if not isBarrel:
0143       yBaseStep = yBaseStep * 2
0144       yRange = yRange // 2
0145 
0146     # horizontal 
0147     x1 = xMin
0148     x2 = xMin + xLen
0149     y1 = yMin
0150     y2 = yMin
0151 
0152     lineObj = ROOT.TLine()
0153     lineObj.SetBit(ROOT.kCanDelete)
0154 
0155     for i in range(yRange):
0156       w = 1 if i % 2 else 2
0157       self.drawLine(lineObj, x1, x2, y1, y2, w)
0158       self.drawLine(lineObj, x1, x2, -y1, -y2, w)
0159       self.drawLine(lineObj, -x1, -x2, -y1, -y2,w )
0160       self.drawLine(lineObj, -x1, -x2, y1, y2, w)
0161 
0162       y1 = y1 + yBaseStep
0163       y2 = y2 + yBaseStep
0164 
0165     # vertical
0166     x1 = xMin
0167     x2 = xMin
0168     y1 = yMin
0169     y2 = yMin + yLen
0170 
0171     for i in range(xRange):
0172       self.drawLine(lineObj, x1, x2, y1, y2, style = 9)
0173       self.drawLine(lineObj, x1, x2, -y1, -y2, style = 9)
0174       self.drawLine(lineObj, -x1, -x2, -y1, -y2, style = 9)
0175       self.drawLine(lineObj, -x1, -x2, y1, y2, style = 9)
0176 
0177       x1 = x1 + xBaseStep
0178       x2 = x2 + xBaseStep
0179 
0180     # mark zero ROC
0181     zeroModuleHeight = yBaseStep if isBarrel else yBaseStep * 0.5 # because there are two panels height of roc is smaller
0182 
0183     yRange = int(yMax) if isBarrel else int(yMax) * 2
0184     
0185     x1_base = 0                         + xMin
0186     x2_base = xBaseStep / float(rocsInRow) + xMin
0187     y1_base = zeroModuleHeight          + yMin
0188     y2_base = 2 * zeroModuleHeight      + yMin
0189 
0190     for i in range(yRange):
0191       y1 = y1_base + i * (zeroModuleHeight * 2) - (zeroModuleHeight if i % 2 else 0)
0192       y2 = y2_base + i * (zeroModuleHeight * 2) - (zeroModuleHeight if i % 2 else 0)
0193       
0194       #negative ladders/blades
0195       for j in range(int(xMax)):
0196         x1 = x1_base + j * xBaseStep
0197         x2 = x2_base + j * xBaseStep
0198         if yMax == 6.5 and x1 <0:
0199           y1 = y1_base + i * (zeroModuleHeight * 2) + (zeroModuleHeight if i % 2 else 0)
0200           y2 = y2_base + i * (zeroModuleHeight * 2) + (zeroModuleHeight if i % 2 else 0)
0201           self.drawRectangle(lineObj,(xBaseStep+x1-(x2-x1)),(xBaseStep+x2-(x2-x1)), y1+(y1-y2), y2+(y1-y2), color=8)
0202           x1, x2 = -x1, -x2
0203           yPosChange = -zeroModuleHeight if i % 2 else zeroModuleHeight
0204           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)
0205         else:
0206           self.drawRectangle(lineObj, x1, x2, y1, y2, color=8)
0207 
0208           x1, x2 = -x1, -x2
0209           yPosChange = -zeroModuleHeight if i % 2 else zeroModuleHeight
0210           self.drawRectangle(lineObj, x1, x2, y1 - yPosChange, y2 - yPosChange, color=8)
0211 
0212 
0213       # positive ladders/blades
0214       y1 = y1 - yMin + yBaseStep
0215       y2 = y2 - yMin + yBaseStep
0216 
0217       for j in range(int(xMax)):
0218         x1 = x1_base + j * xBaseStep
0219         x2 = x2_base + j * xBaseStep
0220 
0221         if yMax== 6.5 and x1 <0:
0222           self.drawRectangle(lineObj, xBaseStep+x1-(x2-x1), xBaseStep+x2-(x2-x1), y1+(y1-y2), y2+(y1-y2), color=8)
0223           x1, x2 = -x1, -x2
0224           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)
0225         else:
0226           self.drawRectangle(lineObj, x1, x2, y1, y2, color=8)
0227           x1, x2 = -x1, -x2
0228           self.drawRectangle(lineObj, x1, x2, y1 - yPosChange, y2 - yPosChange, color=8)
0229 
0230 #      hist.GetZaxis().SetRangeUser(-0.5,15.5)
0231 
0232   def saveHistograms(self):
0233     for hists in [self.barrelHists, self.forwardHists]:
0234       for hist in hists:
0235         # if hist.GetEntries():
0236         c1 = ROOT.TCanvas(hist.GetName(), hist.GetName(), hRes, vRes)
0237         if colorCoded:
0238           hist.GetZaxis().SetRangeUser(0,5)
0239           ROOT.gStyle.SetPalette(55)
0240         elif pixelAlive:
0241           hist.GetZaxis().SetRangeUser(0,4160)
0242           ROOT.gStyle.SetPalette(70)
0243         hist.Draw()
0244 
0245         txt = TLatex()
0246         txt.SetNDC()
0247         txt.SetTextFont(1)
0248         txt.SetTextColor(1)
0249         txt.SetTextAlign(22)
0250         txt.SetTextAngle(0)
0251         txt.SetTextSize(0.05)
0252         txt.DrawLatex(0.5, 0.95, hist.GetName())
0253 
0254         xMin = hist.GetXaxis().GetXmin()
0255 
0256         yMin = hist.GetYaxis().GetXmin()
0257 
0258         box1 = TBox(xMin*1.1,yMin*1.25,xMin*1,yMin*1.15);
0259         box1.SetFillColor(kRed+3)
0260         box1.Draw()
0261         txt.SetTextSize(0.035)
0262         txt.DrawLatex(0.25, 0.077, "Dead At Beginning")
0263 
0264 
0265         box2 = TBox(xMin*0.45,yMin*1.25,xMin*0.35,yMin*1.15);
0266         box2.SetFillColor(kAzure+2)
0267         box2.Draw()
0268         txt.SetTextSize(0.035)
0269         txt.DrawLatex(0.47, 0.077, "Dead At End")
0270 
0271 
0272         self.prettifyCanvas(hist)
0273         colorString = ""
0274         if colorCoded:
0275           colorString = "_coded"
0276         elif pixelAlive:
0277           colorString = "_pixelalive"
0278         if useFileSuffix:
0279           c1.Print(hist.GetName() + colorString + "_" + inputFileName[:-4] + ".png")
0280         else:
0281           c1.Print(hist.GetName() + colorString + ".png")
0282             
0283 #####################################################
0284 
0285 class Barrel:
0286   def __init__ (self, part, sector, layer, ladder, module, roc, reason="unknown"):
0287     self.part = part
0288     self.sector = sector
0289     self.layer = layer
0290     self.ladder = ladder
0291     self.module = module
0292     self.roc = roc
0293     self.isCoverted = False
0294     self.reason = reason
0295   def __str__(self):
0296     return str([self.part, self.sector, self.layer, self.ladder, self.module])
0297   def convertParts(self):
0298     if not self.isCoverted:
0299       self.ladder = -self.ladder if self.part % 2 else self.ladder
0300       self.module = -self.module if self.part <= 2 else self.module
0301       isConverted = True
0302   def GetXYCoords(self):
0303 
0304     xBase = -0.625 + ((maxRocIdx - self.roc if self.roc >= rocsInRow else self.roc) + 1) * rocXLen
0305 
0306     flipY = False
0307     if self.module < 0:
0308       if self.ladder < 0:
0309         if abs(self.ladder) % 2:
0310           flipY = True
0311       else:
0312         if self.ladder % 2 == 0:
0313           flipY = True
0314     else:
0315       if self.ladder < 0:
0316         if abs(self.ladder) % 2 == 0:
0317           flipY = True
0318       else:
0319         if self.ladder % 2:
0320           flipY = True
0321 
0322     tmpRoc = maxRocIdx - self.roc if flipY else self.roc;
0323 
0324     yBase = -0.5 * (tmpRoc // rocsInRow)
0325 
0326     x = self.module + (xBase if self.module < 0 else -xBase - rocXLen)
0327     y = self.ladder + yBase   
0328             
0329     #print("roc=%d\t: (%f;%f)"%(self.roc, x, y))
0330     
0331     return x, y    
0332 
0333 class Forward:
0334   def __init__ (self, part, disk, blade, panel, ring, roc, reason="unknown"):
0335     self.part = part
0336     self.disk = disk
0337     self.blade = blade
0338     self.panel = panel
0339     self.ring = ring
0340     self.roc = roc
0341     self.reason = reason
0342     self.isCoverted = False
0343   def __str__(self):
0344     return str([self.part, self.disk, self.blade, self.panel, self.ring])
0345   def convertParts(self):
0346     if not self.isCoverted:
0347       self.blade = -self.blade if self.part % 2 else self.blade
0348       self.disk = -self.disk if self.part <= 2 else self.disk 
0349       self.isCoverted = True
0350   def GetXYCoords(self):
0351 
0352     xBase = -0.625 + ((maxRocIdx - self.roc if self.roc >= rocsInRow else self.roc) + 1) * rocXLen
0353 
0354     x = self.disk + (xBase if self.disk < 0 else -xBase - rocXLen)
0355     
0356     flipY = (self.panel == 2 if self.disk < 0 else self.panel == 1)
0357 
0358     tmpRoc = maxRocIdx - self.roc if flipY else self.roc;
0359 
0360     yBase = -0.25 - 0.25 * (tmpRoc // rocsInRow) + 0.5 * (self.panel - 1)
0361 
0362     y = self.blade + yBase
0363 
0364     # print("roc=%d\t: (%f;%f)"%(self.roc, x, y))
0365     return x, y
0366 
0367 def TranslatePartString(thePartStr):
0368   if thePartStr == "mO":
0369     return 1
0370   elif thePartStr == "mI":
0371     return 2
0372   elif thePartStr == "pO":
0373     return 3
0374   elif thePartStr == "pI":
0375     return 4
0376   else:
0377     print("Unrecognized part <%s>, the script is likely to crash..." % (thePartStr))
0378 
0379 def TranslateReasonStringBPix(theReasonStr):
0380   if theReasonStr == "unknown":
0381     return 1
0382   elif theReasonStr == "notprogrammable":
0383     return 1
0384   elif theReasonStr == "vcthr":
0385     return 2
0386   elif theReasonStr == "pixelalive":
0387     return 2
0388   elif theReasonStr == "iana":
0389     return 2
0390   elif theReasonStr == "calib":
0391     return 2
0392   elif theReasonStr== "fedphases":
0393     return 4
0394   elif theReasonStr == "tbmdelay":
0395     return 1
0396   elif theReasonStr == "power":
0397     return 5
0398   else:
0399     return 1
0400     print("Unrecognized part <%s>, the script is likely to crash..." % (theReasonStr))
0401 
0402 def TranslateReasonStringFPix(theReasonStr):
0403   if theReasonStr == "flaky":
0404     return 1
0405   elif theReasonStr == "power":   #check github for the real reason
0406     return 5
0407   elif theReasonStr == "tbmdelay":  #
0408     return 1
0409   elif theReasonStr == "unknown":
0410     return 2
0411   else:
0412     return 2
0413     print("Unrecognized part <%s>, the script is likely to crash..." % (theReasonStr))
0414 
0415     
0416 def GetOnlineBarrelCharacteristics(detElements, roc, reason="unknown"):
0417   onlinePart = int(detElements[1][1:]) if useNumberAsPartName else TranslatePartString(detElements[1][1:])
0418   onlineSector = int(detElements[2][3:])
0419   onlineLayer = int(detElements[3][3:])
0420   
0421   if detElements[4][-1] == "H" or detElements[4][-1] == "F":
0422     onlineLadder = int(detElements[4][3:-1])
0423   else:
0424     onlineLadder = int(detElements[4][3:])
0425     
0426   onlineModule = int(detElements[5][3:])
0427 
0428   return Barrel(*[onlinePart, onlineSector, onlineLayer, onlineLadder, onlineModule, roc, reason])
0429 
0430 def GetOnlineForwardCharacteristics(detElements, roc, reason="unknown"):
0431   onlinePart = int(detElements[1][1:]) if useNumberAsPartName else TranslatePartString(detElements[1][1:])
0432   onlineDisk = int(detElements[2][1:])
0433   onlineBlade = int(detElements[3][3:])
0434   onlinePanel = int(detElements[4][3:])
0435   onlineRing = int(detElements[5][3:])
0436 
0437   return Forward(*[onlinePart, onlineDisk, onlineBlade, onlinePanel, onlineRing, roc, reason])
0438 
0439 
0440 def GetAffectedRocs(rocString):
0441 
0442     rocString = str(rocString)
0443     iComma=rocString.find(",")
0444     listOfRocs = []
0445     
0446     if iComma!=-1:
0447         listOfRocs.extend(GetAffectedRocs(rocString[0:iComma]))
0448         listOfRocs.extend(GetAffectedRocs(rocString[iComma+1:len(rocString)]))
0449     else:
0450         iHyphen=rocString.find("-")
0451         if iHyphen!=-1:
0452             start=int(rocString[0:iHyphen])
0453             end=int(rocString[iHyphen+1:len(rocString)])+1
0454             listOfRocs.extend(range(start,end))
0455         else:
0456             return [int(rocString)]
0457         
0458     return listOfRocs
0459     
0460 
0461 
0462 #####################################################
0463 
0464 histMan = HistogramManager()
0465 barrelObjs = []
0466 forwardObjs = []
0467 
0468 if len(sys.argv) > 1:
0469   inputFileName = sys.argv[1]
0470   print(inputFileName)
0471   
0472   if len(sys.argv) > 2:
0473     opts, args = getopt.getopt(sys.argv[2:], "bscp", ["help", "output="])
0474     for o, a in opts:
0475       if o == "-b":
0476         useNumberAsPartName = False
0477       if o == "-s":
0478         useFileSuffix = True
0479       if o == "-c":
0480         colorCoded = True
0481       if o == "-p":
0482         pixelAlive = True
0483 
0484 i = 1
0485 with open (inputFileName, "r") as inputFile:
0486 
0487   for item in inputFile:
0488 #    print("Processing record #%d" % (i))
0489     
0490     inputs = item.split(" ")
0491 
0492     if len(inputs) >= 2: # but take only first 2 elements (ignore others like '\n')
0493 
0494       detElements = inputs[0].split("_")
0495       if detElements[3]=='LYR1' and (detElements[1]=='BmI' or detElements[1]=='BmO'):
0496 
0497         rocs = []
0498         roc = GetAffectedRocs(inputs[1])
0499         for roc_rotate in roc:
0500           if int(str(roc_rotate)) <= 7:
0501             rocs.append(int(str(roc_rotate))+8)
0502           elif int(str(roc_rotate)) >= 8:
0503             rocs.append(int(str(roc_rotate)) -8)
0504       else:
0505         rocs = GetAffectedRocs(inputs[1])
0506 #      rocs = GetAffectedRocs(inputs[1]) #int(inputs[1]) #- 1 #shifts 0..16 rocNum to 0..15
0507 
0508       if len(inputs) == 3:
0509         reason = str(inputs[2]).lower().strip()
0510       else:
0511         reason="unknown"
0512 
0513       for roc in rocs:
0514         if detElements[0][0] == "B":
0515           barrelObj = GetOnlineBarrelCharacteristics(detElements, roc, reason)
0516           #print(barrelObj)
0517           barrelObj.convertParts()
0518           # print(barrelObj)
0519           barrelObjs.append(barrelObj)
0520         elif detElements[0][0] == "F":
0521           forwardObj = GetOnlineForwardCharacteristics(detElements, roc, reason)
0522           # print(forwardObj)
0523           forwardObj.convertParts()
0524           # print(forwardObj)
0525           forwardObjs.append(forwardObj)
0526         else:
0527           print("Not recognized part type")
0528           
0529         i = i + 1
0530 
0531 histMan.fillHistograms(barrelObjs, forwardObjs)
0532 histMan.saveHistograms()