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 
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"]="nueqe+nuedis+nueres+nueother"
42 mainDefaults["title"]="#nu_{e} Classifier"
43 mainDefaults["trueSignalCut"]="truelabel >=4 && truelabel <=7"
44 
45 mainDefaults["legendX"]=0.5
46 mainDefaults["legendY"]=0.5
47 mainDefaults["legendW"]=0.4
48 mainDefaults["legendH"]=0.4
49 
50 mainDefaults[ "signalEffRange"]="0.0 1.0"
51 mainDefaults["backgroundEffRange"]="0.0 1.0"
52 
53 cfg=SafeConfigParser(mainDefaults)
54 cfg.readfp(open(args.cfgFile))
55 
56 
57 classifierExpression=cfg.get("main","classifierExpression")
58 title=cfg.get("main","title")
59 trueSignalCut=cfg.get("main","trueSignalCut")
60 
61 instancesNames=cfg.get("main","instances").split()
62 
63 outFile=cfg.get("main","outFile")
64 
65 legendX=float(cfg.get("main","legendX"))
66 legendY=float(cfg.get("main","legendY"))
67 legendW=float(cfg.get("main","legendW"))
68 legendH=float(cfg.get("main","legendH"))
69 
70 (sgnlMin,sgnlMax)=tuple([float(x) for x in cfg.get("main","signalEffRange").split()])
71 (bkgdMin,bkgdMax)=tuple([float(x) for x in cfg.get("main","backgroundEffRange").split()])
72 
73 from ROOT import *
74 gROOT.SetBatch(1)
75 
76 rocCurves=[]
77 for instanceName in instancesNames:
78  fileName=cfg.get(instanceName,"fileName")
79  iFile=TFile(fileName,"read")
80  iTree=iFile.Get("t")
81 
82  sgnlEfficiencies=getEfficiencies(iTree,classifierExpression,trueSignalCut)
83 
84  trueBackgroundCut= "! (%s)" %trueSignalCut
85  bkgdEfficiencies=getEfficiencies(iTree,classifierExpression,trueBackgroundCut)
86 
87  rocTitle=cfg.get(instanceName,"title")
88  nPoints=len(bkgdEfficiencies)
89  rocCurve=TGraph(nPoints,bkgdEfficiencies,sgnlEfficiencies)
90  rocCurve.SetTitle(title)
91  rocCurves.append((rocCurve,rocTitle))
92 
93 rocCan=TCanvas("rocCan","rocCan")
94 rocCan.SetLeftMargin(0.1)
95 rocCan.SetBottomMargin(0.12)
96 
97 leg=TLegend(legendX, legendY-legendH, legendX+legendW, legendY)
98 leg.SetBorderSize(0)
99 
100 for i, (rocCurve,rocTitle) in enumerate(rocCurves):
101  rocCurve.SetFillColor(0)
102  rocCurve.SetLineColor(i+1)
103  rocCurve.SetLineWidth(3)
104  leg.AddEntry(rocCurve, rocTitle)
105  if i==0:
106  rocCurve.Draw("AL")
107  rocCurve.GetXaxis().SetTitle("Background Efficiency")
108  rocCurve.GetXaxis().SetRangeUser(bkgdMin, bkgdMax)
109  rocCurve.GetXaxis().SetLabelSize(fontSize)
110  rocCurve.GetXaxis().SetTitleSize(fontSize)
111 
112  rocCurve.GetYaxis().SetTitle("Signal Efficiency")
113  rocCurve.GetYaxis().SetRangeUser(sgnlMin,sgnlMax)
114  rocCurve.GetYaxis().SetLabelSize(fontSize)
115  rocCurve.GetYaxis().SetTitleSize(fontSize)
116 
117  rocCan.Update()
118  else:
119  rocCurve.Draw("L")
120 
121 leg.Draw()
122 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")