-
Notifications
You must be signed in to change notification settings - Fork 4
/
plot_all.py
685 lines (602 loc) · 28.8 KB
/
plot_all.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
from tqdm import tqdm
import json
import os
import numpy as np
import itertools
import uproot
import re
from analysis.utils.plotting_utils import write_table
import sys
sys.path.insert(0,'/home/users/{}/.local/lib/python2.7/site-packages/'.format(os.getenv("USER")))
from matplottery.plotter import plot_stack
from matplottery.utils import Hist1D, MET_LATEX, binomial_obs_z
labels = {}
labels["ft"] = {
"TOTAL" : [("SRCR","SR","SRDISC"), "Region"],
"ht" : [("ttzcr","ttwcr","sr","br","brpostfit"),r"$H_\mathrm{T}$ (GeV)"],
"met" : [("ttzcr","ttwcr","sr","br","brpostfit"), r"$p_\mathrm{T}^{\mathrm{miss}}$ (GeV)"],
"mtmin" : [("ttzcr","ttwcr","sr","br","brpostfit"), r"$m_\mathrm{T}^{\mathrm{min}}$"],
"mll" : [("ttzcr","ttwcr","sr","br","brpostfit"), r"$m_{ll}$"],
"njets" : [("ttzcr","ttwcr","sr","br","brpostfit"), r"$N_\mathrm{jets}$"],
"nbtags" : [("ttzcr","ttwcr","sr","br","brpostfit"),r"$N_\mathrm{b}$"],
"type" : [("ttzcr","ttwcr","sr","br","brpostfit"), "type (mm, em, ee)"],
"type3" : [("ttzcr","ttwcr","sr","br","brpostfit"), "type3 (mmm, mme, mee, eee)"],
"charge" : [("ttzcr","ttwcr","sr","br","brpostfit"), "charge"],
"nleps" : [("ttzcr","ttwcr","sr","br","brpostfit"), "Nlep"],
"l1pt" : [("ttzcr","ttwcr","sr","br","brpostfit"), "l1pt"],
"l2pt" : [("ttzcr","ttwcr","sr","br","brpostfit"), "l2pt"],
"l3pt" : [("ttzcr","ttwcr","sr","br","brpostfit"), "l3pt"],
"disc" : [("br","brpostfit","ttwcr","ttzcr"), "disc"],
"l3pt" : [("ttzcr","sr","br","brpostfit"), "l3pt"],
"mllos" : [("ttzcr",), "mllos"],
"dphil1l2": [("br","brpostfit"), r"$\Delta\phi(l_1,l_2)$"],
"htb": [("br","brpostfit"), r"$H_{T}$(b-jets)"],
"nlb40": [("br","brpostfit"), r"N-loose b-tags, $p_{T}>40$"],
"ntb40": [("br","brpostfit"), r"N-tight b-tags, $p_{T}>40$"],
"detal1l2": [("br","brpostfit"), r"$\Delta\eta(l_1,l_2)$"],
"maxmjoverpt": [("br","brpostfit"), r"max($m_j/p_T$)"],
"ml1j1": [("br","brpostfit"), r"m$(l_1,j_2)$"],
"ptj1": [("br","brpostfit"), "$p_T$ - jet 1"],
"ptj6": [("br","brpostfit"), "$p_T$ - jet 6"],
"ptj7": [("br","brpostfit"), "$p_T$ - jet 7"],
"ptj8": [("br","brpostfit"), "$p_T$ - jet 8"],
# "el_l1pt" : [("sr","br"), "el_l1pt"],
# "el_l2pt" : [("sr","br"), "el_l2pt"],
# "el_l1eta" : [("sr","br"), "el_l1eta"],
# "el_l2eta" : [("sr","br"), "el_l2eta"],
# "el_l1phi" : [("sr","br"), "el_l1phi"],
# "el_l2phi" : [("sr","br"), "el_l2phi"],
# "mu_l1pt" : [("sr","br"), "mu_l1pt"],
# "mu_l2pt" : [("sr","br"), "mu_l2pt"],
# "mu_l1eta" : [("sr","br"), "mu_l1eta"],
# "mu_l2eta" : [("sr","br"), "mu_l2eta"],
# "mu_l1phi" : [("sr","br"), "mu_l1phi"],
# "mu_l2phi" : [("sr","br"), "mu_l2phi"],
# "lepsf" : [("br",), "lepsf"],
# "btagsf" : [("br",), "btagsf"],
# "trigsf" : [("br",), "trigsf"],
# "isrw" : [("br",), "isrw"],
# "puw" : [("br",), "puw"],
# "wsf" : [("br",), "wsf"],
# "mvis" : [("ttzcr","ttwcr","sr","br"), "mvis"],
# "mtvis" : [("ttzcr","ttwcr","sr","br"), "mtvis"],
# "mtop1" : [("sr",), "mtop1"],
# "mtop2" : [("sr",), "mtop2"],
# "sip3d_mu_lep1" : [("sr","br"), "sip3d_mu_lep1"],
# "sip3d_mu_lep2" : [("sr","br"), "sip3d_mu_lep2"],
# "lep1_mu_miniIso" : [("sr","br"), "lep1_mu_miniIso"],
# "lep2_mu_miniIso" : [("sr","br"), "lep2_mu_miniIso"],
# "lep1_mu_ptRel" : [("sr","br"), "lep1_mu_ptRel"],
# "lep2_mu_ptRel" : [("sr","br"), "lep2_mu_ptRel"],
# "lep1_mu_ptRelfail" : [("sr","br"), "lep1_mu_ptRelfail"],
# "lep2_mu_ptRelfail" : [("sr","br"), "lep2_mu_ptRelfail"],
# "lep1_mu_ptRatio" : [("sr","br"), "lep1_mu_ptRatio"],
# "lep2_mu_ptRatio" : [("sr","br"), "lep2_mu_ptRatio"],
# "sip3d_el_lep1" : [("sr","br"), "sip3d_el_lep1"],
# "sip3d_el_lep2" : [("sr","br"), "sip3d_el_lep2"],
# "el_l1pt" : [("sr","br"), "el_l1pt"],
# "el_l2pt" : [("sr","br"), "el_l2pt"],
# "el_l3pt" : [("sr","br"), "el_l3pt"],
# "l1eta_el" : [("sr","br"), "l1eta_el"],
# "l2eta_el" : [("sr","br"), "l2eta_el"],
# "lep1_el_miniIso" : [("sr","br"), "lep1_el_miniIso"],
# "lep2_el_miniIso" : [("sr","br"), "lep2_el_miniIso"],
# "lep1_el_ptRel" : [("sr","br"), "lep1_el_ptRel"],
# "lep2_el_ptRel" : [("sr","br"), "lep2_el_ptRel"],
# "lep1_el_ptRelfail" : [("sr","br"), "lep1_el_ptRelfail"],
# "lep2_el_ptRelfail" : [("sr","br"), "lep2_el_ptRelfail"],
# "lep1_el_ptRatio" : [("sr","br"), "lep1_el_ptRatio"],
# "lep2_el_ptRatio" : [("sr","br"), "lep2_el_ptRatio"],
# "bjetpt" : [("sr","br"), "bjetpt"],
# "jetpt" : [("sr","br"), "jetpt"],
}
def remove(rs1,rs2):
return list(set(rs1)-set(rs2))
ssregions = ("ssbr","br","ml","mlonz","mloffz","hh","lm")
ssregions_ll = ("ssbr","br","ml","mlonz","mloffz","hh","lm","ll")
# ssregions = ("lm",)
labels["ss"] = {
# "TOTAL" : [("SRHH","SRHL","SRLL","SRML","SRLM"), "Region"],
# "ht" : [("br",), r"$H_\mathrm{T}$ (GeV)"],
# "met" : [("br",), r"$p_\mathrm{T}^{\mathrm{miss}}$ (GeV)"],
# "mtmin" : [("br",), r"$m_\mathrm{T}^\mathrm{min}$"],
# "njets" : [("br",), r"$N_\mathrm{jets}$"],
# "nbtags" : [("br",), r"$N_\mathrm{b}$"],
# "charge" : [("br",), r"SS charge"],
"TOTAL" : [("SRHH","SRHL","SRLL","SRML","SRLM"), "Region"],
"category" : [("sr",), r"HH,HL,LL,MLoffZ,MLonZ,LM"],
"mtmin" : [ssregions_ll, r"$m_\mathrm{T}^\mathrm{min}$ (GeV)"],
"ht" : [ssregions_ll, r"$H_\mathrm{T}$ (GeV)"],
"njets" : [ssregions, r"$N_\mathrm{jets}$"],
"met" : [ssregions_ll, r"$p_\mathrm{T}^{\mathrm{miss}}$ (GeV)"],
"mll" : [ssregions, r"$m_{ll}$"],
"mllbig" : [ssregions, r"$m_{ll}$"],
"mllos" : [ssregions, r"$m_{ll}$(OS)"],
"nbtags" : [ssregions, r"$N_\mathrm{b}$"],
"type" : [ssregions, r"$\mu\mu,\mu e,e\mu,ee$"],
"charge" : [ssregions, r"SS charge"],
"el_charge" : [ssregions, r"SS el charge"],
"mu_charge" : [ssregions, r"SS mu charge"],
"dphi" : [ssregions, r"dphi(l1,l2)"],
"nleps" : [ssregions, r"$N_\mathrm{leps}$"],
"l1pt" : [ssregions, r"ordered $p_{T}$(lep1)"],
"l2pt" : [ssregions, r"ordered $p_{T}$(lep2)"],
"el_l1pt" : [ssregions, r"unsorted $p_{T}$(lep1, e)"],
"el_l2pt" : [ssregions, r"unsorted $p_{T}$(lep2, e)"],
"el_l1eta" : [ssregions, r"unsorted $\eta $(lep1, e)"],
"el_l2eta" : [ssregions, r"unsorted $\eta $(lep2, e)"],
"mu_l1pt" : [ssregions, r"unsorted $p_{T}$(lep1, $\mu$)"],
"mu_l2pt" : [ssregions, r"unsorted $p_{T}$(lep2, $\mu$)"],
"mu_l1eta" : [ssregions, r"unsorted $\eta $(lep1, $\mu$)"],
"mu_l2eta" : [ssregions, r"unsorted $\eta $(lep2, $\mu$)"],
"el_l1phi" : [ssregions, "el_l1phi"],
"el_l2phi" : [ssregions, "el_l2phi"],
"mu_l1phi" : [ssregions, "mu_l1phi"],
"mu_l2phi" : [ssregions, "mu_l2phi"],
"nvtx" : [ssregions, r"nvtx"],
"lumiblock" : [ssregions, "lumiblock"],
"run" : [ssregions, "run"],
"class" : [ssregions, r"hypclass"],
"l3pt" : [ssregions, r"ordered $p_{T}$(lep3)"],
"type3" : [remove(ssregions,("lm",)), r"$\mu\mu\mu,\mu\mu e,\mu ee,eee$"],
"q3" : [remove(ssregions,("lm",)), r"lep 3 charge"],
"charge3" : [remove(ssregions,("lm",)), r"$\pm\pm\pm$, $\pm\pm\mp$"],
"el_l3pt" : [remove(ssregions,("lm",)), r"unsorted $p_{T}$(lep3, e)"],
"el_l3eta" : [remove(ssregions,("lm",)), r"unsorted $\eta $(lep3, e)"],
"mu_l3pt" : [remove(ssregions,("lm",)), r"unsorted $p_{T}$(lep3, $\mu$)"],
"mu_l3eta" : [remove(ssregions,("lm",)), r"unsorted $\eta $(lep3, $\mu$)"],
}
do_paper_plots_only = False
if do_paper_plots_only:
labels["ft"] = {
"ht" : [("ttzcr","ttwcr","sr"),r"$H_\mathrm{T}$ (GeV)"],
"met" : [("ttzcr","ttwcr","sr"), r"$p_\mathrm{T}^{\mathrm{miss}}$ (GeV)"],
"njets" : [("ttzcr","ttwcr","sr"), r"$N_\mathrm{jets}$"],
"nbtags" : [("ttzcr","ttwcr","sr"),r"$N_\mathrm{b}$"],
}
labels["ss"] = {
# "TOTAL" : [("SRHH","SRHL","SRLL","SRML","SRLM"), "Region"],
"ht" : [("br",), r"$H_\mathrm{T}$ (GeV)"],
"met" : [("br",), r"$p_\mathrm{T}^{\mathrm{miss}}$ (GeV)"],
"mtmin" : [("br",), r"$m_\mathrm{T}^\mathrm{min}$ (GeV)"],
"njets" : [("br",), r"$N_\mathrm{jets}$"],
"nbtags" : [("br",), r"$N_\mathrm{b}$"],
"charge" : [("br",), r"SS charge"],
}
d_label_colors = {}
d_label_colors["ft"] = {
"fakes" : (r"Nonprompt lep.", [0.85, 0.85, 0.85]),
# "fakes_mc" : (r"MC fakes", [0.85, 0.85, 0.85]),
"flips" : (r"Charge misid.", [0.4, 0.4, 0.4]),
"rares" : (r"Rare", [1.0, 0.4, 1.0]),
"tth" : (r"$t\bar{t}H$", [0.4, 0.4, 0.6]),
"ttvv" : (r"$t\bar{t}VV$" , [0.4, 0.6, 1.0]),
"ttw" : (r"$t\bar{t}W$", [0.0, 0.4, 0.0]),
"ttz" : (r"$t\bar{t}Z$", [0.4, 0.8, 0.4]),
"xg" : (r"$X\gamma$" , [0.4, 0.0, 0.8]),
}
d_label_colors["ss"] = {
"fakes" : (r"Nonprompt lep.", [0.85, 0.85, 0.85]),
# "fakes_mc" : (r"MC fakes", [0.85, 0.85, 0.85]),
"flips" : (r"Charge misid.", [0.4, 0.4, 0.4]),
"rares" : (r"Rare", [1.0, 0.4, 1.0]),
"tth" : (r"$t\bar{t}H$", [0.4, 0.4, 0.6]),
"wz" : (r"WZ" , [1.0,0.8,0.0]),
"ww" : (r"WW" , [1.0,0.6,0.0]),
"ttw" : (r"$t\bar{t}W$", [0.0, 0.4, 0.0]),
"ttz" : (r"$t\bar{t}Z$", [0.4, 0.8, 0.4]),
"xg" : (r"$X\gamma$" , [0.4, 0.0, 0.8]),
}
d_flat_systematics = { }
d_flat_systematics["ft"] = {
"fakes": 0.3,
"flips": 0.2,
"rares": 0.2,
"ttw": 0.4,
"ttz": 0.4,
"tth": 0.25,
"ttvv": 0.11,
"xg": 0.11,
}
d_flat_systematics["ss"] = {
"fakes": 0.40,
"flips": 0.2,
"rares": 0.5,
"ww": 0.3,
"ttw": 0.3,
"ttz": 0.3,
"wz": 0.3,
"tth": 0.3,
"xg": 0.5,
}
# "fakes/ttZ/ttH (30%); flips/WW (20%); WZ/ttW (13%); Xg/Rares (50%)"
# from ../limits/test/crfit/
# after doing testcrbins.py to reduce bins (3 bins from crwsplit)
# and run_all_limits.sh with extra containing --unblinded
# d_crpostfitsf = {'fakes': 1.1948199139074693,
# 'flips': 1.020599049200507,
# 'rares': 1.1224350665249947,
# 'total': 1.2794573639702325,
# 'total_background': 1.3056080457107933,
# 'total_signal': 0.0,
# 'tth': 1.155083830402133,
# 'tttt': 0.0,
# 'ttvv': 1.0550142330872638,
# 'ttw': 1.4807971683302559,
# 'ttz': 1.3958306982737914,
# 'xg': 1.0842022472153388}
# d_crpostfitsf_errs = {'fakes': 0.5330058353969307,
# 'flips': 0.14395684087751284,
# 'rares': 0.3343497393873837,
# 'total': 0.14901268940160062,
# 'total_background': 0.1538191984828496,
# 'total_signal': 0.0,
# 'tth': 0.30185266795309806,
# 'tttt': 0.0,
# 'ttvv': 0.28627852193747866,
# 'ttw': 0.28537781734630363,
# 'ttz': 0.2800949349793666,
# 'xg': 0.3090238224979612}
d_crpostfitsf = {'fakes': 1.133,
'flips': 1.001,
'rares': 1.02,
'total': 1.2039836025074864,
'total_background': 1.2254378772947825,
'total_signal': 1.076,
'tth': 1.094,
'tttt': 1.076,
'ttvv': 1.014,
'ttw': 1.31,
'ttz': 1.266,
'xg': 1.017}
d_crpostfitsf_errs = {'fakes': 0.6836929317614069,
'flips': 0.5375067661493199,
'rares': 0.2513117714751323,
'total': 0.18140301518082388,
'total_background': 0.18528878200425344,
'total_signal': 0.0,
'tth': 0.31260232283875494,
'tttt': 0.0,
'ttvv': 0.1789620767510818,
'ttw': 0.5355134476152815,
'ttz': 0.348624399392768,
'xg': 0.2526828586109242}
# yuck, gotta make these global for multiprocessing since uproot file objects can't be pickled
files = {}
signames_ = []
def worker(info):
global files, signames_
analysis, outputdir, year, lumi, (var, (regions, xlabel)) = info
if signames_ == ["tttt"]:
sigstrs = [r"$t\bar{t}t\bar{t}$"]
else:
sigstrs = []
for signame in signames_:
# print signame
sigtag = signame.split("_m")[0].replace("fs_","")
# print sigtag
massstr = ",".join(re.findall("_m([0-9]*)",signame))
modelname = {
"t1tttt": "T1tttt",
"t6ttww": "T6ttWW",
"t5qqqqvv": "T5qqqqVV",
"t5qqqqvv_dm20": "T5qqqqVV, $\Delta$m=20",
"rpv_t1qqqql": "T1qqqqL",
}.get(sigtag,sigtag)
sigstr = r"{} ({}) $\times 10$".format(modelname,massstr)
sigstrs.append(sigstr)
fnames = []
for region in regions:
title = region.upper()
lumi_ = str(lumi)
if analysis == "ft":
if title in ["SRCR","SR"]:
if var == "TOTAL":
title = "Cut-based"
else:
title = "SR (pre-fit)"
elif title == "SRDISC":
title = "BDT"
elif title == "TTZCR":
title = "CRZ (pre-fit)"
elif title == "TTWCR":
title = "CRW (pre-fit)"
if analysis == "ss":
if title in ["BR"]:
title = "Baseline"
if title in ["SRHH","SRHL","SRLL","SRML","SRLM"] and (var == "TOTAL"):
title = title.replace("SR","")
region_for_hist = region[:]
if region == "brpostfit":
region_for_hist = "br"
def get_sf(proc):
return d_crpostfitsf.get(proc,1.0)
bgs = [
sum(
get_sf(proc)*Hist1D(files[y][proc]["{}_{}_{}".format(region_for_hist,var,proc)],
label="{} ($\\times${:.2f})".format(label,get_sf(proc)),
color=color,
dummy=proc,
) for y in files.keys())
for proc,(label,color) in d_label_colors[analysis].items()
]
else:
bgs = [
sum(Hist1D(files[y][proc]["{}_{}_{}".format(region_for_hist,var,proc)],label=label,color=color,dummy=proc) for y in files.keys())
for proc,(label,color) in d_label_colors[analysis].items()
]
data = sum([Hist1D(files[y]["data"]["{}_{}_data".format(region_for_hist,var)]) for y in files.keys()])
# sigcolors = ["red","#5863F8","#FCCA46","#04A777","#944BBB","#233D4D"]
sigcolors = [[0.75,0.15,0.22],"#5863F8","#FCCA46","#04A777","#944BBB","#233D4D"]
# print sigcolors
if region == "brpostfit":
region_for_hist = "br"
def get_sf(proc):
return d_crpostfitsf.get(proc,1.0)
sigs = [
sum(
get_sf(signame)*Hist1D(files[y][signame]["{}_{}_{}".format(region_for_hist,var,signame)],
label="{} ($\\times${:.2f})".format(sigstr,get_sf(signame)),
color=sigcolors[isig],
dummy=signame,
) for y in files.keys())
for isig,(signame,sigstr) in enumerate(zip(signames_,sigstrs))
]
else:
sigs = [
sum([Hist1D(files[y][signame]["{}_{}_{}".format(region_for_hist,var,signame)],color=sigcolors[isig]) for y in files.keys()])
for isig,signame in enumerate(signames_)
]
for isig,sigstr in enumerate(sigstrs):
sigs[isig].set_attr("label", sigstr)
if signames_[isig] != "tttt":
sigs[isig] *= 10.
bgs = sorted(bgs, key=lambda bg: bg.get_integral())
for bg in bgs:
# add flat systematic to stat unc in quadrature
bg._errors = np.hypot(bg._counts*d_flat_systematics[analysis].get(bg.get_attr("dummy"),0.),bg._errors)
if analysis == "ss" and region in ["SRML","ml","mlonz","mloffz"]:
# remove flips and ww since they are 0 for multilepton regions
new_bgs = [bg for bg in bgs if (bg.get_attr("dummy") not in ["flips","ww"])]
bgs = new_bgs
data.set_attr("label", "Data [{}]".format(int(data.get_integral())))
# if data.get_integral() < 1e-6: return
if abs(sum(bgs).get_integral()) < 1e-6: continue
ax_main_callback = None
ax_ratio_callback = None
mpl_legend_params = {}
ratio_range = [0.,3.]
xticks = []
mpl_xtick_params = {}
# # FIXME FIXME FIXME
# if analysis == "ss" and year != 2016 and not unblindall:
# data._counts *= 0.
# data._errors *= 0.
# data.set_attr("label", "Data (blind)")
# # FIXME FIXME FIXME
# data._counts *= 0.
# data._errors *= 0.
# data.set_attr("label", "Data")
if region in ["SRHH","SRHL","SRLL","SRML","SRLM"] and (var == "TOTAL"):
ratio_range = [0.,3.]
mpl_legend_params["fontsize"] = 8
mpl_legend_params["framealpha"] = 0.4
mpl_legend_params["ncol"] = 2
mpl_legend_params["labelspacing"] = 0.12
if region == "SRLL" and lumi == "137": lumi_ = "132"
data.poissonify()
# sbgs = sum(bgs)
# pulls = binomial_obs_z(data.counts,sbgs.counts,sbgs.errors)
# mu_pulls = pulls.mean()
# sig_pulls = pulls.std()
# if year != 2016 and not unblindall:
# data._counts *= 0.
# data._errors *= 0.
# data.set_attr("label", "Data (blind)")
def ax_main_callback(ax):
ax.set_ylim([0.05,ax.get_ylim()[1]*2.0])
ax.set_yscale("log", nonposy='clip'),
if region in ["SRML","SRLM","SRLL","SRHL","SRHH"]:
xticks = range(1,70)
# def ax_ratio_callback(ax):
# ax.text(0.18, -0.6,r"pulls $\mu,\sigma$ = {:.2f},{:.2f}".format(mu_pulls,sig_pulls), color="red", ha="center", va="center", fontsize=10.0, transform = ax.transAxes)
# if region in ["SRML"]:
# def ax_ratio_callback(ax):
# ax.text(0.4, -0.6,"off-Z", color="blue", ha="center", va="center", fontsize=10.0, wrap=True, transform = ax.transAxes)
# ax.text(0.55, -0.6,"on-Z", color="blue", ha="center", va="center", fontsize=10.0, wrap=True, transform = ax.transAxes)
# ax.axvline(x=21.5, color="blue", lw=1.0)
# ax.text(0.18, -0.6,"pulls $\mu,\sigma$ = {:.2f},{:.2f}$".format(mu_pulls,sig_pulls), color="red", ha="center", va="center", fontsize=10.0, transform = ax.transAxes)
elif region.lower() in ["sr","srdisc"]:
# blind all 2018 and all BDT plots since we will retrain
# if year == 2018 or (len(files.keys()) > 1) or region.lower in ["srdisc"]:
# data._counts *= 0.
# data._errors *= 0.
# data.set_attr("label", "Data (blind)")
data.convert_to_poisson_errors()
if var.lower() in ["total"]:
xticks = range(1,20)
if region.lower() in ["srdisc"]:
# def ax_main_callback(ax):
# ax.set_ylim([0.1,ax.get_ylim()[1]*1.5])
# ax.set_yscale("log", nonposy='clip'),
xticks = ["CRZ"]+range(1,25)
if (region.lower() in ["srcr"]) and (var.lower() in ["total"]):
# if not (unblindall or year == 2016):
# if year == 2018:
# data._counts[2:] *= 0.
# data._errors[2:] *= 0.
data.set_attr("label", "Data [{}]".format(int(data.get_integral())))
def ax_main_callback(ax):
ax.set_ylim([0.1,ax.get_ylim()[1]*1.5])
ax.set_yscale("log", nonposy='clip'),
xticks = ["CRZ","CRW"]+range(1,20)
if (var.lower() in ["disc"]) and (region.lower() not in ["ttwcr","ttzcr"]):
# # if not (unblindall or year == 2016):
# if year == 2018:
# data._counts[-10:] *= 0.
# data._errors[-10:] *= 0.
data.set_attr("label", "Data [{}]".format(int(data.get_integral())))
if (var.lower() in ["charge","el_charge","mu_charge","q3"]):
xticks = ["$(-)$","$(+)$",""]
mpl_xtick_params = dict(rotation=0, fontsize=14)
# specific plots tuned by hand
if analysis == "ss":
if region.lower() == "br":
if var.lower() == "charge":
def ax_main_callback(ax):
ax.set_xlim([-1,2.6])
def ax_ratio_callback(ax):
ax.set_xlim([-1,2.6])
if var == "TOTAL":
if region == "SRLL":
def ax_main_callback(ax):
ax.set_ylim([0.05,ax.get_ylim()[1]*5.5])
ax.set_yscale("log", nonposy='clip'),
if region == "SRML":
# mpl_legend_params["fontsize"] = 8
# mpl_legend_params["framealpha"] = 0.4
mpl_legend_params["ncol"] = 3
# mpl_legend_params["labelspacing"] = 0.12
def ax_main_callback(ax):
ax.set_ylim([0.05,ax.get_ylim()[1]*7.5])
ax.set_yscale("log", nonposy='clip'),
if region == "SRLM":
def ax_main_callback(ax):
ax.set_ylim([0.05,ax.get_ylim()[1]*4.0])
ax.set_yscale("log", nonposy='clip'),
if len(files.keys()) > 1:
fname = "{}/run2_{}_{}.pdf".format(outputdir,region,var)
else:
fname = "{}/y{}_{}_{}.pdf".format(outputdir,files.keys()[0],region,var)
fnames.append(fname)
ylabel="Events"
binwidth = data.get_bin_widths()[0]
if var in ["ht","met","njets","nbtags"]:
if var in ["ht","met"]:
ylabel = "Events / {} GeV".format(int(binwidth))
mpl_legend_params["fontsize"] = 12
if var in ["njets","nbtags"] and region in ["ttwcr","ttzcr"]:
data.poissonify()
ratio_range = [0.,2.]
if var in ["njets","nbtags"] and region in ["sr"]:
ratio_range = [0.,2.]
# mpl_legend_params["framealpha"] = 0.4
# mpl_legend_params["ncol"] = 1
# mpl_legend_params["labelspacing"] = 0.10
if analysis == "ss":
if var in ["ht","met","mtmin"] and region in ["br"]:
data.poissonify()
mpl_legend_params["fontsize"] = 12
ratio_range = [0.,2.]
ylabel = "Events / {} GeV".format(int(binwidth))
if var in ["njets","nbtags"] and region in ["br"]:
data.poissonify()
mpl_legend_params["fontsize"] = 12
ratio_range = [0.,3.]
if var in ["charge"] and region in ["br"]:
data.poissonify()
mpl_legend_params["fontsize"] = 12
ratio_range = [0.,2.]
for do_log in [False,True]:
fname_tmp = str(fname)
if do_log:
fname_tmp = fname.replace(".pdf","_log.pdf").replace(".png","_log.png")
plot_stack(bgs=bgs, data=data, title=title, xlabel=xlabel, ylabel=ylabel, filename=fname_tmp,
# cms_type = "Preliminary",
cms_type = "",
lumi = lumi_,
ratio_range=ratio_range,
sigs=sigs,
do_log=do_log,
mpl_xtick_params=mpl_xtick_params,
mpl_ratio_params={
"label":"Data/Pred.",
},
xticks=xticks,
mpl_sig_params={
# "hist":False,
},
ax_main_callback=ax_main_callback,
mpl_legend_params=mpl_legend_params,
ax_ratio_callback=ax_ratio_callback,
do_bkg_syst=True,
)
if (region in ["sr","brpostfit","ttwcr","ttzcr"]):
fname_tmp = fname.replace(".pdf","_stacked.pdf").replace(".png","_stacked.png")
plot_stack(bgs=bgs+sigs, data=data, title=title, xlabel=xlabel, ylabel=ylabel, filename=fname_tmp,
# cms_type = "Preliminary",
cms_type = "",
lumi = lumi_,
ratio_range=ratio_range,
# sigs=sigs,
do_log=False,
mpl_xtick_params=mpl_xtick_params,
mpl_ratio_params={
"label":"Data/Pred.",
},
xticks=xticks,
mpl_sig_params={
# "hist":False,
},
ax_main_callback=ax_main_callback,
mpl_legend_params=mpl_legend_params,
ax_ratio_callback=ax_ratio_callback,
do_bkg_syst=True,
)
# os.system("ic {}".format(fname))
# print bgs
# print data
# return
table_info = write_table(data,bgs,signal=(None if not sigs else sigs[0]),outname=fname.replace(".pdf",".txt"))
# table_info = write_table(data,bgs,signal=sig,outname=fname.replace(".pdf",".txt"),
# binlabels=xticks,signame=sigstrs[0].replace(r"$\times 10$","x10").replace(","," "),csv=True)
return ", ".join(fnames)
def make_plots(outputdir="plots", inputdir="outputs", year=2017, lumi="41.5", other_years=[], regions=[], signames="tttt", doss=False, show_mcfakes=False):
global files, signames_, other_files
signames_ = signames
os.system("mkdir -p {}/".format(outputdir))
analysis = "ft"
if doss: analysis = "ss"
if show_mcfakes and ("fakes" in d_label_colors[analysis]):
del d_label_colors[analysis]["fakes"]
d_label_colors[analysis]["fakes_mc"] = (r"MC fakes", [0.85, 0.85, 0.85])
files = {}
for y in [year]+other_years:
files[y] = { }
for proc in d_label_colors[analysis].keys()+["data"]+signames_:
# try:
ystr = str(y)
# if any(x in proc for x in ["fs_","rpv_"]): ystr = "2016"
files[y][proc] = uproot.open("{}/output_{}_{}.root".format(inputdir,ystr,proc))
# except IOError:
# print("{}/output_{}_{}.root doesn't exist, but ignoring because it's probably signal".format(inputdir,y,proc))
infos = [[analysis,outputdir,year,lumi,x] for x in labels[analysis].items()]
# print infos
# # Smarter. Run the first couple without thread pool so that it doesn't obscure errors when crashing
# # Then run rest in parallel
# # wtf. Can't do this because of https://github.com/uqfoundation/pathos/issues/153
# map(worker,infos[:2])
# os.nice(10)
# from multiprocessing import Pool as ThreadPool
# pool = ThreadPool(15)
# for res in pool.imap_unordered(worker,infos[1:]):
# if res:
# print "Wrote {}".format(res)
# map(worker,infos)
# Don't be nice for plots since I need them *now*
# os.nice(4)
from multiprocessing import Pool as ThreadPool
pool = ThreadPool(15)
# print infos
for res in pool.imap_unordered(worker,infos):
if res:
print "Wrote {}".format(res)
if __name__ == "__main__":
# 131.5
make_plots(
outputdir="plots_temp",
inputdir="outputs",
year=2018,
lumi="124.0", # 2016+2017+2018 --> 35.87+41.53+46.57 = 124.0
other_years = [2016,2017],
)