CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/PhysicsTools/PatAlgos/python/tools/trigTools.py

Go to the documentation of this file.
00001 from FWCore.GuiBrowsers.ConfigToolBase import *
00002 
00003 from PhysicsTools.PatAlgos.tools.helpers import *
00004 from PhysicsTools.PatAlgos.patEventContent_cff import patTriggerL1RefsEventContent
00005 
00006 _defaultTriggerMatchers      = [ 'cleanMuonTriggerMatchHLTMu20'
00007                                , 'cleanMuonTriggerMatchHLTDoubleMu6'
00008                                , 'cleanPhotonTriggerMatchHLTPhoton26IsoVLPhoton18'
00009                                , 'cleanElectronTriggerMatchHLTEle27CaloIdVTCaloIsoTTrkIdTTrkIsoT'
00010                                , 'cleanTauTriggerMatchHLTDoubleIsoPFTau20Trk5'
00011                                , 'cleanJetTriggerMatchHLTJet240'
00012                                , 'metTriggerMatchHLTMET100'
00013                                , 'cleanMuonTriggerMatchHLTMu17CentralJet30'
00014                                , 'cleanJetTriggerMatchHLTMu17CentralJet30'
00015                                ]
00016 _defaultTriggerProducer      = 'patTrigger'
00017 _defaultTriggerEventProducer = 'patTriggerEvent'
00018 _defaultSequence             = 'patDefaultSequence'
00019 _defaultHltProcess           = 'HLT'
00020 _defaultOutputModule         = 'out'
00021 _defaultPostfix              = ''
00022 
00023 _defaultTriggerMatchersComment      = "Trigger matcher modules' labels, default: ..."
00024 _defaultTriggerProducerComment      = "PATTriggerProducer module label, default: %s"%( _defaultTriggerProducer )
00025 _defaultTriggerEventProducerComment = "PATTriggerEventProducer module label, default: %s"%( _defaultTriggerEventProducer )
00026 _defaultSequenceComment             = "Name of sequence to use, default: %s"%( _defaultSequence )
00027 _defaultHltProcessComment           = "HLT process name, default: %s"%( _defaultHltProcess )
00028 _defaultOutputModuleComment         = "Output module label, empty label indicates no output, default: %s"%( _defaultOutputModule )
00029 _defaultPostfixComment              = "Postfix to apply to PAT module labels, default: %s"%( _defaultPostfix )
00030 
00031 _longLine = '---------------------------------------------------------------------'
00032 
00033 
00034 def _modulesInSequence( process, sequenceLabel ):
00035     return [ m.label() for m in listModules( getattr( process, sequenceLabel ) ) ]
00036 
00037 
00038 def _addEventContent( outputCommands, eventContent ):
00039     # add new entry to event content
00040     for content in eventContent:
00041         if content not in outputCommands:
00042             outputCommands += [ content ]
00043     # check for obsolete entries
00044     listToRemove = []
00045     for i in range( len( outputCommands ) ):
00046         if i in listToRemove:
00047             continue
00048         command = outputCommands[ i ]
00049         if command[ : 4 ] == 'keep':
00050             dropCommand = command.replace( 'keep ', 'drop ' )
00051             for j in range( i + 1, len( outputCommands ) ):
00052                 testCommand = outputCommands[ j ]
00053                 if testCommand == command:
00054                     listToRemove += [ j ]
00055                 elif testCommand == dropCommand:
00056                     listToRemove += [ i, j ]
00057                     break
00058     # copy entries excl. obsolete ones
00059     newOutputCommands = cms.untracked.vstring()
00060     for i in range( len( outputCommands ) ):
00061         if i not in listToRemove:
00062             newOutputCommands += [ outputCommands[ i ] ]
00063     # return result
00064     return newOutputCommands
00065 
00066 
00067 class SwitchOnTrigger( ConfigToolBase ):
00068     """  Enables trigger information in PAT
00069     SwitchOnTrigger( [cms.Process], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out' )
00070     - [cms.Process]       : the 'cms.Process'
00071     - triggerProducer     : PATTriggerProducer module label;
00072                             optional, default: 'patTrigger'
00073     - triggerEventProducer: PATTriggerEventProducer module label;
00074                             optional, default: 'patTriggerEvent'
00075     - sequence            : name of sequence to use;
00076                             optional, default: 'patDefaultSequence'
00077     - hltProcess          : HLT process name;
00078                             optional, default: 'HLT'
00079     - outputModule        : output module label;
00080                             empty label indicates no output;
00081                             optional, default: 'out'
00082     Using None as any argument restores its default value.
00083     """
00084     _label             = 'switchOnTrigger'
00085     _defaultParameters = dicttypes.SortedKeysDict()
00086 
00087     def __init__( self ):
00088         ConfigToolBase.__init__( self )
00089         self.addParameter( self._defaultParameters, 'triggerProducer'     , _defaultTriggerProducer     , _defaultTriggerProducerComment )
00090         self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
00091         self.addParameter( self._defaultParameters, 'sequence'            , _defaultSequence            , _defaultSequenceComment )
00092         self.addParameter( self._defaultParameters, 'hltProcess'          , _defaultHltProcess          , _defaultHltProcessComment )
00093         self.addParameter( self._defaultParameters, 'outputModule'        , _defaultOutputModule        , _defaultOutputModuleComment )
00094         self._parameters = copy.deepcopy( self._defaultParameters )
00095         self._comment = ""
00096 
00097     def getDefaultParameters( self ):
00098         return self._defaultParameters
00099 
00100     def __call__( self, process
00101                 , triggerProducer      = None
00102                 , triggerEventProducer = None
00103                 , sequence             = None
00104                 , hltProcess           = None
00105                 , outputModule         = None
00106                 ):
00107         if triggerProducer is None:
00108             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00109         if triggerEventProducer is None:
00110             triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
00111         if sequence is None:
00112             sequence = self._defaultParameters[ 'sequence' ].value
00113         if hltProcess is None:
00114             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00115         if outputModule is None:
00116             outputModule = self._defaultParameters[ 'outputModule' ].value
00117         self.setParameter( 'triggerProducer'     , triggerProducer )
00118         self.setParameter( 'triggerEventProducer', triggerEventProducer )
00119         self.setParameter( 'sequence'            , sequence )
00120         self.setParameter( 'hltProcess'          , hltProcess )
00121         self.setParameter( 'outputModule'        , outputModule )
00122         self.apply( process )
00123 
00124     def toolCode( self, process ):
00125         triggerProducer      = self._parameters[ 'triggerProducer' ].value
00126         triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
00127         sequence             = self._parameters[ 'sequence' ].value
00128         hltProcess           = self._parameters[ 'hltProcess' ].value
00129         outputModule         = self._parameters[ 'outputModule' ].value
00130 
00131         # Load default producers from existing config files, if needed
00132         if not hasattr( process, triggerProducer ):
00133             if triggerProducer is self.getDefaultParameters()[ 'triggerProducer' ].value:
00134                 process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi" )
00135         if not hasattr( process, triggerEventProducer ):
00136             if triggerEventProducer is self.getDefaultParameters()[ 'triggerEventProducer' ].value:
00137                 process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerEventProducer_cfi" )
00138 
00139         # Maintain configurations
00140         prodSequence            = getattr( process, sequence )
00141         trigProdMod             = getattr( process, triggerProducer )
00142         trigProdMod.processName = hltProcess
00143         if triggerProducer in _modulesInSequence( process, sequence ):
00144             print '%s():'%( self._label )
00145             print '    PATTriggerProducer module %s exists already in sequence %s'%( triggerProducer, sequence )
00146             print '    ==> entry re-used'
00147             if trigProdMod.onlyStandAlone.value() is True:
00148                 trigProdMod.onlyStandAlone = False
00149                 print '    configuration parameter automatically changed'
00150                 print '    PATTriggerProducer %s.onlyStandAlone --> %s'%( triggerProducer, trigProdMod.onlyStandAlone )
00151             print _longLine
00152         else:
00153             # Sequence arithmetics for PATTriggerProducer module
00154             if hasattr( process, sequence + 'Trigger' ):
00155                 index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00156                 getattr( process, sequence + 'Trigger' ).insert( index, trigProdMod )
00157             else:
00158                 patTriggerSequence = cms.Sequence( trigProdMod )
00159                 setattr( process, sequence + 'Trigger', patTriggerSequence )
00160                 prodSequence *= getattr( process, sequence + 'Trigger' )
00161         trigEvtProdMod             = getattr( process, triggerEventProducer )
00162         trigEvtProdMod.processName = hltProcess
00163         if hasattr( trigEvtProdMod, 'patTriggerProducer' ):
00164             trigEvtProdMod.patTriggerProducer = triggerProducer
00165         else:
00166             trigEvtProdMod.patTriggerProducer = cms.InputTag( triggerProducer )
00167         if triggerEventProducer in _modulesInSequence( process, sequence ):
00168             print '%s():'%( self._label )
00169             print '    PATTriggerEventProducer module %s exists already in sequence %s'%( triggerEventProducer, sequence )
00170             print '    ==> entry re-used'
00171             print _longLine
00172         else:
00173             # Sequence arithmetics for PATTriggerEventProducer module
00174             if hasattr( process, sequence + 'TriggerEvent' ):
00175                 index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00176                 getattr( process, sequence + 'TriggerEvent' ).insert( index, trigEvtProdMod )
00177             else:
00178                 patTriggerEventSequence = cms.Sequence( trigEvtProdMod )
00179                 setattr( process, sequence + 'TriggerEvent', patTriggerEventSequence )
00180                 prodSequence *= getattr( process, sequence + 'TriggerEvent' )
00181 
00182         # Add event content
00183         if outputModule is not '':
00184             patTriggerEventContent = [ 'keep patTriggerObjects_%s_*_%s'%( triggerProducer, process.name_() )
00185                                      , 'keep patTriggerFilters_%s_*_%s'%( triggerProducer, process.name_() )
00186                                      , 'keep patTriggerPaths_%s_*_%s'%( triggerProducer, process.name_() )
00187                                      , 'keep patTriggerEvent_%s_*_%s'%( triggerEventProducer, process.name_() )
00188                                      ]
00189             if hasattr( trigProdMod, 'addL1Algos' ) and trigProdMod.addL1Algos.value() is True:
00190                 patTriggerEventContent += [ 'keep patTriggerConditions_%s_*_%s'%( triggerProducer, process.name_() )
00191                                           , 'keep patTriggerAlgorithms_%s_*_%s'%( triggerProducer, process.name_() )
00192                                           ]
00193             if hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True:
00194                 patTriggerEventContent += patTriggerL1RefsEventContent
00195             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00196 
00197 switchOnTrigger = SwitchOnTrigger()
00198 
00199 
00200 class SwitchOnTriggerStandAlone( ConfigToolBase ):
00201     """  Enables trigger information in PAT, limited to stand-alone trigger objects
00202     SwitchOnTriggerStandAlone( [cms.Process], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out' )
00203     - [cms.Process]       : the 'cms.Process'
00204     - triggerProducer     : PATTriggerProducer module label;
00205                             optional, default: 'patTrigger'
00206     - sequence            : name of sequence to use;
00207                             optional, default: 'patDefaultSequence'
00208     - hltProcess          : HLT process name;
00209                             optional, default: 'HLT'
00210     - outputModule        : output module label;
00211                             empty label indicates no output;
00212                             optional, default: 'out'
00213     Using None as any argument restores its default value.
00214     """
00215     _label             = 'switchOnTriggerStandAlone'
00216     _defaultParameters = dicttypes.SortedKeysDict()
00217 
00218     def __init__( self ):
00219         ConfigToolBase.__init__( self )
00220         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
00221         self.addParameter( self._defaultParameters, 'sequence'       , _defaultSequence       , _defaultSequenceComment )
00222         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
00223         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
00224         self._parameters = copy.deepcopy( self._defaultParameters )
00225         self._comment = ""
00226 
00227     def getDefaultParameters( self ):
00228         return self._defaultParameters
00229 
00230     def __call__( self, process
00231                 , triggerProducer      = None
00232                 , sequence             = None
00233                 , hltProcess           = None
00234                 , outputModule         = None
00235                 ):
00236         if triggerProducer is None:
00237             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00238         if sequence is None:
00239             sequence = self._defaultParameters[ 'sequence' ].value
00240         if hltProcess is None:
00241             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00242         if outputModule is None:
00243             outputModule = self._defaultParameters[ 'outputModule' ].value
00244         self.setParameter( 'triggerProducer', triggerProducer )
00245         self.setParameter( 'sequence'       , sequence )
00246         self.setParameter( 'hltProcess'     , hltProcess )
00247         self.setParameter( 'outputModule'   , outputModule )
00248         self.apply( process )
00249 
00250     def toolCode( self, process ):
00251         triggerProducer = self._parameters[ 'triggerProducer' ].value
00252         sequence        = self._parameters[ 'sequence' ].value
00253         hltProcess      = self._parameters[ 'hltProcess' ].value
00254         outputModule    = self._parameters[ 'outputModule' ].value
00255 
00256         # Load default producer from existing config file, if needed
00257         if not hasattr( process, triggerProducer ):
00258             if triggerProducer is self.getDefaultParameters()[ 'triggerProducer' ].value:
00259                 process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi" )
00260 
00261         # Maintain configuration
00262         prodSequence            = getattr( process, sequence )
00263         trigProdMod             = getattr( process, triggerProducer )
00264         trigProdMod.processName = hltProcess
00265         if triggerProducer in _modulesInSequence( process, sequence ):
00266             print '%s():'%( self._label )
00267             print '    PATTriggerProducer module %s exists already in sequence %s'%( triggerProducer, sequence )
00268             print '    ==> entry re-used'
00269             print _longLine
00270         else:
00271             # Sequence arithmetics for PATTriggerProducer module
00272             if trigProdMod.onlyStandAlone.value() is False:
00273                 trigProdMod.onlyStandAlone = True
00274                 print '%s():'%( self._label )
00275                 print '    configuration parameter automatically changed'
00276                 print '    PATTriggerProducer %s.onlyStandAlone --> %s'%( triggerProducer, trigProdMod.onlyStandAlone )
00277                 print _longLine
00278             if hasattr( process, sequence + 'Trigger' ):
00279                 index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00280                 getattr( process, sequence + 'Trigger' ).insert( index, trigProdMod )
00281             else:
00282                 patTriggerSequence = cms.Sequence( trigProdMod )
00283                 setattr( process, sequence + 'Trigger', patTriggerSequence )
00284                 prodSequence *= getattr( process, sequence + 'Trigger' )
00285 
00286         # Add event content
00287         if outputModule is not '':
00288             patTriggerEventContent = [ 'keep patTriggerObjectStandAlones_%s_*_%s'%( triggerProducer, process.name_() )
00289                                      ]
00290             if hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True:
00291                 patTriggerEventContent += patTriggerL1RefsEventContent
00292             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00293 
00294 switchOnTriggerStandAlone = SwitchOnTriggerStandAlone()
00295 
00296 
00297 class SwitchOnTriggerMatching( ConfigToolBase ):
00298     """  Enables trigger matching in PAT
00299     SwitchOnTriggerMatching( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
00300     - [cms.Process]       : the 'cms.Process'
00301     - triggerMatchers     : PAT trigger matcher module labels (list)
00302                             optional; default: defined in 'triggerMatchingDefaultSequence'
00303                             (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
00304     - triggerProducer     : PATTriggerProducer module label;
00305                             optional, default: 'patTrigger'
00306     - triggerEventProducer: PATTriggerEventProducer module label;
00307                             optional, default: 'patTriggerEvent'
00308     - sequence            : name of sequence to use;
00309                             optional, default: 'patDefaultSequence'
00310     - hltProcess          : HLT process name;
00311                             optional, default: 'HLT'
00312     - outputModule        : output module label;
00313                             empty label indicates no output;
00314                             optional, default: 'out'
00315     - postfix             : postfix to apply to PAT module labels;
00316                             optional, default: ''
00317     Using None as any argument restores its default value.
00318     """
00319     _label             = 'switchOnTriggerMatching'
00320     _defaultParameters = dicttypes.SortedKeysDict()
00321 
00322     def __init__( self ):
00323         ConfigToolBase.__init__( self )
00324         self.addParameter( self._defaultParameters, 'triggerMatchers'     , _defaultTriggerMatchers     , _defaultTriggerMatchersComment )
00325         self.addParameter( self._defaultParameters, 'triggerProducer'     , _defaultTriggerProducer     , _defaultTriggerProducerComment )
00326         self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
00327         self.addParameter( self._defaultParameters, 'sequence'            , _defaultSequence            , _defaultSequenceComment )
00328         self.addParameter( self._defaultParameters, 'hltProcess'          , _defaultHltProcess          , _defaultHltProcessComment )
00329         self.addParameter( self._defaultParameters, 'outputModule'        , _defaultOutputModule        , _defaultOutputModuleComment )
00330         self.addParameter( self._defaultParameters, 'postfix'             , _defaultPostfix             , _defaultPostfixComment )
00331         self._parameters = copy.deepcopy( self._defaultParameters )
00332         self._comment = ""
00333 
00334     def getDefaultParameters( self ):
00335         return self._defaultParameters
00336 
00337     def __call__( self, process
00338                 , triggerMatchers      = None
00339                 , triggerProducer      = None
00340                 , triggerEventProducer = None
00341                 , sequence             = None
00342                 , hltProcess           = None
00343                 , outputModule         = None
00344                 , postfix              = None
00345                 ):
00346         if triggerMatchers is None:
00347             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
00348         if triggerProducer is None:
00349             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00350         if triggerEventProducer is None:
00351             triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
00352         if sequence is None:
00353             sequence = self._defaultParameters[ 'sequence' ].value
00354         if hltProcess is None:
00355             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00356         if outputModule is None:
00357             outputModule = self._defaultParameters[ 'outputModule' ].value
00358         if postfix is None:
00359             postfix = self._defaultParameters[ 'postfix' ].value
00360         self.setParameter( 'triggerMatchers'     , triggerMatchers )
00361         self.setParameter( 'triggerProducer'     , triggerProducer )
00362         self.setParameter( 'triggerEventProducer', triggerEventProducer )
00363         self.setParameter( 'sequence'            , sequence )
00364         self.setParameter( 'hltProcess'          , hltProcess )
00365         self.setParameter( 'outputModule'        , outputModule )
00366         self.setParameter( 'postfix'             , postfix )
00367         self.apply( process )
00368 
00369     def toolCode( self, process ):
00370         triggerMatchers      = self._parameters[ 'triggerMatchers' ].value
00371         triggerProducer      = self._parameters[ 'triggerProducer' ].value
00372         triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
00373         sequence             = self._parameters[ 'sequence' ].value
00374         hltProcess           = self._parameters[ 'hltProcess' ].value
00375         outputModule         = self._parameters[ 'outputModule' ].value
00376         postfix              = self._parameters[ 'postfix' ].value
00377 
00378         # Load default producers from existing config file, if needed
00379         if not hasattr( process, 'triggerMatchingDefaultSequence' ):
00380             for matcher in triggerMatchers:
00381                 if matcher in self.getDefaultParameters()[ 'triggerMatchers' ].value:
00382                     process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcher_cfi" )
00383                     break
00384 
00385         # Switch on PAT trigger information if needed
00386         if ( triggerProducer not in _modulesInSequence( process, sequence ) or triggerEventProducer not in _modulesInSequence( process, sequence ) ):
00387             print '%s():'%( self._label )
00388             print '    PAT trigger production switched on automatically using'
00389             print '    switchOnTrigger( process, %s, %s, %s, %s, %s )'%( hltProcess, triggerProducer, triggerEventProducer, sequence, outputModule )
00390             print _longLine
00391             switchOnTrigger( process, triggerProducer, triggerEventProducer, sequence, hltProcess, outputModule )
00392 
00393         # Maintain configurations
00394         prodSequence   = getattr( process, sequence )
00395         trigEvtProdMod = getattr( process, triggerEventProducer )
00396         if trigEvtProdMod.patTriggerProducer.value() is not triggerProducer:
00397             print '%s():'%( self._label )
00398             print '    Configuration conflict found'
00399             print '    triggerProducer = %s'%( triggerProducer )
00400             print '    differs from'
00401             print '    %s.patTriggerProducer = %s'%( triggerEventProducer, trigEvtProdMod.patTriggerProducer )
00402             print '    parameter automatically changed'
00403             print '    ==> triggerProducer --> %s'%( trigEvtProdMod.patTriggerProducer )
00404             triggerProducer = trigEvtProdMod.patTriggerProducer
00405         for matcher in triggerMatchers:
00406             trigMchMod         = getattr( process, matcher )
00407             trigMchMod.src     = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
00408             trigMchMod.matched = triggerProducer
00409             if matcher in _modulesInSequence( process, sequence ):
00410                 print '%s():'%( self._label )
00411                 print '    PAT trigger matcher %s exists already in sequence %s'%( matcher, sequence )
00412                 print '    ==> entry re-used'
00413                 print _longLine
00414             else:
00415                 # Sequence arithmetics for PAT trigger matcher modules
00416                 index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00417                 getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
00418         matchers = getattr( trigEvtProdMod, 'patTriggerMatches' )
00419         if len( matchers ) > 0:
00420             print '%s():'%( self._label )
00421             print '    PAT trigger matchers already attached to existing PATTriggerEventProducer %s'%( triggerEventProducer )
00422             print '    configuration parameters automatically changed'
00423             for matcher in matchers:
00424                 trigMchMod = getattr( process, matcher )
00425                 if trigMchMod.matched.value() is not triggerProducer:
00426                     removeIfInSequence( process, matcher, sequence + 'Trigger' )
00427                     trigMchMod.matched = triggerProducer
00428                     index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00429                     getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
00430                     print '    PAT trigger matcher %s.matched --> %s'%( matcher, trigMchMod.matched )
00431             print _longLine
00432         else:
00433             trigEvtProdMod.patTriggerMatches = cms.VInputTag()
00434         for matcher in triggerMatchers:
00435             trigEvtProdMod.patTriggerMatches.append( cms.InputTag( matcher ) )
00436 
00437         # Add event content
00438         if outputModule is not '':
00439             patTriggerEventContent = []
00440             for matcher in triggerMatchers:
00441                 patTriggerEventContent += [ 'keep patTriggerObjectsedmAssociation_%s_%s_%s'%( triggerEventProducer, matcher, process.name_() )
00442                                           , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
00443                                           ]
00444             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00445 
00446 switchOnTriggerMatching = SwitchOnTriggerMatching()
00447 
00448 
00449 class SwitchOnTriggerMatchingStandAlone( ConfigToolBase ):
00450     """  Enables trigger matching in PAT
00451     SwitchOnTriggerMatchingStandAlone( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
00452     - [cms.Process]  : the 'cms.Process'
00453     - triggerMatchers: PAT trigger matcher module labels (list)
00454                        optional; default: defined in 'triggerMatchingDefaultSequence'
00455                        (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
00456     - triggerProducer: PATTriggerProducer module label;
00457                        optional, default: 'patTrigger'
00458     - sequence       : name of sequence to use;
00459                        optional, default: 'patDefaultSequence'
00460     - hltProcess     : HLT process name;
00461                        optional, default: 'HLT'
00462     - outputModule   : output module label;
00463                        empty label indicates no output;
00464                        optional, default: 'out'
00465     - postfix        : postfix to apply to PAT module labels;
00466                        optional, default: ''
00467     Using None as any argument restores its default value.
00468     """
00469     _label             = 'switchOnTriggerMatchingStandAlone'
00470     _defaultParameters = dicttypes.SortedKeysDict()
00471 
00472     def __init__( self ):
00473         ConfigToolBase.__init__( self )
00474         self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
00475         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
00476         self.addParameter( self._defaultParameters, 'sequence'       , _defaultSequence       , _defaultSequenceComment )
00477         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
00478         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
00479         self.addParameter( self._defaultParameters, 'postfix'        , _defaultPostfix        , _defaultPostfixComment )
00480         self._parameters = copy.deepcopy( self._defaultParameters )
00481         self._comment = ""
00482 
00483     def getDefaultParameters( self ):
00484         return self._defaultParameters
00485 
00486     def __call__( self, process
00487                 , triggerMatchers = None
00488                 , triggerProducer = None
00489                 , sequence        = None
00490                 , hltProcess      = None
00491                 , outputModule    = None
00492                 , postfix         = None
00493                 ):
00494         if triggerMatchers is None:
00495             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
00496         if triggerProducer is None:
00497             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00498         if sequence is None:
00499             sequence = self._defaultParameters[ 'sequence' ].value
00500         if hltProcess is None:
00501             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00502         if outputModule is None:
00503             outputModule = self._defaultParameters[ 'outputModule' ].value
00504         if postfix is None:
00505             postfix = self._defaultParameters[ 'postfix' ].value
00506         self.setParameter( 'triggerMatchers', triggerMatchers )
00507         self.setParameter( 'triggerProducer', triggerProducer )
00508         self.setParameter( 'sequence'       , sequence )
00509         self.setParameter( 'hltProcess'     , hltProcess )
00510         self.setParameter( 'outputModule'   , outputModule )
00511         self.setParameter( 'postfix'        , postfix )
00512         self.apply( process )
00513 
00514     def toolCode( self, process ):
00515         triggerMatchers = self._parameters[ 'triggerMatchers' ].value
00516         triggerProducer = self._parameters[ 'triggerProducer' ].value
00517         sequence        = self._parameters[ 'sequence' ].value
00518         hltProcess      = self._parameters[ 'hltProcess' ].value
00519         outputModule    = self._parameters[ 'outputModule' ].value
00520         postfix         = self._parameters[ 'postfix' ].value
00521 
00522         # Load default producers from existing config file, if needed
00523         if not hasattr( process, 'triggerMatchingDefaultSequence' ):
00524             for matcher in triggerMatchers:
00525                 if matcher in self.getDefaultParameters()[ 'triggerMatchers' ].value:
00526                     process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcher_cfi" )
00527                     break
00528 
00529         # Switch on PAT trigger information if needed
00530         if triggerProducer not in _modulesInSequence( process, sequence ):
00531             print '%s():'%( self._label )
00532             print '    PAT trigger production switched on automatically using'
00533             print '    switchOnTriggerStandAlone( process, %s, %s, %s, %s )'%( hltProcess, triggerProducer, sequence, outputModule )
00534             print _longLine
00535             switchOnTriggerStandAlone( process, triggerProducer, sequence, hltProcess, outputModule )
00536 
00537         # Maintain configurations
00538         for matcher in triggerMatchers:
00539             trigMchMod         = getattr( process, matcher )
00540             trigMchMod.src     = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
00541             trigMchMod.matched = triggerProducer
00542             if matcher in _modulesInSequence( process, sequence ):
00543                 print '%s():'%( self._label )
00544                 print '    PAT trigger matcher %s exists already in sequence %s'%( matcher, sequence )
00545                 print '    ==> entry re-used'
00546                 print _longLine
00547             else:
00548                 # Sequence arithmetics for PAT trigger matcher modules
00549                 index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00550                 getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
00551 
00552         # Add event content
00553         if outputModule is not '':
00554             patTriggerEventContent = []
00555             for matcher in triggerMatchers:
00556                 patTriggerEventContent += [ 'keep patTriggerObjectStandAlonesedmAssociation_%s_*_%s'%( matcher, process.name_() )
00557                                           , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
00558                                           ]
00559             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00560 
00561 switchOnTriggerMatchingStandAlone = SwitchOnTriggerMatchingStandAlone()
00562 
00563 
00564 class SwitchOnTriggerMatchEmbedding( ConfigToolBase ):
00565     """  Enables embedding of trigger matches into PAT objects
00566     SwitchOnTriggerMatchEmbedding( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
00567     - [cms.Process]  : the 'cms.Process'
00568     - triggerMatchers: PAT trigger matcher module labels (list)
00569                        optional; default: defined in 'triggerMatchingDefaultSequence'
00570                        (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
00571     - triggerProducer: PATTriggerProducer module label;
00572                        optional, default: 'patTrigger'
00573     - sequence       : name of sequence to use;
00574                        optional, default: 'patDefaultSequence'
00575     - hltProcess     : HLT process name;
00576                        optional, default: 'HLT'
00577     - outputModule   : output module label;
00578                        empty label indicates no output;
00579                        optional, default: 'out'
00580     - postfix        : postfix to apply to PAT module labels;
00581                        optional, default: ''
00582     Using None as any argument restores its default value.
00583     """
00584     _label             = 'switchOnTriggerMatchEmbedding'
00585     _defaultParameters = dicttypes.SortedKeysDict()
00586 
00587     def __init__( self ):
00588         ConfigToolBase.__init__( self )
00589         self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
00590         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
00591         self.addParameter( self._defaultParameters, 'sequence'       , _defaultSequence       , _defaultSequenceComment )
00592         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
00593         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
00594         self.addParameter( self._defaultParameters, 'postfix'        , _defaultPostfix        , _defaultPostfixComment )
00595         self._parameters = copy.deepcopy( self._defaultParameters )
00596         self._comment = ""
00597 
00598     def getDefaultParameters( self ):
00599         return self._defaultParameters
00600 
00601     def __call__( self, process
00602                 , triggerMatchers = None
00603                 , triggerProducer = None
00604                 , sequence        = None
00605                 , hltProcess      = None
00606                 , outputModule    = None
00607                 , postfix         = None
00608                 ):
00609         if triggerMatchers is None:
00610             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
00611         if triggerProducer is None:
00612             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00613         if sequence is None:
00614             sequence = self._defaultParameters[ 'sequence' ].value
00615         if hltProcess is None:
00616             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00617         if outputModule is None:
00618             outputModule = self._defaultParameters[ 'outputModule' ].value
00619         if postfix is None:
00620             postfix = self._defaultParameters[ 'postfix' ].value
00621         self.setParameter( 'triggerMatchers', triggerMatchers )
00622         self.setParameter( 'triggerProducer', triggerProducer )
00623         self.setParameter( 'sequence'       , sequence )
00624         self.setParameter( 'hltProcess'     , hltProcess )
00625         self.setParameter( 'outputModule'   , outputModule )
00626         self.setParameter( 'postfix'        , postfix )
00627         self.apply( process )
00628 
00629     def toolCode( self, process ):
00630         triggerMatchers = self._parameters[ 'triggerMatchers' ].value
00631         triggerProducer = self._parameters[ 'triggerProducer' ].value
00632         sequence        = self._parameters[ 'sequence' ].value
00633         hltProcess      = self._parameters[ 'hltProcess' ].value
00634         outputModule    = self._parameters[ 'outputModule' ].value
00635         postfix         = self._parameters[ 'postfix' ].value
00636 
00637         # Build dictionary of known input collections
00638         dictPatObjects = { 'Photons'  : 'PATTriggerMatchPhotonEmbedder'
00639                          , 'Electrons': 'PATTriggerMatchElectronEmbedder'
00640                          , 'Muons'    : 'PATTriggerMatchMuonEmbedder'
00641                          , 'Taus'     : 'PATTriggerMatchTauEmbedder'
00642                          , 'Jets'     : 'PATTriggerMatchJetEmbedder'
00643                          , 'METs'     : 'PATTriggerMatchMETEmbedder'
00644                          }
00645         listPatSteps   = [ 'pat', 'selectedPat', 'cleanPat' ]
00646         listJetAlgos   = [ 'IC5', 'SC5', 'KT4', 'KT6', 'AK5' ]
00647         listJetTypes   = [ 'Calo', 'PF', 'JPT' ]
00648         dictEmbedders  = {}
00649         for objects in dictPatObjects.keys():
00650             steps = len( listPatSteps )
00651             if objects is 'METs':
00652                 steps = 1
00653             for step in range( steps ):
00654                 coll = listPatSteps[ step ] + objects
00655                 dictEmbedders[ coll ]           = dictPatObjects[ objects ]
00656                 dictEmbedders[ coll + postfix ] = dictPatObjects[ objects ]
00657                 if objects is 'Jets':
00658                     for jetAlgo in listJetAlgos:
00659                         for jetType in listJetTypes:
00660                             jetColl = coll + jetAlgo + jetType
00661                             dictEmbedders[ jetColl ]           = dictPatObjects[ objects ]
00662                             dictEmbedders[ jetColl + postfix ] = dictPatObjects[ objects ]
00663 
00664         # Build dictionary of matchers and switch on PAT trigger matching if needed
00665         dictConfig = {}
00666         matchingOn = False
00667         for matcher in triggerMatchers:
00668             trigMchMod = getattr( process, matcher )
00669             if trigMchMod.src.value() in dictConfig:
00670                 dictConfig[ trigMchMod.src.value() ] += [ matcher ]
00671             else:
00672                 dictConfig[ trigMchMod.src.value() ] = [ matcher ]
00673             if matcher not in _modulesInSequence( process, sequence ) and not matchingOn:
00674                 print '%s():'%( self._label )
00675                 print '    PAT trigger matching switched on automatically using'
00676                 print '    switchOnTriggerMatchingStandAlone( process, %s, %s, %s, %s, %s )'%( hltProcess, triggerMatchers, triggerProducer, sequence, outputModule )
00677                 print _longLine
00678                 switchOnTriggerMatchingStandAlone( process, triggerMatchers, triggerProducer, sequence, hltProcess, '', postfix )
00679                 matchingOn = True
00680 
00681         # Maintain configurations
00682         patTriggerEventContent = []
00683         for srcInput in dictConfig.keys():
00684             if dictEmbedders.has_key( srcInput ):
00685                 # Configure embedder module
00686                 dictIndex = srcInput
00687                 srcInput += postfix
00688                 if dictEmbedders.has_key( srcInput ):
00689                     label = srcInput + 'TriggerMatch'
00690                     if label in _modulesInSequence( process, sequence ):
00691                         print '%s():'%( self._label )
00692                         print '    PAT trigger match embedder %s exists already in sequence %s'%( label, sequence )
00693                         print '    ==> entry moved to proper place'
00694                         print _longLine
00695                         removeIfInSequence( process, label, sequence + 'Trigger' )
00696                     module         = cms.EDProducer( dictEmbedders[ dictIndex ] )
00697                     module.src     = cms.InputTag( srcInput )
00698                     module.matches = cms.VInputTag( dictConfig[ dictIndex ] )
00699                     setattr( process, label, module )
00700                     trigEmbMod = getattr( process, label )
00701                     index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00702                     getattr( process, sequence + 'Trigger' ).insert( index, trigEmbMod )
00703                     # Add event content
00704                     patTriggerEventContent += [ 'drop *_%s_*_*'%( srcInput )
00705                                               , 'keep *_%s_*_%s'%( label, process.name_() )
00706                                               ]
00707                 else:
00708                     print '%s():'%( self._label )
00709                     print '    Invalid new input source for trigger match embedding'
00710                     print '    ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ dictIndex ] )
00711                     print _longLine
00712             else:
00713                 print '%s():'%( self._label )
00714                 print '    Invalid input source for trigger match embedding'
00715                 print '    ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ srcInput ] )
00716                 print _longLine
00717         if outputModule is not '':
00718             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00719 
00720 switchOnTriggerMatchEmbedding = SwitchOnTriggerMatchEmbedding()
00721 
00722 
00723 class RemoveCleaningFromTriggerMatching( ConfigToolBase ):
00724     """  Removes cleaning from already existing PAT trigger matching/embedding configuration
00725     RemoveCleaningFromTriggerMatching( [cms.Process], outputModule = 'out' )
00726     - [cms.Process]  : the 'cms.Process'
00727     - sequence       : name of sequence to use;
00728                        optional, default: 'patDefaultSequence'
00729     - outputModule   : output module label;
00730                        empty label indicates no output;
00731                        optional, default: 'out'
00732     Using None as any argument restores its default value.
00733     """
00734     _label             = 'removeCleaningFromTriggerMatching'
00735     _defaultParameters = dicttypes.SortedKeysDict()
00736 
00737     def __init__( self ):
00738         ConfigToolBase.__init__( self )
00739         self.addParameter( self._defaultParameters, 'sequence'    , _defaultSequence    , _defaultSequenceComment )
00740         self.addParameter( self._defaultParameters, 'outputModule', _defaultOutputModule, _defaultOutputModuleComment )
00741         self._parameters = copy.deepcopy( self._defaultParameters )
00742         self._comment = ""
00743 
00744     def getDefaultParameters( self ):
00745         return self._defaultParameters
00746 
00747     def __call__( self, process
00748                 , sequence     = None
00749                 , outputModule = None
00750                 ):
00751         if sequence is None:
00752             sequence = self._defaultParameters[ 'sequence' ].value
00753         if outputModule is None:
00754             outputModule = self._defaultParameters[ 'outputModule' ].value
00755         self.setParameter( 'sequence'    , sequence )
00756         self.setParameter( 'outputModule', outputModule )
00757         self.apply( process )
00758 
00759     def toolCode( self, process ):
00760         sequence     = self._parameters[ 'sequence' ].value
00761         outputModule = self._parameters[ 'outputModule' ].value
00762 
00763         # Maintain configurations
00764         listMatchers = [ 'PATTriggerMatcherDRLessByR'
00765                        , 'PATTriggerMatcherDRDPtLessByR'
00766                        , 'PATTriggerMatcherDRLessByPt'
00767                        , 'PATTriggerMatcherDRDPtLessByPt'
00768                        , 'PATTriggerMatcherDEtaLessByDR'
00769                        , 'PATTriggerMatcherDEtaLessByDEta'
00770                        ]
00771         listEmbedders = [ 'PATTriggerMatchPhotonEmbedder'
00772                         , 'PATTriggerMatchElectronEmbedder'
00773                         , 'PATTriggerMatchMuonEmbedder'
00774                         , 'PATTriggerMatchTauEmbedder'
00775                         , 'PATTriggerMatchJetEmbedder'
00776                         , 'PATTriggerMatchMETEmbedder'
00777                         ]
00778         modules = _modulesInSequence( process, sequence )
00779         oldModules = []
00780         oldSources = []
00781         # input source labels
00782         for module in modules:
00783             if hasattr( process, module ):
00784                 trigMod = getattr( process, module )
00785                 if trigMod.type_() in listMatchers:
00786                     if trigMod.src.value()[ : 8 ] == 'cleanPat':
00787                         trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
00788                         if trigMod.label()[ : 5 ] == 'clean':
00789                             oldModules += [ trigMod.label() ]
00790                             setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
00791                 if trigMod.type_() in listEmbedders:
00792                     if trigMod.src.value()[ : 8 ] == 'cleanPat':
00793                         oldSources += [ trigMod.src.getModuleLabel() ]
00794                         trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
00795                         if trigMod.label()[ : 5 ] == 'clean':
00796                             oldModules += [ trigMod.label() ]
00797                             setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
00798         # matcher labels
00799         for module in modules:
00800             if hasattr( process, module ):
00801                 trigMod = getattr( process, module )
00802                 if trigMod.type_() == 'PATTriggerEventProducer':
00803                     matchers = getattr( trigMod, 'patTriggerMatches' )
00804                     matchers = self._renameMatchers( matchers, oldModules )
00805                 elif trigMod.type_() in listEmbedders:
00806                     matchers = getattr( trigMod, 'matches' )
00807                     matchers = self._renameMatchers( matchers, oldModules )
00808 
00809         # Maintain event content
00810         if outputModule is not '':
00811             patTriggerEventContent = getattr( process, outputModule ).outputCommands
00812             for statement in range( len( patTriggerEventContent ) ):
00813                 for module in oldModules:
00814                     if module in patTriggerEventContent[ statement ]:
00815                         patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
00816                 for source in oldSources:
00817                     if source in patTriggerEventContent[ statement ] and 'drop' in patTriggerEventContent[ statement ]:
00818                         patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
00819         print '%s():'%( self._label )
00820         print '    Input from cleaning has been switched to input from selection;'
00821         print '    matcher and embedder modules have been renamed accordingly.'
00822         print _longLine
00823 
00824     def _renameMatchers( self, matchers, oldModules ):
00825         for matcher in range( len( matchers ) ):
00826             if matchers[ matcher ] in oldModules:
00827                 if matchers[ matcher ][ : 5 ] == 'clean':
00828                      matchers[ matcher ] = matchers[ matcher ].replace( 'clean', 'selected' )
00829         return matchers
00830 
00831 removeCleaningFromTriggerMatching = RemoveCleaningFromTriggerMatching()