ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMVAMulticlass.C
Go to the documentation of this file.
1 /**********************************************************************************
2  * Project : TMVA - a Root-integrated toolkit for multivariate data analysis *
3  * Package : TMVA *
4  * Root Macro: TMVAMulticlass *
5  * *
6  * This macro provides a simple example for the training and testing of the TMVA *
7  * multiclass classification *
8  **********************************************************************************/
9 
10 #include <cstdlib>
11 #include <iostream>
12 #include <map>
13 #include <string>
14 
15 #include "TFile.h"
16 #include "TTree.h"
17 #include "TString.h"
18 #include "TSystem.h"
19 #include "TROOT.h"
20 
21 
22 #include "TMVA/Tools.h"
23 #include "TMVA/Factory.h"
24 #include "TMVA/TMVAMultiClassGui.h"
25 
26 
27 using namespace TMVA;
28 
29 void TMVAMulticlass( TString myMethodList = "" )
30 {
31 
32  // This loads the library
34 
35  // to get access to the GUI and all tmva macros
36  // TString tmva_dir(TString(gRootDir) + "/tmva");
37  // if(gSystem->Getenv("TMVASYS"))
38  // tmva_dir = TString(gSystem->Getenv("TMVASYS"));
39  // gROOT->SetMacroPath(tmva_dir + "/test/:" + gROOT->GetMacroPath() );
40  // gROOT->ProcessLine(".L TMVAMultiClassGui.C");
41 
42 
43  //---------------------------------------------------------------
44  // default MVA methods to be trained + tested
45  std::map<std::string,int> Use;
46  Use["MLP"] = 1;
47  Use["BDTG"] = 1;
48  Use["FDA_GA"] = 0;
49  Use["PDEFoam"] = 0;
50  //---------------------------------------------------------------
51 
52  std::cout << std::endl;
53  std::cout << "==> Start TMVAMulticlass" << std::endl;
54 
55  if (myMethodList != "") {
56  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
57 
58  std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
59  for (UInt_t i=0; i<mlist.size(); i++) {
60  std::string regMethod(mlist[i]);
61 
62  if (Use.find(regMethod) == Use.end()) {
63  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
64  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
65  std::cout << std::endl;
66  return;
67  }
68  Use[regMethod] = 1;
69  }
70  }
71 
72  // Create a new root output file.
73  TString outfileName = "TMVAMulticlass.root";
74  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
75 
76  TMVA::Factory *factory = new TMVA::Factory( "TMVAMulticlass", outputFile,
77  "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=multiclass" );
78  factory->AddVariable( "var1", 'F' );
79  factory->AddVariable( "var2", "Variable 2", "", 'F' );
80  factory->AddVariable( "var3", "Variable 3", "units", 'F' );
81  factory->AddVariable( "var4", "Variable 4", "units", 'F' );
82 
83  TFile *input(0);
84  TString fname = "./tmva_example_multiple_background.root";
85  if (!gSystem->AccessPathName( fname )) {
86  // first we try to find the file in the local directory
87  std::cout << "--- TMVAMulticlass : Accessing " << fname << std::endl;
88  input = TFile::Open( fname );
89  }
90  else {
91  std::cout << "Creating testdata...." << std::endl;
92  gROOT->ProcessLine(".L createData.C");
93  gROOT->ProcessLine("create_MultipleBackground(2000)");
94  std::cout << " created tmva_example_multiple_background.root for tests of the multiclass features"<<std::endl;
95  input = TFile::Open( fname );
96  }
97  if (!input) {
98  std::cout << "ERROR: could not open data file" << std::endl;
99  exit(1);
100  }
101 
102  TTree *signal = (TTree*)input->Get("TreeS");
103  TTree *background0 = (TTree*)input->Get("TreeB0");
104  TTree *background1 = (TTree*)input->Get("TreeB1");
105  TTree *background2 = (TTree*)input->Get("TreeB2");
106 
107  gROOT->cd( outfileName+TString(":/") );
108  factory->AddTree (signal,"Signal");
109  factory->AddTree (background0,"bg0");
110  factory->AddTree (background1,"bg1");
111  factory->AddTree (background2,"bg2");
112 
113  factory->PrepareTrainingAndTestTree( "", "SplitMode=Random:NormMode=NumEvents:!V" );
114 
115  if (Use["BDTG"]) // gradient boosted decision trees
116  factory->BookMethod( TMVA::Types::kBDT, "BDTG", "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.10:UseBaggedBoost:BaggedSampleFraction=0.50:nCuts=20:MaxDepth=2");
117  if (Use["MLP"]) // neural network
118  factory->BookMethod( TMVA::Types::kMLP, "MLP", "!H:!V:NeuronType=tanh:NCycles=1000:HiddenLayers=N+5,5:TestRate=5:EstimatorType=MSE");
119  if (Use["FDA_GA"]) // functional discriminant with GA minimizer
120  factory->BookMethod( TMVA::Types::kFDA, "FDA_GA", "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=300:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" );
121  if (Use["PDEFoam"]) // PDE-Foam approach
122  factory->BookMethod( TMVA::Types::kPDEFoam, "PDEFoam", "!H:!V:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" );
123 
124  // Train MVAs using the set of training events
125  factory->TrainAllMethods();
126 
127  // ---- Evaluate all MVAs using the set of test events
128  factory->TestAllMethods();
129 
130  // ----- Evaluate and compare performance of all configured MVAs
131  factory->EvaluateAllMethods();
132 
133  // --------------------------------------------------------------
134 
135  // Save the output
136  outputFile->Close();
137 
138  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
139  std::cout << "==> TMVAClassification is done!" << std::endl;
140 
141  delete factory;
142 
143  // Launch the GUI for the root macros
144  if (!gROOT->IsBatch()) TMVAMultiClassGui( outfileName );
145 
146 
147 }
148 
149 int main( int argc, char** argv )
150 {
151  // Select methods (don't look at this code - not of interest)
152  TString methodList;
153  for (int i=1; i<argc; i++) {
154  TString regMethod(argv[i]);
155  if(regMethod=="-b" || regMethod=="--batch") continue;
156  if (!methodList.IsNull()) methodList += TString(",");
157  methodList += regMethod;
158  }
159  TMVAMulticlass(methodList);
160  return 0;
161 }
162 
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1213
static Tools & Instance()
Definition: Tools.cxx:80
#define gROOT
Definition: TROOT.h:344
Basic string class.
Definition: TString.h:137
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
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851
Tools & gTools()
Definition: Tools.cxx:79
void TMVAMultiClassGui(const char *fName="TMVAMulticlass.root")
void TMVAMulticlass(TString myMethodList="")
int main(int argc, char **argv)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
MethodBase * BookMethod(TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
Definition: Factory.cxx:706
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
Bool_t IsNull() const
Definition: TString.h:387
void AddTree(TTree *tree, const TString &className, Double_t weight=1.0, const TCut &cut="", Types::ETreeType tt=Types::kMaxTreeType)
Definition: Factory.cxx:407
A TTree object has a header with a name and a title.
Definition: TTree.h:98
std::vector< TString > SplitString(const TString &theOpt, const char separator) const
splits the option string at 'separator' and fills the list 'splitV' with the primitive strings ...
Definition: Tools.cxx:1207
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
Definition: Factory.cxx:679