Logo ROOT   6.08/07
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"
18 
20 {
23 
24  TString outfileName("TMVA.root");
25  TFile *outputFile = TFile::Open(outfileName, "RECREATE");
26 
27  TMVA::Factory *factory = new TMVA::Factory("TMVAClassification", outputFile,
28  "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
29 
30 
31  factory->AddVariable("myvar1 := var1+var2", 'F');
32  factory->AddVariable("myvar2 := var1-var2", "Expression 2", "", 'F');
33  factory->AddVariable("var3", "Variable 3", "units", 'F');
34  factory->AddVariable("var4", "Variable 4", "units", 'F');
35 
36 
37  factory->AddSpectator("spec1 := var1*2", "Spectator 1", "units", 'F');
38  factory->AddSpectator("spec2 := var1*3", "Spectator 2", "units", 'F');
39 
40 
41  TString fname = "./tmva_class_example.root";
42 
43  if (gSystem->AccessPathName(fname)) // file does not exist in local directory
44  gSystem->Exec("curl -O http://root.cern.ch/files/tmva_class_example.root");
45 
46  TFile *input = TFile::Open(fname);
47 
48  std::cout << "--- TMVAClassification : Using input file: " << input->GetName() << std::endl;
49 
50  // --- Register the training and test trees
51 
52  TTree *tsignal = (TTree *)input->Get("TreeS");
53  TTree *tbackground = (TTree *)input->Get("TreeB");
54 
55  // global event weights per tree (see below for setting event-wise weights)
56  Double_t signalWeight = 1.0;
57  Double_t backgroundWeight = 1.0;
58 
59  // You can add an arbitrary number of signal or background trees
60  factory->AddSignalTree(tsignal, signalWeight);
61  factory->AddBackgroundTree(tbackground, backgroundWeight);
62 
63 
64  // Set individual event weights (the variables must exist in the original TTree)
65  factory->SetBackgroundWeightExpression("weight");
66 
67 
68  // Apply additional cuts on the signal and background samples (can be different)
69  TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
70  TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
71 
72  // Tell the factory how to use the training and testing events
73  factory->PrepareTrainingAndTestTree(mycuts, mycutb,
74  "nTrain_Signal=0:nTrain_Background=0:nTest_Signal=0:nTest_Background=0:SplitMode=Random:NormMode=NumEvents:!V");
75 
76 
77  ///////////////////
78  //Booking //
79  ///////////////////
80  // Boosted Decision Trees
81 
82  //PyMVA methods
83  factory->BookMethod(TMVA::Types::kPyRandomForest, "PyRandomForest",
84  "!V:NEstimators=150:Criterion=gini:MaxFeatures=auto:MaxDepth=3:MinSamplesLeaf=1:MinWeightFractionLeaf=0:Bootstrap=kTRUE");
85  factory->BookMethod(TMVA::Types::kPyAdaBoost, "PyAdaBoost",
86  "!V:BaseEstimator=None:NEstimators=100:LearningRate=1:Algorithm=SAMME.R:RandomState=None");
87  factory->BookMethod(TMVA::Types::kPyGTB, "PyGTB",
88  "!V:NEstimators=150:Loss=deviance:LearningRate=0.1:Subsample=1:MaxDepth=6:MaxFeatures='auto'");
89 
90 
91  // Train MVAs using the set of training events
92  factory->TrainAllMethods();
93 
94  // ---- Evaluate all MVAs using the set of test events
95  factory->TestAllMethods();
96 
97  // ----- Evaluate and compare performance of all configured MVAs
98  factory->EvaluateAllMethods();
99  // --------------------------------------------------------------
100 
101  // Save the output
102  outputFile->Close();
103 
104  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
105  std::cout << "==> TMVAClassification is done!" << std::endl;
106 
107 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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:1266
static Tools & Instance()
Definition: Tools.cxx:80
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Definition: Factory.cxx:337
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Basic string class.
Definition: TString.h:137
void TrainAllMethods()
iterates through all booked methods and calls training
Definition: Factory.cxx:822
static void PyInitialize()
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:3907
A specialized string object used for TTree selections.
Definition: TCut.h:27
void Classification()
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
void EvaluateAllMethods(void)
iterates over all MVAs that have been booked, and calls their evaluation methods
Definition: Factory.cxx:1059
void TestAllMethods()
Definition: Factory.cxx:958
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:658
double Double_t
Definition: RtypesCore.h:55
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:904