# How to get tree branch variables for a specific entry based on the thresholds from the ROC curve?

**URL:** <https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663>\
**Category:** TMVA\
**Tags:** python\
**Created:** [June 17, 2024, 9:26am UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663 "2024-06-17T09:26:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [June 17, 2024, 9:26am UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663/1 "2024-06-17T09:26:56Z")

</div>

Hi, ROOT experts,

I have a script to make the ROC curve out of the TMVA output file by using the cuts:

```auto
import numpy as np
import ROOT
from ROOT import TFile, TTree, TCanvas, TGraph, TPad, TLegend

f = TFile.Open("factoryOutputFile.root")
t = f.Get("dataLoader/TestTree")

nSigTot = t.Draw("PyTorch", "classID==0")
nBkgTot = t.Draw("PyTorch", "classID==1")

gr = TGraph()

cutValues = np.array([])
cut = 0
while cut <= 0.96:
    cutValues = np.append(cutValues, cut)
    cut += 0.01
while cut > 0.96 and cut <= 1.0:
    cutValues = np.append(cutValues, cut)
    cut += 0.001

for cut in cutValues:
    cutSig = f"classID==0 && PyTorch > {cut}"
    cutBkg = f"classID==1 && PyTorch > {cut}"

    sigCount = t.GetEntries(cutSig)
    bkgCount = t.GetEntries(cutBkg)

    eff = sigCount / nSigTot
    rej = 1 - bkgCount / nBkgTot
    gr.SetPoint(gr.GetN(), eff, rej)

canvas = TCanvas("c1", "Signal efficiency vs. Background rejection", 10, 10, 850, 500)
canvas.cd()
ROOT.gStyle.SetOptStat(0)
ROOT.gPad.SetLeftMargin(0.15)

grid = TPad("grid", "", 0, 0, 1, 1)
grid.Draw()
grid.cd()
grid.SetGrid()

gr.SetTitle("Signal efficiency vs. Background rejection")
gr.GetXaxis().SetTitle("Signal efficiency (Sensitivity)")
gr.GetXaxis().SetRangeUser(0.92,1.0015)
gr.GetYaxis().SetTitle("Background rejection (Specificity)")
gr.GetYaxis().SetRangeUser(0.9,1.001)
gr.SetLineWidth(2)
gr.SetLineColor(4)
gr.Draw("AL")

leg = TLegend(0.15, 0.15, 0.35, 0.3)
leg.SetHeader("MVA Method", "")
leg.AddEntry(gr, "PyTorch_CNN", "l");
leg.Draw()

canvas.Print("cuts.pdf", "pdf")

```

Here is the plot:  
[roc.pdf](https://root-forum.cern.ch/uploads/short-url/tPe9vc8ZwnSz9JLr9WtJ0wlycXA.pdf) (14.3 KB)

Now I am trying to find a way to get the event numbers added by `loader.AddSpectator("event_number")` for the **false positives** at a certain threshold because I want to see what background events didn’t get rejected by the classifier; for example, when the sensitivity equals 0.97 and the background rejection equals 0.9989 by looking at the ROC curve, how do I do that? I have `event_number` in the `TestTree`, hope you can give me some guidance.

Thank you!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 17, 2024, 11:31am UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663/2 "2024-06-17T11:31:44Z")

</div>

I guess @moneta can help.

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [June 19, 2024, 7:08am UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663/3 "2024-06-19T07:08:22Z")

</div>

Hi @couet  
Thank you for tagging @moneta for me.

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [July 1, 2024, 8:54am UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663/4 "2024-07-01T08:54:58Z")

</div>

Hi @moneta,  
Could you please help me with this question? That would be highly appreciated, thank you!

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [July 18, 2024, 2:35pm UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663/5 "2024-07-18T14:35:44Z")

</div>

I’ve figured it out:

```auto
import numpy as np
import ROOT
from ROOT import TFile, TTree, TCanvas, TGraph, TPad, TLegend

f = TFile.Open("factoryOutputFile.root")
t = f.Get("dataLoader/TestTree")

nSigTot = t.Draw("PyTorch", "classID==0")
nBkgTot = t.Draw("PyTorch", "classID==1")

gr = TGraph()

cutValues = np.array([])
cut = 0
while cut <= 0.96:
    cutValues = np.append(cutValues, cut)
    cut += 0.01
while cut > 0.96 and cut <= 1.0:
    cutValues = np.append(cutValues, cut)
    cut += 0.001

nFalsePositiveEvents = 0
for cut in cutValues:
    cutSig = f"classID==0 && PyTorch > {cut}"
    cutBkg = f"classID==1 && PyTorch > {cut}"

    sigCount = t.GetEntries(cutSig)
    bkgCount = t.GetEntries(cutBkg)

    eff = sigCount / nSigTot
    rej = 1 - bkgCount / nBkgTot
    gr.SetPoint(gr.GetN(), eff, rej)

    if eff < 0.9705 and eff > 0.9695:
        print(f"*** Signal Efficiency: {eff}")
        for i in range(bkgCount):
            t.GetEntry(i)
            if t.classID == 1:
                nFalsePositiveEvents += 1
                run_number = int(t.run_number)
                event_number = int(t.event_number)
                print(f"Run {run_number} Event {event_number}")

print(f"Number of False Positive Events: {nFalsePositiveEvents}")

canvas = TCanvas("c1", "Signal efficiency vs. Background rejection", 10, 10, 850, 500)
canvas.cd()
ROOT.gStyle.SetOptStat(0)
ROOT.gPad.SetLeftMargin(0.15)

grid = TPad("grid", "", 0, 0, 1, 1)
grid.Draw()
grid.cd()
grid.SetGrid()

gr.SetTitle("Signal efficiency vs. Background rejection")
gr.GetXaxis().SetTitle("Signal efficiency (Sensitivity)")
gr.GetXaxis().SetRangeUser(0.92,1.0015)
gr.GetYaxis().SetTitle("Background rejection (Specificity)")
gr.GetYaxis().SetRangeUser(0.9,1.001)
gr.SetLineWidth(2)
gr.SetLineColor(4)
gr.Draw("AL")

leg = TLegend(0.15, 0.15, 0.35, 0.3)
leg.SetHeader("MVA Method", "")
leg.AddEntry(gr, "PyTorch_CNN", "l");
leg.Draw()

canvas.Print("cuts.pdf", "pdf")

```

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [July 22, 2024, 4:08pm UTC](https://root-forum.cern.ch/t/how-to-get-tree-branch-variables-for-a-specific-entry-based-on-the-thresholds-from-the-roc-curve/59663/6 "2024-07-22T16:08:40Z")

</div>

**Update:** (There’s a small mistake in the code above)

```auto
import numpy as np
import ROOT
from ROOT import TFile, TTree, TCanvas, TGraph, TPad, TLegend

f = TFile.Open("factoryOutputFile.root")
t = f.Get("dataLoader/TestTree")

nSigTot = t.Draw("PyTorch", "classID==0")
nBkgTot = t.Draw("PyTorch", "classID==1")

gr = TGraph()

cutValues = np.array([])
cut = 0
while cut <= 0.96:
    cutValues = np.append(cutValues, cut)
    cut += 0.01
while cut > 0.96 and cut <= 1.0:
    cutValues = np.append(cutValues, cut)
    cut += 0.001

nFalsePositiveEvents = 0
for cut in cutValues:
    cutSig = f"classID==0 && PyTorch > {cut}"
    cutBkg = f"classID==1 && PyTorch > {cut}"

    sigCount = t.GetEntries(cutSig)
    bkgCount = t.GetEntries(cutBkg)

    eff = sigCount / nSigTot
    rej = 1 - bkgCount / nBkgTot
    gr.SetPoint(gr.GetN(), eff, rej)

    if eff < 0.9705 and eff > 0.9695:
        print(f"*** Signal Efficiency: {eff}")
        i = 0
        while nFalsePositiveEvents < bkgCount:
            t.GetEntry(i)
            if t.classID == 1 and t.PyTorch > cut:
                nFalsePositiveEvents += 1
                run_number = int(t.run_number)
                event_number = int(t.event_number)
                print(f"Run {run_number} Event {event_number}")
            i += 1

print(f"Number of False Positive Events: {nFalsePositiveEvents}")

canvas = TCanvas("c1", "Signal efficiency vs. Background rejection", 10, 10, 850, 500)
canvas.cd()
ROOT.gStyle.SetOptStat(0)
ROOT.gPad.SetLeftMargin(0.15)

grid = TPad("grid", "", 0, 0, 1, 1)
grid.Draw()
grid.cd()
grid.SetGrid()

gr.SetTitle("Signal efficiency vs. Background rejection")
gr.GetXaxis().SetTitle("Signal efficiency (Sensitivity)")
gr.GetXaxis().SetRangeUser(0.92,1.0015)
gr.GetYaxis().SetTitle("Background rejection (Specificity)")
gr.GetYaxis().SetRangeUser(0.9,1.001)
gr.SetLineWidth(2)
gr.SetLineColor(4)
gr.Draw("AL")

leg = TLegend(0.15, 0.15, 0.35, 0.3)
leg.SetHeader("MVA Method", "")
leg.AddEntry(gr, "PyTorch_CNN", "l");
leg.Draw()

canvas.Print("cuts.pdf", "pdf")

```
