Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:05

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import sys
0005 import os
0006 from ROOT import gROOT, TArrow, TCanvas, TGraph, TH2Poly, gPad, TLatex
0007 from copy import deepcopy
0008 from array import array
0009 
0010 gROOT.SetBatch() # don't pop up canvases
0011 
0012 maxPxBarrel = 4
0013 maxPxForward = 3
0014 barrelLadderShift = [0, 14, 44, 90]
0015 forwardDiskXShift = [25, 75, 125]
0016 forwardDiskYShift = 45; # to make +DISK on top in the 'strip-like' layout
0017 plotWidth, plotHeight = 3000, 2000
0018 listOfDetids=[]
0019 
0020 ##############################################
0021 #Find Data files
0022 def getFileInPath(rfile):
0023 ##############################################
0024    import os 
0025    for dir in os.environ['CMSSW_SEARCH_PATH'].split(":"):
0026      if os.path.exists(os.path.join(dir,rfile)): return os.path.join(dir,rfile)                                                                          
0027    return None
0028 
0029 ##############################################
0030 def __AddNamedBins(BaseTrackerMap, geoFile, tX, tY, sX, sY, applyModuleRotation = False):
0031 ##############################################
0032     for line in geoFile:
0033         lineSpl = line.strip().split("\"")
0034       
0035         detId = lineSpl[0].split(" ")[0]
0036       
0037         vertices = lineSpl[1]
0038         xy = vertices.split(" ")
0039         x, y = array('d'), array('d')
0040         verNum = 1
0041         for coord in xy:
0042             coordSpl = coord.split(",")
0043             if applyModuleRotation:
0044                 x.append(-(float(coordSpl[0]) * sX + tX))
0045                 y.append((float(coordSpl[1]) * sY + tY))
0046             else:
0047                 x.append(float(coordSpl[0]) * sX + tX)
0048                 y.append(float(coordSpl[1]) * sY + tY)
0049             verNum = verNum + 1
0050             #close polygon
0051         x.append(x[0])
0052         y.append(y[0])
0053       
0054         #print(detId, [p for p in x], [q for q in y])
0055         listOfDetids.append(detId)
0056         if applyModuleRotation:
0057             bin = TGraph(verNum, y, x)
0058         else:
0059             bin = TGraph(verNum, x, y)
0060             # bin = TGraph(verNum, y, x) # rotation by 90 deg (so that it had the same layout as for the strips)
0061         bin.SetName(detId)
0062         BaseTrackerMap.AddBin(bin)
0063 
0064 ##############################################
0065 def main():
0066 ##############################################
0067     geometryFilenames = []
0068     for i in range(maxPxBarrel):
0069         geometryFilenames.append(getFileInPath("DQM/SiStripMonitorClient/data/Geometry/vertices_barrel_" + str(i + 1))) 
0070 
0071     for i in range(-maxPxForward, maxPxForward + 1):
0072         if i == 0:
0073             continue #there is no 0 disk
0074         geometryFilenames.append(getFileInPath("DQM/SiStripMonitorClient/data/Geometry/vertices_forward_" + str(i)))
0075 
0076     BaseTrackerMap = TH2Poly("Summary", "", -10, 160, -70, 70)
0077     BaseTrackerMap.SetFloat(1)
0078     BaseTrackerMap.GetXaxis().SetTitle("")
0079     BaseTrackerMap.GetYaxis().SetTitle("")
0080     BaseTrackerMap.SetOption("COLZ L")
0081     BaseTrackerMap.SetStats(0)
0082   
0083     # BARREL FIRST
0084     for i in range(maxPxBarrel):
0085        with open(geometryFilenames[i], "r") as geoFile:
0086           currBarrelTranslateX = 0
0087           currBarrelTranslateY = barrelLadderShift[i]
0088           __AddNamedBins(BaseTrackerMap,geoFile, currBarrelTranslateX, currBarrelTranslateY, 1, 1, True)
0089 
0090     # MINUS FORWARD
0091     for i in range(-maxPxForward, 0):
0092         with open(geometryFilenames[maxPxBarrel + maxPxForward + i], "r") as geoFile:
0093             currForwardTranslateX = forwardDiskXShift[-i - 1]
0094             currForwardTranslateY = -forwardDiskYShift        
0095             __AddNamedBins(BaseTrackerMap,geoFile, currForwardTranslateX, currForwardTranslateY, 1, 1)
0096         
0097     # PLUS FORWARD
0098     for i in range(maxPxForward):
0099         with open(geometryFilenames[maxPxBarrel + maxPxForward + i], "r") as geoFile:
0100             currForwardTranslateX = forwardDiskXShift[i]
0101             currForwardTranslateY = forwardDiskYShift
0102             __AddNamedBins(BaseTrackerMap,geoFile, currForwardTranslateX, currForwardTranslateY, 1, 1)
0103        
0104     print("Base Tracker Map: constructed")
0105 
0106     c1 = TCanvas("c1","c1", plotWidth , plotHeight)
0107     for detid in listOfDetids:
0108        BaseTrackerMap.Fill(str(detid),int(detid))
0109     BaseTrackerMap.Draw("AC COLZ L")        
0110               
0111     gPad.Update()
0112     palette = BaseTrackerMap.FindObject("palette");
0113     palette.SetX1NDC(0.89);
0114     palette.SetX2NDC(0.91);
0115     palette.SetLabelSize(0.05);
0116     gPad.Update()
0117 
0118     ### z arrow
0119     arrow = TArrow(0.05, 27.0, 0.05, -30.0, 0.02, "|>")
0120     arrow.SetLineWidth(4)
0121     arrow.Draw()
0122     ### phi arrow
0123     phiArrow = TArrow(0.0, 27.0, 30.0, 27.0, 0.02, "|>")
0124     phiArrow.SetLineWidth(4)
0125     phiArrow.Draw()
0126     ### x arrow
0127     xArrow = TArrow(25.0, 44.5, 50.0, 44.5, 0.02, "|>")
0128     xArrow.SetLineWidth(4)
0129     xArrow.Draw()
0130     ### y arrow
0131     yArrow = TArrow(25.0, 44.5, 25.0, 69.5, 0.02, "|>")
0132     yArrow.SetLineWidth(4)
0133     yArrow.Draw()
0134 
0135     ###################################################
0136     # add some captions
0137     txt = TLatex()
0138     txt.SetNDC()
0139     txt.SetTextFont(1)
0140     txt.SetTextColor(1)
0141     txt.SetTextAlign(22)
0142     txt.SetTextAngle(0)
0143 
0144     # draw new-style title
0145     txt.SetTextSize(0.05)
0146     txt.DrawLatex(0.5, 0.95, "Pixel Tracker Map")
0147     txt.SetTextSize(0.03)
0148     txt.DrawLatex(0.5, 0.125, "-DISK")
0149     txt.DrawLatex(0.5, 0.075, "NUMBER ->")
0150     txt.DrawLatex(0.5, 0.875, "+DISK")
0151     txt.DrawLatex(0.12, 0.35, "+z")
0152     txt.DrawLatexNDC(0.315, 0.665, "+phi") # WAY TO FORCE IT TO DRAW LATEX CORRECTLY NOT FOUND ('#' DOESN'T WORK)
0153     txt.DrawLatex(0.38, 0.73, "+x")
0154     txt.DrawLatex(0.235, 0.875, "+y")
0155     txt.SetTextAngle(90)
0156     txt.DrawLatex(0.125, 0.5, "BARREL")
0157 
0158     #save to the png
0159     c1.Print("test.png")
0160 
0161 ##################################################
0162 if __name__ == "__main__":        
0163     main()