Logo ROOT   6.14/05
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
Double_t GetValInverse(Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE) const
returns value
Definition: PDF.cxx:737
Int_t fMaxNsmooth
Definition: PDF.h:169
TString fBorderMethodString
Definition: PDF.h:185
float xmin
Definition: THbookFile.cxx:93
void ReadXML(void *pdfnode)
XML file reading.
Definition: PDF.cxx:964
Double_t GetIntegral() const
computes normalisation
Definition: PDF.cxx:638
Int_t fHistAvgEvtPerBin
Definition: PDF.h:180
static const Double_t fgEpsilon
Definition: PDF.h:163
float Float_t
Definition: RtypesCore.h:53
MsgLogger & Log() const
message logger
Definition: PDF.h:200
TString fPDFName
Definition: PDF.h:166
void SmoothHistogram()
Definition: PDF.cxx:454
TH1 * GetSmoothedHist() const
Definition: PDF.h:95
void BuildPDF(const TH1 *theHist)
Definition: PDF.cxx:259
Double_t GetPdfHistBinWidth() const
Definition: PDF.h:138
Base class for spline implementation containing the Draw/Paint methods.
Definition: TSpline.h:20
Float_t fFineFactor
Definition: PDF.h:191
TH1 * GetPDFHist() const
Definition: PDF.h:92
static const Int_t fgNbin_PdfHist
Definition: PDF.h:161
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TString fKDEtypeString
Definition: PDF.h:183
PDF(const TString &name, Bool_t norm=kTRUE)
default constructor needed for ROOT I/O
Definition: PDF.cxx:68
Int_t GetNBins() const
Definition: PDF.h:103
void CheckHist() const
sanity check: compare PDF with original histogram
Definition: PDF.cxx:556
UInt_t GetReadingVersion() const
Definition: PDF.h:120
friend std::ostream & operator<<(std::ostream &os, const PDF &tree)
TF1 * fIGetVal
needed to create PDF from histogram
Definition: PDF.h:178
TMVA::PDF::EInterpolateMethod fInterpolMethod
Definition: PDF.h:172
Double_t GetXmin() const
Definition: TAxis.h:133
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:320
UInt_t fReadingVersion
Definition: PDF.h:193
static PDF * ThisPDF(void)
Definition: PDF.cxx:1139
TH1 * GetNSmoothHist() const
Definition: PDF.h:96
Bool_t fCheckHist
Definition: PDF.h:195
Bool_t fUseHistogram
Definition: PDF.h:155
TGraph * GetGraph() const
Definition: PDF.h:93
PDF wrapper for histograms; uses user-defined spline interpolation.
Definition: PDF.h:63
TString fKDEiterString
Definition: PDF.h:184
const char * GetName() const
Returns name of object.
Definition: PDF.h:116
static const Bool_t fgManualIntegration
Definition: PDF.h:162
void BuildSplinePDF()
build the PDF from the original histograms
Definition: PDF.cxx:320
Bool_t UseHistogram() const
Definition: PDF.h:144
Int_t GetHistNBins(Int_t evtNum=0)
Definition: PDF.cxx:303
TH1 * fHistOriginal
Definition: PDF.h:176
void SetReadingVersion(UInt_t rv)
Definition: PDF.h:119
unsigned int UInt_t
Definition: RtypesCore.h:42
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
Double_t GetXmin() const
Definition: PDF.h:104
float xmax
Definition: THbookFile.cxx:93
Int_t fNsmooth
Definition: PDF.h:167
Int_t fMinNsmooth
Definition: PDF.h:168
#define h(i)
Definition: RSha256.hxx:106
MsgLogger * fLogger
the suffix for options
Definition: PDF.h:199
const Bool_t kFALSE
Definition: RtypesCore.h:88
void DeclareOptions()
define the options (their key words) that can be set in the option string
Definition: PDF.cxx:823
TH1 * fNSmoothHist
Definition: PDF.h:170
void BuildKDEPDF()
creates high-binned reference histogram to be used instead of the PDF for speed reasons ...
Definition: PDF.cxx:386
static PDF *& GetThisPdfThreadLocal()
Definition: PDF.h:205
double Double_t
Definition: RtypesCore.h:55
Double_t GetXmax() const
Definition: PDF.h:105
Bool_t fNormalize
Definition: PDF.h:196
Int_t fHistDefinedNBins
Definition: PDF.h:181
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:56
TGraph * fGraph
Definition: PDF.h:177
void AddXMLTo(void *parent)
XML file writing.
Definition: PDF.cxx:922
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:764
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
TString fSuffix
Definition: PDF.h:198
TH1 * fPDFHist
the used spline type
Definition: PDF.h:174
void ValidatePDF(TH1 *original=0) const
comparison of original histogram with reference PDF
Definition: PDF.cxx:581
Abstract ClassifierFactory template that handles arbitrary types.
TH1 * fHist
Definition: PDF.h:175
virtual ~PDF()
Definition: PDF.cxx:245
TSpline * fSpline
Definition: PDF.h:173
void ProcessOptions()
Definition: PDF.cxx:873
friend std::istream & operator>>(std::istream &istr, PDF &tree)
1-Dim function class
Definition: TF1.h:211
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
TH1 * GetOriginalHist() const
Definition: PDF.h:94
static Double_t IGetVal(Double_t *, Double_t *)
static external auxiliary function (integrand)
Definition: PDF.cxx:649
TSpline * GetSpline() const
Definition: PDF.h:102
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
KDEKernel::EKernelIter fKDEiter
Definition: PDF.h:189
TMVA::PDF::EInterpolateMethod GetInterpolMethod()
Definition: PDF.h:113
Definition: tree.py:1
TString fInterpolateString
Definition: PDF.h:186
virtual Int_t GetNbinsX() const
Definition: TH1.h:291
void FillSplineToHist()
creates high-binned reference histogram to be used instead of the PDF for speed reasons ...
Definition: PDF.cxx:526
const Bool_t kTRUE
Definition: RtypesCore.h:87
EInterpolateMethod
Definition: PDF.h:70
KDEKernel::EKernelBorder fKDEborder
Definition: PDF.h:190
KDEKernel::EKernelType fKDEtype
Definition: PDF.h:188
Double_t GetXmax() const
Definition: TAxis.h:134
void FillHistToGraph()
Simple conversion.
Definition: PDF.cxx:517
char name[80]
Definition: TGX11.cxx:109
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315
Double_t GetVal(Double_t x) const
returns value PDF(x)
Definition: PDF.cxx:704