Crombie Tools
PlotROC.cc
Go to the documentation of this file.
1 /**
2  @file PlotROC.cc
3  Defines function members of the PlotROC class.
4  @author Daniel Abercrombie <dabercro@mit.edu>
5 */
6 
7 #include <iostream>
8 
9 #include "PlotROC.h"
10 
12 
13 //--------------------------------------------------------------------
15 { }
16 
17 //--------------------------------------------------------------------
19 { }
20 
21 //--------------------------------------------------------------------
22 TGraph*
23 PlotROC::MakeROC(TString CutVar, Int_t NumBins)
24 {
25  // Get the minimum and maximum values of the histograms
26  TH1F *htemp;
27  fSignalTree->Draw(CutVar, fSignalCut);
28  htemp = (TH1F*) gPad->GetPrimitive("htemp");
29  Double_t XMin = htemp->GetXaxis()->GetBinLowEdge(1);
30  Int_t NBins = htemp->GetNbinsX();
31  Double_t XMax = htemp->GetXaxis()->GetBinLowEdge(NBins) + htemp->GetXaxis()->GetBinWidth(NBins);
32  fBackgroundTree->Draw(CutVar,fBackgroundCut);
33  htemp = (TH1F*) gPad->GetPrimitive("htemp");
34  Double_t checkM = htemp->GetXaxis()->GetBinLowEdge(1);
35  if (XMin > checkM)
36  XMin = checkM;
37  NBins = htemp->GetNbinsX();
38  checkM = htemp->GetXaxis()->GetBinLowEdge(NBins) + htemp->GetXaxis()->GetBinWidth(NBins);
39  if (XMax < checkM)
40  XMax = checkM;
41 
42  fPlotHists.SetDefaultExpr(CutVar);
47 
48  std::vector<TH1D*> theHists = fPlotHists.MakeHists(NumBins, XMin, XMax);
49  const Int_t numPoints = NumBins + 1;
50  Double_t XVals[numPoints];
51  Double_t RevXVals[numPoints];
52  Double_t YVals[numPoints];
53  Double_t RevYVals[numPoints];
54 
55  Double_t sigArea = theHists[0]->Integral();
56  Double_t backArea = theHists[1]->Integral();
57  if (fPlotType == kROC) {
58  for (Int_t iPoint = 0; iPoint < numPoints; iPoint++) {
59  XVals[iPoint] = theHists[0]->Integral(iPoint, numPoints)/sigArea;
60  RevXVals[iPoint] = theHists[0]->Integral(0, numPoints-iPoint)/sigArea;
61  YVals[iPoint] = theHists[1]->Integral(iPoint, numPoints)/backArea;
62  RevYVals[iPoint] = theHists[1]->Integral(0, numPoints-iPoint)/backArea;
63  }
64  }
65  else if (fPlotType == kSignificance) {
66  for (Int_t iPoint = 0; iPoint < numPoints; iPoint++) {
67  XVals[iPoint] = theHists[0]->GetXaxis()->GetBinLowEdge(iPoint);
68  RevXVals[iPoint] = theHists[0]->GetXaxis()->GetBinLowEdge(iPoint);
69 
70  sigArea = theHists[0]->Integral(iPoint, numPoints);
71  backArea = theHists[1]->Integral(iPoint, numPoints);
72  YVals[iPoint] = (backArea + sigArea == 0) ? 0 : sigArea / sqrt(sigArea + backArea);
73 
74  sigArea = theHists[0]->Integral(0, numPoints - iPoint);
75  backArea = theHists[1]->Integral(0, numPoints - iPoint);
76  YVals[iPoint] = (backArea + sigArea == 0) ? 0 : sigArea / sqrt(sigArea + backArea);
77  }
78  }
79 
80  TGraph *rocCurve = new TGraph(numPoints, XVals, YVals);
81  TGraph *revRocCurve = new TGraph(numPoints, RevXVals, RevYVals);
82  delete theHists[0];
83  delete theHists[1];
84  theHists.resize(0);
85 
86  if (revRocCurve->Integral() > rocCurve->Integral()) {
87  delete rocCurve;
88  return revRocCurve;
89  }
90  else {
91  delete revRocCurve;
92  return rocCurve;
93  }
94 }
95 
96 //--------------------------------------------------------------------
97 std::vector<TGraph*>
98 PlotROC::MakeROCs(Int_t NumBins)
99 {
100  std::vector<TGraph*> theGraphs;
101  for (UInt_t i0 = 0; i0 < fROCVars.size(); i0++)
102  theGraphs.push_back(MakeROC(fROCVars[i0], NumBins));
103 
104  return theGraphs;
105 }
106 
107 //--------------------------------------------------------------------
108 void
109 PlotROC::MakeCanvas(TString FileBase, Int_t NumBins, TString XLabel,
110  TString YLabel, Bool_t logY, Bool_t logX)
111 {
112  if (fAxisMin == fAxisMax)
113  SetAxisMinMax(0.0,1.0);
114 
115  std::vector<TGraph*> rocs = MakeROCs(NumBins);
116  BaseCanvas(FileBase, rocs, XLabel, YLabel, logY, logX);
117 
118  for (UInt_t i0 = 0; i0 != rocs.size(); ++i0)
119  delete rocs[i0];
120 }