forked from RemKamal/ElectronID
-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawROCandWPv4.C
391 lines (323 loc) · 13.5 KB
/
drawROCandWPv4.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "TLatex.h"
#include "TTree.h"
#include "TLegend.h"
#include "TMarker.h"
#include "TCanvas.h"
#include "TString.h"
#include "TH1F.h"
#include "TFile.h"
#include "OptimizationConstants.hh"
#include "VarCut.hh"
#include <iostream>
using std::cout;
// Draw barrel or endcap
const bool drawBarrel = true;
const int nROCs = 4;
const bool verbose = true;
// Files with signal and background trees (ideally the ntuples
// that were used for TMVA optimization
const TString fnameSignal = "root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/samples/DY_Run2Asympt25ns_miniAOD_sept18_flat_ntuple_withWeights.root";
const TString signalTreeName = "electronTree";
const TString fnameBackground = "root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/samples/TT_Run2Asympt25ns_miniAOD_sept21_flat_ntuple_withWeights.root";
const TString backgroundTreeName = "electronTree";
// Name TMVA output file that contains the pre-computed ROC, etc
const TString tmvaFileNameBarrel[nROCs] = {
//only 1st file was optimized and will be used
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root",
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root",
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root",
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root"
};
const TString tmvaFileNameEndcap[nROCs] ={
//no optimization was done for endcap, only for aestetic purpose
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root",
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root",
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root",
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/trainingData/TMVA_training_results_barrel_pass1_20151101_100000.root"
};
//
// Names of ROOT files that contain working points
//
const int nWorkingPointSets = 4;
// Set 1
const int markerColorSet1 = kRed;
const int markerStyleSet1 = 20;
const TString legendSet1 = "WP_Veto";
const int nWP = 1;
const TString cutFileNamesBarrelSet1[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
const TString cutFileNamesEndcapSet1[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
// Set 2
const int markerColorSet2 = kOrange;
const int markerStyleSet2 = 20;
const TString legendSet2 = "WP_Loose";
const TString cutFileNamesBarrelSet2[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
const TString cutFileNamesEndcapSet2[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
// Set 3
const int markerColorSet3 = kBlue;
const int markerStyleSet3 = 20;
const TString legendSet3 = "WP_Medium";
const TString cutFileNamesBarrelSet3[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
const TString cutFileNamesEndcapSet3[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
// Set 4
const int markerColorSet4 = kGreen;
const int markerStyleSet4 = 20;
const TString legendSet4 = "WP_Tight";
const TString cutFileNamesBarrelSet4[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
const TString cutFileNamesEndcapSet4[nWP] = {
"root://cmsxrootd.fnal.gov///store/user/cmsdas/2016/SHORT_EXERCISES/ElectronsAndPhotons/cut_repository/cuts_barrel_20151101_100000_WP_Veto.root"
};
void bazinga (std::string message){
if (verbose)
std::cout<<message<<std::endl;
}
// Forward declarations
TTree *getTreeFromFile(TString fname, TString tname);
void overlayWorkingPoints(TCanvas *c1,
TTree *signalTree, TTree *backgroundTree,
const TString *cutFileNames,
int markerColor, int markerStyle,
TLegend *leg, const TString legendText);
void findEfficiencies(TTree *signalTree, TTree *backgroundTree,
float &effSignal, float &effBackground,
VarCut *cutObject);
//
// Main function
//
void drawROCandWPv4(){
bazinga("Step #0");
TCanvas *c1 = new TCanvas("c1","",10,10,600,600);
// c1->cd();
TString tmvaFileName1 = tmvaFileNameBarrel[0];
TString tmvaFileName2 = tmvaFileNameBarrel[1];
TString tmvaFileName3 = tmvaFileNameBarrel[2];
TString tmvaFileName4 = tmvaFileNameBarrel[3];
bazinga("Step #1");
const TString *cutFileNamesSet1 = cutFileNamesBarrelSet1;
const TString *cutFileNamesSet2 = cutFileNamesBarrelSet2;
const TString *cutFileNamesSet3 = cutFileNamesBarrelSet3;
const TString *cutFileNamesSet4 = cutFileNamesBarrelSet4;
if( !drawBarrel ){
tmvaFileName1 = tmvaFileNameEndcap[0];
tmvaFileName2 = tmvaFileNameEndcap[1];
tmvaFileName3 = tmvaFileNameEndcap[2];
tmvaFileName4 = tmvaFileNameEndcap[3];
cutFileNamesSet1 = cutFileNamesEndcapSet1;
cutFileNamesSet2 = cutFileNamesEndcapSet2;
cutFileNamesSet3 = cutFileNamesEndcapSet3;
cutFileNamesSet4 = cutFileNamesEndcapSet4;
}
bazinga("Step #2");
// Open the file with TMVA output
TFile *tmvaFile1 = new TFile(tmvaFileName1);
TFile *tmvaFile2 = new TFile(tmvaFileName2);
TFile *tmvaFile3 = new TFile(tmvaFileName3);
TFile *tmvaFile4 = new TFile(tmvaFileName4);
if( !tmvaFile1 || !tmvaFile2 || !tmvaFile3 || !tmvaFile4)
assert(0);
bazinga("Step #3");
//
// Draw the ROC curve
//
TH1F *hROC1 = (TH1F*)tmvaFile1->Get("Method_Cuts/Cuts/MVA_Cuts_rejBvsS");
TH1F *hROC2 = (TH1F*)tmvaFile2->Get("Method_Cuts/Cuts/MVA_Cuts_rejBvsS");
TH1F *hROC3 = (TH1F*)tmvaFile3->Get("Method_Cuts/Cuts/MVA_Cuts_rejBvsS");
TH1F *hROC4 = (TH1F*)tmvaFile4->Get("Method_Cuts/Cuts/MVA_Cuts_rejBvsS");
if( !hROC1 || !hROC2 || !hROC3|| !hROC3 )
assert(0);
//c1->cd();
//hROC1->Draw();
bazinga("Step #4");
TH1F * hROC = new TH1F("hROC", "", 100, 0, 1);
hROC->SetDirectory(0);
for(int iBin = 1; iBin <=100; ++iBin){
if (drawBarrel){
// barrrel
if(1<=iBin && iBin<=75)
hROC->SetBinContent(iBin, hROC3->GetBinContent(iBin)) ;
else if(76 <= iBin && iBin<=89)
hROC->SetBinContent(iBin, hROC2->GetBinContent(iBin)) ;
else if(90 <=iBin && iBin<=100)
hROC->SetBinContent(iBin, hROC1->GetBinContent(iBin)) ;
}
else {
// for endcap
if(1<=iBin && iBin<=70)
hROC->SetBinContent(iBin, hROC3->GetBinContent(iBin)) ;
else if(71 <= iBin && iBin<=89)
hROC->SetBinContent(iBin, hROC2->GetBinContent(iBin)) ;
else if(90 <=iBin && iBin<=100)
hROC->SetBinContent(iBin, hROC1->GetBinContent(iBin)) ;
}
}
bazinga("Step #5");
//Set histogram attributes and draw the ROC curve
hROC->SetStats(0);
hROC->SetLineWidth(2);
hROC->SetTitle("");
hROC->GetXaxis()->SetTitle("signal efficiency");
hROC->GetYaxis()->SetTitle("background rejection");
hROC->GetYaxis()->SetTitleOffset(1.4);
if( drawBarrel ){
hROC->GetXaxis()->SetRangeUser(0.6, 1.0);
hROC->GetYaxis()->SetRangeUser(0.951, 1.0);
}else{
hROC->GetXaxis()->SetRangeUser(0.6, 1.0);
hROC->GetYaxis()->SetRangeUser(0.8, 1.0);
}
c1->cd();
// hROC->Draw("L");
hROC1->SetLineColor(kRed);
hROC2->SetLineColor(kOrange);
hROC3->SetLineColor(kBlue);
hROC4->SetLineColor(kGreen);
hROC1->GetYaxis()->SetRangeUser(0.9, 1.0);
hROC1->SetStats(0);
hROC1->Draw("L");
//hROC1->Draw("Lsame");
//hROC2->Draw("Lsame");
//hROC3->Draw("Lsame");
//hROC4->Draw("Lsame");
bazinga("Step #6");
TString commentText = "barrel electrons";
if( !drawBarrel )
commentText = "endcap electrons";
TLatex *comment = new TLatex(0.2, 0.2, commentText);
comment->SetNDC(kTRUE);
comment->Draw();
c1->Update();
bazinga("Step #7");
//
// Overlay the cuts
//
// First find the TestTree for measuring efficiency and rejection
printf("\n Take true electrons from %s tree %s\n",
fnameSignal.Data(), signalTreeName.Data());
TTree *signalTree = getTreeFromFile( fnameSignal, signalTreeName);
// Input background tree
printf("\n Take background electrons from %s tree %s\n",
fnameBackground.Data(), backgroundTreeName.Data());
TTree *backgroundTree = getTreeFromFile( fnameBackground, backgroundTreeName);
bazinga("Step #8");
// Next, draw all working point sets
if( nWorkingPointSets==4 ){
TLegend *leg = new TLegend(0.15, 0.45, 0.5, 0.7);
leg->SetFillStyle(0);
leg->SetBorderSize(0);
overlayWorkingPoints(c1, signalTree, backgroundTree, cutFileNamesSet1,
markerColorSet1, markerStyleSet1, leg, legendSet1);
// bazinga("Step #9");
// overlayWorkingPoints(c1, signalTree, backgroundTree, cutFileNamesSet2,
// markerColorSet2, markerStyleSet2, leg, legendSet2);
// bazinga("Step #10");
// overlayWorkingPoints(c1, signalTree, backgroundTree, cutFileNamesSet3,
// markerColorSet3, markerStyleSet3, leg, legendSet3);
// bazinga("Step #11");
// overlayWorkingPoints(c1, signalTree, backgroundTree, cutFileNamesSet4,
// markerColorSet4, markerStyleSet4, leg, legendSet4);
// bazinga("Step #12");
leg->Draw("same");
}
// Save the figure into a file
TString outname = "figures/plot_ROCandWP_barrel.png";
bazinga("Step #13");
if( !drawBarrel )
outname = "figures/plot_ROCandWP_endcap.png";
c1->Print(outname);
return;
};
// Get a given tree from a given file name.
TTree *getTreeFromFile(TString fname, TString tname){
TFile *file = new TFile( fname );
TTree *tree = (TTree*) file->Get(tname);
return tree;
}
// Draw on a given canvas the full set of working points
void overlayWorkingPoints(TCanvas *c1,
TTree *signalTree, TTree *backgroundTree,
const TString *cutFileNames,
int markerColor, int markerStyle,
TLegend *leg, const TString legendText){
// Now loop over working points
for(int iwp = 0; iwp<nWP; iwp++){
// Load the working point from a ROOT file
TFile *cutFile = new TFile(cutFileNames[iwp]);
if( !cutFile )
assert(0);
VarCut *cutObject = (VarCut*)cutFile->Get("cuts");
if( !cutObject )
assert(0);
// Compute the efficiencies
float effSignal, effBackground;
findEfficiencies(signalTree, backgroundTree, effSignal, effBackground,
cutObject);
printf("Computed eff for cut from %s, effS= %.3f effB= %.3f\n",
cutFileNames[iwp].Data(), effSignal, effBackground);
// Make a marker and draw it.
TMarker *marker = new TMarker(effSignal, 1.0-effBackground, 20);
marker->SetMarkerSize(2);
marker->SetMarkerColor(markerColor);
marker->SetMarkerStyle(markerStyle);
marker->Draw("same");
// Add marker to the legend only once. Do not draw the legend here,
// it is drawn in the main function later
if( iwp == 0 ){
if( !leg )
assert(0);
leg->AddEntry(marker, legendText, "p");
}
c1->Update();
cutFile->Close();
}
}
// Compute signal and background efficiencies for given cuts
void findEfficiencies(TTree *signalTree, TTree *backgroundTree,
float &effSignal, float &effBackground, VarCut *cutObject){
TCut etaCut = "";
if( drawBarrel ){
etaCut = Opt::etaCutBarrel;
}else{
etaCut = Opt::etaCutEndcap;
}
TCut kinematicCuts = Opt::ptCut && etaCut;
TCut preselectionCuts = kinematicCuts && Opt::otherPreselectionCuts;
TCut signalCuts = preselectionCuts && Opt::trueEleCut;
TCut backgroundCuts = preselectionCuts && Opt::fakeEleCut;
TCut selectionCuts = *(cutObject->getCut());
// printf("\nSelecton cuts:\n");
// selectionCuts.Print();
// printf("\nSignal cuts:\n");
// signalCuts.Print();
// printf("\nBackground cuts:\n");
// backgroundCuts.Print();
TH1F *hS_num = new TH1F("hS_num","",100,0.,10000.);
TH1F *hS_den = new TH1F("hS_den","",100,0.,10000.);
TH1F *hBG_num = new TH1F("hBG_num","",100,0.,10000.);
TH1F *hBG_den = new TH1F("hBG_den","",100,0.,10000.);
// draw the histogram
signalTree->Draw("pt>>hS_num", "genWeight"*(selectionCuts && signalCuts), "goff", 100000);
signalTree->Draw("pt>>hS_den", "genWeight"*(signalCuts), "goff", 100000);
effSignal = hS_num->GetSumOfWeights()/ hS_den->GetSumOfWeights();
// draw the histogram
backgroundTree->Draw("pt>>hBG_num", "genWeight"*(selectionCuts && backgroundCuts), "goff", 100000);
backgroundTree->Draw("pt>>hBG_den", "genWeight"*(backgroundCuts), "goff", 100000);
effBackground = hBG_num->GetSumOfWeights()/ hBG_den->GetSumOfWeights();
delete hS_num ; hS_num = nullptr;
delete hS_den ; hS_den = nullptr;
delete hBG_num ; hBG_num = nullptr;
delete hBG_den ; hBG_den = nullptr;
return;
}