Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_Higgs_Classification.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ml
3/// \notebook
4/// Classification example of TMVA based on public Higgs UCI dataset
5///
6/// The UCI data set is a public HIGGS data set , see http://archive.ics.uci.edu/ml/datasets/HIGGS
7/// used in this paper: Baldi, P., P. Sadowski, and D. Whiteson. “Searching for Exotic Particles in High-energy Physics
8/// with Deep Learning.” Nature Communications 5 (July 2, 2014).
9///
10/// \macro_image
11/// \macro_code
12/// \macro_output
13///
14/// \author Lorenzo Moneta
15
16/***
17## Declare Factory
18
19Create the Factory class. Later you can choose the methods
20whose performance you'd like to investigate.
21
22The factory is the major TMVA object you have to interact with. Here is the list of parameters you need to pass
23
24 - The first argument is the base of the name of all the output
25weightfiles in the directory weight/ that will be created with the
26method parameters
27
28 - The second argument is the output file for the training results
29
30 - The third argument is a string option defining some general configuration for the TMVA session. For example all TMVA output can be suppressed by removing the "!" (not) in front of the "Silent" argument in the option string
31
32**/
33
34void TMVA_Higgs_Classification() {
35
36 // options to control used methods
37
38 bool useLikelihood = true; // likelihood based discriminant
39 bool useLikelihoodKDE = false; // likelihood based discriminant
40 bool useFischer = true; // Fischer discriminant
41 bool useMLP = false; // Multi Layer Perceptron
42 bool useBDT = true; // Boosted Decision Tree
43
45
46 auto outputFile = TFile::Open("Higgs_ClassificationOutput.root", "RECREATE");
47
48 TMVA::Factory factory("TMVA_Higgs_Classification", outputFile,
49 "!V:ROC:!Silent:Color:AnalysisType=Classification" );
50
51/**
52
53## Setup Dataset(s)
54
55Define now input data file and signal and background trees
56
57 **/
58
59 TString inputFileName = gROOT->GetTutorialDir() + "/machine_learning/data/Higgs_data.root";
60
61 TFile *inputFile = nullptr;
62
63 if (!gSystem->AccessPathName(inputFileName)) {
64 // file exists
65 inputFile = TFile::Open( inputFileName );
66 }
67 if (!inputFile) {
68 Error("TMVA_Higgs_Classification","Input file is not found - exit");
69 return;
70 }
71
72// --- Register the training and test trees
73
74 TTree *signalTree = (TTree*)inputFile->Get("sig_tree");
75 TTree *backgroundTree = (TTree*)inputFile->Get("bkg_tree");
76
77 signalTree->Print();
78
79/***
80## Declare DataLoader(s)
81
82The next step is to declare the DataLoader class that deals with input variables
83
84Define the input variables that shall be used for the MVA training
85note that you may also use variable expressions, which can be parsed by TTree::Draw( "expression" )]
86
87***/
88
89 TMVA::DataLoader * loader = new TMVA::DataLoader("dataset");
90
91 loader->AddVariable("m_jj");
92 loader->AddVariable("m_jjj");
93 loader->AddVariable("m_lv");
94 loader->AddVariable("m_jlv");
95 loader->AddVariable("m_bb");
96 loader->AddVariable("m_wbb");
97 loader->AddVariable("m_wwbb");
98
99/// We set now the input data trees in the TMVA DataLoader class
100
101// global event weights per tree (see below for setting event-wise weights)
102 Double_t signalWeight = 1.0;
103 Double_t backgroundWeight = 1.0;
104
105// You can add an arbitrary number of signal or background trees
106 loader->AddSignalTree ( signalTree, signalWeight );
107 loader->AddBackgroundTree( backgroundTree, backgroundWeight );
108
109
110// Set individual event weights (the variables must exist in the original TTree)
111// for signal : factory->SetSignalWeightExpression ("weight1*weight2");
112// for background: factory->SetBackgroundWeightExpression("weight1*weight2");
113//loader->SetBackgroundWeightExpression( "weight" );
114
115// Apply additional cuts on the signal and background samples (can be different)
116 TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
117 TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
118
119// Tell the factory how to use the training and testing events
120//
121// If no numbers of events are given, half of the events in the tree are used
122// for training, and the other half for testing:
123// loader->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
124// To also specify the number of testing events, use:
125
126 loader->PrepareTrainingAndTestTree( mycuts, mycutb,
127 "nTrain_Signal=7000:nTrain_Background=7000:SplitMode=Random:NormMode=NumEvents:!V" );
128
129/***
130## Booking Methods
131
132Here we book the TMVA methods. We book a Likelihood based on KDE (Kernel Density Estimation), a Fischer discriminant, a BDT
133and a shallow neural network
134
135 */
136
137
138// Likelihood ("naive Bayes estimator")
139if (useLikelihood) {
140 factory.BookMethod(loader, TMVA::Types::kLikelihood, "Likelihood",
141 "H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
142}
143// Use a kernel density estimator to approximate the PDFs
144if (useLikelihoodKDE) {
145 factory.BookMethod(loader, TMVA::Types::kLikelihood, "LikelihoodKDE",
146 "!H:!V:!TransformOutput:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" );
147
148}
149
150// Fisher discriminant (same as LD)
151if (useFischer) {
152 factory.BookMethod(loader, TMVA::Types::kFisher, "Fisher", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
153}
154
155//Boosted Decision Trees
156if (useBDT) {
157 factory.BookMethod(loader,TMVA::Types::kBDT, "BDT",
158 "!V:NTrees=200:MinNodeSize=2.5%:MaxDepth=2:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20" );
159}
160
161//Multi-Layer Perceptron (Neural Network)
162if (useMLP) {
163 factory.BookMethod(loader, TMVA::Types::kMLP, "MLP",
164 "!H:!V:NeuronType=tanh:VarTransform=N:NCycles=100:HiddenLayers=N+5:TestRate=5:!UseRegulator" );
165}
166
167
168 /**
169## Train Methods
170
171Here we train all the previously booked methods.
172
173 */
174
175 factory.TrainAllMethods();
176
177/**
178 ## Test all methods
179
180 Now we test and evaluate all methods using the test data set
181*/
182
183 factory.TestAllMethods();
184
185 factory.EvaluateAllMethods();
186
187/// after we get the ROC curve and we display
188
189 auto c1 = factory.GetROCCurve(loader);
190 c1->Draw();
191
192/// at the end we close the output file which contains the evaluation result of all methods and it can be used by TMVAGUI
193/// to display additional plots
194
195 outputFile->Close();
196
197
198}
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
#define gROOT
Definition TROOT.h:417
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
A specialized string object used for TTree selections.
Definition TCut.h:25
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3804
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
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
This is the main MVA steering class.
Definition Factory.h:80
static Tools & Instance()
Definition Tools.cxx:72
@ kFisher
Definition Types.h:82
@ kLikelihood
Definition Types.h:79
Basic string class.
Definition TString.h:137
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:1319
A TTree represents a columnar dataset.
Definition TTree.h:95
void Print(Option_t *option="") const override
Print a summary of the tree contents.
Definition TTree.cxx:7559
return c1
Definition legend1.C:41