Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_Higgs_Classification.py
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_output
12## \macro_code
13##
14## \author Harshal Shende
15
16## Declare Factory
17
18
19## Create the Factory class. Later you can choose the methods
20## whose performance you'd like to investigate.
21
22## The 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
25## weightfiles in the directory weight/ that will be created with the
26## method 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
32import ROOT
33
34TMVA = ROOT.TMVA
35TFile = ROOT.TFile
36
38
39# options to control used methods
40useLikelihood = True # likelihood based discriminant
41useLikelihoodKDE = False # likelihood based discriminant
42useFischer = True # Fischer discriminant
43useMLP = False # Multi Layer Perceptron
44useBDT = True # Boosted Decision Tree
45
46outputFile = TFile.Open("Higgs_ClassificationOutput.root", "RECREATE")
47factory = TMVA.Factory(
48 "TMVA_Higgs_Classification", outputFile, V=False, ROC=True, Silent=False, Color=True, AnalysisType="Classification"
49)
50
51
52## Setup Dataset(s)
53
54# Define now input data file and signal and background trees
55
56inputFileName = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/Higgs_data.root"
57
58inputFile = TFile.Open(inputFileName)
59if inputFile is None:
60 raise FileNotFoundError("Input file is not found - exit")
61
62
63# --- Register the training and test trees
64signalTree = inputFile.Get("sig_tree")
65backgroundTree = inputFile.Get("bkg_tree")
66signalTree.Print()
67
68## Declare DataLoader(s)
69
70# The next step is to declare the DataLoader class that deals with input variables
71# Define the input variables that shall be used for the MVA training
72# note that you may also use variable expressions, which can be parsed by TTree::Draw( "expression" )]
73loader = TMVA.DataLoader("dataset")
74
75loader.AddVariable("m_jj")
76loader.AddVariable("m_jjj")
77loader.AddVariable("m_lv")
78loader.AddVariable("m_jlv")
79loader.AddVariable("m_bb")
80loader.AddVariable("m_wbb")
81loader.AddVariable("m_wwbb")
82
83# We set now the input data trees in the TMVA DataLoader class
84# global event weights per tree (see below for setting event-wise weights)
85signalWeight = 1.0
86backgroundWeight = 1.0
87# You can add an arbitrary number of signal or background trees
88loader.AddSignalTree(signalTree, signalWeight)
89loader.AddBackgroundTree(backgroundTree, backgroundWeight)
90
91# Set individual event weights (the variables must exist in the original TTree)
92# for signal : factory->SetSignalWeightExpression ("weight1*weight2");
93# for background: factory->SetBackgroundWeightExpression("weight1*weight2");
94# loader->SetBackgroundWeightExpression( "weight" );
95
96
97# Apply additional cuts on the signal and background samples (can be different)
98mycuts = ROOT.TCut("") # for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
99mycutb = ROOT.TCut("") # for example: TCut mycutb = "abs(var1)<0.5";
100
101# Tell the factory how to use the training and testing events
102#
103# If no numbers of events are given, half of the events in the tree are used
104# for training, and the other half for testing:
105# loader->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
106# To also specify the number of testing events, use:
107
108loader.PrepareTrainingAndTestTree(
109 mycuts, mycutb, nTrain_Signal=7000, nTrain_Background=7000, SplitMode="Random", NormMode="NumEvents", V=False
110)
111
112## Booking Methods
113
114# Here we book the TMVA methods. We book a Likelihood based on KDE (Kernel Density Estimation), a Fischer discriminant, a BDT
115# and a shallow neural network
116# Likelihood ("naive Bayes estimator")
117if useLikelihood:
118 factory.BookMethod(
119 loader,
120 TMVA.Types.kLikelihood,
121 "Likelihood",
122 H=True,
123 V=False,
124 TransformOutput=True,
125 PDFInterpol="Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10",
126 NSmooth=1,
127 NAvEvtPerBin=50,
128 )
129
130# Use a kernel density estimator to approximate the PDFs
131if useLikelihoodKDE:
132 factory.BookMethod(
133 loader,
134 TMVA.Types.kLikelihood,
135 "LikelihoodKDE",
136 H=False,
137 V=False,
138 TransformOutput=False,
139 PDFInterpol="KDE",
140 KDEtype="Gauss",
141 KDEiter="Adaptive",
142 KDEFineFactor=0.3,
143 KDEborder=None,
144 NAvEvtPerBin=50,
145 )
146
147# Fisher discriminant (same as LD)
148if useFischer:
149 factory.BookMethod(
150 loader,
151 TMVA.Types.kFisher,
152 "Fisher",
153 H=True,
154 V=False,
155 Fisher=True,
156 VarTransform=None,
157 CreateMVAPdfs=True,
158 PDFInterpolMVAPdf="Spline2",
159 NbinsMVAPdf=50,
160 NsmoothMVAPdf=10,
161 )
162
163# Boosted Decision Trees
164if useBDT:
165 factory.BookMethod(
166 loader,
167 TMVA.Types.kBDT,
168 "BDT",
169 V=False,
170 NTrees=200,
171 MinNodeSize="2.5%",
172 MaxDepth=2,
173 BoostType="AdaBoost",
174 AdaBoostBeta=0.5,
175 UseBaggedBoost=True,
176 BaggedSampleFraction=0.5,
177 SeparationType="GiniIndex",
178 nCuts=20,
179 )
180
181# Multi-Layer Perceptron (Neural Network)
182if useMLP:
183 factory.BookMethod(
184 loader,
185 TMVA.Types.kMLP,
186 "MLP",
187 H=False,
188 V=False,
189 NeuronType="tanh",
190 VarTransform="N",
191 NCycles=100,
192 HiddenLayers="N+5",
193 TestRate=5,
194 UseRegulator=False,
195 )
196
197## Train Methods
198
199# Here we train all the previously booked methods.
200
201factory.TrainAllMethods()
202## Test all methods
203
204# Now we test and evaluate all methods using the test data set
205factory.TestAllMethods()
206
207factory.EvaluateAllMethods()
208
209# after we get the ROC curve and we display
210
211c1 = factory.GetROCCurve(loader)
212c1.Draw()
213# at the end we close the output file which contains the evaluation result of all methods and it can be used by TMVAGUI
214# to display additional plots
215
216outputFile.Close()
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
This is the main MVA steering class.
Definition Factory.h:80
static Tools & Instance()
Definition Tools.cxx:72