CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PerformanceCurvePlotter.py
Go to the documentation of this file.
1 """
2  PerformanceCurvePlotter
3 
4  Author: Evan K. Friis, UC Davis (friis@physics.ucdavis.edu)
5 
6  Plot the efficiency for a given fake rate
7  for a series of cuts in a set pt range.
8 
9  With the default cuts (20 < pt < 50), this is designed
10  to reproduce (independently) the TancBenchmark plots produced in
11  RecoTauTag/TauTagTools/test/MVABenchmarks.py
12 
13 
14  Instructions:
15 
16  Add the releases to as shown. You must specify and signal
17  and backround file for each release, and a descriptive label.
18  To choose the chain of discriminators to plot, select the appropriate
19  EDProducer defined in Validation.RecoTau.TauTagValidation_cfi
20 
21  PreFourTanc = TauValidationInfo("Signal.root",
22  "Background.root",
23  "310pre5 TaNC",
24  Validation.RecoTau.RecoTauValidation_cfi.RunTancValidation)
25 
26  PreFourIso = TauValidationInfo("Signal.root",
27  "Background.root",
28  "310pre4 Iso",
29  Validation.RecoTau.RecoTauValidation_cfi.PFTausHighEfficiencyLeadingPionBothProngs)
30 
31 
32  ReleasesToBuildFrom = []
33  ReleasesToBuildFrom.append(PreFourTanc)
34  ReleasesToBuildFrom.append(PreFourIso)
35 
36  PlotPerformanceCurves(ReleasesToBuildFrom, "output.png")
37 
38 """
39 
40 from ROOT import TH1F, TGraph, TCanvas, TPad, EColor, TFile, TGraphAsymmErrors, Double, TLegend, gPad, TPaveText, gROOT, gStyle
41 import FWCore.ParameterSet.Config as cms
42 import copy
44 
45 """
46  Kinematic cuts applied to the sample. The Validation histograms
47  will be integrated over this range. You can only select one variable.
48 """
49 
50 KinematicVar = 'pt'
51 LatexVar = "True p_{T}"
52 KinematicVarMin = 20
53 KinematicVarMax = 50
54 VarUnit = "GeV/c"
55 
56 # Uncomment me to display electron and muon rejection performance
57 HideMuonAndElectronRej = True
58 
59 
60 gROOT.SetBatch(True)
61 gROOT.SetStyle("Plain")
62 gStyle.SetOptStat(0)
63 gStyle.SetPalette(1)
64 gStyle.SetTitleBorderSize(0)
65 
67  """ Atomic object to hold information about where files are located, which discriminants to plot """
68  DiscriminatorToMarkerStyleDict = {}
69  StaticMarkerStyleIterator = 20
70  StaticSummaryLineColorIterator = 2
71  StupidRootStupidNameCounter = 0 # to give each a unique name
72  DiscriminatorLegend = TLegend(0.15, 0.6, 0.5, 0.92)
73  DiscriminatorLegend.SetFillColor(0)
74  DiscriminatorBackground = 0
75  SummaryLegend = TLegend(0.6, 0.3, 0.9, 0.4)
76  SummaryLegend.SetBorderSize(0)
77  SummaryLegend.SetFillColor(0)
78  SummaryBackground = 0
79  def __init__(self, SignalFile, BackgroundFile, Label, ValidationProducer):
80  self.Label = Label
81  self.SignalFile = TFile.Open(SignalFile)
82  self.BackgroundFile = TFile.Open(BackgroundFile)
83  self.ValidationProducer = ValidationProducer
84  self.InfoDictionary = { 'producer' : self.ValidationProducer.TauProducer.value(),
85  'label' : self.ValidationProducer.ExtensionName.value(),
86  'var' : KinematicVar }
87 
88  # Store each discriminator as a point. Initally, retrieve the names. Each discriminator will eventual be a tuple holding the effs/fake rates etc
89  self.DiscriminatorPoints = [ {'name' : DiscInfo.discriminator.value().replace(self.InfoDictionary['producer'].replace('Producer', ''), '').replace('Discrimination', ''),
90  'loc' : self.BuildFullPathForDiscriminator(DiscInfo.discriminator.value()) } for DiscInfo in ValidationProducer.discriminators.value() ]
91  if HideMuonAndElectronRej:
92  NewPoints = []
93  for aPoint in self.DiscriminatorPoints:
94  if aPoint['name'].find("Electron") == -1 and aPoint['name'].find("Muon") == -1:
95  NewPoints.append(aPoint)
96  self.DiscriminatorPoints = NewPoints
97 
98  # Add denominator info
99  self.Denominator = {'loc' : "DQMData/RecoTauV/%(producer)s%(label)s_ReferenceCollection/nRef_Taus_vs_%(var)sTauVisible" % self.InfoDictionary }
100 
101  def BuildFullPathForDiscriminator(self, DiscName):
102  self.InfoDictionary['disc'] = DiscName
103  output = "DQMData/RecoTauV/%(producer)s%(label)s_%(disc)s/%(disc)s_vs_%(var)sTauVisible" % self.InfoDictionary
104  del self.InfoDictionary['disc']
105  return output
106  def RebinHistogram(self, Histo, MinBinValue, MaxBinValue):
107  """ Rebin a range of an input histogram into a new histogram w/ 1 bin. """
108  OutputHisto = TH1F("temp_%i" % TauValidationInfo.StupidRootStupidNameCounter, "temp", 1, MinBinValue, MaxBinValue)
109  TauValidationInfo.StupidRootStupidNameCounter += 1
110  MinBinNumber = Histo.FindBin(MinBinValue)
111  MaxBinNumber = Histo.FindBin(MaxBinValue)
112  #print "Integrating histogram between values (%f, %f)" % (Histo.GetBinLowEdge(MinBinNumber), Histo.GetBinLowEdge(MaxBinNumber)+Histo.GetBinWidth(MaxBinNumber))
113  OutputHisto.SetBinContent(1,Histo.Integral(MinBinNumber, MaxBinNumber))
114  return OutputHisto
115  def LoadHistograms(self):
116  # Load original histograms, and rebin them if necessary
117  # Get denominator
118  print "Loading histograms for %s" % self.Label
119  SignalDenominatorHisto = self.SignalFile.Get(self.Denominator['loc'])
120  self.Denominator['SignalHisto'] = SignalDenominatorHisto
121  self.Denominator['SignalHistoRebin'] = self.RebinHistogram(SignalDenominatorHisto, KinematicVarMin, KinematicVarMax)
122  BackgroundDenominatorHisto = self.BackgroundFile.Get(self.Denominator['loc'])
123  self.Denominator['BackgroundHisto'] = BackgroundDenominatorHisto
124  self.Denominator['BackgroundHistoRebin'] = self.RebinHistogram(BackgroundDenominatorHisto, KinematicVarMin, KinematicVarMax)
125  # Get numerators
126  for aPoint in self.DiscriminatorPoints:
127  SignalPointHisto = self.SignalFile.Get(aPoint['loc'])
128  aPoint['SignalHisto'] = SignalPointHisto
129  aPoint['SignalHistoRebin'] = self.RebinHistogram(SignalPointHisto, KinematicVarMin, KinematicVarMax)
130  BackgroundPointHisto = self.BackgroundFile.Get(aPoint['loc'])
131  aPoint['BackgroundHisto'] = BackgroundPointHisto
132  aPoint['BackgroundHistoRebin'] = self.RebinHistogram(BackgroundPointHisto, KinematicVarMin, KinematicVarMax)
134  print "Computing efficiencies for %s" % self.Label
135  print "-----------------------------------------------------"
136  print "%-40s %10s %10s" % ("Discriminator", "SignalEff", "FakeRate")
137  print "-----------------------------------------------------"
138  # Get denominators
139  SignalDenominatorHisto = self.Denominator['SignalHistoRebin']
140  BackgroundDenominatorHisto = self.Denominator['BackgroundHistoRebin']
141  for aPoint in self.DiscriminatorPoints:
142  SignalPointHisto = aPoint['SignalHistoRebin']
143  BackgroundPointHisto = aPoint['BackgroundHistoRebin']
144  # compute length 1 TGraphAsymmErrors, to compute the errors correctly
145  TempSignalEffPoint = TGraphAsymmErrors(SignalPointHisto, SignalDenominatorHisto)
146  TempBackgroundEffPoint = TGraphAsymmErrors(BackgroundPointHisto, BackgroundDenominatorHisto)
147  # Create a new TGraphAsymmErrors, where the x and y coordinates are the Signal/Background efficiencies, respectively
148  PerformancePoint = TGraphAsymmErrors(1) #only one point
149  xValueSignal = Double(0) #stupid root pass by reference crap
150  yValueSignal = Double(0)
151  xValueBackground = Double(0)
152  yValueBackground = Double(0)
153  TempSignalEffPoint.GetPoint(0, xValueSignal, yValueSignal)
154  TempBackgroundEffPoint.GetPoint(0, xValueBackground, yValueBackground)
155  aPoint['SignalEff'] = yValueSignal
156  aPoint['BackgroundEff'] = yValueBackground
157  print "%-40s %10.3f %10.4f" % (aPoint['name'], aPoint['SignalEff'], aPoint['BackgroundEff'])
158  PerformancePoint.SetPoint(0, yValueSignal, yValueBackground)
159  PerformancePoint.SetPointError(0,
160  TempSignalEffPoint.GetErrorYlow(0), #ex low
161  TempSignalEffPoint.GetErrorYhigh(0), #ex high
162  TempBackgroundEffPoint.GetErrorYlow(0), #ey low
163  TempBackgroundEffPoint.GetErrorYhigh(0) ) #ey high
164  PerformancePoint.SetMarkerSize(2)
165  try:
166  PerformancePoint.SetMarkerStyle(TauValidationInfo.DiscriminatorToMarkerStyleDict[aPoint['name']])
167  except KeyError:
168  PerformancePoint.SetMarkerStyle(TauValidationInfo.StaticMarkerStyleIterator)
169  TauValidationInfo.DiscriminatorToMarkerStyleDict[aPoint['name']] = TauValidationInfo.StaticMarkerStyleIterator
170  self.DiscriminatorLegend.AddEntry( PerformancePoint, aPoint['name'],"P")
171  TauValidationInfo.StaticMarkerStyleIterator += 1
172  aPoint['PerformancePoint'] = PerformancePoint
174  print "Building summary"
175  self.SummaryTGraph = TGraph(len(self.DiscriminatorPoints))
176  for index, aPoint in enumerate(self.DiscriminatorPoints):
177  self.SummaryTGraph.SetPoint(index, aPoint['SignalEff'], aPoint['BackgroundEff'])
178  self.SummaryTGraph.SetLineColor(TauValidationInfo.StaticSummaryLineColorIterator)
179  self.SummaryTGraph.SetLineWidth(2)
180  self.SummaryLegend.AddEntry( self.SummaryTGraph, self.Label,"L")
181  TauValidationInfo.StaticSummaryLineColorIterator += 1
182 
183 
184 def PlotPerformanceCurves(ReleasesToBuildFrom, OutputFile):
185  # Setup
186  myCanvas = TCanvas("Validation", "Validation", 800, 800)
187  # Cut label on the plots
188  CutLabel = TPaveText(0.6, 0.1, 0.9, 0.27, "brNDC")
189  CutLabel.AddText("%0.1f%s < %s < %0.1f%s" % (KinematicVarMin, VarUnit, LatexVar, KinematicVarMax, VarUnit))
190  CutLabel.SetFillStyle(0)
191  CutLabel.SetBorderSize(0)
192 
193  # Build the TGraphs of sigEff versus bkgFakeRate for each release
194  for aRelease in ReleasesToBuildFrom:
195  aRelease.LoadHistograms()
196  aRelease.ComputeEfficiencies()
197  aRelease.BuildTGraphSummary()
198 
199  CurrentHistogram = 0
200 
201  for aRelease in ReleasesToBuildFrom:
202  if CurrentHistogram == 0:
203  aRelease.SummaryTGraph.Draw("ALP")
204  CurrentHistogram = aRelease.SummaryTGraph.GetHistogram()
205  else:
206  aRelease.SummaryTGraph.Draw("LP")
207 
208 
209  CurrentHistogram.SetAxisRange(0, 1)
210  CurrentHistogram.GetYaxis().SetRangeUser(0.0001,1)
211  CurrentHistogram.GetXaxis().SetTitle("Efficiency")
212  CurrentHistogram.GetYaxis().SetTitle("Fake Rate")
213  CurrentHistogram.SetTitle("Performance Points")
214 
215  gPad.SetLogy(True)
216 
217  for aRelease in ReleasesToBuildFrom:
218  for aPoint in aRelease.DiscriminatorPoints:
219  aPoint['PerformancePoint'].Draw("P")
220 
221  TauValidationInfo.SummaryLegend.Draw()
222  TauValidationInfo.DiscriminatorLegend.Draw()
223  CutLabel.Draw()
224 
225  myCanvas.SaveAs(OutputFile)
def replace
Definition: linker.py:10
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7