ROOT logo
// @(#)root/tmva $Id$
// 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  - TU Dresden, Germany                                    *
 *                                                                                *
 * Original author of the TFoam implementation:                                   *
 *      S. Jadach - Institute of Nuclear Physics, Cracow, Poland                  *
 *                                                                                *
 * Copyright (c) 2008, 2010:                                                      *
 *      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                                                            //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

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

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

#ifndef ROOT_TMVA_PDEFoamDecisionTree
#include "TMVA/PDEFoamDecisionTree.h"
#endif
#ifndef ROOT_TMVA_PDEFoamEvent
#include "TMVA/PDEFoamEvent.h"
#endif
#ifndef ROOT_TMVA_PDEFoamDiscriminant
#include "TMVA/PDEFoamDiscriminant.h"
#endif
#ifndef ROOT_TMVA_PDEFoamTarget
#include "TMVA/PDEFoamTarget.h"
#endif
#ifndef ROOT_TMVA_PDEFoamMultiTarget
#include "TMVA/PDEFoamMultiTarget.h"
#endif

#ifndef ROOT_TMVA_PDEFoamDensityBase
#include "TMVA/PDEFoamDensityBase.h"
#endif
#ifndef ROOT_TMVA_PDEFoamTargetDensity
#include "TMVA/PDEFoamTargetDensity.h"
#endif
#ifndef ROOT_TMVA_PDEFoamEventDensity
#include "TMVA/PDEFoamEventDensity.h"
#endif
#ifndef ROOT_TMVA_PDEFoamDiscriminantDensity
#include "TMVA/PDEFoamDiscriminantDensity.h"
#endif
#ifndef ROOT_TMVA_PDEFoamDecisionTreeDensity
#include "TMVA/PDEFoamDecisionTreeDensity.h"
#endif

#ifndef ROOT_TMVA_PDEFoamKernelBase
#include "TMVA/PDEFoamKernelBase.h"
#endif
#ifndef ROOT_TMVA_PDEFoamKernelTrivial
#include "TMVA/PDEFoamKernelTrivial.h"
#endif
#ifndef ROOT_TMVA_PDEFoamKernelLinN
#include "TMVA/PDEFoamKernelLinN.h"
#endif
#ifndef ROOT_TMVA_PDEFoamKernelGauss
#include "TMVA/PDEFoamKernelGauss.h"
#endif

namespace TMVA {

   class MethodPDEFoam : public MethodBase {

   public:

      // kernel types
      typedef enum EKernel { kNone=0, kGaus=1, kLinN=2 } EKernel;

      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
      void TrainMultiClassification();           // Classification: one foam for every class

      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();
      PDEFoam* ReadClonedFoamFromFile(TFile*, const TString&);

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

      // calculate multiclass MVA values
      const std::vector<Float_t>& GetMulticlassValues();

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

      // reset the method
      virtual void Reset();

      // ranking of input variables
      const Ranking* CreateRanking();

      // get number of cuts in every dimension, starting at cell
      void GetNCuts(PDEFoamCell *cell, std::vector<UInt_t> &nCuts);

      // helper functions to convert enum types to UInt_t and back
      EKernel GetKernel( void ) { return fKernel; }
      UInt_t KernelToUInt(EKernel ker) const { return UInt_t(ker); }
      EKernel UIntToKernel(UInt_t iker);
      UInt_t TargetSelectionToUInt(ETargetSelection ts) const { return UInt_t(ts); }
      ETargetSelection UIntToTargetSelection(UInt_t its);

   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 the error on the Mva value
      Double_t CalculateMVAError();

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

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

      // create foam and set foam options
      PDEFoam* InitFoam(TString, EFoamType, UInt_t cls=0);

      // create pdefoam kernel
      PDEFoamKernelBase* CreatePDEFoamKernel();

      // delete all trained foams
      void DeleteFoams();

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

   private:

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

      // nice output
      void PrintCoefficients( void );

      // Square function (fastest implementation)
      template<typename T> T Sqr(T x) const { return x*x; }

      // options to be used
      Bool_t        fSigBgSeparated;  // Separate Sig and Bg, or not
      Float_t       fFrac;            // Fraction used for calc of Xmin, Xmax
      Float_t       fDiscrErrCut;     // cut on discrimant error
      Float_t       fVolFrac;         // 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)

      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;         // Keep for bw compatibility: Grabbing cell with maximal RMS to split next (TFoam default)
      UInt_t        fMaxDepth;        // maximum depth of cell tree

      TString       fKernelStr;       // Kernel for GetMvaValue() (option string)
      EKernel       fKernel;          // Kernel for GetMvaValue()
      PDEFoamKernelBase *fKernelEstimator;// Kernel estimator
      TString       fTargetSelectionStr; // method of selecting the target (only mulit target regr.)
      ETargetSelection fTargetSelection; // method of selecting the target (only mulit target regr.)
      Bool_t        fFillFoamWithOrigWeights; // fill the foam with boost weights
      Bool_t        fUseYesNoCell;    // return -1 or 1 for bg or signal like event
      TString       fDTLogic;         // use DT algorithm to split cells
      EDTSeparation fDTSeparation;    // enum which specifies the separation to use for the DT logic
      Bool_t        fPeekMax;         // BACKWARDS COMPATIBILITY: peek up cell with max. driver integral for split

      std::vector<Float_t> fXmin, fXmax; // range for histograms and foams

      std::vector<PDEFoam*> fFoam;    // grown PDEFoams

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

      ClassDef(MethodPDEFoam,0) // Multi-dimensional probability density estimator using TFoam (PDE-Foam)
   };

} // 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
 MethodPDEFoam.h:241
 MethodPDEFoam.h:242
 MethodPDEFoam.h:243
 MethodPDEFoam.h:244
 MethodPDEFoam.h:245
 MethodPDEFoam.h:246
 MethodPDEFoam.h:247
 MethodPDEFoam.h:248
 MethodPDEFoam.h:249