예제 #1
0
def profile(dx, dy, name, bins, xlow=None, xup=None, log=False):
    if xlow == None and xup == None:
        prof = TProfile("prof-%s" % name, "prof-%s" % name, int(len(bins)), bins, "s")
    else:
        if not log:
            prof = TProfile("prof-%s" % name, "prof-%s" % name, int(bins), xlow, xup, "s")
        else:
            if xlow <= 0:
                xlow = 1
            step = np.log((xup - xlow)) / bins
            bins = lseq(xlow, xup, step)
            prof = TProfile("prof-%s" % name, "prof-%s" % name, int(len(bins)) - 1, bins, "s")
    for xi, yi in zip(dx, dy):
        prof.Fill(xi, yi)
    x = np.array([prof.GetBinCenter(i) for i in xrange(prof.GetNbinsX()) if prof.GetBinContent(i) > 0])
    y = np.array([prof.GetBinContent(i) for i in xrange(prof.GetNbinsX()) if prof.GetBinContent(i) > 0])
    yerr = np.array([prof.GetBinError(i) for i in xrange(prof.GetNbinsX()) if prof.GetBinContent(i) > 0])
    n = np.array([prof.GetBinEntries(i) for i in xrange(prof.GetNbinsX()) if prof.GetBinContent(i) > 0])
    return x, y, yerr, n
예제 #2
0
def generator(nuslice_tree, rootfile, pset):
    n_bins = pset.n_bins
    drift_distance = pset.DriftDistance
    bin_width = drift_distance/n_bins
    half_bin_width = bin_width/2.

    xvals = np.arange(half_bin_width, drift_distance, bin_width)
    xerrs = np.array([half_bin_width] * len(xvals))
    dist_to_anode_bins = n_bins
    dist_to_anode_low = 0.
    dist_to_anode_up = drift_distance
    profile_bins = n_bins
    profile_option = 's'  # errors are the standard deviation

    dy_spreads = [None] * n_bins
    dy_means = [None] * n_bins
    dy_hist = TH2D("dy_hist", "#Delta y",
                   dist_to_anode_bins, dist_to_anode_low, dist_to_anode_up,
                   pset.dy_bins, pset.dy_low, pset.dy_up)
    dy_hist.GetXaxis().SetTitle("distance from anode (cm)")
    dy_hist.GetYaxis().SetTitle("y_flash - y_TPC (cm)")
    dy_prof = TProfile("dy_prof", "Profile of dy_spreads in #Delta y",
                       profile_bins, dist_to_anode_low, dist_to_anode_up,
                       pset.dy_low*2, pset.dy_up*2, profile_option)
    dy_prof.GetXaxis().SetTitle("distance from anode (cm)")
    dy_prof.GetYaxis().SetTitle("y_flash - y_TPC (cm)")
    dy_h1 = TH1D("dy_h1", "",
                 profile_bins, dist_to_anode_low, dist_to_anode_up)
    dy_h1.GetXaxis().SetTitle("distance from anode (cm)")
    dy_h1.GetYaxis().SetTitle("y_flash - y_TPC (cm)")

    dz_spreads = [None] * n_bins
    dz_means = [None] * n_bins
    dz_hist = TH2D("dz_hist", "#Delta z",
                   dist_to_anode_bins, dist_to_anode_low, dist_to_anode_up,
                   pset.dz_bins, pset.dz_low, pset.dz_up)
    dz_hist.GetXaxis().SetTitle("distance from anode (cm)")
    dz_hist.GetYaxis().SetTitle("z_flash - z_TPC (cm)")
    dz_prof = TProfile("dz_prof", "Profile of dz_spreads in #Delta z",
                       profile_bins, dist_to_anode_low, dist_to_anode_up,
                       pset.dz_low*2.5, pset.dz_up*2.5, profile_option)
    dz_prof.GetXaxis().SetTitle("distance from anode (cm)")
    dz_prof.GetYaxis().SetTitle("z_flash - z_TPC (cm)")
    dz_h1 = TH1D("dz_h1", "",
                 profile_bins, dist_to_anode_low, dist_to_anode_up)
    dz_h1.GetXaxis().SetTitle("distance from anode (cm)")
    dz_h1.GetYaxis().SetTitle("z_flash - z_TPC (cm)")

    rr_spreads = [None] * n_bins
    rr_means = [None] * n_bins
    rr_hist = TH2D("rr_hist", "PE Spread",
                   dist_to_anode_bins, dist_to_anode_low, dist_to_anode_up,
                   pset.rr_bins, pset.rr_low, pset.rr_up)
    rr_hist.GetXaxis().SetTitle("distance from anode (cm)")
    rr_hist.GetYaxis().SetTitle("RMS flash (cm)")
    rr_prof = TProfile("rr_prof", "Profile of PE Spread",
                       profile_bins, dist_to_anode_low, dist_to_anode_up,
                       pset.rr_low, pset.rr_up, profile_option)
    rr_prof.GetXaxis().SetTitle("distance from anode (cm)")
    rr_prof.GetYaxis().SetTitle("RMS flash (cm)")
    rr_h1 = TH1D("rr_h1", "",
                 profile_bins, dist_to_anode_low, dist_to_anode_up)
    rr_h1.GetXaxis().SetTitle("distance from anode (cm)")
    rr_h1.GetYaxis().SetTitle("RMS flash (cm)")

    if detector == "sbnd":
        pe_spreads = [None] * n_bins
        pe_means = [None] * n_bins
        pe_hist = TH2D("pe_hist", "Uncoated/Coated Ratio",
                       dist_to_anode_bins, dist_to_anode_low, dist_to_anode_up,
                       pset.pe_bins, pset.pe_low, pset.pe_up)
        pe_hist.GetXaxis().SetTitle("distance from anode (cm)")
        pe_hist.GetYaxis().SetTitle("ratio_{uncoated/coated}")
        pe_prof = TProfile("pe_prof", "Profile of Uncoated/Coated Ratio",
                           profile_bins, dist_to_anode_low, dist_to_anode_up,
                           pset.pe_low, pset.pe_up, profile_option)
        pe_prof.GetXaxis().SetTitle("distance from anode (cm)")
        pe_prof.GetYaxis().SetTitle("ratio_{uncoated/coated}")
        pe_h1 = TH1D("pe_h1", "",
                     profile_bins, dist_to_anode_low, dist_to_anode_up)
        pe_h1.GetXaxis().SetTitle("distance from anode (cm)")
        pe_h1.GetYaxis().SetTitle("ratio_{uncoated/coated}")

    match_score_scatter = TH2D("match_score_scatter", "Scatter plot of match scores",
                               dist_to_anode_bins, dist_to_anode_low, dist_to_anode_up,
                               pset.score_hist_bins, pset.score_hist_low, pset.score_hist_up*(3./5.))
    match_score_scatter.GetXaxis().SetTitle("distance from anode (cm)")
    match_score_scatter.GetYaxis().SetTitle("match score (arbitrary)")
    match_score_hist = TH1D("match_score", "Match Score",
                            pset.score_hist_bins, pset.score_hist_low, pset.score_hist_up)
    match_score_hist.GetXaxis().SetTitle("match score (arbitrary)")

    for e in nuslice_tree:
        slice = e.charge_x

        dy_hist.Fill(slice, e.flash_y - e.charge_y)
        dy_prof.Fill(slice, e.flash_y - e.charge_y)
        dz_hist.Fill(slice, e.flash_z - e.charge_z)
        dz_prof.Fill(slice, e.flash_z - e.charge_z)
        rr_hist.Fill(slice, e.flash_r)
        rr_prof.Fill(slice, e.flash_r)
        if detector == "sbnd":
            pe_hist.Fill(slice, e.flash_ratio)
            pe_prof.Fill(slice, e.flash_ratio)

    # fill histograms for match score calculation from profile histograms
    for ib in list(range(0, profile_bins)):
        ibp = ib + 1
        dy_h1.SetBinContent(ibp, dy_prof.GetBinContent(ibp))
        dy_h1.SetBinError(ibp, dy_prof.GetBinError(ibp))
        dy_means[int(ib)] = dy_prof.GetBinContent(ibp)
        dy_spreads[int(ib)] = dy_prof.GetBinError(ibp)
        dz_h1.SetBinContent(ibp, dz_prof.GetBinContent(ibp))
        dz_h1.SetBinError(ibp, dz_prof.GetBinError(ibp))
        dz_means[int(ib)] = dz_prof.GetBinContent(ibp)
        dz_spreads[int(ib)] = dz_prof.GetBinError(ibp)
        rr_h1.SetBinContent(ibp, rr_prof.GetBinContent(ibp))
        rr_h1.SetBinError(ibp, rr_prof.GetBinError(ibp))
        rr_means[int(ib)] = rr_prof.GetBinContent(ibp)
        rr_spreads[int(ib)] = rr_prof.GetBinError(ibp)
        if detector == "sbnd":
            pe_h1.SetBinContent(ibp, pe_prof.GetBinContent(ibp))
            pe_h1.SetBinError(ibp, pe_prof.GetBinError(ibp))
            pe_means[int(ib)] = pe_prof.GetBinContent(ibp)
            pe_spreads[int(ib)] = pe_prof.GetBinError(ibp)

    for e in nuslice_tree:
        slice = e.charge_x
        # calculate match score
        isl = int(slice/bin_width)
        score = 0.
        if dy_spreads[isl] <= 1.e-8:
            print("Warning zero spread.\n",
                  f"slice: {slice}. isl: {isl}. dy_spreads[isl]: {dy_spreads[isl]} ")
            dy_spreads[isl] = dy_spreads[isl+1]
        if dz_spreads[isl] <= 1.e-8:
            print("Warning zero spread.\n",
                  f"slice: {slice}. isl: {isl}. dz_spreads[isl]: {dz_spreads[isl]} ")
            dz_spreads[isl] = dz_spreads[isl+1]
        if rr_spreads[isl] <= 1.e-8:
            print("Warning zero spread.\n",
                  f"slice: {slice}. isl: {isl}. rr_spreads[isl]: {rr_spreads[isl]} ")
            rr_spreads[isl] = rr_spreads[isl+1]
        if detector == "sbnd" and pe_spreads[isl] <= 1.e-8:
            print("Warning zero spread.\n",
                  f"slice: {slice}. isl: {isl}. pe_spreads[isl]: {pe_spreads[isl]} ")
            pe_spreads[isl] = pe_spreads[isl+1]
        score += abs(abs(e.flash_y-e.charge_y) - dy_means[isl])/dy_spreads[isl]
        score += abs(abs(e.flash_z-e.charge_z) - dz_means[isl])/dz_spreads[isl]
        score += abs(e.flash_r-rr_means[isl])/rr_spreads[isl]
        if detector == "sbnd" and pset.UseUncoatedPMT:
            score += abs(e.flash_ratio-pe_means[isl])/pe_spreads[isl]
        match_score_scatter.Fill(slice, score)
        match_score_hist.Fill(score)
    metrics_filename = 'fm_metrics_' + detector + '.root'
    hfile = gROOT.FindObject(metrics_filename)
    if hfile:
        hfile.Close()
    hfile = TFile(metrics_filename, 'RECREATE',
                  'Simple flash matching metrics for ' + detector.upper())
    dy_hist.Write()
    dy_prof.Write()
    dy_h1.Write()
    dz_hist.Write()
    dz_prof.Write()
    dz_h1.Write()
    rr_hist.Write()
    rr_prof.Write()
    rr_h1.Write()
    if detector == "sbnd":
        pe_hist.Write()
        pe_prof.Write()
        pe_h1.Write()
    match_score_scatter.Write()
    match_score_hist.Write()
    hfile.Close()

    canv = TCanvas("canv")

    dy_hist.Draw()
    crosses = TGraphErrors(n_bins,
                           array('f', xvals), array('f', dy_means),
                           array('f', xerrs), array('f', dy_spreads))
    crosses.SetLineColor(9)
    crosses.SetLineWidth(3)
    crosses.Draw("Psame")
    canv.Print("dy.pdf")
    canv.Update()

    dz_hist.Draw()
    crosses = TGraphErrors(n_bins,
                           array('f', xvals), array('f', dz_means),
                           array('f', xerrs), array('f', dz_spreads))
    crosses.SetLineColor(9)
    crosses.SetLineWidth(3)
    crosses.Draw("Psame")
    canv.Print("dz.pdf")
    canv.Update()

    rr_hist.Draw()
    crosses = TGraphErrors(n_bins,
                           array('f', xvals), array('f', rr_means),
                           array('f', xerrs), array('f', rr_spreads))
    crosses.SetLineColor(9)
    crosses.SetLineWidth(3)
    crosses.Draw("Psame")
    canv.Print("rr.pdf")
    canv.Update()

    if detector == "sbnd":
        pe_hist.Draw()
        crosses = TGraphErrors(n_bins,
                               array('f', xvals), array('f', pe_means),
                               array('f', xerrs), array('f', pe_spreads))
        crosses.SetLineColor(9)
        crosses.SetLineWidth(3)
        crosses.Draw("Psame")
        canv.Print("pe.pdf")
        canv.Update()

    match_score_scatter.Draw()
    canv.Print("match_score_scatter.pdf")
    canv.Update()

    match_score_hist.Draw()
    canv.Print("match_score.pdf")
    canv.Update()
    sleep(20)
예제 #3
0
    def showENC(self):
        tree1 = TTree()
        header = 'idX/i:vL/F:vH:A:D/i:R:W'
        first = True
        for f in self.dataFiles:
            if first: tree1.ReadFile(f, header)
            else: tree1.ReadFile(f)

        p1 = TProfile('p1', 'p1;#DeltaU [V];Prob', self.bins[0],
                      tree1.GetMinimum('vH-vL') * 0.8,
                      tree1.GetMaximum('vH-vL') * 1.2)
        tree1.Draw("D:(vH-vL)>>p1", "", "profE")

        ### change it to tgraph
        g1 = TGraphErrors()
        for i in range(p1.GetNbinsX() + 2):
            N = p1.GetBinEntries(i)
            if N > 0:
                print i, N, p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(
                    i), p1.GetBinError(i)
                n = g1.GetN()
                g1.SetPoint(n,
                            p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(i))
                g1.SetPointError(n, 0, p1.GetBinError(i))

        p1.Draw("axis")
        g1.Draw('Psame')

        fun1 = TF1('fun1', '0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1])))',
                   0.05, 0.3)
        fun1.SetParameter(0, 0.155)
        fun1.SetParameter(1, 0.005)

        g1.Fit(fun1)
        fun1a = g1.GetFunction('fun1')

        fun1a.SetLineColor(2)

        v0 = fun1a.GetParameter(0)
        e0 = fun1a.GetParError(0)
        v1 = fun1a.GetParameter(1)
        e1 = fun1a.GetParError(1)

        print v0, v1

        fUnit = 1000.
        self.lt.DrawLatexNDC(
            0.185, 0.89,
            '#mu = {0:.1f} #pm {1:.1f} mV'.format(v0 * fUnit, e0 * fUnit))
        self.lt.DrawLatexNDC(
            0.185, 0.84,
            '#sigma = {0:.1f} #pm {1:.1f} mV'.format(v1 * fUnit, e1 * fUnit))
        if self.Info:
            self.lt.DrawLatexNDC(0.185, 0.6, self.Info)

        print 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1)
        fun2 = TF1('gaus1', 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1))
        fun2.SetLineColor(4)
        fun2.SetLineStyle(2)
        fun2.Draw('same')

        lg = TLegend(0.7, 0.4, 0.95, 0.5)
        lg.SetFillStyle(0)
        lg.AddEntry(p1, 'Measurement', 'p')
        lg.AddEntry(fun1a, 'Fit', 'l')
        lg.AddEntry(fun2, 'Gaus', 'l')
        lg.Draw()

        waitRootCmdX()
예제 #4
0
def showENC():
    fname1 = '/data/repos/Mic4Test_KC705/Software/Analysis/data/ENC/ENC_Chip5Col12_scan1.dat'

    tree1 = TTree()
    tree1.ReadFile(
        '/data/repos/Mic4Test_KC705/Software/Analysis/data/ENC/ENC_Chip5Col12_scan1.dat',
        'idX/i:vL/F:vH:A:D/i:R:W')
    tree1.ReadFile(
        '/data/repos/Mic4Test_KC705/Software/Analysis/data/ENC/ENC_Chip5Col12_scan2_mod.dat'
    )

    tree1.Show(500)

    p1 = TProfile('p1', 'p1;#DeltaU [V];Prob', 50, 0.12, 0.2)
    tree1.Draw("D:(vH-vL)>>p1", "", "profE")

    ### change it to tgraph
    g1 = TGraphErrors()
    for i in range(p1.GetNbinsX() + 2):
        N = p1.GetBinEntries(i)
        if N > 0:
            print i, N, p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(
                i), p1.GetBinError(i)
            n = g1.GetN()
            g1.SetPoint(n, p1.GetXaxis().GetBinCenter(i), p1.GetBinContent(i))
            g1.SetPointError(n, 0, p1.GetBinError(i))


#     g1.SetMarkerColor(3)
#     g1.SetLineColor(3)

    p1.Draw("axis")
    g1.Draw('Psame')

    fun1 = TF1('fun1', '0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1])))',
               0.05, 0.3)
    fun1.SetParameter(0, 0.155)
    fun1.SetParameter(1, 0.005)

    g1.Fit(fun1)
    fun1a = g1.GetFunction('fun1')

    #     p1.Fit(fun1)
    #     fun1a = p1.GetFunction('fun1')
    fun1a.SetLineColor(2)

    #     p1.Draw("Esame")

    v0 = fun1a.GetParameter(0)
    e0 = fun1a.GetParError(0)
    v1 = fun1a.GetParameter(1)
    e1 = fun1a.GetParError(1)

    print v0, v1

    fUnit = 1000.
    lt = TLatex()
    lt.DrawLatexNDC(
        0.185, 0.89,
        '#mu = {0:.1f} #pm {1:.1f} mV'.format(v0 * fUnit, e0 * fUnit))
    lt.DrawLatexNDC(
        0.185, 0.84,
        '#sigma = {0:.1f} #pm {1:.1f} mV'.format(v1 * fUnit, e1 * fUnit))

    print 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1)
    fun2 = TF1('gaus1', 'TMath::Gaus(x,{0:.5f},{1:.5f})'.format(v0, v1))
    fun2.SetLineColor(4)
    fun2.SetLineStyle(2)
    fun2.Draw('same')

    lg = TLegend(0.7, 0.4, 0.95, 0.5)
    lg.SetFillStyle(0)
    lg.AddEntry(p1, 'Measurement', 'p')
    lg.AddEntry(fun1a, 'Fit', 'l')
    lg.AddEntry(fun2, 'Gaus', 'l')
    lg.Draw()

    waitRootCmdX()
예제 #5
0
    axes2.errorbar(x,
                   y,
                   yerr=yerr,
                   xerr=None,
                   fmt='none',
                   ecolor='black',
                   label=hist.GetTitle())

# annotations
if classifier == 'BDTGResponse_1':
    axes.plot([0.3, 0.3], [0, 1], color='black', linewidth=2)
    axes.text(0.35,
              0.3,
              'Old working pt ($\geq 0.3$)',
              bbox=dict(boxstyle='larrow', fc='white', ec='black'))
    oeff_s = hist_s.GetBinContent(hist_s.FindBin(0.3))
    axes.annotate('(0.3,{:.3f})'.format(oeff_s),
                  xy=(0.3, oeff_s),
                  xytext=(-0.1, 0.75),
                  arrowprops=dict(facecolor='black',
                                  shrink=0.1,
                                  width=2,
                                  headwidth=5))
elif classifier in ('BDTB', 'BDTG'):
    diff, idx = 1., None
    for i, eff in enumerate(eff_s):
        diff_t = abs(eff - 0.842)  # eff_s @ old BDTG >= 0.3
        if diff > diff_t:
            diff, idx = diff_t, i
    axes.plot(clrange, [0.842, 0.842], color='black', linewidth=2)
    axes.annotate('Working pt @ old eff {}'.format((round(cuts[idx],