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