Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-25 02:14:04

0001 import FWCore.ParameterSet.Config as cms
0002 from PhysicsTools.NanoAOD.nano_eras_cff import *
0003 from PhysicsTools.NanoAOD.common_cff import *
0004 from PhysicsTools.NanoAOD.simplePATPhotonFlatTableProducer_cfi import simplePATPhotonFlatTableProducer
0005 from math import ceil,log
0006 
0007 
0008 photon_id_modules_WorkingPoints_nanoAOD = cms.PSet(
0009     modules = cms.vstring(
0010         'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_RunIIIWinter22_122X_V1_cff',
0011         'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Winter22_122X_V1_cff',
0012         # Fall17: need to include the modules too to make sure they are run
0013         'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V2_cff',
0014         'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V2_cff',
0015    ),
0016    WorkingPoints = cms.vstring(
0017      "egmPhotonIDs:cutBasedPhotonID-RunIIIWinter22-122X-V1-loose",
0018      "egmPhotonIDs:cutBasedPhotonID-RunIIIWinter22-122X-V1-medium",
0019      "egmPhotonIDs:cutBasedPhotonID-RunIIIWinter22-122X-V1-tight",
0020    )
0021 )
0022 
0023 photon_id_modules_WorkingPoints_nanoAOD_Run2 = cms.PSet(
0024     modules = cms.vstring(
0025         'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V2_cff',
0026         'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V2_cff',
0027    ),
0028     WorkingPoints = cms.vstring(
0029       "egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-loose",
0030       "egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-medium",
0031       "egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-tight",
0032     )
0033 
0034 )
0035 
0036 # make Fall17 the default one in Run2
0037 run2_egamma.toModify(photon_id_modules_WorkingPoints_nanoAOD,
0038                      modules=photon_id_modules_WorkingPoints_nanoAOD_Run2.modules).\
0039         toModify(photon_id_modules_WorkingPoints_nanoAOD,
0040                  WorkingPoints=photon_id_modules_WorkingPoints_nanoAOD_Run2.WorkingPoints)
0041 
0042 def make_bitmapVID_docstring(id_modules_working_points_pset):
0043     pset = id_modules_working_points_pset
0044 
0045     for modname in pset.modules:
0046         ids = __import__(modname, globals(), locals(), ['idName','cutFlow'])
0047         for name in dir(ids):
0048             _id = getattr(ids,name)
0049             if hasattr(_id,'idName') and hasattr(_id,'cutFlow'):
0050                 if (len(pset.WorkingPoints)>0 and _id.idName == pset.WorkingPoints[0].split(':')[-1]):
0051                     cut_names = ','.join([cut.cutName.value() for cut in _id.cutFlow])
0052                     n_bits_per_cut = int(ceil(log(len(pset.WorkingPoints)+1,2)))
0053                     return 'VID compressed bitmap (%s), %d bits per cut'%(cut_names, n_bits_per_cut)
0054     raise ValueError("Something is wrong in the photon ID modules parameter set!")
0055 
0056 
0057 bitmapVIDForPho = cms.EDProducer("PhoVIDNestedWPBitmapProducer",
0058     src = cms.InputTag("slimmedPhotons"),
0059     srcForID = cms.InputTag("reducedEgamma","reducedGedPhotons"),
0060     WorkingPoints = photon_id_modules_WorkingPoints_nanoAOD.WorkingPoints,
0061 )
0062 _bitmapVIDForPho_docstring = make_bitmapVID_docstring(photon_id_modules_WorkingPoints_nanoAOD)
0063 
0064 bitmapVIDForPhoRun2 = bitmapVIDForPho.clone(
0065     WorkingPoints = photon_id_modules_WorkingPoints_nanoAOD_Run2.WorkingPoints,
0066 )
0067 _bitmapVIDForPhoRun2_docstring = make_bitmapVID_docstring(photon_id_modules_WorkingPoints_nanoAOD_Run2)
0068 
0069 isoForPho = cms.EDProducer("PhoIsoValueMapProducer",
0070     src = cms.InputTag("slimmedPhotons"),
0071     relative = cms.bool(False),
0072     doQuadratic = cms.bool(True),
0073     rho_PFIso = cms.InputTag("fixedGridRhoFastjetAll"),
0074     QuadraticEAFile_PFIso_Chg  = cms.FileInPath("RecoEgamma/PhotonIdentification/data/RunIII_Winter22/effectiveArea_ChgHadronIso_95percentBased.txt"),
0075     QuadraticEAFile_PFIso_ECal = cms.FileInPath("RecoEgamma/PhotonIdentification/data/RunIII_Winter22/effectiveArea_ECalClusterIso_95percentBased.txt"),
0076     QuadraticEAFile_PFIso_HCal = cms.FileInPath("RecoEgamma/PhotonIdentification/data/RunIII_Winter22/effectiveArea_HCalClusterIso_95percentBased.txt"),
0077 )
0078 
0079 hOverEForPho = cms.EDProducer("PhoHoverEValueMapProducer",
0080     src = cms.InputTag("slimmedPhotons"),
0081     relative = cms.bool(False),
0082     rho = cms.InputTag("fixedGridRhoFastjetAll"),
0083     QuadraticEAFile_HoverE = cms.FileInPath("RecoEgamma/PhotonIdentification/data/RunIII_Winter22/effectiveArea_coneBasedHoverE_95percentBased.txt"),
0084 )
0085 
0086 isoForPhoFall17V2 = isoForPho.clone(
0087     doQuadratic = cms.bool(False),
0088     EAFile_PFIso_Chg = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Fall17/effAreaPhotons_cone03_pfChargedHadrons_90percentBased_V2.txt"),
0089     EAFile_PFIso_Neu = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Fall17/effAreaPhotons_cone03_pfNeutralHadrons_90percentBased_V2.txt"),
0090     EAFile_PFIso_Pho = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Fall17/effAreaPhotons_cone03_pfPhotons_90percentBased_V2.txt"),
0091 )
0092 
0093 
0094 seedGainPho = cms.EDProducer("PhotonSeedGainProducer", src = cms.InputTag("slimmedPhotons"))
0095 
0096 import RecoEgamma.EgammaTools.calibratedEgammas_cff
0097 
0098 calibratedPatPhotonsNano = RecoEgamma.EgammaTools.calibratedEgammas_cff.calibratedPatPhotons.clone(
0099     produceCalibratedObjs = False,
0100     correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2016_UltraLegacy_preVFP_RunFineEtaR9Gain"),
0101 )
0102 
0103 (run2_egamma_2016 & tracker_apv_vfp30_2016).toModify(
0104     calibratedPatPhotonsNano,
0105     correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2016_UltraLegacy_preVFP_RunFineEtaR9Gain")
0106 )
0107 
0108 (run2_egamma_2016 & ~tracker_apv_vfp30_2016).toModify(
0109     calibratedPatPhotonsNano,
0110     correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2016_UltraLegacy_postVFP_RunFineEtaR9Gain"),
0111 )
0112 
0113 run2_egamma_2017.toModify(
0114     calibratedPatPhotonsNano,
0115     correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2017_24Feb2020_runEtaR9Gain_v2")
0116 )
0117 
0118 run2_egamma_2018.toModify(
0119     calibratedPatPhotonsNano,
0120     correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2018_29Sep2020_RunFineEtaR9Gain")
0121 )
0122 
0123 slimmedPhotonsWithUserData = cms.EDProducer("PATPhotonUserDataEmbedder",
0124     src = cms.InputTag("slimmedPhotons"),
0125     parentSrcs = cms.VInputTag("reducedEgamma:reducedGedPhotons"),
0126     userFloats = cms.PSet(
0127         mvaID = cms.InputTag("photonMVAValueMapProducer:PhotonMVAEstimatorRunIIIWinter22v1Values"),
0128         PFIsoChgQuadratic = cms.InputTag("isoForPho:PFIsoChgQuadratic"),
0129         PFIsoAllQuadratic = cms.InputTag("isoForPho:PFIsoAllQuadratic"),
0130         HoverEQuadratic = cms.InputTag("hOverEForPho:HoEForPhoEACorr"),
0131         mvaID_Fall17V2 = cms.InputTag("photonMVAValueMapProducer:PhotonMVAEstimatorRunIIFall17v2Values"),
0132         PFIsoChgFall17V2 = cms.InputTag("isoForPhoFall17V2:PFIsoChg"),
0133         PFIsoAllFall17V2 = cms.InputTag("isoForPhoFall17V2:PFIsoAll"),
0134     ),
0135     userIntFromBools = cms.PSet(
0136         cutBasedID_loose  = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-RunIIIWinter22-122X-V1-loose"),
0137         cutBasedID_medium = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-RunIIIWinter22-122X-V1-medium"),
0138         cutBasedID_tight  = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-RunIIIWinter22-122X-V1-tight"),
0139         cutBasedID_Fall17V2_loose  = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-loose"),
0140         cutBasedID_Fall17V2_medium = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-medium"),
0141         cutBasedID_Fall17V2_tight  = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-tight"),
0142         mvaID_WP90 = cms.InputTag("egmPhotonIDs:mvaPhoID-RunIIIWinter22-v1-wp90"),
0143         mvaID_WP80 = cms.InputTag("egmPhotonIDs:mvaPhoID-RunIIIWinter22-v1-wp80"),
0144         mvaID_Fall17V2_WP90 = cms.InputTag("egmPhotonIDs:mvaPhoID-RunIIFall17-v2-wp90"),
0145         mvaID_Fall17V2_WP80 = cms.InputTag("egmPhotonIDs:mvaPhoID-RunIIFall17-v2-wp80"),
0146     ),
0147     userInts = cms.PSet(
0148         VIDNestedWPBitmap = cms.InputTag("bitmapVIDForPho"),
0149         VIDNestedWPBitmapFall17V2 = cms.InputTag("bitmapVIDForPhoRun2"),
0150         seedGain = cms.InputTag("seedGainPho"),
0151        
0152     )
0153 )
0154 
0155 # no need for the Run3 IDs in Run2
0156 run2_egamma.toModify(slimmedPhotonsWithUserData.userFloats,
0157                      mvaID = None,
0158                      PFIsoChgQuadratic = None,
0159                      PFIsoAllQuadratic = None,
0160                      HoverEQuadratic = None).\
0161                 toModify(slimmedPhotonsWithUserData.userIntFromBools,
0162                           cutBasedID_loose = None,
0163                           cutBasedID_medium = None,
0164                           cutBasedID_tight = None,
0165                           mvaID_WP90 = None,
0166                           mvaID_WP80 = None).\
0167                 toModify(slimmedPhotonsWithUserData.userInts,
0168                          VIDNestedWPBitmap = None)
0169 
0170 run2_egamma.toModify(
0171     slimmedPhotonsWithUserData.userFloats,
0172     ecalEnergyErrPostCorrNew = cms.InputTag("calibratedPatPhotonsNano","ecalEnergyErrPostCorr"),
0173     ecalEnergyPreCorrNew     = cms.InputTag("calibratedPatPhotonsNano","ecalEnergyPreCorr"),
0174     ecalEnergyPostCorrNew    = cms.InputTag("calibratedPatPhotonsNano","ecalEnergyPostCorr"),
0175     energyScaleUpNew            = cms.InputTag("calibratedPatPhotonsNano","energyScaleUp"),
0176     energyScaleDownNew          = cms.InputTag("calibratedPatPhotonsNano","energyScaleDown"),
0177     energySigmaUpNew            = cms.InputTag("calibratedPatPhotonsNano","energySigmaUp"),
0178     energySigmaDownNew          = cms.InputTag("calibratedPatPhotonsNano","energySigmaDown"),
0179 )
0180 
0181 
0182 finalPhotons = cms.EDFilter("PATPhotonRefSelector",
0183     src = cms.InputTag("slimmedPhotonsWithUserData"),
0184     cut = cms.string("pt > 5 ")
0185 )
0186 
0187 photonTable = simplePATPhotonFlatTableProducer.clone(
0188     src = cms.InputTag("linkedObjects","photons"),
0189     name= cms.string("Photon"),
0190     doc = cms.string("slimmedPhotons after basic selection (" + finalPhotons.cut.value()+")"),
0191     variables = cms.PSet(P3Vars,
0192         jetIdx = Var("?hasUserCand('jet')?userCand('jet').key():-1", "int16", doc="index of the associated jet (-1 if none)"),
0193         electronIdx = Var("?hasUserCand('electron')?userCand('electron').key():-1", "int16", doc="index of the associated electron (-1 if none)"),
0194         energyErr = Var("getCorrectedEnergyError('regression2')",float,doc="energy error of the cluster from regression",precision=10),
0195         energyRaw = Var("superCluster().rawEnergy()",float,doc="raw energy of photon supercluster", precision=10),
0196         superclusterEta  = Var("superCluster().eta()",float,doc="supercluster eta",precision=10),
0197         r9 = Var("full5x5_r9()",float,doc="R9 of the supercluster, calculated with full 5x5 region",precision=10),
0198         sieie = Var("full5x5_sigmaIetaIeta()",float,doc="sigma_IetaIeta of the supercluster, calculated with full 5x5 region",precision=10),
0199         sipip = Var("showerShapeVariables().sigmaIphiIphi", float, doc="sigmaIphiIphi of the supercluster", precision=10),
0200         sieip = Var("full5x5_showerShapeVariables().sigmaIetaIphi",float,doc="sigma_IetaIphi of the supercluster, calculated with full 5x5 region",precision=10),
0201         s4 = Var("full5x5_showerShapeVariables().e2x2/full5x5_showerShapeVariables().e5x5",float,doc="e2x2/e5x5 of the supercluster, calculated with full 5x5 region",precision=10),
0202         etaWidth = Var("superCluster().etaWidth()",float,doc="Width of the photon supercluster in eta", precision=10),
0203         phiWidth = Var("superCluster().phiWidth()",float,doc="Width of the photon supercluster in phi", precision=10),
0204         cutBased = Var(
0205             "userInt('cutBasedID_loose')+userInt('cutBasedID_medium')+userInt('cutBasedID_tight')",
0206             "uint8",
0207             doc="cut-based ID bitmap, RunIIIWinter22V1, (0:fail, 1:loose, 2:medium, 3:tight)",
0208         ),
0209         vidNestedWPBitmap = Var(
0210             "userInt('VIDNestedWPBitmap')",
0211             int,
0212             doc="RunIIIWinter22V1 " + _bitmapVIDForPho_docstring
0213         ),
0214         electronVeto = Var("passElectronVeto()",bool,doc="pass electron veto"),
0215         pixelSeed = Var("hasPixelSeed()",bool,doc="has pixel seed"),
0216         hasConversionTracks = Var("hasConversionTracks()",bool,doc="Variable specifying if photon has associated conversion tracks (one-legged or two-legged)"),
0217         mvaID = Var("userFloat('mvaID')",float,doc="MVA ID score, Winter22V1",precision=10),
0218         mvaID_WP90 = Var("userInt('mvaID_WP90')",bool,doc="MVA ID WP90, Winter22V1"),
0219         mvaID_WP80 = Var("userInt('mvaID_WP80')",bool,doc="MVA ID WP80, Winter22V1"),
0220         trkSumPtHollowConeDR03 = Var("trkSumPtHollowConeDR03()",float,doc="Sum of track pT in a hollow cone of outer radius, inner radius", precision=10),
0221         trkSumPtSolidConeDR04 = Var("trkSumPtSolidConeDR04()",float,doc="Sum of track pT in a cone of dR=0.4", precision=10),
0222         ecalPFClusterIso = Var("ecalPFClusterIso()",float,doc="sum pt of ecal clusters, vetoing clusters part of photon", precision=10),
0223         hcalPFClusterIso = Var("hcalPFClusterIso()",float,doc="sum pt of hcal clusters, vetoing clusters part of photon", precision=10), 
0224         pfPhoIso03 = Var("photonIso()",float,doc="PF absolute isolation dR=0.3, photon component (uncorrected)"),
0225         pfChargedIso = Var("chargedHadronIso()",float,doc="PF absolute isolation dR=0.3, charged component with dxy,dz match to PV", precision=8),        
0226         pfChargedIsoPFPV = Var("chargedHadronPFPVIso()",float,doc="PF absolute isolation dR=0.3, charged component (PF PV only)"),
0227         pfChargedIsoWorstVtx = Var("chargedHadronWorstVtxIso()",float,doc="PF absolute isolation dR=0.3, charged component (Vertex with largest isolation)"),
0228         pfRelIso03_chg_quadratic = Var("userFloat('PFIsoChgQuadratic')/pt",float,doc="PF relative isolation dR=0.3, charged hadron component (with quadraticEA*rho*rho + linearEA*rho Winter22V1 corrections)"),
0229         pfRelIso03_all_quadratic = Var("userFloat('PFIsoAllQuadratic')/pt",float,doc="PF relative isolation dR=0.3, total (with quadraticEA*rho*rho + linearEA*rho Winter22V1 corrections)"),
0230         hoe = Var("hadronicOverEm()",float,doc="H over E",precision=8),
0231         hoe_Tower = Var("hadTowOverEm()",float,doc="H over E Tower based calculation",precision=8),
0232         hoe_PUcorr = Var("userFloat('HoverEQuadratic')",float,doc="PU corrected H/E (cone-based with quadraticEA*rho*rho + linearEA*rho Winter22V1 corrections)",precision=8),
0233         isScEtaEB = Var("abs(superCluster().eta()) < 1.4442",bool,doc="is supercluster eta within barrel acceptance"),
0234         isScEtaEE = Var("abs(superCluster().eta()) > 1.566 && abs(superCluster().eta()) < 2.5",bool,doc="is supercluster eta within endcap acceptance"),
0235         seedGain = Var("userInt('seedGain')","uint8",doc="Gain of the seed crystal"),
0236         seediEtaOriX = Var("superCluster().seedCrysIEtaOrIx","int16",doc="iEta or iX of seed crystal. iEta is barrel-only, iX is endcap-only. iEta runs from -85 to +85, with no crystal at iEta=0. iX runs from 1 to 100."),
0237         seediPhiOriY = Var("superCluster().seedCrysIPhiOrIy","int16",doc="iPhi or iY of seed crystal. iPhi is barrel-only, iY is endcap-only. iPhi runs from 1 to 360. iY runs from 1 to 100."),
0238         # position of photon is best approximated by position of seed cluster, not the SC centroid
0239         x_calo = Var("superCluster().seed().position().x()",float,doc="photon supercluster position on calorimeter, x coordinate (cm)",precision=10),
0240         y_calo = Var("superCluster().seed().position().y()",float,doc="photon supercluster position on calorimeter, y coordinate (cm)",precision=10),
0241         z_calo = Var("superCluster().seed().position().z()",float,doc="photon supercluster position on calorimeter, z coordinate (cm)",precision=10),
0242         # ES variables
0243         esEffSigmaRR = Var("full5x5_showerShapeVariables().effSigmaRR()", float, doc="preshower sigmaRR"),
0244         esEnergyOverRawE = Var("superCluster().preshowerEnergy()/superCluster().rawEnergy()", float, doc="ratio of preshower energy to raw supercluster energy"),
0245         haloTaggerMVAVal = Var("haloTaggerMVAVal()",float,doc="Value of MVA based BDT based  beam halo tagger in the Ecal endcap (valid for pT > 200 GeV)",precision=8),
0246     )
0247 )
0248 
0249 _phoVarsExtra = cms.PSet(
0250     r9Frac = Var("r9()",float,doc="Fractional R9 of the supercluster",precision=10),
0251     energy = Var("energy",float,doc="energy after regression",precision=10),
0252     rawPreshowerEnergy = Var("superCluster.preshowerEnergy",float,doc="energy deposited in preshower",precision=10),
0253     seedClusEnergy = Var("superCluster.seed.energy",float,doc="seed cluster energy",precision=10),
0254     e5x5 = Var("full5x5_showerShapeVariables.e5x5",float,doc="energy in 5x5",precision=10),
0255     dEtaSeedClusSuperClus = Var("superCluster.seed.eta-superCluster.position.Eta",float,doc="dEta(seed,SC)",precision=10),
0256     dPhiSeedClusSuperClus = Var("deltaPhi(superCluster.seed.phi,superCluster.position.Phi)",float,doc="dPhi(seed,SC)",precision=10),
0257     sigmaIphiIphiFull5x5 = Var("full5x5_showerShapeVariables().sigmaIphiIphi",float,doc="Full5x5 sigmaIPhiIPhi",precision=10),
0258     eMax = Var("full5x5_maxEnergyXtal()",float,doc="Emax",precision=10),
0259     e2nd = Var("full5x5_showerShapeVariables.e2nd",float,doc="E2nd",precision=10),
0260     eTop = Var("full5x5_showerShapeVariables.eTop",float,doc="Etop",precision=10),
0261     eBottom = Var("full5x5_showerShapeVariables.eBottom",float,doc="Ebottom",precision=10),
0262     eLeft = Var("full5x5_showerShapeVariables.eLeft",float,doc="Eleft",precision=10),
0263     eRight = Var("full5x5_showerShapeVariables.eRight",float,doc="Eright",precision=10),
0264     e2x5max = Var("full5x5_showerShapeVariables.e2x5Max",float,doc="energy in 2x5",precision=10),
0265     e2x5Top = Var("full5x5_showerShapeVariables.e2x5Top",float,doc="E2x5Top",precision=10),
0266     e2x5Bottom = Var("full5x5_showerShapeVariables.e2x5Bottom",float,doc="E2x5Bottom",precision=10),
0267     e2x5Left = Var("full5x5_showerShapeVariables.e2x5Left",float,doc="E2x5Left",precision=10),
0268     e2x5Right = Var("full5x5_showerShapeVariables.e2x5Right",float,doc="E2x5Right",precision=10),
0269     nSaturatedXtals = Var("nSaturatedXtals",int,doc="number of saturated crystals"),
0270     numberOfClusters = Var("superCluster.clusters.size",int,doc="number of clusters"),
0271     hadTowOverEm = Var("hadTowOverEm",float,doc="single tower based H/E",precision=10),
0272     ecalRecHitIsolation = Var("ecalRecHitSumEtConeDR03",float,doc="ECAL RecHit isolation",precision=10),
0273     sigmaIetaIetaFrac = Var("sigmaIetaIeta",float,doc="fractional sigmaIetaIeta",precision=10),
0274     chargedHadronIso = Var("chargedHadronIso",float,doc="charged hadron Isolation",precision=10),
0275     iEtaMod5 = Var("?superCluster.seedCrysIEtaOrIx>0?(superCluster.seedCrysIEtaOrIx-1)%5:(superCluster.seedCrysIEtaOrIx+1)%5",int,doc="iEtaMod5"),
0276     iEtaMod20 = Var("?abs(superCluster.seedCrysIEtaOrIx)<=25?(superCluster.seedCrysIEtaOrIx-(?superCluster.seedCrysIEtaOrIx>0?+1:-1))%20:(superCluster.seedCrysIEtaOrIx-(?superCluster.seedCrysIEtaOrIx>0?+26:-26))%20",int,doc="iEtaMod20"),
0277     iPhiMod2 = Var("(superCluster.seedCrysIPhiOrIy-1)%2",int,doc="iPhiMod2"),
0278     iPhiMod20 = Var("(superCluster.seedCrysIPhiOrIy-1)%20",int,doc="iPhiMod20"),
0279 )
0280 
0281 #these eras need to make the energy correction, hence the "New". Also save only Fall17V2 IDS in Run2, No Run3 Winter22V1 and quadratic iso in Run2 
0282 run2_egamma.toModify(
0283     photonTable.variables,
0284     pt = Var("pt*userFloat('ecalEnergyPostCorrNew')/userFloat('ecalEnergyPreCorrNew')", float, precision=-1, doc="p_{T}"),
0285     energyErr = Var("userFloat('ecalEnergyErrPostCorrNew')",float,doc="energy error of the cluster from regression",precision=6),
0286     eCorr = Var("userFloat('ecalEnergyPostCorrNew')/userFloat('ecalEnergyPreCorrNew')",float,doc="ratio of the calibrated energy/miniaod energy"),
0287     cutBased = Var(
0288             "userInt('cutBasedID_Fall17V2_loose')+userInt('cutBasedID_Fall17V2_medium')+userInt('cutBasedID_Fall17V2_tight')",
0289             "uint8",
0290             doc="cut-based ID bitmap, Fall17V2, (0:fail, 1:loose, 2:medium, 3:tight)",
0291         ),
0292     vidNestedWPBitmap = Var(
0293             "userInt('VIDNestedWPBitmapFall17V2')",
0294             int,
0295             doc="Fall17V2 " + _bitmapVIDForPhoRun2_docstring
0296         ),
0297     mvaID = Var("userFloat('mvaID_Fall17V2')",float,doc="MVA ID score, Fall17V2",precision=10),
0298     mvaID_WP90 = Var("userInt('mvaID_Fall17V2_WP90')",bool,doc="MVA ID WP90, Fall17V2"),
0299     mvaID_WP80 = Var("userInt('mvaID_Fall17V2_WP80')",bool,doc="MVA ID WP80, Fall17V2"),
0300     pfRelIso03_chg = Var("userFloat('PFIsoChgFall17V2')/pt",float,doc="PF relative isolation dR=0.3, charged component (with Fall17V2rho*EA PU corrections)"),
0301     pfRelIso03_all = Var("userFloat('PFIsoAllFall17V2')/pt",float,doc="PF relative isolation dR=0.3, total (with Fall17V2 rho*EA PU corrections)"),
0302     pfRelIso03_chg_quadratic=None,
0303     pfRelIso03_all_quadratic=None,
0304     hoe_PUcorr=None
0305 )
0306 
0307 photonsMCMatchForTable = cms.EDProducer("MCMatcher",  # cut on deltaR, deltaPt/Pt; pick best by deltaR
0308     src         = photonTable.src,                 # final reco collection
0309     matched     = cms.InputTag("finalGenParticles"), # final mc-truth particle collection
0310     mcPdgId     = cms.vint32(11,22),                 # one or more PDG ID (11 = el, 22 = pho); absolute values (see below)
0311     checkCharge = cms.bool(False),              # True = require RECO and MC objects to have the same charge
0312     mcStatus    = cms.vint32(1),                # PYTHIA status code (1 = stable, 2 = shower, 3 = hard scattering)
0313     maxDeltaR   = cms.double(0.3),              # Minimum deltaR for the match
0314     maxDPtRel   = cms.double(0.5),              # Minimum deltaPt/Pt for the match
0315     resolveAmbiguities    = cms.bool(True),     # Forbid two RECO objects to match to the same GEN object
0316     resolveByMatchQuality = cms.bool(True),    # False = just match input in order; True = pick lowest deltaR pair first
0317 )
0318 
0319 photonMCTable = cms.EDProducer("CandMCMatchTableProducer",
0320     src     = photonTable.src,
0321     mcMap   = cms.InputTag("photonsMCMatchForTable"),
0322     objName = photonTable.name,
0323     objType = photonTable.name, #cms.string("Photon"),
0324     branchName = cms.string("genPart"),
0325     docString = cms.string("MC matching to status==1 photons or electrons"),
0326 )
0327 
0328 #adding 4 most imp scale & smearing variables to table
0329 run2_egamma.toModify(
0330     photonTable.variables,
0331     dEscaleUp=Var("userFloat('ecalEnergyPostCorrNew') - userFloat('energyScaleUpNew')", float, doc="ecal energy scale shifted 1 sigma up (adding gain/stat/syst in quadrature)", precision=8),
0332     dEscaleDown=Var("userFloat('ecalEnergyPostCorrNew') - userFloat('energyScaleDownNew')", float, doc="ecal energy scale shifted 1 sigma down (adding gain/stat/syst in quadrature)", precision=8),
0333     dEsigmaUp=Var("userFloat('ecalEnergyPostCorrNew') - userFloat('energySigmaUpNew')", float, doc="ecal energy smearing value shifted 1 sigma up", precision=8),
0334     dEsigmaDown=Var("userFloat('ecalEnergyPostCorrNew') - userFloat('energySigmaDownNew')", float, doc="ecal energy smearing value shifted 1 sigma up", precision=8),
0335 )
0336 
0337 
0338 photonTask = cms.Task(bitmapVIDForPho, bitmapVIDForPhoRun2, isoForPho, hOverEForPho, isoForPhoFall17V2, seedGainPho, slimmedPhotonsWithUserData, finalPhotons)
0339 
0340 photonTablesTask = cms.Task(photonTable)
0341 photonMCTask = cms.Task(photonsMCMatchForTable, photonMCTable)
0342 
0343 _photonTask_Run2 = photonTask.copy()
0344 _photonTask_Run2.remove(bitmapVIDForPho)
0345 _photonTask_Run2.remove(isoForPho)
0346 _photonTask_Run2.remove(hOverEForPho)
0347 _photonTask_Run2.add(calibratedPatPhotonsNano)
0348 run2_egamma.toReplaceWith(photonTask, _photonTask_Run2)