ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMVAClassificationCategoryApplication.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: TMVAClassificationCategoryApplication *
5  * *
6  * This macro provides a simple example on how to use the trained classifiers *
7  * (with categories) 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 "TH1F.h"
22 #include "TStopwatch.h"
23 
24 #if not defined(__CINT__) || defined(__MAKECINT__)
25 #include "TMVA/Tools.h"
26 #include "TMVA/Reader.h"
27 #include "TMVA/MethodCuts.h"
28 #endif
29 
30 // two types of category methods are implemented
32 
34 {
35  // ---------------------------------------------------------------
36  // default MVA methods to be trained + tested
37  std::map<std::string,int> Use;
38  // ---
39  Use["LikelihoodCat"] = 1;
40  Use["FisherCat"] = 1;
41  // ---------------------------------------------------------------
42 
43  std::cout << std::endl
44  << "==> Start TMVAClassificationCategoryApplication" << std::endl;
45 
46  // --- Create the Reader object
47 
48  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
49 
50  // Create a set of variables and spectators and declare them to the reader
51  // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
52  Float_t var1, var2, var3, var4, eta;
53  reader->AddVariable( "var1", &var1 );
54  reader->AddVariable( "var2", &var2 );
55  reader->AddVariable( "var3", &var3 );
56  reader->AddVariable( "var4", &var4 );
57 
58  reader->AddSpectator( "eta", &eta );
59 
60  // --- Book the MVA methods
61 
62  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
63  if (it->second) {
64  TString methodName = it->first + " method";
65  TString weightfile = "weights/TMVAClassificationCategory_" + TString(it->first) + ".weights.xml";
66  reader->BookMVA( methodName, weightfile );
67  }
68  }
69 
70  // Book output histograms
71  UInt_t nbin = 100;
72  std::map<std::string,TH1*> hist;
73  hist["LikelihoodCat"] = new TH1F( "MVA_LikelihoodCat", "MVA_LikelihoodCat", nbin, -1, 0.9999 );
74  hist["FisherCat"] = new TH1F( "MVA_FisherCat", "MVA_FisherCat", nbin, -4, 4 );
75 
76  // Prepare input tree (this must be replaced by your data source)
77  // in this example, there is a toy tree with signal and one with background events
78  // we'll later on use only the "signal" events for the test in this example.
79  //
80  TString fname = TString(gSystem->DirName(__FILE__) ) + "/data/";
81  // if directory data not found try using tutorials dir
82  if (gSystem->AccessPathName( fname )) {
83  fname = TString(gROOT->GetTutorialsDir()) + "/tmva/data/";
84  }
85  if (UseOffsetMethod) fname += "toy_sigbkg_categ_offset.root";
86  else fname += "toy_sigbkg_categ_varoff.root";
87  std::cout << "--- TMVAClassificationApp : Accessing " << fname << "!" << std::endl;
88  TFile *input = TFile::Open(fname);
89  if (!input) {
90  std::cout << "ERROR: could not open data file: " << fname << std::endl;
91  exit(1);
92  }
93 
94  // --- Event loop
95 
96  // Prepare the tree
97  // - here the variable names have to corresponds to your tree
98  // - you can use the same variables as above which is slightly faster,
99  // but of course you can use different ones and copy the values inside the event loop
100  //
101  TTree* theTree = (TTree*)input->Get("TreeS");
102  std::cout << "--- Use signal sample for evalution" << std::endl;
103  theTree->SetBranchAddress( "var1", &var1 );
104  theTree->SetBranchAddress( "var2", &var2 );
105  theTree->SetBranchAddress( "var3", &var3 );
106  theTree->SetBranchAddress( "var4", &var4 );
107 
108  theTree->SetBranchAddress( "eta", &eta ); // spectator
109 
110  std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
111  TStopwatch sw;
112  sw.Start();
113  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
114 
115  if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
116 
117  theTree->GetEntry(ievt);
118 
119  // --- Return the MVA outputs and fill into histograms
120 
121  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
122  if (!it->second) continue;
123  TString methodName = it->first + " method";
124  hist[it->first]->Fill( reader->EvaluateMVA( methodName ) );
125  }
126 
127  }
128  sw.Stop();
129  std::cout << "--- End of event loop: "; sw.Print();
130 
131  // --- Write histograms
132 
133  TFile *target = new TFile( "TMVApp.root","RECREATE" );
134  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++)
135  if (it->second) hist[it->first]->Write();
136 
137  target->Close();
138  std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
139 
140  delete reader;
141  std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl;
142 }
143 
144 int main( int argc, char** argv )
145 {
147  return 0;
148 }
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
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
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:344
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
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:980
bool Bool_t
Definition: RtypesCore.h:59
int main(int argc, char **argv)
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
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 TMVAClassificationCategoryApplication()
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
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:325
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:484
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
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898
Stopwatch class.
Definition: TStopwatch.h:30