def makeEgammaPATWithUserData(process,
                              eleTag=None,
                              phoTag=None,
                              runVID=True,
                              runEnergyCorrections=True,
                              era="2017-Nov17ReReco",
                              suffex="WithUserData"):
    """
    This function embeds the value maps into a pat::Electron,pat::Photon
    This function is not officially supported by e/gamma and is on a best effort bais
    eleTag and phoTag are type cms.InputTag
    outputs new collection with {eleTag/phoTag}.moduleLabel + suffex 
    """
    from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import egamma_modifications, egamma8XLegacyEtScaleSysModifier, egamma8XObjectUpdateModifier
    from RecoEgamma.EgammaTools.egammaObjectModifications_tools import makeVIDBitsModifier, makeVIDinPATIDsModifier, makeEnergyScaleAndSmearingSysModifier
    if runVID:
        egamma_modifications.append(
            makeVIDBitsModifier(process, "egmGsfElectronIDs", "egmPhotonIDs"))
        egamma_modifications.append(
            makeVIDinPATIDsModifier(process, "egmGsfElectronIDs",
                                    "egmPhotonIDs"))
    else:
        egamma_modifications = cms.VPSet(
        )  #reset all the modifications which so far are just VID
    if _isInputFrom80X(era):
        egamma_modifications.append(
            egamma8XObjectUpdateModifier
        )  #if we were generated in 80X, we need fill in missing data members in 94X
    if runEnergyCorrections:
        egamma_modifications.append(
            makeEnergyScaleAndSmearingSysModifier("calibratedElectrons",
                                                  "calibratedPhotons"))
        egamma_modifications.append(egamma8XLegacyEtScaleSysModifier)

    process.egammaPostRecoPatUpdatorTask = cms.Task()

    if eleTag:
        modName = eleTag.moduleLabel + suffex
        setattr(
            process, modName,
            cms.EDProducer(
                "ModifiedElectronProducer",
                src=eleTag,
                modifierConfig=cms.PSet(modifications=egamma_modifications)))
        process.egammaPostRecoPatUpdatorTask.add(getattr(process, modName))

    if phoTag:
        modName = phoTag.moduleLabel + suffex
        setattr(
            process, modName,
            cms.EDProducer(
                "ModifiedPhotonProducer",
                src=phoTag,
                modifierConfig=cms.PSet(modifications=egamma_modifications)))
        process.egammaPostRecoPatUpdatorTask.add(getattr(process, modName))

    process.egammaPostRecoPatUpdatorSeq = cms.Sequence(
        process.egammaPostRecoPatUpdatorTask)
    return process
def _setupEgammaPostVIDUpdator(eleSrc, phoSrc, cfg):
    from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import egamma_modifications, egamma8XLegacyEtScaleSysModifier
    from RecoEgamma.EgammaTools.egammaObjectModifications_tools import makeVIDBitsModifier, makeVIDinPATIDsModifier, makeEnergyScaleAndSmearingSysModifier
    process = cfg.process

    process.egammaPostRecoPatUpdatorTask = cms.Task()

    if not cfg.isMiniAOD:
        return cms.InputTag(eleSrc.value()), cms.InputTag(phoSrc.value())

    if cfg.runVID:
        egamma_modifications.append(
            makeVIDBitsModifier(process, "egmGsfElectronIDs", "egmPhotonIDs"))
        egamma_modifications.append(
            makeVIDinPATIDsModifier(process, "egmGsfElectronIDs",
                                    "egmPhotonIDs"))
    else:
        egamma_modifications = cms.VPSet(
        )  #reset all the modifications which so far are just VID

    if cfg.runEnergyCorrections:
        egamma_modifications.append(
            makeEnergyScaleAndSmearingSysModifier("calibratedPatElectrons",
                                                  "calibratedPatPhotons"))
        egamma_modifications.append(egamma8XLegacyEtScaleSysModifier)

    #add any missing variables to the slimmed electron
    if cfg.runVID:
        #MVA V2 values may not be added by default due to data format consistency issues
        _addMissingMVAValuesToUserData(process, egamma_modifications)
        #now add HEEP trk isolation if old dataformat (new its in the object proper)
        if not _isULDataformat():
            for pset in egamma_modifications:
                if pset.hasParameter(
                        "modifierName") and pset.modifierName == cms.string(
                            'EGExtraInfoModifierFromFloatValueMaps'):
                    pset.electron_config.heepTrkPtIso = cms.InputTag(
                        "heepIDVarValueMaps", "eleTrkPtIso")
                    break

    for pset in egamma_modifications:
        pset.overrideExistingValues = cms.bool(True)
        if hasattr(pset, "electron_config"):
            pset.electron_config.electronSrc = eleSrc
        if hasattr(pset, "photon_config"):
            pset.photon_config.photonSrc = phoSrc

    process.slimmedElectrons = cms.EDProducer(
        "ModifiedElectronProducer",
        src=eleSrc,
        modifierConfig=cms.PSet(modifications=egamma_modifications))
    process.slimmedPhotons = cms.EDProducer(
        "ModifiedPhotonProducer",
        src=phoSrc,
        modifierConfig=cms.PSet(modifications=egamma_modifications))

    #we only run if the modifications are going to do something
    if egamma_modifications != cms.VPSet() or cfg.runningPreVIDUpdator:
        process.egammaPostRecoPatUpdatorTask.add(process.slimmedElectrons)
        process.egammaPostRecoPatUpdatorTask.add(process.slimmedPhotons)
        return eleSrc, phoSrc
    else:
        return cms.InputTag(eleSrc.value()), cms.InputTag(phoSrc.value())
Ejemplo n.º 3
0
def _setupEgammaPostRECOSequenceMiniAOD(process,
                                        applyEnergyCorrections=False,
                                        applyVIDOnCorrectedEgamma=False,
                                        era="2017-Nov17ReReco",
                                        runVID=True,
                                        runEnergyCorrections=True,
                                        applyEPCombBug=False):
    """
    This function loads the calibrated producers calibratedPatElectrons,calibratedPatPhotons, 
    sets VID & other modules to the correct electron/photon source,
    loads up the modifiers and which then creates a new slimmedElectrons,slimmedPhotons collection
    with VID and scale and smearing all loaded in
    """

    if applyEnergyCorrections != applyVIDOnCorrectedEgamma:
        raise RuntimeError(
            'Error, applyEnergyCorrections {} and applyVIDOnCorrectedEgamma {} must be equal to each other for now,\n functionality for them to be different isnt yet availible'
            .format(applyEnergyCorrections, applyVIDOnCorrectedEgamma))

    phoSrc = cms.InputTag('slimmedPhotons',
                          processName=cms.InputTag.skipCurrentProcess())
    eleSrc = cms.InputTag('slimmedElectrons',
                          processName=cms.InputTag.skipCurrentProcess())
    phoCalibSrc = cms.InputTag('calibratedPatPhotons')
    eleCalibSrc = cms.InputTag('calibratedPatElectrons')

    _setupEgammaEnergyCorrectionsMiniAOD(
        process,
        eleSrc=eleSrc,
        phoSrc=phoSrc,
        applyEnergyCorrections=applyEnergyCorrections,
        applyVIDOnCorrectedEgamma=applyVIDOnCorrectedEgamma,
        era=era,
        runEnergyCorrections=runEnergyCorrections,
        applyEPCombBug=applyEPCombBug)

    if applyVIDOnCorrectedEgamma:
        phoVIDSrc = phoCalibSrc
        eleVIDSrc = eleCalibSrc
    else:
        phoVIDSrc = phoSrc
        eleVIDSrc = eleSrc

    if applyEnergyCorrections:
        phoNewSrc = phoCalibSrc
        eleNewSrc = eleCalibSrc
    else:
        phoNewSrc = phoSrc
        eleNewSrc = eleSrc

    if runVID:
        process.egmGsfElectronIDs.physicsObjectSrc = eleVIDSrc
        process.egmPhotonIDs.physicsObjectSrc = phoVIDSrc
        process.electronMVAValueMapProducer.srcMiniAOD = eleVIDSrc
        if hasattr(process, 'electronMVAVariableHelper'):
            process.electronMVAVariableHelper.srcMiniAOD = eleVIDSrc
        process.photonMVAValueMapProducer.srcMiniAOD = phoVIDSrc
        process.photonIDValueMapProducer.srcMiniAOD = phoVIDSrc
        process.egmPhotonIsolation.srcToIsolate = phoVIDSrc

    if runVID and hasattr(process, 'heepIDVarValueMaps'):
        process.heepIDVarValueMaps.elesMiniAOD = eleVIDSrc
        process.heepIDVarValueMaps.dataFormat = 2

    from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import egamma_modifications, egamma8XLegacyEtScaleSysModifier, egamma8XObjectUpdateModifier
    from RecoEgamma.EgammaTools.egammaObjectModifications_tools import makeVIDBitsModifier, makeVIDinPATIDsModifier, makeEnergyScaleAndSmearingSysModifier
    if runVID:
        egamma_modifications.append(
            makeVIDBitsModifier(process, "egmGsfElectronIDs", "egmPhotonIDs"))
        egamma_modifications.append(
            makeVIDinPATIDsModifier(process, "egmGsfElectronIDs",
                                    "egmPhotonIDs"))
    else:
        egamma_modifications = cms.VPSet(
        )  #reset all the modifications which so far are just VID
    if _is80XRelease(era):
        egamma_modifications.append(
            egamma8XObjectUpdateModifier
        )  #if we were generated in 80X, we need fill in missing data members in 94X
    if runEnergyCorrections:
        egamma_modifications.append(
            makeEnergyScaleAndSmearingSysModifier("calibratedPatElectrons",
                                                  "calibratedPatPhotons"))
        egamma_modifications.append(egamma8XLegacyEtScaleSysModifier)

    #add any missing variables to the slimmed electron
    if runVID:
        #MVA V2 values may not be added by default due to data format consistency issues
        _addMissingMVAValuesToUserData(process, egamma_modifications)
        #now add HEEP trk isolation
        for pset in egamma_modifications:
            if pset.hasParameter(
                    "modifierName") and pset.modifierName == cms.string(
                        'EGExtraInfoModifierFromFloatValueMaps'):
                pset.electron_config.heepTrkPtIso = cms.InputTag(
                    "heepIDVarValueMaps", "eleTrkPtIso")
                break

    for pset in egamma_modifications:
        pset.overrideExistingValues = cms.bool(True)
        if hasattr(pset, "electron_config"):
            pset.electron_config.electronSrc = eleNewSrc
        if hasattr(pset, "photon_config"):
            pset.photon_config.photonSrc = phoNewSrc

    process.slimmedElectrons = cms.EDProducer(
        "ModifiedElectronProducer",
        src=eleNewSrc,
        modifierConfig=cms.PSet(modifications=egamma_modifications))
    process.slimmedPhotons = cms.EDProducer(
        "ModifiedPhotonProducer",
        src=phoNewSrc,
        modifierConfig=cms.PSet(modifications=egamma_modifications))

    process.egammaScaleSmearTask = cms.Task()
    process.egammaPostRecoPatUpdatorTask = cms.Task()
    #we only run if the modifications are going to do something
    if egamma_modifications != cms.VPSet():
        process.egammaPostRecoPatUpdatorTask.add(process.slimmedElectrons)
        process.egammaPostRecoPatUpdatorTask.add(process.slimmedPhotons)
        if runEnergyCorrections:
            process.egammaScaleSmearTask.add(process.calibratedPatElectrons)
            process.egammaScaleSmearTask.add(process.calibratedPatPhotons)
def _setupEgammaPostRECOSequenceMiniAOD(process,
                                        applyEnergyCorrections=False,
                                        applyVIDOnCorrectedEgamma=False,
                                        era="2017-Nov17ReReco",
                                        runVID=True,
                                        applyEPCombBug=False):

    if applyEnergyCorrections != applyVIDOnCorrectedEgamma:
        raise RuntimeError(
            'Error, applyEnergyCorrections {} and applyVIDOnCorrectedEgamma {} must be equal to each other for now,\n functionality for them to be different isnt yet availible'
            .format(applyEnergyCorrections, applyVIDOnCorrectedEgamma))

    phoSrc = cms.InputTag('slimmedPhotons',
                          processName=cms.InputTag.skipCurrentProcess())
    eleSrc = cms.InputTag('slimmedElectrons',
                          processName=cms.InputTag.skipCurrentProcess())
    phoCalibSrc = cms.InputTag('slimmedPhotons',
                               processName=cms.InputTag.skipCurrentProcess())
    eleCalibSrc = cms.InputTag('slimmedElectrons',
                               processName=cms.InputTag.skipCurrentProcess())

    process.load('RecoEgamma.EgammaTools.calibratedEgammas_cff')
    process.calibratedPatElectrons.src = eleCalibSrc
    process.calibratedPatPhotons.src = phoCalibSrc

    energyCorrectionFile = _getEnergyCorrectionFile(era)
    process.calibratedPatElectrons.correctionFile = energyCorrectionFile
    process.calibratedPatPhotons.correctionFile = energyCorrectionFile
    if applyEPCombBug:
        process.calibratedPatElectrons.useSmearCorrEcalEnergyErrInComb = True
    else:
        process.calibratedPatElectrons.useSmearCorrEcalEnergyErrInComb = False

    if applyEnergyCorrections and applyVIDOnCorrectedEgamma:
        phoSrc = cms.InputTag('calibratedPatPhotons')
        eleSrc = cms.InputTag('calibratedPatElectrons')
        process.calibratedPatElectrons.produceCalibratedObjs = True
        process.calibratedPatPhotons.produceCalibratedObjs = True
    if not applyEnergyCorrections:
        process.calibratedPatElectrons.produceCalibratedObjs = False
        process.calibratedPatPhotons.produceCalibratedObjs = False

    if runVID:
        process.egmGsfElectronIDs.physicsObjectSrc = eleSrc
        process.egmPhotonIDs.physicsObjectSrc = phoSrc
        process.electronMVAValueMapProducer.srcMiniAOD = eleSrc
        process.photonMVAValueMapProducer.srcMiniAOD = phoSrc
        process.photonIDValueMapProducer.srcMiniAOD = phoSrc
        process.egmPhotonIsolation.srcToIsolate = phoSrc

    if runVID and hasattr(process, 'heepIDVarValueMaps'):
        process.heepIDVarValueMaps.elesMiniAOD = eleSrc
        process.heepIDVarValueMaps.dataFormat = 2

    from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import egamma_modifications, egamma8XLegacyEtScaleSysModifier, egamma8XObjectUpdateModifier
    from RecoEgamma.EgammaTools.egammaObjectModifications_tools import makeVIDBitsModifier, makeVIDinPATIDsModifier, makeEnergyScaleAndSmearingSysModifier
    if _is80XRelease(era):
        egamma_modifications.append(
            egamma8XObjectUpdateModifier
        )  #if we were generated in 80X, we need fill in missing data members in 94X
    if runVID:
        egamma_modifications.append(
            makeVIDBitsModifier(process, "egmGsfElectronIDs", "egmPhotonIDs"))
        egamma_modifications.append(
            makeVIDinPATIDsModifier(process, "egmGsfElectronIDs",
                                    "egmPhotonIDs"))
    else:
        egamma_modifications = cms.VPSet(
        )  #reset all the modifications which so far are just VID
    egamma_modifications.append(
        makeEnergyScaleAndSmearingSysModifier("calibratedPatElectrons",
                                              "calibratedPatPhotons"))
    egamma_modifications.append(egamma8XLegacyEtScaleSysModifier)

    #add the HEEP trk isol to the slimmed electron
    if runVID:
        for pset in egamma_modifications:
            if pset.hasParameter(
                    "modifierName") and pset.modifierName == cms.string(
                        'EGExtraInfoModifierFromFloatValueMaps'):
                pset.electron_config.heepTrkPtIso = cms.InputTag(
                    "heepIDVarValueMaps", "eleTrkPtIso")
                break

    for pset in egamma_modifications:
        pset.overrideExistingValues = cms.bool(True)
        if hasattr(pset, "electron_config"):
            pset.electron_config.electronSrc = eleSrc
        if hasattr(pset, "photon_config"):
            pset.photon_config.photonSrc = phoSrc

    process.slimmedElectrons = cms.EDProducer(
        "ModifiedElectronProducer",
        src=eleSrc,
        modifierConfig=cms.PSet(modifications=egamma_modifications))
    process.slimmedPhotons = cms.EDProducer(
        "ModifiedPhotonProducer",
        src=phoSrc,
        modifierConfig=cms.PSet(modifications=egamma_modifications))
    process.egammaScaleSmearTask = cms.Task(process.calibratedPatElectrons,
                                            process.slimmedElectrons,
                                            process.calibratedPatPhotons,
                                            process.slimmedPhotons)
Ejemplo n.º 5
0
def customizeEGM(process,runOnData):

    # turn on VID producer, indicate data format  to be
    # DataFormat.AOD or DataFormat.MiniAOD, as appropriate 
    dataFormat = DataFormat.MiniAOD
    switchOnVIDElectronIdProducer(process, dataFormat)
    switchOnVIDPhotonIdProducer(process, dataFormat)

    # define which IDs we want to produce
    ele_id_modules =  [ 'RecoEgamma.ElectronIdentification.Identification.cutBasedElectronID_Fall17_94X_V1_cff',
                        'RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V1_cff', 
                        'RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V1_cff'
                        ]
    pho_id_modules =  [ 'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V1_TrueVtx_cff',
                        'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V1_cff', 
                        'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V1p1_cff'
                        ]

    #add them to the VID producer
    for idmod in ele_id_modules:
        setupAllVIDIdsInModule(process,idmod,setupVIDElectronSelection)
    for idmod in pho_id_modules:
        setupAllVIDIdsInModule(process,idmod,setupVIDPhotonSelection)

    process.load('EgammaAnalysis.ElectronTools.calibratedPatElectronsRun2_cfi')
    process.load('EgammaAnalysis.ElectronTools.calibratedPatPhotonsRun2_cfi')
    process.calibratedPatElectrons.electrons = cms.InputTag('slimmedElectrons',processName=cms.InputTag.skipCurrentProcess())
    process.calibratedPatPhotons.photons = cms.InputTag('slimmedPhotons',processName=cms.InputTag.skipCurrentProcess())
    process.egmGsfElectronIDs.physicsObjectSrc = cms.InputTag('calibratedPatElectrons')
    process.egmPhotonIDs.physicsObjectSrc = cms.InputTag('calibratedPatPhotons')
    process.electronMVAValueMapProducer.srcMiniAOD = cms.InputTag('calibratedPatElectrons') 
    process.photonMVAValueMapProducer.srcMiniAOD = cms.InputTag('calibratedPatPhotons')
    process.photonIDValueMapProducer.srcMiniAOD = cms.InputTag('calibratedPatPhotons')
    process.egmPhotonIsolation.srcToIsolate = cms.InputTag('calibratedPatPhotons')
    if hasattr(process,'heepIDVarValueMaps'):
        process.heepIDVarValueMaps.elesMiniAOD = cms.InputTag('calibratedPatElectrons')


    egamma_modifications.append(makeVIDBitsModifier(process,"egmGsfElectronIDs","egmPhotonIDs"))
    egamma_modifications.append(makeVIDinPATIDsModifier(process,"egmGsfElectronIDs","egmPhotonIDs"))
    egamma_modifications.append(makeEnergyScaleAndSmearingSysModifier("calibratedPatElectrons","calibratedPatPhotons"))
    for pset in egamma_modifications:
        pset.overrideExistingValues = cms.bool(True)
        if hasattr(pset,"electron_config"): pset.electron_config.electronSrc = cms.InputTag("calibratedPatElectrons")
        if hasattr(pset,"photon_config"): pset.photon_config.photonSrc = cms.InputTag("calibratedPatPhotons")

    process.slimmedElectrons = cms.EDProducer("ModifiedElectronProducer",
                                              src=cms.InputTag("calibratedPatElectrons"),
                                              modifierConfig = cms.PSet( modifications = egamma_modifications )
                                              )
    process.slimmedPhotons = cms.EDProducer("ModifiedPhotonProducer",
                                            src=cms.InputTag("calibratedPatPhotons"),
                                            modifierConfig = cms.PSet( modifications = cms.VPSet(egamma_modifications) )
                                            )
                                           

    #create the task and sequences
    process.egammaScaleSmearTask = cms.Task(process.calibratedPatElectrons,process.slimmedElectrons,
                                            process.calibratedPatPhotons,process.slimmedPhotons)
    process.egammaScaleSmearSeq = cms.Sequence( process.egammaScaleSmearTask)
    process.egammaScaleSmearAndVIDSeq = cms.Sequence(process.egammaScaleSmearSeq*
                                                     process.egmGsfElectronIDSequence*
                                                     process.egmPhotonIDSequence)