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
51class TSpline;
52class TGraph;
53class TF1;
54
55namespace 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 ?
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
int Int_t
Definition: CPyCppyy.h:43
unsigned int UInt_t
Definition: CPyCppyy.h:44
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:59
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassDef(name, id)
Definition: Rtypes.h:325
float xmin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
Double_t GetXmax() const
Definition: TAxis.h:134
Double_t GetXmin() const
Definition: TAxis.h:133
1-Dim function class
Definition: TF1.h:213
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:320
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
PDF wrapper for histograms; uses user-defined spline interpolation.
Definition: PDF.h:63
TH1 * GetPDFHist() const
Definition: PDF.h:92
void SetReadingVersion(UInt_t rv)
Definition: PDF.h:119
static const Bool_t fgManualIntegration
Definition: PDF.h:162
KDEKernel::EKernelType fKDEtype
Definition: PDF.h:188
void FillHistToGraph()
Simple conversion.
Definition: PDF.cxx:514
void BuildPDF(const TH1 *theHist)
Definition: PDF.cxx:260
KDEKernel::EKernelBorder fKDEborder
Definition: PDF.h:190
static Double_t IGetVal(Double_t *, Double_t *)
static external auxiliary function (integrand)
Definition: PDF.cxx:646
TString fBorderMethodString
Definition: PDF.h:185
TSpline * GetSpline() const
Definition: PDF.h:102
Int_t fMaxNsmooth
Definition: PDF.h:169
TH1 * fPDFHist
the used spline type
Definition: PDF.h:174
Bool_t UseHistogram() const
Definition: PDF.h:144
TMVA::PDF::EInterpolateMethod fInterpolMethod
Definition: PDF.h:172
TSpline * fSpline
Definition: PDF.h:173
UInt_t fReadingVersion
Definition: PDF.h:193
void SmoothHistogram()
Definition: PDF.cxx:455
TGraph * fGraph
Definition: PDF.h:177
Double_t GetValInverse(Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE) const
returns value
Definition: PDF.cxx:734
Double_t GetPdfHistBinWidth() const
Definition: PDF.h:138
void CheckHist() const
sanity check: compare PDF with original histogram
Definition: PDF.cxx:553
MsgLogger & Log() const
message logger
Definition: PDF.h:200
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
Int_t fHistDefinedNBins
Definition: PDF.h:181
TString fPDFName
Definition: PDF.h:166
Double_t GetXmin() const
Definition: PDF.h:104
TString fKDEiterString
Definition: PDF.h:184
void ReadXML(void *pdfnode)
XML file reading.
Definition: PDF.cxx:961
TH1 * GetSmoothedHist() const
Definition: PDF.h:95
TString fInterpolateString
Definition: PDF.h:186
Int_t fNsmooth
Definition: PDF.h:167
Double_t GetXmax() const
Definition: PDF.h:105
void DeclareOptions()
define the options (their key words) that can be set in the option string
Definition: PDF.cxx:820
friend std::istream & operator>>(std::istream &istr, PDF &tree)
TF1 * fIGetVal
needed to create PDF from histogram
Definition: PDF.h:178
void AddXMLTo(void *parent)
XML file writing.
Definition: PDF.cxx:919
TH1 * GetOriginalHist() const
Definition: PDF.h:94
void BuildSplinePDF()
build the PDF from the original histograms
Definition: PDF.cxx:321
TH1 * fNSmoothHist
Definition: PDF.h:170
UInt_t GetReadingVersion() const
Definition: PDF.h:120
void FillSplineToHist()
creates high-binned reference histogram to be used instead of the PDF for speed reasons
Definition: PDF.cxx:523
static PDF *& GetThisPdfThreadLocal()
Definition: PDF.h:205
void BuildKDEPDF()
creates high-binned reference histogram to be used instead of the PDF for speed reasons
Definition: PDF.cxx:387
Int_t fMinNsmooth
Definition: PDF.h:168
Int_t GetHistNBins(Int_t evtNum=0)
Definition: PDF.cxx:304
static const Double_t fgEpsilon
Definition: PDF.h:163
Float_t fFineFactor
Definition: PDF.h:191
const char * GetName() const
Returns name of object.
Definition: PDF.h:116
virtual ~PDF()
Definition: PDF.cxx:246
void ValidatePDF(TH1 *original=0) const
comparison of original histogram with reference PDF
Definition: PDF.cxx:578
Double_t GetVal(Double_t x) const
returns value PDF(x)
Definition: PDF.cxx:701
Int_t fHistAvgEvtPerBin
Definition: PDF.h:180
Double_t GetIntegral() const
computes normalisation
Definition: PDF.cxx:635
TH1 * GetNSmoothHist() const
Definition: PDF.h:96
Bool_t fCheckHist
Definition: PDF.h:195
MsgLogger * fLogger
the suffix for options
Definition: PDF.h:199
friend std::ostream & operator<<(std::ostream &os, const PDF &tree)
TString fKDEtypeString
Definition: PDF.h:183
TGraph * GetGraph() const
Definition: PDF.h:93
Bool_t fUseHistogram
Definition: PDF.h:155
void ProcessOptions()
Definition: PDF.cxx:870
EInterpolateMethod
Definition: PDF.h:70
@ kKDE
Definition: PDF.h:70
@ kSpline1
Definition: PDF.h:70
@ kSpline0
Definition: PDF.h:70
@ kSpline3
Definition: PDF.h:70
@ kSpline2
Definition: PDF.h:70
@ kSpline5
Definition: PDF.h:70
Bool_t fNormalize
Definition: PDF.h:196
TString fSuffix
Definition: PDF.h:198
TH1 * fHistOriginal
Definition: PDF.h:176
Int_t GetNBins() const
Definition: PDF.h:103
static const Int_t fgNbin_PdfHist
Definition: PDF.h:161
PDF(const TString &name, Bool_t norm=kTRUE)
default constructor needed for ROOT I/O
Definition: PDF.cxx:69
TMVA::PDF::EInterpolateMethod GetInterpolMethod()
Definition: PDF.h:113
KDEKernel::EKernelIter fKDEiter
Definition: PDF.h:189
TH1 * fHist
Definition: PDF.h:175
static PDF * ThisPDF(void)
Definition: PDF.cxx:1136
Base class for spline implementation containing the Draw/Paint methods.
Definition: TSpline.h:31
Basic string class.
Definition: TString.h:136
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
static const std::string name("name")
create variable transformations
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
Definition: tree.py:1