PDF.h

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: PDF.h 33928 2010-06-15 16:19:31Z stelzer $
00002 // Author: Asen Christov, Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss 
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : PDF                                                                   *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      PDF wrapper for histograms; uses user-defined spline interpolation        *
00012  *      and kernel density estimation                                             *
00013  *                                                                                *
00014  * Authors (alphabetical):                                                        *
00015  *      Asen Christov   <christov@physik.uni-freiburg.de> - Freiburg U., Germany  *
00016  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00017  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00018  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
00019  *                                                                                *
00020  * Copyright (c) 2005:                                                            *
00021  *      CERN, Switzerland                                                         * 
00022  *      U. of Victoria, Canada                                                    * 
00023  *      MPI-K Heidelberg, Germany                                                 * 
00024  *      Freiburg U., Germany                                                      * 
00025  *                                                                                *
00026  * Redistribution and use in source and binary forms, with or without             *
00027  * modification, are permitted according to the terms listed in LICENSE           *
00028  * (http://tmva.sourceforge.net/LICENSE)                                          *
00029  **********************************************************************************/
00030 
00031 #ifndef ROOT_TMVA_PDF
00032 #define ROOT_TMVA_PDF
00033 
00034 //////////////////////////////////////////////////////////////////////////
00035 //                                                                      //
00036 // PDF                                                                  //
00037 //                                                                      //
00038 // PDF wrapper for histograms; uses user-defined spline interpolation   //
00039 //                                                                      //
00040 //////////////////////////////////////////////////////////////////////////
00041 
00042 #include <iosfwd>
00043 
00044 #ifndef ROOT_TH1
00045 #include "TH1.h"
00046 #endif
00047 #ifndef ROOT_TMVA_KDEKernel
00048 #include "TMVA/KDEKernel.h"
00049 #endif
00050 #ifndef ROOT_TMVA_Configurable
00051 #include "TMVA/Configurable.h"
00052 #endif
00053 
00054 class TSpline;
00055 class TGraph;
00056 class TF1;
00057 
00058 namespace TMVA {
00059 
00060    class MsgLogger;
00061 
00062    class PDF;
00063    ostream& operator<< ( ostream& os, const PDF& tree );
00064    istream& operator>> ( istream& istr, PDF& tree);
00065 
00066    class PDF : public Configurable {
00067 
00068       friend ostream& operator<< ( ostream& os, const PDF& tree );
00069       friend istream& operator>> ( istream& istr, PDF& tree);
00070       
00071    public:
00072 
00073       enum EInterpolateMethod { kSpline0, kSpline1, kSpline2, kSpline3, kSpline5, kKDE };
00074 
00075       explicit PDF( const TString& name, Bool_t norm=kTRUE );
00076       explicit PDF( const TString& name, const TH1* theHist, EInterpolateMethod method = kSpline2, 
00077                     Int_t minnsmooth = 0, Int_t maxnsmooth = 0, Bool_t checkHist = kFALSE, Bool_t norm=kTRUE );
00078       explicit PDF( const TString& name, const TH1* theHist, 
00079                     KDEKernel::EKernelType ktype, KDEKernel::EKernelIter kiter, KDEKernel::EKernelBorder 
00080                     kborder, Float_t FineFactor, Bool_t norm=kTRUE );
00081       explicit PDF( const TString& name, const TString& options, const TString& suffix = "", PDF* defaultPDF = 0, Bool_t norm=kTRUE);
00082       virtual ~PDF();
00083       
00084       //creates the pdf after the definitions have been stored in
00085       void BuildPDF (const TH1* theHist);
00086 
00087       // returns probability density at given abscissa
00088       Double_t GetVal( Double_t x ) const;
00089 
00090       void AddXMLTo( void* parent );
00091       void ReadXML( void* pdfnode );
00092 
00093       // histogram underlying the PDF
00094       TH1*     GetPDFHist()      const { return fPDFHist; }
00095       TH1*     GetOriginalHist() const { return fHistOriginal; }
00096       TH1*     GetSmoothedHist() const { return fHist; }
00097       TH1*     GetNSmoothHist()  const { return fNSmoothHist; }
00098 
00099       // integral of PDF within given range
00100       Double_t GetIntegral( Double_t xmin, Double_t xmax );
00101 
00102       // accessors
00103       TSpline* GetSpline() const { return fSpline; }
00104       Int_t    GetNBins () const { return fHist->GetNbinsX(); }
00105       Double_t GetXmin  () const { return fHist->GetXaxis()->GetXmin();   }
00106       Double_t GetXmax  () const { return fHist->GetXaxis()->GetXmax();   }
00107 
00108       // perform series of validation tests
00109       void     ValidatePDF( TH1* original = 0 ) const;
00110 
00111       //gives the number of needed bins in the source histogram
00112       Int_t    GetHistNBins ( Int_t evtNum = 0 );
00113 
00114       TMVA::PDF::EInterpolateMethod GetInterpolMethod() { return fInterpolMethod;}
00115 
00116       // modified name (remove TMVA::)
00117       const char* GetName() const { return fPDFName; }
00118 
00119       // TMVA version control (for weight files)
00120       void   SetReadingVersion( UInt_t rv ) { fReadingVersion = rv; }      
00121       UInt_t GetReadingVersion() const { return fReadingVersion; }
00122 
00123       //void WriteOptionsToStream ( ostream& o, const TString& prefix ) const;
00124       void ProcessOptions();
00125 
00126       // reads from and option string the definitions for pdf returns it
00127       void DeclareOptions();
00128 
00129    private:
00130 
00131       // sanity check of PDF quality (after smoothing): comparison with 
00132       // original histogram
00133       void     CheckHist() const;
00134       void     FillSplineToHist();
00135       void     BuildKDEPDF();
00136       void     SmoothHistogram();
00137       void     FillHistToGraph();
00138       Double_t GetIntegral() const;
00139       Double_t GetPdfHistBinWidth() const { 
00140          TH1* h = GetPDFHist();
00141          return (fPDFHist) ? (h->GetXaxis()->GetXmax() - h->GetXaxis()->GetXmin())/h->GetNbinsX() : 1;
00142       }
00143 
00144       // do we use the original histogram as reference ?
00145       Bool_t   UseHistogram() const { return fUseHistogram; }
00146 
00147 
00148       void     BuildSplinePDF();
00149 
00150       // flag that indicates that no splines are produced and no smoothing
00151       // is applied, i.e., the original histogram is used as reference
00152       // this is useful for discrete variables      
00153       Bool_t                   fUseHistogram;  // spline0 uses histogram as reference
00154   
00155       // static configuration variables ----------------------------
00156       // to increase computation speed, the final PDF is filled in 
00157       // a high-binned histogram; "GetValue" then returns the histogram
00158       // entry, linearized between adjacent bins
00159       static const Int_t       fgNbin_PdfHist;        // number of bins in high-binned reference histogram
00160       static const Bool_t      fgManualIntegration;   // manual integration (sum over bins) or DGAUSS
00161       static const Double_t    fgEpsilon;             // minimum PDF return
00162       // -----------------------------------------------------------
00163 
00164       TString                  fPDFName;              // for output
00165       Int_t                    fNsmooth;              // Min number of smoothing iterations
00166       Int_t                    fMinNsmooth;           // Min number of smoothing iterations
00167       Int_t                    fMaxNsmooth;           // Max number of smoothing iterations
00168       TH1*                     fNSmoothHist;          // number of smooth for each bin
00169 
00170       TMVA::PDF::EInterpolateMethod fInterpolMethod;  // interpolation method
00171       TSpline*                 fSpline;               //! the used spline type
00172       TH1*                     fPDFHist;              //  the high-binned histogram corresponding to the PDF
00173       TH1*                     fHist;                 //  copy of input histogram
00174       TH1*                     fHistOriginal;         //  the input histogram
00175       TGraph*                  fGraph;                //! needed to create PDF from histogram
00176       TF1*                     fIGetVal;              // integration interface
00177 
00178       Int_t                    fHistAvgEvtPerBin;     // avg event per source hist bin
00179       Int_t                    fHistDefinedNBins;     // source hist bin num set by user
00180 
00181       TString                  fKDEtypeString;        // strings used to read definitions
00182       TString                  fKDEiterString;
00183       TString                  fBorderMethodString;
00184       TString                  fInterpolateString;
00185 
00186       KDEKernel::EKernelType   fKDEtype;              // Kernel type to use for KDE
00187       KDEKernel::EKernelIter   fKDEiter;              // Number of iterations (adaptive or not)
00188       KDEKernel::EKernelBorder fKDEborder;            // The method to take care about "border" effects (string)
00189       Float_t                  fFineFactor;           // fine tuning factor for Adaptive KDE
00190 
00191       UInt_t                   fReadingVersion;       // the TMVA version of the weight file
00192 
00193       Bool_t                   fCheckHist;            // check of source histogram
00194       Bool_t                   fNormalize;            // normalize histogram (false for cumulative distribution used in GaussTranform)
00195 
00196       TString                  fSuffix;               //! the suffix for options
00197       mutable MsgLogger*       fLogger;               //! message logger
00198       MsgLogger&               Log() const { return *fLogger; }    
00199 
00200       // static pointer to this object
00201       static PDF*              fgThisPDF;             // this PDF pointer 
00202       static PDF*              ThisPDF( void ); 
00203 
00204       // external auxiliary functions 
00205       static Double_t          IGetVal( Double_t*, Double_t* );
00206 
00207       ClassDef(PDF,1)  // PDF wrapper for histograms
00208    };
00209 
00210 } // namespace TMVA
00211 
00212 #endif 

Generated on Tue Jul 5 14:27:33 2011 for ROOT_528-00b_version by  doxygen 1.5.1