Logo ROOT   6.08/07
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 #ifndef ROOT_TH1
47 #include "TH1.h"
48 #endif
49 #ifndef ROOT_ThreadLocalStorage
50 #include "ThreadLocalStorage.h"
51 #endif
52 #ifndef ROOT_TMVA_KDEKernel
53 #include "TMVA/KDEKernel.h"
54 #endif
55 #ifndef ROOT_TMVA_Configurable
56 #include "TMVA/Configurable.h"
57 #endif
58 
59 class TSpline;
60 class TGraph;
61 class TF1;
62 
63 namespace TMVA {
64 
65  class MsgLogger;
66 
67  class PDF;
68  std::ostream& operator<< ( std::ostream& os, const PDF& tree );
69  std::istream& operator>> ( std::istream& istr, PDF& tree);
70 
71  class PDF : public Configurable {
72 
73  friend std::ostream& operator<< ( std::ostream& os, const PDF& tree );
74  friend std::istream& operator>> ( std::istream& istr, PDF& tree);
75 
76  public:
77 
79 
80  explicit PDF( const TString& name, Bool_t norm=kTRUE );
81  explicit PDF( const TString& name, const TH1* theHist, EInterpolateMethod method = kSpline2,
82  Int_t minnsmooth = 0, Int_t maxnsmooth = 0, Bool_t checkHist = kFALSE, Bool_t norm=kTRUE );
83  explicit PDF( const TString& name, const TH1* theHist,
85  kborder, Float_t FineFactor, Bool_t norm=kTRUE );
86  explicit PDF( const TString& name, const TString& options, const TString& suffix = "", PDF* defaultPDF = 0, Bool_t norm=kTRUE);
87  virtual ~PDF();
88 
89  //creates the pdf after the definitions have been stored in
90  void BuildPDF (const TH1* theHist);
91 
92  // returns probability density at given abscissa
93  Double_t GetVal ( Double_t x ) const;
94  Double_t GetValInverse( Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE ) const;
95 
96  void AddXMLTo( void* parent );
97  void ReadXML( void* pdfnode );
98 
99  // histogram underlying the PDF
100  TH1* GetPDFHist() const { return fPDFHist; }
101  TGraph* GetGraph() const { return fGraph; }
102  TH1* GetOriginalHist() const { return fHistOriginal; }
103  TH1* GetSmoothedHist() const { return fHist; }
104  TH1* GetNSmoothHist() const { return fNSmoothHist; }
105 
106  // integral of PDF within given range
108 
109  // accessors
110  TSpline* GetSpline() const { return fSpline; }
111  Int_t GetNBins () const { return fHist->GetNbinsX(); }
112  Double_t GetXmin () const { return fHist->GetXaxis()->GetXmin(); }
113  Double_t GetXmax () const { return fHist->GetXaxis()->GetXmax(); }
114 
115  // perform series of validation tests
116  void ValidatePDF( TH1* original = 0 ) const;
117 
118  //gives the number of needed bins in the source histogram
119  Int_t GetHistNBins ( Int_t evtNum = 0 );
120 
122 
123  // modified name (remove TMVA::)
124  const char* GetName() const { return fPDFName; }
125 
126  // TMVA version control (for weight files)
129 
130  //void WriteOptionsToStream ( std::ostream& o, const TString& prefix ) const;
131  void ProcessOptions();
132 
133  // reads from and option string the definitions for pdf returns it
134  void DeclareOptions();
135 
136  private:
137 
138  // sanity check of PDF quality (after smoothing): comparison with
139  // original histogram
140  void CheckHist() const;
141  void FillSplineToHist();
142  void BuildKDEPDF();
143  void SmoothHistogram();
144  void FillHistToGraph();
145  Double_t GetIntegral() const;
147  TH1* h = GetPDFHist();
148  return (fPDFHist) ? (h->GetXaxis()->GetXmax() - h->GetXaxis()->GetXmin())/h->GetNbinsX() : 1;
149  }
150 
151  // do we use the original histogram as reference ?
152  Bool_t UseHistogram() const { return fUseHistogram; }
153 
154  void FindBinInverse( const TH1* histogram, Int_t& lowerBin, Int_t& higherBin, Double_t& lowerBinValue, Double_t& higherBinValue,
155  Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE ) const;
156 
157 
158  void BuildSplinePDF();
159 
160  // flag that indicates that no splines are produced and no smoothing
161  // is applied, i.e., the original histogram is used as reference
162  // this is useful for discrete variables
163  Bool_t fUseHistogram; // spline0 uses histogram as reference
164 
165  // static configuration variables ----------------------------
166  // to increase computation speed, the final PDF is filled in
167  // a high-binned histogram; "GetValue" then returns the histogram
168  // entry, linearized between adjacent bins
169  static const Int_t fgNbin_PdfHist; // number of bins in high-binned reference histogram
170  static const Bool_t fgManualIntegration; // manual integration (sum over bins) or DGAUSS
171  static const Double_t fgEpsilon; // minimum PDF return
172  // -----------------------------------------------------------
173 
174  TString fPDFName; // for output
175  Int_t fNsmooth; // Min number of smoothing iterations
176  Int_t fMinNsmooth; // Min number of smoothing iterations
177  Int_t fMaxNsmooth; // Max number of smoothing iterations
178  TH1* fNSmoothHist; // number of smooth for each bin
179 
181  TSpline* fSpline; //! the used spline type
182  TH1* fPDFHist; // the high-binned histogram corresponding to the PDF
183  TH1* fHist; // copy of input histogram
184  TH1* fHistOriginal; // the input histogram
185  TGraph* fGraph; //! needed to create PDF from histogram
186  TF1* fIGetVal; // integration interface
187 
188  Int_t fHistAvgEvtPerBin; // avg event per source hist bin
189  Int_t fHistDefinedNBins; // source hist bin num set by user
190 
191  TString fKDEtypeString; // strings used to read definitions
195 
196  KDEKernel::EKernelType fKDEtype; // Kernel type to use for KDE
197  KDEKernel::EKernelIter fKDEiter; // Number of iterations (adaptive or not)
198  KDEKernel::EKernelBorder fKDEborder; // The method to take care about "border" effects (string)
199  Float_t fFineFactor; // fine tuning factor for Adaptive KDE
200 
201  UInt_t fReadingVersion; // the TMVA version of the weight file
202 
203  Bool_t fCheckHist; // check of source histogram
204  Bool_t fNormalize; // normalize histogram (false for cumulative distribution used in GaussTranform)
205 
206  TString fSuffix; //! the suffix for options
207  mutable MsgLogger* fLogger; //! message logger
208  MsgLogger& Log() const { return *fLogger; }
209 
210  // static pointer to this object
211  // This is a workaround for OSx where static thread_local data members are
212  // not supported. The C++ solution would indeed be the following:
213  static PDF*& GetThisPdfThreadLocal() { TTHREAD_TLS(PDF*) fgThisPDF(nullptr); return fgThisPDF; };
214  static PDF* ThisPDF( void );
215 
216  // external auxiliary functions
217  static Double_t IGetVal( Double_t*, Double_t* );
218 
219  ClassDef(PDF,1) // PDF wrapper for histograms
220  };
221 
222 } // namespace TMVA
223 
224 #endif
Double_t GetValInverse(Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE) const
returns value PDF^{-1}(y)
Definition: PDF.cxx:732
Int_t fMaxNsmooth
Definition: PDF.h:177
TString fBorderMethodString
Definition: PDF.h:193
float xmin
Definition: THbookFile.cxx:93
void ReadXML(void *pdfnode)
XML file reading.
Definition: PDF.cxx:957
Double_t GetIntegral() const
computes normalisation
Definition: PDF.cxx:633
Int_t fHistAvgEvtPerBin
Definition: PDF.h:188
static const Double_t fgEpsilon
Definition: PDF.h:171
float Float_t
Definition: RtypesCore.h:53
MsgLogger & Log() const
message logger
Definition: PDF.h:208
TString fPDFName
Definition: PDF.h:174
void SmoothHistogram()
Definition: PDF.cxx:449
TH1 * GetSmoothedHist() const
Definition: PDF.h:103
void BuildPDF(const TH1 *theHist)
Definition: PDF.cxx:254
Double_t GetPdfHistBinWidth() const
Definition: PDF.h:146
TH1 * h
Definition: legend2.C:5
Base class for spline implementation containing the Draw/Paint methods //.
Definition: TSpline.h:22
Float_t fFineFactor
Definition: PDF.h:199
TH1 * GetPDFHist() const
Definition: PDF.h:100
static const Int_t fgNbin_PdfHist
Definition: PDF.h:169
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TString fKDEtypeString
Definition: PDF.h:191
PDF(const TString &name, Bool_t norm=kTRUE)
default constructor needed for ROOT I/O
Definition: PDF.cxx:63
Int_t GetNBins() const
Definition: PDF.h:111
void CheckHist() const
sanity check: compare PDF with original histogram
Definition: PDF.cxx:551
UInt_t GetReadingVersion() const
Definition: PDF.h:128
friend std::ostream & operator<<(std::ostream &os, const PDF &tree)
TF1 * fIGetVal
needed to create PDF from histogram
Definition: PDF.h:186
TMVA::PDF::EInterpolateMethod fInterpolMethod
Definition: PDF.h:180
Double_t GetXmin() const
Definition: TAxis.h:139
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
UInt_t fReadingVersion
Definition: PDF.h:201
static PDF * ThisPDF(void)
Definition: PDF.cxx:1132
TH1 * GetNSmoothHist() const
Definition: PDF.h:104
Bool_t fCheckHist
Definition: PDF.h:203
Bool_t fUseHistogram
Definition: PDF.h:163
TGraph * GetGraph() const
Definition: PDF.h:101
Definition: PDF.h:71
TString fKDEiterString
Definition: PDF.h:192
const char * GetName() const
Returns name of object.
Definition: PDF.h:124
static const Bool_t fgManualIntegration
Definition: PDF.h:170
void BuildSplinePDF()
build the PDF from the original histograms
Definition: PDF.cxx:315
Bool_t UseHistogram() const
Definition: PDF.h:152
Int_t GetHistNBins(Int_t evtNum=0)
Definition: PDF.cxx:298
TH1 * fHistOriginal
Definition: PDF.h:184
void SetReadingVersion(UInt_t rv)
Definition: PDF.h:127
unsigned int UInt_t
Definition: RtypesCore.h:42
Double_t GetXmin() const
Definition: PDF.h:112
float xmax
Definition: THbookFile.cxx:93
Int_t fNsmooth
Definition: PDF.h:175
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
print the tree recursinvely using the << operator
Definition: BinaryTree.cxx:159
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
read the tree from an std::istream
Definition: BinaryTree.cxx:205
Int_t fMinNsmooth
Definition: PDF.h:176
MsgLogger * fLogger
the suffix for options
Definition: PDF.h:207
void DeclareOptions()
define the options (their key words) that can be set in the option string know options: PDFInterpol[i...
Definition: PDF.cxx:816
TH1 * fNSmoothHist
Definition: PDF.h:178
void BuildKDEPDF()
creates high-binned reference histogram to be used instead of the PDF for speed reasons ...
Definition: PDF.cxx:381
static PDF *& GetThisPdfThreadLocal()
Definition: PDF.h:213
double Double_t
Definition: RtypesCore.h:55
Double_t GetXmax() const
Definition: PDF.h:113
Bool_t fNormalize
Definition: PDF.h:204
Int_t fHistDefinedNBins
Definition: PDF.h:189
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:80
TGraph * fGraph
Definition: PDF.h:185
void AddXMLTo(void *parent)
XML file writing.
Definition: PDF.cxx:915
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:759
TString fSuffix
Definition: PDF.h:206
TH1 * fPDFHist
the used spline type
Definition: PDF.h:182
void ValidatePDF(TH1 *original=0) const
comparison of original histogram with reference PDF
Definition: PDF.cxx:576
Abstract ClassifierFactory template that handles arbitrary types.
TH1 * fHist
Definition: PDF.h:183
virtual ~PDF()
Definition: PDF.cxx:240
TSpline * fSpline
Definition: PDF.h:181
void ProcessOptions()
Definition: PDF.cxx:866
friend std::istream & operator>>(std::istream &istr, PDF &tree)
1-Dim function class
Definition: TF1.h:149
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
TH1 * GetOriginalHist() const
Definition: PDF.h:102
static Double_t IGetVal(Double_t *, Double_t *)
static external auxiliary function (integrand)
Definition: PDF.cxx:644
TSpline * GetSpline() const
Definition: PDF.h:110
KDEKernel::EKernelIter fKDEiter
Definition: PDF.h:197
TMVA::PDF::EInterpolateMethod GetInterpolMethod()
Definition: PDF.h:121
Definition: tree.py:1
TString fInterpolateString
Definition: PDF.h:194
virtual Int_t GetNbinsX() const
Definition: TH1.h:301
const Bool_t kTRUE
Definition: Rtypes.h:91
void FillSplineToHist()
creates high-binned reference histogram to be used instead of the PDF for speed reasons ...
Definition: PDF.cxx:521
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
EInterpolateMethod
Definition: PDF.h:78
KDEKernel::EKernelBorder fKDEborder
Definition: PDF.h:198
KDEKernel::EKernelType fKDEtype
Definition: PDF.h:196
Double_t GetXmax() const
Definition: TAxis.h:140
void FillHistToGraph()
Simple conversion.
Definition: PDF.cxx:512
char name[80]
Definition: TGX11.cxx:109
TAxis * GetXaxis()
Definition: TH1.h:324
Double_t GetVal(Double_t x) const
returns value PDF(x)
Definition: PDF.cxx:699