Logo ROOT  
Reference Guide
PDF.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Asen Christov, Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss , Jan Therhaag
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : PDF *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * PDF wrapper for histograms; uses user-defined spline interpolation *
12  * and kernel density estimation *
13  * *
14  * Authors (alphabetical): *
15  * Asen Christov <christov@physik.uni-freiburg.de> - Freiburg U., Germany *
16  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
17  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
19  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
20  * *
21  * Copyright (c) 2005-2011: *
22  * CERN, Switzerland *
23  * U. of Victoria, Canada *
24  * MPI-K Heidelberg, Germany *
25  * Freiburg U., Germany *
26  * U. of Bonn, Germany *
27  * *
28  * Redistribution and use in source and binary forms, with or without *
29  * modification, are permitted according to the terms listed in LICENSE *
30  * (http://tmva.sourceforge.net/LICENSE) *
31  **********************************************************************************/
32 
33 #ifndef ROOT_TMVA_PDF
34 #define ROOT_TMVA_PDF
35 
36 //////////////////////////////////////////////////////////////////////////
37 // //
38 // PDF //
39 // //
40 // PDF wrapper for histograms; uses user-defined spline interpolation //
41 // //
42 //////////////////////////////////////////////////////////////////////////
43 
44 #include <iosfwd>
45 
46 #include "TH1.h"
47 #include "ThreadLocalStorage.h"
48 #include "TMVA/KDEKernel.h"
49 #include "TMVA/Configurable.h"
50 
51 class TSpline;
52 class TGraph;
53 class TF1;
54 
55 namespace TMVA {
56 
57  class MsgLogger;
58 
59  class PDF;
60  std::ostream& operator<< ( std::ostream& os, const PDF& tree );
61  std::istream& operator>> ( std::istream& istr, PDF& tree);
62 
63  class PDF : public Configurable {
64 
65  friend std::ostream& operator<< ( std::ostream& os, const PDF& tree );
66  friend std::istream& operator>> ( std::istream& istr, PDF& tree);
67 
68  public:
69 
71 
72  explicit PDF( const TString& name, Bool_t norm=kTRUE );
73  explicit PDF( const TString& name, const TH1* theHist, EInterpolateMethod method = kSpline2,
74  Int_t minnsmooth = 0, Int_t maxnsmooth = 0, Bool_t checkHist = kFALSE, Bool_t norm=kTRUE );
75  explicit PDF( const TString& name, const TH1* theHist,
77  kborder, Float_t FineFactor, Bool_t norm=kTRUE );
78  explicit PDF( const TString& name, const TString& options, const TString& suffix = "", PDF* defaultPDF = 0, Bool_t norm=kTRUE);
79  virtual ~PDF();
80 
81  //creates the pdf after the definitions have been stored in
82  void BuildPDF (const TH1* theHist);
83 
84  // returns probability density at given abscissa
85  Double_t GetVal ( Double_t x ) const;
86  Double_t GetValInverse( Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE ) const;
87 
88  void AddXMLTo( void* parent );
89  void ReadXML( void* pdfnode );
90 
91  // histogram underlying the PDF
92  TH1* GetPDFHist() const { return fPDFHist; }
93  TGraph* GetGraph() const { return fGraph; }
94  TH1* GetOriginalHist() const { return fHistOriginal; }
95  TH1* GetSmoothedHist() const { return fHist; }
96  TH1* GetNSmoothHist() const { return fNSmoothHist; }
97 
98  // integral of PDF within given range
100 
101  // accessors
102  TSpline* GetSpline() const { return fSpline; }
103  Int_t GetNBins () const { return fHist->GetNbinsX(); }
104  Double_t GetXmin () const { return fHist->GetXaxis()->GetXmin(); }
105  Double_t GetXmax () const { return fHist->GetXaxis()->GetXmax(); }
106 
107  // perform series of validation tests
108  void ValidatePDF( TH1* original = 0 ) const;
109 
110  //gives the number of needed bins in the source histogram
111  Int_t GetHistNBins ( Int_t evtNum = 0 );
112 
114 
115  // modified name (remove TMVA::)
116  const char* GetName() const { return fPDFName; }
117 
118  // TMVA version control (for weight files)
121 
122  //void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
123  void ProcessOptions();
124 
125  // reads from and option string the definitions for pdf returns it
126  void DeclareOptions();
127 
128  private:
129 
130  // sanity check of PDF quality (after smoothing): comparison with
131  // original histogram
132  void CheckHist() const;
133  void FillSplineToHist();
134  void BuildKDEPDF();
135  void SmoothHistogram();
136  void FillHistToGraph();
137  Double_t GetIntegral() const;
139  TH1* h = GetPDFHist();
140  return (fPDFHist) ? (h->GetXaxis()->GetXmax() - h->GetXaxis()->GetXmin())/h->GetNbinsX() : 1;
141  }
142 
143  // do we use the original histogram as reference ?
144  Bool_t UseHistogram() const { return fUseHistogram; }
145 
146  void FindBinInverse( const TH1* histogram, Int_t& lowerBin, Int_t& higherBin, Double_t& lowerBinValue, Double_t& higherBinValue,
147  Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE ) const;
148 
149 
150  void BuildSplinePDF();
151 
152  // flag that indicates that no splines are produced and no smoothing
153  // is applied, i.e., the original histogram is used as reference
154  // this is useful for discrete variables
155  Bool_t fUseHistogram; // spline0 uses histogram as reference
156 
157  // static configuration variables ----------------------------
158  // to increase computation speed, the final PDF is filled in
159  // a high-binned histogram; "GetValue" then returns the histogram
160  // entry, linearized between adjacent bins
161  static const Int_t fgNbin_PdfHist; // number of bins in high-binned reference histogram
162  static const Bool_t fgManualIntegration; // manual integration (sum over bins) or DGAUSS
163  static const Double_t fgEpsilon; // minimum PDF return
164  // -----------------------------------------------------------
165 
166  TString fPDFName; // for output
167  Int_t fNsmooth; // Min number of smoothing iterations
168  Int_t fMinNsmooth; // Min number of smoothing iterations
169  Int_t fMaxNsmooth; // Max number of smoothing iterations
170  TH1* fNSmoothHist; // number of smooth for each bin
171 
173  TSpline* fSpline; //! the used spline type
174  TH1* fPDFHist; // the high-binned histogram corresponding to the PDF
175  TH1* fHist; // copy of input histogram
176  TH1* fHistOriginal; // the input histogram
177  TGraph* fGraph; //! needed to create PDF from histogram
178  TF1* fIGetVal; // integration interface
179 
180  Int_t fHistAvgEvtPerBin; // avg event per source hist bin
181  Int_t fHistDefinedNBins; // source hist bin num set by user
182 
183  TString fKDEtypeString; // strings used to read definitions
187 
188  KDEKernel::EKernelType fKDEtype; // Kernel type to use for KDE
189  KDEKernel::EKernelIter fKDEiter; // Number of iterations (adaptive or not)
190  KDEKernel::EKernelBorder fKDEborder; // The method to take care about "border" effects (string)
191  Float_t fFineFactor; // fine tuning factor for Adaptive KDE
192 
193  UInt_t fReadingVersion; // the TMVA version of the weight file
194 
195  Bool_t fCheckHist; // check of source histogram
196  Bool_t fNormalize; // normalize histogram (false for cumulative distribution used in GaussTranform)
197 
198  TString fSuffix; //! the suffix for options
199  mutable MsgLogger* fLogger; //! message logger
200  MsgLogger& Log() const { return *fLogger; }
201 
202  // static pointer to this object
203  // This is a workaround for OSx where static thread_local data members are
204  // not supported. The C++ solution would indeed be the following:
205  static PDF*& GetThisPdfThreadLocal() { TTHREAD_TLS(PDF*) fgThisPDF(nullptr); return fgThisPDF; };
206  static PDF* ThisPDF( void );
207 
208  // external auxiliary functions
209  static Double_t IGetVal( Double_t*, Double_t* );
210 
211  ClassDef(PDF,1) // PDF wrapper for histograms
212  };
213 
214 } // namespace TMVA
215 
216 #endif
TMVA::PDF::fKDEtype
KDEKernel::EKernelType fKDEtype
Definition: PDF.h:188
TMVA::PDF::BuildPDF
void BuildPDF(const TH1 *theHist)
Definition: PDF.cxx:260
TMVA::PDF::fKDEiter
KDEKernel::EKernelIter fKDEiter
Definition: PDF.h:189
TMVA::PDF::GetOriginalHist
TH1 * GetOriginalHist() const
Definition: PDF.h:94
TMVA::PDF::GetPdfHistBinWidth
Double_t GetPdfHistBinWidth() const
Definition: PDF.h:138
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TMVA::PDF::GetSmoothedHist
TH1 * GetSmoothedHist() const
Definition: PDF.h:95
TMVA::PDF::GetPDFHist
TH1 * GetPDFHist() const
Definition: PDF.h:92
TMVA::Configurable
Definition: Configurable.h:66
TMVA::PDF::fKDEborder
KDEKernel::EKernelBorder fKDEborder
Definition: PDF.h:190
TMVA::PDF::FillHistToGraph
void FillHistToGraph()
Simple conversion.
Definition: PDF.cxx:514
TMVA::PDF::fKDEtypeString
TString fKDEtypeString
Definition: PDF.h:183
TMVA::PDF::GetValInverse
Double_t GetValInverse(Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE) const
returns value
Definition: PDF.cxx:734
tree
Definition: tree.py:1
TMVA::PDF::fIGetVal
TF1 * fIGetVal
needed to create PDF from histogram
Definition: PDF.h:178
TMVA::PDF::fHistAvgEvtPerBin
Int_t fHistAvgEvtPerBin
Definition: PDF.h:180
TMVA::PDF::CheckHist
void CheckHist() const
sanity check: compare PDF with original histogram
Definition: PDF.cxx:553
xmax
float xmax
Definition: THbookFile.cxx:95
TMVA::PDF::ReadXML
void ReadXML(void *pdfnode)
XML file reading.
Definition: PDF.cxx:961
TMVA::PDF::operator<<
friend std::ostream & operator<<(std::ostream &os, const PDF &tree)
TMVA::PDF::fReadingVersion
UInt_t fReadingVersion
Definition: PDF.h:193
Float_t
float Float_t
Definition: RtypesCore.h:57
TMVA::PDF::GetIntegral
Double_t GetIntegral() const
computes normalisation
Definition: PDF.cxx:635
TMVA::PDF::Log
MsgLogger & Log() const
message logger
Definition: PDF.h:200
TMVA::PDF::fgEpsilon
static const Double_t fgEpsilon
Definition: PDF.h:163
TMVA::PDF::ThisPDF
static PDF * ThisPDF(void)
Definition: PDF.cxx:1136
TMVA::PDF::GetGraph
TGraph * GetGraph() const
Definition: PDF.h:93
x
Double_t x[n]
Definition: legend1.C:17
TMVA::PDF::SmoothHistogram
void SmoothHistogram()
Definition: PDF.cxx:455
TMVA::operator<<
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
TMVA::PDF::fPDFName
TString fPDFName
Definition: PDF.h:166
TSpline
Definition: TSpline.h:29
TMVA::PDF::fFineFactor
Float_t fFineFactor
Definition: PDF.h:191
TMVA::PDF::fKDEiterString
TString fKDEiterString
Definition: PDF.h:184
TMVA::PDF::fgNbin_PdfHist
static const Int_t fgNbin_PdfHist
Definition: PDF.h:161
TMVA::PDF::fUseHistogram
Bool_t fUseHistogram
Definition: PDF.h:155
TString
Definition: TString.h:136
TMVA::PDF::GetReadingVersion
UInt_t GetReadingVersion() const
Definition: PDF.h:120
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TMVA::operator>>
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
bool
TMVA::PDF::GetNBins
Int_t GetNBins() const
Definition: PDF.h:103
TMVA::PDF::PDF
PDF(const TString &name, Bool_t norm=kTRUE)
default constructor needed for ROOT I/O
Definition: PDF.cxx:69
TMVA::PDF::fgManualIntegration
static const Bool_t fgManualIntegration
Definition: PDF.h:162
TMVA::PDF::fInterpolMethod
TMVA::PDF::EInterpolateMethod fInterpolMethod
Definition: PDF.h:172
TMVA::PDF::fCheckHist
Bool_t fCheckHist
Definition: PDF.h:195
TMVA::PDF::GetNSmoothHist
TH1 * GetNSmoothHist() const
Definition: PDF.h:96
TAxis::GetXmin
Double_t GetXmin() const
Definition: TAxis.h:133
TMVA::PDF::GetXmin
Double_t GetXmin() const
Definition: PDF.h:104
TMVA::PDF::fMinNsmooth
Int_t fMinNsmooth
Definition: PDF.h:168
xmin
float xmin
Definition: THbookFile.cxx:95
h
#define h(i)
Definition: RSha256.hxx:124
TMVA::PDF::fNsmooth
Int_t fNsmooth
Definition: PDF.h:167
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TMVA::PDF::kSpline2
@ kSpline2
Definition: PDF.h:70
TMVA::PDF::GetHistNBins
Int_t GetHistNBins(Int_t evtNum=0)
Definition: PDF.cxx:304
TMVA::PDF::kSpline5
@ kSpline5
Definition: PDF.h:70
TMVA::KDEKernel::EKernelBorder
EKernelBorder
Definition: KDEKernel.h:56
TMVA::PDF::kKDE
@ kKDE
Definition: PDF.h:70
TMVA::PDF::GetName
const char * GetName() const
Returns name of object.
Definition: PDF.h:116
TMVA::PDF::GetXmax
Double_t GetXmax() const
Definition: PDF.h:105
TMVA::PDF::UseHistogram
Bool_t UseHistogram() const
Definition: PDF.h:144
TMVA::PDF::fHistOriginal
TH1 * fHistOriginal
Definition: PDF.h:176
TMVA::PDF::GetThisPdfThreadLocal
static PDF *& GetThisPdfThreadLocal()
Definition: PDF.h:205
y
Double_t y[n]
Definition: legend1.C:17
TMVA::PDF::BuildSplinePDF
void BuildSplinePDF()
build the PDF from the original histograms
Definition: PDF.cxx:321
TMVA::PDF::DeclareOptions
void DeclareOptions()
define the options (their key words) that can be set in the option string
Definition: PDF.cxx:820
Configurable.h
unsigned int
TMVA::PDF::fNormalize
Bool_t fNormalize
Definition: PDF.h:196
TMVA::PDF
Definition: PDF.h:63
TMVA::PDF::fHist
TH1 * fHist
Definition: PDF.h:175
KDEKernel.h
TMVA::PDF::fSuffix
TString fSuffix
Definition: PDF.h:198
TMVA::PDF::SetReadingVersion
void SetReadingVersion(UInt_t rv)
Definition: PDF.h:119
TMVA::PDF::kSpline1
@ kSpline1
Definition: PDF.h:70
TMVA::PDF::fLogger
MsgLogger * fLogger
the suffix for options
Definition: PDF.h:199
TMVA::PDF::GetSpline
TSpline * GetSpline() const
Definition: PDF.h:102
TMVA::PDF::GetInterpolMethod
TMVA::PDF::EInterpolateMethod GetInterpolMethod()
Definition: PDF.h:113
Double_t
double Double_t
Definition: RtypesCore.h:59
TGraph
Definition: TGraph.h:41
TMVA::MsgLogger
Definition: MsgLogger.h:83
TMVA::PDF::ValidatePDF
void ValidatePDF(TH1 *original=0) const
comparison of original histogram with reference PDF
Definition: PDF.cxx:578
TMVA::PDF::IGetVal
static Double_t IGetVal(Double_t *, Double_t *)
static external auxiliary function (integrand)
Definition: PDF.cxx:646
TMVA::PDF::BuildKDEPDF
void BuildKDEPDF()
creates high-binned reference histogram to be used instead of the PDF for speed reasons
Definition: PDF.cxx:387
TMVA::PDF::kSpline0
@ kSpline0
Definition: PDF.h:70
TMVA::PDF::fHistDefinedNBins
Int_t fHistDefinedNBins
Definition: PDF.h:181
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TH1
Definition: TH1.h:57
name
char name[80]
Definition: TGX11.cxx:110
TMVA::PDF::fGraph
TGraph * fGraph
Definition: PDF.h:177
TMVA::PDF::fInterpolateString
TString fInterpolateString
Definition: PDF.h:186
TMVA::PDF::fNSmoothHist
TH1 * fNSmoothHist
Definition: PDF.h:170
TMVA::PDF::kSpline3
@ kSpline3
Definition: PDF.h:70
TMVA::PDF::FindBinInverse
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
Definition: PDF.cxx:761
TMVA::PDF::AddXMLTo
void AddXMLTo(void *parent)
XML file writing.
Definition: PDF.cxx:919
TMVA::PDF::FillSplineToHist
void FillSplineToHist()
creates high-binned reference histogram to be used instead of the PDF for speed reasons
Definition: PDF.cxx:523
TMVA::PDF::fPDFHist
TH1 * fPDFHist
the used spline type
Definition: PDF.h:174
TAxis::GetXmax
Double_t GetXmax() const
Definition: TAxis.h:134
TF1
1-Dim function class
Definition: TF1.h:212
TMVA::PDF::ProcessOptions
void ProcessOptions()
Definition: PDF.cxx:870
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:317
TMVA::PDF::GetVal
Double_t GetVal(Double_t x) const
returns value PDF(x)
Definition: PDF.cxx:701
TMVA::PDF::operator>>
friend std::istream & operator>>(std::istream &istr, PDF &tree)
TH1.h
TMVA::PDF::fBorderMethodString
TString fBorderMethodString
Definition: PDF.h:185
TMVA::PDF::~PDF
virtual ~PDF()
Definition: PDF.cxx:246
ThreadLocalStorage.h
TMVA::PDF::fMaxNsmooth
Int_t fMaxNsmooth
Definition: PDF.h:169
TMVA::KDEKernel::EKernelType
EKernelType
Definition: KDEKernel.h:54
TH1::GetNbinsX
virtual Int_t GetNbinsX() const
Definition: TH1.h:293
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
TMVA::PDF::fSpline
TSpline * fSpline
Definition: PDF.h:173
int
TMVA::KDEKernel::EKernelIter
EKernelIter
Definition: KDEKernel.h:55
TMVA::PDF::EInterpolateMethod
EInterpolateMethod
Definition: PDF.h:70