def make_pdf_histos(var, weight, samples, sn, sampn, cuts, cuts_antiiso, outdir, channel, coupling, binning=None, plot_range=None, asymmetry=None, mtmetcut=None):
    if sn == "qcd" and coupling == "powheg":
        hname = "%s__%s__%s__%s" % (var, sn, "pdf", "up")
        write_histogram(var, hname, str(weight), samples, sn, sampn, cuts, cuts_antiiso, outdir, channel, coupling, binning=binning, plot_range=plot_range, asymmetry=asymmetry, mtmetcut=mtmetcut)
        hname = "%s__%s__%s__%s" % (var, sn, "pdf", "down")
        write_histogram(var, hname, str(weight), samples, sn, sampn, cuts, cuts_antiiso, outdir, channel, coupling, binning=binning, plot_range=plot_range, asymmetry=asymmetry, mtmetcut=mtmetcut)
        return
    if sampn.startswith("Single") or coupling != "powheg":
        return
    nPDFSet_size = 44
    weight_str = str(weight)
    samp = samples[sampn]
    hname_up = "%s__%s__pdf__up" % (var, sn)
    hname_down = "%s__%s__pdf__down" % (var, sn)
    #outfile = File(outdir + "/%s_%s.root" % (sampn,hname), "RECREATE")
    outfile = TFile(outdir + "/%s_%s_pdf.root" % (sampn, var), "RECREATE")
    if sn=="DATA":
        weight_str = "1"
    if var == "eta_lj":
        var = "abs("+var+")"
    
    hist_orig = create_histogram_for_fit(sn, samp, str(weight), cuts, cuts_antiiso, channel, coupling, var, binning=binning, plot_range=plot_range, asymmetry=asymmetry, qcd_extra=None, mtmetcut=mtmetcut)
    hist_std = create_histogram_for_fit(sn, samp, weight_str, cuts, cuts_antiiso, channel, coupling, var, binning=binning, plot_range=plot_range, asymmetry=asymmetry, qcd_extra=None, mtmetcut=mtmetcut)

    hist_plus = hist_orig.Clone(hname_up)
    hist_minus = hist_orig.Clone(hname_down)
    print sn, samp
    weighted_histos = []    
    for i in range(nPDFSet_size):
        #print "pdf nr = ", i
        #weight_str = str(weight * Weights.pdf_refweight * Weight("pdf_weights_MSTW2008nlo68cl["+str(i)+"]"))
        #weight_str = str(weight * Weights.pdf_refweight * Weight("pdf_weights_CT10.pdf_weights_CT10["+str(i)+"]"))
        weight_str = str(weight * Weight("pdf_weights_cteq66["+str(i)+"]"))
        hist = create_histogram_for_fit(sn, samp, weight_str, cuts, cuts_antiiso, channel, coupling, var, binning=binning, plot_range=plot_range, asymmetry=asymmetry, qcd_extra=None, mtmetcut=mtmetcut)
        hist.SetDirectory(0)
        weighted_histos.append(hist)
    
    outfile.cd() #Must cd after histogram creation

    (hist_plus, hist_minus) = calculate_PDF_uncertainties(hist_std, weighted_histos, hist_plus, hist_minus, orig=hist_orig)
    #hist_std.Write()
    hist_plus.Write()
    hist_minus.Write()

    
    #Write histogram to file
    #logging.info("Writing histogram %s to file %s" % (hist.GetName(), outfile.GetPath()))
    #logging.info("%i entries, %.2f events" % (hist.GetEntries(), hist.Integral()))
    
    #(a,b) = hist.GetName().split("_")[0], hist.GetName().split("_")[1]
    #print "YIELD", a+"_"+b, hist.Integral()
    #hist.SetName(hname)
    #hist.SetDirectory(outfile)
    outfile.Write()
    outfile.Close()
    samples = None
def write_histogram(var, hname, weight, samples, sn, sampn, cuts, cuts_antiiso, outdir, channel, coupling, binning=None, plot_range=None, asymmetry=None, mtmetcut=None):
    weight_str = weight
    samp = samples[sampn]
    outfile = TFile(outdir + "/%s_%s.root" % (sampn,hname), "RECREATE")
    if sn=="DATA":
        weight_str = "1"
    if var == "eta_lj":
        var = "abs("+var+")"
    qcd_extra = None
    
    #This is a really ugly way of adding the QCD shape variation, but works. Restructure the whole thing in the future
    if "iso__down" in hname:
        if var == "abs(eta_lj)":
            cuts_antiiso = str(Cuts.eta_fit_antiiso_down(channel))
            qcd_extra = str(Cuts.eta_fit_antiiso(channel)) #hack for now
        else:
            cut = "1"
            for x in str(cuts_antiiso).split("&&"):
                if "mva" in x:
                    cut = x                
            cut = cut.replace("(","").replace(")","")
            cuts_antiiso = str(Cuts.mva_antiiso_down(channel, mva_var=var)) + " && ("+cut+")"
            qcd_extra = str(Cuts.mva_antiiso(channel, mva_var=var)) + " && ("+cut+")" #hack for now
    elif "iso__up" in hname:
        if var == "abs(eta_lj)":
            cuts_antiiso = str(Cuts.eta_fit_antiiso_up(channel))
            qcd_extra = str(Cuts.eta_fit_antiiso(channel)) #hack for now
        else:
            cut = "1"
            for x in str(cuts_antiiso).split("&&"):
                if "mva" in x:
                    cut = x
            cut = cut.replace("(","").replace(")","")
            cuts_antiiso = str(Cuts.mva_antiiso_up(channel, mva_var=var)) + " && ("+cut+")"
            qcd_extra = str(Cuts.mva_antiiso(channel, mva_var=var)) + " && ("+cut+")" #hack for now
    hist = create_histogram_for_fit(sn, samp, weight_str, cuts, cuts_antiiso, channel, coupling, var, binning=binning, plot_range=plot_range, asymmetry=asymmetry, qcd_extra=qcd_extra, mtmetcut=mtmetcut, hname=hname)
    outfile.cd() #Must cd after histogram creation

    #Write histogram to file
    #logging.info("Writing histogram %s to file %s" % (hist.GetName(), outfile.GetPath()))
    #logging.info("%i entries, %.2f events" % (hist.GetEntries(), hist.Integral()))
    
    #(a,b) = hist.GetName().split("_")[0], hist.GetName().split("_")[1]
    #print "YIELD", a+"_"+b, hist.Integral()
    hist.SetName(hname)
    hist.SetDirectory(outfile)
    outfile.Write()
    outfile.Close()
    samples = None