Logo ROOT   6.07/09
Reference Guide
TMVAMulticlass.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tmva
3 /// \notebook -nodraw
4 /// This macro provides a simple example for the training and testing of the TMVA
5 /// multiclass classification
6 /// - Project : TMVA - a Root-integrated toolkit for multivariate data analysis
7 /// - Package : TMVA
8 /// - Root Macro: TMVAMulticlass
9 ///
10 /// \macro_output
11 /// \macro_code
12 /// \author Andreas Hoecker
13 
14 #include <cstdlib>
15 #include <iostream>
16 #include <map>
17 #include <string>
18 
19 #include "TFile.h"
20 #include "TTree.h"
21 #include "TString.h"
22 #include "TSystem.h"
23 #include "TROOT.h"
24 
25 
26 #include "TMVA/Tools.h"
27 #include "TMVA/Factory.h"
28 #include "TMVA/DataLoader.h"
29 #include "TMVA/TMVAMultiClassGui.h"
30 
31 
32 using namespace TMVA;
33 
34 void TMVAMulticlass( TString myMethodList = "" )
35 {
36 
37  // This loads the library
39 
40  // to get access to the GUI and all tmva macros
41  //
42  // TString tmva_dir(TString(gRootDir) + "/tmva");
43  // if(gSystem->Getenv("TMVASYS"))
44  // tmva_dir = TString(gSystem->Getenv("TMVASYS"));
45  // gROOT->SetMacroPath(tmva_dir + "/test/:" + gROOT->GetMacroPath() );
46  // gROOT->ProcessLine(".L TMVAMultiClassGui.C");
47 
48 
49  //---------------------------------------------------------------
50  // Default MVA methods to be trained + tested
51  std::map<std::string,int> Use;
52  Use["MLP"] = 1;
53  Use["BDTG"] = 1;
54  Use["FDA_GA"] = 0;
55  Use["PDEFoam"] = 0;
56  //---------------------------------------------------------------
57 
58  std::cout << std::endl;
59  std::cout << "==> Start TMVAMulticlass" << std::endl;
60 
61  if (myMethodList != "") {
62  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
63 
64  std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
65  for (UInt_t i=0; i<mlist.size(); i++) {
66  std::string regMethod(mlist[i]);
67 
68  if (Use.find(regMethod) == Use.end()) {
69  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
70  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
71  std::cout << std::endl;
72  return;
73  }
74  Use[regMethod] = 1;
75  }
76  }
77 
78  // Create a new root output file.
79  TString outfileName = "TMVAMulticlass.root";
80  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
81 
82  TMVA::Factory *factory = new TMVA::Factory( "TMVAMulticlass", outputFile,
83  "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=multiclass" );
84  TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");
85 
86  dataloader->AddVariable( "var1", 'F' );
87  dataloader->AddVariable( "var2", "Variable 2", "", 'F' );
88  dataloader->AddVariable( "var3", "Variable 3", "units", 'F' );
89  dataloader->AddVariable( "var4", "Variable 4", "units", 'F' );
90 
91  TFile *input(0);
92  TString fname = "./tmva_example_multiple_background.root";
93  if (!gSystem->AccessPathName( fname )) {
94  // first we try to find the file in the local directory
95  std::cout << "--- TMVAMulticlass : Accessing " << fname << std::endl;
96  input = TFile::Open( fname );
97  }
98  else {
99  std::cout << "Creating testdata...." << std::endl;
100  TString createDataMacro = TString(gROOT->GetTutorialsDir()) + "/tmva/createData.C";
101  gROOT->ProcessLine(TString::Format(".L %s",createDataMacro.Data()));
102  gROOT->ProcessLine("create_MultipleBackground(2000)");
103  std::cout << " created tmva_example_multiple_background.root for tests of the multiclass features"<<std::endl;
104  input = TFile::Open( fname );
105  }
106  if (!input) {
107  std::cout << "ERROR: could not open data file" << std::endl;
108  exit(1);
109  }
110 
111  TTree *signalTree = (TTree*)input->Get("TreeS");
112  TTree *background0 = (TTree*)input->Get("TreeB0");
113  TTree *background1 = (TTree*)input->Get("TreeB1");
114  TTree *background2 = (TTree*)input->Get("TreeB2");
115 
116  gROOT->cd( outfileName+TString(":/") );
117  dataloader->AddTree (signalTree,"Signal");
118  dataloader->AddTree (background0,"bg0");
119  dataloader->AddTree (background1,"bg1");
120  dataloader->AddTree (background2,"bg2");
121 
122  dataloader->PrepareTrainingAndTestTree( "", "SplitMode=Random:NormMode=NumEvents:!V" );
123 
124  if (Use["BDTG"]) // gradient boosted decision trees
125  factory->BookMethod( dataloader, TMVA::Types::kBDT, "BDTG", "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.10:UseBaggedBoost:BaggedSampleFraction=0.50:nCuts=20:MaxDepth=2");
126  if (Use["MLP"]) // neural network
127  factory->BookMethod( dataloader, TMVA::Types::kMLP, "MLP", "!H:!V:NeuronType=tanh:NCycles=1000:HiddenLayers=N+5,5:TestRate=5:EstimatorType=MSE");
128  if (Use["FDA_GA"]) // functional discriminant with GA minimizer
129  factory->BookMethod( dataloader, 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" );
130  if (Use["PDEFoam"]) // PDE-Foam approach
131  factory->BookMethod( dataloader, TMVA::Types::kPDEFoam, "PDEFoam", "!H:!V:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" );
132 
133  // Train MVAs using the set of training events
134  factory->TrainAllMethods();
135 
136  // Evaluate all MVAs using the set of test events
137  factory->TestAllMethods();
138 
139  // Evaluate and compare performance of all configured MVAs
140  factory->EvaluateAllMethods();
141 
142  // --------------------------------------------------------------
143 
144  // Save the output
145  outputFile->Close();
146 
147  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
148  std::cout << "==> TMVAClassification is done!" << std::endl;
149 
150  delete factory;
151  delete dataloader;
152 
153  // Launch the GUI for the root macros
154  if (!gROOT->IsBatch()) TMVAMultiClassGui( outfileName );
155 
156 
157 }
158 
159 int main( int argc, char** argv )
160 {
161  // Select methods (don't look at this code - not of interest)
162  TString methodList;
163  for (int i=1; i<argc; i++) {
164  TString regMethod(argv[i]);
165  if(regMethod=="-b" || regMethod=="--batch") continue;
166  if (!methodList.IsNull()) methodList += TString(",");
167  methodList += regMethod;
168  }
169  TMVAMulticlass(methodList);
170  return 0;
171 }
172 
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:1265
static Tools & Instance()
Definition: Tools.cxx:80
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Definition: Factory.cxx:337
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
void TrainAllMethods()
iterates through all booked methods and calls training
Definition: Factory.cxx:822
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
Definition: DataLoader.cxx:455
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:3871
const char * Data() const
Definition: TString.h:349
void TMVAMultiClassGui(const char *fName="TMVAMulticlass.root", TString dataset="")
Tools & gTools()
Definition: Tools.cxx:79
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2335
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
void EvaluateAllMethods(void)
iterates over all MVAs that have been booked, and calls their evaluation methods
Definition: Factory.cxx:1058
void TestAllMethods()
Definition: Factory.cxx:957
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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: DataLoader.cxx:334
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
Definition: DataLoader.cxx:579
Abstract ClassifierFactory template that handles arbitrary types.
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 &#39;separator&#39; and fills the list &#39;splitV&#39; with the primitive strings ...
Definition: Tools.cxx:1207
int main(int argc, char **argv)
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:904