Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 import ROOT
0002 ROOT.gROOT.SetBatch(True)
0003 from setTDRStyle import setTDRStyle
0004 
0005 # load some default things form there
0006 from granularity import *
0007 
0008 
0009 class IterationsPlotter:
0010     def __init__(self):
0011         setTDRStyle()
0012         self.inFile = None
0013         self.outPath = None
0014         self.granularity = standardGranularity
0015         self.title = ""
0016         
0017     def setInputFile(self, inFile):
0018         self.inFile = inFile
0019     
0020     def setGranularity(self, granularity):
0021         self.granularity = granularity
0022     
0023     def setOutputPath(self, outPath):
0024         self.outPath = outPath
0025     
0026     def setTitle(self, title):
0027         self.title = title
0028     
0029     def convertName(self, name):
0030         out = name.replace("Bpix", "BPIX")
0031         out = out.replace("Fpix", "FPIX")
0032         out = out.replace("Plus", "+")
0033         out = out.replace("Minus", "-")
0034         out = out.replace("Fpix", "FPIX")
0035         out = out.replace("Tib", "TIB")
0036         out = out.replace("Tob", "TOB")
0037         out = out.replace("Tid", "TID")
0038         out = out.replace("Tec", "TEC")
0039         out = out.replace("Layer", " L")
0040         out = out.replace("Ring", " R")
0041         out = out.replace("Stereo", "S")
0042         out = out.replace("Rphi", "R") # different from Ring, this one does not add a space in front
0043         out = out.replace("In", "i")
0044         out = out.replace("Out", "o")
0045         return out
0046     
0047     def makeHists(self, sectorRange, coordinate):
0048         sectors = list(range(sectorRange[0],sectorRange[1]+1))
0049         numSectors = len(sectors)
0050         
0051         fi = ROOT.TFile(self.inFile, "READ")
0052         nameTree = fi.Get("nameTree")
0053         nameTree.GetEntry(0)
0054         apeTree = fi.Get("iterTree{}".format(coordinate))
0055         noEntries = apeTree.GetEntries()
0056         
0057         hists = []
0058         names = []
0059         maximum = 10
0060         for i,sector in enumerate(sectors):
0061             hist = ROOT.TH1F("hist{}_{}".format(coordinate, sector), "", noEntries, 0-0.5, noEntries-0.5)
0062             hist.SetTitle(";iteration number;#sigma_{align," + coordinate.lower() + "}  [#mum]")
0063             #~ hist.SetAxisRange(0.,100.,"Y")
0064             hist.SetMarkerStyle(20+i)
0065             hist.SetDirectory(0)
0066             hists.append(hist)
0067             no_it = 1
0068             for it in apeTree:
0069                 hist.SetBinContent(no_it, 10000. * (float(getattr(it,  "Ape_Sector_{}".format(sector))))**0.5)
0070                 no_it += 1
0071             if hist.GetMaximum() > maximum:
0072                 maximum = hist.GetMaximum()
0073                 
0074             sectorName = self.convertName(str(getattr(nameTree, "Ape_Sector_{}".format(sector))))
0075             names.append(sectorName)
0076         
0077 
0078         
0079         fi.Close()
0080         return hists, names, maximum
0081         
0082     def draw(self):
0083         for coordinate in self.granularity.sectors.keys():
0084             rangeList = self.granularity.sectors[coordinate]
0085             for j, sectorRange in enumerate(rangeList):
0086                 self.canvas = ROOT.TCanvas("canvas", "canvas", int(ROOT.gStyle.GetCanvasDefW()*15/10.),ROOT.gStyle.GetCanvasDefH())
0087                 ROOT.gPad.SetRightMargin(0.10)
0088                 
0089                 legend = ROOT.TLegend(0.2,0.73,0.85,0.93)
0090                 legend.SetFillColor(0)
0091                 legend.SetFillStyle(0)
0092                 legend.SetTextSize(0.025)
0093                 legend.SetMargin(0.30)
0094                 legend.SetBorderSize(0)
0095                 legend.SetNColumns(4)
0096                 
0097                 hists, names, maximum = self.makeHists(sectorRange, coordinate)
0098                 for i, hist in enumerate(hists):
0099                     if i == 0:
0100                         drawOption = "P0L"
0101                     else:
0102                         drawOption = "P0Lsame"
0103                     hist.SetMaximum(maximum*1.5)
0104                     hist.Draw(drawOption)
0105                     legend.AddEntry(hist, names[i], "PL")
0106                 legend.Draw()
0107                 
0108                 cmsText = ROOT.TLatex(0.16,0.96,self.title)
0109                 cmsText.SetTextFont(42)
0110                 cmsText.SetNDC()
0111                 cmsText.Draw("same")
0112                 
0113                 granularityText = ROOT.TLatex(0.9,0.96,self.granularity.names[coordinate][j])
0114                 granularityText.SetTextAlign(31)
0115                 granularityText.SetTextFont(42)
0116                 granularityText.SetNDC()
0117                 granularityText.Draw("same")
0118                 
0119                 import os
0120                 if not os.path.isdir(self.outPath):
0121                     os.makedirs(self.outPath)
0122                 
0123                 self.canvas.SaveAs("{}/iterations_{}_{}.pdf".format(self.outPath, coordinate, self.granularity.names[coordinate][j]))
0124                 self.canvas = None
0125 def main():
0126     pass
0127 
0128 if __name__ == "__main__":
0129     main()