CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/RecoTauTag/RecoTau/python/TauDiscriminatorTools.py

Go to the documentation of this file.
00001 import FWCore.ParameterSet.Config as cms
00002 
00003 # require the EXISTANCE of a track - not necessarily above any pt cut (above the basic 0.5 GeV filter)
00004 leadTrackFinding = cms.PSet(
00005       Producer = cms.InputTag('pfRecoTauDiscriminationByLeadingTrackFinding'),
00006       cut = cms.double(0.5)
00007 )
00008 
00009 # Require no prediscriminants
00010 noPrediscriminants = cms.PSet(
00011       BooleanOperator = cms.string("and"),
00012       )
00013 
00014 # Require the existence of a lead track
00015 requireLeadTrack = cms.PSet(
00016       BooleanOperator = cms.string("and"),
00017       leadTrack = leadTrackFinding,
00018       )
00019 
00020 # Require a existence of a lead track in a CaloTau.
00021 requireLeadTrackCalo = cms.PSet(
00022       BooleanOperator = cms.string("and"),
00023       leadTrack = cms.PSet(
00024          Producer = cms.InputTag('caloRecoTauDiscriminationByLeadingTrackFinding'),
00025          cut = cms.double(0.5)
00026          )
00027       )
00028 
00029 # This is equivalent to the lead track case, and shoudl be deprecated.  
00030 #  Preserved for backwards compatibility
00031 requireLeadPion = cms.PSet(
00032       BooleanOperator = cms.string("and"),
00033       leadPion = leadTrackFinding,
00034       )
00035 
00036 def subParameterSets(pSet):
00037    ''' Generator to return all sub-PSets in a PSet '''
00038    for name, value in pSet.parameters_().iteritems():
00039       if isinstance(value, cms.PSet):
00040          yield getattr(pSet, name)
00041 
00042 # For RECO type taus, where the tau producer is [tauType]Producer 
00043 import re
00044 recoTauTypeMapperRegex = re.compile("(\w*)Producer")
00045 def recoTauTypeMapper(tauProducer):
00046    return recoTauTypeMapperRegex.match(tauProducer).group(1)
00047 
00048 # For taus where the producer name is the type, like "allLayer1Taus", etc
00049 producerIsTauTypeMapper = lambda tauProducer: tauProducer
00050 
00051 def adaptTauDiscriminator(discriminator, newTauProducer='shrinkingConePFTauProducer',
00052       oldTauTypeMapper=recoTauTypeMapper, newTauTypeMapper=recoTauTypeMapper,
00053       preservePFTauProducer = False):
00054    ''' Change a TauDiscriminator to use a different tau/prediscriminant sources
00055 
00056    Tau discriminators use the following convention: 
00057         [tauType]DiscriminationByXXX
00058 
00059    i.e. fixedConePFTauDiscriminationByIsolation,
00060    allLayer1TausDiscriminationByIsolation, etc
00061 
00062    However, the mapping of tauType to tau producer name is not constant.  In
00063    RECO, the form is [tauType]Producer.  In PAT, the producer is just named
00064    [tauType].  To manage this oldTauTypeMapper and newTauTypeMapper are
00065    functions with signature f(str) that translate a TauProducer name (like
00066    shrinkingConePFTauProducer) to its type (shrinkingConePFTau).  Two types of
00067    mapping are provided, 
00068         * recoTauTypeMapper
00069               shrinkingConePFTauProducer->shrinkingConePFTau
00070         * producerIsTauTypeMapper
00071               allLayer1Taus->allLayer1Taus
00072 
00073    '''
00074 
00075    oldTauProducer = discriminator.PFTauProducer
00076    if isinstance(newTauProducer, str):
00077       newTauProducer = cms.InputTag(newTauProducer)
00078 
00079    # This is useful for the PF2PAT case where you wish to set the producer name
00080    # seperately
00081    if not preservePFTauProducer: 
00082       discriminator.PFTauProducer = newTauProducer
00083 
00084    oldTauType = oldTauTypeMapper(oldTauProducer.value())
00085    newTauType = newTauTypeMapper(newTauProducer.value())
00086 
00087    replacementRegex = re.compile(oldTauType)
00088 
00089    # Adapt all the prediscriminants
00090    for prediscriminant in subParameterSets(discriminator.Prediscriminants):
00091       oldProducer = prediscriminant.Producer.value() 
00092       # Replace the old tau type by the new tau type in the prediscrimant
00093       # producer
00094       prediscriminant.Producer = cms.InputTag(replacementRegex.sub(newTauType,
00095          oldProducer))
00096 
00097 def adaptTauDiscriminatorSequence(sequence, **kwargs):
00098    def fixer(discriminator):
00099       if hasattr(discriminator, "Prediscriminants"):
00100          adaptTauDiscriminator(discriminator, **kwargs)
00101    sequence.visit(fixer)
00102 
00103 def setTauSource(discriminator, newTauProducer):
00104    ''' Same as adaptTauDiscriminator, kept for backwards compatibility'''
00105    adaptTauDiscriminator(discriminator, newTauProducer)
00106