// @(#)root/tmva $Id$
// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss,Or Cohen, Jan Therhaag, Eckhard von Toerne

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : MethodCompositeBase                                                   *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Virtual base class for all MVA method                                     *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker    <Andreas.Hocker@cern.ch>   - CERN, Switzerland         *
 *      Peter Speckmayer   <Peter.Speckmazer@cern.ch> - CERN, Switzerland         *
 *      Joerg Stelzer      <Joerg.Stelzer@cern.ch>    - CERN, Switzerland         *
 *      Helge Voss         <Helge.Voss@cern.ch>       - MPI-K Heidelberg, Germany *
 *      Jan Therhaag       <Jan.Therhaag@cern.ch>     - U of Bonn, Germany        *
 *      Eckhard v. Toerne  <evt@uni-bonn.de>          - U of Bonn, Germany        *
 *                                                                                *
 * Copyright (c) 2005-2011:                                                       *
 *      CERN, Switzerland                                                         *
 *      U. of Victoria, Canada                                                    *
 *      MPI-K Heidelberg, 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_MethodBoost
#define ROOT_TMVA_MethodBoost

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// MethodBoost                                                          //
//                                                                      //
// Class for boosting a TMVA method                                     //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <iosfwd>
#include <vector>

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

#ifndef ROOT_TMVA_MethodCompositeBase
#include "TMVA/MethodCompositeBase.h"
#endif

namespace TMVA {

   class Factory;  // DSMTEST
   class Reader;   // DSMTEST
   class DataSetManager;  // DSMTEST

   class MethodBoost : public MethodCompositeBase {

   public :

      // constructors
      MethodBoost( const TString& jobName,
                   const TString& methodTitle,
                   DataSetInfo& theData,
                   const TString& theOption = "",
                   TDirectory* theTargetDir = NULL );

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

      virtual ~MethodBoost( void );

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

      // training and boosting all the classifiers
      void Train( void );

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

      // saves the name and options string of the boosted classifier
      Bool_t BookMethod( Types::EMVA theMethod, TString methodTitle, TString theOption );
      void SetBoostedMethodName ( TString methodName )     { fBoostedMethodName  = methodName; }

      Int_t          GetBoostNum() { return fBoostNum; }

      void CleanBoostOptions();

      Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper = 0 );

   private :
      // clean up
      void ClearAll();

      // print fit results
      void PrintResults( const TString&, std::vector<Double_t>&, const Double_t ) const;

      // initializing mostly monitoring tools of the boost process
      void Init();
      void InitHistos();
      void CheckSetup();

      void MonitorBoost( Types::EBoostStage stage, UInt_t methodIdx=0);

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


      MethodBase*  CurrentMethod(){return fCurrentMethod;}
      UInt_t       CurrentMethodIdx(){return fCurrentMethodIdx;}
      // training a single classifier
      void SingleTrain();

      // calculating a boosting weight from the classifier, storing it in the next one
      Double_t SingleBoost(MethodBase* method);
      Double_t AdaBoost(MethodBase* method, Bool_t useYesNoLeaf );
      Double_t Bagging();


      // calculate weight of single method
      Double_t CalcMethodWeight();

      // return ROC integral on training/testing sample
      Double_t GetBoostROCIntegral(Bool_t, Types::ETreeType, Bool_t CalcOverlapIntergral=kFALSE);

      // writing the monitoring histograms and tree to a file
      void WriteMonitoringHistosToFile( void ) const;

      // write evaluation histograms into target file
      virtual void WriteEvaluationHistosToFile(Types::ETreeType treetype);

      // performs the MethodBase testing + testing of each boosted classifier
      virtual void TestClassification();

      // finding the MVA to cut between sig and bgd according to fMVACutPerc,fMVACutType
      void FindMVACut(MethodBase* method);

      // setting all the boost weights to 1
      void ResetBoostWeights();

      // creating the vectors of histogram for monitoring MVA response of each classifier
      void CreateMVAHistorgrams();

      // calculate MVA values of current trained method on training
      // sample
      void CalcMVAValues();
      
      UInt_t                 fBoostNum;           // Number of times the classifier is boosted
      TString                fBoostType;          // string specifying the boost type      

      TString                fTransformString;    // min and max values for the classifier response      
      Bool_t                 fDetailedMonitoring; // produce detailed monitoring histograms (boost-wise)
      
      Double_t               fAdaBoostBeta;       // ADA boost parameter, default is 1      
      UInt_t                 fRandomSeed;         // seed for random number generator used for bagging
      Double_t               fBaggedSampleFraction;// rel.Size of bagged sample
      
      TString                fBoostedMethodName;    // details of the boosted classifier
      TString                fBoostedMethodTitle;   // title 
      TString                fBoostedMethodOptions; // options
      
      Bool_t                 fMonitorBoostedMethod; // monitor the MVA response of every classifier

      // MVA output from each classifier over the training hist, using orignal events weights
      std::vector< TH1* >   fTrainSigMVAHist;
      std::vector< TH1* >   fTrainBgdMVAHist;
      // MVA output from each classifier over the training hist, using boosted events weights
      std::vector< TH1* >   fBTrainSigMVAHist;
      std::vector< TH1* >   fBTrainBgdMVAHist;
      // MVA output from each classifier over the testing hist
      std::vector< TH1* >   fTestSigMVAHist;
      std::vector
< TH1* >   fTestBgdMVAHist;
      
      //monitoring tree/ntuple and it's variables
      TTree*                fMonitorTree;     // tree  to monitor values during the boosting      
      Double_t              fBoostWeight;        // the weight used to boost the next classifier      
      Double_t              fMethodError;     // estimation of the level error of the classifier 
                                                  // analysing the train dataset      
      Double_t           fROC_training;       // roc integral of last trained method (on training sample)

      // overlap integral of mva distributions for signal and
      // background (training sample)
      Double_t           fOverlap_integral;
      
      std::vector<Float_t> *fMVAvalues;       // mva values for the last trained method

      DataSetManager*    fDataSetManager;     // DSMTEST
      friend class Factory;                   // DSMTEST
      friend class Reader;                    // DSMTEST      

      TString fHistoricOption;    //historic variable, only needed for "CompatibilityOptions" 
      Bool_t fHistoricBoolOption; //historic variable, only needed for "CompatibilityOptions" 

   protected:

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

      ClassDef(MethodBoost,0)
   };
}

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