Example #1
0
def profile_plots(pt_array, ntrk_array, weight, gluon_pt_array,
                  gluon_ntrk_array, gluon_weight_array, quark_pt_array,
                  quark_ntrk_array, quark_weight_array, islogpt):
    #gStyle.SetOptFit()
    print 'total events: ' + str(len(pt_array))
    print 'gluons: ' + str(len(gluon_pt_array))
    print 'quarks: ' + str(len(quark_pt_array))
    c1 = TCanvas('c1', '', 200, 10, 700, 500)
    fittype = 'pol1'
    if islogpt:
        ptmin = 4
        ptmax = 7
        nbins = 10
    else:
        ptmin = 0
        ptmax = 1300
        nbins = 10
    hquark = TProfile('hquark', 'Quark Profile of nTrk vs ln(pt)', nbins,
                      ptmin, ptmax, ntrkmin, ntrkmax)
    weighted_hquark = TProfile('weighted_hquark',
                               'Quark Weighted Profile of nTrk vs ln(pt)',
                               nbins, ptmin, ptmax, ntrkmin, ntrkmax)
    count = 0
    for i, j, k in zip(quark_pt_array, quark_ntrk_array, quark_weight_array):
        count += 1
        if count % 100000 == 0:
            print 'processed ' + str(count)
        hquark.Fill(i, j)
        weighted_hquark.Fill(i, j, k)

    draw_hist_with_tag_eff(hquark, "quark", quark_pt_array, quark_ntrk_array,
                           gluon_pt_array, gluon_ntrk_array)
    draw_hist_with_tag_eff(weighted_hquark, "weightedquark", quark_pt_array,
                           quark_ntrk_array, gluon_pt_array, gluon_ntrk_array)

    hgluon = TProfile('hgluon', 'Gluon Profile of nTrk vs ln(pt)', nbins,
                      ptmin, ptmax, ntrkmin, ntrkmax)
    weighted_hgluon = TProfile('weighted_hgluon',
                               'Gluon Weighted Profile of nTrk vs ln(pt)',
                               nbins, ptmin, ptmax, ntrkmin, ntrkmax)
    for i, j, k in zip(gluon_pt_array, gluon_ntrk_array, gluon_weight_array):
        hgluon.Fill(i, j)
        weighted_hgluon.Fill(i, j, k)

    draw_hist_with_tag_eff(hgluon, "gluon", quark_pt_array, quark_ntrk_array,
                           gluon_pt_array, gluon_ntrk_array)
    draw_hist_with_tag_eff(weighted_hgluon, "weightedgluon", quark_pt_array,
                           quark_ntrk_array, gluon_pt_array, gluon_ntrk_array)

    c3 = TCanvas('c3', '', 200, 10, 700, 500)
    weighted_hquark.SetMaximum(30)
    weighted_hquark.Draw("HIST")
    weighted_hgluon.Draw("HIST SAME")
    weighted_hquark.SetLineColor(2)

    c3.SaveAs(settings.outputdir + '/overlay.pdf')
Example #2
0
def plotPulls(optunf="Bayes",
              ntest=10,
              leff=True,
              loufl=False,
              optfun="exp",
              opttfun="",
              gmean=-1.0,
              nrebin=4):

    if opttfun == "":
        opttfun = optfun

    if optfun == "blobel":
        gmean = 0.0

    funttxt, funtxt = funtxts(opttfun, optfun, leff, loufl)

    global histos, canv, canv2
    histos = []

    canv = TCanvas("canv", "thruth vs reco pulls", 600, 800)
    canv.Divide(1, 3)
    canv2 = TCanvas("canv2", "P(chi^2)", 600, 800)
    canv2.Divide(2, 3)

    if optunf == "BasisSplines":
        bininfo = BinInfo(nrebin)
        unfoldtester = UnfoldTester(optunf, nrebin)
    else:
        bininfo = BinInfo()
        unfoldtester = UnfoldTester(optunf)
    trainer = Trainer(bininfo, opttfun, optfun)
    tester = Tester(bininfo, optfun)
    hbininfo = bininfo.create(optfun)
    dx = hbininfo["mhi"]
    for sigma, ipad in [[0.01 * dx, 1], [0.03 * dx, 2], [0.1 * dx, 3]]:
        txt = optunf + ", smear mu, s.d.= " + str(gmean) + ", " + str(
            sigma) + ", train: " + funttxt + ", test: " + funtxt + ", " + str(
                ntest) + " tests"
        hPulls = TProfile("pulls", txt, hbininfo["tbins"], hbininfo["tlo"],
                          hbininfo["thi"])
        hPulls.SetErrorOption("s")
        hPulls.SetYTitle("Thruth reco pull")
        histos.append(hPulls)
        hChisq = TH1D("chisq", "P(chi^2) rec " + txt, 10, 0.0, 1.0)
        hChisqm = TH1D("chisqm", "P(chi^2) mea " + txt, 10, 0.0, 1.0)
        histos.append(hChisq)
        histos.append(hChisqm)
        measurement = createMeasurement(gmean, sigma, leff, optfun)
        response = trainer.train(measurement, loufl=loufl)
        for itest in range(ntest):
            print "Test", itest
            unfold, hTrue, hMeas = unfoldtester.rununfoldtest(
                tester, measurement, response)
            unfold.PrintTable(cout, hTrue, 2)
            hReco = unfold.Hreco(2)
            nbin = hReco.GetNbinsX()
            if hbininfo["nrebin"] > 1:
                hTrue = hTrue.Rebin(nrebin)
            for ibin in range(nbin + 1):
                truevalue = hTrue.GetBinContent(ibin)
                recvalue = hReco.GetBinContent(ibin)
                error = hReco.GetBinError(ibin)
                if error > 0.0:
                    pull = (recvalue - truevalue) / error
                    hPulls.Fill(hReco.GetBinCenter(ibin), pull)
            chisq = unfold.Chi2(hTrue, 2)
            hChisq.Fill(TMath.Prob(chisq, hTrue.GetNbinsX()))
            chisqm = unfold.Chi2measured()
            pchisqm = TMath.Prob(chisqm, hMeas.GetNbinsX() - hReco.GetNbinsX())
            print "Chisq measured=", chisqm, "P(chi^2)=", pchisqm
            hChisqm.Fill(pchisqm)

        canv.cd(ipad)
        gStyle.SetErrorX(0)
        hPulls.SetMinimum(-3.0)
        hPulls.SetMaximum(3.0)
        hPulls.SetMarkerSize(1.0)
        hPulls.SetMarkerStyle(20)
        hPulls.SetStats(False)
        hPulls.Draw()
        canv2.cd(ipad * 2 - 1)
        hChisq.Draw()
        canv2.cd(ipad * 2)
        hChisqm.Draw()

    fname = "RooUnfoldTestPulls_" + optunf + "_" + opttfun + "_" + optfun
    if loufl:
        fname += "_oufl"
    canv.Print(fname + ".pdf")
    fname = "RooUnfoldTestChisq_" + optunf + "_" + opttfun + "_" + optfun
    if loufl:
        fname += "_oufl"
    canv2.Print(fname + ".pdf")

    return
    if ievent % 100 == 0:
        print("Processed %d of %d events..." % (ievent, NEntries))

    ntuple.GetEntry(ievent)

    n_weight = ntuple.__getattr__('weight')
    n_ptPruned = ntuple.__getattr__('ptPruned')
    if (170 < n_ptPruned < 1000):
        x.append(n_ptPruned)
        y.append(n_weight)
        print(n_weight)
        hprof2d.Fill(n_ptPruned, n_weight)

# hprof2d.LabelsOption("h","pT")
hprof2d.SetMinimum(-0.2)
hprof2d.SetMaximum(0.3)
hprof2d.Draw()

c.SaveAs("old2dhist.png")

# # H = verification
# nbins = 100
# # xedges = 101
# # yedges =4
# H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
#
# # print (xedges)
# print (yedges)
# # H= H.T
# # print (H)
# # verification = H