* *
**********************************************************************************/
#include "Riostream.h"
#include "TMVA/PDF.h"
#include "TMVA/TSpline1.h"
#include "TMVA/TSpline2.h"
#include "TH1F.h"
#include "TH1D.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::SmoothMethod method, Int_t nsmooth )
: fNsmooth ( nsmooth ),
fSpline ( 0 ),
fPDFHist ( 0 ),
fHist ( 0 ),
fGraph ( 0 ),
fIntegral( 1.0)
{
if (hist == NULL) {
cout << "--- TMVA::PDF: ERROR!!! Called without valid histogram pointer!" << endl;
exit(1);
}
fNbinsPDFHist = NBIN_PdfHist_;
fHist = (TH1*)hist->Clone();
CheckHist();
if (fNsmooth >0) fHist->Smooth( fNsmooth );
fGraph = new TGraph( hist );
switch (method) {
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:
cout << "--- " << GetName()
<< ": Warning no valid interpolation method given! Use Spline3" << endl;
fSpline = new TSpline3 ( "spline3", fGraph );
}
FillSplineToHist();
fSpline->SetTitle( (TString)hist->GetTitle() + fSpline->GetTitle() );
fSpline->SetName ( (TString)hist->GetName() + fSpline->GetName() );
Integral();
fPDFHist->Scale( 1.0/fIntegral );
}
TMVA::PDF::~PDF( void )
{
if (fSpline != NULL) delete fSpline ;
if (fHist != NULL) delete fHist;
if (fPDFHist != NULL) delete fPDFHist;
}
void TMVA::PDF::FillSplineToHist( void )
{
fPDFHist = new TH1D( "", "", 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) {
cout << "--- " << GetName()
<< ": CheckHist: ERROR!!! Called without valid histogram pointer!" << endl;
exit(1);
}
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) {
cout << "--- " << GetName()
<< ": WARNING More than 50% ("<<(((Float_t)emptyBins/(Float_t)nbins)*100)
<<"%) of the bins in hist '"
<< fHist->GetName() << "' are empty!" << endl;
cout << "--- " << GetName()
<< ": X_min=" << fXmin
<< " mean=" << fHist->GetMean() << " X_max= " << fXmax << endl;
}
if (DEBUG_PDF)
cout << "--- " << GetName() << ": "
<< fXmin << " < x < " << fXmax << " in " << nbins << " bins" << endl;
}
Double_t TMVA::PDF::Integral( void )
{
return GetIntegral( fXmin, fXmax );
}
Double_t TMVA::PDF::GetIntegral( Double_t xmin, Double_t xmax )
{
Double_t integral = 0;
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( const Double_t x )
{
Int_t bin = fPDFHist->FindBin(x);
if (bin < 1 ) bin = 1;
else if (bin > fNbinsPDFHist) bin = fNbinsPDFHist;
Int_t nextbin = bin;
if ((x > fPDFHist->GetBinCenter(bin) && bin != fNbinsPDFHist) || bin == 1)
nextbin++;
else
nextbin--;
if (fIntegral <= 0.0) fIntegral = 1.0;
Double_t dx = fPDFHist->GetBinCenter(bin) - fPDFHist->GetBinCenter(nextbin);
Double_t dy = fPDFHist->GetBinContent(bin) - fPDFHist->GetBinContent(nextbin);
Double_t retval = fPDFHist->GetBinContent(bin) + (x - fPDFHist->GetBinCenter(bin))*dy/dx;
return 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.