示例#1
0
    def saveHistos(self, norm):
        '''
    This is to save the histograms of the sample in a plot
    '''
        if debug: print('Info in Sample.saveHistos()')
        c = TCanvas(self.name, self.name, 800, 600)
        c.DivideSquare(len(self.histos.keys()))

        for i, what in enumerate(self.histos.keys()):
            h = self.histos[what]
            pad = c.cd(i + 1)
            pad.SetLogx(self.histoDefs[what].logX)
            pad.SetLogy(self.histoDefs[what].logY)
            if norm and h.Integral() != 0:
                hdrawn = h.DrawNormalized('LPE')
            else:
                hdrawn = h.Draw('PLE')

            norm_suffix = '_norm' if norm else ''

        c.SaveAs('./plots/' + self.label + suffix + '/' + c.GetName() +
                 norm_suffix + '.png')
        c.SaveAs('./plots/' + self.label + suffix + '/' + c.GetName() +
                 norm_suffix + '.pdf')
        c.SaveAs('./plots/' + self.label + suffix + '/' + c.GetName() +
                 norm_suffix + '.C')
示例#2
0
    def plotHistos(self, norm=False, sameCanvas=False):
        ''' 
    Superimpose histograms for all samples, on the same canvas or on separate canvases
    '''
        if sameCanvas:
            c = TCanvas(self.name, self.name, 6400, 4800)
            tosave = {c.GetName(): c}
        else:
            c = None
            tosave = {}

        legends = []

        hMax = {}
        for j, what in enumerate(self.samples[0].histos.keys()):
            hMax[what] = []
            leg = defaultLegend(x1=0.15, y1=0.7, x2=0.95, y2=0.95, mult=1.2)
            legends.append(leg)
        # do the actual plotting
        for i, sample in enumerate(self.samples):
            if i == 0:
                opt = 'histE'
                if sameCanvas: c.DivideSquare(len(sample.histos.keys()))
            else: opt = 'histEsame'
            for j, what in enumerate(sample.histos.keys()):
                h = sample.histos[what]
                graph_saver.append(h)

                if sameCanvas: pad = c.cd(j + 1)
                else:
                    cname = '%s_%s' % (self.name, what)
                    if cname not in tosave.keys():
                        tosave[cname] = TCanvas(cname, cname, 700, 600)
                    pad = tosave[cname].cd()

                if i == 0:
                    pad.SetLogx(sample.histoDefs[what].logX)
                    pad.SetLogy(sample.histoDefs[what].logY)
                if norm and h.Integral():
                    hdrawn = h.DrawNormalized(opt)
                    hMax[what].append(hdrawn)
                    hdrawn.SetLineColor(self.colors[i])
                    hdrawn.SetMarkerColor(self.colors[i])
                    hdrawn.SetMarkerStyle(self.styles[i])
                    hdrawn.SetMarkerSize(3)
                    hdrawn.SetLineWidth(2)
                    legends[j].AddEntry(hdrawn, sample.legname, 'LP')

                else:
                    h.Draw(opt)
                    hMax[what].append(h)
                    h.SetLineColor(self.colors[i])
                    h.SetMarkerColor(self.colors[i])
                    h.SetMarkerStyle(self.styles[i])
                    h.SetMarkerSize(4)
                    h.SetLineWidth(2)
                    legends[j].AddEntry(hdrawn, sample.legname, 'LP')

                if i == len(self.samples) - 1:
                    legends[j].Draw('same')

        # compute the max of norm histo and set the range accordingly
        for j, what in enumerate(self.samples[0].histos.keys()):
            if len(hMax[what]) > 0:
                max = 0
                for ih in hMax[what]:
                    if (max < ih.GetMaximum()):
                        max = ih.GetMaximum()
                if (sample.histoDefs[what].logY == True):
                    #hMax[name][0].GetYaxis().SetRangeUser(0.01, max * 7)
                    hMax[what][0].SetMaximum(max * 30)
                    #print hMax[what][0].GetMaximum()
                else:
                    #hMax[name][0].GetYaxis().SetRangeUser(0.01, max * 1.5)
                    hMax[what][0].SetMaximum(max * 1.7)

        norm_suffix = '_norm' if norm else ''

        for cname, canv in tosave.items():
            canv.SaveAs('./plots/' + self.label + suffix + '/' + cname +
                        norm_suffix + '.png')
            canv.SaveAs('./plots/' + self.label + suffix + '/' + cname +
                        norm_suffix + '.pdf')
            canv.SaveAs('./plots/' + self.label + suffix + '/' + cname +
                        norm_suffix + '.C')