Logo ROOT   6.10/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["DNN_CPU"] = 0;
45  Use["FDA_GA"] = 0;
46  Use["PDEFoam"] = 0;
47  //---------------------------------------------------------------
48 
49  std::cout << std::endl;
50  std::cout << "==> Start TMVAMulticlassApp" << std::endl;
51  if (myMethodList != "") {
52  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
53 
54  std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
55  for (UInt_t i=0; i<mlist.size(); i++) {
56  std::string regMethod(mlist[i]);
57 
58  if (Use.find(regMethod) == Use.end()) {
59  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
60  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " " << std::endl;
61  std::cout << std::endl;
62  return;
63  }
64  Use[regMethod] = 1;
65  }
66  }
67 
68 
69  // create the Reader object
70  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
71 
72  // create a set of variables and declare them to the reader
73  // - the variable names must corresponds in name and type to
74  // those given in the weight file(s) that you use
75  Float_t var1, var2, var3, var4;
76  reader->AddVariable( "var1", &var1 );
77  reader->AddVariable( "var2", &var2 );
78  reader->AddVariable( "var3", &var3 );
79  reader->AddVariable( "var4", &var4 );
80 
81  // book the MVA methods
82  TString dir = "dataset/weights/";
83  TString prefix = "TMVAMulticlass";
84 
85  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
86  if (it->second) {
87  TString methodName = TString(it->first) + TString(" method");
88  TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
89  reader->BookMVA( methodName, weightfile );
90  }
91  }
92 
93  // book output histograms
94  UInt_t nbin = 100;
95  TH1F *histMLP_signal(0), *histBDTG_signal(0), *histFDAGA_signal(0), *histPDEFoam_signal(0), *histDNNCPU_signal(0);
96  if (Use["MLP"])
97  histMLP_signal = new TH1F( "MVA_MLP_signal", "MVA_MLP_signal", nbin, 0., 1.1 );
98  if (Use["BDTG"])
99  histBDTG_signal = new TH1F( "MVA_BDTG_signal", "MVA_BDTG_signal", nbin, 0., 1.1 );
100  if (Use["DNN_CPU"]) histDNNCPU_signal = new TH1F("MVA_DNNCPU_signal", "MVA_DNNCPU_signal", nbin, 0., 1.1);
101  if (Use["FDA_GA"])
102  histFDAGA_signal = new TH1F( "MVA_FDA_GA_signal", "MVA_FDA_GA_signal", nbin, 0., 1.1 );
103  if (Use["PDEFoam"])
104  histPDEFoam_signal = new TH1F( "MVA_PDEFoam_signal", "MVA_PDEFoam_signal", nbin, 0., 1.1 );
105 
106 
107  TFile *input(0);
108  TString fname = "./tmva_example_multiple_background.root";
109  if (!gSystem->AccessPathName( fname )) {
110  input = TFile::Open( fname ); // check if file in local directory exists
111  }
112  if (!input) {
113  std::cout << "ERROR: could not open data file, please generate example data first!" << std::endl;
114  exit(1);
115  }
116  std::cout << "--- TMVAMulticlassApp : Using input file: " << input->GetName() << std::endl;
117 
118  // prepare the tree
119  // - here the variable names have to corresponds to your tree
120  // - you can use the same variables as above which is slightly faster,
121  // but of course you can use different ones and copy the values inside the event loop
122 
123  TTree* theTree = (TTree*)input->Get("TreeS");
124  std::cout << "--- Select signal sample" << std::endl;
125  theTree->SetBranchAddress( "var1", &var1 );
126  theTree->SetBranchAddress( "var2", &var2 );
127  theTree->SetBranchAddress( "var3", &var3 );
128  theTree->SetBranchAddress( "var4", &var4 );
129 
130  std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
131  TStopwatch sw;
132  sw.Start();
133 
134  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
135  if (ievt%1000 == 0){
136  std::cout << "--- ... Processing event: " << ievt << std::endl;
137  }
138  theTree->GetEntry(ievt);
139 
140  if (Use["MLP"])
141  histMLP_signal->Fill((reader->EvaluateMulticlass( "MLP method" ))[0]);
142  if (Use["BDTG"])
143  histBDTG_signal->Fill((reader->EvaluateMulticlass( "BDTG method" ))[0]);
144  if (Use["DNN_CPU"]) histDNNCPU_signal->Fill((reader->EvaluateMulticlass("DNN_CPU method"))[0]);
145  if (Use["FDA_GA"])
146  histFDAGA_signal->Fill((reader->EvaluateMulticlass( "FDA_GA method" ))[0]);
147  if (Use["PDEFoam"])
148  histPDEFoam_signal->Fill((reader->EvaluateMulticlass( "PDEFoam method" ))[0]);
149 
150  }
151 
152  // get elapsed time
153  sw.Stop();
154  std::cout << "--- End of event loop: "; sw.Print();
155 
156  TFile *target = new TFile( "TMVAMulticlassApp.root","RECREATE" );
157  if (Use["MLP"])
158  histMLP_signal->Write();
159  if (Use["BDTG"])
160  histBDTG_signal->Write();
161  if (Use["DNN_CPU"]) histDNNCPU_signal->Write();
162  if (Use["FDA_GA"])
163  histFDAGA_signal->Write();
164  if (Use["PDEFoam"])
165  histPDEFoam_signal->Write();
166 
167  target->Close();
168  std::cout << "--- Created root file: \"TMVMulticlassApp.root\" containing the MVA output histograms" << std::endl;
169 
170  delete reader;
171 
172  std::cout << "==> TMVAMulticlassApp is done!" << std::endl << std::endl;
173 }
174 
175 int main( int argc, char** argv )
176 {
177  // Select methods (don't look at this code - not of interest)
178  TString methodList;
179  for (int i=1; i<argc; i++) {
180  TString regMethod(argv[i]);
181  if(regMethod=="-b" || regMethod=="--batch") continue;
182  if (!methodList.IsNull()) methodList += TString(",");
183  methodList += regMethod;
184  }
185  TMVAMulticlassApplication(methodList);
186  return 0;
187 }
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:1272
static Tools & Instance()
Definition: Tools.cxx:75
long long Long64_t
Definition: RtypesCore.h:69
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
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:308
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
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:3909
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
Definition: Reader.cxx:377
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
unsigned int UInt_t
Definition: RtypesCore.h:42
Tools & gTools()
The Reader class serves to use the MVAs in a specific analysis context.
Definition: Reader.h:63
Abstract ClassifierFactory template that handles arbitrary types.
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:1210
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:647
Stopwatch class.
Definition: TStopwatch.h:28