import FWCore.ParameterSet.Config as cms
from PhysicsTools.NanoAOD.nano_eras_cff import *
from PhysicsTools.NanoAOD.common_cff import *
import PhysicsTools.PatAlgos.producersLayer1.electronProducer_cfi
from math import ceil,log
#NOTE: All definitions of modules should point to the latest flavour of the electronTable in NanoAOD.
#Common modifications for past eras are done at the end whereas modifications specific to a single era is done after the original definition.
from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import egamma8XObjectUpdateModifier,egamma9X105XUpdateModifier,prependEgamma8XObjectUpdateModifier
ele9X105XUpdateModifier=egamma9X105XUpdateModifier.clone(
    phoPhotonIso = "",
    phoNeutralHadIso = "",
    phoChargedHadIso = "",
    phoChargedHadWorstVtxIso = "",
    phoChargedHadWorstVtxConeVetoIso = "",
    phoChargedHadPFPVIso = ""
)
#we have dataformat changes to 106X so to read older releases we use egamma updators
slimmedElectronsTo106X = cms.EDProducer("ModifiedElectronProducer",
    src = cms.InputTag("slimmedElectrons"),
    modifierConfig = cms.PSet( modifications = cms.VPSet(ele9X105XUpdateModifier) )
)
#might as well fix 80X while we're at it although the differences are not so relavent for nano
run2_miniAOD_80XLegacy.toModify( slimmedElectronsTo106X.modifierConfig.modifications, prependEgamma8XObjectUpdateModifier )

# this below is used only in some eras
slimmedElectronsUpdated = cms.EDProducer("PATElectronUpdater",
    src = cms.InputTag("slimmedElectrons"),
    vertices = cms.InputTag("offlineSlimmedPrimaryVertices"),
    computeMiniIso = cms.bool(False),
    fixDxySign = cms.bool(True),
    pfCandsForMiniIso = cms.InputTag("packedPFCandidates"),
from Configuration.Eras.Modifier_run2_miniAOD_80XLegacy_cff import run2_miniAOD_80XLegacy
from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import (
    egamma8XObjectUpdateModifier,
    egamma9X105XUpdateModifier,
)
from RecoEgamma.ElectronIdentification.heepIdVarValueMapProducer_cfi import heepIDVarValueMaps

heepIDVarValueMapsAOD = heepIDVarValueMaps.copy()
heepIDVarValueMapsAOD.dataFormat = 1

gsfElectron8XObjectUpdateModifier = egamma8XObjectUpdateModifier.clone(
    ecalRecHitsEB="reducedEcalRecHitsEB", ecalRecHitsEE="reducedEcalRecHitsEE")
gsfElectron9X105XUpdateModifier = egamma9X105XUpdateModifier.clone(
    eleCollVMsAreKeyedTo="gedGsfElectrons",
    eleTrkIso="heepIDVarValueMapsAOD:eleTrkPtIso",
    eleTrkIso04="heepIDVarValueMapsAOD:eleTrkPtIso04",
    conversions="allConversions",
    ecalRecHitsEB="reducedEcalRecHitsEB",
    ecalRecHitsEE="reducedEcalRecHitsEE",
)

# we have dataformat changes to 106X so to read older releases we use egamma updators
gedGsfElectronsFrom80XTo106X = cms.EDProducer(
    "ModifiedGsfElectronProducer",
    src=cms.InputTag("gedGsfElectrons"),
    modifierConfig=cms.PSet(modifications=cms.VPSet(
        gsfElectron8XObjectUpdateModifier, gsfElectron9X105XUpdateModifier)),
)

gedGsfElectronsFrom80XTo106XTask = cms.Task(heepIDVarValueMapsAOD,
                                            gedGsfElectronsFrom80XTo106X)