ROOT logo
// @(#)root/tmva $Id: MethodPDEFoam.h 31458 2009-11-30 13:58:20Z stelzer $
// Author: Tancredi Carli, Dominik Dannheim, Alexander Voigt

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate Data analysis       *
 * Package: TMVA                                                                  *
 * Class  : MethodPDEFoam                                                         *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      The PDEFoam method is an extension of the PDERS method, which divides     *
 *      the multi-dimensional phase space in a finite number of hyper-rectangles  *
 *      (cells) of constant event density. This "foam" of cells is filled with    *
 *      averaged probability-density information sampled from a training event    *
 *      sample.    
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Tancredi Carli   - CERN, Switzerland                                      *
 *      Dominik Dannheim - CERN, Switzerland                                      *
 *      Peter Speckmayer <peter.speckmayer@cern.ch>  - CERN, Switzerland          *
 *      Alexander Voigt  - CERN, Switzerland                                      *
 *                                                                                *
 * Original author of the TFoam implementation:                                   *
 *      S. Jadach - Institute of Nuclear Physics, Cracow, Poland                  *
 *                                                                                * 
 * Copyright (c) 2008:                                                            *
 *      CERN, Switzerland                                                         * 
 *      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_MethodPDEFoam
#define ROOT_TMVA_MethodPDEFoam

//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// MethodPDEFoam                                                            //
//                                                                          //
// The PDEFoam method is an                                                 //
// extension of the PDERS method, which divides the multi-dimensional       //
// phase space in a finite number of hyper-rectangles (cells) of constant   //
// event density.                                                           //
// This "foam" of cells is filled with averaged probability-density         //
// information sampled from a training event sample.                        //
//                                                                          //
// For a given number of cells, the binning algorithm adjusts the size      //
// and position of the cells inside the multidimensional phase space        //
// based on a binary-split algorithm, minimizing the variance of the        //
// event density in the cell.                                               //
// The binned event density information of the final foam is stored in      //
// binary trees, allowing for a fast and memory-efficient classification    //
// of events.                                                               //
//                                                                          //
// The implementation of PDEFoam is based on the Monte-Carlo integration    //
// package TFoam included in the analysis package ROOT.                     //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TRandom3
#include "TRandom3.h"
#endif

#ifndef ROOT_TMVA_MethodBase
#include "TMVA/MethodBase.h"
#endif

#ifndef ROOT_TMVA_PDEFoam
#include "TMVA/PDEFoam.h"
#endif

#define FOAM_NUMBER 2

namespace TMVA {

   class MethodPDEFoam : public MethodBase {

   public:
      
      MethodPDEFoam( const TString& jobName, 
                     const TString& methodTitle, 
                     DataSetInfo& dsi,
                     const TString& theOption = "PDEFoam",
                     TDirectory* theTargetDir = 0 );

      MethodPDEFoam( DataSetInfo& dsi, 
                     const TString& theWeightFile,  
                     TDirectory* theTargetDir = NULL );

      virtual ~MethodPDEFoam( void );

      virtual Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets );
    
      // training methods
      void Train( void );
      void TrainMonoTargetRegression( void );    // Regression output: one value
      void TrainMultiTargetRegression( void );   // Regression output: any number of values
      void TrainSeparatedClassification( void ); // Classification: one foam for Sig, one for Bg
      void TrainUnifiedClassification( void );   // Classification: one foam for Signal and Bg

      using MethodBase::ReadWeightsFromStream;

      // write weights to stream
      void AddWeightsXMLTo( void* parent ) const;

      // read weights from stream
      void ReadWeightsFromStream( std::istream & i );
      void ReadWeightsFromXML   ( void* wghtnode );

      // write/read pure foams to/from file
      void WriteFoamsToFile() const;
      void ReadFoamsFromFile();

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

      // regression procedure
      virtual const std::vector<Float_t>& GetRegressionValues();

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

      // possible kernels (option)
      EKernel GetKernel( void ) { return fKernel; }
      UInt_t KernelToUInt(EKernel ker) const { 
         if (ker == kNone)
            return 0;
         else if (ker == kGaus)
            return 1;
         else if (ker == kLinN)
            return 2;
         else {
            Log() << kFATAL << "Error: unknown kernel!" << Endl;
            return 0;
         }
      }
      EKernel UIntToKernel(UInt_t iker){  
         if (iker == 0)
            return kNone;
         else if (iker == 1)
            return kGaus;
         else if (iker == 2)
            return kLinN;
         else {
            Log() << kFATAL << "Error: unknown kernel number: " << iker << Endl;
            return kNone;
         }
      }
      UInt_t TargetSelectionToUInt(ETargetSelection ts) const { 
         if (ts == kMean)
            return 0;
         else if (ts == kMpv)
            return 1;
         else {
            std::cout << "Error: unknown method TargetSelection!" << std::endl;
            return 0;
         }
      }
      ETargetSelection UIntToTargetSelection(UInt_t its){  
         if (its == 0)
            return kMean;
         else if (its == 1)
            return kMpv;
         else {
            std::cout << "Error: unknown method TargetSelection: " << its << std::endl;
            return kMean;
         }
      }

   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;

      // calculate Xmin and Xmax for Foam
      void CalcXminXmax();

      // Set Xmin, Xmax in foam with index 'foam_index'
      void SetXminXmax(TMVA::PDEFoam*);

      // Set foam options
      void InitFoam(TMVA::PDEFoam*, EFoamType);

      // fill variable names into foam
      void FillVariableNamesToFoam() const;

   private:

      // the option handling methods
      void DeclareOptions();
      void ProcessOptions();
  
      // nice output
      void PrintCoefficients( void );

      // options to be used
      Bool_t        fSigBgSeparated;  // Separate Sig and Bg, or not
      Double_t      fFrac;            // Fraction used for calc of Xmin, Xmax
      Double_t      fDiscrErrCut;     // cut on discrimant error
      Int_t         fVolFrac;         // inverse volume fraction (used for density calculation during buildup)
      Float_t       fVolFracInv;      // volume fraction (used for density calculation during buildup)
      Int_t         fnCells;          // Number of Cells  (1000)
      Int_t         fnActiveCells;    // Number of active cells
      Int_t         fnSampl;          // Number of MC events per cell in build-up (1000)
      Int_t         fnBin;            // Number of bins in build-up (100)
      Int_t         fEvPerBin;        // Maximum events (equiv.) per bin in buid-up (1000) 
      Float_t       fNSigBgRatio;     // ratio of number of signal events / bg events (training)

      Bool_t        fCompress;        // compress foam output file
      Bool_t        fMultiTargetRegression; // do regression on multible targets
      UInt_t        fNmin;            // minimal number of events in cell necessary to split cell"
      Bool_t        fCutNmin;         // Grabbing cell with maximal RMS to split next (TFoam default)
      Double_t      fRMSmin;          // minimal number of events in cell necessary to split cell"
      Bool_t        fCutRMSmin;       // Grabbing cell with maximal RMS to split next (TFoam default)

      TString       fKernelStr;       // Kernel for GetMvaValue() (option string)
      EKernel       fKernel;          // Kernel for GetMvaValue()
      TString       fTargetSelectionStr; // method of selecting the target (only mulit target regr.)
      ETargetSelection fTargetSelection; // method of selecting the target (only mulit target regr.)
     
      std::vector<Double_t> Xmin, Xmax; // range for histograms and foams

      // foams and densities
      PDEFoam* foam[FOAM_NUMBER]; // foam[0]=signal, if Sig and BG are Seperated; else foam[0]=signal/bg
                                  // foam[1]=background, if Sig and BG are Seperated; else it is not used

      // default initialisation called by all constructors
      void Init( void );

      ClassDef(MethodPDEFoam,0) // Analysis of PDEFoam discriminant (PDEFoam or Mahalanobis approach) 
   };

} // namespace TMVA

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