CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
runNoPileUpMEtUncertainties.py
Go to the documentation of this file.
2 
4 
5 import PhysicsTools.PatAlgos.tools.helpers as configtools
6 from PhysicsTools.PatAlgos.tools.trigTools import _addEventContent
7 from PhysicsTools.PatUtils.tools.jmeUncertaintyTools import JetMEtUncertaintyTools
8 from PhysicsTools.PatUtils.tools.objectsUncertaintyTools import isValidInputTag,addSmearedJets
9 
10 #from PhysicsTools.PatUtils.patPFMETCorrections_cff import *
11 import RecoMET.METProducers.METSigParams_cfi as jetResolutions
12 #from PhysicsTools.PatAlgos.producersLayer1.metProducer_cfi import patMETs
13 
14 class RunNoPileUpMEtUncertainties(JetMEtUncertaintyTools):
15 
16  """ Shift energy of electrons, photons, muons, tau-jets and other jets
17  reconstructed in the event up/down,
18  in order to estimate effect of energy scale uncertainties on No-PU MET
19  """
20  _label = 'runNoPileUpMEtUncertainties'
21  _defaultParameters = dicttypes.SortedKeysDict()
22  def __init__(self):
23  JetMEtUncertaintyTools.__init__(self)
24  self.addParameter(self._defaultParameters, 'doApplyChargedHadronSubtraction', False,
25  "Flag to enable/disable usage of charged hadron subtraction when reconstructing jets", Type=bool)
26  self.addParameter(self._defaultParameters, 'pfCandCollection', cms.InputTag('particleFlow'),
27  "Input PFCandidate collection", Type=cms.InputTag)
28  self.addParameter(self._defaultParameters, 'doApplyUnclEnergyCalibration', False,
29  "Flag to enable/disable usage of 'unclustered energy' calibration", Type=bool)
30  self.addParameter(self._defaultParameters, 'sfNoPUjetOffsetEnCorr', 0.0,
31  "Parameter of No-PU MET algorithm ", Type=float)
32  self._parameters = copy.deepcopy(self._defaultParameters)
33  self._comment = ""
34 
35  def _addPFNoPUMEt(self, process, metUncertaintySequence,
36  shiftedParticleCollections, pfCandCollection, doApplyUnclEnergyCalibration,
37  sfNoPUjetOffsetEnCorr,
38  collectionsToKeep,
39  doApplyChargedHadronSubtraction,
40  doSmearJets,
41  jecUncertaintyFile, jecUncertaintyTag,
42  varyByNsigmas,
43  postfix):
44 
45  uncorrectedJetCollection = None
46  smearedUncorrectedJetCollection = None
47  correctedJetCollection = None
48  smearedCorrectedJetCollection = None
49  smearedPFCandidateCollection = None
50  smearedPFCandToVertexAssociation = None
51  puJetId = None
52  puJetIdTag = None
53  pfNoPUMEtData = None
54  pfNoPUMEt = None
55  pfNoPUMEtSequence = None
56  patPFMetNoPileUp = None
57  jetCorrLabelUpToL3 = None
58  jetCorrLabelUpToL3Residual = None
59  chsLabel = None
60 
61  if doApplyChargedHadronSubtraction:
62  if not hasattr(process, "pfNoPUchsMEt"):
63  process.load("RecoMET.METPUSubtraction.pfNoPUchsMEt_cff")
64  uncorrectedJetCollection = 'ak4PFchsJets'
65  smearedUncorrectedJetCollection = "smearedUncorrectedJetsForPFNoPUchsMEt"
66  correctedJetCollection = 'calibratedAK4PFchsJetsForPFNoPUchsMEt'
67  smearedCorrectedJetCollection = "smearedCorrectedJetsForPFNoPUchsMEt"
68  smearedPFCandidateCollection = "smearedPFCandidatesForPFNoPUchsMEt"
69  smearedPFCandToVertexAssociation = "smearedPFCandidateToVertexAssociationForPFNoPUchsMEt"
70  puJetId = "puJetIdForPFNoPUchsMEt"
71  puJetIdTag = "fullId"
72  pfNoPUMEtData = "pfNoPUchsMEtData"
73  pfNoPUMEt = "pfNoPUchsMEt"
74  pfNoPUMEtSequence = "pfNoPUchsMEtSequence"
75  patPFMetNoPileUp = "patPFNoPUchsMEt"
76  jetCorrLabelUpToL3 = "ak4PFchsL1FastL2L3Corrector"
77  jetCorrLabelUpToL3Residual = "ak4PFchsL1FastL2L3ResidualCorrector"
78  chsLabel = "chs"
79  else:
80  if not hasattr(process, "pfNoPUMEt"):
81  process.load("RecoMET.METPUSubtraction.pfNoPUMET_cff")
82  uncorrectedJetCollection = 'ak4PFJets'
83  smearedUncorrectedJetCollection = "smearedUncorrectedJetsForPFNoPUMEt"
84  correctedJetCollection = 'calibratedAK4PFJetsForPFNoPUMEt'
85  smearedCorrectedJetCollection = "smearedCorrectedJetsForPFNoPUMEt"
86  smearedPFCandidateCollection = "smearedPFCandidatesForPFNoPUMEt"
87  smearedPFCandToVertexAssociation = "smearedPFCandidateToVertexAssociationForPFNoPUMEt"
88  puJetId = "puJetIdForPFNoPUMEt"
89  puJetIdTag = "full53xId"
90  pfNoPUMEtData = "pfNoPUMEtData"
91  pfNoPUMEt = "pfNoPUMEt"
92  pfNoPUMEtSequence = "pfNoPUMEtSequence"
93  patPFMetNoPileUp = "patPFNoPUMEt"
94  jetCorrLabelUpToL3 = "ak4PFL1FastL2L3Corrector"
95  jetCorrLabelUpToL3Residual = "ak4PFL1FastL2L3ResidualCorrector"
96  chsLabel = ""
97 
98  process.load("RecoMET.METPUSubtraction.pfNoPUMET_cff")
99  if postfix != "":
100  configtools.cloneProcessingSnippet(process, getattr(process, pfNoPUMEtSequence), postfix) #getattr(process, pfNoPUMEtSequence)
101 
102  # CV: set 'sfNoPUjetOffsetEnCorr' parameter to value
103  # that depends on whether data or MC is being processed
104  pfNoPUMEtModule = getattr(process, pfNoPUMEt + postfix)
105  pfNoPUMEtModule.sfNoPUjetOffsetEnCorr = cms.double(sfNoPUjetOffsetEnCorr)
106 
107  uncalibratedPFCandCollection = pfCandCollection.moduleLabel
108  if doSmearJets:
109  process.load("RecoJets.Configuration.GenJetParticles_cff")
110  metUncertaintySequence += process.genParticlesForJetsNoNu
111  process.load("RecoJets.Configuration.RecoGenJets_cff")
112  metUncertaintySequence += process.ak4GenJetsNoNu
113  setattr(process, smearedUncorrectedJetCollection + postfix, cms.EDProducer("SmearedPFJetProducer",
114  src = cms.InputTag(uncorrectedJetCollection),
115  jetCorrLabel = cms.InputTag(jetCorrLabelUpToL3.value()),
116  dRmaxGenJetMatch = cms.string('min(0.5, 0.1 + 0.3*exp(-0.05*(genJetPt - 10.)))'),
117  sigmaMaxGenJetMatch = cms.double(3.),
118  inputFileName = cms.FileInPath('PhysicsTools/PatUtils/data/pfJetResolutionMCtoDataCorrLUT.root'),
119  lutName = cms.string('pfJetResolutionMCtoDataCorrLUT'),
120  jetResolutions = jetResolutions.METSignificance_params,
121  skipRawJetPtThreshold = cms.double(10.), # GeV
122  skipCorrJetPtThreshold = cms.double(1.e-2),
123  srcGenJets = cms.InputTag('ak4GenJetsNoNu'),
124  ##verbosity = cms.int32(1)
125  ))
126  metUncertaintySequence += getattr(process, smearedUncorrectedJetCollection + postfix)
127  getattr(process, correctedJetCollection + postfix).src = cms.InputTag(smearedUncorrectedJetCollection + postfix)
128  setattr(process, smearedPFCandidateCollection + postfix, cms.EDProducer("SmearedPFCandidateProducerForPFNoPUMEt",
129  srcPFCandidates = pfCandCollection,
130  srcJets = cms.InputTag(uncorrectedJetCollection),
131  jetCorrLabel = cms.InputTag(jetCorrLabelUpToL3.value()),
132  dRmaxGenJetMatch = cms.string('min(0.5, 0.1 + 0.3*exp(-0.05*(genJetPt - 10.)))'),
133  sigmaMaxGenJetMatch = cms.double(3.),
134  inputFileName = cms.FileInPath('PhysicsTools/PatUtils/data/pfJetResolutionMCtoDataCorrLUT.root'),
135  lutName = cms.string('pfJetResolutionMCtoDataCorrLUT'),
136  jetResolutions = jetResolutions.METSignificance_params,
137  skipRawJetPtThreshold = cms.double(10.), # GeV
138  skipCorrJetPtThreshold = cms.double(1.e-2),
139  srcGenJets = cms.InputTag('ak4GenJetsNoNu'),
140  ##verbosity = cms.int32(1)
141  ))
142  metUncertaintySequence += getattr(process, smearedPFCandidateCollection + postfix)
143  uncalibratedPFCandCollection = smearedPFCandidateCollection + postfix
144  setattr(process, smearedPFCandToVertexAssociation + postfix, getattr(process, "pfCandidateToVertexAssociationForPFNoPUMEt" + postfix).clone(
145  PFCandidateCollection = cms.InputTag(smearedPFCandidateCollection + postfix)
146  ))
147  metUncertaintySequence += getattr(process, smearedPFCandToVertexAssociation + postfix)
148  modulePFNoPUMEtData = getattr(process, pfNoPUMEtData + postfix) ##1
149  setattr(modulePFNoPUMEtData, "srcPFCandidates", cms.InputTag(smearedPFCandidateCollection + postfix))
150  setattr(modulePFNoPUMEtData, "srcPFCandToVertexAssociations", cms.InputTag(smearedPFCandToVertexAssociation + postfix))
151 
152  calibratedPFCandCollection = uncalibratedPFCandCollection
153  calibratedPFCandToVertexAssociation = "pfCandidateToVertexAssociationForPFNoPUMEt" + postfix
154  calibratedPFCandPFNoPUMEtData = pfNoPUMEtData + postfix
155  if doApplyUnclEnergyCalibration:
156  calibratedPFCandCollection = "calibratedPFCandidatesForPFNoPUMEt" + postfix
157  setattr(process, calibratedPFCandCollection, cms.EDProducer("PFCandResidualCorrProducer",
158  src = cms.InputTag(uncalibratedPFCandCollection),
159  residualCorrLabel = cms.string(""),
160  residualCorrEtaMax = cms.double(9.9),
161  extraCorrFactor = cms.double(1.),
162  isMC = cms.bool(True),
163  srcGenPileUpSummary = cms.InputTag('addPileupInfo'),
164  residualCorrVsNumPileUp = cms.PSet(
165  data = cms.PSet(
166  offset = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_Data_runs190456to208686_pfCands_offset.txt'),
167  slope = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_Data_runs190456to208686_pfCands_slope.txt')
168  ),
169  mc = cms.PSet(
170  offset = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_ZplusJets_madgraph_pfCands_offset.txt'),
171  slope = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_ZplusJets_madgraph_pfCands_slope.txt')
172  )
173  ),
174  verbosity = cms.int32(0)
175  ))
176  metUncertaintySequence += getattr(process, calibratedPFCandCollection)
177  calibratedPFCandToVertexAssociation = "calibratedPFCandidateToVertexAssociationForPFNoPUMEt" + postfix
178  setattr(process, calibratedPFCandToVertexAssociation, getattr(process, "pfCandidateToVertexAssociationForPFNoPUMEt" + postfix).clone(
179  PFCandidateCollection = cms.InputTag(calibratedPFCandCollection)
180  ))
181  metUncertaintySequence += getattr(process, calibratedPFCandToVertexAssociation)
182  calibratedPFCandPFNoPUMEtData = "calibratedPFCandPFNoPUMEtData" + postfix
183  setattr(process, calibratedPFCandPFNoPUMEtData, getattr(process, "pfNoPUMEtData" + postfix).clone(
184  srcPFCandidates = cms.InputTag(calibratedPFCandCollection),
185  srcPFCandToVertexAssociations = cms.InputTag(calibratedPFCandToVertexAssociation),
186  ))
187  getattr(process, pfNoPUMEtSequence + postfix).replace(
188  getattr(process, "pfNoPUMEtData" + postfix),
189  getattr(process, "pfNoPUMEtData" + postfix) + getattr(process, calibratedPFCandPFNoPUMEtData))
190  getattr(process, "pfNoPUMEt" + postfix).srcMVAMEtData = cms.InputTag(calibratedPFCandPFNoPUMEtData)
191  getattr(process, "pfMETcorrType0ForPFNoPUMEt" + postfix).srcPFCandidateToVertexAssociations = cms.InputTag(calibratedPFCandToVertexAssociation)
192 
193  metUncertaintySequence += getattr(process, pfNoPUMEtSequence + postfix)
194 
195 # if doApplyChargedHadronSubtraction:
196  # self._addPATMEtProducer(process, metUncertaintySequence, 'pfNoPUchsMEt' + postfix, 'patPFchsMetNoPileUp', collectionsToKeep, postfix)
197  # else:
198  self._addPATMEtProducer(process, metUncertaintySequence, 'pfNoPUMEt' + postfix, patPFMetNoPileUp, collectionsToKeep, postfix)
199 
200 
201 
202  variations = ['Up','Down']
203  varDir= { 'Up':1., 'Down':-1. }
204 
205  #=====================================================
206  # Leptons
207  #=====================================================
208  for leptonCollection in [ [ 'Electron', 'En', 'electronCollection', 0.3 ],
209  [ 'Photon', 'En', 'photonCollection', 0.3 ],
210  [ 'Muon', 'En', 'muonCollection', 0.3 ],
211  [ 'Tau', 'En', 'tauCollection', 0.3 ] ]:
212  if ( leptonCollection[2] in shiftedParticleCollections ) and isValidInputTag(shiftedParticleCollections[leptonCollection[2]]):
213  pfCandCollectionLeptonShift = \
214  self._addPFCandidatesForPFMEtInput(
215  process, metUncertaintySequence,
216  shiftedParticleCollections['%s' % leptonCollection[2]], leptonCollection[0], leptonCollection[1],
217  shiftedParticleCollections['%s%sUp' % (leptonCollection[2], leptonCollection[1])], shiftedParticleCollections['%s%sDown' % (leptonCollection[2], leptonCollection[1])],
218  leptonCollection[3],
219  cms.InputTag(calibratedPFCandCollection), "ForPFNoPU%sMEt%s" % (chsLabel, postfix))
220 
221 
222  for var in variations:
223 
224  modulePFCandidateToVertexAssociationShift = process.pfCandidateToVertexAssociation.clone(
225  PFCandidateCollection = cms.InputTag(pfCandCollectionLeptonShift[var])
226  )
227  modulePFCandidateToVertexAssociationShiftName = "pfCandidateToVertexAssociation%s%s%sForPileUpPF%sMEt%s" % (leptonCollection[0], leptonCollection[1], var, chsLabel, postfix)
228  setattr(process, modulePFCandidateToVertexAssociationShiftName, modulePFCandidateToVertexAssociationShift)
229  metUncertaintySequence += modulePFCandidateToVertexAssociationShift
230  uncorrectedJetsShiftName = "pfJets%s%s%sForPFNoPU%sMEt%s" % (leptonCollection[0], leptonCollection[1], var, chsLabel, postfix)
231  uncorrectedJetsShift = cms.EDProducer("ShiftedPFJetProducerByMatchedObject",
232  srcJets = cms.InputTag(uncorrectedJetCollection),
233  srcUnshiftedObjects = cms.InputTag(shiftedParticleCollections[ leptonCollection[2] ]),
234  srcShiftedObjects = cms.InputTag(shiftedParticleCollections['%s%sUp' % (leptonCollection[2], leptonCollection[1])]),
235  dRmatch_Jet = cms.double(leptonCollection[3])
236  )
237  setattr(process, uncorrectedJetsShiftName, uncorrectedJetsShift)
238  metUncertaintySequence += uncorrectedJetsShift
239  correctedJetsShift = "correctedJets%s%s%sForPFNoPU%sMEt%s" % (leptonCollection[0], leptonCollection[1], var, chsLabel, postfix)
240  setattr(process, correctedJetsShift, getattr(process, correctedJetCollection + postfix).clone(
241  src = cms.InputTag(uncorrectedJetsShiftName)
242  ))
243  metUncertaintySequence += getattr(process, correctedJetsShift)
244 
245  puJetIdShift = "puJetId%s%s%sForPFNoPU%sMEt%s" % (leptonCollection[0], leptonCollection[1], var, chsLabel, postfix)
246  setattr(process, puJetIdShift, getattr(process, puJetId).clone(
247  jets = cms.InputTag(correctedJetsShift)
248  ))
249  metUncertaintySequence += getattr(process, puJetIdShift)
250  moduleMEtDataLeptonShift = getattr(process, calibratedPFCandPFNoPUMEtData).clone(
251  srcPFCandidates = cms.InputTag(pfCandCollectionLeptonShift[var]),
252  srcPFCandToVertexAssociations = cms.InputTag(modulePFCandidateToVertexAssociationShiftName),
253  srcJets = cms.InputTag(correctedJetsShift),
254  srcJetIds = cms.InputTag(puJetIdShift, puJetIdTag)
255  )
256  moduleMEtDataLeptonShiftName = "%s%s%s%s%s" % (pfNoPUMEtData, leptonCollection[0], leptonCollection[1], var, postfix)
257  setattr(process, moduleMEtDataLeptonShiftName, moduleMEtDataLeptonShift)
258  metUncertaintySequence += moduleMEtDataLeptonShift
259  moduleMEtLeptonShift = getattr(process, pfNoPUMEt + postfix).clone(
260  srcMVAMEtDataLeptonMatch = cms.InputTag(moduleMEtDataLeptonShiftName),
261  srcLeptons = cms.VInputTag(self._getLeptonsForPFMEtInput(
262  shiftedParticleCollections, leptonCollection[2], '%s%s%s' % (leptonCollection[2], leptonCollection[1], var), postfix = postfix))
263  )
264  moduleMEtLeptonShiftName = "%s%s%s%s%s" % (pfNoPUMEt, leptonCollection[0], leptonCollection[1], var, postfix)
265  setattr(process, moduleMEtLeptonShiftName, moduleMEtLeptonShift)
266  metUncertaintySequence += moduleMEtLeptonShift
267  self._addPATMEtProducer(process, metUncertaintySequence,
268  moduleMEtLeptonShiftName, '%s%s%s%s' % (patPFMetNoPileUp, leptonCollection[0], leptonCollection[1], var),
269  collectionsToKeep, postfix)
270 
271 
272  if isValidInputTag(shiftedParticleCollections['jetCollection']):
273 
274  for var in variations:
275  uncorrectedJetsEnShift = None
276  correctedJetsEnShift = None
277  if doApplyChargedHadronSubtraction:
278  uncorrectedJetsEnShift = "uncorrectedJetsEn%sForPFNoPUchsMEt%s" % (var, postfix)
279  correctedJetsEnShift = "correctedJetsEn%sForPFNoPUchsMEt%s" % (var, postfix)
280  else:
281  uncorrectedJetsEnShift = "uncorrectedJetsEn%sForPFNoPUMEt%s" % (var, postfix)
282  correctedJetsEnShift = "correctedJetsEn%sForPFNoPUMEt%s" % (var, postfix)
283 
284  setattr(process, uncorrectedJetsEnShift, cms.EDProducer("ShiftedPFJetProducer",
285  src = cms.InputTag(uncorrectedJetCollection),
286  jetCorrInputFileName = cms.FileInPath(jecUncertaintyFile),
287  jetCorrUncertaintyTag = cms.string(jecUncertaintyTag),
288  addResidualJES = cms.bool(True),
289  jetCorrLabelUpToL3 = cms.InputTag(jetCorrLabelUpToL3.value()),
290  jetCorrLabelUpToL3Res = cms.InputTag(jetCorrLabelUpToL3Residual.value()),
291  shiftBy = cms.double( varDir[var] *varyByNsigmas),
292  ##verbosity = cms.int32(1)
293  ))
294  metUncertaintySequence += getattr(process, uncorrectedJetsEnShift)
295  setattr(process, correctedJetsEnShift, getattr(process, uncorrectedJetsEnShift).clone(
296  src = cms.InputTag(correctedJetCollection + postfix),
297  addResidualJES = cms.bool(False)
298  ))
299  metUncertaintySequence += getattr(process, correctedJetsEnShift)
300  puJetIdJetEnShift = "%sJetEn%s%s" % (puJetId, var, postfix)
301  setattr(process, puJetIdJetEnShift, getattr(process, puJetId + postfix).clone(
302  jets = cms.InputTag(correctedJetsEnShift)
303  ))
304  metUncertaintySequence += getattr(process, puJetIdJetEnShift)
305  pfNoPUMEtDataJetEnShift = "%sJetEn%s%s" % (pfNoPUMEtData, var, postfix)
306  setattr(process, pfNoPUMEtDataJetEnShift, getattr(process, calibratedPFCandPFNoPUMEtData).clone(
307  srcJets = cms.InputTag(correctedJetsEnShift),
308  srcJetIds = cms.InputTag(puJetIdJetEnShift, puJetIdTag)
309  ))
310  metUncertaintySequence += getattr(process, pfNoPUMEtDataJetEnShift)
311  pfNoPUMEtJetEnShift = "%sJetEn%s%s" % (pfNoPUMEt, var, postfix)
312  setattr(process, pfNoPUMEtJetEnShift, getattr(process, pfNoPUMEt + postfix).clone(
313  srcMVAMEtData = cms.InputTag(pfNoPUMEtDataJetEnShift),
314  srcLeptons = cms.VInputTag(self._getLeptonsForPFMEtInput(shiftedParticleCollections, postfix = postfix))
315  ))
316  metUncertaintySequence += getattr(process, pfNoPUMEtJetEnShift)
317  self._addPATMEtProducer(process, metUncertaintySequence,
318  pfNoPUMEtJetEnShift, "%sJetEn%s" % (patPFMetNoPileUp, var), collectionsToKeep, postfix)
319 
320 
321  if hasattr(process, smearedUncorrectedJetCollection + postfix):
322 
323  for var in variations:
324 
325  smearedUncorrectedJetsResShift = None
326  calibratedJetsResShift = None
327  smearedPFCandidatesJetResShift = None
328  smearedPFCandToVertexAssociationJetResShift = None
329  if doApplyChargedHadronSubtraction:
330  smearedUncorrectedJetsResShift = "smearedUncorrectedJetsRes%sForPFNoPUchsMEt%s" % (var, postfix)
331  smearedPFCandidatesJetResShift = "smearedPFCandidatesJetRes%sForPFNoPUchsMEt%s" % (var, postfix)
332  smearedPFCandToVertexAssociationJetResShift = "smearedPFCandidateToVertexAssociationJetRes%sForPFNoPUchsMEt%s" % (var, postfix)
333  else:
334  smearedUncorrectedJetsResShift = "smearedUncorrectedJetsRes%sForPFNoPUMEt%s" % (var, postfix)
335  smearedPFCandidatesJetResShift = "smearedPFCandidatesJetRes%sForPFNoPUMEt%s" % (var, postfix)
336  smearedPFCandToVertexAssociationJetResShift = "smearedPFCandidateToVertexAssociationJetRes%sForPFNoPUMEt%s" % (var, postfix)
337  setattr(process, smearedUncorrectedJetsResShift, getattr(process, smearedUncorrectedJetCollection + postfix).clone(
338  shiftBy = cms.double(varDir[var]*varyByNsigmas)
339  ))
340  metUncertaintySequence += getattr(process, smearedUncorrectedJetsResShift)
341  correctedJetsResShift = correctedJetCollection.replace("Jets", "JetsRes"+var)
342  setattr(process, correctedJetsResShift + postfix, getattr(process, correctedJetCollection + postfix).clone(
343  src = cms.InputTag(smearedUncorrectedJetsResShift)
344  ))
345  metUncertaintySequence += getattr(process, correctedJetsResShift + postfix)
346  puJetIdJetResShift = "%sJetRes%s%s" % (puJetId, var, postfix)
347  setattr(process, puJetIdJetResShift, getattr(process, puJetId + postfix).clone(
348  jets = cms.InputTag(correctedJetsResShift + postfix)
349  ))
350  metUncertaintySequence += getattr(process, puJetIdJetResShift)
351  setattr(process, smearedPFCandidatesJetResShift, getattr(process, smearedPFCandidateCollection + postfix).clone(
352  shiftBy = cms.double(varDir[var]*varyByNsigmas)
353  ))
354  metUncertaintySequence += getattr(process, smearedPFCandidatesJetResShift)
355  setattr(process, smearedPFCandToVertexAssociationJetResShift, getattr(process, "pfCandidateToVertexAssociationForPFNoPUMEt" + postfix).clone(
356  PFCandidateCollection = cms.InputTag(smearedPFCandidatesJetResShift)
357  ))
358  metUncertaintySequence += getattr(process, smearedPFCandToVertexAssociationJetResShift)
359  pfNoPUMEtDataJetResShift = "%sJetRes%s%s" % (pfNoPUMEtData, var, postfix)
360  setattr(process, pfNoPUMEtDataJetResShift, getattr(process, calibratedPFCandPFNoPUMEtData).clone(
361  srcJets = cms.InputTag(correctedJetsResShift +postfix),
362  srcJetIds = cms.InputTag(puJetIdJetResShift, puJetIdTag),
363  srcPFCandidates = cms.InputTag(smearedPFCandidatesJetResShift),
364  srcPFCandToVertexAssociations = cms.InputTag(smearedPFCandToVertexAssociationJetResShift)
365  ))
366  metUncertaintySequence += getattr(process, pfNoPUMEtDataJetResShift)
367  pfNoPUMEtJetResShift = "%sJetRes%s%s" % (pfNoPUMEt, var, postfix)
368  setattr(process, pfNoPUMEtJetResShift, getattr(process, pfNoPUMEt + postfix).clone(
369  srcMVAMEtData = cms.InputTag(pfNoPUMEtDataJetResShift),
370  srcLeptons = cms.VInputTag(self._getLeptonsForPFMEtInput(shiftedParticleCollections, postfix = postfix))
371  ))
372  metUncertaintySequence += getattr(process, pfNoPUMEtJetResShift)
373  self._addPATMEtProducer(process, metUncertaintySequence,
374  pfNoPUMEtJetResShift, "%sJetRes%s" % (patPFMetNoPileUp, var), collectionsToKeep, postfix)
375 
376 
377 
378  pfCandsUnclusteredEnShift = None
379  pfCandidateToVertexAssociationUnclusteredEnShift = None
380  pfMETcorrType0UnclusteredEnShift = None
381  if doApplyChargedHadronSubtraction:
382  pfCandsUnclusteredEnShift = "pfCandsUnclusteredEn%sForPFNoPUchsMEt%s" % (var, postfix)
383  pfCandidateToVertexAssociationUnclusteredEnShift = "pfCandidateToVertexAssociationUnclusteredEn%sForPFNoPUchsMEt%s" % (var, postfix)
384  pfMETcorrType0UnclusteredEnShift = "pfMETcorrType0UnclusteredEn%sForPFNoPUchsMEt%s" % (var, postfix)
385  else:
386  pfCandsUnclusteredEnShift ="pfCandsUnclusteredEn%sForPFNoPUMEt%s" % (var, postfix)
387  pfCandidateToVertexAssociationUnclusteredEnShift = "pfCandidateToVertexAssociationUnclusteredEn%sForPFNoPUMEt%s" % (var, postfix)
388  pfMETcorrType0UnclusteredEnShift = "pfMETcorrType0UnclusteredEn%sForPFNoPUMEt%s" % (var, postfix)
389  setattr(process, pfCandsUnclusteredEnShift, cms.EDProducer("ShiftedPFCandidateProducerForPFNoPUMEt",
390  srcPFCandidates = cms.InputTag(calibratedPFCandCollection),
391  srcJets = cms.InputTag(correctedJetCollection + postfix),
392  jetCorrInputFileName = cms.FileInPath(jecUncertaintyFile),
393  jetCorrUncertaintyTag = cms.string(jecUncertaintyTag),
394  minJetPt = cms.double(10.0),
395  shiftBy = cms.double(varDir[var]*varyByNsigmas),
396  unclEnUncertainty = cms.double(0.10)
397  ))
398  metUncertaintySequence += getattr(process, pfCandsUnclusteredEnShift)
399  setattr(process, pfCandidateToVertexAssociationUnclusteredEnShift, process.pfCandidateToVertexAssociation.clone(
400  PFCandidateCollection = cms.InputTag(pfCandsUnclusteredEnShift)
401  ))
402  metUncertaintySequence += getattr(process, pfCandidateToVertexAssociationUnclusteredEnShift)
403  setattr(process, pfMETcorrType0UnclusteredEnShift, getattr(process, "pfMETcorrType0" + postfix).clone(
404  srcPFCandidateToVertexAssociations = cms.InputTag(pfCandidateToVertexAssociationUnclusteredEnShift)
405  ))
406  metUncertaintySequence += getattr(process, pfMETcorrType0UnclusteredEnShift)
407  pfNoPUMEtDataUnclusteredEnShift = "%sUnclusteredEn%s%s" % (pfNoPUMEtData, var, postfix)
408  setattr(process, pfNoPUMEtDataUnclusteredEnShift, getattr(process, calibratedPFCandPFNoPUMEtData).clone(
409  srcPFCandidates = cms.InputTag(pfCandsUnclusteredEnShift),
410  srcPFCandToVertexAssociations = cms.InputTag(pfCandidateToVertexAssociationUnclusteredEnShift)
411  ))
412  metUncertaintySequence += getattr(process, pfNoPUMEtDataUnclusteredEnShift)
413  pfNoPUMEtUnclusteredEnShift = "%sUnclusteredEn%s%s" % (pfNoPUMEt, var, postfix)
414  setattr(process, pfNoPUMEtUnclusteredEnShift, getattr(process, pfNoPUMEt + postfix).clone(
415  srcMVAMEtData = cms.InputTag(pfNoPUMEtDataUnclusteredEnShift),
416  srcLeptons = cms.VInputTag(self._getLeptonsForPFMEtInput(shiftedParticleCollections, postfix = postfix)),
417  srcType0Correction = cms.InputTag(pfMETcorrType0UnclusteredEnShift)
418  ))
419  metUncertaintySequence += getattr(process, pfNoPUMEtUnclusteredEnShift)
420  self._addPATMEtProducer(process, metUncertaintySequence,
421  pfNoPUMEtUnclusteredEnShift, '%sUnclusteredEn%s' % (patPFMetNoPileUp, var), collectionsToKeep, postfix)
422 
423  def __call__(self, process,
424  electronCollection = None,
425  photonCollection = None,
426  muonCollection = None,
427  tauCollection = None,
428  jetCollection = None,
429  dRjetCleaning = None,
430  jetCorrLabel = None,
431  doApplyChargedHadronSubtraction = None,
432  doSmearJets = None,
433  jetSmearFileName = None,
434  jetSmearHistogram = None,
435  pfCandCollection = None,
436  doApplyUnclEnergyCalibration = None,
437  sfNoPUjetOffsetEnCorr = None,
438  jetCorrPayloadName = None,
439  jetCorrLabelUpToL3 = None,
440  jetCorrLabelUpToL3Res = None,
441  jecUncertaintyFile = None,
442  jecUncertaintyTag = None,
443  varyByNsigmas = None,
444  addToPatDefaultSequence = None,
445  outputModule = None,
446  postfix = None):
447  JetMEtUncertaintyTools.__call__(
448  self, process,
449  electronCollection = electronCollection,
450  photonCollection = photonCollection,
451  muonCollection = muonCollection,
452  tauCollection = tauCollection,
453  jetCollection = jetCollection,
454  jetCorrLabel = jetCorrLabel,
455  doSmearJets = doSmearJets,
456  jetSmearFileName = jetSmearFileName,
457  jetSmearHistogram = jetSmearHistogram,
458  jetCorrPayloadName = jetCorrPayloadName,
459  jetCorrLabelUpToL3 = jetCorrLabelUpToL3,
460  jetCorrLabelUpToL3Res = jetCorrLabelUpToL3Res,
461  jecUncertaintyFile = jecUncertaintyFile,
462  jecUncertaintyTag = jecUncertaintyTag,
463  varyByNsigmas = varyByNsigmas,
464  addToPatDefaultSequence = addToPatDefaultSequence,
465  outputModule = outputModule,
466  postfix = postfix)
467  if doApplyChargedHadronSubtraction is None:
468  doApplyChargedHadronSubtraction = self._defaultParameters['doApplyChargedHadronSubtraction'].value
469  pfCandCollection = self._initializeInputTag(pfCandCollection, 'pfCandCollection')
470  if doApplyUnclEnergyCalibration is None:
471  doApplyUnclEnergyCalibration = self._defaultParameters['doApplyUnclEnergyCalibration'].value
472  if sfNoPUjetOffsetEnCorr is None:
473  sfNoPUjetOffsetEnCorr = self._defaultParameters['sfNoPUjetOffsetEnCorr'].value
474 
475  self.setParameter('doApplyChargedHadronSubtraction', doApplyChargedHadronSubtraction)
476  self.setParameter('pfCandCollection', pfCandCollection)
477  self.setParameter('doApplyUnclEnergyCalibration', doApplyUnclEnergyCalibration)
478  self.setParameter('sfNoPUjetOffsetEnCorr', sfNoPUjetOffsetEnCorr)
479 
480  self.apply(process)
481 
482  def toolCode(self, process):
483  electronCollection = self._parameters['electronCollection'].value
484  photonCollection = self._parameters['photonCollection'].value
485  muonCollection = self._parameters['muonCollection'].value
486  tauCollection = self._parameters['tauCollection'].value
487  jetCollection = self._parameters['jetCollection'].value
488  jetCorrLabel = self._parameters['jetCorrLabel'].value
489  doApplyChargedHadronSubtraction = self._parameters['doApplyChargedHadronSubtraction'].value
490  chsLabel = None
491  if doApplyChargedHadronSubtraction:
492  chsLabel = "chs"
493  else:
494  chsLabel = ""
495  doSmearJets = self._parameters['doSmearJets'].value
496  jetSmearFileName = self._parameters['jetSmearFileName'].value
497  jetSmearHistogram = self._parameters['jetSmearHistogram'].value
498  pfCandCollection = self._parameters['pfCandCollection'].value
499  doApplyUnclEnergyCalibration = self._parameters['doApplyUnclEnergyCalibration'].value
500  sfNoPUjetOffsetEnCorr = self._parameters['sfNoPUjetOffsetEnCorr'].value
501  jetCorrPayloadName = self._parameters['jetCorrPayloadName'].value
502  jetCorrLabelUpToL3 = self._parameters['jetCorrLabelUpToL3'].value
503  jetCorrLabelUpToL3Res = self._parameters['jetCorrLabelUpToL3Res'].value
504  jecUncertaintyFile = self._parameters['jecUncertaintyFile'].value
505  jecUncertaintyTag = self._parameters['jecUncertaintyTag'].value
506  varyByNsigmas = self._parameters['varyByNsigmas'].value
507  addToPatDefaultSequence = self._parameters['addToPatDefaultSequence'].value
508  outputModule = self._parameters['outputModule'].value
509  postfix = self._parameters['postfix'].value
510 
511  if not hasattr(process, "pf%sNoPUMEtUncertaintySequence%s" % (chsLabel, postfix)):
512  metUncertaintySequence = cms.Sequence()
513  setattr(process, "pf%sNoPUMEtUncertaintySequence%s" % (chsLabel, postfix), metUncertaintySequence)
514  metUncertaintySequence = getattr(process, "pf%sNoPUMEtUncertaintySequence%s" % (chsLabel, postfix))
515 
516  collectionsToKeep = []
517 
518  lastJetCollection = jetCollection.value()
519 
520  # smear jet energies to account for difference in jet resolutions between MC and Data
521  # (cf. JME-10-014 PAS)
522  if isValidInputTag(jetCollection):
523  if doSmearJets:
524  lastJetCollection = \
525  addSmearedJets(process, lastJetCollection, [ "smeared", jetCollection.value(), "ForPFNoPU%sMEt" % chsLabel ],
526  jetSmearFileName, jetSmearHistogram, jetResolutions, varyByNsigmas, None,
527  sequence = metUncertaintySequence, postfix = postfix)
528 
529  collectionsToKeep.append(lastJetCollection)
530 
531  #--------------------------------------------------------------------------------------------
532  # produce collection of electrons/photons, muons, tau-jet candidates and jets
533  # shifted up/down in energy by their respective energy uncertainties
534  #--------------------------------------------------------------------------------------------
535 
536  shiftedParticleSequence, shiftedParticleCollections, addCollectionsToKeep = \
537  self._addShiftedParticleCollections(process,
538  electronCollection.value(),
539  photonCollection.value(),
540  muonCollection.value(),
541  tauCollection.value(),
542  jetCollection.value(), lastJetCollection, lastJetCollection,
543  doSmearJets,
544  jetCorrLabelUpToL3, jetCorrLabelUpToL3Res,
545  jecUncertaintyFile, jecUncertaintyTag,
546  jetSmearFileName, jetSmearHistogram,
547  varyByNsigmas,
548  "ForPFNoPU%sMEt%s" % (chsLabel, postfix))
549  setattr(process, "shiftedParticlesForPFNoPU%sMEtUncertainties%s" % (chsLabel, postfix), shiftedParticleSequence)
550  metUncertaintySequence += getattr(process, "shiftedParticlesForPFNoPU%sMEtUncertainties%s" % (chsLabel, postfix))
551  collectionsToKeep.extend(addCollectionsToKeep)
552 
553  #--------------------------------------------------------------------------------------------
554  # propagate shifted particle energies to No-PU MET
555  #--------------------------------------------------------------------------------------------
556 
557  self._addPFNoPUMEt(process, metUncertaintySequence,
558  shiftedParticleCollections, pfCandCollection, doApplyUnclEnergyCalibration,
559  sfNoPUjetOffsetEnCorr,
560  collectionsToKeep,
561  doApplyChargedHadronSubtraction,
562  doSmearJets,
563  jecUncertaintyFile, jecUncertaintyTag,
564  varyByNsigmas,
565  postfix)
566 
567  # insert metUncertaintySequence into patDefaultSequence
568  if addToPatDefaultSequence:
569  if not hasattr(process, "patDefaultSequence"):
570  raise ValueError("PAT default sequence is not defined !!")
571  process.patDefaultSequence += metUncertaintySequence
572 
573  # add shifted + unshifted collections pf pat::Electrons/Photons,
574  # Muons, Taus, Jets and MET to PAT-tuple event content
575  if outputModule is not None and hasattr(process, outputModule):
576  getattr(process, outputModule).outputCommands = _addEventContent(
577  getattr(process, outputModule).outputCommands,
578  [ 'keep *_%s_*_%s' % (collectionToKeep, process.name_()) for collectionToKeep in collectionsToKeep ])
579 
580 runNoPileUpMEtUncertainties = RunNoPileUpMEtUncertainties()
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
def _addEventContent
Definition: trigTools.py:29