ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMVAClassificationCategory.C
Go to the documentation of this file.
1 // @(#)root/tmva $Id: TMVAClassificationCategory.C,v 1.36 2009-04-14 13:08:13 andreas.hoecker Exp $
2 /**********************************************************************************
3  * Project : TMVA - a Root-integrated toolkit for multivariate data analysis *
4  * Package : TMVA *
5  * Root Macro: TMVAClassificationCategory *
6  * *
7  * This macro provides examples for the training and testing of the *
8  * TMVA classifiers in categorisation mode. *
9  * *
10  * As input data is used a toy-MC sample consisting of four Gaussian-distributed *
11  * and linearly correlated input variables with category (eta) dependent *
12  * properties. *
13  * *
14  * For this example, only Fisher and Likelihood are used. Run via: *
15  * *
16  * root -l TMVAClassificationCategory.C *
17  * *
18  * The output file "TMVA.root" can be analysed with the use of dedicated *
19  * macros (simply say: root -l <macro.C>), which can be conveniently *
20  * invoked through a GUI that will appear at the end of the run of this macro. *
21  **********************************************************************************/
22 
23 #include <cstdlib>
24 #include <iostream>
25 #include <map>
26 #include <string>
27 
28 #include "TChain.h"
29 #include "TFile.h"
30 #include "TTree.h"
31 #include "TString.h"
32 #include "TObjString.h"
33 #include "TSystem.h"
34 #include "TROOT.h"
35 
36 #include "TMVA/MethodCategory.h"
37 #include "TMVA/Factory.h"
38 #include "TMVA/Tools.h"
39 #include "TMVA/TMVAGui.h"
40 
41 
42 // two types of category methods are implemented
44 
46 {
47  //---------------------------------------------------------------
48  // Example for usage of different event categories with classifiers
49 
50  std::cout << std::endl << "==> Start TMVAClassificationCategory" << std::endl;
51 
52  // This loads the library
54 
55  bool batchMode = false;
56 
57  // Create a new root output file.
58  TString outfileName( "TMVA.root" );
59  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
60 
61  // Create the factory object (see TMVAClassification.C for more information)
62 
63  std::string factoryOptions( "!V:!Silent:Transformations=I;D;P;G,D" );
64  if (batchMode) factoryOptions += ":!Color:!DrawProgressBar";
65 
66  TMVA::Factory *factory = new TMVA::Factory( "TMVAClassificationCategory", outputFile, factoryOptions );
67 
68  // Define the input variables used for the MVA training
69  factory->AddVariable( "var1", 'F' );
70  factory->AddVariable( "var2", 'F' );
71  factory->AddVariable( "var3", 'F' );
72  factory->AddVariable( "var4", 'F' );
73 
74  // You can add so-called "Spectator variables", which are not used in the MVA training,
75  // but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the
76  // input variables, the response values of all trained MVAs, and the spectator variables
77  factory->AddSpectator( "eta" );
78 
79  // Load the signal and background event samples from ROOT trees
80  TFile *input(0);
81  TString fname = TString(gSystem->DirName(__FILE__) ) + "/data/";
82  if (gSystem->AccessPathName( fname )) {
83  // if directory data not found try using tutorials dir
84  fname = TString(gROOT->GetTutorialsDir()) + "/tmva/data/";
85  }
86  if (UseOffsetMethod) fname += "toy_sigbkg_categ_offset.root";
87  else fname += "toy_sigbkg_categ_varoff.root";
88  if (!gSystem->AccessPathName( fname )) {
89  // first we try to find tmva_example.root in the local directory
90  std::cout << "--- TMVAClassificationCategory: Accessing " << fname << std::endl;
91  input = TFile::Open( fname );
92  }
93 
94  if (!input) {
95  std::cout << "ERROR: could not open data file: " << fname << std::endl;
96  exit(1);
97  }
98 
99  TTree *signal = (TTree*)input->Get("TreeS");
100  TTree *background = (TTree*)input->Get("TreeB");
101 
102  /// Global event weights per tree (see below for setting event-wise weights)
103  Double_t signalWeight = 1.0;
104  Double_t backgroundWeight = 1.0;
105 
106  /// You can add an arbitrary number of signal or background trees
107  factory->AddSignalTree ( signal, signalWeight );
108  factory->AddBackgroundTree( background, backgroundWeight );
109 
110  // Apply additional cuts on the signal and background samples (can be different)
111  TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
112  TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
113 
114  // Tell the factory how to use the training and testing events
115  factory->PrepareTrainingAndTestTree( mycuts, mycutb,
116  "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
117 
118  // ---- Book MVA methods
119 
120  // Fisher discriminant
121  factory->BookMethod( TMVA::Types::kFisher, "Fisher", "!H:!V:Fisher" );
122 
123  // Likelihood
124  factory->BookMethod( TMVA::Types::kLikelihood, "Likelihood",
125  "!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
126 
127  // --- Categorised classifier
128  TMVA::MethodCategory* mcat = 0;
129 
130  // The variable sets
131  TString theCat1Vars = "var1:var2:var3:var4";
132  TString theCat2Vars = (UseOffsetMethod ? "var1:var2:var3:var4" : "var1:var2:var3");
133 
134  // Fisher with categories
135  TMVA::MethodBase* fiCat = factory->BookMethod( TMVA::Types::kCategory, "FisherCat","" );
136  mcat = dynamic_cast<TMVA::MethodCategory*>(fiCat);
137  mcat->AddMethod( "abs(eta)<=1.3", theCat1Vars, TMVA::Types::kFisher, "Category_Fisher_1","!H:!V:Fisher" );
138  mcat->AddMethod( "abs(eta)>1.3", theCat2Vars, TMVA::Types::kFisher, "Category_Fisher_2","!H:!V:Fisher" );
139 
140  // Likelihood with categories
141  TMVA::MethodBase* liCat = factory->BookMethod( TMVA::Types::kCategory, "LikelihoodCat","" );
142  mcat = dynamic_cast<TMVA::MethodCategory*>(liCat);
143  mcat->AddMethod( "abs(eta)<=1.3",theCat1Vars, TMVA::Types::kLikelihood,
144  "Category_Likelihood_1","!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
145  mcat->AddMethod( "abs(eta)>1.3", theCat2Vars, TMVA::Types::kLikelihood,
146  "Category_Likelihood_2","!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
147 
148  // ---- Now you can tell the factory to train, test, and evaluate the MVAs
149 
150  // Train MVAs using the set of training events
151  factory->TrainAllMethods();
152 
153  // ---- Evaluate all MVAs using the set of test events
154  factory->TestAllMethods();
155 
156  // ----- Evaluate and compare performance of all configured MVAs
157  factory->EvaluateAllMethods();
158 
159  // --------------------------------------------------------------
160 
161  // Save the output
162  outputFile->Close();
163 
164  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
165  std::cout << "==> TMVAClassificationCategory is done!" << std::endl;
166 
167  // Clean up
168  delete factory;
169 
170  // Launch the GUI for the root macros
171  if (!gROOT->IsBatch()) TMVA::TMVAGui( outfileName );
172 }
173 int main( int argc, char** argv )
174 {
176  return 0;
177 }
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
static Tools & Instance()
Definition: Tools.cxx:80
TMVA::IMethod * AddMethod(const TCut &, const TString &theVariables, Types::EMVA theMethod, const TString &theTitle, const TString &theOptions)
adds sub-classifier for a category
int main(int argc, char **argv)
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
void TMVAClassificationCategory()
void AddSpectator(const TString &expression, const TString &title="", const TString &unit="", Double_t min=0, Double_t max=0)
user inserts target in data set info
Definition: Factory.cxx:570
Basic string class.
Definition: TString.h:137
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:980
bool Bool_t
Definition: RtypesCore.h:59
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
user inserts discriminating variable in data set info
Definition: Factory.cxx:540
void TrainAllMethods()
iterates through all booked methods and calls training
Definition: Factory.cxx:965
Bool_t UseOffsetMethod
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
Definition: Factory.cxx:458
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 TMVAGui(const char *fName="TMVA.root")
Definition: TMVAGui.cxx:52
A specialized string object used for TTree selections.
Definition: TCut.h:27
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
MethodBase * BookMethod(TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
Definition: Factory.cxx:706
void EvaluateAllMethods(void)
iterates over all MVAs that have been booked, and calls their evaluation methods
Definition: Factory.cxx:1185
void TestAllMethods()
Definition: Factory.cxx:1085
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
double Double_t
Definition: RtypesCore.h:55
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
Definition: Factory.cxx:427
A TTree object has a header with a name and a title.
Definition: TTree.h:98
const Bool_t kTRUE
Definition: Rtypes.h:91
TH1F * background
Definition: fithist.C:4
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
Definition: Factory.cxx:679
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898