def applyElectronRegression(self): """ Apply electron regression from https://twiki.cern.ch/twiki/bin/view/CMS/EGMRegression """ if self.verbose: print("") print("Applying electron regression...") # Read corrections for database from EgammaAnalysis.ElectronTools.regressionWeights_cfi import regressionWeights regressionWeights(self.process) self.process.load( 'EgammaAnalysis.ElectronTools.regressionApplication_cff') # Rename the collection self.process.slimmedElectronsWithRegression = self.process.slimmedElectrons.clone( ) # Look for producers using the default electron input for producer in self.producers: p = getattr(self.process.framework.producers, producer) change_input_tags_and_strings(p, self.__miniaod_electron_collection, 'slimmedElectronsWithRegression', 'producers.' + producer, ' ') self.__miniaod_electron_collection = 'slimmedElectronsWithRegression' if self.verbose: print("New electrons collection: %r" % (self.__miniaod_electron_collection))
def applyMuonCorrection(self, type, tag=None, input=None): """ Apply correction to muon Parameters: type: either "rochester" or "kamuca". Define the type of correction to apply tag: KaMuCa tag (required for KaMuCa correction) input: Rochester input (required for Rochester correction) """ supported = ['kamuca', 'rochester'] if not type in supported: raise Exception( "Unsupported muon correction. Can only be one of %s" % supported) if self.verbose: print("") print("Applying %s correction to muons..." % type.capitalize()) if type == "kamuca": if not tag: raise Exception( "KaMuCa correction requested, but no tag given") if ((self.isData and not tag.startswith("DATA_")) or (not self.isData and not tag.startswith("MC_"))): raise Exception( "KaMuCa tags for data should start with 'DATA_', for MC with 'MC_', got '{0}' and this is {1}" .format(tag, ("data" if self.isData else "MC"))) self.process.slimmedMuonsCorrected = cms.EDProducer( 'KaMuCaCorrectedPATMuonProducer', src=cms.InputTag(self.__miniaod_muon_collection), enabled=cms.bool(True), tag=cms.string(tag)) elif type == "rochester": if not input: raise Exception( "Rochester correction requested, but no input given") self.process.slimmedMuonsCorrected = cms.EDProducer( 'RochesterCorrectedPATMuonProducer', src=cms.InputTag(self.__miniaod_muon_collection), enabled=cms.bool(True), input=cms.FileInPath(input)) self.path.associate(cms.Task(self.process.slimmedMuonsCorrected)) # Look for producers using the default muon input for producer in self.producers: p = getattr(self.process.framework.producers, producer) change_input_tags_and_strings(p, self.__miniaod_muon_collection, 'slimmedMuonsCorrected', 'producers.' + producer, ' ') self.__miniaod_muon_collection = 'slimmedMuonsCorrected' if self.verbose: print("New muons collection: %r" % (self.__miniaod_muon_collection))
def applyElectronSmearing(self, tag): """ Apply electron smearing from https://twiki.cern.ch/twiki/bin/view/CMS/EGMSmearer Parameters: tag: correction file to use (from EgammaAnalysis.ElectronTools.calibrationTablesRun2.files) """ if self.verbose: print("") print("Applying electron smearing...") from EgammaAnalysis.ElectronTools.calibratedPatElectronsRun2_cfi import calibratedPatElectrons from EgammaAnalysis.ElectronTools.calibrationTablesRun2 import files # FIXME: Add a preselection on electron to prevent a crash in the producer # Remove when it's no longer needed (see twiki) self.process.selectedElectrons = cms.EDFilter( "PATElectronSelector", src=cms.InputTag(self.__miniaod_electron_collection), cut=cms.string("pt > 5 && abs(superCluster.eta) < 2.5")) self.process.slimmedElectronsSmeared = calibratedPatElectrons.clone( electrons="selectedElectrons", isMC=not self.isData, correctionFile=files[tag]) self.process.load('Configuration.StandardSequences.Services_cff') self.process.RandomNumberGeneratorService = cms.Service( "RandomNumberGeneratorService", slimmedElectronsSmeared=cms.PSet( initialSeed=cms.untracked.uint32(42), engineName=cms.untracked.string('TRandom3'))) self.path.associate( cms.Task(self.process.selectedElectrons, self.process.slimmedElectronsSmeared, self.process.RandomNumberGeneratorService)) # Look for producers using the default electron input for producer in self.producers: p = getattr(self.process.framework.producers, producer) change_input_tags_and_strings(p, self.__miniaod_electron_collection, 'slimmedElectronsSmeared', 'producers.' + producer, ' ') self.__miniaod_electron_collection = 'slimmedElectronsSmeared' if self.verbose: print("New electrons collection: %r" % (self.__miniaod_electron_collection))
def run(self): upModule = self.getJetSystematicsProducer_(+1) downModule = self.getJetSystematicsProducer_(-1) setattr(self.process, self.formatModuleName(self.jetCollection, 'Up'), upModule) setattr(self.process, self.formatModuleName(self.jetCollection, 'Down'), downModule) # Shifted MET upMetModule = self.addShiftedMETProducer_(upModule.label(), 'Up') downMetModule = self.addShiftedMETProducer_(downModule.label(), 'Down') # Add Framework producers for these new jets collections self.addJetsProducer(upModule.label(), 'Up') self.addJetsProducer(downModule.label(), 'Down') self.addMETProducer(upMetModule.label(), 'Up') self.addMETProducer(downMetModule.label(), 'Down') upJetProducer = self.formatModuleVariableName('jets', 'Up') downJetProducer = self.formatModuleVariableName('jets', 'Down') upMetProducer = self.formatModuleVariableName('met', 'Up') downMetProducer = self.formatModuleVariableName('met', 'Down') if self.framework.verbose: print(" Adding new CMSSW module %r as %r framework producer") % (upModule.label(), upJetProducer) print(" Adding new CMSSW module %r as %r framework producer") % (downModule.label(), downJetProducer) print(" Adding new CMSSW module %r as %r framework producer") % (upMetModule.label(), upMetProducer) print(" Adding new CMSSW module %r as %r framework producer") % (downMetModule.label(), downMetProducer) analyzers = [] for analyzer in self.framework.analyzers: module = getattr(self.process.framework.analyzers, analyzer) if module_has_string(module, 'jets') or module_has_string(module, 'met'): analyzers += [(analyzer, module)] for analyzer, cfg in analyzers: upCfg = copy.deepcopy(cfg) downCfg = copy.deepcopy(cfg) upAnalyzer = self.formatModuleVariableName(analyzer, 'Up') downAnalyzer = self.formatModuleVariableName(analyzer, 'Down') if self.framework.verbose: print " Cloning %r analyzer to %r to run over new collections" % (analyzer, upAnalyzer) padding = ' ' * 8 change_input_tags_and_strings(upCfg, 'jets', upJetProducer, analyzer, padding) change_input_tags_and_strings(upCfg, 'met', upMetProducer, analyzer, padding) if self.framework.verbose: print " Cloning %r analyzer to %r to run over new collections" % (analyzer, downAnalyzer) change_input_tags_and_strings(downCfg, 'jets', downJetProducer, analyzer, padding) change_input_tags_and_strings(downCfg, 'met', downMetProducer, analyzer, padding) upCfg.prefix = self.formatModuleVariableName(upCfg.prefix.value(), 'Up') + '_' downCfg.prefix = self.formatModuleVariableName(downCfg.prefix.value(), 'Down') + '_' index = self.framework.getAnalyzerIndex(analyzer) self.framework.addAnalyzer(upAnalyzer, upCfg, index + 1) self.framework.addAnalyzer(downAnalyzer, downCfg, index + 2)
def applyMuonCorrection(self, type): """ Apply correction to muon Parameters: type: either "rochester" or "kamuca". Define the type of correction to apply """ supported = ['kamuca', 'rochester'] if not type in supported: raise Exception( "Unsupported muon correction. Can only be one of %s" % supported) if self.verbose: print("") print("Applying %s correction to muons..." % type.capitalize()) if type == "kamuca": self.process.slimmedMuonsCorrected = cms.EDProducer( 'KaMuCaCorrectedPATMuonProducer', src=cms.InputTag(self.__miniaod_muon_collection), enabled=cms.bool(True), tag=cms.string(self.__kamuca_tag)) elif type == "rochester": self.process.slimmedMuonsCorrected = cms.EDProducer( 'RochesterCorrectedPATMuonProducer', src=cms.InputTag(self.__miniaod_muon_collection), enabled=cms.bool(True), input=cms.FileInPath(self.__rochester_input)) # Look for producers using the default muon input for producer in self.producers: p = getattr(self.process.framework.producers, producer) change_input_tags_and_strings(p, self.__miniaod_muon_collection, 'slimmedMuonsCorrected', 'producers.' + producer, ' ') self.__miniaod_muon_collection = 'slimmedMuonsCorrected' if self.verbose: print("New muons collection: %r" % (self.__miniaod_muon_collection))
def smearJets(self, resolutionFile=None, scaleFactorFile=None): """ Smear the jets """ if self.isData: return useTxtFiles = resolutionFile and scaleFactorFile if self.verbose: print("") print("Smearing jets...") if useTxtFiles: print(" -> {}".format(resolutionFile)) print(" -> {}".format(scaleFactorFile)) self.process.slimmedJetsSmeared = cms.EDProducer( 'SmearedPATJetProducer', src=cms.InputTag(self.__miniaod_jet_collection), enabled=cms.bool(True), rho=cms.InputTag("fixedGridRhoFastjetAll"), algo=cms.string('AK4PFchs'), algopt=cms.string('AK4PFchs_pt'), genJets=cms.InputTag(self.__miniaod_gen_jet_collection), dRMax=cms.double(0.2), dPtMaxFactor=cms.double(3), variation=cms.int32(0)) if useTxtFiles: del self.process.slimmedJetsSmeared.algo del self.process.slimmedJetsSmeared.algopt self.process.slimmedJetsSmeared.resolutionFile = cms.FileInPath( resolutionFile) self.process.slimmedJetsSmeared.scaleFactorFile = cms.FileInPath( scaleFactorFile) self.__jer_resolution_file = resolutionFile self.__jer_scalefactor_file = scaleFactorFile self.process.shiftedMETCorrModuleForSmearedJets = cms.EDProducer( 'ShiftedParticleMETcorrInputProducer', srcOriginal=cms.InputTag(self.__miniaod_jet_collection), srcShifted=cms.InputTag('slimmedJetsSmeared')) self.process.slimmedMETsSmeared = cms.EDProducer( 'CorrectedPATMETProducer', src=cms.InputTag(self.__miniaod_met_collection), srcCorrections=cms.VInputTag( cms.InputTag('shiftedMETCorrModuleForSmearedJets'))) self.path.associate( cms.Task(self.process.slimmedJetsSmeared, self.process.shiftedMETCorrModuleForSmearedJets, self.process.slimmedMETsSmeared)) # Look for producers using the default jet and met collections for producer in self.producers: p = getattr(self.process.framework.producers, producer) change_input_tags_and_strings(p, self.__miniaod_jet_collection, 'slimmedJetsSmeared', 'producers.' + producer, ' ') change_input_tags_and_strings(p, self.__miniaod_met_collection, 'slimmedMETsSmeared', 'producers.' + producer, ' ') if p.type == 'met': p.parameters.slimmed = cms.untracked.bool(False) self.__miniaod_jet_collection = 'slimmedJetsSmeared' self.__miniaod_met_collection = 'slimmedMETsSmeared' if self.verbose: print( "New jets and MET collections: %r and %r" % (self.__miniaod_jet_collection, self.__miniaod_met_collection))
def redoJEC(self, JECDatabase=None, addBtagDiscriminators=None): """ Redo the Jet Energy Corrections for the default AK4 jet collection, and optionally add some b-tags (which implies redoing the JEC, since b-tagging needs to be run on uncalibrated jets). FIXME: Some love is needed for AK8 jets """ if self.verbose: print("") print("Redoing Jet Energy Corrections{}".format( "" if addBtagDiscriminators is None else ", and adding b-tag classifiers {}". format(", ".join(addBtagDiscriminators)))) if JECDatabase: # Read the JEC from a database self.useJECDatabase(JECDatabase) from cp3_llbb.Framework.Tools import recorrect_jets, recorrect_met jet_collection, prodNames_jet = recorrect_jets( self.process, self.isData, 'AK4PFchs', self.__miniaod_jet_collection, addBtagDiscriminators=addBtagDiscriminators) met_collection, prodNames_met = recorrect_met( self.process, self.isData, self.__miniaod_met_collection, jet_collection) # Fat jets fat_jet_collection, prodNames_fatjet = recorrect_jets( self.process, self.isData, 'AK8PFchs', self.__miniaod_fat_jet_collection, addBtagDiscriminators=addBtagDiscriminators) self.path.associate( cms.Task(*(getattr(self.process, name) for name in chain( prodNames_jet, prodNames_met, prodNames_fatjet)))) # Look for producers using the default jet and met collections for producer in self.producers: p = getattr(self.process.framework.producers, producer) change_input_tags_and_strings(p, self.__miniaod_jet_collection, jet_collection, 'producers.' + producer, ' ') change_input_tags_and_strings(p, self.__miniaod_fat_jet_collection, fat_jet_collection, 'producers.' + producer, ' ') change_input_tags_and_strings(p, self.__miniaod_met_collection, met_collection, 'producers.' + producer, ' ') if p.type == 'met': p.parameters.slimmed = cms.untracked.bool(False) # Change the default collections to the newly created self.__miniaod_jet_collection = jet_collection self.__miniaod_unsmeared_jet_collection = jet_collection self.__miniaod_fat_jet_collection = fat_jet_collection self.__miniaod_met_collection = met_collection if self.verbose: print("New jets and MET collections: %r, %r and %r" % (jet_collection, fat_jet_collection, met_collection)) print("")
def run(self): upModules = self.getJetSystematicsProducers_(+1) downModules = self.getJetSystematicsProducers_(-1) for i in range(len(upModules)): upModule, upModulePostfix = upModules[i] downModule, downModulePostfix = downModules[i] upModulePostfix += "Up" downModulePostfix += "Down" upModuleInputTag = None downModuleInputTag = None if type(upModule) is tuple: assert (len(upModule[0].label()) > 0) upModuleInputTag = cms.InputTag(upModule[0].label(), upModule[1]) downModuleInputTag = cms.InputTag(downModule[0].label(), downModule[1]) else: upModName = self.formatModuleName(self.jetCollection, upModulePostfix) setattr(self.process, upModName, upModule) downModName = self.formatModuleName(self.jetCollection, downModulePostfix) setattr(self.process, downModName, downModule) upModuleInputTag = cms.InputTag(upModule.label()) downModuleInputTag = cms.InputTag(downModule.label()) self.framework.path.associate( cms.Task(*(getattr(self.process, modName) for modName in (upModName, downModName)))) # Shifted MET upMetModule = self.addShiftedMETProducer_(upModuleInputTag.value(), upModulePostfix) downMetModule = self.addShiftedMETProducer_( downModuleInputTag.value(), downModulePostfix) # Add Framework producers for these new jets collections self.addJetsProducer(upModuleInputTag.value(), upModulePostfix) self.addJetsProducer(downModuleInputTag.value(), downModulePostfix) self.addMETProducer(upMetModule.label(), upModulePostfix) self.addMETProducer(downMetModule.label(), downModulePostfix) upJetProducer = self.formatModuleVariableName( 'jets', upModulePostfix) downJetProducer = self.formatModuleVariableName( 'jets', downModulePostfix) upMetProducer = self.formatModuleVariableName( 'met', upModulePostfix) downMetProducer = self.formatModuleVariableName( 'met', downModulePostfix) if self.framework.verbose: print(" Adding new CMSSW module %r as %r framework producer" ) % (upModuleInputTag.value(), upJetProducer) print(" Adding new CMSSW module %r as %r framework producer" ) % (downModuleInputTag.value(), downJetProducer) print(" Adding new CMSSW module %r as %r framework producer" ) % (upMetModule.label(), upMetProducer) print(" Adding new CMSSW module %r as %r framework producer" ) % (downMetModule.label(), downMetProducer) analyzers = [] for analyzer in self.framework.analyzers: module = getattr(self.process.framework.analyzers, analyzer) if module_has_string(module, 'jets') or module_has_string( module, 'met'): analyzers += [(analyzer, module)] for analyzer, cfg in analyzers: upCfg = copy.deepcopy(cfg) downCfg = copy.deepcopy(cfg) upCfg.parameters.systematics = cms.untracked.bool(True) downCfg.parameters.systematics = cms.untracked.bool(True) upAnalyzer = self.formatModuleVariableName( analyzer, upModulePostfix) downAnalyzer = self.formatModuleVariableName( analyzer, downModulePostfix) if self.framework.verbose: print " Cloning %r analyzer to %r to run over new collections" % ( analyzer, upAnalyzer) padding = ' ' * 8 change_input_tags_and_strings(upCfg, 'jets', upJetProducer, analyzer, padding) change_input_tags_and_strings(upCfg, 'met', upMetProducer, analyzer, padding) if self.framework.verbose: print " Cloning %r analyzer to %r to run over new collections" % ( analyzer, downAnalyzer) change_input_tags_and_strings(downCfg, 'jets', downJetProducer, analyzer, padding) change_input_tags_and_strings(downCfg, 'met', downMetProducer, analyzer, padding) upCfg.prefix = self.formatModuleVariableName( upCfg.prefix.value(), upModulePostfix) + '_' downCfg.prefix = self.formatModuleVariableName( downCfg.prefix.value(), downModulePostfix) + '_' index = self.framework.getAnalyzerIndex( analyzer, skipDepsCheck=framework_skipDepChecks) self.framework.addAnalyzer( upAnalyzer, upCfg, index + 1, skipDepsCheck=framework_skipDepChecks) self.framework.addAnalyzer( downAnalyzer, downCfg, index + 2, skipDepsCheck=framework_skipDepChecks)