Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:12

0001 import FWCore.ParameterSet.Config as cms
0002 
0003 from PhysicsTools.PatAlgos.tools.ConfigToolBase import *
0004 
0005 import PhysicsTools.PatAlgos.tools.helpers as configtools
0006 from PhysicsTools.PatAlgos.tools.trigTools import _addEventContent
0007 from PhysicsTools.PatUtils.tools.jmeUncertaintyTools import JetMEtUncertaintyTools
0008 
0009 from PhysicsTools.PatUtils.patPFMETCorrections_cff import *
0010 import RecoMET.METProducers.METSigParams_cfi as jetResolutions
0011 from PhysicsTools.PatAlgos.producersLayer1.metProducer_cfi import patMETs
0012 
0013 class RunJetUncertainties(JetMEtUncertaintyTools):
0014 
0015     """ Produce collection of pat::Jets with jet energy and resolution shifted up/down,
0016         in order to estimate effect of systematic uncertainties
0017     """
0018     _label='runJetUncertainties'
0019     _defaultParameters = dicttypes.SortedKeysDict()
0020     def __init__(self):
0021         JetMEtUncertaintyTools.__init__(self)
0022         self.addParameter(self._defaultParameters, 'dRjetCleaning', 0.5, 
0023                           "Eta-phi distance for extra jet cleaning", Type=float)
0024         self._parameters = copy.deepcopy(self._defaultParameters)
0025         self._comment = ""
0026 
0027     def __call__(self, process,
0028                  electronCollection      = None,
0029                  photonCollection        = None,
0030                  muonCollection          = None,
0031                  tauCollection           = None,
0032                  jetCollection           = None,
0033                  dRjetCleaning           = None,
0034                  jetCorrLabel            = None,
0035                  doSmearJets             = None,
0036                  jetSmearFileName        = None,
0037                  jetSmearHistogram       = None,
0038                  jetCorrPayloadName      = None,
0039                  jetCorrLabelUpToL3      = None,
0040                  jetCorrLabelUpToL3Res   = None,
0041                  jecUncertaintyFile      = None,
0042                  jecUncertaintyTag       = None,
0043                  varyByNsigmas           = None,
0044                  addToPatDefaultSequence = None,
0045                  outputModule            = None,
0046                  postfix                 = None):
0047         JetMEtUncertaintyTools.__call__(
0048             self, process,
0049             electronCollection = electronCollection,
0050             photonCollection = photonCollection,
0051             muonCollection = muonCollection,
0052             tauCollection = tauCollection,
0053             jetCollection = jetCollection,
0054             jetCorrLabel = jetCorrLabel,
0055             doSmearJets = doSmearJets,
0056             jetSmearFileName = jetSmearFileName,
0057             jetSmearHistogram = jetSmearHistogram,
0058             jetCorrPayloadName = jetCorrPayloadName,
0059             jetCorrLabelUpToL3 = jetCorrLabelUpToL3,
0060             jetCorrLabelUpToL3Res = jetCorrLabelUpToL3Res,
0061             jecUncertaintyFile = jecUncertaintyFile,
0062             jecUncertaintyTag = jecUncertaintyTag,
0063             varyByNsigmas = varyByNsigmas,
0064             addToPatDefaultSequence = addToPatDefaultSequence,
0065             outputModule = outputModule,
0066             postfix = postfix)
0067         if dRjetCleaning is None:
0068             dRjetCleaning = self._defaultParameters['dRjetCleaning'].value
0069 
0070         self.setParameter('dRjetCleaning', dRjetCleaning)
0071 
0072         self.apply(process) 
0073 
0074     def toolCode(self, process):        
0075         electronCollection = self._parameters['electronCollection'].value
0076         photonCollection = self._parameters['photonCollection'].value
0077         muonCollection = self._parameters['muonCollection'].value
0078         tauCollection = self._parameters['tauCollection'].value
0079         jetCollection = self._parameters['jetCollection'].value
0080         jetCorrLabel = self._parameters['jetCorrLabel'].value
0081         dRjetCleaning =  self._parameters['dRjetCleaning'].value
0082         doSmearJets = self._parameters['doSmearJets'].value
0083         jetSmearFileName = self._parameters['jetSmearFileName'].value
0084         jetSmearHistogram = self._parameters['jetSmearHistogram'].value
0085         jetCorrPayloadName = self._parameters['jetCorrPayloadName'].value
0086         jetCorrLabelUpToL3 = self._parameters['jetCorrLabelUpToL3'].value
0087         jetCorrLabelUpToL3Res = self._parameters['jetCorrLabelUpToL3Res'].value
0088         jecUncertaintyFile = self._parameters['jecUncertaintyFile'].value
0089         jecUncertaintyTag = self._parameters['jecUncertaintyTag'].value
0090         varyByNsigmas = self._parameters['varyByNsigmas'].value
0091         addToPatDefaultSequence = self._parameters['addToPatDefaultSequence'].value
0092         outputModule = self._parameters['outputModule'].value
0093         postfix = self._parameters['postfix'].value
0094 
0095         if not hasattr(process, "jetUncertaintySequence" + postfix):
0096             jetUncertaintySequence = cms.Sequence()
0097             setattr(process, "jetUncertaintySequence" + postfix, jetUncertaintySequence)
0098         jetUncertaintySequence = getattr(process, "jetUncertaintySequence" + postfix)
0099 
0100         collectionsToKeep = []
0101 
0102         # produce collection of jets not overlapping with reconstructed
0103         # electrons/photons, muons and tau-jet candidates
0104         lastJetCollection, cleanedJetCollection = \
0105             self._addCleanedJets(process, jetCollection,
0106                                  electronCollection, photonCollection, muonCollection, tauCollection,
0107                                  jetUncertaintySequence, postfix)
0108 
0109         # smear jet energies to account for difference in jet resolutions between MC and Data
0110         # (cf. JME-10-014 PAS)        
0111         jetCollectionResUp = None
0112         jetCollectionResDown = None
0113         if doSmearJets:
0114             lastJetCollection = \
0115               self._addSmearedJets(process, cleanedJetCollection, [ "smeared", jetCollection.value() ],
0116                                    jetSmearFileName, jetSmearHistogram, varyByNsigmas,
0117                                    uncertaintySequence = jetUncertaintySequence, postfix = postfix)                
0118             jetCollectionResUp = \
0119               self._addSmearedJets(process, cleanedJetCollection, [ "smeared", jetCollection.value(), "ResUp" ],
0120                                    jetSmearFileName, jetSmearHistogram, varyByNsigmas, -1., 
0121                                    uncertaintySequence = jetUncertaintySequence, postfix = postfix)
0122             collectionsToKeep.append(jetCollectionResUp)
0123             jetCollectionResDown = \
0124               self._addSmearedJets(process, cleanedJetCollection, [ "smeared", jetCollection.value(), "ResDown" ],
0125                                    jetSmearFileName, jetSmearHistogram, varyByNsigmas, +1., 
0126                                    uncertaintySequence = jetUncertaintySequence, postfix = postfix)
0127             collectionsToKeep.append(jetCollectionResDown)
0128 
0129         collectionsToKeep.append(lastJetCollection)
0130 
0131         #--------------------------------------------------------------------------------------------    
0132         # produce collection of electrons/photons, muons, tau-jet candidates and jets
0133         # shifted up/down in energy by their respective energy uncertainties
0134         #--------------------------------------------------------------------------------------------
0135 
0136         shiftedParticleSequence, shiftedParticleCollections, addCollectionsToKeep = \
0137           self._addShiftedParticleCollections(process,
0138                                               None,
0139                                               None,
0140                                               None,
0141                                               None,
0142                                               jetCollection, cleanedJetCollection, lastJetCollection,
0143                                               jetCollectionResUp, jetCollectionResDown,
0144                                               jetCorrLabelUpToL3, jetCorrLabelUpToL3Res,
0145                                               jecUncertaintyFile, jecUncertaintyTag,
0146                                               varyByNsigmas,
0147                                               postfix)
0148         setattr(process, "shiftedParticlesForJetUncertainties" + postfix, shiftedParticleSequence)        
0149         jetUncertaintySequence += getattr(process, "shiftedParticlesForJetUncertainties" + postfix)
0150         collectionsToKeep.extend(addCollectionsToKeep)
0151 
0152         # insert metUncertaintySequence into patDefaultSequence
0153         if addToPatDefaultSequence:
0154             if not hasattr(process, "patDefaultSequence"):
0155                 raise ValueError("PAT default sequence is not defined !!")
0156             process.patDefaultSequence += jetUncertaintySequence        
0157 
0158         # add shifted + unshifted collections pf pat::Electrons/Photons,
0159         # Muons, Taus, Jets and MET to PAT-tuple event content
0160         if outputModule is not None and hasattr(process, outputModule):
0161             getattr(process, outputModule).outputCommands = _addEventContent(
0162                 getattr(process, outputModule).outputCommands,
0163                 [ 'keep *_%s_*_%s' % (collectionToKeep, process.name_()) for collectionToKeep in collectionsToKeep ])
0164 
0165 runJetUncertainties = RunJetUncertainties()