38using std::cout, std::endl;
76 Log() << kERROR <<
"Cannot cal ROC curve etc, as in put mvaS and mvaB have differen #nbins or range "<<
Endl;
92 Log() << kERROR <<
"Cannot Rebin Histograms mvaS and mvaB, ROC values will be calculated without Rebin histograms."<<
Endl;
123 Int_t FillColor__S = c_SignalFill;
124 Int_t FillStyle__S = 1001;
125 Int_t LineColor__S = c_SignalLine;
126 Int_t LineWidth__S = 2;
130 Int_t FillColor__B = c_BackgroundFill;
131 Int_t FillStyle__B = 3554;
132 Int_t LineColor__B = c_BackgroundLine;
133 Int_t LineWidth__B = 2;
162 if (fSplS) {
delete fSplS; fSplS = 0; }
163 if (fSplB) {
delete fSplB; fSplB = 0; }
164 if (fSpleffBvsS) {
delete fSpleffBvsS; fSpleffBvsS = 0; }
165 if (fSplmvaCumS) {
delete fSplmvaCumS; fSplmvaCumS = 0; }
166 if (fSplmvaCumB) {
delete fSplmvaCumB; fSplmvaCumB = 0; }
167 if (fmvaScumul) {
delete fmvaScumul; }
168 if (fmvaBcumul) {
delete fmvaBcumul; }
169 if (effBvsS) {
delete effBvsS; }
170 if (rejBvsS) {
delete rejBvsS; }
171 if (inveffBvsS) {
delete inveffBvsS; }
183 Log() << kERROR <<
"I guess the mva distributions fed into ROCCalc were already normalized, therefore the calculated error on the efficiency will be incorrect !! " <<
Endl;
189 fmvaScumul->
Scale( 1.0/
TMath::Max(std::numeric_limits<double>::epsilon(),fmvaScumul->GetMaximum()) );
190 fmvaBcumul->Scale( 1.0/
TMath::Max(std::numeric_limits<double>::epsilon(),fmvaBcumul->GetMaximum()) );
191 fmvaScumul->SetMinimum(0);
192 fmvaBcumul->SetMinimum(0);
197 if(effBvsS==0) effBvsS =
new TH1D(
"effBvsS",
"ROC-Curve", fNbins, 0, 1 );
198 effBvsS->SetXTitle(
"Signal eff" );
199 effBvsS->SetYTitle(
"Backgr eff" );
202 if(rejBvsS==0) rejBvsS =
new TH1D(
"rejBvsS",
"ROC-Curve", fNbins, 0, 1 );
203 rejBvsS->SetXTitle(
"Signal eff" );
204 rejBvsS->SetYTitle(
"Backgr rejection (1-eff)" );
207 if(inveffBvsS ==0) inveffBvsS =
new TH1D(
"invBeffvsSeff",
"ROC-Curve" , fNbins, 0, 1 );
208 inveffBvsS->SetXTitle(
"Signal eff" );
209 inveffBvsS->SetYTitle(
"Inverse backgr. eff (1/eff)" );
215 fSplmvaCumS =
new TSpline1(
"spline2_signal",
new TGraph( fmvaScumul ) );
216 fSplmvaCumB =
new TSpline1(
"spline2_background",
new TGraph( fmvaBcumul ) );
223 for (
UInt_t bini=1; bini<=fNbins; bini++) {
226 Double_t effS = effBvsS->GetBinCenter( bini );
230 if (fUseSplines) effB = fSplmvaCumB->Eval( cut );
231 else effB = fmvaBcumul->GetBinContent( fmvaBcumul->FindBin( cut ) );
234 effBvsS->SetBinContent( bini, effB );
235 rejBvsS->SetBinContent( bini, 1.0-effB );
236 if (effB>std::numeric_limits<double>::epsilon())
237 inveffBvsS->SetBinContent( bini, 1.0/effB );
246 Double_t effS = 0., rejB = 0., effS_ = 0., rejB_ = 0.;
248 for (
Int_t bini=1; bini<=nbins; bini++) {
251 effS = (bini - 0.5)/
Float_t(nbins);
252 rejB = 1.0 - fSpleffBvsS->Eval( effS );
255 if ((effS - rejB)*(effS_ - rejB_) < 0)
break;
260 fSignalCut = Root( 0.5*(effS + effS_) );
271 if (fSpleffBvsS == 0) this->GetROC();
275 for (
Int_t bini=1; bini<=nbins; bini++) {
278 effS = (bini - 0.5)/
Float_t(nbins);
279 effB = fSpleffBvsS->Eval( effS );
280 integral += (1.0 - effB);
294 Double_t effS=0., effB, effSOld=1., effBOld=0.;
296 if (fSpleffBvsS == 0) this->GetROC();
299 for (
Int_t bini=1; bini<=nbins; bini++) {
301 effS = (bini - 0.5)*step;
302 effB = fSpleffBvsS->Eval( effS );
305 if ((effB - effBref)*(effBOld - effBref) <= 0)
break;
311 effS = 0.5*(effS + effSOld);
314 if (fNevtS > 0) effSerr =
TMath::Sqrt( effS*(1.0 - effS)/fNevtS );
328 if (fUseSplines) retVal = fSplmvaCumS->Eval( theCut );
329 else retVal = fmvaScumul->GetBinContent( fmvaScumul->FindBin( theCut ) );
338 if (theCut-fXmin < eps) retVal = (fCutOrientation > 0) ? 1.0 : 0.0;
339 else if (fXmax-theCut < eps) retVal = (fCutOrientation > 0) ? 0.0 : 1.0;
351 Double_t fa = GetEffForRoot(
a ) - refValue;
352 Double_t fb = GetEffForRoot(
b ) - refValue;
354 Log() << kWARNING <<
"<ROCCalc::Root> initial interval w/o root: "
355 <<
"(a=" <<
a <<
", b=" <<
b <<
"),"
356 <<
" (Eff_a=" << GetEffForRoot(
a )
357 <<
", Eff_b=" << GetEffForRoot(
b ) <<
"), "
358 <<
"(fa=" << fa <<
", fb=" << fb <<
"), "
359 <<
"refValue = " << refValue <<
Endl;
366 for (
Int_t iter= 0; iter <= fMaxIter; iter++) {
367 if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
378 fa = fb; fb = fc; fc = fa;
392 if (ac_equal) {
p = 2 *
m * s;
q = 1 - s; }
394 q = fa / fc;
r = fb / fc;
395 p = s * (2 *
m *
q * (
q -
r) - (
b -
a) * (
r - 1));
396 q = (
q - 1) * (
r - 1) * (s - 1);
404 if (2 *
p < (min1 < min2 ? min1 : min2)) {
408 else {
d =
m;
e =
m; }
414 else b += (
m > 0 ? +tol : -tol);
416 fb = GetEffForRoot(
b ) - refValue;
421 Log() << kWARNING <<
"<ROCCalc::Root> maximum iterations (" << fMaxIter
422 <<
") reached before convergence" <<
Endl;
431 if (fnStot!=nStot || fnBtot!=nBtot || !fSignificance) {
432 GetSignificance(nStot, nBtot);
442 if (fnStot==nStot && fnBtot==nBtot && !fSignificance)
return fSignificance;
443 fnStot=nStot; fnBtot=nBtot;
445 fSignificance = (
TH1*) fmvaScumul->Clone(
"Significance"); fSignificance->SetTitle(
"Significance");
446 fSignificance->Reset(); fSignificance->SetFillStyle(0);
447 fSignificance->SetXTitle(
"mva cut value");
448 fSignificance->SetYTitle(
"Stat. significance S/Sqrt(S+B)");
449 fSignificance->SetLineColor(2);
450 fSignificance->SetLineWidth(5);
452 fPurity = (
TH1*) fmvaScumul->Clone(
"Purity"); fPurity->SetTitle(
"Purity");
453 fPurity->Reset(); fPurity->SetFillStyle(0);
454 fPurity->SetXTitle(
"mva cut value");
455 fPurity->SetYTitle(
"Purity: S/(S+B)");
456 fPurity->SetLineColor(3);
457 fPurity->SetLineWidth(5);
460 for (
Int_t i=1; i<=fSignificance->GetNbinsX(); i++) {
461 Double_t S = fmvaScumul->GetBinContent( i ) * nStot;
462 Double_t B = fmvaBcumul->GetBinContent( i ) * nBtot;
475 cout <<
"S="<<S<<
" B="<<B<<
" purity="<<purity<< endl;
476 fPurity->SetBinContent( i, purity );
477 fSignificance->SetBinContent( i, sig );
494 return fSignificance;
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
virtual void SetLineColor(Color_t lcolor)
Set the line color.
const char * GetTitle() const override
Returns title of object.
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
A TGraph is an object made of two arrays X and Y with npoints each.
1-D histogram with a double per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
void SetTitle(const char *title) override
Change/set the title.
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
virtual void SetXTitle(const char *title)
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
virtual Int_t GetNbinsX() const
virtual void SetMaximum(Double_t maximum=-1111)
virtual TH1 * RebinX(Int_t ngroup=2, const char *newname="")
virtual void SetYTitle(const char *title)
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
ostringstream derivative to redirect and format output
Double_t Root(Double_t)
Root finding using Brents algorithm; taken from CERNLIB function RZERO.
void ApplySignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *any=nullptr)
Double_t GetROCIntegral()
code to compute the area under the ROC ( rej-vs-eff ) curve
Float_t fXmax
min and max of the mva distribution
Bool_t fStatus
false if is found some error in mvaS or mvaB
Int_t fCutOrientation
+1 if larger mva value means more signal like, -1 otherwise
TH1 * GetPurity(Int_t nStot, Int_t nBtot)
Double_t GetEffSForEffBof(Double_t effBref, Double_t &effSerr)
get the signal efficiency for a particular background efficiency that will be the value of the effici...
ROCCalc(TH1 *mvaS, TH1 *mvaB)
TH1 * GetSignificance(Int_t nStot, Int_t nBtot)
MsgLogger & Log() const
message logger
TH1D * GetROC()
get the ROC curve
TH1 * fmvaB
the input mva distributions
TH1 * fmvaBpdf
the normalized (and rebinned) input mva distributions
Double_t GetEffForRoot(Double_t theCut)
returns efficiency as function of cut
Double_t fNevtS
number of signal events (used in error calculation)
Linear interpolation of TGraph.
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.