CMS 3D CMS Logo

TauDiscriminatorTools.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
3 # require the EXISTANCE of a track - not necessarily above any pt cut (above the basic 0.5 GeV filter)
4 leadTrackFinding = cms.PSet(
5  Producer = cms.InputTag('pfRecoTauDiscriminationByLeadingTrackFinding'),
6  cut = cms.double(0.5)
7 )
8 
9 # Require no prediscriminants
10 noPrediscriminants = cms.PSet(
11  BooleanOperator = cms.string("and"),
12  )
13 
14 # Require the existence of a lead track
15 requireLeadTrack = cms.PSet(
16  BooleanOperator = cms.string("and"),
17  leadTrack = leadTrackFinding,
18  )
19 
20 # This is equivalent to the lead track case, and shoudl be deprecated.
21 # Preserved for backwards compatibility
22 requireLeadPion = cms.PSet(
23  BooleanOperator = cms.string("and"),
24  leadPion = leadTrackFinding,
25  )
26 
27 def subParameterSets(pSet):
28  ''' Generator to return all sub-PSets in a PSet '''
29  for name, value in pSet.parameters_().items():
30  if isinstance(value, cms.PSet):
31  yield getattr(pSet, name)
32 
33 # For RECO type taus, where the tau producer is [tauType]Producer
34 import re
35 recoTauTypeMapperRegex = re.compile("(\w*)Producer")
36 def recoTauTypeMapper(tauProducer):
37  return recoTauTypeMapperRegex.match(tauProducer).group(1)
38 
39 # For taus where the producer name is the type, like "allLayer1Taus", etc
40 producerIsTauTypeMapper = lambda tauProducer: tauProducer
41 
42 def adaptTauDiscriminator(discriminator, newTauProducer='shrinkingConePFTauProducer',
43  oldTauTypeMapper=recoTauTypeMapper, newTauTypeMapper=recoTauTypeMapper,
44  preservePFTauProducer = False):
45  ''' Change a TauDiscriminator to use a different tau/prediscriminant sources
46 
47  Tau discriminators use the following convention:
48  [tauType]DiscriminationByXXX
49 
50  i.e. fixedConePFTauDiscriminationByIsolation,
51  allLayer1TausDiscriminationByIsolation, etc
52 
53  However, the mapping of tauType to tau producer name is not constant. In
54  RECO, the form is [tauType]Producer. In PAT, the producer is just named
55  [tauType]. To manage this oldTauTypeMapper and newTauTypeMapper are
56  functions with signature f(str) that translate a TauProducer name (like
57  shrinkingConePFTauProducer) to its type (shrinkingConePFTau). Two types of
58  mapping are provided,
59  * recoTauTypeMapper
60  shrinkingConePFTauProducer->shrinkingConePFTau
61  * producerIsTauTypeMapper
62  allLayer1Taus->allLayer1Taus
63 
64  '''
65 
66  oldTauProducer = discriminator.PFTauProducer
67  if isinstance(newTauProducer, str):
68  newTauProducer = cms.InputTag(newTauProducer)
69 
70  # This is useful for the PF2PAT case where you wish to set the producer name
71  # seperately
72  if not preservePFTauProducer:
73  discriminator.PFTauProducer = newTauProducer
74 
75  oldTauType = oldTauTypeMapper(oldTauProducer.value())
76  newTauType = newTauTypeMapper(newTauProducer.value())
77 
78  replacementRegex = re.compile(oldTauType)
79 
80  # Adapt all the prediscriminants
81  for prediscriminant in subParameterSets(discriminator.Prediscriminants):
82  oldProducer = prediscriminant.Producer.value()
83  # Replace the old tau type by the new tau type in the prediscrimant
84  # producer
85  prediscriminant.Producer = cms.InputTag(replacementRegex.sub(newTauType,
86  oldProducer))
87 
88 def adaptTauDiscriminatorSequence(sequence, **kwargs):
89  def fixer(discriminator):
90  if hasattr(discriminator, "Prediscriminants"):
91  adaptTauDiscriminator(discriminator, **kwargs)
92  sequence.visit(fixer)
93 
94 def setTauSource(discriminator, newTauProducer):
95  ''' Same as adaptTauDiscriminator, kept for backwards compatibility'''
96  adaptTauDiscriminator(discriminator, newTauProducer)
97 
def setTauSource(discriminator, newTauProducer)
def recoTauTypeMapper(tauProducer)
def adaptTauDiscriminatorSequence(sequence, kwargs)
def adaptTauDiscriminator(discriminator, newTauProducer='shrinkingConePFTauProducer', oldTauTypeMapper=recoTauTypeMapper, newTauTypeMapper=recoTauTypeMapper, preservePFTauProducer=False)