ROOT  6.06/09
Reference Guide
Classification.C
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <map>
4 #include <string>
5 
6 #include "TChain.h"
7 #include "TFile.h"
8 #include "TTree.h"
9 #include "TString.h"
10 #include "TObjString.h"
11 #include "TSystem.h"
12 #include "TROOT.h"
13 
14 
15 #include "TMVA/Factory.h"
16 #include "TMVA/Tools.h"
17 #include<TMVA/MethodRXGB.h>
18 #include<TMVA/MethodRSNNS.h>
19 
20 
22 {
25 
26  TString outfileName("TMVA.root");
27  TFile *outputFile = TFile::Open(outfileName, "RECREATE");
28 
29  TMVA::Factory *factory = new TMVA::Factory("RMVAClassification", outputFile,
30  "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
31 
32  factory->AddVariable("myvar1 := var1+var2", 'F');
33  factory->AddVariable("myvar2 := var1-var2", "Expression 2", "", 'F');
34  factory->AddVariable("var3", "Variable 3", "units", 'F');
35  factory->AddVariable("var4", "Variable 4", "units", 'F');
36  factory->AddSpectator("spec1 := var1*2", "Spectator 1", "units", 'F');
37  factory->AddSpectator("spec2 := var1*3", "Spectator 2", "units", 'F');
38 
39  TString fname = "./tmva_class_example.root";
40 
41  if (gSystem->AccessPathName(fname)) // file does not exist in local directory
42  gSystem->Exec("curl -O http://root.cern.ch/files/tmva_class_example.root");
43 
44  TFile *input = TFile::Open(fname);
45 
46  std::cout << "--- TMVAClassification : Using input file: " << input->GetName() << std::endl;
47 
48  // --- Register the training and test trees
49 
50  TTree *tsignal = (TTree *)input->Get("TreeS");
51  TTree *tbackground = (TTree *)input->Get("TreeB");
52 
53  // global event weights per tree (see below for setting event-wise weights)
54  Double_t signalWeight = 1.0;
55  Double_t backgroundWeight = 1.0;
56 
57  // You can add an arbitrary number of signal or background trees
58  factory->AddSignalTree(tsignal, signalWeight);
59  factory->AddBackgroundTree(tbackground, backgroundWeight);
60 
61 
62  // Set individual event weights (the variables must exist in the original TTree)
63  factory->SetBackgroundWeightExpression("weight");
64 
65 
66  // Apply additional cuts on the signal and background samples (can be different)
67  TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
68  TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
69 
70  // Tell the factory how to use the training and testing events
71  factory->PrepareTrainingAndTestTree(mycuts, mycutb,
72  "nTrain_Signal=0:nTrain_Background=0:nTest_Signal=0:nTest_Background=0:SplitMode=Random:NormMode=NumEvents:!V");
73 
74  //R TMVA Methods
75  factory->BookMethod(TMVA::Types::kC50, "C50",
76  "!H:NTrials=10:Rules=kFALSE:ControlSubSet=kFALSE:ControlBands=0:ControlWinnow=kFALSE:ControlNoGlobalPruning=kTRUE:ControlCF=0.25:ControlMinCases=2:ControlFuzzyThreshold=kTRUE:ControlSample=0:ControlEarlyStopping=kTRUE:!V");
77 
78  factory->BookMethod(TMVA::Types::kRXGB, "RXGB", "!V:NRounds=80:MaxDepth=2:Eta=1");
79 
80  factory->BookMethod(TMVA::Types::kRSNNS, "RMLP", "!H:VarTransform=N:Size=c(5):Maxit=200:InitFunc=Randomize_Weights:LearnFunc=Std_Backpropagation:LearnFuncParams=c(0.2,0):!V");
81 
82  factory->BookMethod(TMVA::Types::kRSVM, "RSVM", "!H:Kernel=linear:Type=C-classification:VarTransform=Norm:Probability=kTRUE:Tolerance=0.1:!V");
83 
84 
85  // Train MVAs using the set of training events
86  factory->TrainAllMethods();
87 
88  // ---- Evaluate all MVAs using the set of test events
89  factory->TestAllMethods();
90 
91  // ----- Evaluate and compare performance of all configured MVAs
92  factory->EvaluateAllMethods();
93  // --------------------------------------------------------------
94 
95  // Save the output
96  outputFile->Close();
97 
98  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
99  std::cout << "==> TMVAClassification is done!" << std::endl;
100 
101  // delete factory;
102  r.SetVerbose(1);
103 
104 }
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:1265
static Tools & Instance()
Definition: Tools.cxx:80
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.
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:564
Basic string class.
Definition: TString.h:137
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:534
void TrainAllMethods()
iterates through all booked methods and calls training
Definition: Factory.cxx:959
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:452
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
A specialized string object used for TTree selections.
Definition: TCut.h:27
void Classification()
ROOT::R::TRInterface & r
Definition: Object.C:4
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
MethodBase * BookMethod(TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
Definition: Factory.cxx:700
void EvaluateAllMethods(void)
iterates over all MVAs that have been booked, and calls their evaluation methods
Definition: Factory.cxx:1179
void TestAllMethods()
Definition: Factory.cxx:1079
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
void SetBackgroundWeightExpression(const TString &variable)
Definition: Factory.cxx:596
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:657
double Double_t
Definition: RtypesCore.h:55
void SetVerbose(Bool_t status)
Method to set verbose mode, that produce extra output.
static TRInterface & Instance()
static method to get an TRInterface instance reference
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:421
A TTree object has a header with a name and a title.
Definition: TTree.h:94
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
Definition: Factory.cxx:673
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898