ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMVARegressionApplication.C
Go to the documentation of this file.
1 /**********************************************************************************
2  * Project : TMVA - a Root-integrated toolkit for multivariate data analysis *
3  * Package : TMVA *
4  * Exectuable: TMVARegressionApplication *
5  * *
6  * This macro provides a simple example on how to use the trained regression MVAs *
7  * within an analysis module *
8  **********************************************************************************/
9 
10 #include <cstdlib>
11 #include <vector>
12 #include <iostream>
13 #include <map>
14 #include <string>
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 
23 #include "TMVA/Tools.h"
24 #include "TMVA/Reader.h"
25 
26 using namespace TMVA;
27 
28 void TMVARegressionApplication( TString myMethodList = "" )
29 {
30  //---------------------------------------------------------------
31  // This loads the library
33 
34  // Default MVA methods to be trained + tested
35  std::map<std::string,int> Use;
36 
37  // --- Mutidimensional likelihood and Nearest-Neighbour methods
38  Use["PDERS"] = 0;
39  Use["PDEFoam"] = 1;
40  Use["KNN"] = 1;
41  //
42  // --- Linear Discriminant Analysis
43  Use["LD"] = 1;
44  //
45  // --- Function Discriminant analysis
46  Use["FDA_GA"] = 1;
47  Use["FDA_MC"] = 0;
48  Use["FDA_MT"] = 0;
49  Use["FDA_GAMT"] = 0;
50  //
51  // --- Neural Network
52  Use["MLP"] = 1;
53  //
54  // --- Support Vector Machine
55  Use["SVM"] = 0;
56  //
57  // --- Boosted Decision Trees
58  Use["BDT"] = 0;
59  Use["BDTG"] = 1;
60  // ---------------------------------------------------------------
61 
62  std::cout << std::endl;
63  std::cout << "==> Start TMVARegressionApplication" << std::endl;
64 
65  // Select methods (don't look at this code - not of interest)
66  if (myMethodList != "") {
67  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
68 
69  std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
70  for (UInt_t i=0; i<mlist.size(); i++) {
71  std::string regMethod(mlist[i]);
72 
73  if (Use.find(regMethod) == Use.end()) {
74  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
75  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
76  std::cout << std::endl;
77  return;
78  }
79  Use[regMethod] = 1;
80  }
81  }
82 
83  // --------------------------------------------------------------------------------------------------
84 
85  // --- Create the Reader object
86 
87  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
88 
89  // Create a set of variables and declare them to the reader
90  // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
91  Float_t var1, var2;
92  reader->AddVariable( "var1", &var1 );
93  reader->AddVariable( "var2", &var2 );
94 
95  // Spectator variables declared in the training have to be added to the reader, too
96  Float_t spec1,spec2;
97  reader->AddSpectator( "spec1:=var1*2", &spec1 );
98  reader->AddSpectator( "spec2:=var1*3", &spec2 );
99 
100  // --- Book the MVA methods
101 
102  TString dir = "weights/";
103  TString prefix = "TMVARegression";
104 
105  // Book method(s)
106  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
107  if (it->second) {
108  TString methodName = it->first + " method";
109  TString weightfile = dir + prefix + "_" + TString(it->first) + ".weights.xml";
110  reader->BookMVA( methodName, weightfile );
111  }
112  }
113 
114  // Book output histograms
115  TH1* hists[100];
116  Int_t nhists = -1;
117  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
118  TH1* h = new TH1F( it->first.c_str(), TString(it->first) + " method", 100, -100, 600 );
119  if (it->second) hists[++nhists] = h;
120  }
121  nhists++;
122 
123  // Prepare input tree (this must be replaced by your data source)
124  // in this example, there is a toy tree with signal and one with background events
125  // we'll later on use only the "signal" events for the test in this example.
126  //
127  TFile *input(0);
128  TString fname = "./tmva_reg_example.root";
129  if (!gSystem->AccessPathName( fname )) {
130  input = TFile::Open( fname ); // check if file in local directory exists
131  }
132  else {
133  input = TFile::Open( "http://root.cern.ch/files/tmva_reg_example.root" ); // if not: download from ROOT server
134  }
135 
136  if (!input) {
137  std::cout << "ERROR: could not open data file" << std::endl;
138  exit(1);
139  }
140  std::cout << "--- TMVARegressionApp : Using input file: " << input->GetName() << std::endl;
141 
142  // --- Event loop
143 
144  // Prepare the tree
145  // - here the variable names have to corresponds to your tree
146  // - you can use the same variables as above which is slightly faster,
147  // but of course you can use different ones and copy the values inside the event loop
148  //
149  TTree* theTree = (TTree*)input->Get("TreeR");
150  std::cout << "--- Select signal sample" << std::endl;
151  theTree->SetBranchAddress( "var1", &var1 );
152  theTree->SetBranchAddress( "var2", &var2 );
153 
154  std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
155  TStopwatch sw;
156  sw.Start();
157  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
158 
159  if (ievt%1000 == 0) {
160  std::cout << "--- ... Processing event: " << ievt << std::endl;
161  }
162 
163  theTree->GetEntry(ievt);
164 
165  // Retrieve the MVA target values (regression outputs) and fill into histograms
166  // NOTE: EvaluateRegression(..) returns a vector for multi-target regression
167 
168  for (Int_t ih=0; ih<nhists; ih++) {
169  TString title = hists[ih]->GetTitle();
170  Float_t val = (reader->EvaluateRegression( title ))[0];
171  hists[ih]->Fill( val );
172  }
173  }
174  sw.Stop();
175  std::cout << "--- End of event loop: "; sw.Print();
176 
177  // --- Write histograms
178 
179  TFile *target = new TFile( "TMVARegApp.root","RECREATE" );
180  for (Int_t ih=0; ih<nhists; ih++) hists[ih]->Write();
181  target->Close();
182 
183  std::cout << "--- Created root file: \"" << target->GetName()
184  << "\" containing the MVA output histograms" << std::endl;
185 
186  delete reader;
187 
188  std::cout << "==> TMVARegressionApplication is done!" << std::endl << std::endl;
189 }
190 
191 int main( int argc, char** argv )
192 {
193  // Select methods (don't look at this code - not of interest)
194  TString methodList;
195  for (int i=1; i<argc; i++) {
196  TString regMethod(argv[i]);
197  if(regMethod=="-b" || regMethod=="--batch") continue;
198  if (!methodList.IsNull()) methodList += TString(",");
199  methodList += regMethod;
200  }
201  TMVARegressionApplication(methodList);
202  return 0;
203 }
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
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
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
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
int main(int argc, char **argv)
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Definition: Reader.cxx:307
TH1 * h
Definition: legend2.C:5
const std::vector< Float_t > & EvaluateRegression(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
Definition: Reader.cxx:581
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
int Int_t
Definition: RtypesCore.h:41
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
void TMVARegressionApplication(TString myMethodList="")
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
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 AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
Definition: Reader.cxx:325
void dir(char *path=0)
Definition: rootalias.C:30
The TH1 histogram class.
Definition: TH1.h:80
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
Stopwatch class.
Definition: TStopwatch.h:30