Logo ROOT   6.14/05
Reference Guide
Factory.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne, Jan Therhaag
3 // Updated by: Omar Zapata, Lorenzo Moneta, Sergei Gleyzer
4 
5 /**********************************************************************************
6  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
7  * Package: TMVA *
8  * Class : Factory *
9  * Web : http://tmva.sourceforge.net *
10  * *
11  * Description: *
12  * This is the main MVA steering class: it creates (books) all MVA methods, *
13  * and guides them through the training, testing and evaluation phases. *
14  * *
15  * Authors (alphabetical): *
16  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
17  * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
18  * Peter Speckmayer <peter.speckmayer@cern.ch> - CERN, Switzerland *
19  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
20  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
21  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
22  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
23  * Omar Zapata <Omar.Zapata@cern.ch> - UdeA/ITM Colombia *
24  * Lorenzo Moneta <Lorenzo.Moneta@cern.ch> - CERN, Switzerland *
25  * Sergei Gleyzer <Sergei.Gleyzer@cern.ch> - U of Florida & CERN *
26  * *
27  * Copyright (c) 2005-2011: *
28  * CERN, Switzerland *
29  * U. of Victoria, Canada *
30  * MPI-K Heidelberg, Germany *
31  * U. of Bonn, Germany *
32  * UdeA/ITM, Colombia *
33  * U. of Florida, USA *
34  * *
35  * Redistribution and use in source and binary forms, with or without *
36  * modification, are permitted according to the terms listed in LICENSE *
37  * (http://tmva.sourceforge.net/LICENSE) *
38  **********************************************************************************/
39 
40 #ifndef ROOT_TMVA_Factory
41 #define ROOT_TMVA_Factory
42 
43 //////////////////////////////////////////////////////////////////////////
44 // //
45 // Factory //
46 // //
47 // This is the main MVA steering class: it creates all MVA methods, //
48 // and guides them through the training, testing and evaluation //
49 // phases //
50 // //
51 //////////////////////////////////////////////////////////////////////////
52 
53 #include <string>
54 #include <vector>
55 #include <map>
56 #include "TCut.h"
57 
58 #include "TMVA/Configurable.h"
59 #include "TMVA/Types.h"
60 #include "TMVA/DataSet.h"
61 
62 class TCanvas;
63 class TDirectory;
64 class TFile;
65 class TGraph;
66 class TH1F;
67 class TMultiGraph;
68 class TTree;
69 namespace TMVA {
70 
71  class IMethod;
72  class MethodBase;
73  class DataInputHandler;
74  class DataSetInfo;
75  class DataSetManager;
76  class DataLoader;
77  class ROCCurve;
78  class VariableTransformBase;
79 
80 
81  class Factory : public Configurable {
82  friend class CrossValidation;
83  public:
84 
85  typedef std::vector<IMethod*> MVector;
86  std::map<TString,MVector*> fMethodsMap;//all methods for every dataset with the same name
87 
88  // no default constructor
89  Factory( TString theJobName, TFile* theTargetFile, TString theOption = "" );
90 
91  // contructor to work without file
92  Factory( TString theJobName, TString theOption = "" );
93 
94  // default destructor
95  virtual ~Factory();
96 
97  virtual const char* GetName() const { return "Factory"; }
98 
99 
100  MethodBase* BookMethod( DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption = "" );
101  MethodBase* BookMethod( DataLoader *loader, Types::EMVA theMethod, TString methodTitle, TString theOption = "" );
103  TString /*methodTitle*/,
104  TString /*methodOption*/,
105  TMVA::Types::EMVA /*theComposite*/,
106  TString /*compositeOption = ""*/ ) { return 0; }
107 
108  // optimize all booked methods (well, if desired by the method)
109  std::map<TString,Double_t> OptimizeAllMethods (TString fomType="ROCIntegral", TString fitType="FitGA");
110  void OptimizeAllMethodsForClassification(TString fomType="ROCIntegral", TString fitType="FitGA") { OptimizeAllMethods(fomType,fitType); }
111  void OptimizeAllMethodsForRegression (TString fomType="ROCIntegral", TString fitType="FitGA") { OptimizeAllMethods(fomType,fitType); }
112 
113  // training for all booked methods
114  void TrainAllMethods ();
117 
118  // testing
119  void TestAllMethods();
120 
121  // performance evaluation
122  void EvaluateAllMethods( void );
123  void EvaluateAllVariables(DataLoader *loader, TString options = "" );
124 
125  TH1F* EvaluateImportance( DataLoader *loader,VIType vitype, Types::EMVA theMethod, TString methodTitle, const char *theOption = "" );
126 
127  // delete all methods and reset the method vector
128  void DeleteAllMethods( void );
129 
130  // accessors
131  IMethod* GetMethod( const TString& datasetname, const TString& title ) const;
132  Bool_t HasMethod( const TString& datasetname, const TString& title ) const;
133 
134  Bool_t Verbose( void ) const { return fVerbose; }
135  void SetVerbose( Bool_t v=kTRUE );
136 
137  // make ROOT-independent C++ class for classifier response
138  // (classifier-specific implementation)
139  // If no classifier name is given, help messages for all booked
140  // classifiers are printed
141  virtual void MakeClass(const TString& datasetname , const TString& methodTitle = "" ) const;
142 
143  // prints classifier-specific hepl messages, dedicated to
144  // help with the optimisation and configuration options tuning.
145  // If no classifier name is given, help messages for all booked
146  // classifiers are printed
147  void PrintHelpMessage(const TString& datasetname , const TString& methodTitle = "" ) const;
148 
150 
153 
154  Double_t GetROCIntegral(DataLoader *loader, TString theMethodName, UInt_t iClass = 0);
155  Double_t GetROCIntegral(TString datasetname, TString theMethodName, UInt_t iClass = 0);
156 
157  // Methods to get a TGraph for an indicated method in dataset.
158  // Optional title and axis added with fLegend=kTRUE.
159  // Argument iClass used in multiclass settings, otherwise ignored.
160  TGraph* GetROCCurve(DataLoader *loader, TString theMethodName, Bool_t setTitles=kTRUE, UInt_t iClass=0);
161  TGraph* GetROCCurve(TString datasetname, TString theMethodName, Bool_t setTitles=kTRUE, UInt_t iClass=0);
162 
163  // Methods to get a TMultiGraph for a given class and all methods in dataset.
165  TMultiGraph* GetROCCurveAsMultiGraph(TString datasetname, UInt_t iClass);
166 
167  // Draw all ROC curves of a given class for all methods in the dataset.
168  TCanvas* GetROCCurve(DataLoader *loader, UInt_t iClass=0);
169  TCanvas* GetROCCurve(TString datasetname, UInt_t iClass=0);
170 
171  private:
172 
173  // the beautiful greeting message
174  void Greetings();
175 
176  //evaluate the simple case that is removing 1 variable at time
177  TH1F* EvaluateImportanceShort( DataLoader *loader,Types::EMVA theMethod, TString methodTitle, const char *theOption = "" );
178  //evaluate all variables combinations
179  TH1F* EvaluateImportanceAll( DataLoader *loader,Types::EMVA theMethod, TString methodTitle, const char *theOption = "" );
180  //evaluate randomly given a number of seeds
181  TH1F* EvaluateImportanceRandom( DataLoader *loader,UInt_t nseeds, Types::EMVA theMethod, TString methodTitle, const char *theOption = "" );
182 
183  TH1F* GetImportance(const int nbits,std::vector<Double_t> importances,std::vector<TString> varNames);
184 
185  // Helpers for public facing ROC methods
186  ROCCurve *GetROC(DataLoader *loader, TString theMethodName, UInt_t iClass = 0,
188  ROCCurve *GetROC(TString datasetname, TString theMethodName, UInt_t iClass = 0,
190 
191  void WriteDataInformation(DataSetInfo& fDataSetInfo);
192 
194 
196 
197  private:
198 
199  // data members
200 
201  TFile* fgTargetFile; //! ROOT output file
202 
203 
204  std::vector<TMVA::VariableTransformBase*> fDefaultTrfs; //! list of transformations on default DataSet
205 
206  // cd to local directory
207  TString fOptions; //! option string given by construction (presently only "V")
208  TString fTransformations; //! list of transformations to test
209  Bool_t fVerbose; //! verbose mode
210  TString fVerboseLevel; //! verbosity level, controls granularity of logging
211  Bool_t fCorrelations; //! enable to calculate corelations
212  Bool_t fROC; //! enable to calculate ROC values
213  Bool_t fSilentFile; //! used in contructor wihtout file
214 
215  TString fJobName; //! jobname, used as extension in weight file names
216 
217  Types::EAnalysisType fAnalysisType; //! the training type
218  Bool_t fModelPersistence;//! option to save the trained model in xml file or using serialization
219 
220 
221  protected:
222 
223  ClassDef(Factory,0); // The factory creates all MVA methods, and performs their training and testing
224  };
225 
226 } // namespace TMVA
227 
228 #endif
229 
TH1F * GetImportance(const int nbits, std::vector< Double_t > importances, std::vector< TString > varNames)
Definition: Factory.cxx:2484
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
Definition: Factory.cxx:358
std::vector< TMVA::VariableTransformBase * > fDefaultTrfs
ROOT output file.
Definition: Factory.h:204
void EvaluateAllVariables(DataLoader *loader, TString options="")
Iterates over all MVA input variables and evaluates them.
Definition: Factory.cxx:1318
Bool_t fROC
enable to calculate corelations
Definition: Factory.h:212
void OptimizeAllMethodsForClassification(TString fomType="ROCIntegral", TString fitType="FitGA")
Definition: Factory.h:110
TH1F * EvaluateImportanceShort(DataLoader *loader, Types::EMVA theMethod, TString methodTitle, const char *theOption="")
Definition: Factory.cxx:2267
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
Bool_t Verbose(void) const
Definition: Factory.h:134
EAnalysisType
Definition: Types.h:127
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:35
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
void OptimizeAllMethodsForRegression(TString fomType="ROCIntegral", TString fitType="FitGA")
Definition: Factory.h:111
TString fTransformations
option string given by construction (presently only "V")
Definition: Factory.h:208
Basic string class.
Definition: TString.h:131
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
bool Bool_t
Definition: RtypesCore.h:59
void TrainAllMethods()
Iterates through all booked methods and calls training.
Definition: Factory.cxx:1093
TMultiGraph * GetROCCurveAsMultiGraph(DataLoader *loader, UInt_t iClass)
Generate a collection of graphs, for all methods for a given class.
Definition: Factory.cxx:972
void WriteDataInformation(DataSetInfo &fDataSetInfo)
Definition: Factory.cxx:596
void TrainAllMethodsForClassification(void)
Definition: Factory.h:115
TH1F * EvaluateImportanceRandom(DataLoader *loader, UInt_t nseeds, Types::EMVA theMethod, TString methodTitle, const char *theOption="")
Definition: Factory.cxx:2372
TGraph * GetROCCurve(DataLoader *loader, TString theMethodName, Bool_t setTitles=kTRUE, UInt_t iClass=0)
Argument iClass specifies the class to generate the ROC curve in a multiclass setting.
Definition: Factory.cxx:903
void TrainAllMethodsForRegression(void)
Definition: Factory.h:116
#define ClassDef(name, id)
Definition: Rtypes.h:320
TH1F * EvaluateImportance(DataLoader *loader, VIType vitype, Types::EMVA theMethod, TString methodTitle, const char *theOption="")
Evaluate Variable Importance.
Definition: Factory.cxx:2148
Bool_t fModelPersistence
the training type
Definition: Factory.h:218
TString fVerboseLevel
verbose mode
Definition: Factory.h:210
TH1F * EvaluateImportanceAll(DataLoader *loader, Types::EMVA theMethod, TString methodTitle, const char *theOption="")
Definition: Factory.cxx:2171
Bool_t IsModelPersistence()
Definition: Factory.cxx:304
Class that contains all the data information.
Definition: DataSetInfo.h:60
std::map< TString, MVector * > fMethodsMap
Definition: Factory.h:86
Bool_t fSilentFile
enable to calculate ROC values
Definition: Factory.h:213
virtual ~Factory()
Destructor.
Definition: Factory.cxx:312
std::map< TString, Double_t > OptimizeAllMethods(TString fomType="ROCIntegral", TString fitType="FitGA")
Iterates through all booked methods and sees if they use parameter tuning and if so.
Definition: Factory.cxx:694
TDirectory * RootBaseDir()
Definition: Factory.h:149
Double_t GetROCIntegral(DataLoader *loader, TString theMethodName, UInt_t iClass=0)
Calculate the integral of the ROC curve, also known as the area under curve (AUC), for a given method.
Definition: Factory.cxx:842
SVector< double, 2 > v
Definition: Dict.h:5
Bool_t HasMethod(const TString &datasetname, const TString &title) const
Checks whether a given method name is defined for a given dataset.
Definition: Factory.cxx:579
Bool_t fCorrelations
verbosity level, controls granularity of logging
Definition: Factory.h:211
void EvaluateAllMethods(void)
Iterates over all MVAs that have been booked, and calls their evaluation methods. ...
Definition: Factory.cxx:1333
void TestAllMethods()
Evaluates all booked methods on the testing data and adds the output to the Results in the corresponi...
Definition: Factory.cxx:1231
unsigned int UInt_t
Definition: RtypesCore.h:42
void Greetings()
Print welcome message.
Definition: Factory.cxx:288
This is the main MVA steering class.
Definition: Factory.h:81
void SetVerbose(Bool_t v=kTRUE)
Definition: Factory.cxx:350
TFile * fgTargetFile
Definition: Factory.h:201
void SetInputTreesFromEventAssignTrees()
The Canvas class.
Definition: TCanvas.h:31
ROCCurve * GetROC(DataLoader *loader, TString theMethodName, UInt_t iClass=0, Types::ETreeType type=Types::kTesting)
Private method to generate a ROCCurve instance for a given method.
Definition: Factory.cxx:743
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:34
Class to perform cross validation, splitting the dataloader into folds.
int type
Definition: TGX11.cxx:120
Interface for all concrete MVA method implementations.
Definition: IMethod.h:54
void PrintHelpMessage(const TString &datasetname, const TString &methodTitle="") const
Print predefined help message of classifier.
Definition: Factory.cxx:1291
Abstract ClassifierFactory template that handles arbitrary types.
TString fOptions
list of transformations on default DataSet
Definition: Factory.h:207
Factory(TString theJobName, TFile *theTargetFile, TString theOption="")
Standard constructor.
Definition: Factory.cxx:119
TString fJobName
used in contructor wihtout file
Definition: Factory.h:215
MethodBase * BookMethod(DataLoader *, TMVA::Types::EMVA, TString, TString, TMVA::Types::EMVA, TString)
Definition: Factory.h:102
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
void DeleteAllMethods(void)
Delete methods.
Definition: Factory.cxx:330
Bool_t IsSilentFile()
Definition: Factory.cxx:297
A TTree object has a header with a name and a title.
Definition: TTree.h:70
Types::EAnalysisType fAnalysisType
jobname, used as extension in weight file names
Definition: Factory.h:217
std::vector< IMethod * > MVector
Definition: Factory.h:85
virtual const char * GetName() const
Returns name of object.
Definition: Factory.h:97
virtual void MakeClass(const TString &datasetname, const TString &methodTitle="") const
Definition: Factory.cxx:1263
IMethod * GetMethod(const TString &datasetname, const TString &title) const
Returns pointer to MVA that corresponds to given method title.
Definition: Factory.cxx:561
const Bool_t kTRUE
Definition: RtypesCore.h:87
Bool_t fVerbose
list of transformations to test
Definition: Factory.h:209
MethodBase * BookMethodWeightfile(DataLoader *dataloader, TMVA::Types::EMVA methodType, const TString &weightfile)
Adds an already constructed method to be managed by this factory.
Definition: Factory.cxx:503