This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TWTrigger.py
executable file
·494 lines (367 loc) · 13.1 KB
/
TWTrigger.py
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#! /usr/bin/env python
###################################################################
## ##
## Name: TWrate.py ##
## Author: Kevin Nash ##
## Date: 5/28/2015 ##
## Purpose: This program creates the numerator and denominator ##
## used by TWTrigger_Maker.py to create trigger ##
## Efficiency curves. ##
## ##
###################################################################
import os
import glob
import math
from math import sqrt,exp
import ROOT
from ROOT import std,ROOT,TFile,TLorentzVector,TMath,gROOT, TF1,TH1F,TH1D,TH2F,TH2D
from ROOT import TVector
from ROOT import TFormula
import sys
from DataFormats.FWLite import Events, Handle
from optparse import OptionParser
from array import *
parser = OptionParser()
parser.add_option('-s', '--set', metavar='F', type='string', action='store',
default = 'data',
dest = 'set',
help = 'dataset (ie data,ttbar etc)')
parser.add_option('-u', '--ptreweight', metavar='F', type='string', action='store',
default = 'none',
dest = 'ptreweight',
help = 'on or off')
parser.add_option('-n', '--num', metavar='F', type='string', action='store',
default = 'all',
dest = 'num',
help = 'job number')
parser.add_option('-j', '--jobs', metavar='F', type='string', action='store',
default = '1',
dest = 'jobs',
help = 'number of jobs')
parser.add_option('-g', '--grid', metavar='F', type='string', action='store',
default = 'off',
dest = 'grid',
help = 'running on grid off or on')
parser.add_option('-S', '--split', metavar='F', type='string', action='store',
default = 'file',
dest = 'split',
help = 'split by event of file')
parser.add_option('-b', '--bx', metavar='F', type='string', action='store',
default = '25ns',
dest = 'bx',
help = 'bunch crossing 50ns or 25ns')
parser.add_option('-t', '--tname', metavar='F', type='string', action='store',
default = 'HLT_PFHT900,HLT_AK8PFJet450', #,'HLT_PFHT800_v2',#
dest = 'tname',
help = 'trigger name')
parser.add_option('-p', '--pretname', metavar='F', type='string', action='store',
default = 'HLT_PFHT475_v3',#'NONE',
dest = 'pretname',
help = 'prescaled trigger name')
(options, args) = parser.parse_args()
#If running on the grid we access the script within a tarred directory
di = ""
if options.grid == 'on':
di = "tardir/"
sys.path.insert(0, 'tardir/')
gROOT.Macro(di+"rootlogon.C")
import Bstar_Functions
from Bstar_Functions import *
tname = options.tname.split(',')
tnamestr = ''
Tstr = [options.pretname]
for iname in range(0,len(tname)):
tnamestr+=tname[iname]
if iname!=len(tname)-1:
tnamestr+='OR'
Tstr.append(tname[iname])
print Tstr
#Load up cut values based on what selection we want to run
Cuts = LoadCuts("default")
wpt = Cuts['wpt']
tpt = Cuts['tpt']
dy = Cuts['dy']
tmass = Cuts['tmass']
sjbtag = Cuts['sjbtag']
tau32 = Cuts['tau32']
wmass = Cuts['wmass']
eta1 = Cuts['eta1']
eta2 = Cuts['eta2']
print "Options summary"
print "=================="
for opt,value in options.__dict__.items():
#print str(option)+ ": " + str(options[option])
print str(opt) +': '+ str(value)
print "=================="
print ""
#For large datasets we need to parallelize the processing
jobs=int(options.jobs)
if jobs != 1:
num=int(options.num)
jobs=int(options.jobs)
print "Running over " +str(jobs)+ " jobs"
print "This will process job " +str(num)
else:
print "Running over all events"
#Based on what set we want to analyze, we find all Ntuple root files
files = Load_Ntuples(options.set,di)
jobiter = 0
# We select all the events:
splitfiles = []
#For event counting
jobiter = 0
splitfiles = []
if jobs != 1 and options.split=="file":
for ifile in range(1,len(files)+1):
if (ifile-1) % jobs == 0:
jobiter+=1
count_index = ifile - (jobiter-1)*jobs
if count_index==num:
splitfiles.append(files[ifile-1])
events = Events(splitfiles)
runs = Runs(splitfiles)
if options.split=="event" or jobs == 1:
events = Events(files)
runs = Runs(files)
nevHandle = Handle ( "vector<int> " )
nevLabel = ( "counter" , "nevr")
totnev = 0
for run in runs:
run.getByLabel (nevLabel,nevHandle )
nev = nevHandle.product()
totnev+=nev[0]
print "Total unfiltered events in selection: ",totnev
# if jobs != 1 and options.split=="file":
# for ifile in range(1,len(files)+1):
# if (ifile-1) % jobs == 0:
# jobiter+=1
# count_index = ifile - (jobiter-1)*jobs
# if count_index==num:
# splitfiles.append(files[ifile-1])
# events = Events (splitfiles)
# if options.split=="event" or jobs == 1:
# events = Events (files)
#events = ChainEvent(files)
#Here we load up handles and labels.
#These are used to grab entries from the Ntuples.
#To see all the current types in an Ntuple use edmDumpEventContent /PathtoNtuple/Ntuple.root
AK8HL = Initlv("jetsAK8")
GeneratorHandle = Handle ( "GenEventInfoProduct")
GeneratorLabel = ( "generator" , "")
puHandle = Handle("int")
puLabel = ( "eventUserData", "puNtrueInt" )
softDropPuppiMassHandle = Handle ( "vector<float> " )
softDropPuppiMassLabel = ( "jetsAK8" , "jetAK8PuppiCorrectedsoftDropMass")
vsubjets0indexHandle = Handle ( "vector<float> " )
vsubjets0indexLabel = ( "jetsAK8" , "jetAK8PuppivSubjetIndex0")
vsubjets1indexHandle = Handle ( "vector<float> " )
vsubjets1indexLabel = ( "jetsAK8" , "jetAK8PuppivSubjetIndex1")
subjetsAK8CSVHandle = Handle ( "vector<float> " )
subjetsAK8CSVLabel = ( "subjetsAK8Puppi" , "subjetAK8PuppiCSVv2")
tau1Handle = Handle ( "vector<float> " )
tau1Label = ( "jetsAK8" , "jetAK8Puppitau1")
tau2Handle = Handle ( "vector<float> " )
tau2Label = ( "jetsAK8" , "jetAK8Puppitau2")
tau3Handle = Handle ( "vector<float> " )
tau3Label = ( "jetsAK8" , "jetAK8Puppitau3")
HT475Handle = Handle ( "vector<bool>" )
HT475Label = ( "Filter" , "HT475bit" )
HT800Handle = Handle ( "vector<bool>" )
HT800Label = ( "Filter" , "HT800bit" )
HT900Handle = Handle ( "vector<bool>" )
HT900Label = ("Filter", "HT900bit")
JET450Handle = Handle ( "vector<bool>" )
JET450Label = ("Filter","JET450bit")
#---------------------------------------------------------------------------------------------------------------------#
#Create the output file
if jobs != 1:
f = TFile( "TWTrigger"+options.set+tnamestr+"_pre_"+options.pretname+"_job"+options.num+"of"+options.jobs+".root", "recreate" )
else:
f = TFile( "triggerstudies/TWTrigger"+options.set+tnamestr+"_pre_"+options.pretname+".root", "recreate" )
print "Creating histograms"
#Define Histograms
f.cd()
nev = TH1F("nev", "nev", 1, 0, 1 )
nev.SetBinContent(1,totnev)
#---------------------------------------------------------------------------------------------------------------------#
Htpreuntrig = TH1D("Htpreuntrig", "", 400, 0, 4000 )
Htuntrig = TH1D("Htuntrig", "", 400, 0, 4000 )
Htpreuntrig.Sumw2()
Htuntrig.Sumw2()
Htpre = TH1D("Htpre", "", 400, 0, 4000 )
Ht = TH1D("Ht", "", 400, 0, 4000 )
Htpre.Sumw2()
Ht.Sumw2()
Htprettags = TH1D("Htprettags", "", 400, 0, 4000 )
Htttags = TH1D("Htttags", "", 400, 0, 4000 )
Htprettags.Sumw2()
Htttags.Sumw2()
Ptpre = TH1D("Ptpre", "", 200, 0, 2000 )
Pt = TH1D("Pt", "", 200, 0, 2000 )
Ptpre.Sumw2()
Pt.Sumw2()
Mpre = TH1D("Mpre", "", 100, 0, 200 )
M = TH1D("M", "", 100, 0, 200 )
Mpre.Sumw2()
M.Sumw2()
#---------------------------------------------------------------------------------------------------------------------#
# loop over events
#---------------------------------------------------------------------------------------------------------------------#
count = 0
print "Start looping"
#initialize the ttree variables
totevents = events.size()
print str(totevents) + ' Events total'
# Setup some pre-loop variables
trigdict = {} # Filled with the trigger names as keys and trigger bits as values
prestrig = options.pretname # defaults to NONE and nothing happens
noPassCount = 0
for event in events:
count = count + 1
if count % 100000 == 0 :
print '--------- Processing Event ' + str(count) +' -- percent complete ' + str(100*count/totevents) + '% -- '
# Grab bits for event
event.getByLabel(HT475Label, HT475Handle)
HT475bit = HT475Handle.product()
event.getByLabel(HT800Label, HT800Handle)
HT800bit = HT800Handle.product()
event.getByLabel (HT900Label, HT900Handle)
HT900bit = HT900Handle.product()
event.getByLabel (JET450Label, JET450Handle)
JET450bit = JET450Handle.product()
# Store bits in list
trigdict['HLT_PFHT475_v3'] = HT475bit
trigdict['HLT_PFHT800_v2'] = HT800bit
trigdict['HLT_PFHT900'] = HT900bit
trigdict['HLT_AK8PFJet450'] = JET450bit
AK8LV = Makelv(AK8HL,event)
if len(AK8LV)==0:
continue
tindex,windex = Hemispherize(AK8LV,AK8LV)
# if jobs != 1 and options.split=="event":
# if (count - 1) % jobs == 0:
# jobiter+=1
# count_index = count - (jobiter-1)*jobs
# if count_index!=num:
# continue
wJetsh1 = []
wJetsh0 = []
topJetsh1 = []
topJetsh0 = []
for i in range(0,len(windex[1])):
wJetsh1.append(AK8LV[windex[1][i]])
for i in range(0,len(windex[0])):
wJetsh0.append(AK8LV[windex[0][i]])
for i in range(0,len(tindex[1])):
topJetsh1.append(AK8LV[tindex[1][i]])
for i in range(0,len(tindex[0])):
topJetsh0.append(AK8LV[tindex[0][i]])
wjh0 = 0
wjh1 = 0
#Require 1 pt>150 jet in each hemisphere (top jets already have the 150GeV requirement)
for wjet in wJetsh0:
if wjet.Perp() > 400.0:
wjh0+=1
for wjet in wJetsh1:
if wjet.Perp() > 400.0:
wjh1+=1
njets11w0 = ((len(topJetsh1) == 1) and (wjh0 == 1))
njets11w1 = ((len(topJetsh0) == 1) and (wjh1 == 1))
#We consider both the case that the b is the leading (highest pt) jet (hemis0) and the case where the top is the leading jet (hemis1)
for hemis in ['hemis0']:#,'hemis1']:
if hemis == 'hemis0' :
if not njets11w0:
continue
#The Ntuple entries are ordered in pt, so [0] is the highest pt entry
#We are calling a candidate b jet (highest pt jet in hemisphere0)
tindexval = tindex[1][0]
windexval = windex[0][0]
wjet = wJetsh0[0]
tjet = topJetsh1[0]
if hemis == 'hemis1' :
if not njets11w1:
continue
tindexval = tindex[0][0]
windexval = windex[1][0]
wjet = wJetsh1[0]
tjet = topJetsh0[0]
HT = tjet.Perp() + wjet.Perp()
PT = tjet.Perp()
event.getByLabel (softDropPuppiMassLabel, softDropPuppiMassHandle)
puppiJetMass = softDropPuppiMassHandle.product()
MA = puppiJetMass[tindexval]
tmass_cut = tmass[0]<puppiJetMass[tindexval]<tmass[1]
wmass_cut = wmass[0]<=puppiJetMass[windexval]<wmass[1]
#event.getByLabel (TpsLabel, TpsHandle)
#Tps = TpsHandle.product()
TRIGBOOL = []
presTRIGBOOL = False
for t in trigdict.keys():
# If the trigger is the prescale trigger, save a trigger bit other than FALSE
if t==prestrig:
presTRIGBOOL = trigdict[t]
# print "-----FOUND-----"
# print "prescale " + str(t)
# print "-----FOUND-----"
# Add it to the list of trigger booleans
elif t in Tstr:
TRIGBOOL.append(trigdict[t])
# If there's no prescale trigger, make the bool true
if options.pretname == 'NONE':
presTRIGBOOL = True
# Initialize...
TPASS = False
# If the event passes any of the triggers, pass the event and stop checking if it passes others (break for loop)
for TB in TRIGBOOL:
if TB:
#TPASS = True
break
else:
print "trigger failed"
raw_input('continue')
# Fill HT histo for all events
Htpreuntrig.Fill(HT)
if TPASS:
# and another for events that pass one trigger
Htuntrig.Fill(HT)
# If we don't pass the prescale trigger, go on to the next event
if not presTRIGBOOL:
continue
# Fill HT, Pt, and Mass distributions, pre pass but DO pass prescale
Htpre.Fill(HT)
Ptpre.Fill(PT)
Mpre.Fill(MA)
# Fill HT, Pt, and Mass distributions, post pass but DO pass prescale
if TPASS:
Ht.Fill(HT)
Pt.Fill(PT)
M.Fill(MA)
event.getByLabel (vsubjets0indexLabel,vsubjets0indexHandle )
vsubjets0index = vsubjets0indexHandle.product()
event.getByLabel (vsubjets1indexLabel,vsubjets1indexHandle )
vsubjets1index = vsubjets1indexHandle.product()
event.getByLabel (subjetsAK8CSVLabel,subjetsAK8CSVHandle )
subjetsAK8CSV = subjetsAK8CSVHandle.product()
if len(subjetsAK8CSV)==0:
continue
if len(subjetsAK8CSV)<2:
subjetsAK8CSV[int(vsubjets0index[tindexval])]
else:
SJ_csvvals = [subjetsAK8CSV[int(vsubjets0index[tindexval])],subjetsAK8CSV[int(vsubjets1index[tindexval])]]
SJ_csvmax = max(SJ_csvvals)
sjbtag_cut = sjbtag[0]<SJ_csvmax<=sjbtag[1]
# If we pass top tagging
if sjbtag_cut and tmass_cut:
Htprettags.Fill(HT)
if TPASS:
Htttags.Fill(HT)
f.cd()
f.Write()
f.Close()
# for key in trigdict.keys():
# print key
# print trigdict[key]
# print str(trigdict[key]/count)+'%'
# print
print "Trigger failed: " + str(noPassCount)
print "number of events: " + str(count)