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