ROOT logo
// @(#)root/tmva $Id: PDF.h 39395 2011-05-26 10:05:54Z moneta $
// Author: Asen Christov, Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss , Jan Therhaag

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : PDF                                                                   *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      PDF wrapper for histograms; uses user-defined spline interpolation        *
 *      and kernel density estimation                                             *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Asen Christov   <christov@physik.uni-freiburg.de> - Freiburg U., Germany  *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
 *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
 *      Jan Therhaag       <Jan.Therhaag@cern.ch>     - U of Bonn, Germany        *
 *                                                                                *
 * Copyright (c) 2005-2011:                                                       *
 *      CERN, Switzerland                                                         * 
 *      U. of Victoria, Canada                                                    * 
 *      MPI-K Heidelberg, Germany                                                 * 
 *      Freiburg U., Germany                                                      * 
 *      U. of Bonn, Germany                                                       *
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/

#ifndef ROOT_TMVA_PDF
#define ROOT_TMVA_PDF

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// PDF                                                                  //
//                                                                      //
// PDF wrapper for histograms; uses user-defined spline interpolation   //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <iosfwd>

#ifndef ROOT_TH1
#include "TH1.h"
#endif
#ifndef ROOT_TMVA_KDEKernel
#include "TMVA/KDEKernel.h"
#endif
#ifndef ROOT_TMVA_Configurable
#include "TMVA/Configurable.h"
#endif

class TSpline;
class TGraph;
class TF1;

namespace TMVA {

   class MsgLogger;

   class PDF;
   ostream& operator<< ( ostream& os, const PDF& tree );
   istream& operator>> ( istream& istr, PDF& tree);

   class PDF : public Configurable {

      friend ostream& operator<< ( ostream& os, const PDF& tree );
      friend istream& operator>> ( istream& istr, PDF& tree);
      
   public:

      enum EInterpolateMethod { kSpline0, kSpline1, kSpline2, kSpline3, kSpline5, kKDE };

      explicit PDF( const TString& name, Bool_t norm=kTRUE );
      explicit PDF( const TString& name, const TH1* theHist, EInterpolateMethod method = kSpline2, 
                    Int_t minnsmooth = 0, Int_t maxnsmooth = 0, Bool_t checkHist = kFALSE, Bool_t norm=kTRUE );
      explicit PDF( const TString& name, const TH1* theHist, 
                    KDEKernel::EKernelType ktype, KDEKernel::EKernelIter kiter, KDEKernel::EKernelBorder 
                    kborder, Float_t FineFactor, Bool_t norm=kTRUE );
      explicit PDF( const TString& name, const TString& options, const TString& suffix = "", PDF* defaultPDF = 0, Bool_t norm=kTRUE);
      virtual ~PDF();
      
      //creates the pdf after the definitions have been stored in
      void BuildPDF (const TH1* theHist);

      // returns probability density at given abscissa
      Double_t GetVal       ( Double_t x ) const;
      Double_t GetValInverse( Double_t y, Bool_t isMonotonouslyIncreasingFunction=kFALSE ) const;

      void AddXMLTo( void* parent );
      void ReadXML( void* pdfnode );

      // histogram underlying the PDF
      TH1*     GetPDFHist()      const { return fPDFHist; }
      TH1*     GetOriginalHist() const { return fHistOriginal; }
      TH1*     GetSmoothedHist() const { return fHist; }
      TH1*     GetNSmoothHist()  const { return fNSmoothHist; }

      // integral of PDF within given range
      Double_t GetIntegral( Double_t xmin, Double_t xmax );

      // accessors
      TSpline* GetSpline() const { return fSpline; }
      Int_t    GetNBins () const { return fHist->GetNbinsX(); }
      Double_t GetXmin  () const { return fHist->GetXaxis()->GetXmin();   }
      Double_t GetXmax  () const { return fHist->GetXaxis()->GetXmax();   }

      // perform series of validation tests
      void     ValidatePDF( TH1* original = 0 ) const;

      //gives the number of needed bins in the source histogram
      Int_t    GetHistNBins ( Int_t evtNum = 0 );

      TMVA::PDF::EInterpolateMethod GetInterpolMethod() { return fInterpolMethod;}

      // modified name (remove TMVA::)
      const char* GetName() const { return fPDFName; }

      // TMVA version control (for weight files)
      void   SetReadingVersion( UInt_t rv ) { fReadingVersion = rv; }      
      UInt_t GetReadingVersion() const { return fReadingVersion; }

      //void WriteOptionsToStream ( ostream& o, const TString& prefix ) const;
      void ProcessOptions();

      // reads from and option string the definitions for pdf returns it
      void DeclareOptions();

   private:

      // sanity check of PDF quality (after smoothing): comparison with 
      // original histogram
      void     CheckHist() const;
      void     FillSplineToHist();
      void     BuildKDEPDF();
      void     SmoothHistogram();
      void     FillHistToGraph();
      Double_t GetIntegral() const;
      Double_t GetPdfHistBinWidth() const { 
         TH1* h = GetPDFHist();
         return (fPDFHist) ? (h->GetXaxis()->GetXmax() - h->GetXaxis()->GetXmin())/h->GetNbinsX() : 1;
      }

      // do we use the original histogram as reference ?
      Bool_t   UseHistogram() const { return fUseHistogram; }

      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;


      void     BuildSplinePDF();

      // flag that indicates that no splines are produced and no smoothing
      // is applied, i.e., the original histogram is used as reference
      // this is useful for discrete variables      
      Bool_t                   fUseHistogram;  // spline0 uses histogram as reference
  
      // static configuration variables ----------------------------
      // to increase computation speed, the final PDF is filled in 
      // a high-binned histogram; "GetValue" then returns the histogram
      // entry, linearized between adjacent bins
      static const Int_t       fgNbin_PdfHist;        // number of bins in high-binned reference histogram
      static const Bool_t      fgManualIntegration;   // manual integration (sum over bins) or DGAUSS
      static const Double_t    fgEpsilon;             // minimum PDF return
      // -----------------------------------------------------------

      TString                  fPDFName;              // for output
      Int_t                    fNsmooth;              // Min number of smoothing iterations
      Int_t                    fMinNsmooth;           // Min number of smoothing iterations
      Int_t                    fMaxNsmooth;           // Max number of smoothing iterations
      TH1*                     fNSmoothHist;          // number of smooth for each bin

      TMVA::PDF::EInterpolateMethod fInterpolMethod;  // interpolation method
      TSpline*                 fSpline;               //! the used spline type
      TH1*                     fPDFHist;              //  the high-binned histogram corresponding to the PDF
      TH1*                     fHist;                 //  copy of input histogram
      TH1*                     fHistOriginal;         //  the input histogram
      TGraph*                  fGraph;                //! needed to create PDF from histogram
      TF1*                     fIGetVal;              // integration interface

      Int_t                    fHistAvgEvtPerBin;     // avg event per source hist bin
      Int_t                    fHistDefinedNBins;     // source hist bin num set by user

      TString                  fKDEtypeString;        // strings used to read definitions
      TString                  fKDEiterString;
      TString                  fBorderMethodString;
      TString                  fInterpolateString;

      KDEKernel::EKernelType   fKDEtype;              // Kernel type to use for KDE
      KDEKernel::EKernelIter   fKDEiter;              // Number of iterations (adaptive or not)
      KDEKernel::EKernelBorder fKDEborder;            // The method to take care about "border" effects (string)
      Float_t                  fFineFactor;           // fine tuning factor for Adaptive KDE

      UInt_t                   fReadingVersion;       // the TMVA version of the weight file

      Bool_t                   fCheckHist;            // check of source histogram
      Bool_t                   fNormalize;            // normalize histogram (false for cumulative distribution used in GaussTranform)

      TString                  fSuffix;               //! the suffix for options
      mutable MsgLogger*       fLogger;               //! message logger
      MsgLogger&               Log() const { return *fLogger; }    

      // static pointer to this object
      static PDF*              fgThisPDF;             // this PDF pointer 
      static PDF*              ThisPDF( void ); 

      // external auxiliary functions 
      static Double_t          IGetVal( Double_t*, Double_t* );

      ClassDef(PDF,1)  // PDF wrapper for histograms
   };

} // namespace TMVA

#endif 
 PDF.h:1
 PDF.h:2
 PDF.h:3
 PDF.h:4
 PDF.h:5
 PDF.h:6
 PDF.h:7
 PDF.h:8
 PDF.h:9
 PDF.h:10
 PDF.h:11
 PDF.h:12
 PDF.h:13
 PDF.h:14
 PDF.h:15
 PDF.h:16
 PDF.h:17
 PDF.h:18
 PDF.h:19
 PDF.h:20
 PDF.h:21
 PDF.h:22
 PDF.h:23
 PDF.h:24
 PDF.h:25
 PDF.h:26
 PDF.h:27
 PDF.h:28
 PDF.h:29
 PDF.h:30
 PDF.h:31
 PDF.h:32
 PDF.h:33
 PDF.h:34
 PDF.h:35
 PDF.h:36
 PDF.h:37
 PDF.h:38
 PDF.h:39
 PDF.h:40
 PDF.h:41
 PDF.h:42
 PDF.h:43
 PDF.h:44
 PDF.h:45
 PDF.h:46
 PDF.h:47
 PDF.h:48
 PDF.h:49
 PDF.h:50
 PDF.h:51
 PDF.h:52
 PDF.h:53
 PDF.h:54
 PDF.h:55
 PDF.h:56
 PDF.h:57
 PDF.h:58
 PDF.h:59
 PDF.h:60
 PDF.h:61
 PDF.h:62
 PDF.h:63
 PDF.h:64
 PDF.h:65
 PDF.h:66
 PDF.h:67
 PDF.h:68
 PDF.h:69
 PDF.h:70
 PDF.h:71
 PDF.h:72
 PDF.h:73
 PDF.h:74
 PDF.h:75
 PDF.h:76
 PDF.h:77
 PDF.h:78
 PDF.h:79
 PDF.h:80
 PDF.h:81
 PDF.h:82
 PDF.h:83
 PDF.h:84
 PDF.h:85
 PDF.h:86
 PDF.h:87
 PDF.h:88
 PDF.h:89
 PDF.h:90
 PDF.h:91
 PDF.h:92
 PDF.h:93
 PDF.h:94
 PDF.h:95
 PDF.h:96
 PDF.h:97
 PDF.h:98
 PDF.h:99
 PDF.h:100
 PDF.h:101
 PDF.h:102
 PDF.h:103
 PDF.h:104
 PDF.h:105
 PDF.h:106
 PDF.h:107
 PDF.h:108
 PDF.h:109
 PDF.h:110
 PDF.h:111
 PDF.h:112
 PDF.h:113
 PDF.h:114
 PDF.h:115
 PDF.h:116
 PDF.h:117
 PDF.h:118
 PDF.h:119
 PDF.h:120
 PDF.h:121
 PDF.h:122
 PDF.h:123
 PDF.h:124
 PDF.h:125
 PDF.h:126
 PDF.h:127
 PDF.h:128
 PDF.h:129
 PDF.h:130
 PDF.h:131
 PDF.h:132
 PDF.h:133
 PDF.h:134
 PDF.h:135
 PDF.h:136
 PDF.h:137
 PDF.h:138
 PDF.h:139
 PDF.h:140
 PDF.h:141
 PDF.h:142
 PDF.h:143
 PDF.h:144
 PDF.h:145
 PDF.h:146
 PDF.h:147
 PDF.h:148
 PDF.h:149
 PDF.h:150
 PDF.h:151
 PDF.h:152
 PDF.h:153
 PDF.h:154
 PDF.h:155
 PDF.h:156
 PDF.h:157
 PDF.h:158
 PDF.h:159
 PDF.h:160
 PDF.h:161
 PDF.h:162
 PDF.h:163
 PDF.h:164
 PDF.h:165
 PDF.h:166
 PDF.h:167
 PDF.h:168
 PDF.h:169
 PDF.h:170
 PDF.h:171
 PDF.h:172
 PDF.h:173
 PDF.h:174
 PDF.h:175
 PDF.h:176
 PDF.h:177
 PDF.h:178
 PDF.h:179
 PDF.h:180
 PDF.h:181
 PDF.h:182
 PDF.h:183
 PDF.h:184
 PDF.h:185
 PDF.h:186
 PDF.h:187
 PDF.h:188
 PDF.h:189
 PDF.h:190
 PDF.h:191
 PDF.h:192
 PDF.h:193
 PDF.h:194
 PDF.h:195
 PDF.h:196
 PDF.h:197
 PDF.h:198
 PDF.h:199
 PDF.h:200
 PDF.h:201
 PDF.h:202
 PDF.h:203
 PDF.h:204
 PDF.h:205
 PDF.h:206
 PDF.h:207
 PDF.h:208
 PDF.h:209
 PDF.h:210
 PDF.h:211
 PDF.h:212
 PDF.h:213
 PDF.h:214
 PDF.h:215
 PDF.h:216
 PDF.h:217
 PDF.h:218