コード例 #1
0
def makePlots():

    # gROOT.SetStyle('Plain') # white background
    # gStyle.SetOptStat(222211)

    ROOT.gROOT.ProcessLine('.L ../tdrstyle.C')
    ROOT.gROOT.Reset()
    ROOT.gROOT.ProcessLine('setTDRStyle()')  #Set CMS TDR style

    inputFile = TFile(output_name + '.root', 'READ')

    histos_SR1 = dict()
    histos_SR2 = dict()
    histos_LSB = dict()
    histos_RSB = dict()
    histos_MC = dict()

    keys = [
        'photon_pt', 'photon_eta', 'dimuon_pt', 'dimuon_rapidity', 'chib_pt'
    ]

    for key in keys:
        histos_SR1[key] = inputFile.Get(key + '_SR1')
        histos_SR2[key] = inputFile.Get(key + '_SR2')
        histos_LSB[key] = inputFile.Get(key + '_LSB')
        histos_RSB[key] = inputFile.Get(key + '_RSB')
        histos_MC[key] = inputFile.Get(key + '_MC')

    titles = dict(
        photon_pt='photon E_{T};E_{T}(#gamma) [GeV];Entries / 100 MeV',
        photon_eta='photon_eta;#eta(#gamma);Entries / 0.05',
        dimuon_pt='#Upsilon p_{T};p_{T}(#Upsilon) [GeV];Entries / GeV',
        dimuon_rapidity='#Upsilon rapidity(#Upsilon);y(#Upsilon);Entries / 0.1',
        chib_pt='#chi_{b} p_{T};p_{T}(#chi_{b}) [GeV];Entries / GeV',
    )

    titles_chib = dict(
        photon_pt='photon E_{T};E_{T}(#gamma) [GeV];A.U.',
        photon_eta='photon_eta;#eta(#gamma);A.U.',
        dimuon_pt='#Upsilon p_{T};p_{T}(#Upsilon) [GeV];A.U.',
        dimuon_rapidity='#Upsilon rapidity(#Upsilon);y(#Upsilon);A.U.',
        chib_pt='#chi_{b} p_{T};p_{T}(#chi_{b}) [GeV];A.U.',
    )

    logY = dict(
        photon_pt=0,
        photon_eta=0,
        dimuon_pt=0,
        dimuon_rapidity=0,
        chib_pt=0,
    )

    maxY = dict(
        photon_pt=750,
        photon_eta=350,
        dimuon_pt=800,
        dimuon_rapidity=400,
        chib_pt=800,
    )

    integralData1 = histos_SR1[keys[0]].Integral()
    integralData2 = histos_SR2[keys[0]].Integral()
    integralData = integralData1 + integralData2

    for histo in histos_LSB.values() + histos_RSB.values() + histos_MC.values(
    ):
        if (histo.Integral() != 0):
            histo.Scale(1. / histo.Integral())
            histo.Rebin(3)
    for histo in histos_SR1.values() + histos_SR2.values():
        histo.Rebin(3)

    fBG = 0.48  #0.28
    fLSB = 0.5

    histos_BG = dict()
    for key in keys:
        histos_BG[key] = histos_RSB[key].Clone()
        histos_BG[key].SetNameTitle(key + '_BG', 'bgd from side-bands')
        histos_BG[key].Scale(1 - fLSB)
        histos_BG[key].Add(histos_LSB[key], fLSB)

    histos_Signal1 = dict()
    histos_Signal2 = dict()
    histos_Signal = dict()
    histos_SR = dict()
    for key in keys:
        histos_Signal1[key] = histos_SR1[key].Clone()
        histos_Signal1[key].SetNameTitle(key + '_signal1', 'Signal1')
        histos_Signal1[key].Add(histos_BG[key], -1 * integralData1 * fBG)

        histos_Signal2[key] = histos_SR2[key].Clone()
        histos_Signal2[key].SetNameTitle(key + '_signal2', 'Signal2')
        histos_Signal2[key].Add(histos_BG[key], -1 * integralData2 * fBG)

        histos_Signal[key] = histos_Signal1[key].Clone()
        histos_Signal[key].Add(histos_Signal2[key])
        histos_Signal[key].SetNameTitle(key + '_signal', 'data bkg-subtracted')

        histos_SR[key] = histos_SR1[key].Clone()
        histos_SR[key].Add(histos_SR2[key])
        histos_SR[key].SetNameTitle(key + '_SR', 'data')

    for histo in histos_Signal.values() + histos_BG.values():
        if (histo.Integral() != 0):
            histo.Scale(1. / histo.Integral())

    # Normalize SR to their original value, MC to be comparable with signal background-subtracted and background such that Signal+background = SR
    for histo in histos_MC.values():
        histo.Scale(integralData * (1 - fBG))
    for histo in histos_BG.values():
        histo.Scale(integralData * fBG)
    for histo in histos_LSB.values():
        histo.Scale(integralData * fBG * fLSB)
    for histo in histos_RSB.values():
        histo.Scale(integralData * fBG * (1 - fLSB))

    chi2Prob = dict()
    kolmogorovProb = dict()
    h_ratio = dict()
    for key in histos_Signal.keys():
        h_ratio[key] = histos_SR[key].Clone()
        histo_sum_MC_BG = histos_MC[key].Clone()
        histo_sum_MC_BG.Add(histos_BG[key])
        h_ratio[key].Divide(histo_sum_MC_BG)
        h_ratio[key].SetNameTitle(key + '_ratio', titles[key])
        h_ratio[key].SetMarkerStyle(20)
        h_ratio[key].SetMarkerSize(1)
        h_ratio[key].GetYaxis().SetLabelSize(0.18)
        h_ratio[key].GetYaxis().SetTitle('DATA/MC')
        h_ratio[key].GetYaxis().SetTitleSize(0.22)
        h_ratio[key].GetYaxis().SetTitleOffset(0.2)
        h_ratio[key].GetYaxis().SetNdivisions(005)
        h_ratio[key].GetXaxis().SetLabelSize(0.16)
        h_ratio[key].GetXaxis().SetTitleSize(0.16)
        h_ratio[key].SetMaximum(1.5)
        h_ratio[key].SetMinimum(0.667)
        chi2Prob[key] = histos_SR[key].Chi2Test(
            histo_sum_MC_BG)  #, 'UW')#, 'NORM')
        kolmogorovProb[key] = histos_SR[key].KolmogorovTest(histo_sum_MC_BG)

    for histo in histos_SR.values():
        histo.SetLineColor(ROOT.kBlack)
        histo.SetLineWidth(2)
        histo.SetMarkerColor(ROOT.kBlack)
        histo.SetMarkerStyle(20)

    for histo in histos_SR1.values() + histos_Signal1.values():
        histo.SetLineColor(ROOT.kBlue)
        histo.SetLineWidth(2)
        histo.SetMarkerColor(ROOT.kBlue)
        histo.SetMarkerStyle(22)

    for histo in histos_SR2.values() + histos_Signal2.values():
        histo.SetLineColor(ROOT.kRed)
        histo.SetLineWidth(2)
        histo.SetMarkerColor(ROOT.kRed)
        histo.SetMarkerStyle(23)

    for histo in histos_Signal.values():
        histo.SetLineColor(ROOT.kBlack)
        histo.SetLineWidth(2)
        # histo.SetMarkerColor(ROOT.kBlack)
        # histo.SetMarkerStyle(21)

    for histo in histos_BG.values():
        histo.SetLineColor(ROOT.kRed)
        histo.SetLineWidth(2)
        histo.SetFillColor(ROOT.kRed)
        # histo.SetMarkerColor(ROOT.kRed)
        # histo.SetMarkerStyle(22)

    # for histo in histos_RSB.values():
    #     histo.SetLineColor(ROOT.kViolet)
    #     histo.SetLineWidth(2)

    for histo in histos_MC.values():
        histo.SetLineColor(ROOT.kBlue)
        histo.SetLineWidth(2)
        histo.SetFillColor(ROOT.kBlue)
        # histo.SetMarkerColor(ROOT.kBlue)
        # histo.SetMarkerStyle(20)

    outputFile = TFile(output_name + '_plots.root', 'RECREATE')
    for histo in histos_Signal.values() + histos_MC.values(
    ) + histos_BG.values() + histos_SR.values():
        histo.Write()
    outputFile.Close()

    canvas = TCanvas("canvas", "canvas")
    canvas.Print(outputFile_name + "[")  #apre file .ps

    for key in keys:
        pad1 = TPad("pad1", "The pad 80% of the height", 0.0, 0.2, 1.0, 1.0)
        pad1.cd()
        pad1.SetLogy(logY[key])
        hs = THStack('hs', titles[key])
        #hs.Add(histos_SR[key])
        #hs.Add(histos_Signal[key])
        hs.Add(histos_BG[key])
        hs.Add(histos_MC[key])
        #hs.Add(histos_LSB[key])
        #hs.Add(histos_RSB[key])
        #maxHisto = max(hs.GetMaximum(), histos_SR[key].GetMaximum())
        hs.SetMaximum(maxY[key])  #maxHisto)
        hs.Draw('HIST')
        histos_SR[key].Draw('same')
        leg = pad1.BuildLegend(.65, .93, .95, .7)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.Draw("SAME")
        pad2 = TPad("pad2", "The pad 20% of the height", 0.0, 0.01, 1.0, 0.2)
        pad2.cd()
        h_ratio[key].Draw()
        xmin = h_ratio[key].GetBinLowEdge(1)
        xmax = h_ratio[key].GetBinLowEdge(
            h_ratio[key].GetNbinsX()) + h_ratio[key].GetBinWidth(
                h_ratio[key].GetNbinsX())
        line0 = TLine(xmin, 1, xmax, 1)
        line0.SetLineColor(ROOT.kBlue)
        line0.SetLineWidth(2)
        line0.Draw('same')
        canvas.cd()
        pad1.Draw()
        pad2.Draw()
        canvas.Update()
        canvas.Print(outputFile_name)
        canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '.png')
        canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '.pdf')
        canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '.root')

        # Make comparisison between chib1 and chib2
        histos_SR1[key].Scale(1. / histos_SR1[key].Integral())
        histos_SR2[key].Scale(1. / histos_SR2[key].Integral())
        histos_Signal1[key].Scale(1. / histos_Signal1[key].Integral())
        histos_Signal2[key].Scale(1. / histos_Signal2[key].Integral())
        # histos_SR1[key].Rebin(3)
        # histos_SR2[key].Rebin(3)
        # histos_Signal1[key].Rebin(3)
        # histos_Signal2[key].Rebin(3)
        hsc = THStack('hsc', titles_chib[key])
        hsc.Add(histos_Signal1[key])
        hsc.Add(histos_Signal2[key])
        #hsc.SetMaximum(maxY[key])
        hsc.Draw('nostack')
        legc = canvas.BuildLegend(.65, .93, .95, .7)
        legc.SetFillStyle(0)
        legc.SetBorderSize(0)
        legc.Draw("SAME")
        print 'kolmogorov prob SR     ' + key + ' ' + str(
            histos_SR1[key].KolmogorovTest(histos_SR2[key]))
        print 'kolmogorov prob Signal ' + key + ' ' + str(
            histos_Signal1[key].KolmogorovTest(histos_Signal2[key]))
        cfr = 2 if key not in ['dimuon_pt', 'chib_pt'] else 5
        kolmogorovProb_chi = round(
            histos_Signal1[key].KolmogorovTest(histos_Signal2[key]), cfr)
        pvtxt = TPaveText(.2, .0, 0.5, 0.1, "NDC")
        pvtxt.SetFillStyle(0)
        pvtxt.SetBorderSize(0)
        pvtxt.SetTextSize(0.04)
        pvtxt.AddText('Kolmogorov prob = ' + str(kolmogorovProb_chi))
        pvtxt.Draw()
        canvas.Update()
        canvas.Print(outputFile_name)
        canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '_comparison' +
                     '.png')
        canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '_comparison' +
                     '.pdf')
        canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '_comparison' +
                     '.root')

    canvas.Clear()
    canvas.Print(outputFile_name + "]")  #chiude file .ps

    for key in keys:
        print '\n'
        print key + ' Kolmogorov probability: ' + str(chi2Prob[key])
        print key + ' chiSqure probability: ' + str(kolmogorovProb[key])
    #print 'Kolmogorov probability with sidebands: '+str(histos_MC['dimuon_pt'].KolmogorovTest(histos_LSB['dimuon_pt'])) #

    inputFile.Close()