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

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : Factory                                                               *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      This is the main MVA steering class: it creates (books) all MVA methods,  *
 *      and guides them through the training, testing and evaluation phases.      *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Joerg Stelzer   <stelzer@cern.ch>        - DESY, Germany                  *
 *      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_Factory
#define ROOT_TMVA_Factory

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// Factory                                                              //
//                                                                      //
// This is the main MVA steering class: it creates all MVA methods,     //
// and guides them through the training, testing and evaluation         //
// phases                                                               //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <string>
#include <vector>
#include <map>
#ifndef ROOT_TCut
#include "TCut.h"
#endif

#ifndef ROOT_TMVA_Configurable
#include "TMVA/Configurable.h"
#endif
#ifndef ROOT_TMVA_Types
#include "TMVA/Types.h"
#endif
#ifndef ROOT_TMVA_DataSet
#include "TMVA/DataSet.h"
#endif

class TFile;
class TTree;
class TDirectory;

namespace TMVA {

   class IMethod;
   class MethodBase;
   class DataInputHandler;
   class DataSetInfo;
   class VariableTransformBase;

   class Factory : public Configurable {
   public:

      typedef std::vector<IMethod*> MVector;

      // no default  constructor
      Factory( TString theJobName, TFile* theTargetFile, TString theOption = "" );

      // default destructor
      virtual ~Factory();

      virtual const char*  GetName() const { return "Factory"; }

      // add events to training and testing trees
      void AddSignalTrainingEvent    ( const std::vector<Double_t>& event, Double_t weight = 1.0 );
      void AddBackgroundTrainingEvent( const std::vector<Double_t>& event, Double_t weight = 1.0 );
      void AddSignalTestEvent        ( const std::vector<Double_t>& event, Double_t weight = 1.0 );
      void AddBackgroundTestEvent    ( const std::vector<Double_t>& event, Double_t weight = 1.0 );
      void AddTrainingEvent( const TString& className, const std::vector<Double_t>& event, Double_t weight );
      void AddTestEvent    ( const TString& className, const std::vector<Double_t>& event, Double_t weight );
      void AddEvent        ( const TString& className, Types::ETreeType tt, const std::vector<Double_t>& event, Double_t weight );
      Bool_t UserAssignEvents(UInt_t clIndex);
      TTree* CreateEventAssignTrees( const TString& name );

      DataSetInfo& AddDataSet( DataSetInfo& );
      DataSetInfo& AddDataSet( const TString&  );

      // special case: signal/background

      // Data input related
      void SetInputTrees( const TString& signalFileName, const TString& backgroundFileName, 
                          Double_t signalWeight=1.0, Double_t backgroundWeight=1.0 );
      void SetInputTrees( TTree* inputTree, const TCut& SigCut, const TCut& BgCut );
      // Set input trees  at once
      void SetInputTrees( TTree* signal, TTree* background, 
                          Double_t signalWeight=1.0, Double_t backgroundWeight=1.0) ;

      void AddSignalTree( TTree* signal,    Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
      void AddSignalTree( TString datFileS, Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
      void AddSignalTree( TTree* signal, Double_t weight, const TString& treetype );      

      // ... depreciated, kept for backwards compatibility
      void SetSignalTree( TTree* signal, Double_t weight=1.0);

      void AddBackgroundTree( TTree* background, Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
      void AddBackgroundTree( TString datFileB,  Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
      void AddBackgroundTree( TTree* background, Double_t weight, const TString & treetype );

      // ... depreciated, kept for backwards compatibility
      void SetBackgroundTree( TTree* background, Double_t weight=1.0 );

      void SetSignalWeightExpression( const TString& variable );
      void SetBackgroundWeightExpression( const TString& variable );

      // special case: regression
      void AddRegressionTree( TTree* tree, Double_t weight = 1.0,  
                              Types::ETreeType treetype = Types::kMaxTreeType ) { 
         AddTree( tree, "Regression", weight, "", treetype ); 
      }

      // general

      // Data input related
      void SetTree( TTree* tree, const TString& className, Double_t weight ); // depreciated
      void AddTree( TTree* tree, const TString& className, Double_t weight=1.0,
                    const TCut& cut = "",
                    Types::ETreeType tt = Types::kMaxTreeType );
      void AddTree( TTree* tree, const TString& className, Double_t weight, const TCut& cut, const TString& treeType );

      // set input variable
      void SetInputVariables  ( std::vector<TString>* theVariables ); // depreciated
      void AddVariable        ( const TString& expression, const TString& title, const TString& unit,
                                char type='F', Double_t min = 0, Double_t max = 0 );
      void AddVariable        ( const TString& expression, char type='F',
                                Double_t min = 0, Double_t max = 0 );
      void AddTarget          ( const TString& expression, const TString& title = "", const TString& unit = "",
                                Double_t min = 0, Double_t max = 0 );
      void AddRegressionTarget( const TString& expression, const TString& title = "", const TString& unit = "",
                                Double_t min = 0, Double_t max = 0 )
      {
         AddTarget( expression, title, unit, min, max );
      }
      void AddSpectator         ( const TString& expression, const TString& title = "", const TString& unit = "",
                                Double_t min = 0, Double_t max = 0 );

      // set weight for class
      void SetWeightExpression( const TString& variable, const TString& className = "" );

      // set cut for class
      void SetCut( const TString& cut, const TString& className = "" );
      void SetCut( const TCut& cut, const TString& className = "" );
      void AddCut( const TString& cut, const TString& className = "" );
      void AddCut( const TCut& cut, const TString& className = "" );


      //  prepare input tree for training
      void PrepareTrainingAndTestTree( const TCut& cut, const TString& splitOpt );
      void PrepareTrainingAndTestTree( TCut sigcut, TCut bkgcut, const TString& splitOpt );

      // ... deprecated, kept for backwards compatibility 
      void PrepareTrainingAndTestTree( const TCut& cut, Int_t Ntrain, Int_t Ntest = -1 );

      void PrepareTrainingAndTestTree( const TCut& cut, Int_t NsigTrain, Int_t NbkgTrain, Int_t NsigTest, Int_t NbkgTest, 
                                       const TString& otherOpt="SplitMode=Random:!V" );

      MethodBase* BookMethod( TString theMethodName, TString methodTitle, TString theOption = "" );
      MethodBase* BookMethod( Types::EMVA theMethod,  TString methodTitle, TString theOption = "" );
      MethodBase* BookMethod( TMVA::Types::EMVA /*theMethod*/, 
                              TString /*methodTitle*/, 
                              TString /*methodOption*/, 
                              TMVA::Types::EMVA /*theCommittee*/, 
                              TString /*committeeOption = ""*/ ) { return 0; } 

      // training for all booked methods
      void TrainAllMethods                 ( TString what = "Classification" );
      void TrainAllMethodsForClassification( void ) { TrainAllMethods( "Classification" ); }
      void TrainAllMethodsForRegression    ( void ) { TrainAllMethods( "Regression"     ); }

      // testing
      void TestAllMethods();

      // performance evaluation
      void EvaluateAllMethods( void );
      void EvaluateAllVariables( TString options = "" ); 
  
      // delete all methods and reset the method vector
      void DeleteAllMethods( void );

      // accessors
      IMethod* GetMethod( const TString& title ) const;

      Bool_t Verbose( void ) const { return fVerbose; }
      void SetVerbose( Bool_t v=kTRUE );

      // make ROOT-independent C++ class for classifier response 
      // (classifier-specific implementation)
      // If no classifier name is given, help messages for all booked 
      // classifiers are printed
      virtual void MakeClass( const TString& methodTitle = "" ) const;

      // prints classifier-specific hepl messages, dedicated to 
      // help with the optimisation and configuration options tuning.
      // If no classifier name is given, help messages for all booked 
      // classifiers are printed
      void PrintHelpMessage( const TString& methodTitle = "" ) const;

      static TDirectory* RootBaseDir() { return (TDirectory*)fgTargetFile; }

   private:

      // the beautiful greeting message
      void Greetings();

      void WriteDataInformation();

      DataInputHandler&        DataInput() { return *fDataInputHandler; }
      DataSetInfo&             DefaultDataSetInfo();
      void                     SetInputTreesFromEventAssignTrees();


   private:

      // data members

      static TFile*                             fgTargetFile;     //! ROOT output file

      DataInputHandler*                         fDataInputHandler;

      std::vector<TMVA::VariableTransformBase*> fDefaultTrfs;     //! list of transformations on default DataSet

      // cd to local directory
      TString                                   fOptions;         //! option string given by construction (presently only "V")
      TString                                   fTransformations; //! List of transformations to test
      Bool_t                                    fVerbose;         //! verbose mode

      MVector                                   fMethods;         //! all MVA methods
      TString                                   fJobName;         //! jobname, used as extension in weight file names

      // flag determining the way training and test data are assigned to Factory
      enum DataAssignType { kUndefined = 0, 
                            kAssignTrees,
                            kAssignEvents };
      DataAssignType                            fDataAssignType;  //! flags for data assigning
      std::vector<TTree*>                       fTrainAssignTree; //! for each class: tmp tree if user wants to assign the events directly
      std::vector<TTree*>                       fTestAssignTree;  //! for each class: tmp tree if user wants to assign the events directly

      Int_t                                     fATreeType;          // type of event (=classIndex)
      Float_t                                   fATreeWeight;        // weight of the event
      Float_t*                                  fATreeEvent;         // event variables

   protected:

      ClassDef(Factory,0)  // The factory creates all MVA methods, and performs their training and testing
   };

} // namespace TMVA

#endif

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