Logo ROOT   6.10/09
Reference Guide
TMVAClassificationCategoryApplication.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 classifiers
5 /// (with categories) within an analysis module
6 /// - Project : TMVA - a Root-integrated toolkit for multivariate data analysis
7 /// - Package : TMVA
8 /// - Exectuable: TMVAClassificationCategoryApplication
9 ///
10 /// \macro_output
11 /// \macro_code
12 /// \author Andreas Hoecker
13 
14 
15 #include <cstdlib>
16 #include <vector>
17 #include <iostream>
18 #include <map>
19 #include <string>
20 
21 #include "TFile.h"
22 #include "TTree.h"
23 #include "TString.h"
24 #include "TSystem.h"
25 #include "TROOT.h"
26 #include "TH1F.h"
27 #include "TStopwatch.h"
28 
29 #if not defined(__CINT__) || defined(__MAKECINT__)
30 #include "TMVA/Tools.h"
31 #include "TMVA/Reader.h"
32 #include "TMVA/MethodCuts.h"
33 #endif
34 
35 // two types of category methods are implemented
36 Bool_t UseOffsetMethod = kTRUE;
37 
38 void TMVAClassificationCategoryApplication()
39 {
40  // ---------------------------------------------------------------
41  // default MVA methods to be trained + tested
42  std::map<std::string,int> Use;
43  //
44  Use["LikelihoodCat"] = 1;
45  Use["FisherCat"] = 1;
46  // ---------------------------------------------------------------
47 
48  std::cout << std::endl
49  << "==> Start TMVAClassificationCategoryApplication" << std::endl;
50 
51  // Create the Reader object
52 
53  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
54 
55  // Create a set of variables and spectators and declare them to the reader
56  // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
57  Float_t var1, var2, var3, var4, eta;
58  reader->AddVariable( "var1", &var1 );
59  reader->AddVariable( "var2", &var2 );
60  reader->AddVariable( "var3", &var3 );
61  reader->AddVariable( "var4", &var4 );
62 
63  reader->AddSpectator( "eta", &eta );
64 
65  // Book the MVA methods
66 
67  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
68  if (it->second) {
69  TString methodName = it->first + " method";
70  TString weightfile = "dataset/weights/TMVAClassificationCategory_" + TString(it->first) + ".weights.xml";
71  reader->BookMVA( methodName, weightfile );
72  }
73  }
74 
75  // Book output histograms
76  UInt_t nbin = 100;
77  std::map<std::string,TH1*> hist;
78  hist["LikelihoodCat"] = new TH1F( "MVA_LikelihoodCat", "MVA_LikelihoodCat", nbin, -1, 0.9999 );
79  hist["FisherCat"] = new TH1F( "MVA_FisherCat", "MVA_FisherCat", nbin, -4, 4 );
80 
81  // Prepare input tree (this must be replaced by your data source)
82  // in this example, there is a toy tree with signal and one with background events
83  // we'll later on use only the "signal" events for the test in this example.
84  //
85  TString fname = TString(gSystem->DirName(__FILE__) ) + "/data/";
86  // if directory data not found try using tutorials dir
87  if (gSystem->AccessPathName( fname + "toy_sigbkg_categ_offset.root" )) {
88  fname = gROOT->GetTutorialDir() + "/tmva/data/";
89  }
90  if (UseOffsetMethod) fname += "toy_sigbkg_categ_offset.root";
91  else fname += "toy_sigbkg_categ_varoff.root";
92  std::cout << "--- TMVAClassificationApp : Accessing " << fname << "!" << std::endl;
93  TFile *input = TFile::Open(fname);
94  if (!input) {
95  std::cout << "ERROR: could not open data file: " << fname << std::endl;
96  exit(1);
97  }
98 
99  // Event loop
100 
101  // Prepare the tree
102  // - here the variable names have to corresponds to your tree
103  // - you can use the same variables as above which is slightly faster,
104  // but of course you can use different ones and copy the values inside the event loop
105  //
106  TTree* theTree = (TTree*)input->Get("TreeS");
107  std::cout << "--- Use signal sample for evalution" << std::endl;
108  theTree->SetBranchAddress( "var1", &var1 );
109  theTree->SetBranchAddress( "var2", &var2 );
110  theTree->SetBranchAddress( "var3", &var3 );
111  theTree->SetBranchAddress( "var4", &var4 );
112 
113  theTree->SetBranchAddress( "eta", &eta ); // spectator
114 
115  std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
116  TStopwatch sw;
117  sw.Start();
118  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
119 
120  if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
121 
122  theTree->GetEntry(ievt);
123 
124  // Return the MVA outputs and fill into histograms
125 
126  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
127  if (!it->second) continue;
128  TString methodName = it->first + " method";
129  hist[it->first]->Fill( reader->EvaluateMVA( methodName ) );
130  }
131 
132  }
133  sw.Stop();
134  std::cout << "--- End of event loop: "; sw.Print();
135 
136  // Write histograms
137 
138  TFile *target = new TFile( "TMVApp.root","RECREATE" );
139  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++)
140  if (it->second) hist[it->first]->Write();
141 
142  target->Close();
143  std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
144 
145  delete reader;
146  std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl;
147 }
148 
149 int main( int argc, char** argv )
150 {
151  TMVAClassificationCategoryApplication();
152  return 0;
153 }
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
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
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:375
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:5321
Basic string class.
Definition: TString.h:129
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1003
bool Bool_t
Definition: RtypesCore.h:59
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
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7873
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
void AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
Definition: Reader.cxx:326
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
Definition: Reader.cxx:485
virtual Long64_t GetEntries() const
Definition: TTree.h:381
The Reader class serves to use the MVAs in a specific analysis context.
Definition: Reader.h:63
A TTree object has a header with a name and a title.
Definition: TTree.h:78
const Bool_t kTRUE
Definition: RtypesCore.h:91
int main(int argc, char **argv)
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:904
Stopwatch class.
Definition: TStopwatch.h:28