ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/jbabb/ScriptsAndMacros/TMVAscript_TTbar.C
Revision: 1.1
Committed: Sat Sep 3 21:22:28 2011 UTC (13 years, 8 months ago) by jbabb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 jbabb 1.1 //TMVA script for Z' nTuple
2     //John Babb 7-9-09 ---> mostly stolen from example in TMVA
3    
4    
5     #include <iostream>
6    
7    
8     #include "TFile.h"
9     #include "TSystem.h"
10     #include "TTree.h"
11     #include "TBranch.h"
12     #include "TLeaf.h"
13    
14     #include "tmva/test/TMVAGui.C"
15     #include "TMVA/Tools.h"
16     #include "TMVA/Factory.h"
17     #include "TPluginManager.h"
18    
19     void TMVAscript_TTbar() {
20    
21    
22     std::cout << "Start TMVAnalysis" << std::endl
23     << "=================" << std::endl
24     << std::endl;
25    
26    
27     // Create a new root output file.
28     TString outfileName( "TTbar_BDT.root" );
29     TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
30    
31     TMVA::Factory *factory = new TMVA::Factory( "TMVAnalysis", outputFile, Form("!V:!Silent:%sColor", gROOT->IsBatch()?"!":"") );
32    
33    
34    
35     //Add all the variables from the events
36     factory->AddVariable( "pt_smuon", 'F' );
37     factory->AddVariable( "p_smuon", 'F' );
38     factory->AddVariable( "eta_smuon", 'F' );
39     factory->AddVariable( "ptRel_smuon", 'F' );
40     factory->AddVariable( "drMinMuJ_smuon", 'F' );
41     factory->AddVariable( "drMaxMuJ_smuon", 'F' );
42     factory->AddVariable( "dPhiMuMET_smuon", 'F' );
43     factory->AddVariable( "d0_smuon", 'F' );
44     factory->AddVariable( "MtMuMET_smuon", 'F' );
45     factory->AddVariable( "MtMuJetMET_smuon",'F' );
46     factory->AddVariable( "MMuJet1_smuon", 'F' );
47     factory->AddVariable( "relIso_smuon", 'F' );
48    
49     factory->AddVariable( "pt_jet1", 'F' );
50     factory->AddVariable( "p_jet1", 'F' );
51     factory->AddVariable( "eta_jet1", 'F' );
52    
53     factory->AddVariable( "pt_jet2", 'F' );
54     factory->AddVariable( "p_jet2", 'F' );
55     factory->AddVariable( "eta_jet2", 'F' );
56    
57     factory->AddVariable( "pt_jet3", 'F' );
58     factory->AddVariable( "p_jet3", 'F' );
59     factory->AddVariable( "eta_jet3", 'F' );
60    
61     factory->AddVariable( "met", 'F' );
62     factory->AddVariable( "neutrinoPz", 'F' );
63    
64     //factory->AddVariable( "drJ1J2", 'F' );
65     //factory->AddVariable( "drJ1J3", 'F' );
66     //factory->AddVariable( "drJ2J3", 'F' );
67     //factory->AddVariable( "drMinJ1", 'F' );
68     //factory->AddVariable( "drMinJ2", 'F' );
69     //factory->AddVariable( "drMinJ3", 'F' );
70     factory->AddVariable( "nHardJets", 'I' );
71     factory->AddVariable( "bJets", 'I' );
72     factory->AddVariable( "HtJets", 'F' );
73     factory->AddVariable( "HtJetsLepMET", 'F' );
74     factory->AddVariable( "HJets", 'F' );
75     factory->AddVariable( "HJetsLepMET", 'F' );
76     factory->AddVariable( "MJ1J2J3", 'F' );
77     factory->AddVariable( "etaJ1J2J3", 'F' );
78     factory->AddVariable( "MJ1J2", 'F' );
79     factory->AddVariable( "MJ1J3", 'F' );
80     factory->AddVariable( "MJ2J3", 'F' );
81     factory->AddVariable( "etaJ1J2", 'F' );
82     factory->AddVariable( "etaJ2J3", 'F' );
83     factory->AddVariable( "etaJ1J3", 'F' );
84     factory->AddVariable( "sphericity", 'F' );
85     factory->AddVariable( "aplanarity", 'F' );
86     factory->AddVariable( "centrality", 'F' );
87    
88     //factory->AddVariable("unfittedHadWPx", 'F' );
89     //factory->AddVariable("unfittedHadWPy", 'F' );
90     //factory->AddVariable("unfittedHadWPz", 'F' );
91     factory->AddVariable("unfittedHadWPt", 'F' );
92     factory->AddVariable("unfittedHadWE", 'F' );
93     //factory->AddVariable("unfittedHadWTheta", 'F' );
94     //factory->AddVariable("unfittedHadWPhi", 'F' );
95     factory->AddVariable("unfittedHadWEta", 'F' );
96     factory->AddVariable("unfittedHadWMass", 'F' );
97     //factory->AddVariable("unfittedLepWPx", 'F' );
98     //factory->AddVariable("unfittedLepWPy", 'F' );
99     //factory->AddVariable("unfittedLepWPz", 'F' );
100     factory->AddVariable("unfittedLepWPt", 'F' );
101     factory->AddVariable("unfittedLepWE", 'F' );
102     //factory->AddVariable("unfittedLepWTheta", 'F' );
103     //factory->AddVariable("unfittedLepWPhi", 'F' );
104     factory->AddVariable("unfittedLepWEta", 'F' );
105     factory->AddVariable("unfittedLepWMass", 'F' );
106    
107     //factory->AddVariable("unfittedHadTopPx", 'F' );
108     //factory->AddVariable("unfittedHadTopPy", 'F' );
109     //factory->AddVariable("unfittedHadTopPz", 'F' );
110     factory->AddVariable("unfittedHadTopPt", 'F' );
111     factory->AddVariable("unfittedHadTopE", 'F' );
112     //factory->AddVariable("unfittedHadTopTheta", 'F' );
113     //factory->AddVariable("unfittedHadTopPhi", 'F' );
114     factory->AddVariable("unfittedHadTopEta", 'F' );
115     factory->AddVariable("unfittedHadTopMass", 'F' );
116     //factory->AddVariable("unfittedLepTopPx", 'F' );
117     //factory->AddVariable("unfittedLepTopPy", 'F' );
118     //factory->AddVariable("unfittedLepTopPz", 'F' );
119     factory->AddVariable("unfittedLepTopPt", 'F' );
120     factory->AddVariable("unfittedLepTopE", 'F' );
121     //factory->AddVariable("unfittedLepTopTheta", 'F' );
122     //factory->AddVariable("unfittedLepTopPhi", 'F' );
123     factory->AddVariable("unfittedLepTopEta", 'F' );
124     factory->AddVariable("unfittedLepTopMass", 'F' );
125    
126     factory->AddVariable("unfittedEvent_ttInvariantMass",'F' );
127    
128     factory->AddVariable("tt_dTheta",'F' );
129     factory->AddVariable("tt_dPhi",'F' );
130     factory->AddVariable("tt_dR",'F' );
131    
132    
133    
134    
135    
136    
137    
138     // load the signal and background event samples from ROOT trees
139    
140     //TFile *input1 = TFile::Open("./files/BDT_TrainingSamples_38X/ZPrime500.root");
141     //std::cout << "--- TMVAnalysis : Accessing " << "ZPrime500.root" << std::endl;
142    
143     TFile *input2 = TFile::Open("Preselection/Skynet_Preselection_Zprime750.root");
144     std::cout << "--- TMVAnalysis : Accessing " << "ZPrime750.root" << std::endl;
145    
146     TFile *input3 = TFile::Open("Preselection/Skynet_Preselection_Zprime1000.root");
147     std::cout << "--- TMVAnalysis : Accessing " << "ZPrime1000.root" << std::endl;
148    
149     TFile *input4 = TFile::Open("Preselection/Skynet_Preselection_Zprime1250.root");
150     std::cout << "--- TMVAnalysis : Accessing " << "ZPrime1250.root" << std::endl;
151    
152     //TFile *input5 = TFile::Open("Preselection/Skynet_Preselection_Zprime1500.root");
153     //std::cout << "--- TMVAnalysis : Accessing " << "ZPrime1500.root" << std::endl;
154    
155     //TFile *input6 = TFile::Open("Preselection/Skynet_Preselection_Zprime2000.root");
156     //std::cout << "--- TMVAnalysis : Accessing " << "ZPrime2000.root" << std::endl;
157    
158     TFile *input7 = TFile::Open("Preselection/Skynet_Preselection_TTbar.root");
159     std::cout << "--- TMVAnalysis : Accessing " << "TTbar.root" << std::endl;
160    
161     //TFile *input8 = TFile::Open("./files/Preselection/TTbar_M1TeV.root");
162     //std::cout << "--- TMVAnalysis : Accessing " << "TTbar_M1TeV.root" << std::endl;
163    
164     TFile *input9 = TFile::Open("Preselection/Skynet_Preselection_ST-t-T.root");
165     std::cout << "--- TMVAnalysis : Accessing " << "SingleTop_ST-t-T.root" << std::endl;
166    
167     TFile *input10 = TFile::Open("Preselection/Skynet_Preselection_ST-s-T.root");
168     std::cout << "--- TMVAnalysis : Accessing " << "SingleTop_ST-s-T.root" << std::endl;
169    
170     TFile *input11 = TFile::Open("Preselection/Skynet_Preselection_ST-tW-T.root");
171     std::cout << "--- TMVAnalysis : Accessing " << "SingleTop_ST-tW-T.root" << std::endl;
172    
173     TFile *input12 = TFile::Open("Preselection/Skynet_Preselection_WJets.root");
174     std::cout << "--- TMVAnalysis : Accessing " << "WJets.root" << std::endl;
175    
176     TFile *input13 = TFile::Open("Preselection/Skynet_Preselection_ZJets.root");
177     std::cout << "--- TMVAnalysis : Accessing " << "ZJets.root" << std::endl;
178    
179     /*
180     TFile *input14 = TFile::Open("./files/Preselection/Skynet_Preselection_ZZ.root");
181     std::cout << "--- TMVAnalysis : Accessing " << "ZZ.root" << std::endl;
182    
183     TFile *input15 = TFile::Open("./files/Preselection/Skynet_Preselection_WZ.root");
184     std::cout << "--- TMVAnalysis : Accessing " << "WZ.root" << std::endl;
185    
186     TFile *input16 = TFile::Open("./files/Preselection/Skynet_Preselection_WW.root");
187     std::cout << "--- TMVAnalysis : Accessing " << "WW.root" << std::endl;
188     */
189     TFile *input17 = TFile::Open("Preselection/Skynet_Preselection_QCD.root");
190     std::cout << "--- TMVAnalysis : Accessing " << "InclusiveMu15.root" << std::endl;
191    
192    
193     //Signal
194     //TTree *M500 = (TTree*)input1->Get("PATuple");
195     TTree *M750 = (TTree*)input2->Get("PATuple");
196     TTree *M1000 = (TTree*)input3->Get("PATuple");
197     TTree *M1250 = (TTree*)input4->Get("PATuple");
198     //TTree *M1500 = (TTree*)input5->Get("PATuple");
199     //TTree *M2000 = (TTree*)input6->Get("PATuple");
200     TTree *ttbar = (TTree*)input7->Get("PATuple");
201    
202     //Background
203     //TTree *ttcont = (TTree*)input8->Get("PATuple");
204     TTree *st_t = (TTree*)input9->Get("PATuple");
205     TTree *st_s = (TTree*)input10->Get("PATuple");
206     TTree *st_tW = (TTree*)input11->Get("PATuple");
207     TTree *wjets = (TTree*)input12->Get("PATuple");
208     TTree *zjets = (TTree*)input13->Get("PATuple");
209     //TTree *zz = (TTree*)input14->Get("PATuple");
210     //TTree *wz = (TTree*)input15->Get("PATuple");
211     //TTree *ww = (TTree*)input16->Get("PATuple");
212     TTree *qcd = (TTree*)input17->Get("PATuple");
213    
214    
215    
216    
217    
218     // global event weights per tree (see below for setting event-wise weights)
219     Double_t signalWeight = 1.0;
220     Double_t backgroundWeight = 1.0;
221    
222     // ====== register trees ====================================================
223     //
224     // the following method is the prefered one:
225     // you can add an arbitrary number of signal or background trees
226     factory->AddSignalTree ( ttbar, signalWeight );
227     //factory->AddSignalTree ( M500, signalWeight );
228     factory->AddSignalTree ( M750, signalWeight );
229     factory->AddSignalTree ( M1000, signalWeight );
230     factory->AddSignalTree ( M1250, signalWeight );
231     //factory->AddSignalTree ( M1500, signalWeight );
232     //factory->AddSignalTree ( M2000, signalWeight );
233    
234     //factory->AddBackgroundTree( ttcont, backgroundWeight );
235     factory->AddBackgroundTree( wjets, backgroundWeight );
236     factory->AddBackgroundTree( zjets, backgroundWeight );
237     factory->AddBackgroundTree( st_t, backgroundWeight );
238     factory->AddBackgroundTree( st_s, backgroundWeight );
239     factory->AddBackgroundTree( st_tW, backgroundWeight );
240     //factory->AddBackgroundTree( zz, backgroundWeight );
241     //factory->AddBackgroundTree( wz, backgroundWeight );
242     //factory->AddBackgroundTree( ww, backgroundWeight );
243     factory->AddBackgroundTree( qcd, backgroundWeight );
244    
245     TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
246     TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
247    
248     // tell the factory to use all remaining events in the trees after training for testing:
249     factory->PrepareTrainingAndTestTree( mycuts, mycutb, "SplitMode=Random:NormMode=NumEvents:!V" );
250    
251     factory->BookMethod( TMVA::Types::kBDT, "BDT", "H:V:NTrees=200:nEventsMin=20:nCuts=20:PruneStrength=-1:NodePurityLimit=0.5" );
252    
253    
254     // Train MVAs using the set of training events
255     factory->TrainAllMethods();
256    
257     // ---- Evaluate all MVAs using the set of test events
258     factory->TestAllMethods();
259    
260     // ----- Evaluate and compare performance of all configured MVAs
261     factory->EvaluateAllMethods();
262    
263     // --------------------------------------------------------------
264    
265     // Save the output
266     outputFile->Close();
267    
268     std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
269     std::cout << "==> TMVAnalysis is done!" << std::endl;
270    
271     delete factory;
272    
273     // Launch the GUI for the root macros
274     if (!gROOT->IsBatch()) TMVAGui( outfileName );
275    
276     }
277    
278    
279    
280    
281    
282    
283    
284    
285    
286    
287    
288