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