plotROC.py
Go to the documentation of this file.
1 #!/bin/env python
2 ##
3 ## This script reads the output.root file produced by confusionMatrixTree.py
4 ## and plots the ROC curve (signal efficiency vs bkgd efficiency). Optionally
5 ## one can specificy several output.root files and produce an overlay of ROC
6 ## the ROC curves from each. Specify as an argument a config file (example
7 ## is rocExample.cfg) that details the input root files, which variables to
8 ## sum together to make the classifier, what is signal, what is background,
9 ##
10 import sys
11 from array import array
12 import argparse
13 from ConfigParser import SafeConfigParser
14 #from collections import OrderedDict
15 
16 nBins=500
17 binLo=0.0
18 binHi=1.0
19 
20 fontSize=0.05
21 
22 def getEfficiencies(t,classifierExpression,cut):
23 
24  fullExpression="%s>>h" %classifierExpression
25  h=TH1F("h","h",nBins,binLo,binHi)
26  t.Draw(fullExpression,cut)
27 
28  totIntegral=h.Integral()
29  print totIntegral
30  return array('f', [h.Integral(iBin,nBins)/totIntegral for iBin in range(1,nBins)] )
31 
32 
33 prog=sys.argv[0]
34 parser = argparse.ArgumentParser(prog=prog, description='Make dataset definitions page')
35 
36 
37 parser.add_argument("cfgFile",help="Configuration file",type=str)
38 args=parser.parse_args()
39 
40 mainDefaults={}
41 mainDefaults["classifierExpression"]="nue"
42 mainDefaults["title"]="#nu_{e} Classifier"
43 mainDefaults["trueSignalCut"]="truelabel == 1"
44 mainDefaults["trueBackgroundCut"]=""
45 
46 mainDefaults["legendX"]=0.5
47 mainDefaults["legendY"]=0.5
48 mainDefaults["legendW"]=0.4
49 mainDefaults["legendH"]=0.4
50 
51 mainDefaults[ "signalEffRange"]="0.0 1.0"
52 mainDefaults["backgroundEffRange"]="0.0 1.0"
53 
54 cfg=SafeConfigParser(mainDefaults)
55 cfg.readfp(open(args.cfgFile))
56 
57 
58 classifierExpression=cfg.get("main","classifierExpression")
59 title=cfg.get("main","title")
60 trueSignalCut=cfg.get("main","trueSignalCut")
61 trueBackgroundCut=cfg.get("main","trueBackgroundCut")
62 instancesNames=cfg.get("main","instances").split()
63 
64 outFile=cfg.get("main","outFile")
65 
66 legendX=float(cfg.get("main","legendX"))
67 legendY=float(cfg.get("main","legendY"))
68 legendW=float(cfg.get("main","legendW"))
69 legendH=float(cfg.get("main","legendH"))
70 
71 (sgnlMin,sgnlMax)=tuple([float(x) for x in cfg.get("main","signalEffRange").split()])
72 (bkgdMin,bkgdMax)=tuple([float(x) for x in cfg.get("main","backgroundEffRange").split()])
73 
74 from ROOT import *
75 gROOT.SetBatch(1)
76 
77 rocCurves=[]
78 for instanceName in instancesNames:
79  fileName=cfg.get(instanceName,"fileName")
80  iFile=TFile(fileName,"read")
81  iTree=iFile.Get("t")
82 
83  sgnlEfficiencies=getEfficiencies(iTree,classifierExpression,trueSignalCut)
84  if trueBackgroundCut == "":
85  trueBackgroundCut = "! (%s)" %trueSignalCut
86  bkgdEfficiencies=getEfficiencies(iTree,classifierExpression,trueBackgroundCut)
87 
88  rocTitle=cfg.get(instanceName,"title")
89  nPoints=len(bkgdEfficiencies)
90  rocCurve=TGraph(nPoints,bkgdEfficiencies,sgnlEfficiencies)
91  rocCurve.SetTitle(title)
92  rocCurves.append((rocCurve,rocTitle))
93 
94 rocCan=TCanvas("rocCan","rocCan")
95 rocCan.SetLeftMargin(0.1)
96 rocCan.SetBottomMargin(0.12)
97 
98 leg=TLegend(legendX, legendY-legendH, legendX+legendW, legendY)
99 leg.SetBorderSize(0)
100 
101 for i, (rocCurve,rocTitle) in enumerate(rocCurves):
102  rocCurve.SetFillColor(0)
103  rocCurve.SetLineColor(i+1)
104  rocCurve.SetLineWidth(3)
105 
106  temppoint=Double(0)
107  temppoint=rocCurve.Eval(bkgdMax)
108  rocCurve.SetPoint(rocCurve.GetN(),bkgdMax,temppoint)
109  rocCurve.SetPoint(rocCurve.GetN(),0,0)
110  rocCurve.Sort()
111  cut=TCutG()
112  lastpoint=0
113  for j in range(0,rocCurve.GetN()):
114  tempx, tempy = Double(0), Double(0)
115  rocCurve.GetPoint(j,tempx,tempy)
116  if tempx <= bkgdMax:
117  cut.SetPoint(j, tempx,tempy)
118  lastpoint=lastpoint+1
119  cut.SetPoint(lastpoint,bkgdMax,0)
120  rocIntegral=str(-1*cut.Area())
121 
122  newtitle=str("#splitline{"+rocTitle+"}{Integral: "+rocIntegral+"}")
123  leg.AddEntry(rocCurve, newtitle)
124 
125  if i==0:
126  rocCurve.Draw("AL")
127  rocCurve.GetXaxis().SetTitle("Background Efficiency")
128  rocCurve.GetXaxis().SetRangeUser(bkgdMin, bkgdMax)
129  rocCurve.GetXaxis().SetLabelSize(fontSize)
130  rocCurve.GetXaxis().SetTitleSize(fontSize)
131  rocCurve.GetYaxis().SetTitle("Signal Efficiency")
132  rocCurve.GetYaxis().SetRangeUser(sgnlMin,sgnlMax)
133  rocCurve.GetYaxis().SetLabelSize(fontSize)
134  rocCurve.GetYaxis().SetTitleSize(fontSize)
135 
136  rocCan.Update()
137  else:
138  rocCurve.Draw("L")
139 
140 leg.Draw()
141 rocCan.Print(outFile)
void split(double tt, double *fr)
gargamelle SetTitle("Gargamelle #nu_{e} CC data")
def getEfficiencies(t, classifierExpression, cut)
Definition: plotROC.py:22
procfile open("FD_BRL_v0.txt")