ROOT logo
// @(#)root/tmva $Id: MethodPDERS.h 29122 2009-06-22 06:51:30Z brun $
// Author: Andreas Hoecker, Yair Mahalalel, Joerg Stelzer, Helge Voss, Kai Voss

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : MethodPDERS                                                           *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Multidimensional Likelihood using the "Probability density estimator      *
 *      range search" (PDERS) method suggested in                                 *
 *      T. Carli and B. Koblitz, NIM A 501, 576 (2003)                            *
 *                                                                                *
 *      The multidimensional PDFs for signal and background are modeled           *
 *      by counting the events in the "vicinity" of a test point. The volume      *
 *      that describes "vicinity" is user-defined through the option string.      *
 *      A search method based on binary-trees is used to improve the selection    *
 *      efficiency of the volume search.                                          *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Yair Mahalalel  <Yair.Mahalalel@cern.ch> - CERN, Switzerland              *
 *      Peter Speckmayer <peter.speckmayer@cern.ch>  - CERN, Switzerland          *
 *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
 *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
 *                                                                                *
 * Copyright (c) 2005:                                                            *
 *      CERN, Switzerland                                                         *
 *      U. of Victoria, Canada                                                    *
 *      MPI-K Heidelberg, 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_MethodPDERS
#define ROOT_TMVA_MethodPDERS

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// MethodPDERS                                                          //
//                                                                      //
// Multidimensional Likelihood using the "Probability density           //
// estimator range search" (PDERS) method                               //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TMVA_MethodBase
#include "TMVA/MethodBase.h"
#endif
#ifndef ROOT_TMVA_BinarySearchTree
#include "TMVA/BinarySearchTree.h"
#endif
#ifndef ROOT_TMVA_TVector
#ifndef ROOT_TVector
#include "TVector.h"
#endif
#endif

namespace TMVA {

   class Volume;
   class Event;

   class MethodPDERS : public MethodBase {

   public:

      MethodPDERS( const TString& jobName,
                   const TString& methodTitle, 
                   DataSetInfo& theData,
                   const TString& theOption,
                   TDirectory* theTargetDir = 0 );

      MethodPDERS( DataSetInfo& theData,
                   const TString& theWeightFile,
                   TDirectory* theTargetDir = NULL );

      virtual ~MethodPDERS( void );

      virtual Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets );


      // training method
      void Train( void );

      // write weights to file
      void WriteWeightsToStream( ostream& o ) const;
      void WriteWeightsToStream( TFile& rf ) const;
      void AddWeightsXMLTo( void* parent ) const;

      // read weights from file
      void ReadWeightsFromStream( istream& istr );
      void ReadWeightsFromStream( TFile& istr );
      void ReadWeightsFromXML( void* wghtnode );

      // calculate the MVA value
      Double_t GetMvaValue( Double_t* err = 0 );

      // calculate the MVA value
      const std::vector<Float_t>& GetRegressionValues();
   public:

      // for root finder
      static Double_t IGetVolumeContentForRoot( Double_t );
      Double_t         GetVolumeContentForRoot( Double_t );

      // static pointer to this object
      static MethodPDERS* ThisPDERS( void ) { return fgThisPDERS; }

   protected:

      // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
      void MakeClassSpecific( std::ostream&, const TString& ) const;

      // get help message text
      void GetHelpMessage() const;

      Volume*      fHelpVolume; // auxiliary variable
      Int_t        fFcnCall;    // number of external function calls (RootFinder)

      // accessors
      BinarySearchTree* GetBinaryTree( void ) const { return fBinaryTree; }

      Double_t             CKernelEstimate( const Event&, std::vector<const BinarySearchTreeNode*>&, Volume& );
      void                 RKernelEstimate( const Event&, std::vector<const BinarySearchTreeNode*>&, Volume&, std::vector<Float_t> *pdfSum );

      Double_t ApplyKernelFunction( Double_t normalized_distance );
      Double_t KernelNormalization( Double_t pdf );
      Double_t GetNormalizedDistance( const TMVA::Event &base_event, 
                                      const BinarySearchTreeNode &sample_event, 
                                      Double_t *dim_normalization);
      Double_t NormSinc( Double_t x );
      Double_t LanczosFilter( Int_t level, Double_t x );

      // ranking of input variables
      const Ranking* CreateRanking() { return 0; }

   private:

      // the option handling methods
      void DeclareOptions();
      void ProcessOptions();

      // calculate the averages of the input variables needed for adaptive training
      void CalcAverages();

      // create binary search trees for signal and background
      void CreateBinarySearchTree( Types::ETreeType type );
      
      // get sample of training events
      void GetSample( const Event &e, std::vector<const BinarySearchTreeNode*>& events, Volume *volume);

      // option
      TString fVolumeRange;    // option volume range
      TString fKernelString;   // option kernel estimator

      enum EVolumeRangeMode {
         kUnsupported = 0,
         kMinMax,
         kRMS,
         kAdaptive,
         kUnscaled,
         kkNN
      } fVRangeMode;

      enum EKernelEstimator {
         kBox = 0,
         kSphere,
         kTeepee,
         kGauss,
         kSinc3,     // the sinc enumerators must be consecutive and in order!
         kSinc5,
         kSinc7,
         kSinc9,
         kSinc11,
         kLanczos2,
         kLanczos3,
         kLanczos5,
         kLanczos8,
         kTrim
      } fKernelEstimator;

      BinarySearchTree*  fBinaryTree;   // binary tree

      std::vector<Float_t>*   fDelta;         // size of volume
      std::vector<Float_t>*   fShift;         // volume center
      std::vector<Float_t>    fAverageRMS;    // average RMS of signal and background

      Float_t            fScaleS;        // weight for signal events
      Float_t            fScaleB;        // weight for background events
      Float_t            fDeltaFrac;     // fraction of RMS
      Double_t           fGaussSigma;    // size of Gauss in adaptive volume 
      Double_t           fGaussSigmaNorm;// size of Gauss in adaptive volume (normalised to dimensions)

      Double_t           fNRegOut;       // number of output dimensions for regression

      // input for adaptive volume adjustment
      Float_t            fNEventsMin;    // minimum number of events in adaptive volume
      Float_t            fNEventsMax;    // maximum number of events in adaptive volume
      Float_t            fMaxVIterations;// maximum number of iterations to adapt volume size
      Float_t            fInitialScale;  // initial scale for adaptive volume

      Bool_t             fInitializedVolumeEle; // is volume element initialized ?
      
      Int_t              fkNNMin;        // min number of events in kNN tree
      Int_t              fkNNMax;        // max number of events in kNN tree
      
      Double_t           fMax_distance;  // maximum distance
      Bool_t             fPrinted;       // print
      Bool_t             fNormTree;      // binary-search tree is normalised

      void    SetVolumeElement ( void );

      Double_t              CRScalc           ( const Event& );
      void                  RRScalc           ( const Event&, std::vector<Float_t>* count );

      Float_t GetError         ( Float_t countS, Float_t countB,
                                 Float_t sumW2S, Float_t sumW2B ) const;

      // this carrier
      static MethodPDERS* fgThisPDERS; // this pointer (required by root finder)
      void UpdateThis() { fgThisPDERS = this; }

      void Init( void );

      ClassDef(MethodPDERS,0) // Multi-dimensional probability density estimator range search (PDERS) method
   };

} // namespace TMVA

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