228 if (defaultPDF != 0) {
263 if (hist == NULL)
Log() << kFATAL <<
"Called without valid histogram pointer!" <<
Endl;
267 Log() << kFATAL <<
"Number of entries <= 0 (" << hist->
GetEntries() <<
" in histogram: " << hist->
GetTitle() <<
")" <<
Endl;
270 Log()<< kDEBUG <<
"Create "
274 <<
"type KDE kernel for histogram: \"" << hist->
GetName() <<
"\""
280 Log() << kFATAL <<
"PDF construction called with minnsmooth<0" <<
Endl;
284 Log() << kFATAL <<
"PDF construction called with maxnsmooth<minnsmooth" <<
Endl;
294 fHist ->SetDirectory(
nullptr);
307 Log() << kFATAL <<
"No number of bins set for PDF" <<
Endl;
357 Log() << kWARNING <<
"No valid interpolation method given! Use Spline2" <<
Endl;
359 Log() << kFATAL <<
" Well.. .thinking about it, I better quit so you notice you are forced to fix the mistake " <<
Endl;
373 if (integral < 0)
Log() << kFATAL <<
"Integral: " << integral <<
" <= 0" <<
Endl;
377 if (integral>0)
fPDFHist->Scale( 1.0/integral );
396 fHist, histoLowEdge, histoUpperEdge,
407 fHist->GetBinCenter(i),
415 if (i < fHist->GetNbinsX()/5 ) {
421 2*histoLowEdge-
fHist->GetBinCenter(i),
426 if (i > 4*
fHist->GetNbinsX()/5) {
432 2*histoUpperEdge-
fHist->GetBinCenter(i), i) );
444 if (integral < 0)
Log() << kFATAL <<
"Integral: " << integral <<
" <= 0" <<
Endl;
448 if (integral>0)
fPDFHist->Scale( 1.0/integral );
456 if(
fHist->GetNbinsX()==1)
return;
464 Float_t Err=0, ErrAvg=0, ErrRMS=0 ;
Int_t num=0, smooth;
465 for (
Int_t bin=0; bin<
fHist->GetNbinsX(); bin++) {
466 if (
fHist->GetBinContent(bin+1) <=
fHist->GetBinError(bin+1))
continue;
467 Err =
fHist->GetBinError(bin+1) /
fHist->GetBinContent(bin+1);
468 ErrAvg += Err; ErrRMS += Err*Err; num++;
474 Float_t MaxErr=ErrAvg+ErrRMS, MinErr=ErrAvg-ErrRMS;
478 for (
Int_t bin=0; bin<
fHist->GetNbinsX(); bin++) {
479 if (
fHist->GetBinContent(bin+1) <=
fHist->GetBinError(bin+1))
482 Err =
fHist->GetBinError(bin+1) /
fHist->GetBinContent(bin+1);
494 Int_t MinBin=-1,MaxBin =-1;
495 for (
Int_t bin=0; bin <
fHist->GetNbinsX(); bin++) {
497 if (MinBin==-1) MinBin = bin;
500 else if (MaxBin >= 0) {
501 fHist->Smooth(1,
"R");
555 Log() << kFATAL <<
"<CheckHist> Called without valid histogram pointer!" <<
Endl;
562 for (
Int_t bin=1; bin<=nbins; bin++)
563 if (
fHist->GetBinContent(bin) == 0) emptyBins++;
566 Log() << kWARNING <<
"More than 50% (" << (((
Float_t)emptyBins/(
Float_t)nbins)*100)
567 <<
"%) of the bins in hist '"
568 <<
fHist->GetName() <<
"' are empty!" <<
Endl;
595 for (
Int_t bin=1; bin<=nbins; bin++) {
601 if (binPdfHist<0)
continue;
613 if (
d > 1) { nc1++;
if (
d > 2) { nc2++;
if (
d > 3) { nc3++;
if (
d > 6) nc6++; } } }
617 Log() << kDEBUG <<
"Validation result for PDF \"" << originalHist->
GetTitle() <<
"\"" <<
": " <<
Endl;
620 if ((1.0 -
TMath::Prob( chi2, ndof )) > 0.9999994) {
621 Log() << kDEBUG <<
"Comparison of the original histogram \"" << originalHist->
GetTitle() <<
"\"" <<
Endl;
622 Log() << kDEBUG <<
"with the corresponding PDF gave a chi2/ndof of " << chi2/ndof <<
"," <<
Endl;
623 Log() << kDEBUG <<
"which corresponds to a deviation of more than 5 sigma! Please check!" <<
Endl;
625 Log() << kDEBUG <<
TString::Format(
" #bins-found(#expected-bins) deviating > [1,2,3,6] sigmas: " \
626 "[%i(%i),%i(%i),%i(%i),%i(%i)]",
662 if (imin < 1) imin = 1;
665 for (
Int_t bini = imin; bini <= imax; bini++) {
668 if (bini == imin) dx =
fPDFHist->GetBinLowEdge(bini+1) -
xmin;
669 else if (bini == imax) dx =
xmax -
fPDFHist->GetBinLowEdge(bini);
670 if (dx < 0 && dx > -1.0e-8) dx = 0;
673 <<
"dx = " << dx << std::endl
674 <<
"bini = " << bini << std::endl
675 <<
"xmin = " <<
xmin << std::endl
676 <<
"xmax = " <<
xmax << std::endl
677 <<
"imin = " << imin << std::endl
678 <<
"imax = " << imax << std::endl
679 <<
"low edge of imin" <<
fPDFHist->GetBinLowEdge(imin) << std::endl
680 <<
"low edge of imin+1" <<
fPDFHist->GetBinLowEdge(imin+1) <<
Endl;
681 Log() << kFATAL <<
"<GetIntegral> dx = " << dx <<
" < 0" <<
Endl;
683 integral +=
fPDFHist->GetBinContent(bini)*dx;
711 retval =
fPDFHist->GetBinContent( bin );
716 if ((
x >
fPDFHist->GetBinCenter(bin) && bin !=
fPDFHist->GetNbinsX()) || bin == 1)
724 retval =
fPDFHist->GetBinContent( bin ) + (
x -
fPDFHist->GetBinCenter( bin ))*dy/dx;
735 Int_t lowerBin=0, higherBin=0;
736 Double_t lowerBinValue=0, higherBinValue=0;
742 Double_t length =(higherBinValue-lowerBinValue);
745 fraction=(
y-lowerBinValue)/length;
747 Double_t lengthX =xValueHigherBin-xValueLowerBin;
748 Double_t x =xValueLowerBin+lengthX*fraction;
763 if (isMonotonouslyIncreasingFunction) {
767 Int_t bin=higherBin/2;
769 while (bin>lowerBin && bin<higherBin) {
774 higherBinValue=binContent;
776 else if (
y>=binContent){
778 lowerBinValue =binContent;
780 bin=lowerBin+(higherBin-lowerBin)/2;
785 for (
Int_t bin=0, binEnd=histogram->
GetNbinsX(); bin<binEnd; ++bin) {
790 lowerBinValue =binContent;
791 higherBinValue=binContent;
795 higherBinValue=binContent;
822 "Number of smoothing iterations for the input histograms" );
824 "Min number of smoothing iterations, for bins with most data" );
827 "Max number of smoothing iterations, for bins with least data" );
830 "Average number of events per PDF bin" );
833 "Defined number of bins for the histogram from which the PDF is created" );
836 "Whether or not to check the source histogram of the PDF" );
839 "Interpolation method for reference histograms (e.g. Spline2 or KDE)" );
855 "Fine tuning factor for Adaptive KDE: Factor to multiply the width of the kernel");
858 "Border effects treatment (1=no treatment , 2=kernel renormalization, 3=sample mirroring)" );
877 if (fMaxNsmooth < fMinNsmooth && fMinNsmooth >= 0) {
878 Log() << kFATAL <<
"ERROR: MaxNsmooth = "
883 Log() << kFATAL <<
"ERROR: MaxNsmooth = "
936 gTools().
AddAttr(pdfhist,
"HasEquidistantBins", hasEquidistantBinning );
945 if (!hasEquidistantBinning) {
946 void* pdfhistbins =
gTools().
AddChild(pdfxml,
"HistogramBinning" );
975 Bool_t hasEquidistantBinning;
982 gTools().
ReadAttr( histch,
"HasEquidistantBins", hasEquidistantBinning );
986 if (hasEquidistantBinning) {
990 std::stringstream s(content);
992 for (
UInt_t i=0; i<nbins; i++) {
999 std::stringstream s(content);
1005 if (nbinning != nbins) {
1006 Log() << kFATAL <<
"Number of bins in content and binning array differs"<<
Endl;
1009 std::stringstream sb(binString);
1010 for (
UInt_t i=0; i<=nbins; i++) sb >> binns[i];
1013 for (
UInt_t i=0; i<nbins; i++) {
1020 hnameSmooth.
ReplaceAll(
"_original",
"_smoothed" );
1025 fHist->SetTitle( hnameSmooth );
1026 fHist->SetDirectory(
nullptr);
1037 Int_t dp = os.precision();
1038 os <<
"MinNSmooth " << pdf.
fMinNsmooth << std::endl;
1039 os <<
"MaxNSmooth " << pdf.
fMaxNsmooth << std::endl;
1041 os <<
"KDE_type " << pdf.
fKDEtype << std::endl;
1042 os <<
"KDE_iter " << pdf.
fKDEiter << std::endl;
1043 os <<
"KDE_border " << pdf.
fKDEborder << std::endl;
1044 os <<
"KDE_finefactor " << pdf.
fFineFactor << std::endl;
1059 os <<
"Weights " << std::endl;
1060 os << std::setprecision(8);
1061 for (
Int_t i=0; i<nBins; i++) {
1062 os << std::setw(15) << std::left << histToWrite->
GetBinContent(i+1) << std::right <<
" ";
1063 if ((i+1)%5==0) os << std::endl;
1066 os << std::setprecision(dp);
1079 TString hname=
"_original";
1081 while (!doneReading) {
1083 if (devnullS==
"NSmooth")
1084 {istr >> pdf.fMinNsmooth; pdf.fMaxNsmooth=pdf.fMinNsmooth;}
1085 else if (devnullS==
"MinNSmooth") istr >> pdf.fMinNsmooth;
1086 else if (devnullS==
"MaxNSmooth") istr >> pdf.fMaxNsmooth;
1092 else if (devnullS ==
"KDE_finefactor") {
1093 istr >> pdf.fFineFactor;
1094 if (pdf.GetReadingVersion() != 0 && pdf.GetReadingVersion() <
TMVA_VERSION(3,7,3)) {
1097 doneReading =
kTRUE;
1100 else if (devnullS ==
"Histogram") { istr >> hname >> nbins >>
xmin >>
xmax; }
1101 else if (devnullS ==
"Weights") { doneReading =
kTRUE; }
1104 TString hnameSmooth = hname;
1105 hnameSmooth.
ReplaceAll(
"_original",
"_smoothed" );
1109 std::cout <<
"PDF, trying to create a histogram without defined binning"<< std::endl;
1112 TH1* newhist =
new TH1F( hname,hname, nbins,
xmin,
xmax );
1115 for (
Int_t i=0; i<nbins; i++) {
1120 if (pdf.fHistOriginal != 0)
delete pdf.fHistOriginal;
1121 pdf.fHistOriginal = newhist;
1122 pdf.fHist = (TH1F*)pdf.fHistOriginal->
Clone( hnameSmooth );
1123 pdf.fHist->SetTitle( hnameSmooth );
1124 pdf.fHist->SetDirectory(
nullptr);
1126 if (pdf.fMinNsmooth>=0) pdf.BuildSplinePDF();
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
double Double_t
Double 8 bytes.
float Float_t
Float 4 bytes (float).
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
TVectorT< Double_t > TVectorD
#define TMVA_VERSION(a, b, c)
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
1-D histogram with a float per channel (see TH1 documentation)
1-D histogram with an int per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
virtual Double_t GetSumOfWeights() const
Return the sum of weights across all bins excluding under/overflows.
virtual Int_t GetNbinsX() const
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
virtual Double_t GetEntries() const
Return the current number of entries.
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
virtual TArrayD * GetSumw2()
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
void SetConfigDescription(const char *d)
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
void AddPreDefVal(const T &)
void SetConfigName(const char *n)
Configurable(const TString &theOption="")
constructor
KDE Kernel for "smoothing" the PDFs.
void SetKernelType(EKernelType ktype=kGauss)
fIter == 1 ---> nonadaptive KDE fIter == 2 ---> adaptive KDE
Float_t GetBinKernelIntegral(Float_t lowr, Float_t highr, Float_t mean, Int_t binnum)
calculates the integral of the Kernel
ostringstream derivative to redirect and format output
PDF wrapper for histograms; uses user-defined spline interpolation.
static const Bool_t fgManualIntegration
manual integration (sum over bins) or DGAUSS
KDEKernel::EKernelType fKDEtype
Kernel type to use for KDE.
void FillHistToGraph()
Simple conversion.
void BuildPDF(const TH1 *theHist)
KDEKernel::EKernelBorder fKDEborder
The method to take care about "border" effects (string).
const char * GetName() const override
Returns name of object.
static Double_t IGetVal(Double_t *, Double_t *)
static external auxiliary function (integrand)
TString fBorderMethodString
Int_t fMaxNsmooth
Max number of smoothing iterations.
TH1 * fPDFHist
the high-binned histogram corresponding to the PDF
Bool_t UseHistogram() const
void ValidatePDF(TH1 *original=nullptr) const
comparison of original histogram with reference PDF
TMVA::PDF::EInterpolateMethod fInterpolMethod
interpolation method
TSpline * fSpline
! the used spline type
UInt_t fReadingVersion
the TMVA version of the weight file
TGraph * fGraph
! needed to create PDF from histogram
Double_t GetValInverse(Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE) const
returns value
Double_t GetPdfHistBinWidth() const
void CheckHist() const
sanity check: compare PDF with original histogram
void FindBinInverse(const TH1 *histogram, Int_t &lowerBin, Int_t &higherBin, Double_t &lowerBinValue, Double_t &higherBinValue, Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE) const
find bin from value on ordinate
Int_t fHistDefinedNBins
source hist bin num set by user
TString fPDFName
for output
void ReadXML(void *pdfnode)
XML file reading.
TString fInterpolateString
Int_t fNsmooth
Min number of smoothing iterations.
void DeclareOptions()
define the options (their key words) that can be set in the option string
TF1 * fIGetVal
integration interface
void AddXMLTo(void *parent)
XML file writing.
void BuildSplinePDF()
build the PDF from the original histograms
TH1 * fNSmoothHist
number of smooth for each bin
void FillSplineToHist()
creates high-binned reference histogram to be used instead of the PDF for speed reasons
void BuildKDEPDF()
creates high-binned reference histogram to be used instead of the PDF for speed reasons
Int_t fMinNsmooth
Min number of smoothing iterations.
Int_t GetHistNBins(Int_t evtNum=0)
static const Double_t fgEpsilon
minimum PDF return
TH1 * GetOriginalHist() const
Float_t fFineFactor
fine tuning factor for Adaptive KDE
static PDF *& GetThisPdfThreadLocal()
Double_t GetVal(Double_t x) const
returns value PDF(x)
Int_t fHistAvgEvtPerBin
avg event per source hist bin
Double_t GetIntegral() const
computes normalisation
Bool_t fCheckHist
check of source histogram
MsgLogger * fLogger
! message logger
TString fKDEtypeString
strings used to read definitions
Bool_t fNormalize
normalize histogram (false for cumulative distribution used in GaussTranform)
TString fSuffix
! the suffix for options
Double_t GetIntegral(Double_t xmin, Double_t xmax)
computes PDF integral within given ranges
TH1 * fHistOriginal
the input histogram
static const Int_t fgNbin_PdfHist
number of bins in high-binned reference histogram
PDF(const TString &name, Bool_t norm=kTRUE)
default constructor needed for ROOT I/O
KDEKernel::EKernelIter fKDEiter
Number of iterations (adaptive or not).
TH1 * fHist
copy of input histogram
static PDF * ThisPDF(void)
Linear interpolation of TGraph.
Quadratic interpolation of TGraph.
const char * GetName() const override
Returns name of object.
const char * GetTitle() const override
Returns title of object.
Class to create third splines to interpolate knots Arbitrary conditions can be introduced for first a...
Class to create quintic natural splines to interpolate knots Arbitrary conditions can be introduced f...
TString & ReplaceAll(const TString &s1, const TString &s2)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Element * GetMatrixArray()
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Prob(Double_t chi2, Int_t ndf)
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.