ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMVAMulticlassApplication.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: TMVAMulticlassApplication *
5  * *
6  * This macro provides a simple example on how to use the trained multiclass *
7  * classifiers within an analysis module *
8  **********************************************************************************/
9 
10 #include <cstdlib>
11 #include <iostream>
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 #include "TFile.h"
17 #include "TTree.h"
18 #include "TString.h"
19 #include "TSystem.h"
20 #include "TROOT.h"
21 #include "TStopwatch.h"
22 #include "TH1F.h"
23 
24 #include "TMVA/Tools.h"
25 #include "TMVA/Reader.h"
26 
27 using namespace TMVA;
28 
29 void TMVAMulticlassApplication( TString myMethodList = "" )
30 {
31 
33 
34  //---------------------------------------------------------------
35  // default MVA methods to be trained + tested
36  std::map<std::string,int> Use;
37  Use["MLP"] = 1;
38  Use["BDTG"] = 1;
39  Use["FDA_GA"] = 0;
40  Use["PDEFoam"] = 0;
41  //---------------------------------------------------------------
42 
43  std::cout << std::endl;
44  std::cout << "==> Start TMVAMulticlassApplication" << std::endl;
45  if (myMethodList != "") {
46  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
47 
48  std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
49  for (UInt_t i=0; i<mlist.size(); i++) {
50  std::string regMethod(mlist[i]);
51 
52  if (Use.find(regMethod) == Use.end()) {
53  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
54  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " " << std::endl;
55  std::cout << std::endl;
56  return;
57  }
58  Use[regMethod] = 1;
59  }
60  }
61 
62 
63  // create the Reader object
64  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
65 
66  // create a set of variables and declare them to the reader
67  // - the variable names must corresponds in name and type to
68  // those given in the weight file(s) that you use
69  Float_t var1, var2, var3, var4;
70  reader->AddVariable( "var1", &var1 );
71  reader->AddVariable( "var2", &var2 );
72  reader->AddVariable( "var3", &var3 );
73  reader->AddVariable( "var4", &var4 );
74 
75  // book the MVA methods
76  TString dir = "weights/";
77  TString prefix = "TMVAMulticlass";
78 
79  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
80  if (it->second) {
81  TString methodName = TString(it->first) + TString(" method");
82  TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
83  reader->BookMVA( methodName, weightfile );
84  }
85  }
86 
87  // book output histograms
88  UInt_t nbin = 100;
89  TH1F *histMLP_signal(0), *histBDTG_signal(0), *histFDAGA_signal(0), *histPDEFoam_signal(0);
90  if (Use["MLP"])
91  histMLP_signal = new TH1F( "MVA_MLP_signal", "MVA_MLP_signal", nbin, 0., 1.1 );
92  if (Use["BDTG"])
93  histBDTG_signal = new TH1F( "MVA_BDTG_signal", "MVA_BDTG_signal", nbin, 0., 1.1 );
94  if (Use["FDA_GA"])
95  histFDAGA_signal = new TH1F( "MVA_FDA_GA_signal", "MVA_FDA_GA_signal", nbin, 0., 1.1 );
96  if (Use["PDEFoam"])
97  histPDEFoam_signal = new TH1F( "MVA_PDEFoam_signal", "MVA_PDEFoam_signal", nbin, 0., 1.1 );
98 
99 
100  TFile *input(0);
101  TString fname = "./tmva_example_multiple_background.root";
102  if (!gSystem->AccessPathName( fname )) {
103  input = TFile::Open( fname ); // check if file in local directory exists
104  }
105  if (!input) {
106  std::cout << "ERROR: could not open data file, please generate example data first!" << std::endl;
107  exit(1);
108  }
109  std::cout << "--- TMVAMulticlassApp : Using input file: " << input->GetName() << std::endl;
110 
111  // prepare the tree
112  // - here the variable names have to corresponds to your tree
113  // - you can use the same variables as above which is slightly faster,
114  // but of course you can use different ones and copy the values inside the event loop
115 
116  TTree* theTree = (TTree*)input->Get("TreeS");
117  std::cout << "--- Select signal sample" << std::endl;
118  theTree->SetBranchAddress( "var1", &var1 );
119  theTree->SetBranchAddress( "var2", &var2 );
120  theTree->SetBranchAddress( "var3", &var3 );
121  theTree->SetBranchAddress( "var4", &var4 );
122 
123  std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
124  TStopwatch sw;
125  sw.Start();
126 
127  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
128  if (ievt%1000 == 0){
129  std::cout << "--- ... Processing event: " << ievt << std::endl;
130  }
131  theTree->GetEntry(ievt);
132 
133  if (Use["MLP"])
134  histMLP_signal->Fill((reader->EvaluateMulticlass( "MLP method" ))[0]);
135  if (Use["BDTG"])
136  histBDTG_signal->Fill((reader->EvaluateMulticlass( "BDTG method" ))[0]);
137  if (Use["FDA_GA"])
138  histFDAGA_signal->Fill((reader->EvaluateMulticlass( "FDA_GA method" ))[0]);
139  if (Use["PDEFoam"])
140  histPDEFoam_signal->Fill((reader->EvaluateMulticlass( "PDEFoam method" ))[0]);
141 
142  }
143 
144  // get elapsed time
145  sw.Stop();
146  std::cout << "--- End of event loop: "; sw.Print();
147 
148  TFile *target = new TFile( "TMVAMulticlassApp.root","RECREATE" );
149  if (Use["MLP"])
150  histMLP_signal->Write();
151  if (Use["BDTG"])
152  histBDTG_signal->Write();
153  if (Use["FDA_GA"])
154  histFDAGA_signal->Write();
155  if (Use["PDEFoam"])
156  histPDEFoam_signal->Write();
157 
158  target->Close();
159  std::cout << "--- Created root file: \"TMVMulticlassApp.root\" containing the MVA output histograms" << std::endl;
160 
161  delete reader;
162 
163  std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl;
164 }
165 
166 int main( int argc, char** argv )
167 {
168  // Select methods (don't look at this code - not of interest)
169  TString methodList;
170  for (int i=1; i<argc; i++) {
171  TString regMethod(argv[i]);
172  if(regMethod=="-b" || regMethod=="--batch") continue;
173  if (!methodList.IsNull()) methodList += TString(",");
174  methodList += regMethod;
175  }
176  TMVAMulticlassApplication(methodList);
177  return 0;
178 }
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
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:217
long long Long64_t
Definition: RtypesCore.h:69
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
float Float_t
Definition: RtypesCore.h:53
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Definition: Reader.cxx:307
void TMVAMulticlassApplication(TString myMethodList="")
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5144
Basic string class.
Definition: TString.h:137
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 Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
Definition: Reader.cxx:376
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
int main(int argc, char **argv)
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t IsNull() const
Definition: TString.h:387
void dir(char *path=0)
Definition: rootalias.C:30
virtual Long64_t GetEntries() const
Definition: TTree.h:386
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
const std::vector< Float_t > & EvaluateMulticlass(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
Definition: Reader.cxx:646
Stopwatch class.
Definition: TStopwatch.h:30