forked from gitytakahas/Run3BparkingTriggerStudies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficiency.py
315 lines (198 loc) · 8.74 KB
/
efficiency.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
import copy, math, os
from numpy import array
#from CMGTools.H2TauTau.proto.plotter.categories_TauMu import cat_Inc
from ROOT import TFile, TH1F, TH2F, TTree, gROOT, gStyle, TCanvas, TColor, kLightTemperature, TGraphErrors, Double
from DisplayManager import DisplayManager, add_Preliminary, add_CMS, add_label
from officialStyle import officialStyle
from array import array
import numpy as np
gROOT.SetBatch(True)
officialStyle(gStyle)
gStyle.SetOptTitle(0)
gStyle.SetOptStat(0)
colours = [1, 2, 4, 6, 8, 13, 15]
styles = [1, 2, 4, 3, 5, 1, 1]
def calc(num, den):
eff = num/den
efferr = 0.
if num!=0:
efferr = math.sqrt(eff*(1-eff)/num)
# print num, den, eff, efferr
return eff, efferr
def set_palette(name="palette", ncontours=999):
"""Set a color palette from a given RGB list
stops, red, green and blue should all be lists of the same length
see set_decent_colors for an example"""
if name == "gray" or name == "grayscale":
stops = [0.00, 0.34, 0.61, 0.84, 1.00]
red = [1.00, 0.84, 0.61, 0.34, 0.00]
green = [1.00, 0.84, 0.61, 0.34, 0.00]
blue = [1.00, 0.84, 0.61, 0.34, 0.00]
# elif name == "whatever":
# (define more palettes)
else:
# default palette, looks cool
stops = [0.00, 0.34, 0.61, 0.84, 1.00]
red = [0.00, 0.00, 0.87, 1.00, 0.51]
green = [0.20, 0.2, 0.2, 0.2, 0.2]
# red = [0.1, 0.2, 0.3, 0.4, 0.5]
green = [0.1, 0.1, 0.1, 0.1, 0.1]
blue = [0.51, 1.00, 0.12, 0.00, 0.00]
# blue = [0.1, 0.2, 0.3, 0.4, 0.5]
s = array('d', stops)
r = array('d', red)
g = array('d', green)
b = array('d', blue)
npoints = len(s)
TColor.CreateGradientColorTable(npoints, s, r, g, b, ncontours)
gStyle.SetNumberContours(ncontours)
def applyHistStyle(h, i):
# print h, i
h.SetLineColor(colours[i])
h.SetMarkerColor(colours[i])
h.SetMarkerSize(0)
h.SetLineStyle(styles[i])
h.SetLineWidth(3)
h.SetStats(False)
def ensureDir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def comparisonPlots(hists, titles, isLog=False, pname='sync.pdf', isEff = False, isRatio=False, isLegend=False, x=None, y=None, label="test"):
display = DisplayManager(pname, isLog, isRatio, 0.6, 0.7, x, y, label)
display.draw_legend = isLegend
display.isEff = isEff
display.Draw(hists, titles)
def sproducer(key, rootfile, name, ivar, addsel = '1'):
hist = TH1F('h_' + key + '_' + name,
'h_' + key + '_' + name,
ivar['nbin'], ivar['xmin'], ivar['xmax'])
hist.Sumw2()
exp = '(' + ivar['sel'] + '&&' + addsel + ')'
tree = rootfile.Get(ivar['tree'])
# print ivar['var'] + ' >> ' + hist.GetName(), exp
tree.Draw(ivar['var'] + ' >> ' + hist.GetName(), exp)
hist.GetXaxis().SetTitle(ivar['title'])
hist.GetYaxis().SetTitle('a.u.')
hist.GetYaxis().SetNdivisions(506)
return copy.deepcopy(hist)
def effproducer(key, rootfile, name, ivar, addsel='1'):
hist = TH2F('h_' + key + '_' + name,
'h_' + key + '_' + name,
ivar['xnbin'], ivar['xmin'], ivar['xmax'],
ivar['ynbin'], ivar['ymin'], ivar['ymax'])
hist.Sumw2()
exp = '(' + ivar['sel'] + '&&' + addsel + ')'
tree = rootfile.Get(ivar['tree'])
# print ivar['var'], 'effstr = ', effstr + ' >> ' + hist.GetName(), exp
tree.Draw(ivar['yvar'] + ':' + ivar['xvar'] + ' >> ' + hist.GetName(), exp)
hprof = hist.ProfileX()
hprof.SetMaximum(ivar['ymax'])
# hprof.SetMaximum(1.2)
hprof.SetMinimum(ivar['ymin'])
hprof.GetXaxis().SetTitle(ivar['xtitle'])
hprof.GetYaxis().SetTitle(ivar['ytitle'])
hprof.GetYaxis().SetNdivisions(506)
if name.find('eff')!=-1:
hprof.SetMaximum(1.)
hprof.SetMinimum(0)
if name.find('res')!=-1:
# print '!!!!!!!!!!!!!!!!!!!!!'
hprof.SetMaximum(1.)
hprof.SetMinimum(-1.)
return copy.deepcopy(hprof), copy.deepcopy(hist)
xtit = "Generator-level electron p_{T} [GeV]"
xtit_b = "Generator-level B p_{T} [GeV]"
effvardict = {
# 'l1e_match_eff':{'tree':'tree', 'xvar':'gen_ept', 'yvar':'l1_mindr < 0.4', 'xnbin':25, 'xmin':0, 'xmax':25, 'ynbin':2, 'ymin':-0.5, 'ymax':1.5, 'xtitle':xtit, 'ytitle':"Matching eff.", 'sel':'1'},
# 'l1e_dr':{'tree':'pairtree', 'xvar':'gen_bpt', 'yvar':'l1e_dr', 'xnbin':25, 'xmin':0, 'xmax':50, 'ynbin':30, 'ymin':0., 'ymax':5, 'xtitle':xtit_b, 'ytitle':"#DeltaR(l1e, l1e)", 'sel':'l1e_dr!=9'},
# 'mu':{'tree':'tree', 'xvar':None, 'yvar':'l1e_dr', 'xnbin':25, 'xmin':0, 'xmax':50, 'ynbin':30, 'ymin':0., 'ymax':5, 'xtitle':xtit_b, 'ytitle':"#DeltaR(l1e, l1e)", 'sel':'l1e_dr!=9'},
}
#vardict = {
# 'gen_ept':{'tree':'tree', 'var':'gen_ept', 'nbin':30, 'xmin':0, 'xmax':20, 'title':xtit, 'sel':'1'},
# 'l1_mindr':{'tree':'tree', 'var':'l1_mindr', 'nbin':30, 'xmin':0, 'xmax':10, 'title':'min. #Delta R (gen, L1)', 'sel':'1'},
# 'res_ept':{'tree':'tree', 'var':'gen_ept - l1_pt', 'nbin':30, 'xmin':-10, 'xmax':10, 'title':'#Delta p_{T} (gen, L1)', 'sel':'l1_mindr < 0.4'},
# 'res_eeta':{'tree':'tree', 'var':'gen_eeta - l1_eta', 'nbin':30, 'xmin':-0.2, 'xmax':0.2, 'title':'#Delta #eta (gen, L1)', 'sel':'l1_mindr < 0.4'},
# 'res_ephi':{'tree':'tree', 'var':'gen_ephi - l1_phi', 'nbin':30, 'xmin':-1, 'xmax':1, 'title':'#Delta #phi (gen, L1)', 'sel':'l1_mindr < 0.4'},
#
# 'gen_bpt':{'tree':'pairtree', 'var':'gen_bpt', 'nbin':30, 'xmin':0, 'xmax':30, 'title':xtit_b, 'sel':'1'},
# 'l1e_dr':{'tree':'pairtree', 'var':'l1e_dr', 'nbin':30, 'xmin':0, 'xmax':6, 'title':'#Delta R(l1e, l1e)', 'sel':'l1e_dr !=9'},
# }
ensureDir('plots/')
sfile = TFile('genstudy_signal_1285files_303513events.root')
tree = sfile.Get('tree')
#for vkey, ivar in vardict.iteritems():
#
# hists = []
# titles = []
#
# addsel = '1'
#
# hist = sproducer('hist', sfile, vkey, ivar, addsel)
#
# hists.append(copy.deepcopy(hist))
# titles.append('Kee inclusive Sig.')
#
# for ii, ihist in enumerate(hists):
# applyHistStyle(ihist, ii)
#
# ihist.Scale(1./ihist.GetSumOfWeights())
# ihist.SetMaximum(ihist.GetBinContent(ihist.GetMaximumBin())*1.2)
#
# comparisonPlots(hists, titles, False, 'plots/dist_' + vkey + '.pdf', False, False, False)
#for vkey, ivar in effvardict.iteritems():
# efficiecny to find muon with pT > X GeV
hists = []
titles = []
ptrange = np.arange(0, 12.1, 0.2).tolist()
graph_singleMu = TGraphErrors()
graph_singleMu.SetName('singleMuon')
graph_singleMu.SetTitle('singleMuon')
den = Double(tree.GetEntries())
for ipt, pt in enumerate(ptrange):
num = Double(tree.GetEntries('mu1_pt > ' + str(pt)))
# print num, den
eff, efferr = calc(num, den)
print 'singleMu (ipt, pt) = ', ipt, pt, ', (num, den, eff, efferr) = ', num, den, eff, efferr
# eff = num/den
# efferr = 0.
# if num!=0:
# efferr = math.sqrt(eff*(1-eff)/num)
graph_singleMu.SetPoint(ipt, pt, eff)
graph_singleMu.SetPointError(ipt, 0, efferr)
graphs_MuE = []
for ipt_mu, pt_mu in enumerate([3,4,5,6,7,8,9]):
graph_MuE = TGraphErrors()
graph_MuE.SetTitle('Mu' + str(pt_mu))
graph_MuE.SetName('Mu' + str(pt_mu))
for ipt_e, pt_e in enumerate([3,4,5,6,7,8,9]):
num = Double(tree.GetEntries('mu1_pt > ' + str(pt_mu) + ' && e1_pt > ' + str(pt_e)))
eff, efferr = calc(num, den)
graph_MuE.SetPoint(ipt_e, pt_e, eff)
graph_MuE.SetPointError(ipt_e, 0, efferr)
print 'MuE (ipt_mu, pt_mu, ipt_e, pt_e) = ', ipt_mu, pt_mu, ipt_e, pt_e, ', (num, den, eff, efferr) = ', num, den, eff, efferr
graphs_MuE.append(graph_MuE)
graphs_EE = []
for ipt_e1, pt_e1 in enumerate([3,4,5,6,7,8,9]):
graph_EE = TGraphErrors()
graph_EE.SetTitle('E' + str(pt_e1))
graph_EE.SetName('E' + str(pt_e1))
for ipt_e2, pt_e2 in enumerate([3,4,5,6,7,8,9]):
if ipt_e2 > ipt_e1: continue
num = Double(tree.GetEntries('e1_pt > ' + str(pt_e1) + ' && e2_pt > ' + str(pt_e2) ) )
eff, efferr = calc(num, den)
graph_EE.SetPoint(ipt_e2, pt_e2, eff)
graph_EE.SetPointError(ipt_e2, 0, efferr)
print 'EE (ipt_e1, pt_e1, ipt_e2, pt_e2) = ', ipt_e1, pt_e1, ipt_e2, pt_e2, ', (num, den, eff, efferr) = ', num, den, eff, efferr
graphs_EE.append(graph_EE)
out = TFile('plot.root', 'recreate')
graph_singleMu.Write()
for graph in graphs_MuE:
graph.Write()
for graph in graphs_EE:
graph.Write()
out.Write()
out.Close()
#hist, hs = effproducer('hist', sfile, vkey, ivar, addsel)
#hists.append(copy.deepcopy(hist))
#titles.append('Kee inclusive Sig.')
#comparisonPlots(hists, titles, False, 'plots/eff_' + vkey + '.pdf', True, False, False)