def main():
   
   gROOT.SetBatch(1)
   SetAtlasStyle()
   
   channels = ["ejets","mujets"]
   #channels = ["mujets"]
   
   variables = ["Top1_lhood", "Top2_lhood", "SystemMass", "SystemPt", "SystemRapidity"]
   #variables = ["Top1_lhood"]
   
   methods = ["Inversion","BinByBin","Bayes","SVD" ]
   #methods = ["SVD"]
   
   for j in range(len(variables)):
      variable = variables[j]    
      print j,variable
      
      for i in range(len(channels)):
         channel = channels[i]
         print i,channel
      
         postfix = ''
         if channel is 'ejets':
            postfix = 'el'
         elif channel is 'mujets':
            postfix = 'mu'
         elif channel is 'combined':
            postfix = ''
      
         can = TCanvas(variable+"_"+channel,variable+"_"+channel,0,0,800,600)
         can.SetMargin(0.2,0.05,0.15,0.03)
   
         ratioPlots = []
         
         label = ''
         latexlabel = ""
         if variable is 'Top1_lhood':
            label = 'Leptonic top p^{t}_{T}'
            regnumber = "reg4"
            latexlabel = "#left(#frac{d#sigma}{dp^{t}_{T}}_{unf} - #frac{d#sigma}{dp^{t}_{T}}_{truth}#right) #times #left(#frac{d#sigma}{dp^{t}_{T}}_{truth}#right)^{-1}"
         elif variable is 'Top2_lhood':
            label = 'Hadronic top p^{t}_{T}'
            regnumber = "reg4"
            latexlabel = "#left(#frac{d#sigma}{dp^{t}_{T}}_{unf} - #frac{d#sigma}{dp^{t}_{T}}_{truth}#right) #times #left(#frac{d#sigma}{dp^{t}_{T}}_{truth}#right)^{-1}"
         elif variable is 'SystemMass':
            label = 't#bar{t} System mass'
            regnumber = "reg3"
            latexlabel = "#left(#frac{d#sigma}{dM_{t#bar{t}}}_{unf} - #frac{d#sigma}{dM_{t#bar{t}}}_{truth}#right) #times #left(#frac{d#sigma}{dM_{t#bar{t}}}_{truth}#right)^{-1}"
         elif variable is 'SystemPt':
            label = 't#bar{t} System p_{T}'
            regnumber = "reg3"
            latexlabel = "#left(#frac{d#sigma}{dp^{t#bar{t}}_{T}}_{unf} - #frac{d#sigma}{dp^{t#bar{t}}_{T}}_{truth}#right) #times #left(#frac{d#sigma}{dp^{t#bar{t}}_{T}}_{truth}#right)^{-1}" 
         elif variable is 'SystemRapidity':
            label = 't#bar{t} System rapidity'
            regnumber = "reg4"
            latexlabel = "#left(#frac{d#sigma}{dY_{t#bar{t}}_{T}}_{unf} - #frac{d#sigma}{dY_{t#bar{t}}_{T}}_{truth}#right) #times #left(#frac{d#sigma}{dY_{t#bar{t}}_{T}}_{truth}#right)^{-1}" 
   
         legend = TLegend(0.5,0.95,0.85,0.7,label)
         legend.SetFillStyle(0)
         legend.SetBorderSize(0)
            
         for k in range(len(methods)):
            method = methods[k]
            print k,method 
            
            metfix = ''
            histfix = ''
            addfix = ''
            if method is 'SVD':
               metfix = "_svd_"+regnumber
               histfix = "_SVD_"+regnumber
               addfix = regnumber + "/"
            elif method is 'BinByBin':
               metfix = "_bin"
               histfix = "_BinByBin"
               addfix = ""
            elif method is 'Bayes':
               metfix = "_bay_iter4"
               histfix = "_Bayes"
               addfix = ""
            elif method is 'Inversion':
               metfix = "_inv"               
               histfix = "_Inversion"
               addfix = ""
            
            
            filename = "../data/data_Unfolded_14_02_2013/McAtNlo/MCClosure/RecoClosure_Tag1_" + variable +"_"+postfix + "_half0_toy5000" + metfix +".root"
            file = TFile(filename)
            if not file.IsOpen():
               print 'ERROR opening ',filename 
         
            #open the histograms to do the %
            name = "unfolding/toys/nominal/" + postfix + "/Tag1_" + variable + "/" + method + "/" + addfix + "toy5000/H_" + postfix + "_Tag1_" + variable + histfix + "_toy5000_data_unfolded_diffxs"
            print name
            m_unf =file.Get(name)
            m_unf.SetName("Unfolded_"+method+"_"+variable+"_"+postfix)   
      
            m_truth =file.Get("unfolding/toys/nominal/" + postfix + "/Tag1_" + variable + "/" + method + "/" + addfix + "toy5000/H_" + postfix + "_Tag1_" + variable + histfix + "_toy5000_mc_truth_diffxs")
            m_truth.SetName("Truth_"+method+"_"+variable+"_"+postfix)
      
            nbins = m_unf.GetNbinsX()
            
            ratio = m_unf.Clone("Ratio_"+method+"_"+variable+"_"+postfix)
            
            ratio.SetDirectory(0)
            
            for bin in range(nbins):
               binratio = (m_unf.GetBinContent(bin+1)-m_truth.GetBinContent(bin+1));
               print bin,binratio,m_unf.GetBinContent(bin+1),m_truth.GetBinContent(bin+1)
               ratio.SetBinContent(bin+1,binratio)
            
            ratio.GetYaxis().SetTitle(latexlabel)
            ratio.GetYaxis().SetTitleOffset(1.6)
            ratio.Divide(m_truth)
            ratio.GetYaxis().SetRangeUser(-0.25,0.25)
            if variable is 'SystemRapidity':
               ratio.GetYaxis().SetRangeUser(-0.07,0.06)
            ratio.SetMarkerColor(kBlack+k)
            ratio.SetMarkerStyle(20+k)
            ratio.SetLineColor(kBlack+k)
      
            ratioPlots.append(ratio)
      
            can.cd()
            if k > 0:
               ratio.Draw('same')
            else:
               ratio.Draw()
               #ratio1.Draw('same')
               
            legend.AddEntry(ratio,method,"p")
      
         can.cd()
         horizontal = TF1("line","pol1",-200,2800)
         horizontal.SetParameters(1,0)
         horizontal.SetLineColor(kBlack)
         horizontal.SetLineStyle(2)
         horizontal.Draw('same')
   
         legend.Draw('same')
   
         can.SaveAs("ComparisonMethods_"+variable+"_"+channel+".eps")
def main():

    gROOT.SetBatch(1)
    SetAtlasStyle()

    can = TCanvas("can", "can", 0, 0, 800, 600)
    can.SetMargin(0.2, 0.05, 0.15, 0.03)

    # open files, get the histograms
    histoname = 'h_theory'
    baseFilePath = '../data/data_14_02_2013'

    filename_topMass = baseFilePath + '/pttopnnloapprox7lhc173m.root'
    file_topMass = TFile(filename_topMass)
    histo_m = file_topMass.Get(histoname)
    if not histo_m:
        print 'ERROR retreiving', histoname, 'from', filename_topMass
        return
    histo_m.SetName('kidonakis_m')
    norm_histo_m = histo_m.Clone('norm_' + histo_m.GetName())

    filename_topTransverseMass = baseFilePath + '/pttopnnloapprox7lhc173mT.root'
    file_topTransverseMass = TFile(filename_topTransverseMass)
    histo_mT = file_topTransverseMass.Get(histoname)
    if not histo_mT:
        print 'ERROR retreiving', histoname, 'from', filename_topTransverseMass
        return
    histo_mT.SetName('kidonakis_mT')
    norm_histo_mT = histo_mT.Clone('norm_' + histo_mT.GetName())

    filename_twiceTopMass = baseFilePath + '/pttopnnloapprox7lhc1732m.root'
    file_twiceTopMass = TFile(filename_twiceTopMass)
    histo_2m = file_twiceTopMass.Get(histoname)
    if not histo_2m:
        print 'ERROR retreiving', histoname, 'from', filename_twiceTopMass
        return
    histo_2m.SetName('kidonakis_2m')
    norm_histo_2m = histo_2m.Clone('norm_' + histo_2m.GetName())

    filename_halfTopMass = baseFilePath + '/pttopnnloapprox7lhc173halfm.root'
    file_halfTopMass = TFile(filename_halfTopMass)
    histo_halfm = file_halfTopMass.Get(histoname)
    if not histo_halfm:
        print 'ERROR retreiving', histoname, 'from', filename_halfTopMass
        return
    histo_halfm.SetName('kidonakis_halfm')
    norm_histo_halfm = histo_halfm.Clone('norm_' + histo_halfm.GetName())

    output_filename = baseFilePath + '/kidonakis.root'

    outFile = TFile(output_filename, 'UPDATE')

    histo_m.Write(histo_m.GetName(), TObject.kOverwrite)
    histo_mT.Write(histo_mT.GetName(), TObject.kOverwrite)
    histo_2m.Write(histo_2m.GetName(), TObject.kOverwrite)
    histo_halfm.Write(histo_halfm.GetName(), TObject.kOverwrite)

    # create plot of kidonakis using m as nominal, then others are systematic
    h_m_syst = histo_m.Clone('kidonakis_m_withSyst')

    for bin in range(1, h_m_syst.GetNbinsX() + 1):
        diff_mT = math.fabs(
            histo_m.GetBinContent(bin) - histo_mT.GetBinContent(bin))
        diff_2m = math.fabs(
            histo_m.GetBinContent(bin) - histo_2m.GetBinContent(bin))
        diff_halfm = math.fabs(
            histo_m.GetBinContent(bin) - histo_halfm.GetBinContent(bin))

        if (diff_mT > diff_2m and diff_mT > diff_halfm):
            h_m_syst.SetBinError(bin, diff_mT)
        elif (diff_2m > diff_mT and diff_2m > diff_halfm):
            h_m_syst.SetBinError(bin, diff_2m)
        elif (diff_halfm > diff_2m and diff_halfm > diff_mT):
            h_m_syst.SetBinError(bin, diff_halfm)

    h_m_syst.SetMarkerStyle(20)
    h_m_syst.SetMarkerColor(kBlack)
    h_m_syst.SetLineColor(kBlack)
    h_m_syst.SetTitle(
        ';p_{T}^{t} [GeV];#frac{d#sigma}{dp_{T}^{t}} #left[#frac{pb}{GeV}#right]'
    )
    h_m_syst.Write(h_m_syst.GetName(), TObject.kOverwrite)

    can.cd()
    h_m_syst.Draw()
    can.SaveAs('kidonakis.eps')

    # create normalized version
    h_m_syst.Scale(1. / h_m_syst.Integral('width'))
    h_m_syst.SetTitle(
        ';p_{T}^{t} [GeV];#frac{1}{#sigma} #frac{d#sigma}{dp_{T}^{t}}#left[#frac{1}{GeV}#right]'
    )
    h_m_syst.GetYaxis().SetTitleOffset(1.7)
    h_m_syst.Write(h_m_syst.GetName() + '_norm', TObject.kOverwrite)

    can.cd()
    h_m_syst.Draw()
    can.SaveAs('kidonakis_norm.eps')

    outFile.Close()
Exemple #3
0
def main():

    gROOT.SetBatch(1)
    SetAtlasStyle()

    can = TCanvas("can", "can", 0, 0, 800, 600)
    can.SetMargin(0.2, 0.05, 0.15, 0.03)

    # open files, get the histograms
    histoname = 'h_theoryMtt'
    baseFilePath = '../data/data_14_02_2013'

    filename = baseFilePath + '/Mtt_7000_Mtt_fin.root'
    file = TFile(filename)

    histos = []

    for i in range(7):
        var = i
        if var == 0:
            histos.append(file.Get(histoname + '_'))
        elif var > 0:
            histos.append(file.Get(histoname + '_' + str(i)))

        if not histos[i]:
            print 'ERROR retreiving', histoname + '_' + str(
                i), 'from', filename
            return
        histos[i].SetName('yangPecjak_' + str(i))

    output_filename = baseFilePath + '/yangPecjak.root'

    outFile = TFile(output_filename, 'UPDATE')

    nominal = histos[0].Clone()
    nominal.SetName('yangPecjak_withSyst')

    # add other plots 1-6 in quadrature as error
    for histo in histos:
        for bin in range(1, nominal.GetNbinsX() + 1):

            diff = math.fabs(
                nominal.GetBinContent(bin) - histo.GetBinContent(bin))

            nominal.SetBinError(bin, nominal.GetBinError(bin) + diff * diff)

    # take squareroot
    for bin in range(1, nominal.GetNbinsX() + 1):
        nominal.SetBinError(bin, math.sqrt(nominal.GetBinError(bin)))

    nominal.SetMarkerStyle(20)
    nominal.SetMarkerColor(kBlack)
    nominal.SetLineColor(kBlack)
    nominal.SetTitle(
        ';M_{t#bar{t}} [GeV]; #frac{d#sigma}{dM_{t#bar{t}}}#left[#frac{pb}{GeV}#right]'
    )
    nominal.Write(nominal.GetName(), TObject.kOverwrite)

    can.cd()
    nominal.Draw()
    can.SaveAs("yangPecjak.eps")

    outFile.Close()
Exemple #4
0
def main():

    gROOT.SetBatch(True)
    SetAtlasStyle()

    base_path = '../data/March_4/McAtNlo/UnfoldingMethodTest'
    output_eps_path = base_path + '/eps'
    base_filename = 'unfoldingMethodsTest_mc'

    systematic = 'nominal'
    toys = 5000
    iterValue = 4
    histogram_names = ['mc_truth_diffxs', 'data_unfolded_diffxs']

    output_filename = base_path + '/unfoldingMethodsPlots'
    output_filename_ext = '.eps'
    can = TCanvas('can', 'can', 0, 0, 800, 600)
    can.SetMargin(0.2, 0.05, 0.15, 0.05)

    horizontalAt0 = TF1("horizontalAt0", "[0]+x*[1]", -100000, 100000)
    horizontalAt0.SetParameter(0, 0)
    horizontalAt0.SetParameter(1, 0)
    horizontalAt0.SetLineColor(kBlack)
    horizontalAt0.SetLineStyle(9)
    horizontalAt0.SetLineWidth(1)

    y_title = "#left(#frac{d#sigma}{dX}|_{unf} - #frac{d#sigma}{dX}|_{tru}#right)#times#left(#frac{d#sigma}{dX}|_{tru}#right)^{-1}"

    # load histograms
    for sampleName in sampleNames:
        x_title = ''

        latex_label = ''
        if sampleName is 'Tag1_SystemMass':
            x_title = 'M_{t#bar{t}} [GeV]'
        elif sampleName is 'Tag1_SystemPt':
            x_title = 'p_{T}^{t#bar{t}} [GeV]'
        elif sampleName is 'Tag1_Top1_lhood':
            x_title = 'leptonic top p_{T} [GeV]'
        elif sampleName is 'Tag1_Top2_lhood':
            x_title = 'hadronic top p_{T} [GeV]'

        for channel in channels:

            diffxs = {}
            truth = {}
            ratios = {}
            gratios = {}
            for half in halves:

                # create canvas and legend
                can.cd()
                legA = TLegend(0.23, 0.93, 0.53, 0.75)
                legA.SetBorderSize(0)
                legA.SetFillStyle(0)
                legB = TLegend(0.53, 0.93, 0.83, 0.75)
                legB.SetBorderSize(0)
                legB.SetFillStyle(0)

                # loop over methods and get histograms
                diffxs[half] = {}
                truth[half] = {}
                ratios[half] = {}
                gratios[half] = {}
                for unfoldingMethod in unfoldingMethods:

                    # get the method value
                    methodValue = -1
                    if unfoldingMethod == SVD:
                        methodValue = regValues[sampleName]
                    elif unfoldingMethod == Bayesian:
                        methodValue = iterValue

                    # open file with histograms
                    in_filename = base_path + '/' + get_input_filename(
                        systematic, sampleName, channel, unfoldingMethod, half,
                        toys, methodValue) + '.root'
                    in_file = TFile(in_filename)
                    if not in_file.IsOpen():
                        print 'ERROR opening file', in_filename
                        return

                    # retrieve histograms
                    histoname_base = get_histoname_base(
                        systematic, sampleName, channel, unfoldingMethod, half,
                        toys, methodValue)

                    # get truth
                    histoname = histoname_base + '_mc_truth_diffxs'
                    histo = in_file.Get(histoname)
                    if not histo:
                        print 'ERROR retrieving histogram,\n', histoname, ',\n from file,\n', in_filename
                        return
                    histo.SetDirectory(0)
                    histo.SetName(histoname_base + '_' + unfoldingMethod +
                                  '_mc_truth_diffxs')

                    truth[half][unfoldingMethod] = histo

                    # get unfolded distribution
                    histoname = histoname_base + '_data_unfolded_diffxs'
                    histo = in_file.Get(histoname)
                    if not histo:
                        print 'ERROR retrieving histogram,\n', histoname, ',\n from file,\n', in_filename
                        return
                    histo.SetDirectory(0)
                    histo.SetName(histoname_base + '_' + unfoldingMethod +
                                  '_data_unfolded_diffxs')

                    diffxs[half][unfoldingMethod] = histo

                    in_file.Close()
                    # end unfoldingMethods
                # end halves

            for half in halves:
                for unfoldingMethod in unfoldingMethods:
                    # calculate ratio of unfolded-truth over truth
                    ratio = GetRatioPlot(
                        diffxs[half][unfoldingMethod], truth[oppositeHalves[
                            halves.index(half)]][unfoldingMethod])
                    histoname_base = get_histoname_base(
                        systematic, sampleName, channel, unfoldingMethod, half,
                        toys, methodValue)
                    ratio.SetName(histoname_base + '_' + unfoldingMethod +
                                  '_ratio')
                    ratio.SetDirectory(0)

                    ratios[half][unfoldingMethod] = ratio

                    axis_length = ratio.GetXaxis().GetXmax() - ratio.GetXaxis(
                    ).GetXmin()
                    offset = (unfoldingMethods.index(unfoldingMethod) -
                              1.5) * 0.005 * axis_length
                    gratio = GetTGraphAsymmErrors(ratio, offset)

                    # plot ratio
                    gratio.SetMarkerColor(
                        unfoldingMethods.index(unfoldingMethod) + 1)
                    gratio.SetMarkerStyle(
                        unfoldingMethods.index(unfoldingMethod) + 21)
                    gratio.SetLineColor(
                        unfoldingMethods.index(unfoldingMethod) + 1)
                    gratio.SetLineWidth(2)

                    # if this is the first method, need to set proper axis parameters
                    if unfoldingMethods.index(unfoldingMethod) == 0:
                        if sampleName is LeptonicTop:
                            gratio.SetMaximum(0.11)
                            gratio.SetMinimum(-0.09)
                        elif sampleName is HadronicTop:
                            gratio.SetMaximum(0.08)
                            gratio.SetMinimum(-0.1)
                        elif sampleName is SystemMass:
                            gratio.SetMaximum(0.1)
                            gratio.SetMinimum(-0.08)
                        elif sampleName is SystemPt:
                            gratio.SetMaximum(0.15)
                            gratio.SetMinimum(-0.3)
                        elif sampleName is SystemRapidity:
                            gratio.SetMaximum(0.07)
                            gratio.SetMinimum(-0.05)

                        gratio.GetXaxis().SetTitle(x_title)
                        gratio.GetYaxis().SetTitle(y_title)
                        gratio.GetYaxis().SetTitleOffset(1.6)
                        gratio.Draw('ap')
                    else:
                        gratio.Draw('p')

                    # add to legend
                    if unfoldingMethods.index(unfoldingMethod) >= 2:
                        legB.AddEntry(gratio, unfoldingMethod, 'pl')
                    else:
                        legA.AddEntry(gratio, unfoldingMethod, 'pl')

                    gratios[half][unfoldingMethod] = gratio

                    horizontalAt0.Draw('same')

                    legA.Draw('same')
                    legB.Draw('same')

                    text = TPaveText(0.2, 0.25, 0.4, 0.18, "NDC")
                    text.SetFillStyle(0)
                    text.SetBorderSize(0)
                    text.AddText(half + ', ' + channel)
                    #    text.Draw('same')
                    can.SaveAs(output_filename + '_' + channel + '_' + half +
                               '_' + sampleName + output_filename_ext)
def main():

    gROOT.SetBatch(1)
    SetAtlasStyle()

    channels = ["ejets", "mujets"]
    #channels = ["mujets"]

    variables = [
        "Top1_lhood", "Top2_lhood", "SystemMass", "SystemPt", "SystemRapidity"
    ]
    #variables = ["Top1_lhood"]

    for j in range(len(variables)):
        variable = variables[j]
        print j, variable

        can = TCanvas(variable, variable, 0, 0, 800, 600)
        can.SetMargin(0.2, 0.05, 0.15, 0.03)

        label = ''
        regnumber = ''

        if variable is 'Top1_lhood':
            label = 'Leptonic top p^{t}_{T}'
            regnumber = "reg4"
        elif variable is 'Top2_lhood':
            label = 'Hadronic top p^{t}_{T}'
            regnumber = "reg4"
        elif variable is 'SystemMass':
            label = 't#bar{t} System mass'
            regnumber = "reg3"
        elif variable is 'SystemPt':
            label = 't#bar{t} System p_{T}'
            regnumber = "reg3"
        elif variable is 'SystemRapidity':
            label = 't#bar{t} System rapidity'
            regnumber = "reg4"

        legend = TLegend(0.6, 0.95, 0.95, 0.75, label)
        legend.SetFillStyle(0)
        legend.SetBorderSize(0)

        ratioPlots = []

        for i in range(len(channels)):
            channel = channels[i]

            print i, channel

            postfix = ''
            labelChannel = ''
            if channel is 'ejets':
                postfix = 'el'
                labelChannel = 'e + jets'
            elif channel is 'mujets':
                postfix = 'mu'
                labelChannel = '#mu + jets'
            elif channel is 'combined':
                postfix = 'co'

            filename = "../data/data_Unfolded_14_02_2013/Alpgen/nominal_Tag1_" + variable + "_" + postfix + "_toy5000_svd_" + regnumber + ".root"
            fileDi = TFile(filename)
            if not fileDi.IsOpen():
                print 'ERROR opening ', filename

            m_histo = fileDi.Get("unfolding/toys/nominal/" + postfix +
                                 "/Tag1_" + variable + "/SVD/" + regnumber +
                                 "/toy5000/H_" + postfix + "_Tag1_" +
                                 variable + "_SVD_" + regnumber +
                                 "_toy5000_Regularization")
            m_histo.SetName("diFactor" + variable + postfix)
            nbins = m_histo.GetNbinsX() - 1

            diFactor = m_histo.Clone()
            diFactor.SetDirectory(0)
            diFactor.SetNdivisions(508)
            diFactor.GetXaxis().SetRangeUser(1, nbins + 1)

            for bin in range(nbins):
                bindiFactor = m_histo.GetBinContent(bin + 1)
                print bin, bindiFactor
                diFactor.SetBinContent(bin + 2, bindiFactor)

            diFactor.SetTitle("")
            diFactor.SetMarkerColor(kBlack + i)
            diFactor.SetMarkerStyle(20 + i)
            diFactor.SetLineColor(kBlack + i)
            diFactor.GetYaxis().SetRangeUser(0.005, 400)
            diFactor.GetYaxis().SetTitle("|d_{i}|")
            diFactor.GetXaxis().SetTitle("bin number")

            ratioPlots.append(diFactor)

            legend.AddEntry(diFactor, labelChannel, "p")

            if i > 0:
                diFactor.Draw('histosame')
            else:
                diFactor.Draw("histo")

        can.cd()
        horizontal = TF1("line", "pol1", 0, 2800)
        horizontal.SetParameters(1, 0)
        horizontal.SetLineColor(kBlack)
        horizontal.SetLineStyle(2)
        horizontal.Draw('same')

        legend.Draw('same')
        can.SetLogy()
        can.SaveAs("regularizationValue_" + variable + ".eps")
Exemple #6
0
 def __init__(self, logger=None):
     Logger.__init__(self, logger=logger)
     SetAtlasStyle()
     from ROOT import TFile, gROOT
     gROOT.ProcessLine("gErrorIgnoreLevel = kFatal;")
     self._logger.info('create Profile instance..')
#!/usr/bin/env python
from ROOT import TH1D, gROOT
gROOT.SetBatch(True)
from RatioCanvas import RatioCanvas
from AtlasStyle import SetAtlasStyle
SetAtlasStyle()

ratio = RatioCanvas(ratioPad_y_ndiv=204, )

h1 = TH1D('h1', ';p_{T} [GeV];Counts', 150, 0, 150)
h1.FillRandom('landau', 100000)
h1.Sumw2()
h2 = TH1D('h2', ';p_{T} [GeV];Counts', 150, 0, 150)
h2.FillRandom('landau', 100000)
h2.Sumw2()
h3 = TH1D('h3', ';p_{T} [GeV];Counts', 150, 0, 150)
h3.FillRandom('landau', 100000)
h3.Sumw2()

h1.GetXaxis().SetTitleOffset(1)

ratio.AddPlot(h1, 'e', 'first', 'lp', True)
ratio.AddPlot(h2, 'e', 'second', 'lp')
ratio.AddPlot(h3, 'e', 'third', 'lp')

ratio.Draw('ratioTest.ps')
def main():

    gROOT.SetBatch(1)
    SetAtlasStyle()

    #channels = ["ejets","mujets"]
    channels = ["ejets"]

    variables = [
        "Top1_lhood", "Top2_lhood", "SystemMass", "SystemPt", "SystemRapidity"
    ]

    for j in range(len(variables)):
        variable = variables[j]
        print j, variable

        can = TCanvas(variable, variable, 0, 0, 800, 600)
        can.SetMargin(0.2, 0.05, 0.15, 0.03)

        ratioPlots = []

        label = ''
        latexlabel = ""
        if variable is 'Top1_lhood':
            label = 'Leptonic top p^{t}_{T}'
            latexlabel = "#frac{d#sigma}{dp^{t}_{T}}_{rew} #times #left[#frac{d#sigma}{dp^{t}_{T}}_{std}#right]^{-1}"
        elif variable is 'Top2_lhood':
            label = 'Hadronic top p^{t}_{T}'
            latexlabel = "#frac{d#sigma}{dp_{T}^{t}}_{rew} #times #left[#frac{d#sigma}{dp_{T}^{t}}_{std}#right]^{-1}"
        elif variable is 'SystemMass':
            label = 't#bar{t} System mass'
            latexlabel = "#frac{d#sigma}{dM_{t#bar{t}}}_{rew} #times #left[#frac{d#sigma}{dM_{t#bar{t}}}_{std}#right]^{-1}"
        elif variable is 'SystemRapidity':
            label = 't#bar{t} System rapidity'
            latexlabel = "#frac{d#sigma}{dY_{t#bar{t}}}_{rew} #times #left[#frac{d#sigma}{dY_{t#bar{t}}}_{std}#right]^{-1}"
        elif variable is 'SystemPt':
            label = 't#bar{t} System p_{T}'
            latexlabel = "#frac{d#sigma}{dp^{t#bar{t}}_{T}}_{rew} #times #left[#frac{d#sigma}{d}p^{t#bar{t}}_{T}_{std}#right]^{-1}"

        legend = TLegend(0.25, 0.95, 0.5, 0.75, label)
        legend.SetFillStyle(0)
        legend.SetBorderSize(0)

        for i in range(len(channels)):
            channel = channels[i]

            print i, channel

            postfix = ''
            if channel is 'ejets':
                postfix = '_el'
            elif channel is 'mujets':
                postfix = '_mu'
            elif channel is 'combined':
                postfix = ''

            filename = "../data/data_Unfolded_14_02_2013/Alpgen/SVD/unfoldedResult_Tag1_" + variable + postfix + ".root"
            file_std = TFile(filename)
            if not file_std.IsOpen():
                print 'ERROR opening ', filename
            filename = "../data/test_Unfolded_08_03_2013/Alpgen/SVD/unfoldedResult_Tag1_" + variable + postfix + ".root"
            file_rew = TFile(filename)
            if not file_rew.IsOpen():
                print 'ERROR opening ', filename

            diffxs_std = file_std.Get("diffxs_stat")
            diffxs_std.SetName("diffxs_stat_std" + postfix)
            measured_std = file_std.Get("measured")
            measured_std.SetName("measured_std" + postfix)
            diffxs_rew = file_rew.Get("diffxs_stat")
            measured_rew = file_rew.Get("measured")
            measured_rew.SetName("measured_rew" + postfix)
            diffxs_rew.SetName("diffxs_stat_rew" + postfix)

            ratio = diffxs_rew.Clone("diffxs_stat_ratio" + postfix)
            ratio.SetDirectory(0)
            ratio.GetYaxis().SetTitle(latexlabel)
            ratio.GetYaxis().SetTitleOffset(1.6)
            ratio.Divide(diffxs_std)
            ratio.GetYaxis().SetRangeUser(0.9, 1.2)
            ratio.SetMarkerColor(kBlack + i)
            ratio.SetMarkerStyle(20 + i)
            ratio.SetLineColor(kBlack + i)

            ratio1 = measured_rew.Clone("measured_ratio" + postfix)
            ratio1.SetDirectory(0)
            ratio1.GetYaxis().SetTitle(latexlabel)
            ratio1.GetYaxis().SetTitleOffset(1.6)
            ratio1.Divide(measured_std)
            ratio1.GetYaxis().SetRangeUser(0.7, 1.2)
            if variable is 'SystemRapidity':
                ratio.GetYaxis().SetRangeUser(-0.07, 0.04)
            ratio1.SetMarkerColor(kRed + i)
            ratio1.SetMarkerStyle(20 + i)
            ratio1.SetLineColor(kRed + i)

            ratioPlots.append(ratio)
            ratioPlots.append(ratio1)

            legend.AddEntry(ratio, "unfolded", "p")
            legend.AddEntry(ratio1, "measured", "p")

            can.cd()
            if i > 0:
                ratio.Draw('same')
            else:
                ratio.Draw()
                ratio1.Draw('same')

        can.cd()
        horizontal = TF1("line", "pol1", 200, 2800)
        horizontal.SetParameters(1, 0)
        horizontal.SetLineColor(kBlack)
        horizontal.SetLineStyle(2)
        horizontal.Draw('same')

        legend.Draw('same')

        can.SaveAs(variable + ".eps")

    controlPlots = [
        'InclusiveJetBinLeptonPt', 'Top1Pt', 'Top2Pt', 'SystemMass', 'SystemPt'
    ]

    ratioPlots1 = []

    for j in range(len(controlPlots)):
        controlPlot = controlPlots[j]

        can1 = TCanvas(controlPlot, controlPlot, 0, 0, 800, 600)
        can1.SetMargin(0.2, 0.05, 0.15, 0.03)

        legend1 = TLegend(0.45, 0.95, 0.90, 0.75, "")
        legend1.SetFillStyle(0)
        legend1.SetBorderSize(0)

        labelx = ''
        if controlPlot is 'InclusiveJetBinLeptonPt':
            labelx = 'lepton p_{T}, all-jet bin[GeV/c]'
        elif controlPlot is 'Top1Pt':
            labelx = 'Leptonic top p^{t}_{T}[GeV/c]'
        elif controlPlot is 'Top2Pt':
            labelx = 'Hadronic top p^{t}_{T}[GeV/c]'
        elif controlPlot is 'SystemMass':
            labelx = 'M_{t#bar{t}}[GeV/c^{2}]'
        elif controlPlot is 'SystemPt':
            labelx = 'p^{t#bar{t}}_{T}[GeV/c]'

        for i in range(len(channels)):
            channel = channels[i]
            print i, channel

            postfix = ''
            if channel is 'ejets':
                postfix = '_el'
            elif channel is 'mujets':
                postfix = '_mu'
            elif channel is 'combined':
                postfix = ''

            filename = "../data/data_14_02_2013/nominal/ToUnfold_nominal_AlpgenJimmy/tagged_" + channel + ".root"
            cfile_std = TFile(filename)
            if not cfile_std.IsOpen():
                print 'ERROR opening ', filename
            filename = "../data/test_08_03_2013/nominal/ToUnfold_nominal_AlpgenJimmy/tagged_" + channel + ".root"
            cfile_rew = TFile(filename)
            if not cfile_rew.IsOpen():
                print 'ERROR opening ', filename

            control_std = cfile_std.Get(controlPlot + "_Data")
            control_std.SetName("control_std" + postfix)
            control_rew = cfile_rew.Get(controlPlot + "_Data")
            control_rew.SetName("control_rew" + postfix)

            control_std.SetDirectory(0)
            control_std.SetTitle("")
            control_std.GetYaxis().SetTitle("Events")
            control_std.GetXaxis().SetTitle(labelx)
            control_std.GetYaxis().SetTitleOffset(1.6)
            #control_std.GetYaxis().SetRangeUser(0.7,1.3)
            control_std.SetMarkerColor(kBlack + i)
            control_std.SetMarkerStyle(20 + i)
            control_std.SetLineColor(kBlack + i)

            control_rew.SetMarkerColor(kRed + i)
            control_rew.SetMarkerStyle(20 + i)
            control_rew.SetLineColor(kRed + i)

            if i > 0:
                control_std.Draw('same')
                control_rew.Draw('same')
            else:
                control_std.Draw()
                control_rew.Draw('same')

            legend1.AddEntry(control_std, "nominal", "p")
            legend1.AddEntry(control_rew, "Lepton p_{T} reweighting", "p")
            legend1.Draw('same')

        can1.SetLogy()
        can1.SaveAs(controlPlot + "_log.eps")