Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:10

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