/
runMETCorrectionsAndUncertainties.py
1570 lines (1279 loc) · 81.4 KB
/
runMETCorrectionsAndUncertainties.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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import FWCore.ParameterSet.Config as cms
from FWCore.GuiBrowsers.ConfigToolBase import *
import PhysicsTools.PatAlgos.tools.helpers as configtools
from PhysicsTools.PatAlgos.tools.jetTools import switchJetCollection
def isValidInputTag(input):
input_str = input
if isinstance(input, cms.InputTag):
input_str = input.value()
if input is None or input_str == '""':
return False
else:
return True
class RunMETCorrectionsAndUncertainties(ConfigToolBase):
_label='RunMETCorrectionsAndUncertainties'
_defaultParameters=dicttypes.SortedKeysDict()
def __init__(self):
ConfigToolBase.__init__(self)
self.addParameter(self._defaultParameters, 'metType', "PF",
"Type of considered MET (only PF supported so far)", Type=str)
self.addParameter(self._defaultParameters, 'correctionLevel', [""],
"level of correction : available corrections for pfMet are T0, T1, T2, Txy and Smear; irrelevant entry for MVAMet)",
allowedValues=["T0","T1","T2","Txy","Smear",""])
self.addParameter(self._defaultParameters, 'computeUncertainties', True,
"enable/disable the uncertainty computation", Type=bool)
self.addParameter(self._defaultParameters, 'produceIntermediateCorrections', False,
"enable/disable the production of all correction schemes (only for the most common)", Type=bool)
self.addParameter(self._defaultParameters, 'electronCollection', cms.InputTag('selectedPatElectrons'),
"Input electron collection", Type=cms.InputTag, acceptNoneValue=True)
# empty default InputTag for photons to avoid double-counting wrt. cleanPatElectrons collection
self.addParameter(self._defaultParameters, 'photonCollection', None,
"Input photon collection", Type=cms.InputTag, acceptNoneValue=True)
self.addParameter(self._defaultParameters, 'muonCollection', cms.InputTag('selectedPatMuons'),
"Input muon collection", Type=cms.InputTag, acceptNoneValue=True)
self.addParameter(self._defaultParameters, 'tauCollection', cms.InputTag('selectedPatTaus'),
"Input tau collection", Type=cms.InputTag, acceptNoneValue=True)
self.addParameter(self._defaultParameters, 'jetCollection', cms.InputTag('selectedPatJets'),
"Input jet collection", Type=cms.InputTag, acceptNoneValue=True)
self.addParameter(self._defaultParameters, 'jetCollectionUnskimmed', cms.InputTag('patJets'),
"Input unskimmed jet collection for T1 MET computation", Type=cms.InputTag, acceptNoneValue=True)
self.addParameter(self._defaultParameters, 'pfCandCollection', cms.InputTag('particleFlow'),
"pf Candidate collection", Type=cms.InputTag, acceptNoneValue=True)
self.addParameter(self._defaultParameters, 'autoJetCleaning', 'LepClean',
"Enable the jet cleaning for the uncertainty computation: Full for tau/photons/jet cleaning, Partial for jet cleaning, LepClean for jet cleaning with muon and electrons only, None or Manual for no cleaning", Type=str)
self.addParameter(self._defaultParameters, 'jetFlavor', 'AK4PFchs',
"Use AK4PF/AK4PFchs for PFJets,AK4Calo for CaloJets", Type=str)
self.addParameter(self._defaultParameters, 'jetCorrectionType', 'L1L2L3-L1',
"Use L1L2L3-L1 for the standard L1 removal / L1L2L3-RC for the random-cone correction", Type=str)
self.addParameter(self._defaultParameters, 'jetCorLabelUpToL3', cms.InputTag('ak4PFCHSL1FastL2L3Corrector'), "Use ak4PFL1FastL2L3Corrector (ak4PFCHSL1FastL2L3Corrector) for PFJets with (without) charged hadron subtraction, ak4CaloL1FastL2L3Corrector for CaloJets", Type=cms.InputTag)
self.addParameter(self._defaultParameters, 'jetCorLabelL3Res', cms.InputTag('ak4PFCHSL1FastL2L3ResidualCorrector'), "Use ak4PFL1FastL2L3ResidualCorrector (ak4PFCHSL1FastL2L3ResidualCorrector) for PFJets with (without) charged hadron subtraction, ak4CaloL1FastL2L3ResidualCorrector for CaloJets", Type=cms.InputTag)
self.addParameter(self._defaultParameters, 'jecUncertaintyFile', 'CondFormats/JetMETObjects/data/Summer15_50nsV5_DATA_UncertaintySources_AK4PFchs.txt',
"Extra JES uncertainty file", Type=str)
self.addParameter(self._defaultParameters, 'jecUncertaintyTag', 'SubTotalMC',
"JES uncertainty Tag", Type=str)
self.addParameter(self._defaultParameters, 'mvaMetLeptons',["Electrons","Muons"],
"Leptons to be used for recoil computation in the MVA MET, available values are: Electrons, Muons, Taus, Photons", allowedValues=["Electrons","Muons","Taus","Photons",""])
self.addParameter(self._defaultParameters, 'addToPatDefaultSequence', True,
"Flag to enable/disable that metUncertaintySequence is inserted into patDefaultSequence", Type=bool)
self.addParameter(self._defaultParameters, 'manualJetConfig', False,
"Enable jet configuration options", Type=bool)
self.addParameter(self._defaultParameters, 'recoMetFromPFCs', False,
"Recompute the MET from scratch using the pfCandidate collection", Type=bool)
self.addParameter(self._defaultParameters, 'reclusterJets', False,
"Flag to enable/disable the jet reclustering", Type=bool)
self.addParameter(self._defaultParameters, 'CHS', False,
"Flag to enable/disable the CHS jets", Type=bool)
self.addParameter(self._defaultParameters, 'runOnData', False,
"Switch for data/MC processing", Type=bool)
self.addParameter(self._defaultParameters, 'onMiniAOD', False,
"Switch on miniAOD configuration", Type=bool)
self.addParameter(self._defaultParameters, 'postfix', '',
"Technical parameter to identify the resulting sequence and its modules (allows multiple calls in a job)", Type=str)
self._parameters = copy.deepcopy(self._defaultParameters)
self._comment = ""
def getDefaultParameters(self):
return self._defaultParameters
#=========================================================================================
def __call__(self, process,
metType =None,
correctionLevel =None,
computeUncertainties =None,
produceIntermediateCorrections = None,
electronCollection =None,
photonCollection =None,
muonCollection =None,
tauCollection =None,
jetCollection =None,
jetCollectionUnskimmed =None,
pfCandCollection =None,
autoJetCleaning =None,
jetFlavor =None,
jetCorr =None,
jetCorLabelUpToL3 =None,
jetCorLabelL3Res =None,
jecUncertaintyFile =None,
jecUncertaintyTag =None,
mvaMetLeptons =None,
addToPatDefaultSequence =None,
manualJetConfig =None,
recoMetFromPFCs =None,
reclusterJets =None,
CHS =None,
runOnData =None,
onMiniAOD =None,
postfix =None):
electronCollection = self.initializeInputTag(electronCollection, 'electronCollection')
photonCollection = self.initializeInputTag(photonCollection, 'photonCollection')
muonCollection = self.initializeInputTag(muonCollection, 'muonCollection')
tauCollection = self.initializeInputTag(tauCollection, 'tauCollection')
jetCollection = self.initializeInputTag(jetCollection, 'jetCollection')
jetCollectionUnskimmed = self.initializeInputTag(jetCollectionUnskimmed, 'jetCollectionUnskimmed')
pfCandCollection = self.initializeInputTag(pfCandCollection, 'pfCandCollection')
if metType is None :
metType = self._defaultParameters['metType'].value
if correctionLevel is None :
correctionLevel = self._defaultParameters['correctionLevel'].value
if computeUncertainties is None :
computeUncertainties = self._defaultParameters['computeUncertainties'].value
if produceIntermediateCorrections is None :
produceIntermediateCorrections = self._defaultParameters['produceIntermediateCorrections'].value
if electronCollection is None :
electronCollection = self._defaultParameters['electronCollection'].value
if photonCollection is None :
photonCollection = self._defaultParameters['photonCollection'].value
if muonCollection is None :
muonCollection = self._defaultParameters['muonCollection'].value
if tauCollection is None :
tauCollection = self._defaultParameters['tauCollection'].value
if jetCollection is None :
jetCollection = self._defaultParameters['jetCollection'].value
if jetCollectionUnskimmed is None :
jetCollectionUnskimmed = self._defaultParameters['jetCollectionUnskimmed'].value
if pfCandCollection is None :
pfCandCollection = self._defaultParameters['pfCandCollection'].value
if autoJetCleaning is None :
autoJetCleaning = self._defaultParameters['autoJetCleaning'].value
if jetFlavor is None :
jetFlavor = self._defaultParameters['jetFlavor'].value
if jetCorr is None :
jetCorr = self._defaultParameters['jetCorrectionType'].value
if jetCorLabelUpToL3 is None:
jetCorLabelUpToL3 = self._defaultParameters['jetCorLabelUpToL3'].value
if jetCorLabelL3Res is None:
jetCorLabelL3Res = self._defaultParameters['jetCorLabelL3Res'].value
if jecUncertaintyFile is None:
jecUncertaintyFile = self._defaultParameters['jecUncertaintyFile'].value
if jecUncertaintyTag is None:
jecUncertaintyTag = self._defaultParameters['jecUncertaintyTag'].value
if mvaMetLeptons is None:
mvaMetLeptons = self._defaultParameters['mvaMetLeptons'].value
if addToPatDefaultSequence is None :
addToPatDefaultSequence = self._defaultParameters['addToPatDefaultSequence'].value
if manualJetConfig is None :
manualJetConfig = self._defaultParameters['manualJetConfig'].value
if recoMetFromPFCs is None :
recoMetFromPFCs = self._defaultParameters['recoMetFromPFCs'].value
if reclusterJets is None :
reclusterJets = self._defaultParameters['reclusterJets'].value
if CHS is None :
CHS = self._defaultParameters['CHS'].value
if runOnData is None :
runOnData = self._defaultParameters['runOnData'].value
if onMiniAOD is None :
onMiniAOD = self._defaultParameters['onMiniAOD'].value
if postfix is None :
postfix = self._defaultParameters['postfix'].value
self.setParameter('metType',metType),
self.setParameter('correctionLevel',correctionLevel),
self.setParameter('computeUncertainties',computeUncertainties),
self.setParameter('produceIntermediateCorrections',produceIntermediateCorrections),
self.setParameter('electronCollection',electronCollection),
self.setParameter('photonCollection',photonCollection),
self.setParameter('muonCollection',muonCollection),
self.setParameter('tauCollection',tauCollection),
self.setParameter('jetCollection',jetCollection),
self.setParameter('jetCollectionUnskimmed',jetCollectionUnskimmed),
self.setParameter('pfCandCollection',pfCandCollection),
self.setParameter('autoJetCleaning',autoJetCleaning),
self.setParameter('jetFlavor',jetFlavor),
#optional
self.setParameter('jecUncertaintyFile',jecUncertaintyFile),
self.setParameter('jecUncertaintyTag',jecUncertaintyTag),
self.setParameter('mvaMetLeptons',mvaMetLeptons),
self.setParameter('addToPatDefaultSequence',addToPatDefaultSequence),
self.setParameter('recoMetFromPFCs',recoMetFromPFCs),
self.setParameter('runOnData',runOnData),
self.setParameter('onMiniAOD',onMiniAOD),
self.setParameter('postfix',postfix),
#if mva MET, autoswitch to std jets
if metType == "MVA":
self.setParameter('CHS',False),
#jet energy scale uncertainty needs
if manualJetConfig:
self.setParameter('CHS',CHS)
self.setParameter('jetCorLabelUpToL3',jetCorLabelUpToL3)
self.setParameter('jetCorLabelL3Res',jetCorLabelL3Res)
self.setParameter('reclusterJets',reclusterJets)
else:
#internal jet configuration
self.jetConfiguration()
#met reprocessing and jet reclustering
if recoMetFromPFCs:
self.setParameter('reclusterJets',True)
#jet collection overloading for automatic jet reclustering
if reclusterJets:
self.setParameter('jetCollection',cms.InputTag('selectedPatJets'))
self.setParameter('jetCollectionUnSkimmed',cms.InputTag('patJets'))
self.apply(process)
def toolCode(self, process):
metType = self._parameters['metType'].value
correctionLevel = self._parameters['correctionLevel'].value
computeUncertainties = self._parameters['computeUncertainties'].value
produceIntermediateCorrections = self._parameters['produceIntermediateCorrections'].value
electronCollection = self._parameters['electronCollection'].value
photonCollection = self._parameters['photonCollection'].value
muonCollection = self._parameters['muonCollection'].value
tauCollection = self._parameters['tauCollection'].value
jetCollection = self._parameters['jetCollection'].value
jetCollectionUnskimmed = self._parameters['jetCollectionUnskimmed'].value
pfCandCollection = self._parameters['pfCandCollection'].value
autoJetCleaning = self._parameters['autoJetCleaning'].value
jetFlavor = self._parameters['jetFlavor'].value
jetCorLabelUpToL3 = self._parameters['jetCorLabelUpToL3'].value
jetCorLabelL3Res = self._parameters['jetCorLabelL3Res'].value
jecUncertaintyFile = self._parameters['jecUncertaintyFile'].value
jecUncertaintyTag = self._parameters['jecUncertaintyTag'].value
mvaMetLeptons = self._parameters['mvaMetLeptons'].value
addToPatDefaultSequence = self._parameters['addToPatDefaultSequence'].value
recoMetFromPFCs = self._parameters['recoMetFromPFCs'].value
reclusterJets = self._parameters['reclusterJets'].value
onMiniAOD = self._parameters['onMiniAOD'].value
postfix = self._parameters['postfix'].value
#prepare jet configuration
jetUncInfos = { "jCorrPayload":jetFlavor, "jCorLabelUpToL3":jetCorLabelUpToL3,
"jCorLabelL3Res":jetCorLabelL3Res, "jecUncFile":jecUncertaintyFile,
"jecUncTag":jecUncertaintyTag }
patMetModuleSequence = cms.Sequence()
# recompute the MET (and thus the jets as well for correction) from scratch
if recoMetFromPFCs:
self.recomputeRawMetFromPfcs(process,
pfCandCollection,
onMiniAOD,
patMetModuleSequence,
postfix)
reclusterJets = True
elif onMiniAOD: #raw MET extraction if running on miniAODs
self.extractMET(process, "raw", patMetModuleSequence, postfix)
#default MET production
self.produceMET(process, metType,patMetModuleSequence, postfix)
#jet AK4 reclustering if needed for JECs
if reclusterJets:
jetCollection = self.ak4JetReclustering(process, pfCandCollection,
patMetModuleSequence, postfix)
#preparation to run over miniAOD (met reproduction)
if onMiniAOD:
# reclusterJets = True
self.miniAODConfiguration(process,
pfCandCollection,
jetCollectionUnskimmed,
patMetModuleSequence,
postfix
)
#jet ES configuration and jet cleaning
self.jetCleaning(process, autoJetCleaning, postfix)
# correct the MET
patMetCorrectionSequence, metModName = self.getCorrectedMET(process, metType, correctionLevel,
produceIntermediateCorrections,
patMetModuleSequence, postfix )
#fix the default jets for the type1 computation to those used to compute the uncertainties
#in order to be consistent with what is done in the correction and uncertainty step
#particularly true for miniAODs
if isValidInputTag(jetCollectionUnskimmed) and "T1" in metModName:
getattr(process,"patPFMetT1T2Corr").src = jetCollectionUnskimmed
getattr(process,"patPFMetT2Corr").src = jetCollectionUnskimmed
if postfix!="" and reclusterJets:
getattr(process,"patPFMetT1T2Corr"+postfix).src = cms.InputTag(jetCollectionUnskimmed.value()+postfix)
getattr(process,"patPFMetT2Corr"+postfix).src = cms.InputTag(jetCollectionUnskimmed.value()+postfix)
#compute the uncertainty on the MET
patMetUncertaintySequence = cms.Sequence()
if computeUncertainties:
patMetUncertaintySequence = self.getMETUncertainties(process, metType, metModName,
electronCollection,
photonCollection,
muonCollection,
tauCollection,
jetCollection,
jetUncInfos,
patMetModuleSequence,
postfix)
setattr(process, "patMetCorrectionSequence"+postfix, patMetCorrectionSequence)
setattr(process, "patMetUncertaintySequence"+postfix, patMetUncertaintySequence)
setattr(process, "patMetModuleSequence"+postfix, patMetModuleSequence)
#prepare and fill the final sequence containing all the sub-sequence
fullPatMetSequence = cms.Sequence()
fullPatMetSequence += getattr(process, "patMetCorrectionSequence"+postfix)
fullPatMetSequence += getattr(process, "patMetUncertaintySequence"+postfix)
fullPatMetSequence += getattr(process, "patMetModuleSequence"+postfix)
setattr(process,"fullPatMetSequence"+postfix,fullPatMetSequence)
# insert the fullPatMetSequence into patDefaultSequence if needed
if addToPatDefaultSequence:
if not hasattr(process, "patDefaultSequence"):
raise ValueError("PAT default sequence is not defined !!")
process.patDefaultSequence += getattr(process, "fullPatMetSequence"+postfix)
#====================================================================================================
def produceMET(self, process, metType, metModuleSequence, postfix):
if metType == "PF" and not hasattr(process, 'pat'+metType+'Met'):
process.load("PhysicsTools.PatUtils.patPFMETCorrections_cff")
if postfix != "" and metType == "PF" and not hasattr(process, 'pat'+metType+'Met'+postfix):
configtools.cloneProcessingSnippet(process, getattr(process,"producePatPFMETCorrections"), postfix)
setattr(process, 'pat'+metType+'Met'+postfix, getattr(process,'patPFMet' ).clone() )
getattr(process, "patPFMet"+postfix).metSource = cms.InputTag("pfMet"+postfix)
getattr(process, "patPFMet"+postfix).srcJets = cms.InputTag("selectedPatJets"+postfix)
getattr(process, "patPFMet"+postfix).srcPFCands = self._parameters["pfCandCollection"].value
if self._parameters["runOnData"].value:
getattr(process, "patPFMet"+postfix).addGenMET = False
#MM: FIXME MVA
if metType == "MVA": # and not hasattr(process, 'pat'+metType+'Met'):
# process.load("PhysicsTools.PatUtils.patPFMETCorrections_cff")
mvaMetProducer = self.createMVAMETModule(process)
setattr(process, 'pfMVAMet'+postfix, mvaMetProducer )
setattr(process, 'pat'+metType+'Met'+postfix, getattr(process,'patPFMet' ).clone(
metSource = cms.InputTag('pfMVAMet'),
) )
metModuleSequence += getattr(process, 'pat'+metType+'Met'+postfix )
#====================================================================================================
def getCorrectedMET(self, process, metType, correctionLevel,produceIntermediateCorrections, metModuleSequence, postfix ):
# default outputs
patMetCorrectionSequence = cms.Sequence()
metModName = "pat"+metType+"Met"+postfix
# loading correction file if not already here
#if not hasattr(process, 'patMetCorrectionSequence'):
# process.load("PhysicsTools.PatUtils.patPFMETCorrections_cff")
#MM loaded at the production level in principle
if metType == "MVA": #corrections are irrelevant for the MVA MET (except jet smearing?)
return patMetCorrectionSequence, metModName
## MM: FIXME, smearing procedure needs a lot of work, still 2010 recipes everywhere
## ==> smearing disabled for all cases
# if cor == "Smear":
# #print "WARNING: smearing procedure still uses 2010 recipe, disabled per default for the moment"
corNames = { #not really needed but in case we have changes in the future....
"T0":"T0pc",
"T1":"T1",
"T2":"T2",
"Txy":"Txy",
"Smear":"Smear",
}
#if empty correction level, no need to try something
for cor in correctionLevel: #MM to be changed!!!!!!
if cor not in corNames.keys():
if cor != "":
print "ERROR : ",cor," is not a proper MET correction name! aborting the MET correction production"
return patMetCorrectionSequence, metModName
corModNames = {
"T0": "patPFMetT0CorrSequence"+postfix,
"T1": "patPFMetT1T2CorrSequence"+postfix,
"T2": "patPFMetT2CorrSequence"+postfix,
"Txy": "patPFMetTxyCorrSequence"+postfix,
"Smear": "patPFMetSmearCorrSequence"+postfix,
"T2Smear": "patPFMetT2SmearCorrSequence"+postfix
}
if postfix != "":
configtools.cloneProcessingSnippet(process, getattr(process,"patPFMetT0CorrSequence"), postfix)
configtools.cloneProcessingSnippet(process, getattr(process,"patPFMetT1T2CorrSequence"), postfix)
configtools.cloneProcessingSnippet(process, getattr(process,"patPFMetT2CorrSequence"), postfix)
configtools.cloneProcessingSnippet(process, getattr(process,"patPFMetTxyCorrSequence"), postfix)
configtools.cloneProcessingSnippet(process, getattr(process,"patPFMetSmearCorrSequence"), postfix)
configtools.cloneProcessingSnippet(process, getattr(process,"patPFMetT2SmearCorrSequence"), postfix)
corModules = {}
for mod in corModNames.keys():
corModules[mod] = getattr(process, corModNames[mod] )
corTags = {
"T0":cms.InputTag('patPFMetT0Corr'+postfix),
"T1":cms.InputTag('patPFMetT1T2Corr'+postfix, 'type1'),
"T2":cms.InputTag('patPFMetT2Corr'+postfix, 'type2'),
"Txy": cms.InputTag('patPFMetTxyCorr'+postfix),
"Smear":cms.InputTag('patPFMetSmearCorr'+postfix, 'type1'),
"Smear":cms.InputTag('patPFMetT1T2SmearCorr'+postfix, 'type1'),
"T2Smear":cms.InputTag('patPFMetT2SmearCorr'+postfix, 'type2')
}
corScheme=""
corrections = []
correctionSequence = []
for cor in correctionLevel:
corScheme += corNames[cor]
corrections.append(corTags[cor])
correctionSequence.append(corModules[cor])
#T2 and smearing corModuleTag switch, specific case
if "T2" in correctionLevel and "Smear" in correctionLevel:
corrections.append(corTags["T2Smear"])
correctionSequence.append(corModules["T2Smear"])
# if not produceIntermediateCorrections:
# #print "REMOVAL"
# correctionSequence.remove( corModules["Smear"] )
# corrections.remove(corTags["Smear"])
#Txy parameter tuning
if "Txy" in correctionLevel:
self.tuneTxyParameters(process, corScheme, postfix)
getattr(process, "patPFMetTxyCorr"+postfix).srcPFlow = self._parameters["pfCandCollection"].value
#Enable MET significance in the type1 MET is computed
#if "T1" in correctionLevel:
# getattr(process, "pat"+metType+"Met"+postfix).computeMETSignificance = cms.bool(True)
#T1 parameter tuning when CHS jets are not used
if "T1" in correctionLevel and not self._parameters["CHS"].value:
setattr(process, "corrPfMetType1"+postfix, getattr(process, "corrPfMetType1" ).clone() )
getattr(process, "corrPfMetType1"+postfix).src = cms.InputTag("ak4PFJets"+postfix)
getattr(process, "corrPfMetType1"+postfix).jetCorrLabel = "ak4PFL1FastL2L3Corrector"
getattr(process, "corrPfMetType1"+postfix).jetCorrLabelRes = "ak4PFL1FastL2L3ResidualCorrector"
getattr(process, "corrPfMetType1"+postfix).offsetCorrLabel = "ak4PFL1FastjetCorrector"
if "T1" in correctionLevel and self._parameters["CHS"].value and self._parameters["reclusterJets"].value:
getattr(process, "corrPfMetType1"+postfix).src = cms.InputTag("ak4PFJetsCHS"+postfix)
#create the main MET producer
metModName = "pat"+metType+"Met"+corScheme+postfix
sequenceName=""
corMetProducer=None
if metType == "PF":
corMetProducer = cms.EDProducer("CorrectedPATMETProducer",
src = cms.InputTag('pat'+metType+'Met' + postfix),
srcCorrections = cms.VInputTag(corrections)
)
sequenceName="patMetCorrectionSequence"
#MM: FIXME MVA
#if metType == "MVA":
# return patMetCorrectionSequence, metModName #FIXME
# corMetProducer = self.createMVAMETModule(process)
# sequenceName="pfMVAMEtSequence"
setattr(process,metModName, corMetProducer)
# adding the full sequence only if it does not exist
if not hasattr(process, sequenceName+postfix):
for corModule in correctionSequence:
patMetCorrectionSequence += corModule
setattr(process, sequenceName+postfix, patMetCorrectionSequence)
else: #if it exists, only add the missing correction modules, no need to redo everything
patMetCorrectionSequence = cms.Sequence()
setattr(process, sequenceName+postfix,patMetCorrectionSequence)
for mod in corModNames.keys():
if not hasattr(process, corModNames[mod]):
patMetCorrectionSequence += corModule
#plug the main patMetproducer
metModuleSequence += getattr(process, metModName)
#create the intermediate MET steps
#and finally add the met producers in the sequence for scheduled mode
if produceIntermediateCorrections:
interMets = self.addIntermediateMETs(process, metType, correctionLevel, corScheme, corTags,corNames, postfix)
for met in interMets.keys():
setattr(process,met, interMets[met] )
metModuleSequence += getattr(process, met)
return patMetCorrectionSequence, metModName
#====================================================================================================
def addIntermediateMETs(self, process, metType, correctionLevel, corScheme, corTags, corNames, postfix):
interMets = {}
# we don't want to duplicate an exisiting module if we ask for a simple 1-corr scheme
if len(correctionLevel) == 1:
return interMets
#ugly, but it works
nCor=len(correctionLevel)+1
ids = [0]*nCor
for i in range(nCor**nCor):
tmp=i
exists=False
corName=""
corrections = []
for j in range(nCor):
ids[j] = tmp%nCor
tmp = tmp//nCor
if j != 0 and ids[j-1] < ids[j]:
exists=True
for k in range(0,j):
if ids[k] == ids[j] and ids[k]!=0:
exists=True
if exists or sum(ids[j] for j in range(nCor))==0:
continue
for cor in range(nCor):
cid = ids[nCor-cor-1]
cKey = correctionLevel[cid-1]
if cid ==0:#empty correction
continue
else :
corName += corNames[cKey]
corrections.append( corTags[ cKey ] )
if corName == corScheme:
continue
corName='pat'+metType+'Met' + corName + postfix
interMets[corName] = cms.EDProducer("CorrectedPATMETProducer",
src = cms.InputTag('pat'+metType+'Met' + postfix),
srcCorrections = cms.VInputTag(corrections)
)
return interMets
#====================================================================================================
def getMETUncertainties(self, process, metType, metModName, electronCollection, photonCollection,
muonCollection, tauCollection, jetCollection, jetUncInfos, patMetModuleSequence, postfix):
# uncertainty sequence
metUncSequence = cms.Sequence()
#===================================================================================
# jet energy resolution shifts
#===================================================================================
if not isValidInputTag(jetCollection): #or jetCollection=="":
print "INFO : jet collection %s does not exists, no energy resolution shifting will be performed in MET uncertainty tools" % jetCollection
else:
preId=""
if "Smear" in metModName:
preId="Smeared"
metJERUncModules = self.getVariations(process, metModName, "Jet",preId, jetCollection, "Res", metUncSequence, postfix=postfix )
for mod in metJERUncModules.keys():
setattr(process, mod, metJERUncModules[mod] )
patMetModuleSequence += getattr(process, mod)
#===================================================================================
# Unclustered energy shifts
#===================================================================================
metUnclEUncModules = self.getUnclusteredVariations(process, metModName, metUncSequence, postfix )
for mod in metUnclEUncModules.keys():
setattr(process, mod, metUnclEUncModules[mod] )
patMetModuleSequence += getattr(process, mod)
#===================================================================================
# Other energy shifts
#===================================================================================
objectCollections = { "Jet":jetCollection,
"Electron":electronCollection,
"Photon":photonCollection,
"Muon":muonCollection,
"Tau":tauCollection,
}
for obj in objectCollections.keys():
if not isValidInputTag(objectCollections[obj]): # or objectCollections[obj]=="":
print "INFO : %s collection %s does not exists, no energy scale shifting will be performed in MET uncertainty tools" %(obj, objectCollections[obj])
else:
metObjUncModules = self.getVariations(process, metModName, obj,"", objectCollections[obj], "En", metUncSequence, jetUncInfos, postfix )
#adding the shifted MET produced to the proper patMetModuleSequence
for mod in metObjUncModules.keys():
setattr(process, mod, metObjUncModules[mod] )
patMetModuleSequence += getattr(process, mod)
#return the sequence containing the shifted collections producers
return metUncSequence
#====================================================================================================
def createEnergyScaleShiftedUpModule(self, process,identifier, objectCollection,
varyByNsigmas, jetUncInfos=None, postfix=""):
shiftedModuleUp = None
if identifier == "Electron":
shiftedModuleUp = cms.EDProducer("ShiftedPATElectronProducer",
src = objectCollection,
binning = cms.VPSet(
cms.PSet(
binSelection = cms.string('isEB'),
binUncertainty = cms.double(0.006)
),
cms.PSet(
binSelection = cms.string('!isEB'),
binUncertainty = cms.double(0.015)
),
),
shiftBy = cms.double(+1.*varyByNsigmas)
)
if identifier == "Photon":
shiftedModuleUp = cms.EDProducer("ShiftedPATPhotonProducer",
src = objectCollection,
binning = cms.VPSet(
cms.PSet(
binSelection = cms.string('isEB'),
binUncertainty = cms.double(0.01)
),
cms.PSet(
binSelection = cms.string('!isEB'),
binUncertainty = cms.double(0.025)
),
),
shiftBy = cms.double(+1.*varyByNsigmas)
)
if identifier == "Muon":
shiftedModuleUp = cms.EDProducer("ShiftedPATMuonProducer",
src = objectCollection,
binning = cms.VPSet(
cms.PSet(
binSelection = cms.string('pt < 100'),
binUncertainty = cms.double(0.002)
),
cms.PSet(
binSelection = cms.string('pt >= 100'),
binUncertainty = cms.double(0.05)
),
),
shiftBy = cms.double(+1.*varyByNsigmas)
)
if identifier == "Tau":
shiftedModuleUp = cms.EDProducer("ShiftedPATTauProducer",
src = objectCollection,
uncertainty = cms.double(0.03),
shiftBy = cms.double(+1.*varyByNsigmas)
)
if identifier == "Jet":
moduleType="ShiftedPATJetProducer"
#MM: FIXME MVA
#if self._parameters["metType"].value == "MVA":
# moduleType="ShiftedPFJetProducer"
shiftedModuleUp = cms.EDProducer(moduleType,
src = objectCollection,
jetCorrInputFileName = cms.FileInPath(jetUncInfos["jecUncFile"] ), #jecUncertaintyFile),
jetCorrUncertaintyTag = cms.string(jetUncInfos["jecUncTag"] ), #jecUncertaintyTag),
addResidualJES = cms.bool(True),
jetCorrLabelUpToL3 = cms.InputTag(jetUncInfos["jCorLabelUpToL3"].value() ), #jetCorrLabelUpToL3.value()),
jetCorrLabelUpToL3Res = cms.InputTag(jetUncInfos["jCorLabelL3Res"].value() ), #jetCorrLabelUpToL3Res.value()),
jetCorrPayloadName = cms.string(jetUncInfos["jCorrPayload"] ),
shiftBy = cms.double(+1.*varyByNsigmas),
)
return shiftedModuleUp
#====================================================================================================
#====================================================================================================
def removePostfix(self, name, postfix):
if postfix=="":
return name
baseName = name
if baseName[-len(postfix):] == postfix:
baseName = baseName[0:-len(postfix)]
else:
raise Exception("Tried to remove postfix %s from %s, but it wasn't there" % (postfix, baseName))
return baseName
#====================================================================================================
def tuneTxyParameters(self, process, corScheme, postfix):
import PhysicsTools.PatUtils.patPFMETCorrections_cff as metCors
xyTags = {
"Txy_50ns":metCors.patMultPhiCorrParams_Txy_50ns,
"T1Txy_50ns":metCors.patMultPhiCorrParams_T1Txy_50ns,
"T0pcTxy_50ns":metCors.patMultPhiCorrParams_T0pcTxy_50ns,
"T0pcT1Txy_50ns":metCors.patMultPhiCorrParams_T0pcT1Txy_50ns,
"T1T2Txy_50ns":metCors.patMultPhiCorrParams_T1T2Txy_50ns,
"T0pcT1T2Txy_50ns":metCors.patMultPhiCorrParams_T0pcT1T2Txy_50ns,
"T1SmearTxy_50ns":metCors.patMultPhiCorrParams_T1SmearTxy_50ns,
"T1T2SmearTxy_50ns":metCors.patMultPhiCorrParams_T1T2SmearTxy_50ns,
"T0pcT1SmearTxy_50ns":metCors.patMultPhiCorrParams_T0pcT1SmearTxy_50ns,
"T0pcT1T2SmearTxy_50ns":metCors.patMultPhiCorrParams_T0pcT1T2SmearTxy_50ns,
"Txy_25ns":metCors.patMultPhiCorrParams_Txy_25ns,
"T1Txy_25ns":metCors.patMultPhiCorrParams_T1Txy_25ns,
"T0pcTxy_25ns":metCors.patMultPhiCorrParams_T0pcTxy_25ns,
"T0pcT1Txy_25ns":metCors.patMultPhiCorrParams_T0pcT1Txy_25ns,
"T1T2Txy_25ns":metCors.patMultPhiCorrParams_T1T2Txy_25ns,
"T0pcT1T2Txy_25ns":metCors.patMultPhiCorrParams_T0pcT1T2Txy_25ns,
"T1SmearTxy_25ns":metCors.patMultPhiCorrParams_T1SmearTxy_25ns,
"T1T2SmearTxy_25ns":metCors.patMultPhiCorrParams_T1T2SmearTxy_25ns,
"T0pcT1SmearTxy_25ns":metCors.patMultPhiCorrParams_T0pcT1SmearTxy_25ns,
"T0pcT1T2SmearTxy_25ns":metCors.patMultPhiCorrParams_T0pcT1T2SmearTxy_25ns
}
getattr(process, "patPFMetTxyCorr"+postfix).parameters = xyTags[corScheme+"_25ns"]
##for automatic switch to 50ns / 25ns corrections ==> does not work...
#from Configuration.StandardSequences.Eras import eras
#eras.run2_50ns_specific.toModify( getattr(process, "patPFMetTxyCorr"+postfix) , parameters=xyTags[corScheme+"_50ns"] )
#eras.run2_25ns_specific.toModify( getattr(process, "patPFMetTxyCorr"+postfix) , parameters=xyTags[corScheme+"_25ns"] )
#====================================================================================================
def getVariations(self, process, metModName, identifier,preId, objectCollection, varType,
metUncSequence, jetUncInfos=None, postfix="" ):
# temporary hardcoded varyByNSigma value
varyByNsigmas=1
# remove the postfix to put it at the end
baseName = self.removePostfix(metModName, postfix)
#default shifted MET producers
shiftedMetProducers = {preId+identifier+varType+'Up':None, preId+identifier+varType+'Down':None}
#create the shifted collection producers=========================================
shiftedCollModules = {'Up':None, 'Down':None}
if identifier=="Jet" and varType=="Res":
smear=False
if "Smear" in metModName:
smear=True
shiftedCollModules['Up'] = self.createShiftedJetResModule(process, smear, objectCollection, +1.*varyByNsigmas,
"Up", metUncSequence, postfix)
shiftedCollModules['Down'] = self.createShiftedJetResModule(process, smear, objectCollection, -1.*varyByNsigmas,
"Down", metUncSequence, postfix)
else:
shiftedCollModules['Up'] = self.createEnergyScaleShiftedUpModule(process, identifier, objectCollection, varyByNsigmas, jetUncInfos, postfix)
shiftedCollModules['Down'] = shiftedCollModules['Up'].clone( shiftBy = cms.double(-1.*varyByNsigmas) )
if identifier=="Jet" and varType=="Res":
smear=False
if "Smear" in metModName:
objectCollection=cms.InputTag("selectedPatJetsForMetT1T2SmearCorr"+postfix)
#and the MET producers
shiftedMetProducers = self.createShiftedModules(process, shiftedCollModules, identifier, preId, objectCollection,
metModName, varType, metUncSequence, postfix)
return shiftedMetProducers
#========================================================================================
def createShiftedJetResModule(self, process, smear, objectCollection, varyByNsigmas, varDir, metUncSequence, postfix ):
smearedJetModule = self.createSmearedJetModule(process, objectCollection, smear, varyByNsigmas, varDir, metUncSequence, postfix)
return smearedJetModule
#========================================================================================
def createShiftedModules(self, process, shiftedCollModules, identifier, preId, objectCollection, metModName, varType, metUncSequence, postfix):
shiftedMetProducers = {}
# remove the postfix to put it at the end
baseName = self.removePostfix(metModName, postfix)
#adding the shifted collection producers to the sequence, create the shifted MET correction Modules and add them as well
for mod in shiftedCollModules.keys():
modName = "shiftedPat"+preId+identifier+varType+mod+postfix
#MM: FIXME MVA
#if "MVA" in metModName and identifier == "Jet": #dummy fix
# modName = "uncorrectedshiftedPat"+preId+identifier+varType+mod+postfix
setattr(process, modName, shiftedCollModules[mod])
metUncSequence += getattr(process, modName)
#removing the uncorrected
modName = "shiftedPat"+preId+identifier+varType+mod+postfix
#PF MET =================================================================================
if "PF" in metModName:
#create the MET shifts and add them to the sequence
shiftedMETCorrModule = self.createShiftedMETModule(process, objectCollection, modName)
modMETShiftName = "shiftedPatMETCorr"+preId+identifier+varType+mod+postfix
setattr(process, modMETShiftName, shiftedMETCorrModule)
metUncSequence += getattr(process, modMETShiftName)
#and finally prepare the shifted MET producers
modName = baseName+identifier+varType+mod+postfix
shiftedMETModule = getattr(process, metModName).clone(
src = cms.InputTag( metModName ),
srcCorrections = cms.VInputTag( cms.InputTag(modMETShiftName) )
)
shiftedMetProducers[ modName ] = shiftedMETModule
#MM: FIXME MVA
#MVA MET, duplication of the MVA MET producer ============================================
#if "MVA" in metModName:
# print "name: ",metModName, modName
# shiftedMETModule = self.createMVAMETModule(process, identifier, modName, True)
# modName = baseName+identifier+varType+mod+postfix
# setattr(process, modName, shiftedMETModule)
# shiftedMetProducers[ modName ] = shiftedMETModule
#
# #pileupjetId and =====
# if identifier == "Jet":
# #special collection replacement for the MVAMET for the jet case ======
# origCollection = cms.InputTag("calibratedAK4PFJetsForPFMVAMEt"+postfix) #self._parameters["jetCollection"].value
# newCollection = cms.InputTag("uncorrectedshiftedPat"+preId+identifier+varType+mod+postfix)
# moduleName = "shiftedPat"+preId+identifier+varType+mod+postfix
# corrShiftedModule = getattr(process,"calibratedAK4PFJetsForPFMVAMEt").clone(
# src=newCollection
# )
# setattr(process, moduleName, corrShiftedModule)
# metUncSequence += getattr(process, moduleName)
# puJetIdProducer = getattr(process, "puJetIdForPFMVAMEt").clone(
# jets = moduleName
# )
# puJetIdName = "puJetIdForPFMVAMEt"+preId+identifier+varType+mod+postfix
# setattr(process, puJetIdName, puJetIdProducer)
# metUncSequence += getattr(process, puJetIdName)
# shiftedMETModule.srcMVAPileupJetId = cms.InputTag(puJetIdName,"fullDiscriminant")
#==========================================================================================
return shiftedMetProducers
#========================================================================================
def createShiftedMETModule(self, process, originCollection, shiftedCollection):
shiftedModule = cms.EDProducer("ShiftedParticleMETcorrInputProducer",
srcOriginal = originCollection,
srcShifted = cms.InputTag(shiftedCollection),
)
return shiftedModule
#========================================================================================
def createMVAMETModule(self, process, identifier="", shiftedCollection="", isShifted=False, postfix="" ):
if not hasattr(process, "pfMVAMEt"):
process.load("RecoMET.METPUSubtraction.mvaPFMET_cff")
#retrieve collections
electronCollection = self._parameters["electronCollection"].value
muonCollection = self._parameters["electronCollection"].value
photonCollection = self._parameters["photonCollection"].value
tauCollection = self._parameters["tauCollection"].value
pfCandCollection = self._parameters["pfCandCollection"].value
corJetCollection = cms.InputTag("calibratedAK4PFJetsForPFMVAMEt"+postfix)
uncorJetCollection = cms.InputTag("ak4PFJets")
#shift if needed===
if isShifted:
if identifier == "Electron":
electronCollection = cms.InputTag(shiftedCollection)
if identifier == "Muon":
muonCollection = cms.InputTag(shiftedCollection)
if identifier == "Tau":
tauCollection = cms.InputTag(shiftedCollection)
if identifier == "Photon":
photonCollection = cms.InputTag(shiftedCollection)
if identifier == "Unclustered":
pfCandCollection = cms.InputTag(shiftedCollection)
if identifier == "Jet":
corJetCollection = cms.InputTag(shiftedCollection)
uncorJetCollection = cms.InputTag("uncorrected"+shiftedCollection)
#leptons
mvaMetLeptons = self._parameters["mvaMetLeptons"].value
leptons = cms.VInputTag([])
if "Electrons" in mvaMetLeptons and isValidInputTag(electronCollection):
leptons.append = electronCollection
if "Muons" in mvaMetLeptons and isValidInputTag(muonCollection):
leptons.append = muonCollection
if "Photons" in mvaMetLeptons and isValidInputTag(photonCollection):
leptons.append = photonCollection
if "Taus" in mvaMetLeptons and isValidInputTag(tauCollection):
leptons.append = tauCollection
mvaMetProducer=getattr(process, "pfMVAMEt").clone(
srcCorrJets = corJetCollection, #cms.InputTag("calibratedAK4PFJetsForPFMVAMEt"+postfix),
srcUncorrJets = uncorJetCollection,
srcPFCandidates = pfCandCollection,
srcLeptons = leptons,
)
return mvaMetProducer
#========================================================================================
def getUnclusteredVariations(self, process, metModName, metUncSequence, postfix ):
varyByNsigmas=1
unclEnMETcorrectionsSrcs = [
[ 'pfCandMETcorr' + postfix, [ '' ] ],
[ 'patPFMetT1T2Corr' + postfix, [ 'type2', 'offset' ] ],
[ 'patPFMetT2Corr' + postfix, [ 'type2' ] ],
]
#MM missing protection against missing corrections needed to compute the uncertainties
#for srcUnclEnMETcorr in unclEnMETcorrectionsSrcs:
# if not hasattr(process, srcUnclEnMETcorr[0])
# metUncSequence
shiftedMetProducers = {}
variations={"Up":1.,"Down":-1.}
for var in variations.keys():
modName = self.removePostfix(metModName, postfix)
modName = modName+"UnclusteredEn"+var+postfix
#MM: FIXME MVA
##MVA MET special case
#if "MVA" in metModName:
# shiftedMetProducers[ modName ] = self.getUnclusteredVariationsForMVAMET(process, var, variations[var]*varyByNsigmas, metUncSequence, postfix )
# continue
for srcUnclEnMETcorr in unclEnMETcorrectionsSrcs:
moduleUnclEnMETcorr = cms.EDProducer("ShiftedMETcorrInputProducer",
src = cms.VInputTag(
[ cms.InputTag(srcUnclEnMETcorr[0], instanceLabel) for instanceLabel in srcUnclEnMETcorr[1] ]
),
uncertainty = cms.double(0.10),
shiftBy = cms.double(variations[var]*varyByNsigmas)
)
baseName = self.removePostfix(srcUnclEnMETcorr[0], postfix)