ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/grimes/VHbbAnalysisCode/src/tools.cpp
Revision: 1.1
Committed: Wed May 2 15:59:19 2012 UTC (13 years ago) by grimes
Branch: MAIN
Log Message:
Added a command line utility to print out a btag discriminator given a desired operating point and btag performance file. Some of that functionality is in functions in tools.h.

File Contents

# Content
1 #include "TrkUpgradeAnalysis/VHbb/interface/tools.h"
2
3 #include <stdexcept>
4 #include <iostream>
5
6 #include <TFile.h>
7 #include <TH1F.h>
8
9 trkupgradeanalysis::tools::TFile_auto_ptr::TFile_auto_ptr( const std::string& filename )
10 {
11 this->reset( TFile::Open( filename.c_str() ) );
12 }
13
14 trkupgradeanalysis::tools::TFile_auto_ptr::~TFile_auto_ptr()
15 {
16 // If the pointer is not null, close the file. Let the auto_ptr destructor delete it.
17 if( this->get() ) this->get()->Close();
18 }
19
20 bool trkupgradeanalysis::tools::TFile_auto_ptr::operator==( const TFile* pOtherFile )
21 {
22 return this->get()==pOtherFile;
23 }
24
25
26
27
28
29
30 float trkupgradeanalysis::tools::linearInterpolate( std::pair<float,float> point1, std::pair<float,float> point2, float requiredY )
31 {
32 // Using the equation for a straight line of y=mx+c, work out m and c so
33 // that the line passes through point1 and point2
34 float m=(point1.second-point2.second)/(point1.first-point2.first);
35 float c=point2.second-m*point2.first;
36
37 // Then I can return x=(y-c)/m
38 return (requiredY-c)/m;
39 }
40
41
42
43
44
45
46 float trkupgradeanalysis::tools::findDiscriminator( const TH1F* pEventsVersusDiscriminatorHistogram, const float requiredEfficiency, const bool verbose )
47 {
48 if( pEventsVersusDiscriminatorHistogram==NULL ) throw std::runtime_error("findDiscriminator was called with a NULL histogram pointer");
49
50 TAxis* pHistogramXAxis=pEventsVersusDiscriminatorHistogram->GetXaxis();
51 float accumulatedEvents=0; // The number of events that would pass the current cut (i.e. number of events in this bin and all higher ones)
52 float previousEfficiency=1; // The efficiency on the last run through the loop. Set high for logic to work
53
54 // Loop backwards over each of the bins in the histogram
55 for( int bin=pHistogramXAxis->GetNbins(); bin>=1; --bin )
56 {
57 accumulatedEvents+=pEventsVersusDiscriminatorHistogram->GetBinContent(bin);
58
59 float currentEfficiency=accumulatedEvents/pEventsVersusDiscriminatorHistogram->GetEntries();
60 // See if this loop has gone a bin too far. If so interpolate between this and the previous loop.
61 if( currentEfficiency>requiredEfficiency )
62 {
63 // The working point is somewhere between the previous bin and this bin.
64 // Do a linear interpolation between the bins.
65 std::pair<float,float> point1( pHistogramXAxis->GetBinLowEdge(bin), currentEfficiency );
66 std::pair<float,float> point2( pHistogramXAxis->GetBinLowEdge(bin+1), previousEfficiency );
67 float operatingPoint=trkupgradeanalysis::tools::linearInterpolate( point1, point2, requiredEfficiency );
68
69 if( verbose )
70 {
71 std::cout << "[findDiscriminator verbose] The operating point of " << requiredEfficiency << " efficiency for \"" << pEventsVersusDiscriminatorHistogram->GetName() << "\" is between:" << "\n"
72 << "[findDiscriminator verbose] \t" << pHistogramXAxis->GetBinLowEdge(bin) << " -> " << currentEfficiency << " (bin " << bin << ") and " << "\n"
73 << "[findDiscriminator verbose] \t" << pHistogramXAxis->GetBinLowEdge(bin+1) << " -> " << previousEfficiency << " (bin " << bin+1 << ")." << "\n"
74 << "[findDiscriminator verbose] Linear interpolation gives:" << "\n"
75 << "[findDiscriminator verbose] \t" << operatingPoint << std::endl;
76 }
77
78 return operatingPoint;
79 }
80 previousEfficiency=currentEfficiency; // Set this ready for the next loop around
81
82 } // end of backwards loop over histogram bins
83
84 // If flow has got this far, something has gone wrong
85 throw std::runtime_error("findDiscriminator was unable to find an operating point");
86
87 } // end of function findDiscriminator
88
89
90
91
92
93
94
95 float trkupgradeanalysis::tools::findEfficiency( const TH1F* pEventsVersusDiscriminatorHistogram, const float requiredDiscriminator, const bool verbose )
96 {
97 if( pEventsVersusDiscriminatorHistogram==NULL ) throw std::runtime_error("findEfficiency was called with a NULL histogram pointer");
98
99 TAxis* pHistogramXAxis=pEventsVersusDiscriminatorHistogram->GetXaxis();
100 float accumulatedEvents=0; // The number of events that would pass the current cut (i.e. number of events in this bin and all higher ones)
101 float previousEfficiency=1; // The efficiency on the last run through the loop. Set high for logic to work
102
103 // Loop backwards over each of the bins in the histogram
104 for( int bin=pHistogramXAxis->GetNbins(); bin>=1; --bin )
105 {
106 accumulatedEvents+=pEventsVersusDiscriminatorHistogram->GetBinContent(bin);
107
108 float currentEfficiency=accumulatedEvents/pEventsVersusDiscriminatorHistogram->GetEntries();
109 // See if this loop step has gone too far, if so then interpolate between this and the previous step.
110 if( pHistogramXAxis->GetBinLowEdge(bin)<requiredDiscriminator )
111 {
112 // The efficiency is somewhere between the previous bin and this bin.
113 // Do a linear interpolation between the bins. This is similar to findDiscriminator but
114 // I've switched around the x and y coordinates.
115 std::pair<float,float> point1( currentEfficiency, pHistogramXAxis->GetBinLowEdge(bin) );
116 std::pair<float,float> point2( previousEfficiency, pHistogramXAxis->GetBinLowEdge(bin+1) );
117 float efficiency=trkupgradeanalysis::tools::linearInterpolate( point1, point2, requiredDiscriminator );
118
119 if( verbose )
120 {
121 std::cout << "[findEfficiency verbose] The discriminator of " << requiredDiscriminator << " for \"" << pEventsVersusDiscriminatorHistogram->GetName() << "\" is between:" << "\n"
122 << "[findEfficiency verbose] \t" << pHistogramXAxis->GetBinLowEdge(bin) << " -> " << currentEfficiency << " (bin " << bin << ") and " << "\n"
123 << "[findEfficiency verbose] \t" << pHistogramXAxis->GetBinLowEdge(bin+1) << " -> " << previousEfficiency << " (bin " << bin+1 << ")." << "\n"
124 << "[findEfficiency verbose] Linear interpolation gives an efficiency of:" << "\n"
125 << "[findEfficiency verbose] \t" << efficiency << std::endl;
126 }
127
128 return efficiency;
129 }
130 previousEfficiency=currentEfficiency; // Set this ready for the next loop around
131
132 } // end of backwards loop over histogram bins
133
134 // If flow has got this far, something has gone wrong
135 throw std::runtime_error("findEfficiency was unable to find an operating point");
136
137 } // end of function findEfficiency