-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMVAClassification.C
98 lines (76 loc) · 4.09 KB
/
TMVAClassification.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**********************************************************************************
* This macro performs training and testing of a BDT classifier to discriminate *
* physics signal from a soft-QCD/MinimumBias background. *
* *
* Input data: Signal = Drell-Yan Z->ee events, Background = MinimumBias events *
* *
* To run this macro, type the following command in a tereminal window *
* *
* root -l TMVAClassification.C *
* *
* The output file "TMVA.root" can be analysed with the use of dedicated *
* macros (simply say: root -l <macro.C>), which can be conveniently *
* invoked through a GUI that will appear at the end of the run of this macro. *
**********************************************************************************/
#include "TMVAGui.C"
void TMVAClassification( )
{
// this loads the library
TMVA::Tools::Instance();
std::cout << std::endl;
std::cout << "==> Start TMVAClassification" << std::endl;
// Create a new root output file.
TString outfileName( "TMVA.root" );
TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
// Create the factory object.
TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile,
"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D" );
// ---------- input variables
factory->AddVariable("iso12x12", 'F');
factory->AddVariable("iso4x4", 'F');
factory->AddVariable("isoLshaped", 'F');
factory->AddVariable("PUM0", 'F');
// ---------- spectators ----------------
factory->AddSpectator("pt", "F");
factory->AddSpectator("eta", "F");
factory->AddSpectator("phi", "F");
factory->AddSpectator("e", "F");
// read training and test data
TString fSig = "DrellYan_Zee.root";
TString fBkg = "MinBias.root";
TFile *fileSig = TFile::Open( fSig );
TFile *fileBkg = TFile::Open( fBkg );
TTree *signal = (TTree*)fileSig->Get("tree");
TTree *background = (TTree*)fileBkg->Get("tree");
// global event weights per tree (see below for setting event-wise weights)
Double_t signalWeight = 1.0;
Double_t backgroundWeight = 1.0;
// ====== register trees ====================================================
// you can add an arbitrary number of signal or background trees
factory->AddSignalTree ( signal, signalWeight );
factory->AddBackgroundTree( background, backgroundWeight );
TCut mycuts = "pt > 10";
TCut mycutb = "pt > 10";
// tell the factory to use all remaining events in the trees after training for testing:
factory->PrepareTrainingAndTestTree( mycuts, mycutb,
"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
// If no numbers of events are given, half of the events in the tree are used for training, and
// the other half for testing:
// ---- Book MVA methods
// Boosted Decision Trees
factory->BookMethod( TMVA::Types::kBDT, "BDT",
"!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
// ---- Train MVAs using the set of training events
factory->TrainAllMethods();
// ---- Evaluate all MVAs using the set of test events
factory->TestAllMethods();
// ----- Evaluate and compare performance of all configured MVAs
factory->EvaluateAllMethods();
// Save the output
outputFile->Close();
std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
std::cout << "==> TMVAClassification is done!" << std::endl;
delete factory;
// Launch the GUI for the root macros
if (!gROOT->IsBatch()) TMVAGui( outfileName );
}