示例#1
0
messycleanhist = gDirectory.Get("messyclean")
c.Print(outfilename + ".pdf")

c.SetLogz(0)

effhist = messyhist.Clone("eff")
effhist.Divide(cleanhist)

effhistnopickup = messycleanhist.Clone("effnopickup")
effhistnopickup.Sumw2()
#effhist = messyhist.Clone("eff")
effhistnopickup.Divide(cleanhist)
effhist.GetZaxis().SetRangeUser(0, 1.0)

eff = TEfficiency(messycleanhist, cleanhist)
eff.SetName("teff")
eff.Write()

outfile.Write()

effhist.Draw("surf")
c.Print(outfilename + ".pdf")

effhist.Draw("colz")
c.Print(outfilename + ".pdf")

effhistnopickup.Draw("surf")
c.Print(outfilename + ".pdf")

effhistnopickup.Draw("colz")
c.Print(outfilename + ".pdf")
示例#2
0
文件: efficiency.py 项目: meeg/DPAna
def make_effplots():
    hitcut = "abs(({0})-({1}))<2".format(bar_var,barexp_var) # require hit in the extrapolated bar or the adjacent bar

    events.Draw("{0}:{1}>>hexp2d({2},{3})".format(y_var,x_var,x_binning,y_binning),"","colz");
    c.Print(outfilename+".pdf");

    events.Draw("{0}:{1}>>hexp2d_withhit({2},{3})".format(y_var,x_var,x_binning,y_binning),hitcut,"colz");
    c.Print(outfilename+".pdf");

    numerator = gDirectory.Get("hexp2d_withhit")
    denominator = gDirectory.Get("hexp2d")
    #numerator.Sumw2();
    numerator.Divide(denominator)
    numerator.GetZaxis().SetRangeUser(0,1)
    numerator.SetTitle("efficiency vs. position, {0}, no quad cut;extrapolated x [cm];extrapolated y [cm]".format(station))
    numerator.Draw("colz");
    c.Print(outfilename+".pdf");
    for quad in range(0,4):
        quadcut = "quad=={0}".format(quad)
        flipped_x = x_var+"*({0})".format(1 if quad%2==0 else -1) # extrapolated X flipped such that the fiducial area is on the positive side
        flipped_y = y_var+"*({0})".format(1 if quad<2 else -1) # extrapolated Y flipped such that the fiducial area is on the positive side
        fiducialcut_x = "{0}>{1}".format(flipped_x,xedge[quad])
        fiducialcuts = "{0} && {1}>{2}".format(fiducialcut_x,flipped_y,yedge[quad])

        '''
        events.Draw("fElementID_exp-1>>hhits_exp(40,-0.5,39.5)",quadcut,"");
        events.Draw("fElementID_exp-1>>hhits(40,-0.5,39.5)",quadcut+" && "+hitcut,"");
        gDirectory.Get("hhits").Sumw2();
        gDirectory.Get("hhits").Divide(gDirectory.Get("hhits_exp"));
        gDirectory.Get("hhits").Draw();
        c.Print(outfilename+".pdf");
        '''

        events.Draw(barexp_var+">>hhits_exp_{0}_quad{1}({2})".format(station,quad,bar_binning)," && ".join([quadcut,fiducialcuts]),"");
        events.Draw(barexp_var+">>hhits_{0}_quad{1}({2})".format(station,quad,bar_binning)," && ".join([quadcut,fiducialcuts,hitcut]),"");
        numerator = gDirectory.Get("hhits_{0}_quad{1}".format(station,quad))
        denominator = gDirectory.Get("hhits_exp_{0}_quad{1}".format(station,quad))
        eff = TEfficiency(numerator,denominator)
        eff.SetName("eff_{0}_quad{1}".format(station,quad))
        #eff.GetYaxis().SetRangeUser(0,1.1)
        eff.SetTitle("efficiency vs. bar, {0}, quad {1} ({2});extrapolated bar ID;efficiency".format(station,quad,quadnames[quad]))
        eff.Write()
        eff.Draw()
        gPad.Update()
        eff.GetPaintedGraph().SetMinimum(0)
        eff.GetPaintedGraph().SetMaximum(1.1)
        #numerator.Sumw2();
        #numerator.Divide(denominator)
        #numerator.GetYaxis().SetRangeUser(0,1.1)
        #numerator.SetTitle("efficiency vs. bar, {0}, quad {1};extrapolated bar ID;efficiency".format(station,quad))
        #numerator.Draw();
        c.Print(outfilename+".pdf");

        events.Draw(bar_var+">>hhits2_{0}_quad{1}({2})".format(station,quad,bar_binning)," && ".join([quadcut,fiducialcuts,hitcut]),"");
        numerator = gDirectory.Get("hhits2_{0}_quad{1}".format(station,quad))
        denominator = gDirectory.Get("hhits_exp_{0}_quad{1}".format(station,quad))
        numerator.Sumw2();
        numerator.Divide(denominator)
        numerator.GetYaxis().SetRangeUser(0,1.1)
        numerator.SetTitle("efficiency vs. bar, {0}, quad {1} ({2});hit bar ID;efficiency".format(station,quad,quadnames[quad]))
        numerator.Draw();
        c.Print(outfilename+".pdf");

        '''
        leg = TLegend(0.0,0.75,0.2,0.9)
        for xslice in range(0,10):
            expslicehistname = "hhits_exp_{0}".format(xslice)
            events.Draw("fElementID_exp>>{0}(40,-0.5,39.5)".format(expslicehistname),"quad=={0}&&abs(tx*797+x0-{1})<5".format(quad,xslice*10-45),"");
            slicehistname = "hhits_{0}".format(xslice)
            #print(slicehistname)
            events.Draw("fElementID>>{0}(40,-0.5,39.5)".format(slicehistname),"quad=={0} && abs(fElementID-fElementID_exp+1)<2&&abs(tx*797+x0-{1})<5".format(quad,xslice*10-45),"");
            #print("fElementID>>hhits(40,-0.5,39.5)","quad=={0} && abs(fElementID-fElementID_exp+1)<2&&abs(tx*797+x0-{1})<5".format(quad,xslice*10-45,slicehistname))
            #print("fElementID>>{0}(40,-0.5,39.5)","quad=={0} && abs(fElementID-fElementID_exp+1)<2&&abs(tx*797+x0-{1})<5".format(quad,xslice*10-45,slicehistname))
            #events.Draw("fElementID>>{2}(40,-0.5,39.5)","quad=={0} && abs(fElementID-fElementID_exp+1)<2&&abs(tx*797+x0-{1})<5".format(quad,xslice*10-45,slicehistname),"");
            #gDirectory.ls()
            hist = gDirectory.Get(slicehistname)
            gDirectory.Get(slicehistname).Sumw2();
            gDirectory.Get(slicehistname).Divide(gDirectory.Get(expslicehistname));
            gDirectory.Get(slicehistname).Draw();
            hist.SetLineColor(xslice+1)
            leg.AddEntry(hist,"abs(tx*797+x0-{1})<5".format(quad,xslice*10-45))
            if (xslice==0):
                gDirectory.Get(slicehistname).GetYaxis().SetRangeUser(0,1);
                hs = THStack("hs",gDirectory.Get(slicehistname).GetTitle());
                gDirectory.Get(slicehistname).DrawClone();
            else:
                gDirectory.Get(slicehistname).DrawClone("same");
            hs.Add(gDirectory.Get(slicehistname))
            #c.Print(outfilename+".pdf");
        hs.Draw("nostack")
        leg.Draw()
        c.Print(outfilename+".pdf");
        '''

        events.Draw(flipped_y+">>hy_exp({0})".format(flipped_y_binning)," && ".join([quadcut,fiducialcut_x]),"");
        #c.Print(outfilename+".pdf");
        events.Draw(flipped_y+">>hy({0})".format(flipped_y_binning)," && ".join([quadcut,fiducialcut_x,hitcut]),"");
        #c.Print(outfilename+".pdf");
        numerator = gDirectory.Get("hy")
        denominator = gDirectory.Get("hy_exp")
        eff = TEfficiency(numerator,denominator)
        #eff.GetYaxis().SetRangeUser(0,1.1)
        eff.SetTitle("efficiency vs. position, {0}, quad {1};extrapolated y [cm];efficiency".format(station,quad))
        eff.Draw("APZ")
        #eff.Draw("A3")
        gPad.Update()
        #eff.GetPaintedGraph().SetFillColor(12)
        #eff.GetPaintedGraph().SetFillStyle(1001)
        eff.GetPaintedGraph().SetMinimum(0)
        eff.GetPaintedGraph().SetMaximum(1.1)
        #numerator.Sumw2();
        #numerator.Divide(denominator)
        #numerator.GetYaxis().SetRangeUser(0,1.1)
        #numerator.SetTitle("efficiency vs. position, {0}, quad {1};extrapolated y [cm];efficiency".format(station,quad))
        #numerator.Draw();
        c.Print(outfilename+".pdf");

        #events.Draw("ty*797+y0:tx*797+x0>>hexp2d_fid(200,-50,50,200,-50,50)",fiducialcuts,"colz");
        #c.Print(outfilename+".pdf");

        events.Draw("{0}:{1}>>hexp2d({2},{3})".format(y_var,x_var,x_binning,y_binning),quadcut,"colz");
        c.Print(outfilename+".pdf");
        events.Draw("{0}:{1}>>hexp2d_withhit({2},{3})".format(y_var,x_var,x_binning,y_binning)," && ".join([quadcut,hitcut]),"colz");
        c.Print(outfilename+".pdf");
        numerator = gDirectory.Get("hexp2d_withhit")
        denominator = gDirectory.Get("hexp2d")
        eff = TEfficiency(numerator,denominator)
        #eff.GetZaxis().SetRangeUser(0,1)
        eff.SetTitle("efficiency vs. position, {0}, quad {1};extrapolated x [cm];extrapolated y [cm]".format(station,quad))
        eff.Draw("colz")
        gPad.Update()
        eff.GetPaintedHistogram().GetZaxis().SetRangeUser(0,1)
        #eff.GetPaintedGraph().SetMaximum(1.1)
        #numerator.Sumw2();
        #numerator.Divide(denominator)
        #numerator.GetZaxis().SetRangeUser(0,1)
        #numerator.SetTitle("efficiency vs. position, {0}, quad {1};extrapolated x [cm];extrapolated y [cm]".format(station,quad))
        #numerator.Draw("colz");
        c.Print(outfilename+".pdf");

        '''
示例#3
0
                    hist_eff_QMisID.SetName(eff_QMisID_name)
                    hist_eff_fake_scaled.SetName(eff_fake_scaled_name)

                    hists[eff_QMisID_name]      = hist_eff_QMisID
                    hists[eff_fake_scaled_name] = hist_eff_fake_scaled

                # *********************************************************************************************************************************

                # 2.
                # The TEfficiency class handles the special cases not covered by TH1::Divide
                #
		t_efficiency = None
		if TEfficiency.CheckConsistency(hist_pass, hist_tot,"w"):
		    t_efficiency = TEfficiency(hist_pass, hist_tot)
		    t_efficiency.SetName(histname + "_TEfficiency_" + append_str)

		    t_efficiency.SetConfidenceLevel(0.683)

                    # Use TEfficiency, with the frequentist Clopper-Pearson confidence interval at XX% CL (set before)
                    # (this handles the eff = 0, 1 case)
                    #
                    # DOES NOT SEEM TO WORK FOR WEIGHTED HISTOGRAMS --> IT REDUCES TO THE NORMAL APPROX
                    #
		    #t_efficiency.SetStatisticOption(TEfficiency.kFCP)
                    #
                    # Use TEfficiency, with the Bayesian uniform prior at XX% CL (set before)
                    # (This is the same as the TGraphAsymmErrors below)
                    #
                    # In order to get the same value for efficiency as in a frequentist approach (as from TH1::Divide("B")),
                    # the MODE should be used as an estimator. This works as long as a uniform prior is chosen.
示例#4
0
def buildHisto(ntpInName,
               ntpOutName,
               bin_spec,
               name,
               x='b0_TRUEP_Z',
               y='b0_TRUEPT',
               particle='b0',
               treeName='TupleB0/DecayTree',
               spdCut=True,
               tosTrg='L0MuonDecision_TOS'):
    xbins, ybins = bin_spec

    # Need to convert bin boundaries to C++ arrays
    # NOTE: This can't happen outside this function, as the returned C++ pointer
    # will be garbage-collected, making the whole thing malfunctioning
    v_xbins = std.vector('Double_t')(xbins)
    v_ybins = std.vector('Double_t')(ybins)

    histoTot = TH2D(f'{name}_tot', f'{name}_tot',
                    len(xbins) - 1, v_xbins.data(),
                    len(ybins) - 1, v_ybins.data())
    # For TISTOS method
    histoTos = TH2D(f'{name}_tos', f'{name}_tos',
                    len(xbins) - 1, v_xbins.data(),
                    len(ybins) - 1, v_ybins.data())
    histoTis = TH2D(f'{name}_tis', f'{name}_tis',
                    len(xbins) - 1, v_xbins.data(),
                    len(ybins) - 1, v_ybins.data())
    histoTistos = TH2D(f'{name}_tistos', f'{name}_tistos',
                       len(xbins) - 1, v_xbins.data(),
                       len(ybins) - 1, v_ybins.data())
    histoNotTos = TH2D(f'{name}_not_tos', f'{name}_not_tos',
                       len(xbins) - 1, v_xbins.data(),
                       len(ybins) - 1, v_ybins.data())
    histoTisNotTos = TH2D(f'{name}_tis_not_tos', f'{name}_tis_not_tos',
                          len(xbins) - 1, v_xbins.data(),
                          len(ybins) - 1, v_ybins.data())

    ntpIn = TFile.Open(ntpInName, 'READ')
    tree = ntpIn.Get(treeName)
    tis, tos = [f'{particle}_{i}' for i in ['L0Global_TIS', tosTrg]]

    for event in tree:
        brX = Log(getattr(event, x))
        brY = Log(getattr(event, y))
        brTis = getattr(event, tis)
        brTos = getattr(event, tos)
        brSpd = getattr(event, 'NumSPDHits')

        # Apply a global nSPDhits cut
        if spdCut and brSpd >= 450:
            continue

        histoTot.Fill(brX, brY)

        if brTis:
            histoTis.Fill(brX, brY)
        if brTos:
            histoTos.Fill(brX, brY)
        if brTis and brTos:
            histoTistos.Fill(brX, brY)

        if not brTos:
            histoNotTos.Fill(brX, brY)
        if brTis and not brTos:
            histoTisNotTos.Fill(brX, brY)

    histoTosX = histoTos.ProjectionX(f'{name}_tos_proj_x')
    histoTosY = histoTos.ProjectionY(f'{name}_tos_proj_y')
    histoTisX = histoTis.ProjectionX(f'{name}_tis_proj_x')
    histoTisY = histoTis.ProjectionY(f'{name}_tis_proj_y')
    histoTotX = histoTot.ProjectionX(f'{name}_tot_proj_x')
    histoTotY = histoTot.ProjectionY(f'{name}_tot_proj_y')
    histoTistosX = histoTistos.ProjectionX(f'{name}_tistos_proj_x')
    histoTistosY = histoTistos.ProjectionY(f'{name}_tistos_proj_y')
    histoNotTosX = histoNotTos.ProjectionX(f'{name}_not_tos_proj_x')
    histoNotTosY = histoNotTos.ProjectionY(f'{name}_not_tos_proj_y')
    histoTisNotTosX = histoTisNotTos.ProjectionX(f'{name}_tis_not_tos_proj_x')
    histoTisNotTosY = histoTisNotTos.ProjectionY(f'{name}_tis_not_tos_proj_y')

    histoTosBin = buildHistoBinnedProjection(histoTos, f'{name}_tos')
    histoTisBin = buildHistoBinnedProjection(histoTis, f'{name}_tis')
    histoTotBin = buildHistoBinnedProjection(histoTot, f'{name}_tot')
    histoTistosBin = buildHistoBinnedProjection(histoTistos, f'{name}_tistos')
    histoNotTosBin = buildHistoBinnedProjection(histoNotTos, f'{name}_not_tos')
    histoTisNotTosBin = buildHistoBinnedProjection(histoTisNotTos,
                                                   f'{name}_tis_not_tos')

    # Generate efficiency histograms
    histoEffProjX = TEfficiency(histoTistosX, histoTosX)
    histoEffProjX.SetName(f'{name}_eff_proj_x')
    histoEffProjY = TEfficiency(histoTistosY, histoTosY)
    histoEffProjY.SetName(f'{name}_eff_proj_y')
    histoEffProjBin = TEfficiency(histoTistosBin, histoTosBin)
    histoEffProjBin.SetName(f'{name}_eff_proj_bin')

    histoEffProjXDir = TEfficiency(histoTisX, histoTotX)
    histoEffProjXDir.SetName(f'{name}_eff_proj_x_dir')
    histoEffProjYDir = TEfficiency(histoTisY, histoTotY)
    histoEffProjYDir.SetName(f'{name}_eff_proj_y_dir')
    histoEffProjBinDir = TEfficiency(histoTisBin, histoTotBin)
    histoEffProjBinDir.SetName(f'{name}_eff_proj_bin_dir')

    histoEffProjXNotTos = TEfficiency(histoTisNotTosX, histoNotTosX)
    histoEffProjXNotTos.SetName(f'{name}_eff_proj_x_not_tos')
    histoEffProjYNotTos = TEfficiency(histoTisNotTosY, histoNotTosY)
    histoEffProjYNotTos.SetName(f'{name}_eff_proj_y_not_tos')
    histoEffProjBinNotTos = TEfficiency(histoTisNotTosBin, histoNotTosBin)
    histoEffProjBinNotTos.SetName(f'{name}_eff_proj_bin_not_tos')

    ntpOut = TFile.Open(ntpOutName, NTP_WRT_MODE)
    ntpOut.cd()
    histoTot.Write()
    histoTos.Write()
    histoTistos.Write()
    histoNotTos.Write()
    histoTisNotTos.Write()

    histoTosBin.Write()
    histoTistosBin.Write()
    histoTisNotTosBin.Write()

    histoEffProjX.Write()
    histoEffProjY.Write()
    histoEffProjBin.Write()
    histoEffProjXDir.Write()
    histoEffProjYDir.Write()
    histoEffProjBinDir.Write()
    histoEffProjXNotTos.Write()
    histoEffProjYNotTos.Write()
    histoEffProjBinNotTos.Write()