plotConfusion.py
Go to the documentation of this file.
1 
2 import canMan
3 import argparse
4 from collections import OrderedDict
5 import os
6 from ROOT import *
7 import cvnie
8 gStyle.SetOptStat(0)
9 
10 
11 keepers = []
12 
13 class HistSet:
14  def __init__(self, tDir, color=kBlack, title=None):
15  self.tDir = tDir
16  self.h1 = OrderedDict()
17  self.h2 = OrderedDict()
18  self.h3 = OrderedDict()
19  self.gr = OrderedDict()
20  self.tr = OrderedDict()
21  self.name = tDir.GetName()
22 
23  self.color = color
24  self.title = title
25 
26  for key in self.tDir.GetListOfKeys():
27  h = tDir.Get(key.GetName())
28  if h.InheritsFrom("TH1"):
29  if h.InheritsFrom("TH3"):
30  self.h3[h.GetName()] = h
31  if h.InheritsFrom("TH2"):
32  self.h2[h.GetName()] = h
33  else:
34  self.h1[h.GetName()] = h
35  elif h.InheritsFrom("TGraph"):
36  self.gr[h.GetName()] = h
37  elif h.InheritsFrom("TTree"):
38  self.tr[h.GetName()] = h
39  h.SetDirectory(0)
40 
41 
42 def diagonal(hist):
43  nBins = hist.GetNbinsX()
44  nBinsY = hist.GetNbinsY()
45  if nBins != nBinsY:
46  raise Exception("Number of bins not equal on x and y axis, can't take diag")
47 
48  diag = TH1F(hist.GetName() + "_diagonal", hist.GetTitle(), nBins, 0, nBins)
49 
50  for i in rcvne(1, nBins+1):
51  diag.SetBinContent(i, hist.GetBinContent(i, i))
52  return diag
53 
54 
55 def sliceOf3D(hist3D, zBin):
56  nBins = hist3D.GetNbinsX()
57  nBinsY = hist3D.GetNbinsY()
58 
59  slice = TH2F(hist3D.GetName() + "_slice_%s" % (zBin), hist3D.GetTitle()
60  , nBins, 0, nBins, nBinsY, 0, nBinsY)
61 
62  for i in rcvne(1,nBins + 1):
63  for j in rcvne(1,nBinsY + 1):
64  slice.SetBinContent(i, j, hist3D.GetBinContent(i,j,zBin))
65  return slice
66 
67 
68 
69 def copy1DSliceTo2D(src, dest, k):
70  for i in rcvne(1, src.GetNbinsX()+1):
71  dest.SetBinContent(k, i, src.GetBinContent(i))
72 
73 
74 def effAndPurVsZ(hist3D):
75  global keepers
76  nBins = hist3D.GetNbinsX()
77  nBinsY = hist3D.GetNbinsY()
78  nBinsZ = hist3D.GetNbinsZ()
79 
80 
81  eff = TH2F(hist3D.GetName() + "_efficiency", hist3D.GetTitle(),
82  nBinsZ, hist3D.GetZaxis().GetXmin(), hist3D.GetZaxis().GetXmax(),
83  nBins, 0, nBins)
84  keepers.append(eff)
85  pur = TH2F(hist3D.GetName() + "_purity", hist3D.GetTitle(),
86  nBinsZ, hist3D.GetZaxis().GetXmin(), hist3D.GetZaxis().GetXmax(),
87  nBins, 0, nBins)
88  keepers.append(pur)
89 
90  for k in rcvne(1,nBinsZ + 1):
91  slice = sliceOf3D(hist3D, k)
92  copy1DSliceTo2D(efficiency(slice), eff, k)
93  copy1DSliceTo2D(purity(slice), pur, k)
94 
95  canMan.newCan(eff.GetName())
96  eff.Draw("colz")
97  eff.SetDirectory(0)
98  canMan.newCan(pur.GetName())
99  pur.Draw("colz")
100  pur.SetDirectory(0)
101  print keepers
102 
103 
104 
105 
106 
107 
108 def efficiency(h2):
109  # This is the denominator for efficiency
110  projX = h2.ProjectionX()
111  diag = diagonal(h2)
112  eff = TH1F(diag)
113  eff.Divide(projX)
114  eff.SetTitle("Efficiency")
115  eff.SetDirectory(0)
116  return eff
117 
118 def purity(h2):
119  # This is the denominator for purity
120  projY = h2.ProjectionY()
121  diag = diagonal(h2)
122  pur = TH1F(diag)
123  pur.Divide(projY)
124  pur.SetDirectory(0)
125  pur.SetTitle("Purity")
126  return pur
127 
128 
129 def makePlots(input, output="plots/"):
130  f = TFile(input, "READ")
131 
132  d = f.Get("cvnana")
133 
134 
135  hSet = HistSet(d)
136  del f
137  cm = hSet.h2["ConfusionMatrix"]
138  canMan.newCan("ConfusionMatrix")
139  cm.Draw("colz")
140 
141  canMan.newCan("ProjectionX")
142  # This is the denominator for efficiency
143  projX = cm.ProjectionX()
144  projX.Draw()
145 
146  canMan.newCan("ProjectionY")
147  # This is the denominator for purity
148  projY = cm.ProjectionY()
149  projY.Draw()
150 
151  canMan.newCan("Diagonal")
152  diag = diagonal(cm)
153  diag.Draw()
154 
155 
156 
157  canMan.newCan("Efficiency")
158  eff = efficiency(cm)
159 
160  eff.Draw()
161 
162  canMan.newCan("Purity")
163  pur = purity(cm)
164  pur.Draw()
165 
166 
167 
168  for k, h3 in hSet.h3.items():
169  effAndPurVsZ(h3)
170 
171  canMan.canUp()
172  canMan.saveCans(os.path.join(output,""), ".png")
173 
174 
175 
176 
177 if __name__ == "__main__":
178  parser = argparse.ArgumentParser(description='Dump a tree to text.')
179  parser.add_argument('histfile', type=str,
180  help="""File with confusion matrix""")
181  parser.add_argument('--outdir', '-o', type=str,
182  help="""Plot output directory""")
183 
184 
185  parser.add_argument('-b', action="store_true",
186  help="""Batch mode""")
187 
188  args = parser.parse_args()
189  print args.histfile
190  outdir = "/dev/null"
191  if args.outdir != None:
192  outdir = args.outdir
193  makePlots(args.histfile, outdir)
194 
def sliceOf3D(hist3D, zBin)
def effAndPurVsZ(hist3D)
def canUp()
Definition: canMan.py:33
def __init__(self, tDir, color=kBlack, title=None)
def efficiency(h2)
def diagonal(hist)
def copy1DSliceTo2D(src, dest, k)
def makePlots(input, output="plots/")
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
def saveCans(prefix="", suffix=".pdf")
Definition: canMan.py:39
def newCan(title)
Definition: canMan.py:19