Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:55:58

0001 import ROOT
0002 ROOT.gROOT.SetBatch(True)
0003 from setTDRStyle import setTDRStyle
0004 
0005 
0006 from granularity import *
0007 
0008 
0009 class ValidationPlotter:
0010     def __init__(self):
0011         setTDRStyle()
0012         self.inFiles = {}
0013         self.labels = {}
0014         self.colors = {}
0015         self.markers = {}
0016         self.outPath = None
0017         self.granularity = standardGranularity
0018         self.validationGranularity = validationGranularity
0019 
0020         self.order = []
0021         
0022     def addInputFile(self, label, inFile, color=None, marker=20):
0023         self.order.append(label)
0024         self.inFiles[label] = inFile
0025         self.labels[label] = label
0026         self.markers[label] = marker
0027         if color != None:
0028             self.colors[label] = color
0029         else:
0030             # choose first not occupied color (other than white)
0031             for autoColor in range(1,100):
0032                 if autoColor not in self.colors.values() and not autoColor == 10:
0033                     self.colors[label] = autoColor
0034                     break
0035     
0036     def setGranularity(self, granularity):
0037         self.granularity = granularity
0038     
0039     def setOutputPath(self, outPath):
0040         self.outPath = outPath
0041         
0042     def convertName(self, name):
0043         out = name.replace("Bpix", "BPIX")
0044         out = out.replace("Fpix", "FPIX")
0045         out = out.replace("Plus", "+")
0046         out = out.replace("Minus", "-")
0047         out = out.replace("Fpix", "FPIX")
0048         out = out.replace("Tib", "TIB")
0049         out = out.replace("Tob", "TOB")
0050         out = out.replace("Tid", "TID")
0051         out = out.replace("Tec", "TEC")
0052         out = out.replace("Layer", " L")
0053         out = out.replace("Ring", " R")
0054         out = out.replace("Stereo", "S")
0055         out = out.replace("Rphi", "R") # different from Ring, this one does not add a space in front
0056         out = out.replace("In", "i")
0057         out = out.replace("Out", "o")
0058         return out
0059     
0060     def plotHist(self, folder, name, title, hists, twoDimensional=False):
0061         self.canvas = ROOT.TCanvas("canvas", "canvas", ROOT.gStyle.GetCanvasDefW(),ROOT.gStyle.GetCanvasDefH())
0062         ROOT.gPad.SetRightMargin(0.10)
0063         if twoDimensional:
0064             ROOT.gPad.SetRightMargin(0.2)
0065             
0066         
0067         legend = ROOT.TLegend(0.2,0.7,0.9,0.90)
0068         legend.SetFillColor(0)
0069         legend.SetFillStyle(0)
0070         legend.SetTextSize(0.03)
0071         legend.SetMargin(0.15)
0072         legend.SetNColumns(2)
0073         legend.SetBorderSize(0)
0074         
0075         normalize = False
0076         if len (hists) > 1:
0077             normalize = True
0078         
0079 
0080         firstHist = True
0081         scaleHist = None
0082         maximum = 0
0083         for hist, label in hists:
0084             
0085             n = int(hist.Integral())
0086             mu = hist.GetMean()
0087             sigma = hist.GetRMS()
0088             
0089             if normalize:
0090                 hist.Scale(1./hist.Integral())
0091             
0092             if hist.GetMaximum() > maximum:
0093                 maximum = hist.GetMaximum()
0094             
0095             if firstHist:
0096                 scaleHist = hist
0097                 addDraw = ""
0098                 firstHist = False
0099             else:
0100                 addDraw = "same"
0101             
0102             if not twoDimensional:
0103                 if self.markers[label] != 0:
0104                     drawMode = "P0%s"%(addDraw)
0105                 else:
0106                     drawMode = "hist%s"%(addDraw)
0107             else:
0108                 drawMode = "COLZ"
0109                 
0110             scaleHist.SetMaximum(maximum*1.5)
0111             scaleHist.SetMinimum(0)
0112             
0113             hist.Draw(drawMode)
0114             
0115             legText = "#splitline{{{label}}}{{N={n:.2E},#mu={mu:.2f},RMS={sigma:.2f}}}".format(label=label,n=n, mu=mu,sigma=sigma)
0116             
0117             if self.markers[label] != 0:
0118                 legend.AddEntry(hist, legText, "p")
0119             else:
0120                 legend.AddEntry(hist, legText, "l")
0121         
0122         if not twoDimensional:
0123             legend.Draw()
0124         
0125         cmsText = ROOT.TLatex(0.26,0.96,title)
0126         cmsText.SetTextFont(42)
0127         cmsText.SetNDC()
0128         cmsText.Draw("same")
0129         
0130         import os
0131         if not os.path.isdir(self.outPath):
0132             os.makedirs(self.outPath)
0133         if not os.path.isdir(self.outPath+"/"+folder):
0134             os.makedirs(self.outPath+"/"+folder)
0135         self.canvas.SaveAs("{}/{}/{}.pdf".format(self.outPath, folder, name))
0136         self.canvas = None
0137             
0138         
0139         
0140     def makeResidualPlot(self, sectorNumber, coordinate):
0141         # residual
0142         hists = []
0143         for label in self.order:
0144             fi = ROOT.TFile(self.inFiles[label], "READ")
0145             hist = fi.Get("ApeEstimator1/Sector_{sectorNumber}/Results/h_Res{coordinate}".format(sectorNumber=sectorNumber, coordinate=coordinate))
0146             hist.SetLineColor(self.colors[label])
0147             hist.SetMarkerColor(self.colors[label])
0148             hist.SetMarkerStyle(self.markers[label])
0149             hist.SetDirectory(0)
0150             if self.markers[label] == 0:
0151                 hist.SetMarkerSize(0)
0152                 hist.SetLineWidth(2)
0153                 
0154             hists.append((hist, label))
0155             nameHist = fi.Get("ApeEstimator1/Sector_{sectorNumber}/z_name".format(sectorNumber=sectorNumber))
0156             nameHist.SetDirectory(0)
0157             title = self.convertName(nameHist.GetTitle())
0158             fi.Close()
0159             
0160         name = "Sector_{sectorNumber}_Res{coordinate}".format(sectorNumber=sectorNumber, coordinate=coordinate)
0161         
0162         self.plotHist("residuals", name, title, hists)
0163         
0164         
0165         # normalized residual
0166         hists = []
0167         for label in self.order:
0168             fi = ROOT.TFile(self.inFiles[label], "READ")
0169             hist = fi.Get("ApeEstimator1/Sector_{sectorNumber}/Results/h_NorRes{coordinate}".format(sectorNumber=sectorNumber, coordinate=coordinate))
0170             hist.SetLineColor(self.colors[label])
0171             hist.SetMarkerColor(self.colors[label])
0172             hist.SetMarkerStyle(self.markers[label])
0173             hist.SetDirectory(0)
0174             if self.markers[label] == 0:
0175                 hist.SetMarkerSize(0)
0176                 hist.SetLineWidth(2)
0177                 
0178             hists.append((hist, label))
0179             nameHist = fi.Get("ApeEstimator1/Sector_{sectorNumber}/z_name".format(sectorNumber=sectorNumber))
0180             nameHist.SetDirectory(0)
0181             title = self.convertName(nameHist.GetTitle())
0182             fi.Close()
0183         name = "Sector_{sectorNumber}_NorRes{coordinate}".format(sectorNumber=sectorNumber, coordinate=coordinate)
0184         
0185         self.plotHist("residuals", name, title, hists)
0186         
0187     def makeTrackPlot(self,histName, twoDimensional=False):
0188         hists = []
0189         for label in self.order:
0190             fi = ROOT.TFile(self.inFiles[label], "READ")
0191             hist = fi.Get("ApeEstimator2/TrackVariables/{histName}".format(histName=histName))
0192             hist.SetLineColor(self.colors[label])
0193             hist.SetMarkerColor(self.colors[label])
0194             hist.SetMarkerStyle(self.markers[label])
0195             hist.SetDirectory(0)
0196             if self.markers[label] == 0:
0197                 hist.SetMarkerSize(0)
0198                 hist.SetLineWidth(2)
0199             fi.Close()
0200             if twoDimensional:
0201                 self.plotHist("tracks", histName+"_"+label, label, [(hist, label),], twoDimensional=True)
0202             else:
0203                 hists.append((hist, label))
0204         if len(hists) > 0:
0205             self.plotHist("tracks", histName, histName, hists, twoDimensional=twoDimensional)
0206             
0207     def makeHitsPlot(self, sectorNumber, histName, twoDimensional=False):
0208         hists = []
0209         for label in self.order:
0210             fi = ROOT.TFile(self.inFiles[label], "READ")
0211             sectorName = fi.Get("ApeEstimator2/Sector_{sector}/z_name".format(sector=sectorNumber)).GetTitle()
0212             hist = fi.Get("ApeEstimator2/Sector_{sector}/{histName}".format(sector=sectorNumber,histName=histName))
0213             hist.SetLineColor(self.colors[label])
0214             hist.SetMarkerColor(self.colors[label])
0215             hist.SetMarkerStyle(self.markers[label])
0216             hist.SetDirectory(0)
0217             fi.Close()
0218             if self.markers[label] == 0:
0219                 hist.SetMarkerSize(0)
0220                 hist.SetLineWidth(2)
0221             
0222             if twoDimensional:
0223                 self.plotHist("hits"+"/{}".format(sectorName), histName+"_"+label, "{}: ".format(sectorName)+histName+" "+label, [(hist, label),], twoDimensional=True)
0224             else:
0225                 hists.append((hist, label))
0226         if len(hists) > 0:
0227             self.plotHist("hits"+"/{}".format(sectorName), histName, "{}: ".format(sectorName)+histName, hists, twoDimensional=twoDimensional)
0228             
0229     
0230     
0231     def draw(self):
0232         for coordinate in self.granularity.sectors.keys():
0233             rangeList = self.granularity.sectors[coordinate]
0234             for first, last in rangeList:
0235                 for i in range(first, last+1):
0236                     self.makeResidualPlot(i, coordinate)
0237                     
0238         self.makeTrackPlot("h_hitsSize")
0239         self.makeTrackPlot("h_hitsValid")
0240         self.makeTrackPlot("h_hitsInvalid")
0241         self.makeTrackPlot("h_hits2D")
0242         self.makeTrackPlot("h_layersMissed")
0243         self.makeTrackPlot("h_hitsPixel")
0244         self.makeTrackPlot("h_hitsStrip")
0245         self.makeTrackPlot("h_charge")
0246         self.makeTrackPlot("h_chi2")
0247         self.makeTrackPlot("h_ndof")
0248         self.makeTrackPlot("h_norChi2")
0249         self.makeTrackPlot("h_prob")
0250         self.makeTrackPlot("h_eta")
0251         self.makeTrackPlot("h_etaErr")
0252         self.makeTrackPlot("h_theta")
0253         self.makeTrackPlot("h_phi")
0254         self.makeTrackPlot("h_phiErr")
0255         self.makeTrackPlot("h_d0Beamspot")
0256         self.makeTrackPlot("h_d0BeamspotErr")
0257         self.makeTrackPlot("h_dz")
0258         self.makeTrackPlot("h_dzErr")
0259         self.makeTrackPlot("h_pt")
0260         self.makeTrackPlot("h_ptErr")
0261         self.makeTrackPlot("h_meanAngle")
0262         self.makeTrackPlot("h_hitsGood")
0263         # these don't have several histograms in one plot
0264         self.makeTrackPlot("h2_meanAngleVsHits", twoDimensional=True)
0265         self.makeTrackPlot("h2_hitsGoodVsHitsValid", twoDimensional=True)
0266         self.makeTrackPlot("h2_hitsPixelVsEta", twoDimensional=True)
0267         self.makeTrackPlot("h2_hitsPixelVsTheta", twoDimensional=True)
0268         self.makeTrackPlot("h2_hitsStripVsEta", twoDimensional=True)
0269         self.makeTrackPlot("h2_hitsStripVsTheta", twoDimensional=True)
0270         self.makeTrackPlot("h2_ptVsTheta", twoDimensional=True)
0271         
0272         rangeList = self.validationGranularity.sectors["X"]
0273         for first, last in rangeList:
0274             for i in range(first, last+1):
0275                 # Get list of plots per sector, as pixel sectors have different plots
0276                 # We get the list from one of the result files, the rest should have the same structure
0277                 directoryFile = ROOT.TFile(self.inFiles[self.order[0]], "READ")
0278                 directory = directoryFile.GetDirectory("ApeEstimator2/Sector_{}".format(i))
0279                 
0280                 for histObject in directory.GetListOfKeys():
0281                     histName = histObject.GetName()
0282                     if histName.startswith("h_"): # 1D hist
0283                         self.makeHitsPlot(i, histName)
0284                     elif histName.startswith("h2_"):
0285                         self.makeHitsPlot(i, histName, twoDimensional=True)
0286 
0287                 directoryFile.Close()
0288                 
0289 
0290 def main():
0291     pass
0292 
0293 
0294 if __name__ == "__main__":
0295     main()