CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Validation/RecoTau/Tools/PerformanceCurvePlotter.py

Go to the documentation of this file.
00001 """
00002         PerformanceCurvePlotter
00003 
00004         Author: Evan K. Friis, UC Davis (friis@physics.ucdavis.edu)
00005 
00006         Plot the efficiency for a given fake rate 
00007         for a series of cuts in a set pt range.
00008 
00009         With the default cuts (20 < pt < 50), this is designed
00010         to reproduce (independently) the TancBenchmark plots produced in
00011         RecoTauTag/TauTagTools/test/MVABenchmarks.py
00012 
00013 
00014             Instructions:
00015 
00016             Add the releases to as shown.  You must specify and signal 
00017             and backround file for each release, and a descriptive label.
00018             To choose the chain of discriminators to plot, select the appropriate
00019             EDProducer defined in Validation.RecoTau.TauTagValidation_cfi
00020 
00021             PreFourTanc = TauValidationInfo("Signal.root", 
00022                                             "Background.root",
00023                                             "310pre5 TaNC",
00024                                             Validation.RecoTau.RecoTauValidation_cfi.RunTancValidation)
00025 
00026             PreFourIso = TauValidationInfo("Signal.root", 
00027                                             "Background.root",
00028                                             "310pre4 Iso",
00029                                             Validation.RecoTau.RecoTauValidation_cfi.PFTausHighEfficiencyLeadingPionBothProngs)
00030 
00031 
00032             ReleasesToBuildFrom = []
00033             ReleasesToBuildFrom.append(PreFourTanc)
00034             ReleasesToBuildFrom.append(PreFourIso)
00035 
00036             PlotPerformanceCurves(ReleasesToBuildFrom, "output.png")
00037 
00038 """
00039 
00040 from ROOT import TH1F, TGraph, TCanvas, TPad, EColor, TFile, TGraphAsymmErrors, Double, TLegend, gPad, TPaveText, gROOT, gStyle
00041 import FWCore.ParameterSet.Config as cms
00042 import copy
00043 import Validation.RecoTau.RecoTauValidation_cfi 
00044 
00045 """
00046         Kinematic cuts applied to the sample.  The Validation histograms
00047         will be integrated over this range.  You can only select one variable.
00048 """
00049 
00050 KinematicVar    = 'pt'
00051 LatexVar        = "True p_{T}"
00052 KinematicVarMin = 20
00053 KinematicVarMax = 50 
00054 VarUnit         = "GeV/c"
00055 
00056 # Uncomment me to display electron and muon rejection performance
00057 HideMuonAndElectronRej = True
00058 
00059 
00060 gROOT.SetBatch(True)
00061 gROOT.SetStyle("Plain")
00062 gStyle.SetOptStat(0)
00063 gStyle.SetPalette(1)
00064 gStyle.SetTitleBorderSize(0)
00065 
00066 class TauValidationInfo:
00067    """ Atomic object to hold information about where files are located, which discriminants to plot """
00068    DiscriminatorToMarkerStyleDict = {} 
00069    StaticMarkerStyleIterator      = 20
00070    StaticSummaryLineColorIterator = 2 
00071    StupidRootStupidNameCounter    = 0  # to give each a unique name
00072    DiscriminatorLegend = TLegend(0.15, 0.6, 0.5, 0.92)
00073    DiscriminatorLegend.SetFillColor(0)
00074    DiscriminatorBackground = 0
00075    SummaryLegend = TLegend(0.6, 0.3, 0.9, 0.4)
00076    SummaryLegend.SetBorderSize(0)
00077    SummaryLegend.SetFillColor(0)
00078    SummaryBackground = 0
00079    def __init__(self, SignalFile, BackgroundFile, Label, ValidationProducer):
00080       self.Label = Label
00081       self.SignalFile = TFile.Open(SignalFile)
00082       self.BackgroundFile = TFile.Open(BackgroundFile)
00083       self.ValidationProducer = ValidationProducer
00084       self.InfoDictionary      = { 'producer' : self.ValidationProducer.TauProducer.value(),
00085                                    'label'    : self.ValidationProducer.ExtensionName.value(),
00086                                    'var'      : KinematicVar }
00087 
00088       # Store each discriminator as a point.  Initally, retrieve the names. Each discriminator will eventual be a tuple holding the effs/fake rates etc
00089       self.DiscriminatorPoints = [ {'name' : DiscInfo.discriminator.value().replace(self.InfoDictionary['producer'].replace('Producer', ''), '').replace('Discrimination', ''), 
00090                                     'loc'  : self.BuildFullPathForDiscriminator(DiscInfo.discriminator.value()) } for DiscInfo in ValidationProducer.discriminators.value() ]
00091       if HideMuonAndElectronRej:
00092          NewPoints = []
00093          for aPoint in self.DiscriminatorPoints:
00094             if aPoint['name'].find("Electron") == -1 and aPoint['name'].find("Muon") == -1:
00095                NewPoints.append(aPoint)
00096          self.DiscriminatorPoints = NewPoints
00097                
00098       # Add denominator info
00099       self.Denominator         =  {'loc'  : "DQMData/RecoTauV/%(producer)s%(label)s_ReferenceCollection/nRef_Taus_vs_%(var)sTauVisible" % self.InfoDictionary } 
00100 
00101    def BuildFullPathForDiscriminator(self, DiscName):
00102       self.InfoDictionary['disc'] = DiscName
00103       output = "DQMData/RecoTauV/%(producer)s%(label)s_%(disc)s/%(disc)s_vs_%(var)sTauVisible" % self.InfoDictionary
00104       del self.InfoDictionary['disc']
00105       return output
00106    def RebinHistogram(self, Histo, MinBinValue, MaxBinValue):
00107       """ Rebin a range of an input histogram into a new histogram w/ 1 bin. """
00108       OutputHisto = TH1F("temp_%i" % TauValidationInfo.StupidRootStupidNameCounter, "temp", 1, MinBinValue, MaxBinValue)
00109       TauValidationInfo.StupidRootStupidNameCounter += 1
00110       MinBinNumber = Histo.FindBin(MinBinValue)
00111       MaxBinNumber = Histo.FindBin(MaxBinValue)
00112       #print "Integrating histogram between values (%f, %f)" % (Histo.GetBinLowEdge(MinBinNumber), Histo.GetBinLowEdge(MaxBinNumber)+Histo.GetBinWidth(MaxBinNumber))
00113       OutputHisto.SetBinContent(1,Histo.Integral(MinBinNumber, MaxBinNumber))
00114       return OutputHisto
00115    def LoadHistograms(self):
00116       # Load original histograms, and rebin them if necessary
00117       # Get denominator
00118       print "Loading histograms for %s" % self.Label
00119       SignalDenominatorHisto                   = self.SignalFile.Get(self.Denominator['loc'])
00120       self.Denominator['SignalHisto']          = SignalDenominatorHisto
00121       self.Denominator['SignalHistoRebin']     = self.RebinHistogram(SignalDenominatorHisto, KinematicVarMin, KinematicVarMax)
00122       BackgroundDenominatorHisto               = self.BackgroundFile.Get(self.Denominator['loc'])
00123       self.Denominator['BackgroundHisto']      = BackgroundDenominatorHisto
00124       self.Denominator['BackgroundHistoRebin'] = self.RebinHistogram(BackgroundDenominatorHisto, KinematicVarMin, KinematicVarMax)
00125       # Get numerators
00126       for aPoint in self.DiscriminatorPoints:
00127          SignalPointHisto               = self.SignalFile.Get(aPoint['loc'])
00128          aPoint['SignalHisto']          = SignalPointHisto
00129          aPoint['SignalHistoRebin']     = self.RebinHistogram(SignalPointHisto, KinematicVarMin, KinematicVarMax)
00130          BackgroundPointHisto           = self.BackgroundFile.Get(aPoint['loc'])
00131          aPoint['BackgroundHisto']      = BackgroundPointHisto
00132          aPoint['BackgroundHistoRebin'] = self.RebinHistogram(BackgroundPointHisto, KinematicVarMin, KinematicVarMax)
00133    def ComputeEfficiencies(self):
00134       print "Computing efficiencies for %s" % self.Label
00135       print "-----------------------------------------------------"
00136       print "%-40s %10s %10s" % ("Discriminator", "SignalEff", "FakeRate") 
00137       print "-----------------------------------------------------"
00138       # Get denominators
00139       SignalDenominatorHisto     = self.Denominator['SignalHistoRebin']
00140       BackgroundDenominatorHisto = self.Denominator['BackgroundHistoRebin']
00141       for aPoint in self.DiscriminatorPoints:
00142          SignalPointHisto = aPoint['SignalHistoRebin']
00143          BackgroundPointHisto = aPoint['BackgroundHistoRebin']
00144          # compute length 1 TGraphAsymmErrors, to compute the errors correctly
00145          TempSignalEffPoint = TGraphAsymmErrors(SignalPointHisto, SignalDenominatorHisto)
00146          TempBackgroundEffPoint = TGraphAsymmErrors(BackgroundPointHisto, BackgroundDenominatorHisto)
00147          # Create a new TGraphAsymmErrors, where the x and y coordinates are the Signal/Background efficiencies, respectively
00148          PerformancePoint = TGraphAsymmErrors(1) #only one point
00149          xValueSignal = Double(0) #stupid root pass by reference crap
00150          yValueSignal = Double(0) 
00151          xValueBackground = Double(0)
00152          yValueBackground = Double(0) 
00153          TempSignalEffPoint.GetPoint(0, xValueSignal, yValueSignal)
00154          TempBackgroundEffPoint.GetPoint(0, xValueBackground, yValueBackground)
00155          aPoint['SignalEff']     = yValueSignal
00156          aPoint['BackgroundEff'] = yValueBackground
00157          print "%-40s %10.3f %10.4f" % (aPoint['name'], aPoint['SignalEff'], aPoint['BackgroundEff'])
00158          PerformancePoint.SetPoint(0, yValueSignal, yValueBackground)
00159          PerformancePoint.SetPointError(0, 
00160                                         TempSignalEffPoint.GetErrorYlow(0),             #ex low
00161                                         TempSignalEffPoint.GetErrorYhigh(0),            #ex high
00162                                         TempBackgroundEffPoint.GetErrorYlow(0),         #ey low
00163                                         TempBackgroundEffPoint.GetErrorYhigh(0) )       #ey high
00164          PerformancePoint.SetMarkerSize(2)
00165          try:
00166             PerformancePoint.SetMarkerStyle(TauValidationInfo.DiscriminatorToMarkerStyleDict[aPoint['name']])
00167          except KeyError:
00168             PerformancePoint.SetMarkerStyle(TauValidationInfo.StaticMarkerStyleIterator)
00169             TauValidationInfo.DiscriminatorToMarkerStyleDict[aPoint['name']] = TauValidationInfo.StaticMarkerStyleIterator
00170             self.DiscriminatorLegend.AddEntry( PerformancePoint, aPoint['name'],"P")
00171             TauValidationInfo.StaticMarkerStyleIterator += 1
00172          aPoint['PerformancePoint'] = PerformancePoint
00173    def BuildTGraphSummary(self):
00174       print "Building summary"
00175       self.SummaryTGraph = TGraph(len(self.DiscriminatorPoints))
00176       for index, aPoint in enumerate(self.DiscriminatorPoints):
00177          self.SummaryTGraph.SetPoint(index, aPoint['SignalEff'], aPoint['BackgroundEff'])
00178       self.SummaryTGraph.SetLineColor(TauValidationInfo.StaticSummaryLineColorIterator)
00179       self.SummaryTGraph.SetLineWidth(2)
00180       self.SummaryLegend.AddEntry( self.SummaryTGraph, self.Label,"L")
00181       TauValidationInfo.StaticSummaryLineColorIterator += 1
00182 
00183 
00184 def PlotPerformanceCurves(ReleasesToBuildFrom, OutputFile):
00185    # Setup
00186    myCanvas = TCanvas("Validation", "Validation", 800, 800)
00187    # Cut label on the plots
00188    CutLabel = TPaveText(0.6, 0.1, 0.9, 0.27, "brNDC")
00189    CutLabel.AddText("%0.1f%s < %s < %0.1f%s" % (KinematicVarMin, VarUnit, LatexVar, KinematicVarMax, VarUnit))
00190    CutLabel.SetFillStyle(0)
00191    CutLabel.SetBorderSize(0)
00192 
00193    # Build the TGraphs of sigEff versus bkgFakeRate for each release
00194    for aRelease in ReleasesToBuildFrom:
00195       aRelease.LoadHistograms()
00196       aRelease.ComputeEfficiencies()
00197       aRelease.BuildTGraphSummary()
00198 
00199    CurrentHistogram = 0
00200 
00201    for aRelease in ReleasesToBuildFrom:
00202       if CurrentHistogram == 0:
00203          aRelease.SummaryTGraph.Draw("ALP")
00204          CurrentHistogram = aRelease.SummaryTGraph.GetHistogram()
00205       else:
00206          aRelease.SummaryTGraph.Draw("LP")
00207 
00208 
00209    CurrentHistogram.SetAxisRange(0, 1)
00210    CurrentHistogram.GetYaxis().SetRangeUser(0.0001,1)
00211    CurrentHistogram.GetXaxis().SetTitle("Efficiency")
00212    CurrentHistogram.GetYaxis().SetTitle("Fake Rate")
00213    CurrentHistogram.SetTitle("Performance Points")
00214 
00215    gPad.SetLogy(True)
00216 
00217    for aRelease in ReleasesToBuildFrom:
00218       for aPoint in aRelease.DiscriminatorPoints:
00219          aPoint['PerformancePoint'].Draw("P")
00220 
00221    TauValidationInfo.SummaryLegend.Draw()
00222    TauValidationInfo.DiscriminatorLegend.Draw()
00223    CutLabel.Draw()
00224 
00225    myCanvas.SaveAs(OutputFile)