Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
import ROOT
ROOT.gROOT.SetBatch(True)
from setTDRStyle import setTDRStyle

# load some default things form there
from granularity import *


class IterationsPlotter:
    def __init__(self):
        setTDRStyle()
        self.inFile = None
        self.outPath = None
        self.granularity = standardGranularity
        self.title = ""
        
    def setInputFile(self, inFile):
        self.inFile = inFile
    
    def setGranularity(self, granularity):
        self.granularity = granularity
    
    def setOutputPath(self, outPath):
        self.outPath = outPath
    
    def setTitle(self, title):
        self.title = title
    
    def convertName(self, name):
        out = name.replace("Bpix", "BPIX")
        out = out.replace("Fpix", "FPIX")
        out = out.replace("Plus", "+")
        out = out.replace("Minus", "-")
        out = out.replace("Fpix", "FPIX")
        out = out.replace("Tib", "TIB")
        out = out.replace("Tob", "TOB")
        out = out.replace("Tid", "TID")
        out = out.replace("Tec", "TEC")
        out = out.replace("Layer", " L")
        out = out.replace("Ring", " R")
        out = out.replace("Stereo", "S")
        out = out.replace("Rphi", "R") # different from Ring, this one does not add a space in front
        out = out.replace("In", "i")
        out = out.replace("Out", "o")
        return out
    
    def makeHists(self, sectorRange, coordinate):
        sectors = list(range(sectorRange[0],sectorRange[1]+1))
        numSectors = len(sectors)
        
        fi = ROOT.TFile(self.inFile, "READ")
        nameTree = fi.Get("nameTree")
        nameTree.GetEntry(0)
        apeTree = fi.Get("iterTree{}".format(coordinate))
        noEntries = apeTree.GetEntries()
        
        hists = []
        names = []
        maximum = 10
        for i,sector in enumerate(sectors):
            hist = ROOT.TH1F("hist{}_{}".format(coordinate, sector), "", noEntries, 0-0.5, noEntries-0.5)
            hist.SetTitle(";iteration number;#sigma_{align," + coordinate.lower() + "}  [#mum]")
            #~ hist.SetAxisRange(0.,100.,"Y")
            hist.SetMarkerStyle(20+i)
            hist.SetDirectory(0)
            hists.append(hist)
            no_it = 1
            for it in apeTree:
                hist.SetBinContent(no_it, 10000. * (float(getattr(it,  "Ape_Sector_{}".format(sector))))**0.5)
                no_it += 1
            if hist.GetMaximum() > maximum:
                maximum = hist.GetMaximum()
                
            sectorName = self.convertName(str(getattr(nameTree, "Ape_Sector_{}".format(sector))))
            names.append(sectorName)
        

        
        fi.Close()
        return hists, names, maximum
        
    def draw(self):
        for coordinate in self.granularity.sectors.keys():
            rangeList = self.granularity.sectors[coordinate]
            for j, sectorRange in enumerate(rangeList):
                self.canvas = ROOT.TCanvas("canvas", "canvas", int(ROOT.gStyle.GetCanvasDefW()*15/10.),ROOT.gStyle.GetCanvasDefH())
                ROOT.gPad.SetRightMargin(0.10)
                
                legend = ROOT.TLegend(0.2,0.73,0.85,0.93)
                legend.SetFillColor(0)
                legend.SetFillStyle(0)
                legend.SetTextSize(0.025)
                legend.SetMargin(0.30)
                legend.SetBorderSize(0)
                legend.SetNColumns(4)
                
                hists, names, maximum = self.makeHists(sectorRange, coordinate)
                for i, hist in enumerate(hists):
                    if i == 0:
                        drawOption = "P0L"
                    else:
                        drawOption = "P0Lsame"
                    hist.SetMaximum(maximum*1.5)
                    hist.Draw(drawOption)
                    legend.AddEntry(hist, names[i], "PL")
                legend.Draw()
                
                cmsText = ROOT.TLatex(0.16,0.96,self.title)
                cmsText.SetTextFont(42)
                cmsText.SetNDC()
                cmsText.Draw("same")
                
                granularityText = ROOT.TLatex(0.9,0.96,self.granularity.names[coordinate][j])
                granularityText.SetTextAlign(31)
                granularityText.SetTextFont(42)
                granularityText.SetNDC()
                granularityText.Draw("same")
                
                import os
                if not os.path.isdir(self.outPath):
                    os.makedirs(self.outPath)
                
                self.canvas.SaveAs("{}/iterations_{}_{}.pdf".format(self.outPath, coordinate, self.granularity.names[coordinate][j]))
                self.canvas = None
def main():
    pass

if __name__ == "__main__":
    main()