-
Notifications
You must be signed in to change notification settings - Fork 5
/
projections_preprocessing.py
533 lines (468 loc) · 18.5 KB
/
projections_preprocessing.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
#!/usr/bin/env python
"""
Thomas Klijnsma
"""
#____________________________________________________________________
# Imports
import logging
import os, sys, re, copy, glob, json
from OptionHandler import flag_as_option
import differentials
import differentialutils
import LatestBinning
import LatestPaths
import projections_paths as paths
# differentials.combine.preprocessing.Datacard.outdir = 'projections/cards_{0}'.format(differentials.core.datestr())
#____________________________________________________________________
pt_cards = differentials.core.AttrDict()
pt_cards.hgg = paths.pt_cards.hgg
pt_cards.hzz = paths.pt_cards.hzz
pt_cards.hbb = paths.pt_cards.hbb
pt_cards.combWithHbb = paths.pt_cards.combWithHbb
@flag_as_option
def list_nuisances_s2grouping(args):
card_comb = differentials.combine.preprocessing.Datacard(pt_cards.combWithHbb)
card_hgg = differentials.combine.preprocessing.Datacard(pt_cards.hgg)
card_hzz = differentials.combine.preprocessing.Datacard(pt_cards.hzz)
card_hbb = differentials.combine.preprocessing.Datacard(pt_cards.hbb)
nuis_comb = card_comb.get_nuisance_names()
nuis_hgg = card_hgg.get_nuisance_names()
nuis_hzz = card_hzz.get_nuisance_names()
nuis_hbb = card_hbb.get_nuisance_names()
nuis_name_length = max(map(len, nuis_comb))
group_name_length = max(map(len, card_comb.nuis_group_dict.keys()))
print '{0:{nuis_name_length}} {1:{group_name_length}} {2}'.format(
'Name', 'Group', 'Decay channels',
nuis_name_length = nuis_name_length,
group_name_length = group_name_length,
)
for nuis in card_comb.nuisances:
is_in = []
if nuis.name in nuis_hgg: is_in.append('hgg')
if nuis.name in nuis_hzz: is_in.append('hzz')
if nuis.name in nuis_hbb: is_in.append('hbb')
group = nuis.group if not(nuis.group is None) else 'pOther'
r = '{0:{nuis_name_length}} {1:{group_name_length}} {2}'.format(
nuis.name,
group,
', '.join(is_in),
nuis_name_length = nuis_name_length,
group_name_length = group_name_length,
)
print r
@flag_as_option
def create_projection_datacard_s2grouping(args):
decay_channel = differentialutils.get_decay_channel_tag(args)
card = LatestPaths.card.pth_smH[decay_channel]
datacard = differentials.combine.preprocessing.Datacard(card)
datacard.delete_current_nuisance_groups()
nuisgroupfinder = NuisanceGroupFinder()
datacard.lines.extend(
nuisgroupfinder.compile_datacard_lines(datacard.get_nuisance_names())
)
datacard.add_lumiscale_rateparam()
datacard.out('projection_{0}_s2groups'.format(decay_channel))
@flag_as_option
def create_projection_datacards_s2grouping(args):
for dc in [ 'hbb', 'hgg', 'hzz', 'combWithHbb' ]:
args = differentialutils.set_one_decay_channel(args, dc)
create_projection_datacard_s2grouping(args)
@flag_as_option
def create_kbkc_projection_datacard_s2grouping(args):
decay_channel = differentialutils.get_decay_channel_tag(args)
input_card = LatestPaths.card.yukawa[decay_channel]
datacard = differentials.combine.preprocessing.Datacard(input_card)
NuisanceGroupFinder().process_datacard(datacard)
datacard.add_lumiscale_rateparam()
datacard.out('projection_yukawa_{0}_s2groups'.format(decay_channel))
@flag_as_option
def create_ktcgkb_projection_datacard_s2grouping(args):
decay_channel = differentialutils.get_decay_channel_tag(args)
input_card = LatestPaths.card.pth_ggH[decay_channel]
datacard = differentials.combine.preprocessing.Datacard(input_card)
NuisanceGroupFinder().process_datacard(datacard)
datacard.add_lumiscale_rateparam()
datacard.out('projection_ktcgkb_{0}_s2groups'.format(decay_channel))
@flag_as_option
def create_coupling_projection_datacards_s2grouping(args):
for dc in [ 'hzz', 'hgg', 'combWithHbb' ]:
newargs = differentialutils.set_one_decay_channel(args, dc)
create_ktcgkb_projection_datacard_s2grouping(newargs)
#____________________________________________________________________
# Notes:
# Remember to let CMS_xH_incxs scale with lumi
@flag_as_option
def print_nuisances(args):
decay_channel = differentialutils.get_decay_channel_tag(args)
card = LatestPaths.card.pth_smH[decay_channel]
datacard = differentials.combine.preprocessing.Datacard(card)
datacard.print_nuisances()
@flag_as_option
def print_nuisances_caterina(args):
card = 'projections/caterina_Jun12/comb_2017_ggHbb.txt'
datacard = differentials.combine.preprocessing.Datacard(card)
datacard.print_nuisances()
@flag_as_option
def print_nuisances_ftr_hgg(args):
card = 'projections/ftr_hgg/simple_datacards/hgg_card_13TeV_projection.simple.txt'
datacard = differentials.combine.preprocessing.Datacard(card)
datacard.print_nuisances()
@flag_as_option
def create_projection_datacards(args):
create_hgg_projection_datacard(args)
create_hbb_projection_datacard(args)
create_hzz_projection_datacard(args)
@flag_as_option
def create_combWithHbb_projection_datacard(args):
differentials.combine.preprocessing.combine_cards(
os.path.join('suppliedInput/projection_combWithHbb'),
[ 'hgg', 'suppliedInput/fromVittorio/pT_newBins_Feb28/projection_hgg_Jun28.txt' ],
[ 'hzz', 'suppliedInput/fromJavier/bernstein_r7428/projection_hbb_Jun28.txt' ],
[ 'hbb', 'suppliedInput/fromDavid/PTH_Jan24_newBinning/smH/projection_hzz_Jun28.txt' ],
)
@flag_as_option
def create_kbkc_projection_datacard(args):
decay_channel = differentialutils.get_decay_channel_tag(args)
input_card = LatestPaths.card.yukawa[decay_channel]
datacard = differentials.combine.preprocessing.Datacard(input_card)
datacard.add_lumiscale_rateparam()
datacard.out('projection_yukawa_{0}'.format(decay_channel))
@flag_as_option
def create_ktcgkb_projection_datacard(args):
decay_channel = differentialutils.get_decay_channel_tag(args)
input_card = LatestPaths.card.pth_ggH[decay_channel]
datacard = differentials.combine.preprocessing.Datacard(input_card)
datacard.add_lumiscale_rateparam()
datacard.out('projection_ktcgkb_{0}'.format(decay_channel))
@flag_as_option
def create_hgg_projection_datacard(args):
card = LatestPaths.card.pth_smH.hgg
datacard = differentials.combine.preprocessing.Datacard(card)
pho_energy = [
'CMS_hgg_nuisance_deltafracright',
'CMS_hgg_nuisance_HighR9EE_13TeVscale',
'CMS_hgg_nuisance_LowR9EE_13TeVscale',
'CMS_hgg_nuisance_HighR9EB_13TeVscale',
'CMS_hgg_nuisance_LowR9EB_13TeVscale',
'CMS_hgg_nuisance_HighR9EE_13TeVsmear',
'CMS_hgg_nuisance_LowR9EE_13TeVsmear',
'CMS_hgg_nuisance_HighR9EB_13TeVsmear',
'CMS_hgg_nuisance_LowR9EB_13TeVsmear',
'CMS_hgg_nuisance_MaterialCentral_scale',
'CMS_hgg_nuisance_MaterialForward_scale',
'CMS_hgg_nuisance_NonLinearity_13TeVscale',
'CMS_hgg_nuisance_Geant4_13TeVscale',
'CMS_hgg_nuisance_Gain1EB_13TeVscale',
'CMS_hgg_nuisance_Gain6EB_13TeVscale',
'CMS_hgg_nuisance_LightColl_13TeVscale',
'CMS_hgg_nuisance_Absolute_13TeVscale',
]
datacard.add_group('pho_energy', pho_energy)
scale_with_lumi = [
'CMS_hgg_JER',
'CMS_hgg_TriggerWeight',
'CMS_hgg_phoIdMva',
'CMS_hgg_LooseMvaSF',
'CMS_hgg_SigmaEOverEShift',
'CMS_hgg_JEC',
'CMS_hgg_PreselSF',
'CMS_hgg_electronVetoSF',
# 'CMS_hgg_tth_parton_shower',
# 'CMS_hgg_tth_gluon_splitting',
# 'CMS_hgg_tth_mc_low_stat',
'lumi_13TeV',
'CMS_hgg_PUJIDShift',
# 'CMS_hgg_eff_e',
]
datacard.add_group('scale_with_lumi', scale_with_lumi)
datacard.add_lumiscale_rateparam()
datacard.out('projection_hgg')
@flag_as_option
def create_hbb_projection_datacard_befores2(args):
card = LatestPaths.card.pth_smH.hbb
datacard = differentials.combine.preprocessing.Datacard(card)
mcstat_group = [
'hqq125GenpT2failcat1mcstat',
'hqq125GenpT2passcat1mcstat',
'hqq125GenpT3failcat1mcstat',
'hqq125GenpT3failcat2mcstat',
'hqq125GenpT3passcat1mcstat',
'hqq125GenpT3passcat2mcstat',
'hqq125GenpT4failcat1mcstat',
'hqq125GenpT4failcat2mcstat',
'hqq125GenpT4passcat1mcstat',
'hqq125GenpT4passcat2mcstat',
'tqqfailcat1mcstat',
'tqqfailcat2mcstat',
'tqqpasscat1mcstat',
'tqqpasscat2mcstat',
'wqqfailcat1mcstat',
'wqqfailcat2mcstat',
'wqqpasscat1mcstat',
'wqqpasscat2mcstat',
'xhqq125GenpT1failcat1mcstat',
'xhqq125GenpT1failcat2mcstat',
'xhqq125GenpT1passcat1mcstat',
'xhqq125GenpT1passcat2mcstat',
'xhqq125GenpT2failcat1mcstat',
'xhqq125GenpT2failcat2mcstat',
'xhqq125GenpT2passcat1mcstat',
'xhqq125GenpT2passcat2mcstat',
'xhqq125GenpT3failcat1mcstat',
'xhqq125GenpT3failcat2mcstat',
'xhqq125GenpT3passcat1mcstat',
'xhqq125GenpT3passcat2mcstat',
'xhqq125GenpT4failcat1mcstat',
'xhqq125GenpT4failcat2mcstat',
'xhqq125GenpT4passcat1mcstat',
'xhqq125GenpT4passcat2mcstat',
'zqqfailcat1mcstat',
'zqqfailcat2mcstat',
'zqqpasscat1mcstat',
'zqqpasscat2mcstat',
]
# Group is already in the datacard!
# datacard.add_group('mcstat', mcstat_group)
theory_group = [
'hqq125pt',
'hqq125ptShape',
]
datacard.add_group('theory', theory_group)
tagging_group = [
'bbeff',
'scale',
'scalept',
'smear',
'veff',
]
datacard.add_group('tagging', tagging_group)
ttbarcr_group = [
'muid',
'muiso',
'mutrigger',
'qcdpassmuonCRmcstat',
'qcdfailmuonCRmcstat',
'stqqfailmuonCRmcstat',
'stqqpassmuonCRmcstat',
'tqqfailmuonCRmcstat',
'tqqpassmuonCRmcstat',
'zqqfailmuonCRmcstat',
'hqq125failmuonCRmcstat',
'zllfailmuonCRmcstat',
'wlnupassmuonCRmcstat',
'wlnufailmuonCRmcstat',
'vvqqfailmuonCRmcstat ',
]
datacard.add_group('ttbarcr', ttbarcr_group)
datacard.add_lumiscale_rateparam()
datacard.out('projection_hbb')
@flag_as_option
def create_hzz_projection_datacard(args):
card = LatestPaths.card.pth_smH.hzz
datacard = differentials.combine.preprocessing.Datacard(card)
datacard.add_lumiscale_rateparam()
datacard.out('projection_hzz')
@flag_as_option
def test_nuisgroups(args):
nuisgroupfinder = NuisanceGroupFinder()
nuisgroupfinder.print_groups()
class NuisanceGroupFinder(object):
"""docstring for NuisanceGroupFinder"""
grouping_path = 'projections/andrew/np-groups-July17'
unassigned_means_pother = False
return_value_for_not_found = 'unassigned'
def __init__(self):
super(NuisanceGroupFinder, self).__init__()
self.grouping = {}
self.read_grouping()
# hbb card
self.add_grouping({'mcstat' : [
'hqq125GenpT2failcat1mcstat',
'hqq125GenpT2passcat1mcstat',
'hqq125GenpT3failcat1mcstat',
'hqq125GenpT3failcat2mcstat',
'hqq125GenpT3passcat1mcstat',
'hqq125GenpT3passcat2mcstat',
'hqq125GenpT4failcat1mcstat',
'hqq125GenpT4failcat2mcstat',
'hqq125GenpT4passcat1mcstat',
'hqq125GenpT4passcat2mcstat',
'tqqfailcat1mcstat',
'tqqfailcat2mcstat',
'tqqpasscat1mcstat',
'tqqpasscat2mcstat',
'wqqfailcat1mcstat',
'wqqfailcat2mcstat',
'wqqpasscat1mcstat',
'wqqpasscat2mcstat',
'xhqq125GenpT1failcat1mcstat',
'xhqq125GenpT1failcat2mcstat',
'xhqq125GenpT1passcat1mcstat',
'xhqq125GenpT1passcat2mcstat',
'xhqq125GenpT2failcat1mcstat',
'xhqq125GenpT2failcat2mcstat',
'xhqq125GenpT2passcat1mcstat',
'xhqq125GenpT2passcat2mcstat',
'xhqq125GenpT3failcat1mcstat',
'xhqq125GenpT3failcat2mcstat',
'xhqq125GenpT3passcat1mcstat',
'xhqq125GenpT3passcat2mcstat',
'xhqq125GenpT4failcat1mcstat',
'xhqq125GenpT4failcat2mcstat',
'xhqq125GenpT4passcat1mcstat',
'xhqq125GenpT4passcat2mcstat',
'zqqfailcat1mcstat',
'zqqfailcat2mcstat',
'zqqpasscat1mcstat',
'zqqpasscat2mcstat',
# Originally in ttbarcr group
'qcdpassmuonCRmcstat',
'qcdfailmuonCRmcstat',
'stqqfailmuonCRmcstat',
'stqqpassmuonCRmcstat',
'tqqfailmuonCRmcstat',
'tqqpassmuonCRmcstat',
'zqqfailmuonCRmcstat',
'hqq125failmuonCRmcstat',
'zllfailmuonCRmcstat',
'wlnupassmuonCRmcstat',
'wlnufailmuonCRmcstat',
'vvqqfailmuonCRmcstat ',
# Originally no group for:
'vvqqfailmuonCRmcstat',
'xhqq125failmuonCRmcstat',
'xhqq125passmuonCRmcstat',
]})
# The following are not in a hbb group, and also not in Andrew's groups
# Looks very much like it belongs to the bkgTheory group
# --> Actually these are rateParams, and t2ws crashes if you put them in
# a group. Instead make them unassigned.
self.add_grouping({
'unassigned' : [
'tqqfailcat1norm',
'tqqfailcat2norm',
'tqqfailmuonCRnorm',
'tqqpasscat1norm',
'tqqpasscat2norm',
'tqqpassmuonCRnorm',
]
})
# Fixing hgg
self.add_grouping({
'pLumi' : [
'lumi_13TeV',
],
'pResJ' : [
'CMS_hgg_JER',
],
'pScaleJ' : [
'CMS_hgg_JEC',
],
'pScaleJPileup' : [
'CMS_hgg_PUJIDShift',
],
'unassigned' : [
# Nuisances in Hgg datacard that are not grouped by Andrews grouping
'CMS_hgg_nuisance_MaterialCentral_scale',
'CMS_hgg_nuisance_LightColl_13TeVscale',
'CMS_hgg_nuisance_Absolute_13TeVscale',
'CMS_hgg_nuisance_HighR9EE_13TeVsmear',
'CMS_hgg_nuisance_LowR9EE_13TeVsmear',
'CMS_hgg_nuisance_HighR9EB_13TeVsmear',
'CMS_hgg_nuisance_LowR9EB_13TeVsmear',
'CMS_hgg_LooseMvaSF',
# These ones are simply empty in the datacard! That's why they crash...
'CMS_hgg_tth_parton_shower',
'CMS_hgg_tth_gluon_splitting',
'CMS_hgg_tth_mc_low_stat',
'CMS_hgg_eff_e',
]
})
# Fixing hzz
self.add_grouping({
'pMuonID' : [
'CMS_eff_m',
'CMS_zz4l_mean_m_sig',
'CMS_zz4l_sigma_m_sig',
],
'bkgTheory' : [ 'QCDscale_ggVV', 'kfactor_ggzz', 'norm_nonResH' ],
'unassigned' : [
'CMS_eff_e',
'CMS_hzz2e2mu_Zjets',
'CMS_hzz4e_Zjets',
'CMS_hzz4mu_Zjets',
'CMS_zjets_bkgdcompo',
# No idea for these:
'CMS_zz4l_n_sig_1_8',
'CMS_zz4l_n_sig_3_8',
'CMS_zz4l_mean_e_sig',
'CMS_zz4l_sigma_e_sig',
'CMS_zz4l_n_sig_2_8',
],
})
# The nuis due to uncertainty on xH should scale with lumi
self.add_grouping({'pOther' : ['CMS_xH_incxs']})
def read_grouping(self):
json_files = glob.glob(os.path.join(self.grouping_path, '*.json'))
for json_file in json_files:
with open(json_file, 'rb') as json_fp:
grouping = json.load(json_fp)
self.add_grouping(grouping, skip_deletion=True)
def add_grouping(self, grouping, skip_deletion=False):
for key, values in grouping.iteritems():
if not key in self.grouping: self.grouping[key] = []
if not skip_deletion:
for val in values: self.delete_from_already_grouped(val)
self.grouping[key].extend(values)
def delete_from_already_grouped(self, nuis):
group = self.check_already_grouped(nuis)
if group is None: return
logging.info('Removing {0} from group {1}'.format(nuis, group))
self.grouping[group].pop(self.grouping[group].index(nuis))
def check_already_grouped(self, nuis):
for key, values in self.grouping.iteritems():
if nuis in values:
return key
else:
return None
def lookup(self, nuis):
if nuis.startswith('pdfindex_'): return None
for key, values in self.grouping.iteritems():
if nuis in values:
if self.unassigned_means_pother and key == 'unassigned':
logging.info('Found \'unassigned\' for \'{0}\'; returning pOther'.format(nuis))
return 'pOther'
return key
else:
logging.info('Found no group for \'{0}\'; returning {1}'.format(nuis, self.return_value_for_not_found))
return self.return_value_for_not_found
def print_groups(self):
for key, values in self.grouping.iteritems():
print 'group {0}'.format(key)
print ' ' + '\n '.join(values)
def compile_datacard_lines(self, nuisances, verbose=True):
# Gather all the groups that should be created
groups = {}
for nuis in nuisances:
group = self.lookup(nuis)
if group is None or group == 'unassigned': continue
if not group in groups.keys(): groups[group] = []
groups[group].append(nuis)
# Create lines
lines = []
for groupname, nuisnames in groups.iteritems():
line = '{0} group = {1}'.format(
groupname, ' '.join(nuisnames)
)
lines.append(line)
if verbose:
logging.info(
'Compiled the following new group lines:\n{0}'
.format('\n'.join(lines))
)
return lines
def process_datacard(self, datacard):
datacard.delete_current_nuisance_groups()
datacard.lines.extend(
self.compile_datacard_lines(datacard.get_nuisance_names())
)