CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pfTools.py
Go to the documentation of this file.
2 
6 
7 from PhysicsTools.PatAlgos.tools.helpers import listModules, applyPostfix
8 
9 from copy import deepcopy
10 
12  print "WARNING: particle based isolation must be studied"
13 
14 from CommonTools.ParticleFlow.Tools.pfIsolation import setupPFElectronIso, setupPFMuonIso
15 
16 def useGsfElectrons(process, postfix, dR = "04"):
17  print "using Gsf Electrons in PF2PAT"
18  print "WARNING: this will destory the feature of top projection which solves the ambiguity between leptons and jets because"
19  print "WARNING: there will be overlap between non-PF electrons and jets even though top projection is ON!"
20  print "********************* "
21  module = applyPostfix(process,"patElectrons",postfix)
22  module.useParticleFlow = False
23  print "Building particle-based isolation for GsfElectrons in PF2PAT(PFBRECO)"
24  print "********************* "
25  adaptPFIsoElectrons( process, module, postfix+"PFIso", dR )
26  getattr(process,'patDefaultSequence'+postfix).replace( getattr(process,"patElectrons"+postfix),
27  setupPFElectronIso(process, 'gsfElectrons', "PFIso", postfix, runPF2PAT=True) +
28  getattr(process,"patElectrons"+postfix) )
29 
30 def adaptPFIsoElectrons(process,module, postfix = "PFIso", dR = "04"):
31  #FIXME: adaptPFElectrons can use this function.
32  module.isoDeposits = cms.PSet(
33  pfChargedHadrons = cms.InputTag("elPFIsoDepositCharged" + postfix),
34  pfChargedAll = cms.InputTag("elPFIsoDepositChargedAll" + postfix),
35  pfPUChargedHadrons = cms.InputTag("elPFIsoDepositPU" + postfix),
36  pfNeutralHadrons = cms.InputTag("elPFIsoDepositNeutral" + postfix),
37  pfPhotons = cms.InputTag("elPFIsoDepositGamma" + postfix)
38  )
39  module.isolationValues = cms.PSet(
40  pfChargedHadrons = cms.InputTag("elPFIsoValueCharged"+dR+"PFId"+ postfix),
41  pfChargedAll = cms.InputTag("elPFIsoValueChargedAll"+dR+"PFId"+ postfix),
42  pfPUChargedHadrons = cms.InputTag("elPFIsoValuePU"+dR+"PFId" + postfix),
43  pfNeutralHadrons = cms.InputTag("elPFIsoValueNeutral"+dR+"PFId" + postfix),
44  pfPhotons = cms.InputTag("elPFIsoValueGamma"+dR+"PFId" + postfix)
45  )
46  module.isolationValuesNoPFId = cms.PSet(
47  pfChargedHadrons = cms.InputTag("elPFIsoValueCharged"+dR+"NoPFId"+ postfix),
48  pfChargedAll = cms.InputTag("elPFIsoValueChargedAll"+dR+"NoPFId"+ postfix),
49  pfPUChargedHadrons = cms.InputTag("elPFIsoValuePU"+dR+"NoPFId" + postfix),
50  pfNeutralHadrons = cms.InputTag("elPFIsoValueNeutral"+dR+"NoPFId" + postfix),
51  pfPhotons = cms.InputTag("elPFIsoValueGamma"+dR+"NoPFId" + postfix)
52  )
53 
54 def adaptPFIsoMuons(process,module, postfix = "PFIso", dR = "04"):
55  #FIXME: adaptPFMuons can use this function.
56  module.isoDeposits = cms.PSet(
57  pfChargedHadrons = cms.InputTag("muPFIsoDepositCharged" + postfix),
58  pfChargedAll = cms.InputTag("muPFIsoDepositChargedAll" + postfix),
59  pfPUChargedHadrons = cms.InputTag("muPFIsoDepositPU" + postfix),
60  pfNeutralHadrons = cms.InputTag("muPFIsoDepositNeutral" + postfix),
61  pfPhotons = cms.InputTag("muPFIsoDepositGamma" + postfix)
62  )
63  module.isolationValues = cms.PSet(
64  pfChargedHadrons = cms.InputTag("muPFIsoValueCharged" + dR + postfix),
65  pfChargedAll = cms.InputTag("muPFIsoValueChargedAll" + dR + postfix),
66  pfPUChargedHadrons = cms.InputTag("muPFIsoValuePU" + dR + postfix),
67  pfNeutralHadrons = cms.InputTag("muPFIsoValueNeutral" + dR + postfix),
68  pfPhotons = cms.InputTag("muPFIsoValueGamma" + dR + postfix)
69  )
70 
71 def usePFIso(process, postfix = "PFIso"):
72  print "Building particle-based isolation "
73  print "***************** "
74  process.eleIsoSequence = setupPFElectronIso(process, 'gsfElectrons', postfix)
75  process.muIsoSequence = setupPFMuonIso(process, 'muons', postfix)
76  adaptPFIsoMuons( process, applyPostfix(process,"patMuons",""), postfix)
77  adaptPFIsoElectrons( process, applyPostfix(process,"patElectrons",""), postfix)
78  getattr(process,'patDefaultSequence').replace( getattr(process,"patCandidates"),
79  process.pfParticleSelectionSequence +
80  process.eleIsoSequence +
81  process.muIsoSequence +
82  getattr(process,"patCandidates") )
83 
84 def adaptPFMuons(process,module,postfix="" ):
85  print "Adapting PF Muons "
86  print "***************** "
87  #warningIsolation()
88  print
89  module.useParticleFlow = True
90  module.pfMuonSource = cms.InputTag("pfIsolatedMuons" + postfix)
91  module.userIsolation = cms.PSet()
92  ## module.isoDeposits = cms.PSet(
93  ## pfChargedHadrons = cms.InputTag("muPFIsoDepositCharged" + postfix),
94  ## pfChargedAll = cms.InputTag("muPFIsoDepositChargedAll" + postfix),
95  ## pfPUChargedHadrons = cms.InputTag("muPFIsoDepositPU" + postfix),
96  ## pfNeutralHadrons = cms.InputTag("muPFIsoDepositNeutral" + postfix),
97  ## pfPhotons = cms.InputTag("muPFIsoDepositGamma" + postfix)
98  ## )
99  ## module.isolationValues = cms.PSet(
100  ## pfChargedHadrons = cms.InputTag("muPFIsoValueCharged04"+ postfix),
101  ## pfChargedAll = cms.InputTag("muPFIsoValueChargedAll04"+ postfix),
102  ## pfPUChargedHadrons = cms.InputTag("muPFIsoValuePU04" + postfix),
103  ## pfNeutralHadrons = cms.InputTag("muPFIsoValueNeutral04" + postfix),
104  ## pfPhotons = cms.InputTag("muPFIsoValueGamma04" + postfix)
105  ## )
106  # matching the pfMuons, not the standard muons.
107  applyPostfix(process,"muonMatch",postfix).src = module.pfMuonSource
108 
109  print " muon source:", module.pfMuonSource
110  ## print " isolation :",
111  ## print module.isolationValues
112  ## print " isodeposits: "
113  ## print module.isoDeposits
114  print
115 
116 
117 def adaptPFElectrons(process,module, postfix):
118  # module.useParticleFlow = True
119  print "Adapting PF Electrons "
120  print "********************* "
121  #warningIsolation()
122  print
123  module.useParticleFlow = True
124  module.pfElectronSource = cms.InputTag("pfIsolatedElectrons" + postfix)
125  module.userIsolation = cms.PSet()
126  ## module.isoDeposits = cms.PSet(
127  ## pfChargedHadrons = cms.InputTag("elPFIsoDepositCharged" + postfix),
128  ## pfChargedAll = cms.InputTag("elPFIsoDepositChargedAll" + postfix),
129  ## pfPUChargedHadrons = cms.InputTag("elPFIsoDepositPU" + postfix),
130  ## pfNeutralHadrons = cms.InputTag("elPFIsoDepositNeutral" + postfix),
131  ## pfPhotons = cms.InputTag("elPFIsoDepositGamma" + postfix)
132  ## )
133  ## module.isolationValues = cms.PSet(
134  ## pfChargedHadrons = cms.InputTag("elPFIsoValueCharged04PFId"+ postfix),
135  ## pfChargedAll = cms.InputTag("elPFIsoValueChargedAll04PFId"+ postfix),
136  ## pfPUChargedHadrons = cms.InputTag("elPFIsoValuePU04PFId" + postfix),
137  ## pfNeutralHadrons = cms.InputTag("elPFIsoValueNeutral04PFId" + postfix),
138  ## pfPhotons = cms.InputTag("elPFIsoValueGamma04PFId" + postfix)
139  ## )
140 
141  # COLIN: since we take the egamma momentum for pat Electrons, we must
142  # match the egamma electron to the gen electrons, and not the PFElectron.
143  # -> do not uncomment the line below.
144  # process.electronMatch.src = module.pfElectronSource
145  # COLIN: how do we depend on this matching choice?
146 
147  print " PF electron source:", module.pfElectronSource
148  ## print " isolation :"
149  ## print module.isolationValues
150  ## print " isodeposits: "
151  ## print module.isoDeposits
152  print
153 
154 
155 
156 
157 def adaptPFPhotons(process,module):
158  raise RuntimeError, "Photons are not supported yet"
159 
160 from RecoTauTag.RecoTau.TauDiscriminatorTools import adaptTauDiscriminator, producerIsTauTypeMapper
161 
162 def reconfigurePF2PATTaus(process,
163  tauType='shrinkingConePFTau',
164  pf2patSelection=["DiscriminationByIsolation", "DiscriminationByLeadingPionPtCut"],
165  selectionDependsOn=["DiscriminationByLeadingTrackFinding"],
166  producerFromType=lambda producer: producer+"Producer",
167  postfix = ""):
168  print "patTaus will be produced from taus of type: %s that pass %s" \
169  % (tauType, pf2patSelection)
170 
171 
172 
173  # Get the prototype of tau producer to make, i.e. fixedConePFTauProducer
174  producerName = producerFromType(tauType)
175  # Set as the source for the pf2pat taus (pfTaus) selector
176  applyPostfix(process,"pfTaus", postfix).src = producerName+postfix
177  # Start our pf2pat taus base sequence
178  oldTauSansRefs = getattr(process,'pfTausProducerSansRefs'+postfix)
179  oldTau = getattr(process,'pfTausProducer'+postfix)
180  ## copy tau and setup it properly
181  newTauSansRefs = None
182  newTau = getattr(process,producerName+postfix).clone()
183 
184  ## adapted to new structure in RecoTauProducers PLEASE CHECK!!!
185  if tauType=='shrinkingConePFTau':
186  newTauSansRefs = getattr(process,producerName+"SansRefs").clone()
187  newTauSansRefs.modifiers[1] = cms.PSet(
188  pfTauTagInfoSrc = cms.InputTag("pfTauTagInfoProducer"+postfix),
189  name = cms.string('pfTauTTIworkaround'+postfix),
190  plugin = cms.string('RecoTauTagInfoWorkaroundModifer')
191  )
192  newTau.modifiers[1] = newTauSansRefs.modifiers[1]
193  newTauSansRefs.piZeroSrc = "pfJetsLegacyTaNCPiZeros"+postfix
194  newTau.piZeroSrc = newTauSansRefs.piZeroSrc
195  newTauSansRefs.builders[0].pfCandSrc = oldTauSansRefs.builders[0].pfCandSrc
196  newTauSansRefs.jetRegionSrc = oldTauSansRefs.jetRegionSrc
197  newTauSansRefs.jetSrc = oldTauSansRefs.jetSrc
198  elif tauType=='fixedConePFTau':
199  newTau.piZeroSrc = "pfJetsLegacyTaNCPiZeros"+postfix
200  elif tauType=='hpsPFTau':
201  newTau = getattr(process,'combinatoricRecoTaus'+postfix).clone()
202  newTau.piZeroSrc="pfJetsLegacyHPSPiZeros"+postfix
203  newTau.modifiers[3] = cms.PSet(
204  pfTauTagInfoSrc = cms.InputTag("pfTauTagInfoProducer"+postfix),
205  name = cms.string('pfTauTTIworkaround'+postfix),
206  plugin = cms.string('RecoTauTagInfoWorkaroundModifer')
207  )
208  from PhysicsTools.PatAlgos.tools.helpers import cloneProcessingSnippet
209  #cloneProcessingSnippet(process, process.produceHPSPFTaus, postfix)
210  setattr(process,'produceHPSPFTaus'+postfix,cms.Sequence(applyPostfix(process,'hpsSelectionDiscriminator',postfix)+applyPostfix(process,'hpsPFTauProducerSansRefs',postfix)+applyPostfix(process,'hpsPFTauProducer',postfix)))
211  massSearchReplaceParam(getattr(process,"produceHPSPFTaus"+postfix),
212  "PFTauProducer",
213  cms.InputTag("combinatoricRecoTaus"+postfix),
214  cms.InputTag("pfTausBase"+postfix) )
215  massSearchReplaceParam(getattr(process,"produceHPSPFTaus"+postfix),
216  "src",
217  cms.InputTag("combinatoricRecoTaus"+postfix),
218  cms.InputTag("pfTausBase"+postfix) )
219  ### Next three lines crash, oldTau does not have any of these attributes. Why?###
220  #newTau.builders[0].pfCandSrc = oldTau.builders[0].pfCandSrc
221  #newTau.jetRegionSrc = oldTau.jetRegionSrc
222  #newTau.jetSrc = oldTau.jetSrc
223  #newTau.builders[0].pfCandSrc = cms.InputTag("pfNoElectronJMEPFlow")
224  #newTau.jetRegionSrc = cms.InputTag("pfTauPFJets08RegionPFlow")
225  #newTau.jetSrc = cms.InputTag("pfJetsPFlow")
226  # replace old tau producer by new one put it into baseSequence
227  setattr(process,"pfTausBase"+postfix,newTau)
228  if tauType=='shrinkingConePFTau':
229  setattr(process,"pfTausBaseSansRefs"+postfix,newTauSansRefs)
230  getattr(process,"pfTausBase"+postfix).src = "pfTausBaseSansRefs"+postfix
231  baseSequence += getattr(process,"pfTausBaseSansRefs"+postfix)
232 
233  #make custom mapper to take postfix into account (could have gone with lambda of lambda but... )
234  def producerIsTauTypeMapperWithPostfix(tauProducer):
235  return lambda x: producerIsTauTypeMapper(tauProducer)+x.group(1)+postfix
236 
237  def recoTauTypeMapperWithGroup(tauProducer):
238  return "%s(.*)"%recoTauTypeMapper(tauProducer)
239 
240  # Get our prediscriminants
241  for predisc in selectionDependsOn:
242  # Get the prototype
243  originalName = tauType+predisc # i.e. fixedConePFTauProducerDiscriminationByLeadingTrackFinding
244  clonedName = "pfTausBase"+predisc+postfix
245  clonedDisc = getattr(process, originalName).clone()
246  setattr(process, clonedName, clonedDisc)
247 
248  tauCollectionToSelect = None
249  if tauType != 'hpsPFTau' :
250  tauCollectionToSelect = "pfTausBase"+postfix
251  #cms.InputTag(clonedDisc.PFTauProducer.value()+postfix)
252  else:
253  tauCollectionToSelect = "hpsPFTauProducer"+postfix
254  # Adapt this discriminator for the cloned prediscriminators
255  adaptTauDiscriminator(clonedDisc, newTauProducer="pfTausBase",
256  oldTauTypeMapper=recoTauTypeMapperWithGroup,
257  newTauTypeMapper=producerIsTauTypeMapperWithPostfix,
258  preservePFTauProducer=True)
259  clonedDisc.PFTauProducer = tauCollectionToSelect
260 
261  # Reconfigure the pf2pat PFTau selector discrimination sources
262  getattr(process,"pfTaus" + postfix).discriminators = cms.VPSet()
263  for selection in pf2patSelection:
264  # Get our discriminator that will be used to select pfTaus
265  originalName = tauType+selection
266  clonedName = "pfTausBase"+selection+postfix
267  clonedDisc = getattr(process, originalName).clone()
268  setattr(process, clonedName, clonedDisc)
269 
270  tauCollectionToSelect = None
271 
272  if tauType != 'hpsPFTau' :
273  tauCollectionToSelect = cms.InputTag("pfTausBase"+postfix)
274  #cms.InputTag(clonedDisc.PFTauProducer.value()+postfix)
275  else:
276  tauCollectionToSelect = cms.InputTag("hpsPFTauProducer"+postfix)
277  #Adapt our cloned discriminator to the new prediscriminants
278  adaptTauDiscriminator(clonedDisc, newTauProducer="pfTausBase",
279  oldTauTypeMapper=recoTauTypeMapperWithGroup,
280  newTauTypeMapper=producerIsTauTypeMapperWithPostfix,
281  preservePFTauProducer=True)
282  clonedDisc.PFTauProducer = tauCollectionToSelect
283 
284  # Add this selection to our pfTau selectors
285  getattr(process,"pfTaus" + postfix).discriminators.append(cms.PSet(
286  discriminator=cms.InputTag(clonedName), selectionCut=cms.double(0.5)))
287  # Set the input of the final selector.
288  if tauType != 'hpsPFTau':
289  getattr(process,"pfTaus" + postfix).src = "pfTausBase"+postfix
290  else:
291  # If we are using HPS taus, we need to take the output of the clenaed
292  # collection
293  getattr(process,"pfTaus" + postfix).src = "hpsPFTauProducer"+postfix
294 
295 
296 
297 def adaptPFTaus(process,tauType = 'shrinkingConePFTau', postfix = ""):
298  # Set up the collection used as a preselection to use this tau type
299  if tauType != 'hpsPFTau' :
300  reconfigurePF2PATTaus(process, tauType, postfix=postfix)
301  else:
302  reconfigurePF2PATTaus(process, tauType,
303  ["DiscriminationByLooseCombinedIsolationDBSumPtCorr"],
304  ["DiscriminationByDecayModeFinding"],
305  postfix=postfix)
306  # new default use unselected taus (selected only for jet cleaning)
307  if tauType != 'hpsPFTau' :
308  getattr(process,"patTaus" + postfix).tauSource = cms.InputTag("pfTausBase"+postfix)
309  else:
310  getattr(process,"patTaus" + postfix).tauSource = cms.InputTag("hpsPFTauProducer"+postfix)
311  # to use preselected collection (old default) uncomment line below
312  #applyPostfix(process,"patTaus", postfix).tauSource = cms.InputTag("pfTaus"+postfix)
313 
314  ### apparently not needed anymore, function gone from tauTools.py###
315  #redoPFTauDiscriminators(process,
316  #cms.InputTag(tauType+'Producer'),
317  #applyPostfix(process,"patTaus", postfix).tauSource,
318  #tauType, postfix=postfix)
319 
320 
321  if tauType != 'hpsPFTau' :
322  switchToPFTauByType(process, pfTauType=tauType,
323  patTauLabel="pfTausBase"+postfix,
324  tauSource=cms.InputTag(tauType+'Producer'+postfix),
325  postfix=postfix)
326  getattr(process,"patTaus" + postfix).tauSource = cms.InputTag("pfTausBase"+postfix)
327  else:
328  switchToPFTauByType(process, pfTauType=tauType,
329  patTauLabel="",
330  tauSource=cms.InputTag(tauType+'Producer'+postfix),
331  postfix=postfix)
332  getattr(process,"patTaus" + postfix).tauSource = cms.InputTag("hpsPFTauProducer"+postfix)
333 
334 
335 
336 #helper function for PAT on PF2PAT sample
337 def tauTypeInPF2PAT(process,tauType='shrinkingConePFTau', postfix = ""):
338  process.load("CommonTools.ParticleFlow.pfTaus_cff")
339  applyPostfix(process, "pfTaus",postfix).src = cms.InputTag(tauType+'Producer'+postfix)
340 
341 
342 def addPFCandidates(process,src,patLabel='PFParticles',cut="",postfix=""):
343  from PhysicsTools.PatAlgos.producersLayer1.pfParticleProducer_cfi import patPFParticles
344  # make modules
345  producer = patPFParticles.clone(pfCandidateSource = src)
346  filter = cms.EDFilter("PATPFParticleSelector",
347  src = cms.InputTag("pat" + patLabel),
348  cut = cms.string(cut))
349  counter = cms.EDFilter("PATCandViewCountFilter",
350  minNumber = cms.uint32(0),
351  maxNumber = cms.uint32(999999),
352  src = cms.InputTag("pat" + patLabel))
353  # add modules to process
354  setattr(process, "pat" + patLabel, producer)
355  setattr(process, "selectedPat" + patLabel, filter)
356  setattr(process, "countPat" + patLabel, counter)
357 
358  # summary tables
359  applyPostfix(process, "patCandidateSummary", postfix).candidates.append(cms.InputTag('pat' + patLabel))
360  applyPostfix(process, "selectedPatCandidateSummary", postfix).candidates.append(cms.InputTag('selectedPat' + patLabel))
361 
362 
363 def switchToPFMET(process,input=cms.InputTag('pfMET'), type1=False, postfix=""):
364  print 'MET: using ', input
365  if( not type1 ):
366  oldMETSource = applyPostfix(process, "patMETs",postfix).metSource
367  applyPostfix(process, "patMETs",postfix).metSource = input
368  applyPostfix(process, "patMETs",postfix).addMuonCorrections = False
369  else:
370  # type1 corrected MET
371  # name of corrected MET hardcoded in PAT and meaningless
372  print 'Apply TypeI corrections for MET'
373  #getattr(process, "patPF2PATSequence"+postfix).remove(applyPostfix(process, "patMETCorrections",postfix))
374  jecLabel = getattr(process,'patJetCorrFactors'+postfix).payload.pythonValue().replace("'","")
375  getattr(process,jecLabel+'Type1CorMet'+postfix).src = input.getModuleLabel()
376  #getattr(process,'patMETs'+postfix).metSource = jecLabel+'Type1CorMet'+postfix
377  #getattr(process,'patMETs'+postfix).addMuonCorrections = False
378 
379 def switchToPFJets(process, input=cms.InputTag('pfNoTauClones'), algo='AK5', postfix = "", jetCorrections=('AK5PFchs', ['L1FastJet','L2Relative', 'L3Absolute']), type1=False, outputModules=['out']):
380 
381  print "Switching to PFJets, ", algo
382  print "************************ "
383  print "input collection: ", input
384 
385  if( algo == 'IC5' ):
386  genJetCollection = cms.InputTag('iterativeCone5GenJetsNoNu')
387  elif algo == 'AK5':
388  genJetCollection = cms.InputTag('ak5GenJetsNoNu')
389  elif algo == 'AK7':
390  genJetCollection = cms.InputTag('ak7GenJetsNoNu')
391  else:
392  print 'bad jet algorithm:', algo, '! for now, only IC5, AK5 and AK7 are allowed. If you need other algorithms, please contact Colin'
393  sys.exit(1)
394 
395  # changing the jet collection in PF2PAT:
396  from CommonTools.ParticleFlow.Tools.jetTools import jetAlgo
397  inputCollection = getattr(process,"pfJets"+postfix).src
398  setattr(process,"pfJets"+postfix,jetAlgo(algo)) # problem for cfgBrowser
399  getattr(process,"pfJets"+postfix).src = inputCollection
400  inputJetCorrLabel=jetCorrections
401 
402  switchJetCollection(process,
403  jetSource = input,
404  algo=algo,
405  postfix=postfix,
406  jetTrackAssociation=True,
407  jetCorrections=inputJetCorrLabel,
408  outputModules = outputModules,
409  #btagDiscriminators = ['combinedSecondaryVertexBJetTags',]
410  )
411 
412  # check whether L1FastJet is in the list of correction levels or not
413  applyPostfix(process, "patJetCorrFactors", postfix).useRho = False
414  for corr in inputJetCorrLabel[1]:
415  if corr == 'L1FastJet':
416  applyPostfix(process, "patJetCorrFactors", postfix).useRho = True
417  applyPostfix(process, "pfJets", postfix).doAreaFastjet = True
418  # do correct treatment for TypeI MET corrections
419  #type1=True
420  if type1:
421  for mod in process.producerNames().split(' '):
422 
423  if mod.startswith("kt6") and mod.endswith("Jets"+postfix) and not 'GenJets' in mod:
424  prefix = mod.replace(postfix,'')
425  prefix = prefix.replace('kt6PFJets','')
426  prefix = prefix.replace('kt6CaloJets','')
427  prefix = getattr(process,'patJetCorrFactors'+prefix+postfix).payload.pythonValue().replace("'","")
428  for essource in process.es_sources_().keys():
429  if essource == prefix+'L1FastJet':
430  setattr(process,essource+postfix,getattr(process,essource).clone(srcRho=cms.InputTag(mod,'rho')))
431  setattr(process,prefix+'CombinedCorrector'+postfix,getattr(process,prefix+'CombinedCorrector').clone())
432  getattr(process,prefix+'CorMet'+postfix).corrector = prefix+'CombinedCorrector'+postfix
433  for cor in getattr(process,prefix+'CombinedCorrector'+postfix).correctors:
434  if cor == essource:
435  idx = getattr(process,prefix+'CombinedCorrector'+postfix).correctors.index(essource);
436  getattr(process,prefix+'CombinedCorrector'+postfix).correctors[idx] = essource+postfix
437 
438  if hasattr( getattr( process, "patJets" + postfix), 'embedCaloTowers' ): # optional parameter, which defaults to 'False' anyway
439  applyPostfix(process, "patJets", postfix).embedCaloTowers = False
440  applyPostfix(process, "patJets", postfix).embedPFCandidates = True
441 
442 #-- Remove MC dependence ------------------------------------------------------
443 def removeMCMatchingPF2PAT( process, postfix="", outputModules=['out'] ):
444  #from PhysicsTools.PatAlgos.tools.coreTools import removeMCMatching
445  ### no longe needed in unscheduled mode###
446  #removeIfInSequence(process, "genForPF2PATSequence", "patDefaultSequence", postfix)
447  removeMCMatching(process, names=['All'], postfix=postfix, outputModules=outputModules)
448 
449 
450 def adaptPVs(process, pvCollection=cms.InputTag('offlinePrimaryVertices'), postfix=''):
451  print "Switching PV collection for PF2PAT:", pvCollection
452  print "***********************************"
453 
454  # PV sources to be exchanged:
455  pvExchange = ['Vertices','vertices','pvSrc','primaryVertices','srcPVs']
456  # PV sources NOT to be exchanged:
457  #noPvExchange = ['src','PVProducer','primaryVertexSrc','vertexSrc','primaryVertex']
458 
459  # find out all added jet collections (they don't belong to PF2PAT)
460  interPostfixes = []
461  for m in process.producerNames().split(' '):
462  if m.startswith('patJets') and m.endswith(postfix) and not len(m)==len('patJets')+len(postfix):
463  interPostfix = m.replace('patJets','')
464  interPostfix = interPostfix.replace(postfix,'')
465  interPostfixes.append(interPostfix)
466 
467  # exchange the primary vertex source of all relevant modules
468  for m in process.producerNames().split(' '):
469  modName = m.replace(postfix,'')
470  # only if the module has a source with a relevant name
471  for namePvSrc in pvExchange:
472  if hasattr(getattr(process,m),namePvSrc):
473  # only if the module is not coming from an added jet collection
474  interPostFixFlag = False
475  for pfix in interPostfixes:
476  if modName.endswith(pfix):
477  interPostFixFlag = True
478  break
479  if not interPostFixFlag:
480  setattr(getattr(process,m),namePvSrc,deepcopy(pvCollection))
481 
482 
483 def usePF2PAT(process,runPF2PAT=True, jetAlgo='ak5', runOnMC=True, postfix="", jetCorrections=('AK5PFchs', ['L1FastJet','L2Relative','L3Absolute'],'None'), pvCollection=cms.InputTag('offlinePrimaryVertices',), typeIMetCorrections=False, outputModules=['out'],excludeFromTopProjection=['Tau']):
484  # PLEASE DO NOT CLOBBER THIS FUNCTION WITH CODE SPECIFIC TO A GIVEN PHYSICS OBJECT.
485  # CREATE ADDITIONAL FUNCTIONS IF NEEDED.
486 
487  if typeIMetCorrections:
488  jetCorrections = (jetCorrections[0],jetCorrections[1],'Type-1')
489  """Switch PAT to use PF2PAT instead of AOD sources. if 'runPF2PAT' is true, we'll also add PF2PAT in front of the PAT sequence"""
490 
491  # -------- CORE ---------------
492  from PhysicsTools.PatAlgos.tools.helpers import loadWithPostfix
493  if runPF2PAT:
494  loadWithPostfix(process,'PhysicsTools.PatAlgos.patSequences_cff',postfix)
495  loadWithPostfix(process,"CommonTools.ParticleFlow.PF2PAT_cff",postfix)
496  else:
497  loadWithPostfix(process,'PhysicsTools.PatAlgos.patSequences_cff',postfix)
498 
499 
500 
501 
502  # -------- OBJECTS ------------
503  # Muons
504 
505  adaptPFMuons(process,
506  applyPostfix(process,"patMuons",postfix),
507  postfix)
508 
509  # Electrons
510 
511  adaptPFElectrons(process,
512  applyPostfix(process,"patElectrons",postfix),
513  postfix)
514 
515  # Photons
516  #print "Temporarily switching off photons completely"
517 
518  #removeSpecificPATObjects(process,names=['Photons'],outputModules=outputModules,postfix=postfix)
519 
520  # Jets
521  if runOnMC :
522  switchToPFJets( process, cms.InputTag('pfNoTauClones'+postfix), jetAlgo, postfix=postfix,
523  jetCorrections=jetCorrections, type1=typeIMetCorrections, outputModules=outputModules )
524 
525  else :
526  if not 'L2L3Residual' in jetCorrections[1]:
527  ### think of a more accurate warning
528  print '#################################################'
529  print 'WARNING! Not using L2L3Residual but this is data.'
530  print 'If this is okay with you, disregard this message.'
531  print '#################################################'
532  switchToPFJets( process, cms.InputTag('pfNoTauClones'+postfix), jetAlgo, postfix=postfix,
533  jetCorrections=jetCorrections, type1=typeIMetCorrections, outputModules=outputModules )
534  # Taus
535  #adaptPFTaus( process, tauType='shrinkingConePFTau', postfix=postfix )
536  #adaptPFTaus( process, tauType='fixedConePFTau', postfix=postfix )
537  adaptPFTaus( process, tauType='hpsPFTau', postfix=postfix )
538  # MET
539  switchToPFMET(process, cms.InputTag('pfMET'+postfix), type1=typeIMetCorrections, postfix=postfix)
540 
541  # Unmasked PFCandidates
542  addPFCandidates(process,cms.InputTag('pfNoJetClones'+postfix),patLabel='PFParticles'+postfix,cut="",postfix=postfix)
543 
544  # adapt primary vertex collection
545  adaptPVs(process, pvCollection=pvCollection, postfix=postfix)
546 
547  if runOnMC:
548 
549  loadWithPostfix(process,"CommonTools.ParticleFlow.genForPF2PAT_cff",postfix)
550 
551  else:
552  runOnData(process,postfix=postfix,outputModules=outputModules)
553 
554  # Configure Top Projections
555  getattr(process,"pfNoPileUp"+postfix).enable = True
556  getattr(process,"pfNoMuon"+postfix).enable = True
557  getattr(process,"pfNoElectron"+postfix).enable = True
558  getattr(process,"pfNoTau"+postfix).enable = False
559  getattr(process,"pfNoJet"+postfix).enable = True
560  exclusionList = ''
561  for object in excludeFromTopProjection:
562  getattr(process,"pfNo"+object+postfix).enable = False
563  exclusionList=exclusionList+object+','
564  exclusionList=exclusionList.rstrip(',')
565  print "Done: PF2PAT interfaced to PAT, postfix=", postfix,", Exluded from Top Projection:",exclusionList
def setupPFMuonIso
Definition: pfIsolation.py:112
def adaptPFPhotons
Definition: pfTools.py:157
def switchToPFMET
Definition: pfTools.py:363
def adaptPFTaus
Definition: pfTools.py:297
def tauTypeInPF2PAT
Definition: pfTools.py:337
string jetAlgo
Jet configuration.
def adaptPFIsoMuons
Definition: pfTools.py:54
def replace
Definition: linker.py:10
def adaptPFMuons
Definition: pfTools.py:84
def removeMCMatchingPF2PAT
Definition: pfTools.py:443
def reconfigurePF2PATTaus
Definition: pfTools.py:167
def applyPostfix
Definition: helpers.py:70
def usePF2PAT
Definition: pfTools.py:483
def massSearchReplaceParam
Definition: helpers.py:245
def addPFCandidates
Definition: pfTools.py:342
def adaptPVs
Definition: pfTools.py:450
def adaptPFElectrons
Definition: pfTools.py:117
def switchToPFJets
Definition: pfTools.py:379
def loadWithPostfix
Definition: helpers.py:18
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
def useGsfElectrons
Definition: pfTools.py:16
if(dp >Float(M_PI)) dp-
def warningIsolation
Definition: pfTools.py:11
def usePFIso
Definition: pfTools.py:71
def switchToPFTauByType
Definition: tauTools.py:184
def setupPFElectronIso
Definition: pfIsolation.py:121
double split
Definition: MVATrainer.cc:139
def adaptPFIsoElectrons
Definition: pfTools.py:30