CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
propagateMEtUncertainties.py
Go to the documentation of this file.
2 import PhysicsTools.PatAlgos.tools.helpers as configtools
3 
4 def propagateMEtUncertainties(process, particleCollection, particleType,
5  shiftType, particleCollectionShiftUp,
6  particleCollectionShiftDown, metProducer,
7  metType, sequence, postfix):
8 
9  # produce MET correction objects
10  # (sum of differences in four-momentum between original and up/down shifted particle collection)
11 
12  moduleMETcorrShiftUp = cms.EDProducer("ShiftedParticleMETcorrInputProducer",
13  srcOriginal = cms.InputTag(particleCollection),
14  srcShifted = cms.InputTag(particleCollectionShiftUp)
15  )
16  moduleMETcorrShiftUpName = "pat%sMETcorr%s%sUp" % (metType,particleType, shiftType)
17  moduleMETcorrShiftUpName += postfix
18  setattr(process, moduleMETcorrShiftUpName, moduleMETcorrShiftUp)
19  sequence += moduleMETcorrShiftUp
20  moduleMETcorrShiftDown = moduleMETcorrShiftUp.clone(
21  srcShifted = cms.InputTag(particleCollectionShiftDown)
22  )
23  moduleMETcorrShiftDownName = "pat%sMETcorr%s%sDown" % (metType,particleType, shiftType)
24  moduleMETcorrShiftDownName += postfix
25  setattr(process, moduleMETcorrShiftDownName, moduleMETcorrShiftDown)
26  sequence += moduleMETcorrShiftDown
27 
28  # propagate effects of up/down shifts to MET
29  moduleMETshiftUp = metProducer.clone(
30  src = cms.InputTag(metProducer.label()),
31  srcType1Corrections = cms.VInputTag(
32  cms.InputTag(moduleMETcorrShiftUpName)
33  )
34  )
35  metProducerLabel = metProducer.label()
36  if postfix != "":
37  if metProducerLabel[-len(postfix):] == postfix:
38  metProducerLabel = metProducerLabel[0:-len(postfix)]
39  else:
40  raise StandardError("Tried to remove postfix %s from label %s, but it wasn't there" % (postfix, metProducerLabel))
41  moduleMETshiftUpName = "%s%s%sUp" % (metProducerLabel, particleType, shiftType)
42  moduleMETshiftUpName += postfix
43  setattr(process, moduleMETshiftUpName, moduleMETshiftUp)
44  sequence += moduleMETshiftUp
45  moduleMETshiftDown = moduleMETshiftUp.clone(
46  srcType1Corrections = cms.VInputTag(
47  cms.InputTag(moduleMETcorrShiftDownName)
48  )
49  )
50  moduleMETshiftDownName = "%s%s%sDown" % (metProducerLabel, particleType, shiftType)
51  moduleMETshiftDownName += postfix
52  setattr(process, moduleMETshiftDownName, moduleMETshiftDown)
53  sequence += moduleMETshiftDown
54 
55  metCollectionsUp_Down = [
56  moduleMETshiftUpName,
57  moduleMETshiftDownName
58  ]
59 
60  return metCollectionsUp_Down
61 
62 
63 
64 
65 def propagateShiftedSingleParticles(process, shiftedParticleCollections, metProducers,
66  metType, sequence, postfix):
67 
68  metCollectionsUpDown = []
69 
70  #looping over existing single particles collections, rejecting jets and Up or Down variations
71 
72  for shiftedCol in shiftedParticleCollections.keys():
73  if shiftedCol.find('jet')!=-1 or shiftedCol.find('Jet')!=-1 or shiftedCol.find('Up')!=-1 or shiftedCol.find('Down')!=-1 :
74  continue
75 
76  particleType=""
77  if shiftedCol.find('electron')!=-1:
78  particleType="Electron"
79  if shiftedCol.find('photon')!=-1:
80  particleType="Photon"
81  if shiftedCol.find('muon')!=-1:
82  particleType="Muon"
83  if shiftedCol.find('tau')!=-1:
84  particleType="Tau"
85 
86  for metProducer in metProducers:
87  tmpMetCollections = propagateMEtUncertainties(process,
88  shiftedParticleCollections[shiftedCol],
89  particleType, "En",
90  shiftedParticleCollections[shiftedCol+'EnUp'],
91  shiftedParticleCollections[shiftedCol+'EnDown'],
92  metProducer, metType, sequence, postfix)
93 
94  metCollectionsUpDown.extend( tmpMetCollections )
95 
96  return metCollectionsUpDown
97 
98 
99 
100 
101 #def propagateESShiftedJets(process, shiftedParticleCollections, metProducers,
102 # metType, sequence, postfix):
103 #
104 # metCollectionsUp_DownForRawMEt = \
105 # propagateMEtUncertainties(
106 # process, shiftedParticleCollections['lastJetCollection'], "Jet", "En",
107 # shiftedParticleCollections['jetCollectionEnUpForRawMEt'], shiftedParticleCollections['jetCollectionEnDownForRawMEt'],
108 # getattr(process, "patPFMet" + postfix), "PF", metUncertaintySequence, postfix)
109 # collectionsToKeep.extend(metCollectionsUp_DownForRawMEt)
110 
111 
112 
113 
114 def propagateERShiftedJets(process, shiftedParticleCollections, metProducers,
115  metType, sequence, postfix):
116 
117  metCollectionsUpDown = []
118 
119  for metProducer in metProducers:
120 
121  tmpMetCollections = propagateMEtUncertainties(process,
122  shiftedParticleCollections['lastJetCollection'], "Jet", "Res",
123  shiftedParticleCollections['jetCollectionResUp'],
124  shiftedParticleCollections['jetCollectionResDown'],
125  metProducer, metType, sequence, postfix)
126 
127  metCollectionsUpDown.extend( tmpMetCollections )
128 
129  return metCollectionsUpDown
130 
131 
132 
133 
134 
135 def createPatMETModules(process, metType, metPatSequence, applyT1Cor=False,
136  applyT2Cor=False, applyT0pcCor=False, applyXYShiftCor=False,
137  applyUncEnCalib=False,sysShiftCorrParameter=cms.VPSet(), postfix=""):
138 
139  ##FIXME: postfix is set to null as the whoelsequence receive it later
140  postfix=""
141 
142  if applyUncEnCalib :
143  applyT2Cor = True
144 
145  ## standard naming convention
146  metModName = "pat"+metType+"Met"
147  metModNameT1=metModName
148  metModNameT1T2=metModName
149  if applyT0pcCor :
150  metModNameT1 += "T0pc"
151  metModNameT1T2 += "T0pc"
152  metModNameT1 += "T1"
153  metModNameT1T2 += "T1T2"
154  if applyXYShiftCor :
155  metModNameT1 += "Txy"
156  metModNameT1T2 += "Txy"
157  # disabled spec. name for the moment as it just modifies the type2 MET
158  # if applyUncEnCalib :
159  # metModNameT1 += "UEC"
160  # metModNameT1T2 += "UEC"
161 
162 
163  #plug the MET modules in to the sequence
164  setattr(process, metModName, getattr(process, metModName ) )
165  if applyT1Cor :
166  setattr(process, metModNameT1+postfix, getattr(process, metModNameT1 ).clone(
167  src = cms.InputTag(metModName + postfix)
168  ))
169  metPatSequence += getattr(process, metModNameT1+postfix)
170  if applyT2Cor :
171  setattr(process, metModNameT1T2+postfix, getattr(process, metModNameT1T2 ).clone(
172  src = cms.InputTag(metModName + postfix)
173  ) )
174  metPatSequence += getattr(process, metModNameT1T2+postfix)
175 
176  patMetCorrectionsCentralValue = []
177 
178 
179  #Type0 for pfT1 and pfT1T2 MET
180  if metType == "PF":
181  patMetCorrectionsCentralValue = [ cms.InputTag('patPFJetMETtype1p2Corr' + postfix, 'type1') ]
182  if applyT0pcCor :
183  patMetCorrectionsCentralValue.extend([ cms.InputTag('patPFMETtype0Corr' + postfix) ])
184 
185 
186  # compute XY shift correction if asked, and configure the tool accordingly
187  if applyXYShiftCor :
188  if not hasattr(process, 'pfMEtSysShiftCorrSequence'):
189  process.load("JetMETCorrections.Type1MET.pfMETsysShiftCorrections_cfi")
190  if postfix != "":
191  configtools.cloneProcessingSnippet(process, process.pfMEtSysShiftCorrSequence, postfix)
192 
193  getattr(process, "pfMEtSysShiftCorr" + postfix).parameter = sysShiftCorrParameter
194  metPatSequence += getattr(process, "pfMEtSysShiftCorrSequence" + postfix)
195 
196  patMetCorrectionsCentralValue.extend([ cms.InputTag('pfMEtSysShiftCorr' + postfix) ])
197 
198 
199  #finalize T1/T2 correction process
200  if applyT1Cor :
201  getattr(process, metModNameT1 + postfix).srcType1Corrections = cms.VInputTag(patMetCorrectionsCentralValue)
202  if applyT2Cor :
203  getattr(process, metModNameT1T2 + postfix).srcType1Corrections = cms.VInputTag(patMetCorrectionsCentralValue)
204 
205 
206  # Apply unclustered energy calibration on pfMET T1T2 if asked -> discard type2 and replace it with
207  # calibration computed with the jet residual correction
208  if metType == "PF":
209  if applyUncEnCalib:
210  applyUnclEnergyCalibrationOnPfT1T2Met(process, postfix)
211  patPFMetT1T2 = getattr(process, metModNameT1T2)
212  patPFMetT1T2.applyType2Corrections = cms.bool(True)
213  patPFMetT1T2.srcUnclEnergySums = cms.VInputTag(
214  cms.InputTag('pfCandMETresidualCorr' + postfix),
215  cms.InputTag("patPFJetMETtype1p2Corr" + postfix, "type2")
216  )
217  patPFMetT1T2.type2CorrFormula = cms.string("A")
218  patPFMetT1T2.type2CorrParameter = cms.PSet(A = cms.double(2.))
219 
220 
221  collectionsToKeep = [ 'patPFMet' + postfix ]
222  if applyT1Cor:
223  collectionsToKeep.append( metModNameT1 + postfix )
224  if applyT2Cor:
225  collectionsToKeep.append( metModNameT1T2 + postfix )
226 
227  return (metModName, metModNameT1, metModNameT1T2, collectionsToKeep)
228 
229 
230 
231 
233 
234  patPFJetMETtype1p2Corr = getattr(process, "patPFJetMETtype1p2Corr" + postfix)
235  patPFJetMETtype1p2Corr.type2ResidualCorrLabel = cms.string("")
236  patPFJetMETtype1p2Corr.type2ResidualCorrEtaMax = cms.double(9.9)
237  patPFJetMETtype1p2Corr.type2ResidualCorrOffset = cms.double(1.)
238  patPFJetMETtype1p2Corr.type2ExtraCorrFactor = cms.double(1.)
239  patPFJetMETtype1p2Corr.isMC = cms.bool(True)
240  patPFJetMETtype1p2Corr.srcGenPileUpSummary = cms.InputTag('addPileupInfo')
241  patPFJetMETtype1p2Corr.type2ResidualCorrVsNumPileUp = cms.PSet(
242  data = cms.PSet(
243  offset = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_Data_runs190456to208686_pfCands_offset.txt'),
244  slope = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_Data_runs190456to208686_pfCands_slope.txt')
245  ),
246  mc = cms.PSet(
247  offset = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_ZplusJets_madgraph_pfCands_offset.txt'),
248  slope = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_ZplusJets_madgraph_pfCands_slope.txt')
249  )
250  )
251  patPFJetMETtype1p2Corr.verbosity = cms.int32(0)
252  pfCandMETcorr = getattr(process, "pfCandMETcorr" + postfix)
253  pfCandMETresidualCorr = pfCandMETcorr.clone(
254  residualCorrLabel = cms.string(""),
255  residualCorrEtaMax = cms.double(9.9),
256  residualCorrOffset = cms.double(1.),
257  extraCorrFactor = cms.double(1.),
258  isMC = cms.bool(True),
259  srcGenPileUpSummary = cms.InputTag('addPileupInfo'),
260  residualCorrVsNumPileUp = cms.PSet(
261  data = cms.PSet(
262  offset = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_Data_runs190456to208686_pfCands_offset.txt'),
263  slope = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_Data_runs190456to208686_pfCands_slope.txt')
264  ),
265  mc = cms.PSet(
266  offset = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_ZplusJets_madgraph_pfCands_offset.txt'),
267  slope = cms.FileInPath('JetMETCorrections/Type1MET/data/unclEnResidualCorr_ZplusJets_madgraph_pfCands_slope.txt')
268  )
269  ),
270  verbosity = cms.int32(0)
271  )
272  setattr(process, "pfCandMETresidualCorr" + postfix, pfCandMETresidualCorr)
273  getattr(process, "producePatPFMETCorrectionsUnc" + postfix).replace(pfCandMETcorr, pfCandMETcorr + pfCandMETresidualCorr)
274 
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135