ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Factory *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * This is the main MVA steering class: it creates (books) all MVA methods, *
12  * and guides them through the training, testing and evaluation phases. *
13  * *
14  * Authors (alphabetical): *
15  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
16  * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
17  * Peter Speckmayer <peter.speckmayer@cern.ch> - CERN, Switzerland *
18  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
19  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
20  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
21  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
22  * *
23  * Copyright (c) 2005-2011: *
24  * CERN, Switzerland *
25  * U. of Victoria, Canada *
26  * MPI-K Heidelberg, Germany *
27  * U. of Bonn, Germany *
28  * *
29  * Redistribution and use in source and binary forms, with or without *
30  * modification, are permitted according to the terms listed in LICENSE *
31  * (http://tmva.sourceforge.net/LICENSE) *
32  **********************************************************************************/
33 
34 #ifndef ROOT_TMVA_Factory
35 #define ROOT_TMVA_Factory
36 
37 //////////////////////////////////////////////////////////////////////////
38 // //
39 // Factory //
40 // //
41 // This is the main MVA steering class: it creates all MVA methods, //
42 // and guides them through the training, testing and evaluation //
43 // phases //
44 // //
45 //////////////////////////////////////////////////////////////////////////
46 
47 #include <string>
48 #include <vector>
49 #include <map>
50 #ifndef ROOT_TCut
51 #include "TCut.h"
52 #endif
53 
54 #ifndef ROOT_TMVA_Configurable
55 #include "TMVA/Configurable.h"
56 #endif
57 #ifndef ROOT_TMVA_Types
58 #include "TMVA/Types.h"
59 #endif
60 #ifndef ROOT_TMVA_DataSet
61 #include "TMVA/DataSet.h"
62 #endif
63 
64 class TFile;
65 class TTree;
66 class TDirectory;
67 
68 namespace TMVA {
69 
70  class IMethod;
71  class MethodBase;
72  class DataInputHandler;
73  class DataSetInfo;
74  class DataSetManager;
75  class VariableTransformBase;
76 
77  class Factory : public Configurable {
78  public:
79 
80  typedef std::vector<IMethod*> MVector;
81 
82  // no default constructor
83  Factory( TString theJobName, TFile* theTargetFile, TString theOption = "" );
84 
85  // default destructor
86  virtual ~Factory();
87 
88  virtual const char* GetName() const { return "Factory"; }
89 
90  // add events to training and testing trees
91  void AddSignalTrainingEvent ( const std::vector<Double_t>& event, Double_t weight = 1.0 );
92  void AddBackgroundTrainingEvent( const std::vector<Double_t>& event, Double_t weight = 1.0 );
93  void AddSignalTestEvent ( const std::vector<Double_t>& event, Double_t weight = 1.0 );
94  void AddBackgroundTestEvent ( const std::vector<Double_t>& event, Double_t weight = 1.0 );
95  void AddTrainingEvent( const TString& className, const std::vector<Double_t>& event, Double_t weight );
96  void AddTestEvent ( const TString& className, const std::vector<Double_t>& event, Double_t weight );
97  void AddEvent ( const TString& className, Types::ETreeType tt, const std::vector<Double_t>& event, Double_t weight );
100 
102  DataSetInfo& AddDataSet( const TString& );
103 
104  // special case: signal/background
105 
106  // Data input related
107  void SetInputTrees( const TString& signalFileName, const TString& backgroundFileName,
108  Double_t signalWeight=1.0, Double_t backgroundWeight=1.0 );
109  void SetInputTrees( TTree* inputTree, const TCut& SigCut, const TCut& BgCut );
110  // Set input trees at once
111  void SetInputTrees( TTree* signal, TTree* background,
112  Double_t signalWeight=1.0, Double_t backgroundWeight=1.0) ;
113 
114  void AddSignalTree( TTree* signal, Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
115  void AddSignalTree( TString datFileS, Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
116  void AddSignalTree( TTree* signal, Double_t weight, const TString& treetype );
117 
118  // ... depreciated, kept for backwards compatibility
119  void SetSignalTree( TTree* signal, Double_t weight=1.0);
120 
122  void AddBackgroundTree( TString datFileB, Double_t weight=1.0, Types::ETreeType treetype = Types::kMaxTreeType );
123  void AddBackgroundTree( TTree* background, Double_t weight, const TString & treetype );
124 
125  // ... depreciated, kept for backwards compatibility
126  void SetBackgroundTree( TTree* background, Double_t weight=1.0 );
127 
128  void SetSignalWeightExpression( const TString& variable );
129  void SetBackgroundWeightExpression( const TString& variable );
130 
131  // special case: regression
132  void AddRegressionTree( TTree* tree, Double_t weight = 1.0,
133  Types::ETreeType treetype = Types::kMaxTreeType ) {
134  AddTree( tree, "Regression", weight, "", treetype );
135  }
136 
137  // general
138 
139  // Data input related
140  void SetTree( TTree* tree, const TString& className, Double_t weight ); // depreciated
141  void AddTree( TTree* tree, const TString& className, Double_t weight=1.0,
142  const TCut& cut = "",
144  void AddTree( TTree* tree, const TString& className, Double_t weight, const TCut& cut, const TString& treeType );
145 
146  // set input variable
147  void SetInputVariables ( std::vector<TString>* theVariables ); // depreciated
148  void AddVariable ( const TString& expression, const TString& title, const TString& unit,
149  char type='F', Double_t min = 0, Double_t max = 0 );
150  void AddVariable ( const TString& expression, char type='F',
151  Double_t min = 0, Double_t max = 0 );
152  void AddTarget ( const TString& expression, const TString& title = "", const TString& unit = "",
153  Double_t min = 0, Double_t max = 0 );
154  void AddRegressionTarget( const TString& expression, const TString& title = "", const TString& unit = "",
155  Double_t min = 0, Double_t max = 0 )
156  {
157  AddTarget( expression, title, unit, min, max );
158  }
159  void AddSpectator ( const TString& expression, const TString& title = "", const TString& unit = "",
160  Double_t min = 0, Double_t max = 0 );
161 
162  // set weight for class
163  void SetWeightExpression( const TString& variable, const TString& className = "" );
164 
165  // set cut for class
166  void SetCut( const TString& cut, const TString& className = "" );
167  void SetCut( const TCut& cut, const TString& className = "" );
168  void AddCut( const TString& cut, const TString& className = "" );
169  void AddCut( const TCut& cut, const TString& className = "" );
170 
171 
172  // prepare input tree for training
173  void PrepareTrainingAndTestTree( const TCut& cut, const TString& splitOpt );
174  void PrepareTrainingAndTestTree( TCut sigcut, TCut bkgcut, const TString& splitOpt );
175 
176  // ... deprecated, kept for backwards compatibility
177  void PrepareTrainingAndTestTree( const TCut& cut, Int_t Ntrain, Int_t Ntest = -1 );
178 
179  void PrepareTrainingAndTestTree( const TCut& cut, Int_t NsigTrain, Int_t NbkgTrain, Int_t NsigTest, Int_t NbkgTest,
180  const TString& otherOpt="SplitMode=Random:!V" );
181 
182  MethodBase* BookMethod( TString theMethodName, TString methodTitle, TString theOption = "" );
183  MethodBase* BookMethod( Types::EMVA theMethod, TString methodTitle, TString theOption = "" );
185  TString /*methodTitle*/,
186  TString /*methodOption*/,
187  TMVA::Types::EMVA /*theComposite*/,
188  TString /*compositeOption = ""*/ ) { return 0; }
189 
190  // optimize all booked methods (well, if desired by the method)
191  void OptimizeAllMethods (TString fomType="ROCIntegral", TString fitType="FitGA");
192  void OptimizeAllMethodsForClassification(TString fomType="ROCIntegral", TString fitType="FitGA") { OptimizeAllMethods(fomType,fitType); }
193  void OptimizeAllMethodsForRegression (TString fomType="ROCIntegral", TString fitType="FitGA") { OptimizeAllMethods(fomType,fitType); }
194 
195  // training for all booked methods
196  void TrainAllMethods ();
199 
200  // testing
201  void TestAllMethods();
202 
203  // performance evaluation
204  void EvaluateAllMethods( void );
205  void EvaluateAllVariables( TString options = "" );
206 
207  // delete all methods and reset the method vector
208  void DeleteAllMethods( void );
209 
210  // accessors
211  IMethod* GetMethod( const TString& title ) const;
212 
213  Bool_t Verbose( void ) const { return fVerbose; }
214  void SetVerbose( Bool_t v=kTRUE );
215 
216  // make ROOT-independent C++ class for classifier response
217  // (classifier-specific implementation)
218  // If no classifier name is given, help messages for all booked
219  // classifiers are printed
220  virtual void MakeClass( const TString& methodTitle = "" ) const;
221 
222  // prints classifier-specific hepl messages, dedicated to
223  // help with the optimisation and configuration options tuning.
224  // If no classifier name is given, help messages for all booked
225  // classifiers are printed
226  void PrintHelpMessage( const TString& methodTitle = "" ) const;
227 
229 
230  private:
231 
232  // the beautiful greeting message
233  void Greetings();
234 
235  void WriteDataInformation();
236 
240 
241 
242  private:
243 
244  // data members
245 
246 
248 
249  static TFile* fgTargetFile; //! ROOT output file
250 
252 
253  std::vector<TMVA::VariableTransformBase*> fDefaultTrfs; //! list of transformations on default DataSet
254 
255  // cd to local directory
256  TString fOptions; //! option string given by construction (presently only "V")
257  TString fTransformations; //! List of transformations to test
258  Bool_t fVerbose; //! verbose mode
259 
260  MVector fMethods; //! all MVA methods
261  TString fJobName; //! jobname, used as extension in weight file names
262 
263  // flag determining the way training and test data are assigned to Factory
267  DataAssignType fDataAssignType; //! flags for data assigning
268  std::vector<TTree*> fTrainAssignTree; //! for each class: tmp tree if user wants to assign the events directly
269  std::vector<TTree*> fTestAssignTree; //! for each class: tmp tree if user wants to assign the events directly
270 
271  Int_t fATreeType; // type of event (=classIndex)
272  Float_t fATreeWeight; // weight of the event
273  Float_t* fATreeEvent; // event variables
274 
275  Types::EAnalysisType fAnalysisType; //! the training type
276 
277  protected:
278 
279  ClassDef(Factory,0) // The factory creates all MVA methods, and performs their training and testing
280  };
281 
282 } // namespace TMVA
283 
284 #endif
285 
Int_t fATreeType
for each class: tmp tree if user wants to assign the events directly
Definition: Factory.h:271
void AddSignalTrainingEvent(const std::vector< Double_t > &event, Double_t weight=1.0)
add signal training event
Definition: Factory.cxx:284
void SetInputTrees(const TString &signalFileName, const TString &backgroundFileName, Double_t signalWeight=1.0, Double_t backgroundWeight=1.0)
Definition: Factory.cxx:519
virtual void MakeClass(const TString &methodTitle="") const
Print predefined help message of classifier iterate over methods and test.
Definition: Factory.cxx:1115
static TDirectory * RootBaseDir()
Definition: Factory.h:228
void 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:931
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
std::vector< TMVA::VariableTransformBase * > fDefaultTrfs
Definition: Factory.h:253
DataInputHandler * fDataInputHandler
ROOT output file.
Definition: Factory.h:251
std::vector< TTree * > fTestAssignTree
for each class: tmp tree if user wants to assign the events directly
Definition: Factory.h:269
float Float_t
Definition: RtypesCore.h:53
void OptimizeAllMethodsForClassification(TString fomType="ROCIntegral", TString fitType="FitGA")
Definition: Factory.h:192
void SetSignalWeightExpression(const TString &variable)
Definition: Factory.cxx:595
void SetInputVariables(std::vector< TString > *theVariables)
fill input variables in data set
Definition: Factory.cxx:587
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
EAnalysisType
Definition: Types.h:124
void AddRegressionTree(TTree *tree, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
Definition: Factory.h:132
void OptimizeAllMethodsForRegression(TString fomType="ROCIntegral", TString fitType="FitGA")
Definition: Factory.h:193
void AddBackgroundTrainingEvent(const std::vector< Double_t > &event, Double_t weight=1.0)
add signal training event
Definition: Factory.cxx:300
TString fTransformations
option string given by construction (presently only "V")
Definition: Factory.h:257
void AddSpectator(const TString &expression, const TString &title="", const TString &unit="", Double_t min=0, Double_t max=0)
user inserts target in data set info
Definition: Factory.cxx:570
Basic string class.
Definition: TString.h:137
std::vector< TTree * > fTrainAssignTree
flags for data assigning
Definition: Factory.h:268
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
user inserts discriminating variable in data set info
Definition: Factory.cxx:540
void TrainAllMethods()
iterates through all booked methods and calls training
Definition: Factory.cxx:965
DataSetInfo & DefaultDataSetInfo()
default creation
Definition: Factory.cxx:579
static TFile * fgTargetFile
Definition: Factory.h:249
void AddTrainingEvent(const TString &className, const std::vector< Double_t > &event, Double_t weight)
add signal training event
Definition: Factory.cxx:316
void AddTarget(const TString &expression, const TString &title="", const TString &unit="", Double_t min=0, Double_t max=0)
user inserts target in data set info
Definition: Factory.cxx:558
void TrainAllMethodsForClassification(void)
Definition: Factory.h:197
void SetSignalTree(TTree *signal, Double_t weight=1.0)
Definition: Factory.cxx:487
void WriteDataInformation()
put correlations of input data and a few (default + user selected) transformations into the root file...
Definition: Factory.cxx:837
MVector fMethods
verbose mode
Definition: Factory.h:260
void AddTestEvent(const TString &className, const std::vector< Double_t > &event, Double_t weight)
add signal test event
Definition: Factory.cxx:324
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
Definition: Factory.cxx:458
void AddEvent(const TString &className, Types::ETreeType tt, const std::vector< Double_t > &event, Double_t weight)
add event vector event : the order of values is: variables + targets + spectators ...
Definition: Factory.cxx:333
void TrainAllMethodsForRegression(void)
Definition: Factory.h:198
#define ClassDef(name, id)
Definition: Rtypes.h:254
TText * tt
Definition: textangle.C:16
IMethod * GetMethod(const TString &title) const
returns pointer to MVA that corresponds to given method title
Definition: Factory.cxx:821
void EvaluateAllVariables(TString options="")
iterates over all MVA input varables and evaluates them
Definition: Factory.cxx:1170
TTree * CreateEventAssignTrees(const TString &name)
create the data assignment tree (for event-wise data assignment by user)
Definition: Factory.cxx:251
DataInputHandler & DataInput()
Definition: Factory.h:237
A specialized string object used for TTree selections.
Definition: TCut.h:27
Bool_t UserAssignEvents(UInt_t clIndex)
Definition: Factory.cxx:367
virtual ~Factory()
destructor delete fATreeEvent;
Definition: Factory.cxx:188
void AddBackgroundTestEvent(const std::vector< Double_t > &event, Double_t weight=1.0)
add signal training event
Definition: Factory.cxx:308
void SetCut(const TString &cut, const TString &className="")
Definition: Factory.cxx:621
DataSetInfo & AddDataSet(DataSetInfo &)
Definition: Factory.cxx:229
MethodBase * BookMethod(TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
Definition: Factory.cxx:706
SVector< double, 2 > v
Definition: Dict.h:5
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
void EvaluateAllMethods(void)
iterates over all MVAs that have been booked, and calls their evaluation methods
Definition: Factory.cxx:1185
void TestAllMethods()
Definition: Factory.cxx:1085
unsigned int UInt_t
Definition: RtypesCore.h:42
Float_t fATreeWeight
Definition: Factory.h:272
void AddCut(const TString &cut, const TString &className="")
Definition: Factory.cxx:634
void SetBackgroundWeightExpression(const TString &variable)
Definition: Factory.cxx:602
void Greetings()
print welcome message options are: kLogoWelcomeMsg, kIsometricWelcomeMsg, kLeanWelcomeMsg ...
Definition: Factory.cxx:177
void SetVerbose(Bool_t v=kTRUE)
Definition: Factory.cxx:222
DataAssignType
jobname, used as extension in weight file names
Definition: Factory.h:264
void AddRegressionTarget(const TString &expression, const TString &title="", const TString &unit="", Double_t min=0, Double_t max=0)
Definition: Factory.h:154
void SetInputTreesFromEventAssignTrees()
assign event-wise local trees to data set
Definition: Factory.cxx:375
void PrintHelpMessage(const TString &methodTitle="") const
Print predefined help message of classifier iterate over methods and test.
Definition: Factory.cxx:1143
virtual const char * GetName() const
Returns name of object.
Definition: Factory.h:88
tuple tree
Definition: tree.py:24
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:44
void AddTree(TTree *tree, const TString &className, Double_t weight=1.0, const TCut &cut="", Types::ETreeType tt=Types::kMaxTreeType)
Definition: Factory.cxx:407
int type
Definition: TGX11.cxx:120
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
#define name(a, b)
Definition: linkTestLib0.cpp:5
DataAssignType fDataAssignType
Definition: Factory.h:267
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
Definition: Factory.cxx:427
MethodBase * BookMethod(TMVA::Types::EMVA, TString, TString, TMVA::Types::EMVA, TString)
Definition: Factory.h:184
void SetWeightExpression(const TString &variable, const TString &className="")
Log() << kWarning << DefaultDataSetInfo().GetNClasses() /*fClasses.size()*/ << Endl;.
Definition: Factory.cxx:610
TString fOptions
list of transformations on default DataSet
Definition: Factory.h:256
Bool_t Verbose(void) const
Definition: Factory.h:213
Factory(TString theJobName, TFile *theTargetFile, TString theOption="")
TString fJobName
all MVA methods
Definition: Factory.h:261
void DeleteAllMethods(void)
delete methods
Definition: Factory.cxx:210
Float_t * fATreeEvent
Definition: Factory.h:273
DataSetManager * fDataSetManager
Definition: Factory.h:247
void AddSignalTestEvent(const std::vector< Double_t > &event, Double_t weight=1.0)
add signal testing event
Definition: Factory.cxx:292
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Types::EAnalysisType fAnalysisType
Definition: Factory.h:275
std::vector< IMethod * > MVector
Definition: Factory.h:80
const Bool_t kTRUE
Definition: Rtypes.h:91
void SetTree(TTree *tree, const TString &className, Double_t weight)
set background tree
Definition: Factory.cxx:502
TH1F * background
Definition: fithist.C:4
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
Definition: Factory.cxx:679
Bool_t fVerbose
List of transformations to test.
Definition: Factory.h:258
void SetBackgroundTree(TTree *background, Double_t weight=1.0)
Definition: Factory.cxx:494