CMS 3D CMS Logo

photons_cff.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 from Configuration.Eras.Modifier_run2_miniAOD_80XLegacy_cff import run2_miniAOD_80XLegacy
4 from math import ceil,log
5 
6 from Configuration.Eras.Modifier_run2_miniAOD_80XLegacy_cff import run2_miniAOD_80XLegacy
7 from Configuration.Eras.Modifier_run2_nanoAOD_94XMiniAODv1_cff import run2_nanoAOD_94XMiniAODv1
8 from Configuration.Eras.Modifier_run2_nanoAOD_94XMiniAODv2_cff import run2_nanoAOD_94XMiniAODv2
9 from Configuration.Eras.Modifier_run2_nanoAOD_94X2016_cff import run2_nanoAOD_94X2016
10 from Configuration.Eras.Modifier_run2_nanoAOD_102Xv1_cff import run2_nanoAOD_102Xv1
11 
12 photon_id_modules_WorkingPoints_nanoAOD = cms.PSet(
13  modules = cms.vstring(
14  'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V1_TrueVtx_cff',
15  'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V2_cff',
16  'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V1p1_cff',
17  'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V2_cff',
18  'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Spring16_V2p2_cff',
19  'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Spring16_nonTrig_V1_cff',
20  ),
21  WorkingPoints = cms.vstring(
22  "egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-loose",
23  "egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-medium",
24  "egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-tight",
25  )
26 )
27 photon_id_modules_WorkingPoints_nanoAOD_Spring16V2p2 = cms.PSet(
28  modules = photon_id_modules_WorkingPoints_nanoAOD.modules,
29  WorkingPoints = cms.vstring(
30  "egmPhotonIDs:cutBasedPhotonID-Spring16-V2p2-loose",
31  "egmPhotonIDs:cutBasedPhotonID-Spring16-V2p2-medium",
32  "egmPhotonIDs:cutBasedPhotonID-Spring16-V2p2-tight",
33  )
34 )
35 
36 def make_bitmapVID_docstring(id_modules_working_points_pset):
37  pset = id_modules_working_points_pset
38 
39  for modname in pset.modules:
40  ids = __import__(modname, globals(), locals(), ['idName','cutFlow'])
41  for name in dir(ids):
42  _id = getattr(ids,name)
43  if hasattr(_id,'idName') and hasattr(_id,'cutFlow'):
44  if (len(pset.WorkingPoints)>0 and _id.idName == pset.WorkingPoints[0].split(':')[-1]):
45  cut_names = ','.join([cut.cutName.value() for cut in _id.cutFlow])
46  n_bits_per_cut = int(ceil(log(len(pset.WorkingPoints)+1,2)))
47  return 'VID compressed bitmap (%s), %d bits per cut'%(cut_names, n_bits_per_cut)
48  raise ValueError("Something is wrong in the photon ID modules parameter set!")
49 
50 bitmapVIDForPho = cms.EDProducer("PhoVIDNestedWPBitmapProducer",
51  src = cms.InputTag("slimmedPhotons"),
52  WorkingPoints = photon_id_modules_WorkingPoints_nanoAOD.WorkingPoints,
53 )
54 
55 bitmapVIDForPhoSpring16V2p2 = cms.EDProducer("PhoVIDNestedWPBitmapProducer",
56  src = cms.InputTag("slimmedPhotons"),
57  WorkingPoints = photon_id_modules_WorkingPoints_nanoAOD_Spring16V2p2.WorkingPoints,
58 )
59 
60 isoForPho = cms.EDProducer("PhoIsoValueMapProducer",
61  src = cms.InputTag("slimmedPhotons"),
62  relative = cms.bool(False),
63  rho_PFIso = cms.InputTag("fixedGridRhoFastjetAll"),
64  mapIsoChg = cms.InputTag("photonIDValueMapProducer:phoChargedIsolation"),
65  mapIsoNeu = cms.InputTag("photonIDValueMapProducer:phoNeutralHadronIsolation"),
66  mapIsoPho = cms.InputTag("photonIDValueMapProducer:phoPhotonIsolation"),
67  EAFile_PFIso_Chg = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Fall17/effAreaPhotons_cone03_pfChargedHadrons_90percentBased_V2.txt"),
68  EAFile_PFIso_Neu = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Fall17/effAreaPhotons_cone03_pfNeutralHadrons_90percentBased_V2.txt"),
69  EAFile_PFIso_Pho = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Fall17/effAreaPhotons_cone03_pfPhotons_90percentBased_V2.txt"),
70 )
71 for modifier in run2_miniAOD_80XLegacy, run2_nanoAOD_94X2016:
72  modifier.toModify(isoForPho,
73  EAFile_PFIso_Chg = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Spring16/effAreaPhotons_cone03_pfChargedHadrons_90percentBased.txt"),
74  EAFile_PFIso_Neu = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Spring16/effAreaPhotons_cone03_pfNeutralHadrons_90percentBased.txt"),
75  EAFile_PFIso_Pho = cms.FileInPath("RecoEgamma/PhotonIdentification/data/Spring16/effAreaPhotons_cone03_pfPhotons_90percentBased.txt"),
76  )
77 
78 seedGainPho = cms.EDProducer("PhotonSeedGainProducer", src = cms.InputTag("slimmedPhotons"))
79 
80 import RecoEgamma.EgammaTools.calibratedEgammas_cff
81 calibratedPatPhotons102Xv1 = RecoEgamma.EgammaTools.calibratedEgammas_cff.calibratedPatPhotons.clone(
82  produceCalibratedObjs = False,
83  correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2018_Step2Closure_CoarseEtaR9Gain_v2")
84 )
85 
86 calibratedPatPhotons94Xv1 = RecoEgamma.EgammaTools.calibratedEgammas_cff.calibratedPatPhotons.clone(
87  produceCalibratedObjs = False,
88  correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2017_17Nov2017_v1_ele_unc")
89 )
90 
91 calibratedPatPhotons80XLegacy = RecoEgamma.EgammaTools.calibratedEgammas_cff.calibratedPatPhotons.clone(
92  produceCalibratedObjs = False,
93  correctionFile = cms.string("EgammaAnalysis/ElectronTools/data/ScalesSmearings/Legacy2016_07Aug2017_FineEtaR9_v3_ele_unc"),
94 )
95 
96 slimmedPhotonsWithUserData = cms.EDProducer("PATPhotonUserDataEmbedder",
97  src = cms.InputTag("slimmedPhotons"),
98  userFloats = cms.PSet(
99  mvaID = cms.InputTag("photonMVAValueMapProducer:PhotonMVAEstimatorRunIIFall17v2Values"),
100  mvaID_Fall17V1p1 = cms.InputTag("photonMVAValueMapProducer:PhotonMVAEstimatorRunIIFall17v1p1Values"),
101  mvaID_Spring16nonTrigV1 = cms.InputTag("photonMVAValueMapProducer:PhotonMVAEstimatorRun2Spring16NonTrigV1Values"),
102  PFIsoChg = cms.InputTag("isoForPho:PFIsoChg"),
103  PFIsoAll = cms.InputTag("isoForPho:PFIsoAll"),
104  ),
105  userIntFromBools = cms.PSet(
106  cutbasedID_loose = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-loose"),
107  cutbasedID_medium = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-medium"),
108  cutbasedID_tight = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V2-tight"),
109  mvaID_WP90 = cms.InputTag("egmPhotonIDs:mvaPhoID-RunIIFall17-v2-wp90"),
110  mvaID_WP80 = cms.InputTag("egmPhotonIDs:mvaPhoID-RunIIFall17-v2-wp80"),
111  cutbasedIDV1_loose = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V1-loose"),
112  cutbasedIDV1_medium = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V1-medium"),
113  cutbasedIDV1_tight = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Fall17-94X-V1-tight"),
114  cutID_Spring16_loose = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Spring16-V2p2-loose"),
115  cutID_Spring16_medium = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Spring16-V2p2-medium"),
116  cutID_Spring16_tight = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Spring16-V2p2-tight"),
117  mvaID_Spring16nonTrigV1_WP90 = cms.InputTag("egmPhotonIDs:mvaPhoID-Spring16-nonTrig-V1-wp90"),
118  mvaID_Spring16nonTrigV1_WP80 = cms.InputTag("egmPhotonIDs:mvaPhoID-Spring16-nonTrig-V1-wp80"),
119  ),
120  userInts = cms.PSet(
121  VIDNestedWPBitmap = cms.InputTag("bitmapVIDForPho"),
122  VIDNestedWPBitmap_Spring16V2p2 = cms.InputTag("bitmapVIDForPhoSpring16V2p2"),
123  seedGain = cms.InputTag("seedGainPho"),
124  ),
125 )
126 
127 run2_miniAOD_80XLegacy.toModify(slimmedPhotonsWithUserData.userFloats,
128  ecalEnergyErrPostCorrNew = cms.InputTag("calibratedPatPhotons80XLegacy","ecalEnergyErrPostCorr"),
129  ecalEnergyPreCorrNew = cms.InputTag("calibratedPatPhotons80XLegacy","ecalEnergyPreCorr"),
130  ecalEnergyPostCorrNew = cms.InputTag("calibratedPatPhotons80XLegacy","ecalEnergyPostCorr"),
131 )
132 run2_nanoAOD_94XMiniAODv1.toModify(slimmedPhotonsWithUserData.userFloats,
133  ecalEnergyErrPostCorrNew = cms.InputTag("calibratedPatPhotons94Xv1","ecalEnergyErrPostCorr"),
134  ecalEnergyPreCorrNew = cms.InputTag("calibratedPatPhotons94Xv1","ecalEnergyPreCorr"),
135  ecalEnergyPostCorrNew = cms.InputTag("calibratedPatPhotons94Xv1","ecalEnergyPostCorr"),
136 )
137 run2_nanoAOD_102Xv1.toModify(slimmedPhotonsWithUserData.userFloats,
138  ecalEnergyErrPostCorrNew = cms.InputTag("calibratedPatPhotons102Xv1","ecalEnergyErrPostCorr"),
139  ecalEnergyPreCorrNew = cms.InputTag("calibratedPatPhotons102Xv1","ecalEnergyPreCorr"),
140  ecalEnergyPostCorrNew = cms.InputTag("calibratedPatPhotons102Xv1","ecalEnergyPostCorr"),
141 )
142 
143 finalPhotons = cms.EDFilter("PATPhotonRefSelector",
144  src = cms.InputTag("slimmedPhotonsWithUserData"),
145  cut = cms.string("pt > 5 ")
146 )
147 
148 photonTable = cms.EDProducer("SimpleCandidateFlatTableProducer",
149  src = cms.InputTag("linkedObjects","photons"),
150  cut = cms.string(""), #we should not filter on cross linked collections
151  name= cms.string("Photon"),
152  doc = cms.string("slimmedPhotons after basic selection (" + finalPhotons.cut.value()+")"),
153  singleton = cms.bool(False), # the number of entries is variable
154  extension = cms.bool(False), # this is the main table for the photons
155  variables = cms.PSet(CandVars,
156  jetIdx = Var("?hasUserCand('jet')?userCand('jet').key():-1", int, doc="index of the associated jet (-1 if none)"),
157  electronIdx = Var("?hasUserCand('electron')?userCand('electron').key():-1", int, doc="index of the associated electron (-1 if none)"),
158  energyErr = Var("getCorrectedEnergyError('regression2')",float,doc="energy error of the cluster from regression",precision=6),
159  r9 = Var("full5x5_r9()",float,doc="R9 of the supercluster, calculated with full 5x5 region",precision=10),
160  sieie = Var("full5x5_sigmaIetaIeta()",float,doc="sigma_IetaIeta of the supercluster, calculated with full 5x5 region",precision=10),
161  cutBased = Var(
162  "userInt('cutbasedID_loose')+userInt('cutbasedID_medium')+userInt('cutbasedID_tight')",
163  int,
164  doc="cut-based ID bitmap, Fall17V2, (0:fail, 1:loose, 2:medium, 3:tight)"
165  ),
166  cutBased_Fall17V1Bitmap = Var(
167  "userInt('cutbasedIDV1_loose')+2*userInt('cutbasedIDV1_medium')+4*userInt('cutbasedIDV1_tight')",
168  int,
169  doc="cut-based ID bitmap, Fall17V1, 2^(0:loose, 1:medium, 2:tight).",
170  ),
171  vidNestedWPBitmap = Var(
172  "userInt('VIDNestedWPBitmap')",
173  int,
174  doc="Fall17V2 " + make_bitmapVID_docstring(photon_id_modules_WorkingPoints_nanoAOD)
175  ),
176  electronVeto = Var("passElectronVeto()",bool,doc="pass electron veto"),
177  pixelSeed = Var("hasPixelSeed()",bool,doc="has pixel seed"),
178  mvaID = Var("userFloat('mvaID')",float,doc="MVA ID score, Fall17V2",precision=10),
179  mvaID_Fall17V1p1 = Var("userFloat('mvaID_Fall17V1p1')",float,doc="MVA ID score, Fall17V1p1",precision=10),
180  mvaID_WP90 = Var("userInt('mvaID_WP90')",bool,doc="MVA ID WP90, Fall17V2"),
181  mvaID_WP80 = Var("userInt('mvaID_WP80')",bool,doc="MVA ID WP80, Fall17V2"),
182  cutBased_Spring16V2p2 = Var(
183  "userInt('cutID_Spring16_loose')+userInt('cutID_Spring16_medium')+userInt('cutID_Spring16_tight')",
184  int,
185  doc="cut-based ID bitmap, Spring16V2p2, (0:fail, 1:loose, 2:medium, 3:tight)"
186  ),
187  mvaID_Spring16nonTrigV1 = Var(
188  "userFloat('mvaID_Spring16nonTrigV1')",
189  float,
190  doc="MVA ID score, Spring16nonTrigV1",
191  precision=10
192  ),
193  vidNestedWPBitmap_Spring16V2p2 = Var(
194  "userInt('VIDNestedWPBitmap_Spring16V2p2')",
195  int,
196  doc="Spring16V2p2 " + make_bitmapVID_docstring(photon_id_modules_WorkingPoints_nanoAOD_Spring16V2p2)
197  ),
198  pfRelIso03_chg = Var("userFloat('PFIsoChg')/pt",float,doc="PF relative isolation dR=0.3, charged component (with rho*EA PU corrections)"),
199  pfRelIso03_all = Var("userFloat('PFIsoAll')/pt",float,doc="PF relative isolation dR=0.3, total (with rho*EA PU corrections)"),
200  hoe = Var("hadronicOverEm()",float,doc="H over E",precision=8),
201  isScEtaEB = Var("abs(superCluster().eta()) < 1.4442",bool,doc="is supercluster eta within barrel acceptance"),
202  isScEtaEE = Var("abs(superCluster().eta()) > 1.566 && abs(superCluster().eta()) < 2.5",bool,doc="is supercluster eta within endcap acceptance"),
203  seedGain = Var("userInt('seedGain')","uint8",doc="Gain of the seed crystal"),
204  )
205 )
206 
207 #these eras have the energy correction in the mini
208 for modifier in run2_nanoAOD_94XMiniAODv2, run2_nanoAOD_94X2016:
209  modifier.toModify(photonTable.variables,
210  pt = Var("pt*userFloat('ecalEnergyPostCorr')/userFloat('ecalEnergyPreCorr')", float, precision=-1, doc="p_{T}"),
211  energyErr = Var("userFloat('ecalEnergyErrPostCorr')",float,doc="energy error of the cluster from regression",precision=6),
212  eCorr = Var("userFloat('ecalEnergyPostCorr')/userFloat('ecalEnergyPreCorr')",float,doc="ratio of the calibrated energy/miniaod energy"),
213  )
214 
215 #these eras need to make the energy correction, hence the "New"
216 for modifier in run2_nanoAOD_94XMiniAODv1, run2_miniAOD_80XLegacy, run2_nanoAOD_102Xv1:
217  modifier.toModify(photonTable.variables,
218  pt = Var("pt*userFloat('ecalEnergyPostCorrNew')/userFloat('ecalEnergyPreCorrNew')", float, precision=-1, doc="p_{T}"),
219  energyErr = Var("userFloat('ecalEnergyErrPostCorrNew')",float,doc="energy error of the cluster from regression",precision=6),
220  eCorr = Var("userFloat('ecalEnergyPostCorrNew')/userFloat('ecalEnergyPreCorrNew')",float,doc="ratio of the calibrated energy/miniaod energy"),
221  )
222 
223 # only add the Spring16 IDs for 2016 nano
224 (~(run2_nanoAOD_94X2016 | run2_miniAOD_80XLegacy)).toModify(photonTable.variables,
225  cutBased_Spring16V2p2 = None,
226  mvaID_Spring16nonTrigV1 = None,
227  vidNestedWPBitmap_Spring16V2p2 = None,
228 )
229 
230 
231 photonsMCMatchForTable = cms.EDProducer("MCMatcher", # cut on deltaR, deltaPt/Pt; pick best by deltaR
232  src = photonTable.src, # final reco collection
233  matched = cms.InputTag("finalGenParticles"), # final mc-truth particle collection
234  mcPdgId = cms.vint32(11,22), # one or more PDG ID (11 = el, 22 = pho); absolute values (see below)
235  checkCharge = cms.bool(False), # True = require RECO and MC objects to have the same charge
236  mcStatus = cms.vint32(1), # PYTHIA status code (1 = stable, 2 = shower, 3 = hard scattering)
237  maxDeltaR = cms.double(0.3), # Minimum deltaR for the match
238  maxDPtRel = cms.double(0.5), # Minimum deltaPt/Pt for the match
239  resolveAmbiguities = cms.bool(True), # Forbid two RECO objects to match to the same GEN object
240  resolveByMatchQuality = cms.bool(True), # False = just match input in order; True = pick lowest deltaR pair first
241 )
242 
243 photonMCTable = cms.EDProducer("CandMCMatchTableProducer",
244  src = photonTable.src,
245  mcMap = cms.InputTag("photonsMCMatchForTable"),
246  objName = photonTable.name,
247  objType = photonTable.name, #cms.string("Photon"),
248  branchName = cms.string("genPart"),
249  docString = cms.string("MC matching to status==1 photons or electrons"),
250 )
251 
252 from RecoEgamma.EgammaTools.egammaObjectModificationsInMiniAOD_cff import egamma8XObjectUpdateModifier,egamma9X105XUpdateModifier,prependEgamma8XObjectUpdateModifier
253 #we have dataformat changes to 106X so to read older releases we use egamma updators
254 slimmedPhotonsTo106X = cms.EDProducer("ModifiedPhotonProducer",
255  src = cms.InputTag("slimmedPhotons"),
256  modifierConfig = cms.PSet( modifications = cms.VPSet(egamma9X105XUpdateModifier) )
257 )
258 #might as well fix 80X while we're at it although the differences are not so relavent for nano
259 run2_miniAOD_80XLegacy.toModify( slimmedPhotonsTo106X.modifierConfig.modifications, prependEgamma8XObjectUpdateModifier )
260 
261 for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_94X2016 ,run2_nanoAOD_102Xv1:
262  modifier.toModify(bitmapVIDForPho, src = "slimmedPhotonsTo106X")
263  modifier.toModify(bitmapVIDForPhoSpring16V2p2, src = "slimmedPhotonsTo106X")
264  modifier.toModify(isoForPho, src = "slimmedPhotonsTo106X")
265  modifier.toModify(calibratedPatPhotons102Xv1, src = "slimmedPhotonsTo106X")
266  modifier.toModify(calibratedPatPhotons94Xv1, src = "slimmedPhotonsTo106X")
267  modifier.toModify(calibratedPatPhotons80XLegacy, src = "slimmedPhotonsTo106X")
268  modifier.toModify(slimmedPhotonsWithUserData, src = "slimmedPhotonsTo106X")
269  modifier.toModify(seedGainPho, src = "slimmedPhotonsTo106X")
270 
271 
272 photonSequence = cms.Sequence(
273  bitmapVIDForPho + \
274  bitmapVIDForPhoSpring16V2p2 + \
275  isoForPho + \
276  seedGainPho + \
277  slimmedPhotonsWithUserData + \
278  finalPhotons
279 )
280 
281 photonTables = cms.Sequence ( photonTable)
282 photonMC = cms.Sequence(photonsMCMatchForTable + photonMCTable)
283 
284 from RecoEgamma.EgammaIsolationAlgos.egmPhotonIsolationMiniAOD_cff import egmPhotonIsolation
285 from RecoEgamma.PhotonIdentification.photonIDValueMapProducer_cff import photonIDValueMapProducer
286 _updatePhoTo106X_sequence =cms.Sequence(egmPhotonIsolation + photonIDValueMapProducer + slimmedPhotonsTo106X)
287 _withUpdatePho_sequence = photonSequence.copy()
288 _withUpdatePho_sequence.insert(0,_updatePhoTo106X_sequence)
289 for modifier in run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_94X2016 ,run2_nanoAOD_102Xv1:
290  modifier.toReplaceWith(photonSequence, _withUpdatePho_sequence)
291 
292 _with80XScale_sequence = _withUpdatePho_sequence.copy()
293 _with80XScale_sequence.replace(slimmedPhotonsWithUserData, calibratedPatPhotons80XLegacy + slimmedPhotonsWithUserData)
294 run2_miniAOD_80XLegacy.toReplaceWith(photonSequence, _with80XScale_sequence)
295 
296 _with94Xv1Scale_sequence = _withUpdatePho_sequence.copy()
297 _with94Xv1Scale_sequence.replace(slimmedPhotonsWithUserData, calibratedPatPhotons94Xv1 + slimmedPhotonsWithUserData)
298 run2_nanoAOD_94XMiniAODv1.toReplaceWith(photonSequence, _with94Xv1Scale_sequence)
299 
300 _with102Xv1Scale_sequence = photonSequence.copy()
301 _with102Xv1Scale_sequence.replace(slimmedPhotonsWithUserData, calibratedPatPhotons102Xv1 + slimmedPhotonsWithUserData)
302 run2_nanoAOD_102Xv1.toReplaceWith(photonSequence, _with102Xv1Scale_sequence)
def Var(expr, valtype, compression=None, doc=None, mcOnly=False, precision=-1)
Definition: common_cff.py:20
def make_bitmapVID_docstring(id_modules_working_points_pset)
Definition: photons_cff.py:36
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
dbl *** dir
Definition: mlp_gen.cc:35
double split
Definition: MVATrainer.cc:139