Esempio n. 1
0
def main():
    print "Program start..."

    #ROOT.gROOT.ProcessLine('TFile* f_zpt = TFile::Open("results/zpt_weight.root")')
    #ROOT.gROOT.ProcessLine('TH1D* h_zpt_ratio  = (TH1D*)f_zpt->Get("h_zpt_ratio")')

    if dotest:
        input_data = "inputs/zmumu/input_data.txt"
        input_dy = "inputs/zmumu/input_zjets.txt"
        input_ttbar = "inputs/zmumu/input_ttbar_dilepton.txt"
    else:
        input_data = "inputs/zmumu/input_data.txt"
        input_dy = "inputs/zmumu/input_zjets.txt"
        input_ttbar = "inputs/zmumu/input_ttbar_dilepton.txt"
        input_ttbar_1lep = "inputs/zmumu/input_ttbar_singlelepton.txt"
        input_ttbar_0lep = "inputs/zmumu/input_ttbar_hadronic.txt"
        input_ww = "inputs/zmumu/input_ww.txt"
        input_wz = "inputs/zmumu/input_wz.txt"
        input_zz = "inputs/zmumu/input_zz.txt"
        input_zxx = "inputs/zmumu/input_zxx.txt"

    DataSamp = Sample(input_data,
                      isMC=False,
                      legend="Data",
                      name="Data",
                      bjetVeto=VetoB)
    DYSamp = Sample(input_dy,
                    xsec=6077.22 * 1e3,
                    color=92,
                    reweightzpt=True,
                    legend="Z#rightarrow#mu#mu",
                    name="DY",
                    bjetVeto=VetoB,
                    nmcevt=311461009200.830078 / 128007. * 126620.8)
    TTbarSamp = Sample(input_ttbar,
                       xsec=831.76 * 0.105 * 1e3,
                       color=46,
                       reweightzpt=False,
                       legend="t#bar{t}",
                       name="ttbar2lep",
                       bjetVeto=VetoB,
                       nmcevt=360411094.548295 / 335.0 * 325.0)
    if not dotest:
        WWSamp = Sample(input_ww,
                        xsec=12.178 * 1e3,
                        color=38,
                        reweightzpt=False,
                        legend="WW2L",
                        name="WW",
                        bjetVeto=VetoB,
                        nmcevt=1000000.0)
        WZSamp = Sample(input_wz,
                        xsec=5.26 * 1e3,
                        color=39,
                        reweightzpt=False,
                        legend="WZ3L",
                        name="WZ",
                        bjetVeto=VetoB,
                        nmcevt=885000.00)
        ZZSamp = Sample(input_zz,
                        xsec=0.564 * 1e3,
                        color=37,
                        reweightzpt=False,
                        legend="ZZ2L",
                        name="ZZ",
                        bjetVeto=VetoB,
                        nmcevt=1000000.0)
        ZXXSamp = Sample(input_zxx,
                         xsec=1.0,
                         color=40,
                         reweightzpt=False,
                         legend="ZXX",
                         name="ZXX",
                         bjetVeto=VetoB,
                         nmcevt=3.1146101e+11)
        TT1LepSamp = Sample(input_ttbar_1lep,
                            xsec=831.76 * 0.219 * 1e3,
                            color=47,
                            reweightzpt=False,
                            legend="t#bar{t}",
                            name="ttbar1lep",
                            bjetVeto=VetoB,
                            nmcevt=1.2561491e+09 / 335.0 * 325.0)
        TT0LepSamp = Sample(input_ttbar_0lep,
                            xsec=831.76 * 0.219 * 1e3,
                            color=48,
                            reweightzpt=False,
                            legend="t#bar{t}",
                            name="ttbar0lep",
                            bjetVeto=VetoB,
                            nmcevt=6.2414786e+09 / 335.0 * 325)

    if not dotest:
        sampMan = SampleManager(DataSamp, [
            DYSamp, TTbarSamp, WWSamp, WZSamp, ZZSamp, ZXXSamp, TT1LepSamp,
            TT0LepSamp
        ])
    else:
        sampMan = SampleManager(DataSamp, [DYSamp, TTbarSamp])
    sampMan.groupMCs(["WW", "WZ", "ZZ", "ZXX"], "EWK", 216, "EWK")
    sampMan.groupMCs(["ttbar2lep", "ttbar1lep", "ttbar0lep"], "ttbar", 96,
                     "t#bar{t}")

    sampMan.DefineAll("zpt", "Z.Pt()")
    sampMan.DefineAll("zmass", "ZMass")
    sampMan.DefineAll("zy", "Z.Rapidity()")
    sampMan.DefineAll("zeta", "Z.Rapidity()")
    sampMan.DefineAll("zphi", "Z.Phi()")

    sampMan.DefineAll("urawvec", "UVec(zpt, zphi, met_raw_pt, met_raw_phi)")
    sampMan.DefineAll("uraw_pt", "urawvec.Mod()")
    sampMan.DefineAll("uraw_phi", "urawvec.Phi()")
    sampMan.DefineAll("uraw1",
                      "uraw_pt * TMath::Cos(uraw_phi + TMath::Pi() - zphi)")
    sampMan.DefineAll("uraw2",
                      "uraw_pt * TMath::Sin(uraw_phi + TMath::Pi() - zphi)")

    sampMan.DefineAll("uvec",
                      "UVec(zpt, zphi, met_corrected_pt, met_corrected_phi)")
    sampMan.DefineAll("u_pt", "uvec.Mod()")
    sampMan.DefineAll("u_phi", "uvec.Phi()")
    sampMan.DefineAll("ucor1", "u_pt * TMath::Cos(u_phi + TMath::Pi() - zphi)")
    sampMan.DefineAll("ucor2", "u_pt * TMath::Sin(u_phi + TMath::Pi() - zphi)")

    sampMan.DefineAll("nPV", "npv")

    sampMan.DefineAll("m1pt", "lep1_corr.Pt()")
    sampMan.DefineAll("m1eta", "lep1_corr.Eta()")
    sampMan.DefineAll("m2pt", "lep2_corr.Pt()")
    sampMan.DefineAll("m2eta", "lep2_corr.Eta()")

    sampMan.DefineAll("LeadMuon_pt", "(m1pt > m2pt ? m1pt : m2pt)")
    sampMan.DefineAll("LeadMuon_eta", "(m1pt > m2pt ? m1eta : m2eta)")
    sampMan.DefineAll("SubleadMuon_pt", "(m1pt > m2pt ? m2pt : m1pt)")
    sampMan.DefineAll("SubleadMuon_eta", "(m1pt > m2pt ? m2eta : m1eta)")

    #DYSamp.Define("u_pt_corr_central", "TMath::Sqrt(u1_corr_central*u1_corr_central + u2_corr_central*u2_corr_central)")
    #sampMan.DefineAll("u1_corr_central",     "u1"  , excludes=['DY'])

    met_pt_bins = np.array([
        0., 2.0, 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36,
        39, 42, 45, 48, 51, 55, 60, 65, 70, 75, 80, 90, 100, 110, 120, 135,
        150, 165, 180, 200
    ])
    u1_bins = np.array([
        -20.0, -16, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16,
        18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 53,
        56, 59, 64, 68, 72, 76, 80, 85, 90, 100
    ])
    u2_bins = np.array([
        -40, -35, -30, -25., -22., -20, -18, -16, -14, -12, -10, -8, -6, -4,
        -2, 0, 2, 4, 6, 8, 10, 12, 16, 18, 20, 22, 25, 30, 35, 40
    ])
    #u_bins = np.array([0., 2., 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, 52, 56, 60, 64, 68, 72, 76, 80, 85, 90, 95, 100, 105, 110, 115, 120, 130, 140, 150])
    phimin = -ROOT.TMath.Pi()
    phimax = ROOT.TMath.Pi()

    eta_bins = np.concatenate(
        (np.linspace(-2.4, -2.0,
                     3), np.linspace(-1.8, 1.8, 37), np.linspace(2.0, 2.4, 3)))
    pt_bins = np.concatenate((np.linspace(25, 35, 6), np.linspace(36, 55, 20),
                              np.linspace(57, 63, 4), np.linspace(66, 70, 2)))
    mass_bins = np.concatenate(
        (np.linspace(70, 76, 3), np.linspace(78, 86,
                                             5), np.linspace(87, 96, 10),
         np.linspace(98, 104, 5), np.linspace(106, 110, 2)))
    u_bins = np.concatenate((np.linspace(0, 20, 11), np.linspace(24, 80, 15),
                             np.linspace(85, 109, 4), np.linspace(120, 150,
                                                                  3)))

    # z pt befor and after pt reweighting
    sampMan.cacheDraw("zpt",
                      "histo_zjets_zpt_WoZptWeight_mumu",
                      u_bins,
                      DrawConfig(xmin=0,
                                 xmax=150,
                                 xlabel='p_{T}^{#mu#mu} [GeV]'),
                      weightname="weight_WoVpt")
    sampMan.cacheDraw("zmass", "histo_zjets_zmass_mumu", mass_bins,
                      DrawConfig(xmin=70, xmax=110, xlabel='m_{#mu#mu} [GeV]'))
    sampMan.cacheDraw(
        "zeta", "histo_zjets_zeta_mumu", eta_bins,
        DrawConfig(xmin=-2.5,
                   xmax=2.5,
                   xlabel='#eta_{#mu#mu} [GeV]',
                   ymax=1e7,
                   ylabel='Events / 1'))
    sampMan.cacheDraw(
        "zy", "histo_zjets_zrapidity_mumu", eta_bins,
        DrawConfig(xmin=-2.5,
                   xmax=2.5,
                   xlabel='y_{#mu#mu} [GeV]',
                   ymax=1e7,
                   ylabel='Events / 1'))

    sampMan.cacheDraw(
        "nPV", "histo_nPV_mumu", 10, 0, 10,
        DrawConfig(xmin=0, xmax=10, xlabel='# PV', ylabel='Events / 1'))

    sampMan.cacheDraw(
        "LeadMuon_pt", "histo_leadMuon_pt_mumu", pt_bins,
        DrawConfig(xmin=20, xmax=70, xlabel='p_{T}(Leading #mu) [GeV]'))
    sampMan.cacheDraw(
        "LeadMuon_eta", "histo_leadMuon_eta_mumu", eta_bins,
        DrawConfig(xmin=-2.6,
                   xmax=2.6,
                   xlabel='#eta (Leading #mu) [GeV]',
                   ymax=1e7,
                   ylabel='Events / 1'))
    sampMan.cacheDraw(
        "SubleadMuon_pt", "histo_subleadMuon_pt_mumu", pt_bins,
        DrawConfig(xmin=20, xmax=70, xlabel='p_{T}(Subleading #mu) [GeV]'))
    sampMan.cacheDraw(
        "SubleadMuon_eta", "histo_subleadMuon_eta_mumu", eta_bins,
        DrawConfig(xmin=-2.6,
                   xmax=2.6,
                   xlabel='#eta (Subleading #mu) [GeV]',
                   ymax=1e7,
                   ylabel='Events / 1'))

    sampMan.cacheDraw("met", "histo_zjets_mumu_pfmet_pt", met_pt_bins,
                      DrawConfig(xmin=0, xmax=100, xlabel='PF MET [GeV]'))
    sampMan.cacheDraw(
        "metPhi", "histo_zjets_mumu_pfmet_phi", 30, phimin, phimax,
        DrawConfig(xmin=phimin, xmax=phimax, xlabel='PF MET #phi'))

    sampMan.cacheDraw("met_raw_pt", "histo_zjets_mumu_pfmet_raw_pt",
                      met_pt_bins,
                      DrawConfig(xmin=0, xmax=100, xlabel='Raw PF MET [GeV]'))
    sampMan.cacheDraw(
        "met_raw_phi", "histo_zjets_mumu_pfmet_raw_phi", 30, phimin, phimax,
        DrawConfig(xmin=phimin, xmax=phimax, xlabel='Raw PF MET #phi'))

    sampMan.cacheDraw("met_wlepcorr_pt", "histo_zjets_mumu_pfmet_wlepcorr_pt",
                      met_pt_bins,
                      DrawConfig(xmin=0, xmax=100, xlabel='PF MET [GeV]'))
    sampMan.cacheDraw(
        "met_wlepcorr_phi", "histo_zjets_mumu_pfmet_wlepcorr_phi", 30, phimin,
        phimax, DrawConfig(xmin=phimin, xmax=phimax, xlabel='PF MET #phi'))

    sampMan.cacheDraw("met_corrected_pt",
                      "histo_zjets_mumu_pfmet_corrected_pt", met_pt_bins,
                      DrawConfig(xmin=0, xmax=100, xlabel='PF MET [GeV]'))
    sampMan.cacheDraw(
        "met_corrected_phi", "histo_zjets_mumu_pfmet_corrected_phi", 30,
        phimin, phimax,
        DrawConfig(xmin=phimin, xmax=phimax, xlabel='PF MET #phi'))

    sampMan.cacheDraw(
        "uraw1", "histo_zjets_mumu_pfmet_uraw1_pt", u1_bins,
        DrawConfig(xmin=-20.0,
                   xmax=100.0,
                   xlabel="Raw u_{#parallel} [GeV]",
                   extraText='#mu#mu channel'))
    sampMan.cacheDraw(
        "uraw2", "histo_zjets_mumu_pfmet_uraw2_pt", u2_bins,
        DrawConfig(xmin=-30.0,
                   xmax=30.0,
                   xlabel="Raw u_{#perp  } [GeV]",
                   extraText='#mu#mu channel'))

    sampMan.cacheDraw(
        "ucor1", "histo_zjets_mumu_pfmet_ucor1_pt", u1_bins,
        DrawConfig(xmin=-20.0,
                   xmax=100.0,
                   xlabel="u_{#parallel} [GeV]",
                   extraText='#mu#mu channel'))
    sampMan.cacheDraw(
        "ucor2", "histo_zjets_mumu_pfmet_ucor2_pt", u2_bins,
        DrawConfig(xmin=-30.0,
                   xmax=30.0,
                   xlabel="u_{#perp  } [GeV]",
                   extraText='#mu#mu channel'))

    sampMan.launchDraw()

    sampMan.dumpCounts()

    print "Program end..."

    raw_input()

    return
Esempio n. 2
0
def main():
    print "Program start..."

    #ROOT.gROOT.ProcessLine('TFile* f_zpt = TFile::Open("results/zpt_weight.root")')
    #ROOT.gROOT.ProcessLine('TH1D* h_zpt_ratio  = (TH1D*)f_zpt->Get("h_zpt_ratio")')

    if doMuon:
        input_antiiso_data = "inputs/awmunu/input_data.txt"
        input_antiiso_wl0 = "inputs/awmunu/input_wm0.txt"
        input_antiiso_wl1 = "inputs/awmunu/input_wm1.txt"
        input_antiiso_wl2 = "inputs/awmunu/input_wm2.txt"
        input_antiiso_ttbar = "inputs/awmunu/input_ttbar_dilepton.txt"
        input_antiiso_ttbar_1lep = "inputs/awmunu/input_ttbar_singlelepton.txt"
        input_antiiso_ttbar_0lep = "inputs/awmunu/input_ttbar_hadronic.txt"
        input_antiiso_ww = "inputs/awmunu/input_ww.txt"
        input_antiiso_wz = "inputs/awmunu/input_wz.txt"
        input_antiiso_zz = "inputs/awmunu/input_zz.txt"
        input_antiiso_zxx = "inputs/awmunu/input_zxx.txt"
        input_antiiso_wx0 = "inputs/awmunu/input_wx0.txt"
        input_antiiso_wx1 = "inputs/awmunu/input_wx1.txt"
        input_antiiso_wx2 = "inputs/awmunu/input_wx2.txt"
    else:
        input_antiiso_data = "inputs/awenu/input_data.txt"
        input_antiiso_wl0 = "inputs/awenu/input_we0.txt"
        input_antiiso_wl1 = "inputs/awenu/input_we1.txt"
        input_antiiso_wl2 = "inputs/awenu/input_we2.txt"
        input_antiiso_ttbar = "inputs/awenu/input_ttbar_dilepton.txt"
        input_antiiso_ttbar_1lep = "inputs/awenu/input_ttbar_singlelepton.txt"
        input_antiiso_ttbar_0lep = "inputs/awenu/input_ttbar_hadronic.txt"
        input_antiiso_ww = "inputs/awenu/input_ww.txt"
        input_antiiso_wz = "inputs/awenu/input_wz.txt"
        input_antiiso_zz = "inputs/awenu/input_zz.txt"
        input_antiiso_zxx = "inputs/awenu/input_zxx.txt"
        input_antiiso_wx0 = "inputs/awenu/input_wx0.txt"
        input_antiiso_wx1 = "inputs/awenu/input_wx1.txt"
        input_antiiso_wx2 = "inputs/awenu/input_wx2.txt"

    ## for the QCD background estimation (data-driven)
    qcdnorm = -1.0
    DataAisoSamp = Sample(input_antiiso_data,
                          isMC=False,
                          name="Data_aiso",
                          isWSR=True,
                          additionalnorm=-1.0 * qcdnorm,
                          legend='QCD',
                          color='226')
    # W -> lnu
    Wl0AisoSamp = Sample(input_antiiso_wl0,
                         isMC=True,
                         name="wl0_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wl1AisoSamp = Sample(input_antiiso_wl1,
                         isMC=True,
                         name="wl1_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wl2AisoSamp = Sample(input_antiiso_wl2,
                         isMC=True,
                         name="wl2_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    # ttbar
    TTbarAisoSamp = Sample(input_antiiso_ttbar,
                           isMC=True,
                           name="ttbar_dilepton_aiso",
                           isWSR=True,
                           additionalnorm=-1.0 * qcdnorm)
    TT1LepAisoSamp = Sample(input_antiiso_ttbar_1lep,
                            isMC=True,
                            name="ttbar_1lepton_aiso",
                            isWSR=True,
                            additionalnorm=-1.0 * qcdnorm)
    TT0LepAisoSamp = Sample(input_antiiso_ttbar_0lep,
                            isMC=True,
                            name="ttbar_0lepton_aiso",
                            isWSR=True,
                            additionalnorm=-1.0 * qcdnorm)
    ## dibosons
    WWAisoSamp = Sample(input_antiiso_ww,
                        isMC=True,
                        name="WW_aiso",
                        isWSR=True,
                        additionalnorm=-1.0 * qcdnorm)
    WZAisoSamp = Sample(input_antiiso_wz,
                        isMC=True,
                        name="WZ_aiso",
                        isWSR=True,
                        additionalnorm=-1.0 * qcdnorm)
    ZZAisoSamp = Sample(input_antiiso_zz,
                        isMC=True,
                        name="ZZ_aiso",
                        isWSR=True,
                        additionalnorm=-1.0 * qcdnorm)
    # tau
    ZXXAisoSamp = Sample(input_antiiso_zxx,
                         isMC=True,
                         name="ZXX_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wx0AisoSamp = Sample(input_antiiso_wx0,
                         isMC=True,
                         name="wx0_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wx1AisoSamp = Sample(input_antiiso_wx1,
                         isMC=True,
                         name="wx1_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wx2AisoSamp = Sample(input_antiiso_wx2,
                         isMC=True,
                         name="wx2_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)

    sampMan = SampleManager(DataAisoSamp, [
        Wl0AisoSamp, Wl1AisoSamp, Wl2AisoSamp, TTbarAisoSamp, TT1LepAisoSamp,
        TT0LepAisoSamp, WWAisoSamp, WZAisoSamp, ZZAisoSamp, ZXXAisoSamp,
        Wx0AisoSamp, Wx1AisoSamp, Wx2AisoSamp
    ])

    sampMan.groupMCs([
        "WW_aiso", "WZ_aiso", "ZZ_aiso", "ZXX_aiso", "wx0_aiso", "wx1_aiso",
        "wx2_aiso"
    ], "EWK", 216, "EWK")
    sampMan.groupMCs(
        ["ttbar_dilepton_aiso", "ttbar_1lepton_aiso", "ttbar_0lepton_aiso"],
        "ttbar", 96, "t#bar{t}")
    label = "W#rightarrow#mu#nu" if doMuon else "W#rightarrow e#nu"
    sampMan.groupMCs(['wl0_aiso', 'wl1_aiso', 'wl2_aiso'], "wlnu", 92, label)

    sampMan.DefineAll("Lep_pt", "lep.Pt()")
    sampMan.ApplyCutAll("Lep_pt > 25.0")

    sampMan.DefineAll("w_iso1", "(relIso > 0.15 && relIso < 0.30)")
    sampMan.DefineAll("w_iso2", "(relIso > 0.30 && relIso < 0.45)")
    sampMan.DefineAll("w_iso3", "(relIso > 0.45 && relIso < 0.55)")
    sampMan.DefineAll("w_iso4", "(relIso > 0.55 && relIso < 0.65)")

    # muon and electron isolation distributions are different
    # more coarse binning for electrons to make sure enough statistics
    if doMuon:
        sampMan.DefineAll("w_iso5", "(relIso > 0.20 && relIso < 0.25)")
        sampMan.DefineAll("w_iso6", "(relIso > 0.25 && relIso < 0.30)")
        sampMan.DefineAll("w_iso7", "(relIso > 0.30 && relIso < 0.35)")
        sampMan.DefineAll("w_iso8", "(relIso > 0.35 && relIso < 0.40)")
        sampMan.DefineAll("w_iso9", "(relIso > 0.40 && relIso < 0.45)")
        sampMan.DefineAll("w_iso10", "(relIso > 0.45 && relIso < 0.50)")
        sampMan.DefineAll("w_iso11", "(relIso > 0.50 && relIso < 0.55)")
        sampMan.DefineAll("w_iso12", "(relIso > 0.55 && relIso < 0.60)")
        sampMan.DefineAll("w_iso13", "(relIso > 0.60 && relIso < 0.65)")
        isobins = [
            "iso1", "iso2", "iso3", "iso4", "iso5", "iso6", "iso7", "iso8",
            "iso9", "iso10", "iso11", "iso12", "iso13"
        ]
    else:
        sampMan.DefineAll("w_iso5", "(relIso > 0.20 && relIso < 0.25)")
        sampMan.DefineAll("w_iso6", "(relIso > 0.25 && relIso < 0.30)")
        sampMan.DefineAll("w_iso7", "(relIso > 0.30 && relIso < 0.35)")
        sampMan.DefineAll("w_iso8", "(relIso > 0.35 && relIso < 0.40)")
        sampMan.DefineAll("w_iso9", "(relIso > 0.40 && relIso < 0.50)")
        sampMan.DefineAll("w_iso10", "(relIso > 0.50 && relIso < 0.70)")
        isobins = [
            "iso1", "iso2", "iso3", "iso4", "iso5", "iso6", "iso7", "iso8",
            "iso9", "iso10"
        ]

    sampMan.DefineAll("Lep_eta", "lep.Eta()")
    sampMan.DefineMC("met_pt", "metVars[1]", excludes=['Data_aiso'])
    sampMan.DefineMC("met_phi",
                     "TVector2::Phi_mpi_pi(metVarsPhi[1])",
                     excludes=['Data_aiso'])
    # Data does not run any recoil corrections
    DataAisoSamp.Define("met_pt", "metVars[0]")
    DataAisoSamp.Define("met_phi", "TVector2::Phi_mpi_pi(metVarsPhi[0])")
    sampMan.DefineAll("met_raw_pt", "metVars[0]")
    sampMan.DefineAll("met_raw_phi", "TVector2::Phi_mpi_pi(metVarsPhi[0])")

    #sampMan.DefineAll("weight_wplus", "weight_WoVpt * (q>0)", excludeGroups=['QCD'])
    #sampMan.DefineAll("weight_wminus", "weight_WoVpt * (q<0)", excludeGroups=['QCD'])
    #sampMan.DefineSpecificMCs("weight_wplus", "weight_WoVpt * (q>0)", sampgroupnames=['QCD'])
    #sampMan.DefineSpecificMCs("weight_wminus", "weight_WoVpt * (q<0)", sampgroupnames=['QCD'])
    sampMan.DefineAll("weight_wplus", "weight_WoVpt * (q>0)")
    sampMan.DefineAll("weight_wminus", "weight_WoVpt * (q<0)")

    #sampMan.DefineAll("weight_eta0to0p8",   "fabs(Lep_eta)<0.8")
    #sampMan.DefineAll("weight_eta0p8to1p4", "fabs(Lep_eta)>0.8 && fabs(Lep_eta)<1.4")
    #sampMan.DefineAll("weight_eta1p4to2",   "fabs(Lep_eta)<1.4 && fabs(Lep_eta)<2.0")
    #sampMan.DefineAll("weight_eta2to2p4",   "fabs(Lep_eta)>2.0 && fabs(Lep_eta)<2.4")

    # recoil variables
    sampMan.DefineAll("V2W", "UVec(lep.Pt(), lep.Phi(), met_pt, met_phi)")
    sampMan.DefineAll("WpT", "V2W.Mod()")
    sampMan.DefineAll("Wphi", "TVector2::Phi_mpi_pi(V2W.Phi())")

    # WpT bins
    sampMan.DefineAll("WpT_bin0", "WpT>0.0")
    sampMan.DefineAll("WpT_bin1", "WpT<8.0")
    sampMan.DefineAll("WpT_bin2", "WpT>8.0 &&  WpT<16.0")
    sampMan.DefineAll("WpT_bin3", "WpT>16.0 && WpT<24.0")
    sampMan.DefineAll("WpT_bin4", "WpT>24.0 && WpT<32.0")
    sampMan.DefineAll("WpT_bin5", "WpT>32.0 && WpT<40.0")
    sampMan.DefineAll("WpT_bin6", "WpT>40.0 && WpT<50.0")
    sampMan.DefineAll("WpT_bin7", "WpT>50.0 && WpT<70.0")
    sampMan.DefineAll("WpT_bin8", "WpT>70.0 && WpT<100.0")
    wptbins = [
        "WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5",
        "WpT_bin6", "WpT_bin7", "WpT_bin8"
    ]

    # eta bins for electrons: barral and endcap
    sampMan.DefineAll("lepEta_bin0", "1.0")
    sampMan.DefineAll("lepEta_bin1", "abs(lep.Eta()) <= 1.4442")
    sampMan.DefineAll("lepEta_bin2", "abs(lep.Eta()) > 1.4442")

    if doMuon:
        etabins = ["lepEta_bin0"]
    else:
        etabins = ["lepEta_bin0", "lepEta_bin1", "lepEta_bin2"]

    # pT_ell / mT
    sampMan.DefineAll("ptOvermT", "Lep_pt / mtCorr")

    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9', 'iso10', 'iso11', 'iso12', 'iso13']:
    for iso in isobins:
        for chg in ['WoVpt', 'wplus', 'wminus']:
            #for eta in ['0to0p8', '0p8to1p4', '1p4to2', '2to2p4']:
            #for wpt in ["WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5", "WpT_bin6", "WpT_bin7", "WpT_bin8"]:
            for wpt in wptbins:
                for lepeta in etabins:
                    sampMan.DefineAll(
                        "weight_{}_{}_{}_{}".format(chg, iso, wpt, lepeta),
                        "w_{} * weight_{} * {} * {}".format(
                            iso, chg, wpt, lepeta))

    met_pt_bins = np.array([
        0., 2.0, 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36,
        39, 42, 45, 48, 51, 55, 60, 65, 70, 75, 80, 90, 100, 110, 120, 135,
        150, 165, 180, 200
    ])
    u1_bins = np.array([
        -40., -36., -32., -28., -25., -22.0, -20.0, -18, -16, -14, -12, -10,
        -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
        30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 53, 56, 59, 64, 68, 72, 76,
        80, 85, 90, 100
    ])
    u2_bins = np.array([
        -80., -70., -65., -60., -56., -52, -48, -44, -40, -37, -34, -31, -28,
        -25., -22., -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6,
        8, 10, 12, 16, 18, 20, 22, 25, 28, 31, 34, 37, 40, 44, 48, 52, 56, 60,
        65, 70, 80
    ])
    #u_bins = np.array([0., 2., 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, 52, 56, 60, 64, 68, 72, 76, 80, 85, 90, 95, 100, 105, 110, 115, 120, 130, 140, 150])
    u_bins = np.concatenate((np.linspace(0, 20, 11), np.linspace(24, 60, 10)))
    phimin = -ROOT.TMath.Pi()
    phimax = ROOT.TMath.Pi()

    eta_bins = np.concatenate(
        (np.linspace(-2.4, -2.0,
                     3), np.linspace(-1.8, 1.8, 37), np.linspace(2.0, 2.4, 3)))
    pt_bins = np.concatenate(
        (np.linspace(0, 15, 6), np.linspace(17, 23, 4), np.linspace(25, 35, 6),
         np.linspace(36, 55, 20), np.linspace(57, 63,
                                              4), np.linspace(66, 70, 2),
         np.linspace(75, 100, 6)))
    mass_bins = np.concatenate(
        (np.linspace(0, 52, 14), np.linspace(55, 67,
                                             5), np.linspace(70, 76, 3),
         np.linspace(78, 86, 5), np.linspace(87, 96, 10),
         np.linspace(98, 104, 5), np.linspace(106, 110, 2)))
    u_bins = np.concatenate((np.linspace(0, 20, 11), np.linspace(24, 80, 15),
                             np.linspace(85, 109, 4), np.linspace(120, 150,
                                                                  3)))
    mt_bins = np.concatenate(
        (np.linspace(0, 20, 6), np.linspace(22, 38,
                                            9), np.linspace(40, 60, 21),
         np.linspace(62, 80, 10), np.linspace(84, 120, 10)))
    met_pt_bins = np.array([
        0., 2.0, 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36,
        39, 42, 45, 48, 51, 55, 60, 65, 70, 75, 80
    ])

    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9']:
    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9', 'iso10', 'iso11', 'iso12', 'iso13']:
    for iso in isobins:
        for chg in ['WoVpt', 'wplus', 'wminus']:
            #for wpt in ["WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5", "WpT_bin6", "WpT_bin7", "WpT_bin8"]:
            for wpt in wptbins:
                for lepeta in etabins:
                    if chg == 'WoVpt':
                        continue
                    strname = "weight_{}_{}_{}_{}".format(
                        chg, iso, wpt, lepeta)
                    #outputname = "histo_wjetsAntiIso_mtcorr_" + strname
                    ##sampMan.cacheDraw("mtCorr", outputname, mt_bins, DrawConfig(xmin=0, xmax=120, xlabel="m_{T} [GeV]", dology=False, ymax=1.0e4), weightname = strname)
                    #sampMan.cacheDraw("mtCorr", outputname, 25, 40, 140, DrawConfig(xmin=40, xmax=140, xlabel="m_{T} [GeV]", dology=False, ymax=1e4, donormalizebin=False, addOverflow=False, addUnderflow=False, showratio=True), weightname = strname)

                    outputname = "histo_wjetsAntiIso_fullrange_mtcorr_" + strname
                    sampMan.cacheDraw("mtCorr",
                                      outputname,
                                      70,
                                      0,
                                      140,
                                      DrawConfig(xmin=0,
                                                 xmax=140,
                                                 xlabel="m_{T} [GeV]",
                                                 dology=False,
                                                 ymax=2e4,
                                                 donormalizebin=False,
                                                 addOverflow=False,
                                                 addUnderflow=False,
                                                 showratio=True),
                                      weightname=strname)
                    #outputname = "histo_wjetsAntiIso_WpTcorr_" + strname
                    #sampMan.cacheDraw("WpT", outputname, u_bins, DrawConfig(xmin=0, xmax=60, xlabel="p^{W}_{T} [GeV]", dology=False, ymax=1.0e4), weightname = strname)
                    #outputname = "histo_wjetsAntiIso_leppt_" + strname
                    #sampMan.cacheDraw("Lep_pt", outputname, pt_bins, DrawConfig(xmin=25, xmax=100, xlabel="p^{#mu}_{T} [GeV]", dology=False, ymax=1.0e4), weightname = strname)
                    #outputname = "histo_wjetsAntiIso_metpt_" + strname
                    #sampMan.cacheDraw("met_pt", outputname, met_pt_bins, DrawConfig(xmin=0, xmax=80, xlabel="PF MET [GeV]", dology=False, ymax=1e4), weightname = strname)

    sampMan.launchDraw()

    #hmts_comp = OrderedDict()
    ##for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9']:
    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9', 'iso10', 'iso11', 'iso12', 'iso13']:
    #    for chg in ['WoVpt', 'wplus', 'wminus']:
    #        for wpt in ["WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5", "WpT_bin6", "WpT_bin7", "WpT_bin8"]:
    #            for lepeta in etabins:
    #                if chg == "WoVpt":
    #                    continue
    #                strname = "weight_{}_{}_{}_{}".format(chg, iso, wpt, lepeta)

    #                outputname = "histo_wjetsAntiIso_mtcorr_" + strname
    #                hstacked = THStack2TH1(sampMan.hsmcs[outputname])
    #                for ibin in xrange(hstacked.GetNbinsX()+1):
    #                    # hstacked should always be above 0
    #                    hstacked.SetBinContent(ibin, max(hstacked.GetBinContent(ibin), 0))
    #                sampMan.hdatas[outputname].Add( hstacked, -1.0 )
    #                hmts_comp[strname] = sampMan.hdatas[outputname]
    #                hmts_comp[strname].SetName(outputname)

    #outfile = ROOT.TFile("output_qcdshape.root", "recreate")

    #for chg in ["wplus", "wminus"]:
    #    for i in xrange(1,13):
    #        for wpt in ["WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5", "WpT_bin6", "WpT_bin7", "WpT_bin8"]:
    #            iso = "iso" + str(i)
    #            iso_next = "iso" + str(i+1)
    #            strname = "weight_{}_{}_{}".format(chg, iso, wpt)
    #            strname_next = "weight_{}_{}_{}".format(chg, iso_next, wpt)

    #            outputname = "histo_wjetsAntiIso_mtcorr_" + strname

    #            hcenter = hmts_comp[strname]
    #            hup = hmts_comp[strname_next].Clone(outputname+"_shapeUp")
    #            hdown = hmts_comp[strname_next].Clone(outputname+"_shapeDown")

    #            hup.Scale(hcenter.Integral() / hup.Integral())

    #            for ibin in xrange(1, hcenter.GetNbinsX()+1):
    #                center = hcenter.GetBinContent(ibin)
    #                up = hup.GetBinContent(ibin)
    #                hdown.SetBinContent(ibin, max(2*center - up, 0))

    #                hcenter.SetBinContent(ibin, max(center, 0))
    #                hup.SetBinContent(ibin, max(up, 0))

    #            hcenter.SetDirectory(outfile)
    #            hcenter.Write()
    #            hup.SetDirectory(outfile)
    #            hup.Write()
    #            hdown.SetDirectory(outfile)
    #            hdown.Write()

    #outfile.Close()

    # repeat the same for the full mT one
    hmts_comp = OrderedDict()
    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9']:
    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9', 'iso10', 'iso11', 'iso12', 'iso13']:
    for iso in isobins:
        for chg in ['WoVpt', 'wplus', 'wminus']:
            #for wpt in ["WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5", "WpT_bin6", "WpT_bin7", "WpT_bin8"]:
            for wpt in wptbins:
                for lepeta in etabins:
                    if chg == "WoVpt":
                        continue
                    strname = "weight_{}_{}_{}_{}".format(
                        chg, iso, wpt, lepeta)

                    outputname = "histo_wjetsAntiIso_fullrange_mtcorr_" + strname
                    hstacked = THStack2TH1(sampMan.hsmcs[outputname])
                    for ibin in xrange(hstacked.GetNbinsX() + 1):
                        # hstacked should always be above 0
                        hstacked.SetBinContent(
                            ibin, max(hstacked.GetBinContent(ibin), 0))
                    sampMan.hdatas[outputname].Add(hstacked, -1.0)
                    hmts_comp[strname] = sampMan.hdatas[outputname]
                    hmts_comp[strname].SetName(outputname)

    lepname = "mu" if doMuon else "e"
    outfile = ROOT.TFile("output_qcdshape_fullrange_" + lepname + "nu.root",
                         "recreate")

    for chg in ["wplus", "wminus"]:
        #for i in xrange(1,13):
        #for i in xrange(1,10):
        for i in xrange(1, len(isobins)):
            #for wpt in ["WpT_bin0", "WpT_bin1", "WpT_bin2", "WpT_bin3", "WpT_bin4", "WpT_bin5", "WpT_bin6", "WpT_bin7", "WpT_bin8"]:
            for wpt in wptbins:
                for lepeta in etabins:
                    # skip iso10 as it is used for the uncertainty cal for iso9
                    iso = "iso" + str(i)
                    iso_next = "iso" + str(i + 1)
                    strname = "weight_{}_{}_{}_{}".format(
                        chg, iso, wpt, lepeta)
                    strname_next = "weight_{}_{}_{}_{}".format(
                        chg, iso_next, wpt, lepeta)

                    outputname = "histo_wjetsAntiIso_fullrange_mtcorr_" + strname

                    hcenter = hmts_comp[strname]
                    hup = hmts_comp[strname_next].Clone(outputname +
                                                        "_shapeUp")
                    hdown = hmts_comp[strname_next].Clone(outputname +
                                                          "_shapeDown")

                    hup.Scale(hcenter.Integral() / hup.Integral())

                    for ibin in xrange(1, hcenter.GetNbinsX() + 1):
                        center = hcenter.GetBinContent(ibin)
                        up = hup.GetBinContent(ibin)
                        hdown.SetBinContent(ibin, max(2 * center - up, 0))

                        hcenter.SetBinContent(ibin, max(center, 0))
                        hup.SetBinContent(ibin, max(up, 0))

                    hcenter.SetDirectory(outfile)
                    hcenter.Write()
                    hup.SetDirectory(outfile)
                    hup.Write()
                    hdown.SetDirectory(outfile)
                    hdown.Write()

    outfile.Close()

    # calculate the shape uncertainty and save them to root file
    #hmts_comp = OrderedDict()
    ##hwpts_comp = OrderedDict()
    ##hmupts_comp = OrderedDict()
    ##hmetpts_comp = OrderedDict()
    #for iso in ['iso1', 'iso2', 'iso3', 'iso4', 'iso5', 'iso6', 'iso7', 'iso8', 'iso9']:
    #    for chg in ['WoVpt', 'wplus', 'wminus']:
    #        if chg == "WoVpt":
    #            continue
    #        strname = "weight_{}_{}".format(chg, iso)

    #        outputname = "histo_wjetsAntiIso_mtcorr_" + strname
    #        sampMan.hdatas[outputname].Add( THStack2TH1(sampMan.hsmcs[outputname]), -1 )
    #        hmts_comp[strname] = sampMan.hdatas[outputname]

    #outputname = "histo_wjetsAntiIso_WpTcorr_" + strname
    #sampMan.hdatas[outputname].Add( THStack2TH1(sampMan.hsmcs[outputname]), -1 )
    #hwpts_comp[strname] = sampMan.hdatas[outputname]

    #outputname = "histo_wjetsAntiIso_leppt_" + strname
    #sampMan.hdatas[outputname].Add( THStack2TH1(sampMan.hsmcs[outputname]), -1 )
    #hmupts_comp[strname] = sampMan.hdatas[outputname]

    #outputname = "histo_wjetsAntiIso_metpt_" + strname
    #sampMan.hdatas[outputname].Add( THStack2TH1(sampMan.hsmcs[outputname]), -1 )
    #hmetpts_comp[strname] = sampMan.hdatas[outputname]

    #labels = ["|#eta|<0.8", "0.8<|#eta|<1.4", "1.4<|#eta|<2.0", "2.0<|#eta|<2.4"]
    #DrawHistos(hmts_comp.values(), labels, 0, 120, 'm_{T} [GeV]', 2e-4, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_mT', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3, 4], ratiobase=1, drawoptions=["e", "e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hwpts_comp.values(), labels, 0, 60, 'p^{W}_{T} [GeV]', 2e-3, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_WpT', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3, 4], ratiobase=1, drawoptions=["e", "e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hmupts_comp.values(), labels, 25, 100, 'p^{#mu}_{T} [GeV]', 2e-3, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_mupt', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3, 4], ratiobase=1, drawoptions=["e", "e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hmetpts_comp.values(), labels, 0, 80, 'p^{miss}_{T} [GeV]', 2e-3, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_metpt', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3, 4], ratiobase=1, drawoptions=["e", "e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hmts_comp.values(), ['0.15<I<0.30', '0.30<I<0.45', '0.45<I<0.55'], 40, 120, 'm_{T} [GeV]', 2e-4, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_mT', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3], ratiobase=1, drawoptions=["e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hwpts_comp.values(), ['0.15<I<0.30', '0.30<I<0.45', '0.45<I<0.55'], 0, 60, 'p^{W}_{T} [GeV]', 2e-3, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_WpT', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3], ratiobase=1, drawoptions=["e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hmupts_comp.values(), ['0.15<I<0.30', '0.30<I<0.45', '0.45<I<0.55'], 25, 70, 'p^{#mu}_{T} [GeV]', 2e-3, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_mupt', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3], ratiobase=1, drawoptions=["e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #DrawHistos(hmetpts_comp.values(), ['0.15<I<0.30', '0.30<I<0.45', '0.45<I<0.55'], 0, 80, 'p^{W}_{T} [GeV]', 2e-3, 5e-1, 'A.U.', 'histo_wjetsAntiIso_comp_metpt', dology=True, showratio=True, donormalize=True, mycolors=[1,2,3], ratiobase=1, drawoptions=["e", "e", "e"], legendoptions=['LPE', 'LPE', 'LPE'], yrmin=0.71, yrmax=1.29)

    #sampMan.launchDraw()

    sampMan.dumpCounts()

    print "Program end..."

    raw_input()

    return
Esempio n. 3
0
def main():

    if isData:
        inputfile = "inputs/inputs_Z/input_data.txt"
        samp = Sample(inputfile, isMC=False, name="Data")
        samp.donormalization = False
        samp.varyQCDScale = False
        samples = [samp]
    elif isAMCNLO:
        inputfile = "inputs/inputs_Z/input_zjets.txt"
        samp = Sample(inputfile, isMC=True, name="ZJets_NLO")
        samp.donormalization = False
        samp.varyQCDScale = True
        samples = [samp]
    elif isMADGRAPH:
        inputfile = "inputs/inputs_Z/input_zjets_madgraph.txt"
        samp = Sample(inputfile, isMC=True, name="ZJets_MG")
        samp.donormalization = False
        samp.varyQCDScale = False
        samples = [samp]
    elif isTTbar:
        inputfile = "inputs/inputs_Z/input_ttbar.txt"
        samp = Sample(inputfile,
                      isMC=True,
                      name="TTbar",
                      xsec=831.76 * 0.105 * 1e3)
        samp.donormalization = True
        samp.varyQCDScale = True
        samples = [samp]
    elif isDiboson:
        input_WW2L = "inputs/inputs_Z/input_WWTo2L2Nu.txt"
        input_WZ3L = "inputs/inputs_Z/input_WZTo3LNu.txt"
        input_ZZ2L = "inputs/inputs_Z/input_ZZTo2L2Nu.txt"
        input_ZZ4L = "inputs/inputs_Z/input_ZZTo4L.txt"
        WW2LSamp = Sample(input_WW2L, xsec=12.178 * 1e3, name="WW2L")
        WZ3LSamp = Sample(input_WZ3L, xsec=5.26 * 1e3, name="WZ3L")
        ZZ2LSamp = Sample(input_ZZ2L, xsec=0.564 * 1e3, name="ZZ2L")
        ZZ4LSamp = Sample(input_ZZ4L, xsec=1.212 * 1e3, name="ZZ4L")
        WW2LSamp.donormalization = True
        WZ3LSamp.donormalization = True
        ZZ2LSamp.donormalization = True
        ZZ4LSamp.donormalization = True
        WW2LSamp.varyQCDScale = False
        WZ3LSamp.varyQCDScale = False
        ZZ2LSamp.varyQCDScale = False
        ZZ4LSamp.varyQCDScale = False
        samples = [WW2LSamp, WZ3LSamp, ZZ2LSamp, ZZ4LSamp]

    ptbins = getPtBins()
    njetbins = getJetBins()

    print ptbins
    print njetbins

    # prepare u1 (uparal) and u2 (uperp) distributions
    # in differen Z pt and jet multiplicity bins
    def prepareU1U2(rdf, postfix="", extra_weight="1.0"):
        histos_u1 = OrderedDict()
        histos_u2 = OrderedDict()
        for ijet in xrange(njetbins.size - 1):
            njetmin = njetbins[ijet]
            njetmax = njetbins[ijet + 1]
            histos_u1[(njetmin, njetmax)] = OrderedDict()
            histos_u2[(njetmin, njetmax)] = OrderedDict()
            for ipt in xrange(ptbins.size - 1):
                ptmin = ptbins[ipt]
                ptmax = ptbins[ipt + 1]

                wstring = "njetbin_{}_ptbin_{}_{}".format(ijet, ipt, postfix)
                rdf = rdf.Define(
                    wstring,
                    "(jet_n>={} && jet_n<={}) * (Z_pt>={} && Z_pt<{} ) * weight * {}"
                    .format(njetmin, njetmax, ptmin, ptmax, extra_weight))
                hname_u1 = "hist_uparal_{}".format(wstring)
                histos_u1[(njetmin, njetmax)][(ptmin, ptmax)] = rdf.Histo1D(
                    (hname_u1, hname_u1, 240 + int(ptmax) - int(ptmin),
                     -120.0 + int(ptmin), 120.0 + int(ptmax)), "u1", wstring)
                hname_u2 = "hist_uperp_{}".format(wstring)
                histos_u2[(njetmin, njetmax)][(ptmin, ptmax)] = rdf.Histo1D(
                    (hname_u2, hname_u2, 240, -120.0, 120.0), "u2", wstring)

        return histos_u1, histos_u2

    for samp in samples:
        print "\n\n\nWork on sample ", samp.name

        weights = OrderedDict()
        if samp.varyQCDScale:
            # do the QCD scale variations
            # Skip mur_2_muf_0p5: EventWeights[5] and
            # and mur_0p5_muf_2: EventWeights[7]
            weights["weight_mur_1_muf_1"] = "EventWeights[0] / EventWeights[0]"
            #weights["weight_mur_1_muf_2"   ]  =  "EventWeights[1] / EventWeights[0]"
            #weights["weight_mur_1_muf_0p5" ]  =  "EventWeights[2] / EventWeights[0]"
            #weights["weight_mur_2_muf_1"   ]  =  "EventWeights[3] / EventWeights[0]"
            #weights["weight_mur_2_muf_2"   ]  =  "EventWeights[4] / EventWeights[0]"
            #weights["weight_mur_0p5_muf_1" ]  =  "EventWeights[6] / EventWeights[0]"
            #weights["weight_mur_0p5_muf_0p5"] =  "EventWeights[8] / EventWeights[0]"
        else:
            weights["central"] = "1.0"

        histos_u1 = OrderedDict()
        histos_u2 = OrderedDict()
        print "\n\nMaking histograms..."
        for wname, wstring in weights.iteritems():
            # making all histograms first, such that rdataframe could handle
            # everything in one loop.
            print wname, wstring
            samp.rdf = samp.rdf.Define(wname, wstring)
            histos_u1[wname], histos_u2[wname] = prepareU1U2(
                samp.rdf,
                postfix="{}_{}".format(samp.name, wname),
                extra_weight=wname)

        if samp.donormalization:
            print "\n\nScale the {} MC to the xsec with normalization factor {}".format(
                samp.name, samp.normfactor)
            for wname in histos_u1:
                for ijetbin in histos_u1[wname]:
                    for iptbin in histos_u1[wname][ijetbin]:
                        histos_u1[wname][ijetbin][iptbin].Scale(
                            samp.normfactor)
                        histos_u2[wname][ijetbin][iptbin].Scale(
                            samp.normfactor)

        print "\n\nWriting to output file..."
        for wname, wstring in weights.iteritems():
            ofilename = "results/U1U2/histos_u1u2_{}_njets_pt_{}.root".format(
                samp.name, wname)

            print "Write to output file ", ofilename
            ofile = ROOT.TFile(ofilename, "recreate")
            for ijetbin in histos_u1[wname].keys():
                for iptbin in histos_u1[wname][ijetbin].keys():
                    histos_u1[wname][ijetbin][iptbin].GetValue().SetDirectory(
                        ofile)
                    histos_u1[wname][ijetbin][iptbin].GetValue().Write()
                    histos_u2[wname][ijetbin][iptbin].GetValue().SetDirectory(
                        ofile)
                    histos_u2[wname][ijetbin][iptbin].GetValue().Write()

        ofile.Close()

    print "Finished.."

    return
Esempio n. 4
0
def main():
    print "Program start..."

    if dotest:
        input_data = "inputs/inputs_W/input_data_test.txt"
        input_wjets = "inputs/inputs_W/input_wjets_test.txt"
        # for the data-driven bkg estimation from the QCD dominated region
        input_data_qcdcr = "inputs/inputs_QCDCR/input_data_test.txt"
        input_wjets_qcdcr = "inputs/inputs_QCDCR/input_wjets_test.txt"
        input_zjets_qcdcr = "inputs/inputs_QCDCR/input_zjets_test.txt"
        input_ttbar_qcdcr = "inputs/inputs_QCDCR/input_ttbar_test.txt"
        input_lept_qcdcr = "inputs/inputs_QCDCR/input_ttbar_singlelep_fromT_test.txt"
        input_leptbar_qcdcr = "inputs/inputs_QCDCR/input_ttbar_singlelep_fromTbar_test.txt"
    else:
        input_data = "inputs/inputs_W/input_data.txt"
        input_wjets = "inputs/inputs_W/input_wjets.txt"
        input_zjets = "inputs/inputs_W/input_zjets.txt"
        input_ttbar = "inputs/inputs_W/input_ttbar.txt"
        input_lept = "inputs/inputs_W/input_ttbar_singlelep_fromT.txt"
        input_leptbar = "inputs/inputs_W/input_ttbar_singlelep_fromTbar.txt"
        #input_qcd     = "inputs/inputs_W/input_qcd.txt"
        # for the data-driven bkg estimation from the QCD dominated region
        input_data_qcdcr = "inputs/inputs_QCDCR/input_data.txt"
        input_wjets_qcdcr = "inputs/inputs_QCDCR/input_wjets.txt"
        input_zjets_qcdcr = "inputs/inputs_QCDCR/input_zjets.txt"
        input_ttbar_qcdcr = "inputs/inputs_QCDCR/input_ttbar.txt"
        input_lept_qcdcr = "inputs/inputs_QCDCR/input_ttbar_singlelep_fromT.txt"
        input_leptbar_qcdcr = "inputs/inputs_QCDCR/input_ttbar_singlelep_fromTbar.txt"

    DataSamp = Sample(input_data,
                      isMC=False,
                      legend="Data",
                      name="Data",
                      isZSR=False,
                      isWSR=True)
    WJetsSamp = Sample(input_wjets,
                       xsec=61526.7 * 1e3,
                       color=38,
                       legend="WJets",
                       name="WJets",
                       isZSR=False,
                       isWSR=True)
    if not dotest:
        ZJetsSamp = Sample(input_zjets,
                           xsec=6077.22 * 1e3,
                           color=5,
                           legend="DYJets",
                           name="ZJets",
                           isZSR=False,
                           isWSR=True)
        TTbarSamp = Sample(input_ttbar,
                           xsec=831.76 * 0.105 * 1e3,
                           color=46,
                           legend="t#bar{t}",
                           name="ttbar2lep",
                           isZSR=False,
                           isWSR=True)
        TTLepTSamp = Sample(input_lept,
                            xsec=831.76 * 0.219 * 1e3,
                            color=47,
                            legend="t#bar{t}",
                            name="ttbarlept",
                            isZSR=False,
                            isWSR=True)
        TTLepTbarSamp = Sample(input_leptbar,
                               xsec=831.76 * 0.219 * 1e3,
                               color=48,
                               legend="t#bar{t}",
                               name="ttbarleptbar",
                               isZSR=False,
                               isWSR=True)
        #QCDSamp   = Sample(input_qcd,   xsec = 720648000.0*0.00042*1e3, color=8, legend="QCD", name="QCD", isZSR=False, isWSR=True)

    DataQCDCRSamp = Sample(input_data_qcdcr,
                           isMC=False,
                           legend="Data_QCDCR",
                           name="Data_QCDCR",
                           isZSR=False,
                           isWSR=True)
    WJetsQCDCRSamp = Sample(input_wjets_qcdcr,
                            xsec=-61526.7 * 1e3,
                            color=38,
                            legend="WJets_QCDCR",
                            name="WJets_QCDCR",
                            isZSR=False,
                            isWSR=True,
                            applySF=False)
    ZJetsQCDCRSamp = Sample(input_zjets_qcdcr,
                            xsec=-6077.22 * 1e3,
                            color=5,
                            legend="ZJets_QCDCR",
                            name="ZJets_QCDCR",
                            isZSR=False,
                            isWSR=True,
                            applySF=False)
    TTbarQCDCRSamp = Sample(input_ttbar_qcdcr,
                            xsec=-831.76 * 0.105 * 1e3,
                            color=46,
                            legend="t#bar{t} QCDCR",
                            name="ttbar2lep_QCDCR",
                            isZSR=False,
                            isWSR=True,
                            applySF=False)
    TTLepTQCDCRSamp = Sample(input_lept_qcdcr,
                             xsec=-831.76 * 0.219 * 1e3,
                             color=47,
                             legend="t#bar{t} QCDCR",
                             name="ttbarlept_QCDCR",
                             isZSR=False,
                             isWSR=True,
                             applySF=False)
    TTLepTbarQCDCRSamp = Sample(input_leptbar_qcdcr,
                                xsec=-831.76 * 0.219 * 1e3,
                                color=48,
                                legend="t#bar{t} QCDCR",
                                name="ttbarleptb",
                                isZSR=False,
                                isWSR=True,
                                applySF=False)

    if not dotest:
        sampMan = SampleManager(DataSamp, [
            WJetsSamp, ZJetsSamp, TTbarSamp, TTLepTSamp, TTLepTbarSamp,
            DataQCDCRSamp, WJetsQCDCRSamp, ZJetsQCDCRSamp, TTbarQCDCRSamp,
            TTLepTQCDCRSamp, TTLepTbarQCDCRSamp
        ])
    else:
        sampMan = SampleManager(DataSamp, [
            WJetsSamp, DataQCDCRSamp, WJetsQCDCRSamp, ZJetsQCDCRSamp,
            TTbarQCDCRSamp, TTLepTQCDCRSamp, TTLepTbarQCDCRSamp
        ])

    # create renormalized QCD scale variation weights for WJets
    def prepareWJetsQCDScaleVariations():
        print "prepare the renormalized QCD scale variations"
        QCDVariations = OrderedDict({
            "central": 0,
            "mur_1_muf_2": 1,
            "mur_1_muf_0p5": 2,
            "mur_2_muf_1": 3,
            "mur_2_muf_2": 4,
            "mur_0p5_muf_1": 6,
            "mur_0p5_muf_0p5": 8
        })
        hcounts = OrderedDict()
        WJetsSamp.Define("const_1", "1.0")
        for var, index in QCDVariations.iteritems():
            WJetsSamp.Define(
                "weight_" + var,
                "weight * EventWeights[%d]/EventWeights[0]" % index)
            hcounts[var] = WJetsSamp.rdf.Histo1D(
                ("hcounts_" + var, "hcounts_" + var, 2, 0, 2), "const_1",
                "weight_" + var)

        nevents_norm = hcounts['central'].Integral()
        for var, hist in hcounts.iteritems():
            nevents = hcounts[var].Integral()
            print "For {}: Total: {}, weighted:{}. Norm factor {}".format(
                var, nevents_norm, nevents, nevents_norm / nevents)
            weightname = "weight_" + var
            WJetsSamp.Define(
                weightname + "_scaled",
                "{} * {}".format(weightname, nevents_norm / nevents))
        print "finished preparing renormalized QCD scale variations"

    prepareWJetsQCDScaleVariations()

    sampMan.groupMCs([
        "Data_QCDCR", "WJets_QCDCR", "ZJets_QCDCR", "ttbar2lep_QCDCR",
        "ttbarlept_QCDCR", "ttbarleptb"
    ],
                     "QCD",
                     17,
                     "QCD",
                     renormalize=False,
                     renormalizefactor=3.29)
    sampMan.groupMCs(["ttbar2lep", "ttbarlept", "ttbarleptbar"], "ttbar", 46,
                     "t#bar{t}")

    # QCD enriched isolation regions
    sampMan.ApplyCutSpecificMCs("(mu_pfIso[0]>0.30 && mu_pfIso[0]<0.60)",
                                sampgroupnames=["QCD"])
    sampMan.DefineSpecificMCs(
        "weight_Iso0",
        "weight * (mu_pfIso[0]>0.30 && mu_pfIso[0]<0.60)",
        sampgroupnames=["QCD"])
    sampMan.DefineAll("weight_Iso0", "weight", excludeGroups=['QCD'])
    sampMan.DefineSpecificMCs(
        "weight_Iso1",
        "weight * (mu_pfIso[0]>0.30 && mu_pfIso[0]<0.60) * 3.32/3.29",
        sampgroupnames=["QCD"])
    sampMan.DefineAll("weight_Iso1", "weight", excludeGroups=['QCD'])
    sampMan.DefineSpecificMCs(
        "weight_Iso2",
        "weight * (mu_pfIso[0]>0.30 && mu_pfIso[0]<0.60) * 3.20/3.29",
        sampgroupnames=["QCD"])
    sampMan.DefineAll("weight_Iso2", "weight", excludeGroups=['QCD'])

    sampMan.DefineAll("Mu_pt", "mu_pt[0]")
    sampMan.DefineAll(
        "mT_PF", "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], met_pt, met_phi)")
    sampMan.DefineAll(
        "mT_Puppi",
        "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], puppimet_pt, puppimet_phi)")
    sampMan.DefineAll(
        "mT_Deep",
        "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt,  deepmet_phi)")
    sampMan.DefineAll("u_reco_pt_PF",
                      "calUT_PtPhi(mu_pt[0], mu_phi[0], met_pt, met_phi)")
    sampMan.DefineAll(
        "u_reco_pt_Puppi",
        "calUT_PtPhi(mu_pt[0], mu_phi[0], puppimet_pt, puppimet_phi)")
    sampMan.DefineAll(
        "u_reco_pt_Deep",
        "calUT_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt,  deepmet_phi)")

    # correct DeepMET
    WJetsSamp.Define("trueW_index", "trueWIndex(trueW_n)")
    WJetsSamp.Define(
        "W_pt",
        "(trueW_index >=0 && trueW_index<=1) ? trueW_pt[trueW_index] : 0")
    WJetsSamp.Define(
        "W_phi",
        "(trueW_index>=0 && trueW_index<=1) ? trueW_phi[trueW_index] : 0")
    WJetsSamp.Define("Uvec",
                     "UVec(mu_pt[0], mu_phi[0], deepmet_pt, deepmet_phi)")
    WJetsSamp.Define("u_pt", "Uvec.Mod()")
    WJetsSamp.Define("u_phi", "Uvec.Phi()")
    WJetsSamp.Define("u1", "u_pt * TMath::Cos(u_phi + TMath::Pi() - W_phi)")
    WJetsSamp.Define("u2", "u_pt * TMath::Sin(u_phi + TMath::Pi() - W_phi)")

    ROOT.gROOT.ProcessLine(
        'TFile* fitfunctions_Data = TFile::Open("results/Fit/fitfunctions_Data_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TH1F* h1_ptbins_Data_central   = (TH1F*)fitfunctions_Data->Get("h1_ptbins_Data_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TH1F* h1_njetbins_Data_central = (TH1F*)fitfunctions_Data->Get("h1_njetbins_Data_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_Data_u1_njets_pt_central  = (TList*)fitfunctions_Data->Get("tfs_Data_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_Data_u2_njets_pt_central  = (TList*)fitfunctions_Data->Get("tfs_Data_u2_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TFile* fitfunctions_ZJets_NLO_central = TFile::Open("results/Fit/fitfunctions_ZJets_NLO_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_u1_njets_pt_central = (TList*)fitfunctions_ZJets_NLO_central->Get("tfs_ZJets_NLO_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_u2_njets_pt_central = (TList*)fitfunctions_ZJets_NLO_central->Get("tfs_ZJets_NLO_u2_njets_pt_central")'
    )

    WJetsSamp.Define(
        "u1_corr_central",
        "UCorrection_Quant(u1, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u1_njets_pt_central, tfs_MC_u1_njets_pt_central, 0.00001)"
    )
    WJetsSamp.Define(
        "u2_corr_central",
        "UCorrection_Quant(u2, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u2_njets_pt_central, tfs_MC_u2_njets_pt_central, 0.00001)"
    )
    WJetsSamp.Define(
        "deepmet_corr_central",
        "METVec(mu_pt[0], mu_phi[0], u1_corr_central, u2_corr_central, W_phi)")
    WJetsSamp.Define("deepmet_pt_corr_central", "deepmet_corr_central.Mod()")
    WJetsSamp.Define("deepmet_phi_corr_central",
                     "TVector2::Phi_mpi_pi(deepmet_corr_central.Phi())")
    WJetsSamp.Define(
        "mT_Deep_corr_central",
        "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_central, deepmet_phi_corr_central)"
    )
    sampMan.DefineAll("deepmet_pt_corr_central",
                      "deepmet_pt",
                      excludes=['WJets'])
    sampMan.DefineAll("deepmet_phi_corr_central",
                      "deepmet_phi",
                      excludes=['WJets'])
    sampMan.DefineAll("mT_Deep_corr_central", "mT_Deep", excludes=['WJets'])
    sampMan.DefineAll(
        "u_reco_pt_Deep_corr_central",
        "calUT_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_central,  deepmet_phi_corr_central)"
    )

    # systematics variations
    # corrections from MadGraph
    ROOT.gROOT.ProcessLine(
        'TFile* fitfunctions_ZJets_MG_central = TFile::Open("results/Fit/fitfunctions_ZJets_MG_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_MG_u1_njets_pt_central = (TList*)fitfunctions_ZJets_MG_central->Get("tfs_ZJets_MG_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_MG_u2_njets_pt_central = (TList*)fitfunctions_ZJets_MG_central->Get("tfs_ZJets_MG_u2_njets_pt_central")'
    )

    WJetsSamp.Define(
        "u1_corr_central_MG",
        "UCorrection_Quant(u1, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u1_njets_pt_central, tfs_MC_MG_u1_njets_pt_central, 0.00001)"
    )
    WJetsSamp.Define(
        "u2_corr_central_MG",
        "UCorrection_Quant(u2, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u2_njets_pt_central, tfs_MC_MG_u2_njets_pt_central, 0.00001)"
    )
    WJetsSamp.Define(
        "deepmet_corr_central_MG",
        "METVec(mu_pt[0], mu_phi[0], u1_corr_central_MG, u2_corr_central_MG, W_phi)"
    )
    WJetsSamp.Define("deepmet_pt_corr_central_MG",
                     "deepmet_corr_central_MG.Mod()")
    WJetsSamp.Define("deepmet_phi_corr_central_MG",
                     "TVector2::Phi_mpi_pi(deepmet_corr_central_MG.Phi())")
    WJetsSamp.Define(
        "mT_Deep_corr_central_MG",
        "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_central_MG, deepmet_phi_corr_central_MG)"
    )
    sampMan.DefineAll("deepmet_pt_corr_central_MG",
                      "deepmet_pt",
                      excludes=['WJets'])
    sampMan.DefineAll("deepmet_phi_corr_central_MG",
                      "deepmet_phi",
                      excludes=['WJets'])
    sampMan.DefineAll("mT_Deep_corr_central_MG", "mT_Deep", excludes=['WJets'])
    sampMan.DefineAll(
        "u_reco_pt_Deep_corr_central_MG",
        "calUT_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_central_MG,  deepmet_phi_corr_central_MG)"
    )

    # corrections from Gaussian Smearing
    ROOT.gROOT.ProcessLine(
        'TFile* gaussSmoother_Data_njets_pt_central = TFile::Open("results/GaussSmoother/gaussSmoother_Data_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_Data_u1_njets_pt_central = (TList*)gaussSmoother_Data_njets_pt_central->Get("cdfs_Data_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_Data_u2_njets_pt_central = (TList*)gaussSmoother_Data_njets_pt_central->Get("cdfs_Data_u2_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TFile* gaussSmoother_ZJets_NLO_njets_pt_central = TFile::Open("results/GaussSmoother/gaussSmoother_ZJets_NLO_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_ZJets_NLO_u1_njets_pt_central = (TList*)gaussSmoother_ZJets_NLO_njets_pt_central->Get("cdfs_ZJets_NLO_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_ZJets_NLO_u2_njets_pt_central = (TList*)gaussSmoother_ZJets_NLO_njets_pt_central->Get("cdfs_ZJets_NLO_u2_njets_pt_central")'
    )

    WJetsSamp.Define(
        "u1_corr_central_GKS",
        "UCorrection_Quant(u1, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, cdfs_Data_u1_njets_pt_central, cdfs_ZJets_NLO_u1_njets_pt_central, 0.00001)"
    )
    WJetsSamp.Define(
        "u2_corr_central_GKS",
        "UCorrection_Quant(u2, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, cdfs_Data_u2_njets_pt_central, cdfs_ZJets_NLO_u2_njets_pt_central, 0.00001)"
    )
    WJetsSamp.Define(
        "deepmet_corr_central_GKS",
        "METVec(mu_pt[0], mu_phi[0], u1_corr_central_GKS, u2_corr_central_GKS, W_phi)"
    )
    WJetsSamp.Define("deepmet_pt_corr_central_GKS",
                     "deepmet_corr_central_GKS.Mod()")
    WJetsSamp.Define("deepmet_phi_corr_central_GKS",
                     "TVector2::Phi_mpi_pi(deepmet_corr_central_GKS.Phi())")
    WJetsSamp.Define(
        "mT_Deep_corr_central_GKS",
        "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_central_GKS, deepmet_phi_corr_central_GKS)"
    )
    sampMan.DefineAll("deepmet_pt_corr_central_GKS",
                      "deepmet_pt",
                      excludes=['WJets'])
    sampMan.DefineAll("deepmet_phi_corr_central_GKS",
                      "deepmet_phi",
                      excludes=['WJets'])
    sampMan.DefineAll("mT_Deep_corr_central_GKS",
                      "mT_Deep",
                      excludes=['WJets'])
    sampMan.DefineAll(
        "u_reco_pt_Deep_corr_central_GKS",
        "calUT_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_central_GKS,  deepmet_phi_corr_central_GKS)"
    )

    # correction from ttbar qcd scale:
    def varyTTbarQCDScale(wstring):
        ROOT.gROOT.ProcessLine(
            'TFile* fitfunctions_Data_{WS} = TFile::Open("results/Fit/fitfunctions_Data_njets_pt_{WS}.root")'
            .format(WS=wstring))
        ROOT.gROOT.ProcessLine(
            'TList* tfs_Data_u1_njets_pt_{WS}  = (TList*)fitfunctions_Data_{WS}->Get("tfs_Data_u1_njets_pt_{WS}")'
            .format(WS=wstring))
        ROOT.gROOT.ProcessLine(
            'TList* tfs_Data_u2_njets_pt_{WS}  = (TList*)fitfunctions_Data_{WS}->Get("tfs_Data_u2_njets_pt_{WS}")'
            .format(WS=wstring))

        WJetsSamp.Define(
            "u1_corr_" + wstring,
            "UCorrection_Quant(u1, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u1_njets_pt_{WS}, tfs_MC_u1_njets_pt_central, 0.00001)"
            .format(WS=wstring))
        WJetsSamp.Define(
            "u2_corr_" + wstring,
            "UCorrection_Quant(u2, jet_n, W_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u2_njets_pt_{WS}, tfs_MC_u2_njets_pt_central, 0.00001)"
            .format(WS=wstring))
        WJetsSamp.Define(
            "deepmet_corr_" + wstring,
            "METVec(mu_pt[0], mu_phi[0], u1_corr_{WS}, u2_corr_{WS}, W_phi)".
            format(WS=wstring))
        WJetsSamp.Define("deepmet_pt_corr_" + wstring,
                         "deepmet_corr_{WS}.Mod()".format(WS=wstring))
        WJetsSamp.Define(
            "deepmet_phi_corr_" + wstring,
            "TVector2::Phi_mpi_pi(deepmet_corr_{WS}.Phi())".format(WS=wstring))
        WJetsSamp.Define(
            "mT_Deep_corr_" + wstring,
            "calMT_fromMET_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_{WS}, deepmet_phi_corr_{WS})"
            .format(WS=wstring))
        sampMan.DefineAll("deepmet_pt_corr_" + wstring,
                          "deepmet_pt",
                          excludes=['WJets'])
        sampMan.DefineAll("deepmet_phi_corr_" + wstring,
                          "deepmet_phi",
                          excludes=['WJets'])
        sampMan.DefineAll("mT_Deep_corr_" + wstring,
                          "mT_Deep",
                          excludes=['WJets'])
        sampMan.DefineAll(
            "u_reco_pt_Deep_corr_" + wstring,
            "calUT_PtPhi(mu_pt[0], mu_phi[0], deepmet_pt_corr_{WS},  deepmet_phi_corr_{WS})"
            .format(WS=wstring))

    varyTTbarQCDScale("mur_1_muf_0p5")
    varyTTbarQCDScale("mur_1_muf_2")
    varyTTbarQCDScale("mur_0p5_muf_1")
    varyTTbarQCDScale("mur_2_muf_1")
    varyTTbarQCDScale("mur_0p5_muf_0p5")
    varyTTbarQCDScale("mur_2_muf_2")

    met_pt_bins = np.array([
        0., 2.0, 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36,
        39, 42, 45, 48, 51, 55, 60, 65, 70, 75, 80, 90, 100, 110, 120, 130,
        140, 150, 160, 170, 180, 190, 200
    ])
    u1_bins = np.array([
        -40., -36., -32., -28., -25., -22.0, -20.0, -18, -16, -14, -12, -10,
        -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
        30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 53, 56, 59, 64, 68, 72, 76,
        80, 85, 90, 100
    ])
    u2_bins = np.array([
        -80., -75., -70., -65., -60., -56., -52, -48, -44, -40, -37, -34, -31,
        -28, -25., -22., -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4,
        6, 8, 10, 12, 16, 18, 20, 22, 25, 28, 31, 34, 37, 40, 44, 48, 52, 56,
        60, 65, 70, 75, 80
    ])
    u_bins = np.array([
        0., 2., 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34,
        36, 38, 40, 43, 46, 49, 52, 56, 60, 64, 68, 72, 76, 80, 85, 90, 95,
        100, 105, 110, 115, 120, 130, 140, 150
    ])

    ymin = 1e3
    ymax = 5e9
    legendPos = [0.92, 0.88, 0.70, 0.62]
    sampMan.cacheDraw(
        "Mu_pt", "h_wjets_mu_pt", 35, 30, 100,
        DrawConfig(xmin=30,
                   xmax=100,
                   xlabel='p_{T}^{#mu} [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))

    sampMan.cacheDraw(
        "met_pt", "h_wjets_met_pt", 50, 0, 150,
        DrawConfig(xmin=0,
                   xmax=150,
                   xlabel='PF MET [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))
    sampMan.cacheDraw(
        "puppimet_pt", "h_wjets_puppimet_pt", 50, 0, 150,
        DrawConfig(xmin=0,
                   xmax=150,
                   xlabel='Puppi MET [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))
    sampMan.cacheDraw(
        "deepmet_pt", "h_wjets_deepmet_pt", 50, 0, 150,
        DrawConfig(xmin=0,
                   xmax=150,
                   xlabel='Deep MET [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))

    sampMan.cacheDraw(
        "met_phi", "h_wjets_met_phi", 20, -ROOT.TMath.Pi(), ROOT.TMath.Pi(),
        DrawConfig(xmin=-ROOT.TMath.Pi(),
                   xmax=ROOT.TMath.Pi(),
                   xlabel='PF MET #phi',
                   ymin=10.,
                   ymax=1e12))
    sampMan.cacheDraw(
        "puppimet_phi", "h_wjets_puppimet_phi", 20, -ROOT.TMath.Pi(),
        ROOT.TMath.Pi(),
        DrawConfig(xmin=-ROOT.TMath.Pi(),
                   xmax=ROOT.TMath.Pi(),
                   xlabel='Puppi MET #phi',
                   ymin=10.,
                   ymax=1e12))
    sampMan.cacheDraw(
        "deepmet_phi", "h_wjets_deepmet_phi", 20, -ROOT.TMath.Pi(),
        ROOT.TMath.Pi(),
        DrawConfig(xmin=-ROOT.TMath.Pi(),
                   xmax=ROOT.TMath.Pi(),
                   xlabel='Deep MET #phi',
                   ymin=10.,
                   ymax=1e12))

    sampMan.cacheDraw(
        "mT_PF", "h_wjets_mT_PF", 70, 0, 140,
        DrawConfig(xmin=0,
                   xmax=140,
                   xlabel='PF m_{T} [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))
    sampMan.cacheDraw(
        "mT_Puppi", "h_wjets_mT_Puppi", 70, 0, 140,
        DrawConfig(xmin=0,
                   xmax=140,
                   xlabel='Puppi m_{T} [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))
    sampMan.cacheDraw(
        "mT_Deep", "h_wjets_mT_Deep", 70, 0, 140,
        DrawConfig(xmin=0,
                   xmax=140,
                   xlabel='Deep m_{T} [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))

    sampMan.cacheDraw(
        "u_reco_pt_PF", "h_wjets_u_reco_pt_PF", 28, 0, 140,
        DrawConfig(xmin=0,
                   xmax=140,
                   xlabel='q_{T} PF [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))
    sampMan.cacheDraw(
        "u_reco_pt_Puppi", "h_wjets_u_reco_pt_Puppi", 28, 0, 140,
        DrawConfig(xmin=0,
                   xmax=140,
                   xlabel='q_{T} Puppi [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))
    sampMan.cacheDraw(
        "u_reco_pt_Deep", "h_wjets_u_reco_pt_Deep", 28, 0, 140,
        DrawConfig(xmin=0,
                   xmax=140,
                   xlabel='Deep q_{T} [GeV]',
                   ymin=ymin,
                   ymax=ymax,
                   legendPos=legendPos))

    # corrected deepmet
    def DrawCorrection(postfix):
        sampMan.cacheDraw(
            "deepmet_pt_corr_" + postfix, "h_wjets_deepmet_pt_corr_" + postfix,
            50, 0, 150,
            DrawConfig(xmin=0,
                       xmax=150,
                       xlabel='Deep MET Corrected [GeV]',
                       ymin=ymin,
                       ymax=ymax,
                       legendPos=legendPos))
        sampMan.cacheDraw(
            "mT_Deep_corr_" + postfix, "h_wjets_mT_Deep_corr_" + postfix, 70,
            0, 140,
            DrawConfig(xmin=0,
                       xmax=140,
                       xlabel='Deep m_{T} Corrected [GeV]',
                       ymin=ymin,
                       ymax=ymax,
                       legendPos=legendPos))
        sampMan.cacheDraw(
            "u_reco_pt_Deep_corr_" + postfix,
            "h_wjets_u_reco_pt_Deep_corr_" + postfix, 28, 0, 140,
            DrawConfig(xmin=0,
                       xmax=140,
                       xlabel='Deep q_{T} Corrected [GeV]',
                       ymin=ymin,
                       ymax=ymax,
                       legendPos=legendPos))

    DrawCorrection("central")
    DrawCorrection("central_MG")
    DrawCorrection("central_GKS")
    DrawCorrection("mur_1_muf_0p5")
    DrawCorrection("mur_1_muf_2")
    DrawCorrection("mur_0p5_muf_1")
    DrawCorrection("mur_2_muf_1")
    DrawCorrection("mur_0p5_muf_0p5")
    DrawCorrection("mur_2_muf_2")

    # vary the QCD Scale in WJets and compare the variations
    def CompareWJetsQCDScale(postfix, fixxsec=True):
        QCDVariations = {
            "central": 0,
            "mur_1_muf_2": 1,
            "mur_1_muf_0p5": 2,
            "mur_2_muf_1": 3,
            "mur_2_muf_2": 4,
            "mur_0p5_muf_1": 6,
            "mur_0p5_muf_0p5": 8
        }
        weightname = "weight_wjetsqcdscale_" + postfix
        if fixxsec:
            # use the pre-calculated weights
            WJetsSamp.Define(weightname, "weight_" + postfix + "_scaled")
        else:
            WJetsSamp.Define(
                weightname, "EventWeights[%d]/EventWeights[0] * weight" %
                QCDVariations[postfix])
        sampMan.DefineAll(weightname, "weight", excludes=['WJets'])

        sampMan.cacheDraw("deepmet_pt",
                          "h_wjets_deepmet_pt_wjetsqcdscale_" + postfix,
                          50,
                          0,
                          150,
                          DrawConfig(xmin=0,
                                     xmax=150,
                                     xlabel='Deep MET [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("mT_Deep",
                          "h_wjets_mT_Deep_wjetsqcdscale_" + postfix,
                          70,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep m_{T} [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("u_reco_pt_Deep",
                          "h_wjets_u_reco_pt_Deep_wjetsqcdscale_" + postfix,
                          28,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep q_{T} [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("deepmet_pt_corr_central",
                          "h_wjets_deepmet_pt_corr_central_wjetsqcdscale_" +
                          postfix,
                          50,
                          0,
                          150,
                          DrawConfig(xmin=0,
                                     xmax=150,
                                     xlabel='Deep MET Corrected [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("mT_Deep_corr_central",
                          "h_wjets_mT_Deep_corr_central_wjetsqcdscale_" +
                          postfix,
                          70,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep m_{T} Corrected [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw(
            "u_reco_pt_Deep_corr_central",
            "h_wjets_u_reco_pt_Deep_corr_central_wjetsqcdscale_" + postfix,
            28,
            0,
            140,
            DrawConfig(xmin=0,
                       xmax=140,
                       xlabel='Deep q_{T} Corrected [GeV]',
                       ymin=ymin,
                       ymax=ymax,
                       legendPos=legendPos),
            weightname=weightname)

    CompareWJetsQCDScale("central")
    CompareWJetsQCDScale("mur_1_muf_2")
    CompareWJetsQCDScale("mur_1_muf_0p5")
    CompareWJetsQCDScale("mur_2_muf_1")
    CompareWJetsQCDScale("mur_2_muf_2")
    CompareWJetsQCDScale("mur_0p5_muf_1")
    CompareWJetsQCDScale("mur_0p5_muf_0p5")

    def CompareWeights(weightname, postfix):
        sampMan.cacheDraw("deepmet_pt",
                          "h_wjets_deepmet_pt_" + postfix,
                          50,
                          0,
                          150,
                          DrawConfig(xmin=0,
                                     xmax=150,
                                     xlabel='Deep MET [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("mT_Deep",
                          "h_wjets_mT_Deep_" + postfix,
                          70,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep m_{T} [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("u_reco_pt_Deep",
                          "h_wjets_u_reco_pt_Deep_" + postfix,
                          28,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep q_{T} [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("deepmet_pt_corr_central",
                          "h_wjets_deepmet_pt_corr_central_" + postfix,
                          50,
                          0,
                          150,
                          DrawConfig(xmin=0,
                                     xmax=150,
                                     xlabel='Deep MET Corrected [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("mT_Deep_corr_central",
                          "h_wjets_mT_Deep_corr_central_" + postfix,
                          70,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep m_{T} Corrected [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)
        sampMan.cacheDraw("u_reco_pt_Deep_corr_central",
                          "h_wjets_u_reco_pt_Deep_corr_central_" + postfix,
                          28,
                          0,
                          140,
                          DrawConfig(xmin=0,
                                     xmax=140,
                                     xlabel='Deep q_{T} Corrected [GeV]',
                                     ymin=ymin,
                                     ymax=ymax,
                                     legendPos=legendPos),
                          weightname=weightname)

    CompareWeights("weight_Iso0", "MuIso0")
    CompareWeights("weight_Iso1", "MuIso1")
    CompareWeights("weight_Iso2", "MuIso2")

    sampMan.launchDraw()

    def compMCRatios(hnames,
                     colors,
                     linestyles,
                     legends,
                     outputtag,
                     ymin=0.8,
                     ymax=1.2):
        # compare MC with MC
        histos_met = [
            THStack2TH1(sampMan.hsmcs["h_wjets_deepmet_pt_{}".format(hname)],
                        "MCRatio") for hname in hnames
        ]
        histos_u_pt = [
            THStack2TH1(
                sampMan.hsmcs["h_wjets_u_reco_pt_Deep_{}".format(hname)],
                "MCRatio") for hname in hnames
        ]
        histos_mt = [
            THStack2TH1(sampMan.hsmcs["h_wjets_mT_Deep_{}".format(hname)],
                        "MCRatio") for hname in hnames
        ]

        histos_met_base = histos_met[0].Clone(histos_met[0].GetName() +
                                              "_Cloned")
        histos_u_pt_base = histos_u_pt[0].Clone(histos_u_pt[0].GetName() +
                                                "_Cloned")
        histos_mt_base = histos_mt[0].Clone(histos_mt[0].GetName() + "_Cloned")

        for hmet, hupt, hmt in zip(histos_met, histos_u_pt, histos_mt):
            hmet.Divide(histos_met_base)
            hmet.SetFillColor(0)
            hupt.Divide(histos_u_pt_base)
            hupt.SetFillColor(0)
            hmt.Divide(histos_mt_base)
            hmt.SetFillColor(0)

        if len(hnames) > 5:
            legendNCols = 2
            legendPos = [0.20, 0.88, 0.70, 0.74]
        elif len(hnames) > 8:
            legendNCols = 3
            legendPos = [0.15, 0.99, 0.92, 0.72]
        else:
            legendNCols = 1
            legendPos = [0.20, 0.88, 0.35, 0.74]

        if "corr" in hnames[0]:
            metlabel = "Deep MET Corrected [GeV]"
            qtlabel = "Deep q_{T} Corrected [GeV]"
            mtlabel = "Deep m_{T} Corrected [GeV]"
        else:
            metlabel = "Deep MET [GeV]"
            qtlabel = "Deep q_{T} [GeV]"
            mtlabel = "Deep m_{T} [GeV]"
        DrawHistos(histos_met,
                   legends,
                   0.,
                   150.,
                   metlabel,
                   ymin,
                   ymax,
                   "Ratio",
                   "h_wjets_MCratio_deepmet_pt_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u_pt,
                   legends,
                   0.,
                   140.,
                   qtlabel,
                   ymin,
                   ymax,
                   "Ratio",
                   "h_wjets_MCratio_u_reco_pt_Deep_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_mt,
                   legends,
                   0.,
                   140.,
                   mtlabel,
                   ymin,
                   ymax,
                   "Ratio",
                   "h_wjets_MCratio_mT_Deep_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)

    colors = [1, 2]
    linestyles = [1, 1]
    legends = ["amc@NLO", "MadGraph"]
    compMCRatios(["corr_central", "corr_central_MG"],
                 colors,
                 linestyles,
                 legends,
                 "MCSample",
                 ymin=0.9,
                 ymax=1.1)

    legends = ["2-Gaussian Fit", "Gaussian Smoothing"]
    compMCRatios(["corr_central", "corr_central_GKS"],
                 colors,
                 linestyles,
                 legends,
                 "FitVSSmoothing",
                 ymin=0.9,
                 ymax=1.1)

    legends = [
        "#mu_{r}=1, #mu_{f}=1", "#mu_{r}=1, #mu_{f}=0.5",
        "#mu_{r}=1, #mu_{f}=2", "#mu_{r}=0.5, #mu_{f}=1",
        "#mu_{r}=2, #mu_{f}=1", "#mu_{r}=0.5, #mu_{f}=0.5",
        "#mu_{r}=2, #mu_{f}=2"
    ]
    postfixs = [
        "corr_central", "corr_mur_1_muf_0p5", "corr_mur_1_muf_2",
        "corr_mur_0p5_muf_1", "corr_mur_2_muf_1", "corr_mur_0p5_muf_0p5",
        "corr_mur_2_muf_2"
    ]
    colors = [1, 2, 3, 4, 5, 6, 7]
    linestyles = [1, 1, 1, 1, 1, 1, 1]
    compMCRatios(postfixs,
                 colors,
                 linestyles,
                 legends,
                 "QCDScale",
                 ymin=0.9,
                 ymax=1.1)

    colors = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    linestyles = [1, 1, 1, 1, 1, 1, 1, 1, 1]
    legends = [
        "central", "MadGraph", "Gauss Smoothing", "#mu_{r}=1, #mu_{f}=0.5",
        "#mu_{r}=1, #mu_{f}=2", "#mu_{r}=0.5, #mu_{f}=1",
        "#mu_{r}=2, #mu_{f}=1", "#mu_{r}=0.5, #mu_{f}=0.5",
        "#mu_{r}=2, #mu_{f}=2"
    ]
    postfixs = [
        "corr_central", "corr_central_MG", "corr_central_GKS",
        "corr_mur_1_muf_0p5", "corr_mur_1_muf_2", "corr_mur_0p5_muf_1",
        "corr_mur_2_muf_1", "corr_mur_0p5_muf_0p5", "corr_mur_2_muf_2"
    ]
    compMCRatios(postfixs,
                 colors,
                 linestyles,
                 legends,
                 "Systematics",
                 ymin=0.9,
                 ymax=1.1)

    colors = [1, 2, 3, 4, 5, 6, 7]
    linestyles = [1, 1, 1, 1, 1, 1, 1]
    legends = [
        "#mu_{r}=1, #mu_{f}=1", "#mu_{r}=1, #mu_{f}=0.5",
        "#mu_{r}=1, #mu_{f}=2", "#mu_{r}=0.5, #mu_{f}=1",
        "#mu_{r}=2, #mu_{f}=1", "#mu_{r}=0.5, #mu_{f}=0.5",
        "#mu_{r}=2, #mu_{f}=2"
    ]
    postfixs = [
        "wjetsqcdscale_central", "wjetsqcdscale_mur_1_muf_0p5",
        "wjetsqcdscale_mur_1_muf_2", "wjetsqcdscale_mur_0p5_muf_1",
        "wjetsqcdscale_mur_2_muf_1", "wjetsqcdscale_mur_0p5_muf_0p5",
        "wjetsqcdscale_mur_2_muf_2"
    ]
    compMCRatios(postfixs, colors, linestyles, legends, "WJetsQCDScale")

    colors = [1, 2, 3, 4, 5, 6, 7]
    linestyles = [1, 1, 1, 1, 1, 1, 1]
    legends = [
        "#mu_{r}=1, #mu_{f}=1", "#mu_{r}=1, #mu_{f}=0.5",
        "#mu_{r}=1, #mu_{f}=2", "#mu_{r}=0.5, #mu_{f}=1",
        "#mu_{r}=2, #mu_{f}=1", "#mu_{r}=0.5, #mu_{f}=0.5",
        "#mu_{r}=2, #mu_{f}=2"
    ]
    postfixs = [
        "corr_central_wjetsqcdscale_central",
        "corr_central_wjetsqcdscale_mur_1_muf_0p5",
        "corr_central_wjetsqcdscale_mur_1_muf_2",
        "corr_central_wjetsqcdscale_mur_0p5_muf_1",
        "corr_central_wjetsqcdscale_mur_2_muf_1",
        "corr_central_wjetsqcdscale_mur_0p5_muf_0p5",
        "corr_central_wjetsqcdscale_mur_2_muf_2"
    ]
    compMCRatios(postfixs, colors, linestyles, legends, "corr_WJetsQCDScale")

    colors = [1, 2, 3]
    linestyles = [1, 1, 1]
    postfixs = [
        "corr_central_MuIso0", "corr_central_MuIso1", "corr_central_MuIso2"
    ]
    legends = ["0.30<I<0.60", "0.30<I<0.45", "0.45<I<0.60"]
    legends = ["From m_{T} < 40", "From m_{T} < 30", "From m_{T} < 50"]
    compMCRatios(postfixs, colors, linestyles, legends, "corr_MuIso")

    return
Esempio n. 5
0
def main():
    print "Program start..."

    #ROOT.gROOT.ProcessLine('TFile* f_zpt = TFile::Open("results/zpt_weight.root")')
    #ROOT.gROOT.ProcessLine('TH1D* h_zpt_ratio  = (TH1D*)f_zpt->Get("h_zpt_ratio")')

    input_data = "inputs/wenu/input_data.txt"
    input_we0 = "inputs/wenu/input_we0.txt"
    input_we1 = "inputs/wenu/input_we1.txt"
    input_we2 = "inputs/wenu/input_we2.txt"
    input_ttbar = "inputs/wenu/input_ttbar_dilepton.txt"
    input_ttbar_1lep = "inputs/wenu/input_ttbar_singlelepton.txt"
    input_ttbar_0lep = "inputs/wenu/input_ttbar_hadronic.txt"
    input_ww = "inputs/wenu/input_ww.txt"
    input_wz = "inputs/wenu/input_wz.txt"
    input_zz = "inputs/wenu/input_zz.txt"
    input_zxx = "inputs/wenu/input_zxx.txt"
    input_wx0 = "inputs/wenu/input_wx0.txt"
    input_wx1 = "inputs/wenu/input_wx1.txt"
    input_wx2 = "inputs/wenu/input_wx2.txt"

    input_antiiso_data = "inputs/awenu/input_data.txt"
    input_antiiso_we0 = "inputs/awenu/input_we0.txt"
    input_antiiso_we1 = "inputs/awenu/input_we1.txt"
    input_antiiso_we2 = "inputs/awenu/input_we2.txt"
    input_antiiso_ttbar = "inputs/awenu/input_ttbar_dilepton.txt"
    input_antiiso_ttbar_1lep = "inputs/awenu/input_ttbar_singlelepton.txt"
    input_antiiso_ttbar_0lep = "inputs/awenu/input_ttbar_hadronic.txt"
    input_antiiso_ww = "inputs/awenu/input_ww.txt"
    input_antiiso_wz = "inputs/awenu/input_wz.txt"
    input_antiiso_zz = "inputs/awenu/input_zz.txt"
    input_antiiso_zxx = "inputs/awenu/input_zxx.txt"
    input_antiiso_wx0 = "inputs/awenu/input_wx0.txt"
    input_antiiso_wx1 = "inputs/awenu/input_wx1.txt"
    input_antiiso_wx2 = "inputs/awenu/input_wx2.txt"

    DataSamp = Sample(input_data,
                      isMC=False,
                      legend="Data",
                      name="Data",
                      isWSR=True)
    # W -> enu
    wjetsnorm = 1.075
    Wm0Samp = Sample(input_we0,
                     isMC=True,
                     name="we0",
                     isWSR=True,
                     additionalnorm=wjetsnorm)
    Wm1Samp = Sample(input_we1,
                     isMC=True,
                     name="we1",
                     isWSR=True,
                     additionalnorm=wjetsnorm)
    Wm2Samp = Sample(input_we2,
                     isMC=True,
                     name="we2",
                     isWSR=True,
                     additionalnorm=wjetsnorm)
    # ttbar
    ttbarnorm = 1.2054
    TTbarSamp = Sample(input_ttbar,
                       isMC=True,
                       name="ttbar_dilepton",
                       isWSR=True,
                       additionalnorm=ttbarnorm)
    TT1LepSamp = Sample(input_ttbar_1lep,
                        isMC=True,
                        name="ttbar_1lepton",
                        isWSR=True,
                        additionalnorm=ttbarnorm)
    TT0LepSamp = Sample(input_ttbar_0lep,
                        isMC=True,
                        name="ttbar_0lepton",
                        isWSR=True,
                        additionalnorm=ttbarnorm)
    ## dibosons
    WWSamp = Sample(input_ww, isMC=True, name="WW", isWSR=True)
    WZSamp = Sample(input_wz, isMC=True, name="WZ", isWSR=True)
    ZZSamp = Sample(input_zz, isMC=True, name="ZZ", isWSR=True)
    # tau
    ZXXSamp = Sample(input_zxx, isMC=True, name="ZXX", isWSR=True)
    Wx0Samp = Sample(input_wx0, isMC=True, name="wx0", isWSR=True)
    Wx1Samp = Sample(input_wx1, isMC=True, name="wx1", isWSR=True)
    Wx2Samp = Sample(input_wx2, isMC=True, name="wx2", isWSR=True)

    ## for the QCD background estimation (data-driven)
    qcdnorm = 16.7254
    DataAisoSamp = Sample(input_antiiso_data,
                          isMC=False,
                          name="Data_aiso",
                          isWSR=True,
                          additionalnorm=qcdnorm,
                          legend='QCD',
                          color='226')
    # W -> enu
    Wm0AisoSamp = Sample(input_antiiso_we0,
                         isMC=True,
                         name="we0_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wm1AisoSamp = Sample(input_antiiso_we1,
                         isMC=True,
                         name="we1_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wm2AisoSamp = Sample(input_antiiso_we2,
                         isMC=True,
                         name="we2_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    # ttbar
    TTbarAisoSamp = Sample(input_antiiso_ttbar,
                           isMC=True,
                           name="ttbar_dilepton_aiso",
                           isWSR=True,
                           additionalnorm=-1.0 * qcdnorm)
    TT1LepAisoSamp = Sample(input_antiiso_ttbar_1lep,
                            isMC=True,
                            name="ttbar_1lepton_aiso",
                            isWSR=True,
                            additionalnorm=-1.0 * qcdnorm)
    TT0LepAisoSamp = Sample(input_antiiso_ttbar_0lep,
                            isMC=True,
                            name="ttbar_0lepton_aiso",
                            isWSR=True,
                            additionalnorm=-1.0 * qcdnorm)
    ## dibosons
    WWAisoSamp = Sample(input_antiiso_ww,
                        isMC=True,
                        name="WW_aiso",
                        isWSR=True,
                        additionalnorm=-1.0 * qcdnorm)
    WZAisoSamp = Sample(input_antiiso_wz,
                        isMC=True,
                        name="WZ_aiso",
                        isWSR=True,
                        additionalnorm=-1.0 * qcdnorm)
    ZZAisoSamp = Sample(input_antiiso_zz,
                        isMC=True,
                        name="ZZ_aiso",
                        isWSR=True,
                        additionalnorm=-1.0 * qcdnorm)
    # tau
    ZXXAisoSamp = Sample(input_antiiso_zxx,
                         isMC=True,
                         name="ZXX_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wx0AisoSamp = Sample(input_antiiso_wx0,
                         isMC=True,
                         name="wx0_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wx1AisoSamp = Sample(input_antiiso_wx1,
                         isMC=True,
                         name="wx1_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)
    Wx2AisoSamp = Sample(input_antiiso_wx2,
                         isMC=True,
                         name="wx2_aiso",
                         isWSR=True,
                         additionalnorm=-1.0 * qcdnorm)

    sampMan = SampleManager(DataSamp, [
        Wm0Samp, Wm1Samp, Wm2Samp, TTbarSamp, TT1LepSamp, TT0LepSamp, WWSamp,
        WZSamp, ZZSamp, ZXXSamp, Wx0Samp, Wx1Samp, Wx2Samp, DataAisoSamp,
        Wm0AisoSamp, Wm1AisoSamp, Wm2AisoSamp, TTbarAisoSamp, TT1LepAisoSamp,
        TT0LepAisoSamp, WWAisoSamp, WZAisoSamp, ZZAisoSamp, ZXXAisoSamp,
        Wx0AisoSamp, Wx1AisoSamp, Wx2AisoSamp
    ])
    #sampMan = SampleManager(DataSamp, [Wm0Samp, Wm1Samp, Wm2Samp, TTbarSamp, TT1LepSamp, TT0LepSamp, WWSamp, WZSamp, ZZSamp, ZXXSamp, Wx0Samp, Wx1Samp, Wx2Samp,
    #        DataAisoSamp])
    sampMan.groupMCs(["WW", "WZ", "ZZ", "ZXX", "wx0", "wx1", "wx2"], "EWK",
                     216, "EWK")
    sampMan.groupMCs(["ttbar_dilepton", "ttbar_1lepton", "ttbar_0lepton"],
                     "ttbar", 96, "t#bar{t}")
    sampMan.groupMCs(['we0', 'we1', 'we2'], "wenu", 92, "W#rightarrow e#nu")
    sampMan.groupMCs([
        'Data_aiso', 'we0_aiso', 'we1_aiso', 'we2_aiso', 'ttbar_dilepton_aiso',
        'ttbar_1lepton_aiso', 'ttbar_0lepton_aiso', 'WW_aiso', 'WZ_aiso',
        'ZZ_aiso', 'ZXX_aiso', 'wx0_aiso', 'wx1_aiso', 'wx2_aiso'
    ], 'QCD', 226, 'QCD')

    sampMan.ApplyCutSpecificMCs('relIso > 0.35 && relIso < 0.45',
                                sampgroupnames=['QCD'])

    #sampMan.DefineAll("zpt", "Z.Pt()")
    #sampMan.DefineAll("zmass", "ZMass")
    #sampMan.DefineAll("zy",  "Z.Rapidity()")
    #sampMan.DefineAll("zeta",  "Z.Rapidity()")
    #sampMan.DefineAll("nPV", "npv")

    sampMan.DefineAll("Muon_pt", "lep.Pt()")
    sampMan.DefineAll("Muon_eta", "lep.Eta()")
    sampMan.DefineMC("met_pt", "metVars[1]", excludes=['Data_aiso'])
    sampMan.DefineMC("met_phi",
                     "TVector2::Phi_mpi_pi(metVarsPhi[1])",
                     excludes=['Data_aiso'])
    # Data does not run any recoil corrections
    DataSamp.Define("met_pt", "metVars[0]")
    DataSamp.Define("met_phi", "TVector2::Phi_mpi_pi(metVarsPhi[0])")
    DataAisoSamp.Define("met_pt", "metVars[0]")
    DataAisoSamp.Define("met_phi", "TVector2::Phi_mpi_pi(metVarsPhi[0])")
    sampMan.DefineAll("met_raw_pt", "metVars[0]")
    sampMan.DefineAll("met_raw_phi", "TVector2::Phi_mpi_pi(metVarsPhi[0])")

    sampMan.DefineAll("weight_wplus",
                      "weight_WoVpt * (q>0)",
                      excludeGroups=['QCD'])
    sampMan.DefineAll("weight_wminus",
                      "weight_WoVpt * (q<0)",
                      excludeGroups=['QCD'])
    sampMan.DefineSpecificMCs("weight_wplus",
                              "weight_WoVpt * (q>0)",
                              sampgroupnames=['QCD'])
    sampMan.DefineSpecificMCs("weight_wminus",
                              "weight_WoVpt * (q<0)",
                              sampgroupnames=['QCD'])

    # deltaPhi
    sampMan.DefineAll("deltaPhi", "TVector2::Phi_0_2pi(lep.Phi() - met_phi)")

    # recoil variables
    sampMan.DefineAll("V2W", "UVec(lep.Pt(), lep.Phi(), met_pt, met_phi)")
    sampMan.DefineAll("WpT", "V2W.Mod()")
    sampMan.DefineAll("Wphi", "TVector2::Phi_mpi_pi(V2W.Phi())")

    met_pt_bins = np.array([
        0., 2.0, 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36,
        39, 42, 45, 48, 51, 55, 60, 65, 70, 75, 80
    ])
    u1_bins = np.array([
        -40., -36., -32., -28., -25., -22.0, -20.0, -18, -16, -14, -12, -10,
        -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
        30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 53, 56, 59, 64, 68, 72, 76,
        80, 85, 90, 100
    ])
    u2_bins = np.array([
        -80., -70., -65., -60., -56., -52, -48, -44, -40, -37, -34, -31, -28,
        -25., -22., -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6,
        8, 10, 12, 16, 18, 20, 22, 25, 28, 31, 34, 37, 40, 44, 48, 52, 56, 60,
        65, 70, 80
    ])
    #u_bins = np.array([0., 2., 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, 52, 56, 60, 64, 68, 72, 76, 80, 85, 90, 95, 100, 105, 110, 115, 120, 130, 140, 150])
    mt_bins = np.concatenate(
        (np.linspace(40, 90, 51), np.linspace(92, 100,
                                              5), np.linspace(104, 120, 5)))
    phimin = -ROOT.TMath.Pi()
    phimax = ROOT.TMath.Pi()
    dphi_bins = np.concatenate((np.linspace(0, 1, 3), np.linspace(
        1.2, 2.4, 7), np.linspace(2.5, 3.5, 11), np.linspace(
            3.6, 4.8, 7), np.linspace(5.0, 6.0, 3))) * ROOT.TMath.Pi() / 3

    eta_bins = np.concatenate(
        (np.linspace(-2.4, -2.0,
                     3), np.linspace(-1.8, 1.8, 37), np.linspace(2.0, 2.4, 3)))
    pt_bins = np.concatenate((np.linspace(25, 35, 6), np.linspace(36, 55, 20),
                              np.linspace(57, 63, 4), np.linspace(66, 70, 2)))
    mass_bins = np.concatenate(
        (np.linspace(70, 76, 3), np.linspace(78, 86,
                                             5), np.linspace(87, 96, 10),
         np.linspace(98, 104, 5), np.linspace(106, 110, 2)))
    u_bins = np.concatenate((np.linspace(0, 20, 11), np.linspace(24, 80, 15),
                             np.linspace(85, 109, 4), np.linspace(120, 150,
                                                                  3)))
    u_bins = np.concatenate((np.linspace(0, 20, 11), np.linspace(24, 60, 10)))

    #sampMan.cacheDraw("zpt",   "histo_wjets_zpt_WoZptWeight_enu", u_bins, DrawConfig(xmin=0, xmax=150, xlabel='p_{T}^{#mu#mu} [GeV]'), weightname="weight_WoVpt")
    #sampMan.cacheDraw("zmass", "histo_wjets_zmass_enu", mass_bins, DrawConfig(xmin=70, xmax=110, xlabel='m_{#mu#mu} [GeV]'))
    #sampMan.cacheDraw("zeta",  "histo_wjets_zeta_enu", eta_bins, DrawConfig(xmin=-2.5, xmax=2.5, xlabel='#eta_{#mu#mu} [GeV]', ymax=1e7, ylabel='Events / 1'))
    #sampMan.cacheDraw("zy",    "histo_wjets_zrapidity_enu", eta_bins, DrawConfig(xmin=-2.5, xmax=2.5, xlabel='y_{#mu#mu} [GeV]', ymax=1e7, ylabel='Events / 1'))

    sf = 0.7

    # nPV
    sampMan.cacheDraw(
        "npv", "histo_nPV_enu", 10, 0, 10,
        DrawConfig(xmin=0,
                   xmax=10,
                   xlabel='# PV',
                   ylabel='Events / 1',
                   dology=False,
                   ymax=9e5 * sf))
    sampMan.cacheDraw("npv",
                      "histo_nPV_eplusnu",
                      10,
                      0,
                      10,
                      DrawConfig(xmin=0,
                                 xmax=10,
                                 xlabel='# PV',
                                 ylabel='Events / 1',
                                 dology=False,
                                 ymax=5e5 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("npv",
                      "histo_nPV_eminusnu",
                      10,
                      0,
                      10,
                      DrawConfig(xmin=0,
                                 xmax=10,
                                 xlabel='# PV',
                                 ylabel='Events / 1',
                                 dology=False,
                                 ymax=4.5e5 * sf),
                      weightname="weight_wminus")
    # lepton pt
    sampMan.cacheDraw(
        "Muon_pt", "histo_Muon_pt_enu", pt_bins,
        DrawConfig(xmin=20,
                   xmax=70,
                   xlabel='p_{T}(e) [GeV]',
                   dology=False,
                   ymax=1.05e5 * sf))
    sampMan.cacheDraw(
        "Muon_eta", "histo_Muon_eta_enu", eta_bins,
        DrawConfig(xmin=-2.6,
                   xmax=2.6,
                   xlabel='#eta (e) [GeV]',
                   ylabel='Events / 1',
                   dology=False,
                   ymax=6e5 * sf))
    sampMan.cacheDraw("Muon_pt",
                      "histo_Muon_pt_eplusnu",
                      pt_bins,
                      DrawConfig(xmin=20,
                                 xmax=70,
                                 xlabel='p_{T}(e) [GeV]',
                                 dology=False,
                                 ymax=6.05e4 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("Muon_eta",
                      "histo_Muon_eta_eplusnu",
                      eta_bins,
                      DrawConfig(xmin=-2.6,
                                 xmax=2.6,
                                 xlabel='#eta (e) [GeV]',
                                 ylabel='Events / 1',
                                 dology=False,
                                 ymax=4e5 * sf),
                      weightname="weight_wminus")
    sampMan.cacheDraw("Muon_pt",
                      "histo_Muon_pt_eminusnu",
                      pt_bins,
                      DrawConfig(xmin=20,
                                 xmax=70,
                                 xlabel='p_{T}(e) [GeV]',
                                 dology=False,
                                 ymax=5.05e4 * sf),
                      weightname="weight_wminus")
    sampMan.cacheDraw("Muon_eta",
                      "histo_Muon_eta_eminusnu",
                      eta_bins,
                      DrawConfig(xmin=-2.6,
                                 xmax=2.6,
                                 xlabel='#eta (e) [GeV]',
                                 ylabel='Events / 1',
                                 dology=False,
                                 ymax=3.5e5 * sf),
                      weightname="weight_wminus")

    # met and raw_met
    sampMan.cacheDraw(
        "met_pt", "histo_wjets_enu_pfmet_pt", met_pt_bins,
        DrawConfig(xmin=0,
                   xmax=80,
                   xlabel='PF MET [GeV]',
                   dology=False,
                   ymax=8e4 * sf))
    sampMan.cacheDraw(
        "met_phi", "histo_wjets_enu_pfmet_phi", 30, phimin, phimax,
        DrawConfig(xmin=phimin,
                   xmax=phimax,
                   xlabel='PF MET #phi',
                   dology=False,
                   ymax=6e5 * sf))
    sampMan.cacheDraw(
        "met_raw_pt", "histo_wjets_enu_pfmet_raw_pt", met_pt_bins,
        DrawConfig(xmin=0,
                   xmax=80,
                   xlabel='Raw PF MET [GeV]',
                   dology=False,
                   ymax=8e4 * sf))
    sampMan.cacheDraw(
        "met_raw_phi", "histo_wjets_enu_pfmet_raw_phi", 30, phimin, phimax,
        DrawConfig(xmin=phimin,
                   xmax=phimax,
                   xlabel='Raw PF MET #phi',
                   dology=False,
                   ymax=6e5 * sf))

    sampMan.cacheDraw("met_pt",
                      "histo_wjets_eplusnu_pfmet_pt",
                      met_pt_bins,
                      DrawConfig(xmin=0,
                                 xmax=80,
                                 xlabel='PF MET [GeV]',
                                 dology=False,
                                 ymax=5e4 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("met_phi",
                      "histo_wjets_eplusnu_pfmet_phi",
                      30,
                      phimin,
                      phimax,
                      DrawConfig(xmin=phimin,
                                 xmax=phimax,
                                 xlabel='PF MET #phi',
                                 dology=False,
                                 ymax=4e5 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("met_raw_pt",
                      "histo_wjets_eplusnu_pfmet_raw_pt",
                      met_pt_bins,
                      DrawConfig(xmin=0,
                                 xmax=80,
                                 xlabel='Raw PF MET [GeV]',
                                 dology=False,
                                 ymax=5e4 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("met_raw_phi",
                      "histo_wjets_eplusnu_pfmet_raw_phi",
                      30,
                      phimin,
                      phimax,
                      DrawConfig(xmin=phimin,
                                 xmax=phimax,
                                 xlabel='Raw PF MET #phi',
                                 dology=False,
                                 ymax=3.5e5 * sf),
                      weightname="weight_wplus")

    sampMan.cacheDraw("met_pt",
                      "histo_wjets_eminusnu_pfmet_pt",
                      met_pt_bins,
                      DrawConfig(xmin=0,
                                 xmax=80,
                                 xlabel='PF MET [GeV]',
                                 dology=False,
                                 ymax=4e4 * sf),
                      weightname="weight_wminus")
    sampMan.cacheDraw("met_phi",
                      "histo_wjets_eminusnu_pfmet_phi",
                      30,
                      phimin,
                      phimax,
                      DrawConfig(xmin=phimin,
                                 xmax=phimax,
                                 xlabel='PF MET #phi',
                                 dology=False,
                                 ymax=3e5 * sf),
                      weightname="weight_wminus")
    sampMan.cacheDraw("met_raw_pt",
                      "histo_wjets_eminusnu_pfmet_raw_pt",
                      met_pt_bins,
                      DrawConfig(xmin=0,
                                 xmax=80,
                                 xlabel='Raw PF MET [GeV]',
                                 dology=False,
                                 ymax=4e4 * sf),
                      weightname="weight_wminus")
    sampMan.cacheDraw("met_raw_phi",
                      "histo_wjets_eminusnu_pfmet_raw_phi",
                      30,
                      phimin,
                      phimax,
                      DrawConfig(xmin=phimin,
                                 xmax=phimax,
                                 xlabel='Raw PF MET #phi',
                                 dology=False,
                                 ymax=3e5 * sf),
                      weightname="weight_wminus")

    # deltaPhi
    sampMan.cacheDraw(
        "deltaPhi", "histo_wjets_enu_deltaphi", dphi_bins,
        DrawConfig(xmin=0.,
                   xmax=2 * phimax,
                   xlabel='#Delta #phi(e, MET)',
                   dology=False,
                   ymax=2e6 * sf))
    sampMan.cacheDraw("deltaPhi",
                      "histo_wjets_eplusnu_deltaphi",
                      dphi_bins,
                      DrawConfig(xmin=0.,
                                 xmax=2 * phimax,
                                 xlabel='#Delta #phi(e, MET)',
                                 dology=False,
                                 ymax=1.4e6 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("deltaPhi",
                      "histo_wjets_eminusnu_deltaphi",
                      dphi_bins,
                      DrawConfig(xmin=0.,
                                 xmax=2 * phimax,
                                 xlabel='#Delta #phi(e, MET)',
                                 dology=False,
                                 ymax=1.4e6 * sf),
                      weightname="weight_wminus")

    # mt
    sampMan.cacheDraw(
        "mtCorr", "histo_wjets_enu_mtcorr", mt_bins,
        DrawConfig(xmin=40,
                   xmax=120,
                   xlabel="m_{T} [GeV]",
                   dology=False,
                   ymax=7e4 * sf))
    sampMan.cacheDraw("mtCorr",
                      "histo_wjets_eplusnu_mtcorr",
                      mt_bins,
                      DrawConfig(xmin=40,
                                 xmax=120,
                                 xlabel="m_{T} [GeV]",
                                 dology=False,
                                 ymax=4e4 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("mtCorr",
                      "histo_wjets_eminusnu_mtcorr",
                      mt_bins,
                      DrawConfig(xmin=40,
                                 xmax=120,
                                 xlabel="m_{T} [GeV]",
                                 dology=False,
                                 ymax=3.5e4 * sf),
                      weightname="weight_wminus")

    # W pt
    sampMan.cacheDraw(
        "WpT", "histo_wjets_enu_wpt", u_bins,
        DrawConfig(xmin=0.,
                   xmax=60,
                   xlabel="p^{W}_{T} [GeV]",
                   dology=False,
                   ymax=1.1e5 * sf))
    sampMan.cacheDraw("WpT",
                      "histo_wjets_eplusnu_wpt",
                      u_bins,
                      DrawConfig(xmin=0.,
                                 xmax=60,
                                 xlabel="p^{W}_{T} [GeV]",
                                 dology=False,
                                 ymax=7e4 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("WpT",
                      "histo_wjets_eminusnu_wpt",
                      u_bins,
                      DrawConfig(xmin=0.,
                                 xmax=60,
                                 xlabel="p^{W}_{T} [GeV]",
                                 dology=False,
                                 ymax=5.0e4 * sf),
                      weightname="weight_wminus")

    sampMan.cacheDraw(
        "Wphi", "histo_wjets_enu_wphi", 30, phimin, phimax,
        DrawConfig(xmin=phimin,
                   xmax=phimax,
                   xlabel="#phi^{W}",
                   dology=False,
                   ymax=6e5 * sf))
    sampMan.cacheDraw("Wphi",
                      "histo_wjets_eplusnu_wphi",
                      30,
                      phimin,
                      phimax,
                      DrawConfig(xmin=phimin,
                                 xmax=phimax,
                                 xlabel="#phi^{W}",
                                 dology=False,
                                 ymax=4e5 * sf),
                      weightname="weight_wplus")
    sampMan.cacheDraw("Wphi",
                      "histo_wjets_eminusnu_wphi",
                      30,
                      phimin,
                      phimax,
                      DrawConfig(xmin=phimin,
                                 xmax=phimax,
                                 xlabel="#phi^{W}",
                                 dology=False,
                                 ymax=3e5 * sf),
                      weightname="weight_wminus")

    sampMan.launchDraw()

    sampMan.dumpCounts()

    print "Program end..."

    raw_input()

    return
Esempio n. 6
0
def main():
    print "Program start..."

    ROOT.gROOT.ProcessLine(
        'TFile* f_zpt = TFile::Open("results/zpt_weight.root")')
    ROOT.gROOT.ProcessLine(
        'TH1D* h_zpt_ratio  = (TH1D*)f_zpt->Get("h_zpt_ratio")')

    if dotest:
        input_data = "inputs/inputs_Z/input_data_test.txt"
        input_dy = "inputs/inputs_Z/input_zjets_test.txt"
        input_ttbar = "inputs/inputs_Z/input_ttbar_test.txt"
    else:
        input_data = "inputs/inputs_Z/input_data.txt"
        input_dy = "inputs/inputs_Z/input_zjets.txt"
        input_ttbar = "inputs/inputs_Z/input_ttbar.txt"
        input_WW2L = "inputs/inputs_Z/input_WWTo2L2Nu.txt"
        input_WZ3L = "inputs/inputs_Z/input_WZTo3LNu.txt"
        input_ZZ2L = "inputs/inputs_Z/input_ZZTo2L2Nu.txt"
        input_ZZ4L = "inputs/inputs_Z/input_ZZTo4L.txt"
        input_lept = "inputs/inputs_Z/input_ttbar_singlelep_fromT.txt"
        input_leptbar = "inputs/inputs_Z/input_ttbar_singlelep_fromTbar.txt"

    DataSamp = Sample(input_data,
                      isMC=False,
                      legend="Data",
                      name="Data",
                      bjetVeto=VetoB)
    DYSamp = Sample(input_dy,
                    xsec=6077.22 * 1e3,
                    color=5,
                    reweightzpt=True,
                    legend="DY",
                    name="DY",
                    bjetVeto=VetoB)
    TTbarSamp = Sample(input_ttbar,
                       xsec=831.76 * 0.105 * 1e3,
                       color=46,
                       reweightzpt=False,
                       legend="t#bar{t}",
                       name="ttbar",
                       bjetVeto=VetoB)
    if not dotest:
        WW2LSamp = Sample(input_WW2L,
                          xsec=12.178 * 1e3,
                          color=38,
                          reweightzpt=False,
                          legend="WW2L",
                          name="WW2L",
                          bjetVeto=VetoB)
        WZ3LSamp = Sample(input_WZ3L,
                          xsec=5.26 * 1e3,
                          color=39,
                          reweightzpt=False,
                          legend="WZ3L",
                          name="WZ3L",
                          bjetVeto=VetoB)
        ZZ2LSamp = Sample(input_ZZ2L,
                          xsec=0.564 * 1e3,
                          color=37,
                          reweightzpt=False,
                          legend="ZZ2L",
                          name="ZZ2L",
                          bjetVeto=VetoB)
        ZZ4LSamp = Sample(input_ZZ4L,
                          xsec=1.212 * 1e3,
                          color=36,
                          reweightzpt=False,
                          legend="ZZ4L",
                          name="ZZ4L",
                          bjetVeto=VetoB)
        #TTLepTSamp = Sample(input_lept,  xsec = 831.76*0.219*1e3, color=47, reweightzpt = False, legend="t#bar{t}", name="ttbarlept", bjetVeto=VetoB)
        #TTLepTbarSamp = Sample(input_leptbar, xsec = 831.76*0.219*1e3, color=48, reweightzpt = False, legend="t#bar{t}", name="ttbarleptb", bjetVeto=VetoB)

    if not dotest:
        sampMan = SampleManager(
            DataSamp,
            [DYSamp, WW2LSamp, WZ3LSamp, TTbarSamp, ZZ2LSamp, ZZ4LSamp])
    else:
        sampMan = SampleManager(DataSamp, [DYSamp, TTbarSamp])
    sampMan.groupMCs(["WW2L", "WZ3L", "ZZ2L", "ZZ4L"], "Dibosons", 38,
                     "Dibosons")

    sampMan.DefineAll("zpt", "Z_pt")

    # define u1 and u2 for PUPPI and PF
    # u1 and u2 for DeepMET has already been defined in SampleManager
    sampMan.DefineAll("Uvec_PF", "UVec(Z_pt, Z_phi, met_pt, met_phi)")
    sampMan.DefineAll("u_PF_pt", "Uvec_PF.Mod()")
    sampMan.DefineAll("u_PF_phi", "Uvec_PF.Phi()")
    sampMan.DefineAll("u1_PF",
                      "u_PF_pt * TMath::Cos(u_PF_phi + TMath::Pi() - Z_phi)")
    sampMan.DefineAll("u2_PF",
                      "u_PF_pt * TMath::Sin(u_PF_phi + TMath::Pi() - Z_phi)")

    sampMan.DefineAll("Uvec_PUPPI",
                      "UVec(Z_pt, Z_phi, puppimet_pt, puppimet_phi)")
    sampMan.DefineAll("u_PUPPI_pt", "Uvec_PUPPI.Mod()")
    sampMan.DefineAll("u_PUPPI_phi", "Uvec_PUPPI.Phi()")
    sampMan.DefineAll(
        "u1_PUPPI",
        "u_PUPPI_pt * TMath::Cos(u_PUPPI_phi + TMath::Pi() - Z_phi)")
    sampMan.DefineAll(
        "u2_PUPPI",
        "u_PUPPI_pt * TMath::Sin(u_PUPPI_phi + TMath::Pi() - Z_phi)")

    # Default correction:
    ROOT.gROOT.ProcessLine(
        'TFile* fitfunctions_Data = TFile::Open("results/Fit/fitfunctions_Data_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TH1F* h1_ptbins_Data_central   = (TH1F*)fitfunctions_Data->Get("h1_ptbins_Data_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TH1F* h1_njetbins_Data_central = (TH1F*)fitfunctions_Data->Get("h1_njetbins_Data_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_Data_u1_njets_pt_central  = (TList*)fitfunctions_Data->Get("tfs_Data_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_Data_u2_njets_pt_central  = (TList*)fitfunctions_Data->Get("tfs_Data_u2_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TFile* fitfunctions_ZJets_NLO_central = TFile::Open("results/Fit/fitfunctions_ZJets_NLO_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_u1_njets_pt_central = (TList*)fitfunctions_ZJets_NLO_central->Get("tfs_ZJets_NLO_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_u2_njets_pt_central = (TList*)fitfunctions_ZJets_NLO_central->Get("tfs_ZJets_NLO_u2_njets_pt_central")'
    )

    DYSamp.Define(
        "u1_corr_central",
        "UCorrection_Quant(u1, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u1_njets_pt_central, tfs_MC_u1_njets_pt_central, 0.00001)"
    )
    DYSamp.Define(
        "u2_corr_central",
        "UCorrection_Quant(u2, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u2_njets_pt_central, tfs_MC_u2_njets_pt_central, 0.00001)"
    )
    DYSamp.Define(
        "u_pt_corr_central",
        "TMath::Sqrt(u1_corr_central*u1_corr_central + u2_corr_central*u2_corr_central)"
    )
    sampMan.DefineAll("u1_corr_central", "u1", excludes=['DY'])
    sampMan.DefineAll("u2_corr_central", "u2", excludes=['DY'])
    sampMan.DefineAll("u_pt_corr_central", "u_pt", excludes=['DY'])
    sampMan.DefineAll("deepmet_corr_central",
                      "METVec(Z_pt, Z_phi, u1_corr_central, u2_corr_central)")
    sampMan.DefineAll("deepmet_pt_corr_central", "deepmet_corr_central.Mod()")
    sampMan.DefineAll("deepmet_phi_corr_central",
                      "TVector2::Phi_mpi_pi(deepmet_corr_central.Phi())")

    # systematic variations
    # corrections from MadGraph
    ROOT.gROOT.ProcessLine(
        'TFile* fitfunctions_ZJets_MG_central = TFile::Open("results/Fit/fitfunctions_ZJets_MG_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_MG_u1_njets_pt_central = (TList*)fitfunctions_ZJets_MG_central->Get("tfs_ZJets_MG_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* tfs_MC_MG_u2_njets_pt_central = (TList*)fitfunctions_ZJets_MG_central->Get("tfs_ZJets_MG_u2_njets_pt_central")'
    )

    DYSamp.Define(
        "u1_corr_central_MG",
        "UCorrection_Quant(u1, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u1_njets_pt_central, tfs_MC_MG_u1_njets_pt_central, 0.00001)"
    )
    DYSamp.Define(
        "u2_corr_central_MG",
        "UCorrection_Quant(u2, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u2_njets_pt_central, tfs_MC_MG_u2_njets_pt_central, 0.00001)"
    )
    DYSamp.Define(
        "u_pt_corr_central_MG",
        "TMath::Sqrt(u1_corr_central_MG*u1_corr_central_MG + u2_corr_central_MG*u2_corr_central_MG)"
    )
    sampMan.DefineAll("u1_corr_central_MG", "u1", excludes=['DY'])
    sampMan.DefineAll("u2_corr_central_MG", "u2", excludes=['DY'])
    sampMan.DefineAll("u_pt_corr_central_MG", "u_pt", excludes=['DY'])
    sampMan.DefineAll(
        "deepmet_corr_central_MG",
        "METVec(Z_pt, Z_phi, u1_corr_central_MG, u2_corr_central_MG)")
    sampMan.DefineAll("deepmet_pt_corr_central_MG",
                      "deepmet_corr_central_MG.Mod()")
    sampMan.DefineAll("deepmet_phi_corr_central_MG",
                      "TVector2::Phi_mpi_pi(deepmet_corr_central_MG.Phi())")

    # corrections from Gaussian Smearing
    ROOT.gROOT.ProcessLine(
        'TFile* gaussSmoother_Data_njets_pt_central = TFile::Open("results/GaussSmoother/gaussSmoother_Data_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_Data_u1_njets_pt_central = (TList*)gaussSmoother_Data_njets_pt_central->Get("cdfs_Data_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_Data_u2_njets_pt_central = (TList*)gaussSmoother_Data_njets_pt_central->Get("cdfs_Data_u2_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TFile* gaussSmoother_ZJets_NLO_njets_pt_central = TFile::Open("results/GaussSmoother/gaussSmoother_ZJets_NLO_njets_pt_central.root")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_ZJets_NLO_u1_njets_pt_central = (TList*)gaussSmoother_ZJets_NLO_njets_pt_central->Get("cdfs_ZJets_NLO_u1_njets_pt_central")'
    )
    ROOT.gROOT.ProcessLine(
        'TList* cdfs_ZJets_NLO_u2_njets_pt_central = (TList*)gaussSmoother_ZJets_NLO_njets_pt_central->Get("cdfs_ZJets_NLO_u2_njets_pt_central")'
    )

    DYSamp.Define(
        "u1_corr_central_GKS",
        "UCorrection_Quant(u1, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, cdfs_Data_u1_njets_pt_central, cdfs_ZJets_NLO_u1_njets_pt_central, 0.00001)"
    )
    DYSamp.Define(
        "u2_corr_central_GKS",
        "UCorrection_Quant(u2, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, cdfs_Data_u2_njets_pt_central, cdfs_ZJets_NLO_u2_njets_pt_central, 0.00001)"
    )
    DYSamp.Define(
        "u_pt_corr_central_GKS",
        "TMath::Sqrt(u1_corr_central_GKS*u1_corr_central_GKS + u2_corr_central_GKS*u2_corr_central_GKS)"
    )
    sampMan.DefineAll("u1_corr_central_GKS", "u1", excludes=['DY'])
    sampMan.DefineAll("u2_corr_central_GKS", "u2", excludes=['DY'])
    sampMan.DefineAll("u_pt_corr_central_GKS", "u_pt", excludes=['DY'])
    sampMan.DefineAll(
        "deepmet_corr_central_GKS",
        "METVec(Z_pt, Z_phi, u1_corr_central_GKS, u2_corr_central_GKS)")
    sampMan.DefineAll("deepmet_pt_corr_central_GKS",
                      "deepmet_corr_central_GKS.Mod()")
    sampMan.DefineAll("deepmet_phi_corr_central_GKS",
                      "TVector2::Phi_mpi_pi(deepmet_corr_central_GKS.Phi())")

    # correction from ttbar qcd scale:
    def varyQCDScale(wstring):
        ROOT.gROOT.ProcessLine(
            'TFile* fitfunctions_Data_{WS} = TFile::Open("results/Fit/fitfunctions_Data_njets_pt_{WS}.root")'
            .format(WS=wstring))
        ROOT.gROOT.ProcessLine(
            'TList* tfs_Data_u1_njets_pt_{WS}  = (TList*)fitfunctions_Data_{WS}->Get("tfs_Data_u1_njets_pt_{WS}")'
            .format(WS=wstring))
        ROOT.gROOT.ProcessLine(
            'TList* tfs_Data_u2_njets_pt_{WS}  = (TList*)fitfunctions_Data_{WS}->Get("tfs_Data_u2_njets_pt_{WS}")'
            .format(WS=wstring))

        DYSamp.Define(
            "u1_corr_{}".format(wstring),
            "UCorrection_Quant(u1, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u1_njets_pt_{WS}, tfs_MC_u1_njets_pt_central, 0.00001)"
            .format(WS=wstring))
        DYSamp.Define(
            "u2_corr_{}".format(wstring),
            "UCorrection_Quant(u2, jet_n, Z_pt, h1_njetbins_Data_central, h1_ptbins_Data_central, tfs_Data_u2_njets_pt_{WS}, tfs_MC_u2_njets_pt_central, 0.00001)"
            .format(WS=wstring))
        DYSamp.Define(
            "u_pt_corr_{}".format(wstring),
            "TMath::Sqrt(u1_corr_{WS}*u1_corr_{WS} + u2_corr_{WS}*u2_corr_{WS})"
            .format(WS=wstring))
        sampMan.DefineAll("u1_corr_{}".format(wstring), "u1", excludes=['DY'])
        sampMan.DefineAll("u2_corr_{}".format(wstring), "u2", excludes=['DY'])
        sampMan.DefineAll("u_pt_corr_{}".format(wstring),
                          "u_pt",
                          excludes=['DY'])
        sampMan.DefineAll(
            "deepmet_corr_{}".format(wstring),
            "METVec(Z_pt, Z_phi, u1_corr_{WS}, u2_corr_{WS})".format(
                WS=wstring))
        sampMan.DefineAll("deepmet_pt_corr_{}".format(wstring),
                          "deepmet_corr_{}.Mod()".format(wstring))
        sampMan.DefineAll(
            "deepmet_phi_corr_{}".format(wstring),
            "TVector2::Phi_mpi_pi(deepmet_corr_{}.Phi())".format(wstring))

    varyQCDScale("mur_1_muf_0p5")
    varyQCDScale("mur_1_muf_2")
    varyQCDScale("mur_0p5_muf_1")
    varyQCDScale("mur_2_muf_1")
    varyQCDScale("mur_0p5_muf_0p5")
    varyQCDScale("mur_2_muf_2")

    met_pt_bins = np.array([
        0., 2.0, 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36,
        39, 42, 45, 48, 51, 55, 60, 65, 70, 75, 80, 90, 100, 110, 120, 135,
        150, 165, 180, 200
    ])
    u1_bins = np.array([
        -40., -36., -32., -28., -25., -22.0, -20.0, -18, -16, -14, -12, -10,
        -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
        30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 53, 56, 59, 64, 68, 72, 76,
        80, 85, 90, 100
    ])
    u2_bins = np.array([
        -80., -70., -65., -60., -56., -52, -48, -44, -40, -37, -34, -31, -28,
        -25., -22., -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6,
        8, 10, 12, 16, 18, 20, 22, 25, 28, 31, 34, 37, 40, 44, 48, 52, 56, 60,
        65, 70, 80
    ])
    u_bins = np.array([
        0., 2., 4., 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34,
        36, 38, 40, 43, 46, 49, 52, 56, 60, 64, 68, 72, 76, 80, 85, 90, 95,
        100, 105, 110, 115, 120, 130, 140, 150
    ])
    phimin = -ROOT.TMath.Pi()
    phimax = ROOT.TMath.Pi()

    # z pt befor and after pt reweighting
    sampMan.cacheDraw("zpt",
                      "histo_zjets_zpt_WoZptWeight",
                      30,
                      0,
                      60,
                      DrawConfig(xmin=0, xmax=60, xlabel='p^{ll}_{T} [GeV]'),
                      weightname="weight_WoVpt")
    sampMan.cacheDraw("zpt", "histo_zjets_zpt", 30, 0, 60,
                      DrawConfig(xmin=0, xmax=60, xlabel='p^{ll}_{T} [GeV]'))

    sampMan.cacheDraw("met_pt", "histo_zjets_pfmet_pt", met_pt_bins,
                      DrawConfig(xmin=0, xmax=200, xlabel='PF MET [GeV]'))
    sampMan.cacheDraw("met_phi", "histo_zjets_pfmet_phi", 30, phimin, phimax,
                      DrawConfig(xmin=phimin, xmax=phimax, xlabel='PF #phi'))
    sampMan.cacheDraw("puppimet_pt", "histo_zjets_puppimet_pt", met_pt_bins,
                      DrawConfig(xmin=0, xmax=200, xlabel='PUPPI MET [GeV]'))
    sampMan.cacheDraw(
        "puppimet_phi", "histo_zjets_puppimet_phi", 30, phimin, phimax,
        DrawConfig(xmin=phimin, xmax=phimax, xlabel='PUPPI #phi'))

    # uncorrected deepmet
    sampMan.cacheDraw("deepmet_pt", "histo_zjets_deepmet_pt", met_pt_bins,
                      DrawConfig(xmin=0, xmax=200, xlabel='Deep MET [GeV]'))
    sampMan.cacheDraw(
        "deepmet_phi", "histo_zjets_deepmet_phi", 30, phimin, phimax,
        DrawConfig(xmin=phimin, xmax=phimax, xlabel='Deep MET #phi'))
    sampMan.cacheDraw(
        "u1", "histo_zjets_u1", u1_bins,
        DrawConfig(xmin=-40.0, xmax=100, xlabel='u_{#parallel} [GeV]'))
    sampMan.cacheDraw(
        "u2", "histo_zjets_u2", u2_bins,
        DrawConfig(xmin=-80., xmax=80., xlabel='u_{#perp} [GeV]'))
    sampMan.cacheDraw("u_pt", "histo_zjets_u_pt", u_bins,
                      DrawConfig(xmin=0, xmax=150, xlabel='u [GeV]'))

    # corrected deepmet
    def DrawCorrection(postfix):
        sampMan.cacheDraw(
            "deepmet_pt_corr_" + postfix,
            "histo_zjets_deepmet_pt_corr_" + postfix, met_pt_bins,
            DrawConfig(xmin=0, xmax=200, xlabel='Deep MET Corrected [GeV]'))
        sampMan.cacheDraw(
            "deepmet_phi_corr_" + postfix,
            "histo_zjets_deepmet_phi_corr_" + postfix, 30, phimin, phimax,
            DrawConfig(xmin=phimin,
                       xmax=phimax,
                       xlabel='Deep MET Corrected #phi'))
        sampMan.cacheDraw(
            "u1_corr_" + postfix, "histo_zjets_u1_corr_" + postfix, u1_bins,
            DrawConfig(xmin=-40.0,
                       xmax=100,
                       xlabel='u_{#parallel} Corrected [GeV]'))
        sampMan.cacheDraw(
            "u2_corr_" + postfix, "histo_zjets_u2_corr_" + postfix, u2_bins,
            DrawConfig(xmin=-80., xmax=80.,
                       xlabel='u_{#perp} Corrected [GeV]'))
        sampMan.cacheDraw(
            "u_pt_corr_" + postfix, "histo_zjets_u_pt_corr_" + postfix, u_bins,
            DrawConfig(xmin=0, xmax=150, xlabel='u Corrected [GeV]'))

    DrawCorrection("central")
    DrawCorrection("central_MG")
    DrawCorrection("central_GKS")
    DrawCorrection("mur_1_muf_0p5")
    DrawCorrection("mur_1_muf_2")
    DrawCorrection("mur_0p5_muf_1")
    DrawCorrection("mur_2_muf_1")
    DrawCorrection("mur_0p5_muf_0p5")
    DrawCorrection("mur_2_muf_2")

    ## sys: corrected deepmet from MG
    #sampMan.cacheDraw("deepmet_pt_corr_central_MG", "histo_zjets_deepmet_pt_corr_central_MG", met_pt_bins, DrawConfig(xmin=0, xmax=200, xlabel='Deep MET Corrected [GeV]'))
    #sampMan.cacheDraw("u1_corr_central_MG", "histo_zjets_u1_corr_central_MG", u1_bins, DrawConfig(xmin=-40.0, xmax=100, xlabel='u_{#parallel} Corrected [GeV]'))
    #sampMan.cacheDraw("u2_corr_central_MG", "histo_zjets_u2_corr_central_MG", u2_bins, DrawConfig(xmin=-80., xmax=80., xlabel='u_{#perp} Corrected [GeV]'))
    #sampMan.cacheDraw("u_pt_corr_central_MG",    "histo_zjets_u_pt_corr_central_MG", u_bins,  DrawConfig(xmin=0, xmax=150, xlabel='u Corrected [GeV]'))

    ## sys: corrected deepmet from Gauss Smearing
    #sampMan.cacheDraw("deepmet_pt_corr_central_GKS", "histo_zjets_deepmet_pt_corr_central_GKS", met_pt_bins, DrawConfig(xmin=0, xmax=200, xlabel='Deep MET Corrected [GeV]'))
    #sampMan.cacheDraw("u1_corr_central_GKS", "histo_zjets_u1_corr_central_GKS", u1_bins, DrawConfig(xmin=-40.0, xmax=100, xlabel='u_{#parallel} Corrected [GeV]'))
    #sampMan.cacheDraw("u2_corr_central_GKS", "histo_zjets_u2_corr_central_GKS", u2_bins, DrawConfig(xmin=-80., xmax=80., xlabel='u_{#perp} Corrected [GeV]'))
    #sampMan.cacheDraw("u_pt_corr_central_GKS",    "histo_zjets_u_pt_corr_central_GKS", u_bins,  DrawConfig(xmin=0, xmax=150, xlabel='u Corrected [GeV]'))

    sampMan.launchDraw()

    # compare some ratio distributions for systematics
    rmin = 0.7
    rmax = 1.3
    colors = [1, 2, 3]
    linestyles = [1, 3, 3]

    def compRatios(hnames, colors, linestyles, legends, outputtag):
        histos_met = [
            sampMan.hratios["histo_zjets_deepmet_pt_{}".format(
                hname)].values()[0] for hname in hnames
        ]
        histos_u_pt = [
            sampMan.hratios["histo_zjets_u_pt_{}".format(hname)].values()[0]
            for hname in hnames
        ]
        histos_u1 = [
            sampMan.hratios["histo_zjets_u1_{}".format(hname)].values()[0]
            for hname in hnames
        ]
        histos_u2 = [
            sampMan.hratios["histo_zjets_u2_{}".format(hname)].values()[0]
            for hname in hnames
        ]

        if len(hnames) > 5:
            legendNCols = 2
            legendPos = [0.20, 0.88, 0.70, 0.74]
        else:
            legendNCols = 1
            legendPos = [0.20, 0.88, 0.35, 0.74]

        DrawHistos(histos_met,
                   legends,
                   0.,
                   150.,
                   "Deep MET Corrected [GeV]",
                   rmin,
                   rmax,
                   "Data / MC",
                   "histo_zjets_ratio_deepmet_pt_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u_pt,
                   legends,
                   0.,
                   150.,
                   "u Corrected [GeV]",
                   0.7,
                   1.3,
                   "Data / MC",
                   "histo_zjets_ratio_u_pt_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u1,
                   legends,
                   -40.0,
                   100.,
                   "u_{#parallel} Corrected [GeV]",
                   0.7,
                   1.3,
                   "Data / MC",
                   "histo_zjets_ratio_u1_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u2,
                   legends,
                   -80.0,
                   80.,
                   "u_{#perp} Corrected [GeV]",
                   0.7,
                   1.3,
                   "Data / MC",
                   "histo_zjets_ratio_u2_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)

    def compMCRatios(hnames, colors, linestyles, legends, outputtag):
        # compare MC with MC
        histos_met = [
            THStack2TH1(
                sampMan.hsmcs["histo_zjets_deepmet_pt_{}".format(hname)],
                "MCRatio") for hname in hnames
        ]
        histos_u_pt = [
            THStack2TH1(sampMan.hsmcs["histo_zjets_u_pt_{}".format(hname)],
                        "MCRatio") for hname in hnames
        ]
        histos_u1 = [
            THStack2TH1(sampMan.hsmcs["histo_zjets_u1_{}".format(hname)],
                        "MCRatio") for hname in hnames
        ]
        histos_u2 = [
            THStack2TH1(sampMan.hsmcs["histo_zjets_u2_{}".format(hname)],
                        "MCRatio") for hname in hnames
        ]

        histos_met_base = histos_met[0].Clone(histos_met[0].GetName() +
                                              "_Cloned")
        histos_u_pt_base = histos_u_pt[0].Clone(histos_u_pt[0].GetName() +
                                                "_Cloned")
        histos_u1_base = histos_u1[0].Clone(histos_u1[0].GetName() + "_Cloned")
        histos_u2_base = histos_u2[0].Clone(histos_u2[0].GetName() + "_Cloned")

        for hmet, hupt, hu1, hu2 in zip(histos_met, histos_u_pt, histos_u1,
                                        histos_u2):
            hmet.Divide(histos_met_base)
            hmet.SetFillColor(0)
            hupt.Divide(histos_u_pt_base)
            hupt.SetFillColor(0)
            hu1.Divide(histos_u1_base)
            hu1.SetFillColor(0)
            hu2.Divide(histos_u2_base)
            hu2.SetFillColor(0)

        if len(hnames) > 5:
            legendNCols = 2
            legendPos = [0.20, 0.88, 0.70, 0.74]
        else:
            legendNCols = 1
            legendPos = [0.20, 0.88, 0.35, 0.74]

        DrawHistos(histos_met,
                   legends,
                   0.,
                   150.,
                   "Deep MET Corrected [GeV]",
                   rmin,
                   rmax,
                   "Ratio",
                   "histo_zjets_MCratio_deepmet_pt_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u_pt,
                   legends,
                   0.,
                   150.,
                   "u Corrected [GeV]",
                   0.7,
                   1.3,
                   "Ratio",
                   "histo_zjets_MCratio_u_pt_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u1,
                   legends,
                   -40.0,
                   100.,
                   "u_{#parallel} Corrected [GeV]",
                   0.7,
                   1.3,
                   "Ratio",
                   "histo_zjets_MCratio_u1_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)
        DrawHistos(histos_u2,
                   legends,
                   -80.0,
                   80.,
                   "u_{#perp} Corrected [GeV]",
                   0.7,
                   1.3,
                   "Ratio",
                   "histo_zjets_MCratio_u2_{}".format(outputtag),
                   dology=False,
                   drawashist=True,
                   mycolors=colors,
                   linestyles=linestyles,
                   legendPos=legendPos,
                   legendNCols=legendNCols)

    colors = [1, 2]
    linestyles = [1, 1]
    legends = ["amc@NLO", "MadGraph"]
    compRatios(["corr_central", "corr_central_MG"], colors, linestyles,
               legends, "MCSample")
    compMCRatios(["corr_central", "corr_central_MG"], colors, linestyles,
                 legends, "MCSample")

    legends = ["2-Gaussian Fit", "Gaussian Smoothing"]
    compRatios(["corr_central", "corr_central_GKS"], colors, linestyles,
               legends, "FitVSSmoothing")
    compMCRatios(["corr_central", "corr_central_GKS"], colors, linestyles,
                 legends, "FitVSSmoothing")

    legends = [
        "#mu_{r}=1, #mu_{f}=1", "#mu_{r}=1, #mu_{f}=0.5",
        "#mu_{r}=1, #mu_{f}=2", "#mu_{r}=0.5, #mu_{f}=1",
        "#mu_{r}=2, #mu_{f}=1", "#mu_{r}=0.5, #mu_{f}=0.5",
        "#mu_{r}=2, #mu_{f}=2"
    ]
    postfixs = [
        "corr_central", "corr_mur_1_muf_0p5", "corr_mur_1_muf_2",
        "corr_mur_0p5_muf_1", "corr_mur_2_muf_1", "corr_mur_0p5_muf_0p5",
        "corr_mur_2_muf_2"
    ]
    colors = [1, 2, 3, 4, 5, 6, 7]
    linestyles = [1, 1, 1, 1, 1, 1, 1]
    compRatios(postfixs, colors, linestyles, legends, "QCDScale")
    compMCRatios(postfixs, colors, linestyles, legends, "QCSScale")

    doDump = False
    if doDump:
        print "Dump corrections into root file..."
        branchList = ROOT.vector('string')()
        for branchName in [
                "zpt", "deepmet_pt", "u1", "u2", "u1_corr_njets_3Gauss",
                "u2_corr_njets_3Gauss", "u_pt_corr_njets_3Gauss",
                "deepmet_pt_corr_njets_3Gauss", "weight", "weight_WoVpt",
                "vtx_n", "u1_PF", "u2_PF", "u1_PUPPI", "u2_PUPPI",
                "jet_CSVLoose_n"
        ]:
            branchList.push_back(branchName)
        #TTbarSamp.rdf.Snapshot("DeepMETCorr_TTbar", "results/deepmetcorr_TTbar.root", branchList)
        DYSamp.rdf.Snapshot("DeepMETCorr_DY", "results/deepmetcorr_DY.root",
                            branchList)

        branchList = ROOT.vector('string')()
        for branchName in [
                "zpt", "deepmet_pt", "u1", "u2", "weight", "weight_WoVpt",
                "vtx_n", "u1_PF", "u2_PF", "u1_PUPPI", "u2_PUPPI",
                "jet_CSVLoose_n"
        ]:
            branchList.push_back(branchName)
        DataSamp.rdf.Snapshot("DeepMETCorr_Data",
                              "results/deepmetcorr_Data.root", branchList)

    print "Program end..."

    raw_input()

    return