#include "Riostream.h"
#include "TMVA/PDF.h"
#include "TMVA/TSpline1.h"
#include "TMVA/TSpline2.h"
#include "TH1F.h"
namespace TMVA {
const Bool_t DEBUG_PDF=kFALSE;
const Double_t PDF_epsilon_=1.0e-02;
const Int_t NBIN_PdfHist_=10000;
}
using namespace std;
ClassImp(TMVA::PDF)
;
TMVA::PDF::PDF( const TH1 *hist, TMVA::PDF::ESmoothMethod method, Int_t nsmooth )
: fUseHistogram( kFALSE ),
fNsmooth ( nsmooth ),
fSpline ( 0 ),
fPDFHist ( 0 ),
fHist ( 0 ),
fGraph ( 0 ),
fLogger ( this )
{
if (hist == NULL) fLogger << kFATAL << "called without valid histogram pointer!" << Endl;
fNbinsPDFHist = NBIN_PdfHist_;
fHist = (TH1*)hist->Clone();
if (method != TMVA::PDF::kSpline0) CheckHist();
if (fNsmooth > 0) fHist->Smooth( fNsmooth );
fGraph = new TGraph( hist );
switch (method) {
case TMVA::PDF::kSpline0:
fUseHistogram = kTRUE;
break;
case TMVA::PDF::kSpline1:
fSpline = new TMVA::TSpline1( "spline1", fGraph );
break;
case TMVA::PDF::kSpline2:
fSpline = new TMVA::TSpline2( "spline2", fGraph );
break;
case TMVA::PDF::kSpline3:
fSpline = new TSpline3( "spline3", fGraph );
break;
case TMVA::PDF::kSpline5:
fSpline = new TSpline5( "spline5", fGraph );
break;
default:
fLogger << kWARNING << "no valid interpolation method given! Use Spline3" << Endl;
fSpline = new TMVA::TSpline2( "spline2", fGraph );
}
FillSplineToHist();
if (!UseHistogram()) {
fSpline->SetTitle( (TString)fHist->GetTitle() + fSpline->GetTitle() );
fSpline->SetName ( (TString)fHist->GetName() + fSpline->GetName() );
}
Double_t integral = GetIntegral();
if (integral < 0) fLogger << kFATAL << "integral: " << integral << " <= 0" << Endl;
if(integral>0) fPDFHist->Scale( 1.0/integral );
}
TMVA::PDF::~PDF( void )
{
if (fSpline != NULL) delete fSpline ;
if (fHist != NULL) delete fHist;
if (fPDFHist != NULL) delete fPDFHist;
}
void TMVA::PDF::FillSplineToHist( void )
{
if (UseHistogram()) {
fPDFHist = (TH1*)fHist->Clone();
fPDFHist->SetTitle( (TString)fHist->GetTitle() + "_hist from_spline0" );
fPDFHist->SetName ( (TString)fHist->GetName() + "_hist_from_spline0" );
}
else {
fPDFHist = new TH1F( "", "", fNbinsPDFHist, fXmin, fXmax );
fPDFHist->SetTitle( (TString)fHist->GetTitle() + "_hist from_" + fSpline->GetTitle() );
fPDFHist->SetName ( (TString)fHist->GetName() + "_hist_from_" + fSpline->GetTitle() );
for (Int_t bin=1; bin <= fNbinsPDFHist; bin++) {
Double_t x = fPDFHist->GetBinCenter( bin );
Double_t y = fSpline->Eval( x );
if (y <= TMVA::PDF_epsilon_) y = fHist->GetBinContent( fHist->FindBin( x ) );
fPDFHist->SetBinContent( bin, TMath::Max(y, TMVA::PDF_epsilon_) );
}
}
}
void TMVA::PDF::CheckHist(void)
{
if (fHist == NULL) {
fLogger << kFATAL << "<CheckHist> called without valid histogram pointer!" << Endl;
}
fXmin = fHist->GetXaxis()->GetXmin();
fXmax = fHist->GetXaxis()->GetXmax();
Int_t nbins = fHist->GetNbinsX();
Int_t emptyBins=0;
for(Int_t bin=1; bin<=nbins; bin++)
if (fHist->GetBinContent(bin) == 0) emptyBins++;
if (((Float_t)emptyBins/(Float_t)nbins) > 0.5) {
fLogger << kWARNING << "more than 50% (" << (((Float_t)emptyBins/(Float_t)nbins)*100)
<<"%) of the bins in hist '"
<< fHist->GetName() << "' are empty!" << Endl;
fLogger << kWARNING << "X_min=" << fXmin
<< " mean=" << fHist->GetMean() << " X_max= " << fXmax << Endl;
}
if (DEBUG_PDF)
fLogger << kINFO << fXmin << " < x < " << fXmax << " in " << nbins << " bins" << Endl;
}
Double_t TMVA::PDF::GetIntegral( void )
{
return GetIntegral( fXmin, fXmax );
}
Double_t TMVA::PDF::GetIntegral( Double_t xmin, Double_t xmax )
{
Double_t integral = 0;
if (UseHistogram()) {
integral = fPDFHist->GetEntries();
}
else {
Int_t nsteps = 10000;
Double_t intBin = (xmax - xmin)/nsteps;
for (Int_t bini=0; bini < nsteps; bini++) {
Double_t x = (bini + 0.5)*intBin + xmin;
integral += GetVal( x );
}
integral *= intBin;
}
return integral;
}
Double_t TMVA::PDF::GetVal( Double_t x )
{
Int_t bin = fPDFHist->FindBin(x);
if (bin < 1 ) bin = 1;
else if (bin > fNbinsPDFHist) bin = fNbinsPDFHist;
Double_t retval = 0;
if (UseHistogram()) {
retval = fPDFHist->GetBinContent( bin );
}
else {
Int_t nextbin = bin;
if ((x > fPDFHist->GetBinCenter(bin) && bin != fNbinsPDFHist) || bin == 1)
nextbin++;
else
nextbin--;
Double_t dx = fPDFHist->GetBinCenter( bin ) - fPDFHist->GetBinCenter( nextbin );
Double_t dy = fPDFHist->GetBinContent( bin ) - fPDFHist->GetBinContent( nextbin );
retval = fPDFHist->GetBinContent( bin ) + (x - fPDFHist->GetBinCenter( bin ))*dy/dx;
}
return TMath::Max( retval, TMVA::PDF_epsilon_ );
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.