CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/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         trigEvtProdMod.patTriggerMatches += triggerMatchers
00435 
00436         # Add event content
00437         if outputModule is not '':
00438             patTriggerEventContent = []
00439             for matcher in triggerMatchers:
00440                 patTriggerEventContent += [ 'keep patTriggerObjectsedmAssociation_%s_%s_%s'%( triggerEventProducer, matcher, process.name_() )
00441                                           , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
00442                                           ]
00443             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00444 
00445 switchOnTriggerMatching = SwitchOnTriggerMatching()
00446 
00447 
00448 class SwitchOnTriggerMatchingStandAlone( ConfigToolBase ):
00449     """  Enables trigger matching in PAT
00450     SwitchOnTriggerMatchingStandAlone( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
00451     - [cms.Process]  : the 'cms.Process'
00452     - triggerMatchers: PAT trigger matcher module labels (list)
00453                        optional; default: defined in 'triggerMatchingDefaultSequence'
00454                        (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
00455     - triggerProducer: PATTriggerProducer module label;
00456                        optional, default: 'patTrigger'
00457     - sequence       : name of sequence to use;
00458                        optional, default: 'patDefaultSequence'
00459     - hltProcess     : HLT process name;
00460                        optional, default: 'HLT'
00461     - outputModule   : output module label;
00462                        empty label indicates no output;
00463                        optional, default: 'out'
00464     - postfix        : postfix to apply to PAT module labels;
00465                        optional, default: ''
00466     Using None as any argument restores its default value.
00467     """
00468     _label             = 'switchOnTriggerMatchingStandAlone'
00469     _defaultParameters = dicttypes.SortedKeysDict()
00470 
00471     def __init__( self ):
00472         ConfigToolBase.__init__( self )
00473         self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
00474         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
00475         self.addParameter( self._defaultParameters, 'sequence'       , _defaultSequence       , _defaultSequenceComment )
00476         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
00477         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
00478         self.addParameter( self._defaultParameters, 'postfix'        , _defaultPostfix        , _defaultPostfixComment )
00479         self._parameters = copy.deepcopy( self._defaultParameters )
00480         self._comment = ""
00481 
00482     def getDefaultParameters( self ):
00483         return self._defaultParameters
00484 
00485     def __call__( self, process
00486                 , triggerMatchers = None
00487                 , triggerProducer = None
00488                 , sequence        = None
00489                 , hltProcess      = None
00490                 , outputModule    = None
00491                 , postfix         = None
00492                 ):
00493         if triggerMatchers is None:
00494             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
00495         if triggerProducer is None:
00496             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00497         if sequence is None:
00498             sequence = self._defaultParameters[ 'sequence' ].value
00499         if hltProcess is None:
00500             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00501         if outputModule is None:
00502             outputModule = self._defaultParameters[ 'outputModule' ].value
00503         if postfix is None:
00504             postfix = self._defaultParameters[ 'postfix' ].value
00505         self.setParameter( 'triggerMatchers', triggerMatchers )
00506         self.setParameter( 'triggerProducer', triggerProducer )
00507         self.setParameter( 'sequence'       , sequence )
00508         self.setParameter( 'hltProcess'     , hltProcess )
00509         self.setParameter( 'outputModule'   , outputModule )
00510         self.setParameter( 'postfix'        , postfix )
00511         self.apply( process )
00512 
00513     def toolCode( self, process ):
00514         triggerMatchers = self._parameters[ 'triggerMatchers' ].value
00515         triggerProducer = self._parameters[ 'triggerProducer' ].value
00516         sequence        = self._parameters[ 'sequence' ].value
00517         hltProcess      = self._parameters[ 'hltProcess' ].value
00518         outputModule    = self._parameters[ 'outputModule' ].value
00519         postfix         = self._parameters[ 'postfix' ].value
00520 
00521         # Load default producers from existing config file, if needed
00522         if not hasattr( process, 'triggerMatchingDefaultSequence' ):
00523             for matcher in triggerMatchers:
00524                 if matcher in self.getDefaultParameters()[ 'triggerMatchers' ].value:
00525                     process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcher_cfi" )
00526                     break
00527 
00528         # Switch on PAT trigger information if needed
00529         if triggerProducer not in _modulesInSequence( process, sequence ):
00530             print '%s():'%( self._label )
00531             print '    PAT trigger production switched on automatically using'
00532             print '    switchOnTriggerStandAlone( process, %s, %s, %s, %s )'%( hltProcess, triggerProducer, sequence, outputModule )
00533             print _longLine
00534             switchOnTriggerStandAlone( process, triggerProducer, sequence, hltProcess, outputModule )
00535 
00536         # Maintain configurations
00537         for matcher in triggerMatchers:
00538             trigMchMod         = getattr( process, matcher )
00539             trigMchMod.src     = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
00540             trigMchMod.matched = triggerProducer
00541             if matcher in _modulesInSequence( process, sequence ):
00542                 print '%s():'%( self._label )
00543                 print '    PAT trigger matcher %s exists already in sequence %s'%( matcher, sequence )
00544                 print '    ==> entry re-used'
00545                 print _longLine
00546             else:
00547                 # Sequence arithmetics for PAT trigger matcher modules
00548                 index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00549                 getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
00550 
00551         # Add event content
00552         if outputModule is not '':
00553             patTriggerEventContent = []
00554             for matcher in triggerMatchers:
00555                 patTriggerEventContent += [ 'keep patTriggerObjectStandAlonesedmAssociation_%s_*_%s'%( matcher, process.name_() )
00556                                           , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
00557                                           ]
00558             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00559 
00560 switchOnTriggerMatchingStandAlone = SwitchOnTriggerMatchingStandAlone()
00561 
00562 
00563 class SwitchOnTriggerMatchEmbedding( ConfigToolBase ):
00564     """  Enables embedding of trigger matches into PAT objects
00565     SwitchOnTriggerMatchEmbedding( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
00566     - [cms.Process]  : the 'cms.Process'
00567     - triggerMatchers: PAT trigger matcher module labels (list)
00568                        optional; default: defined in 'triggerMatchingDefaultSequence'
00569                        (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
00570     - triggerProducer: PATTriggerProducer module label;
00571                        optional, default: 'patTrigger'
00572     - sequence       : name of sequence to use;
00573                        optional, default: 'patDefaultSequence'
00574     - hltProcess     : HLT process name;
00575                        optional, default: 'HLT'
00576     - outputModule   : output module label;
00577                        empty label indicates no output;
00578                        optional, default: 'out'
00579     - postfix        : postfix to apply to PAT module labels;
00580                        optional, default: ''
00581     Using None as any argument restores its default value.
00582     """
00583     _label             = 'switchOnTriggerMatchEmbedding'
00584     _defaultParameters = dicttypes.SortedKeysDict()
00585 
00586     def __init__( self ):
00587         ConfigToolBase.__init__( self )
00588         self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
00589         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
00590         self.addParameter( self._defaultParameters, 'sequence'       , _defaultSequence       , _defaultSequenceComment )
00591         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
00592         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
00593         self.addParameter( self._defaultParameters, 'postfix'        , _defaultPostfix        , _defaultPostfixComment )
00594         self._parameters = copy.deepcopy( self._defaultParameters )
00595         self._comment = ""
00596 
00597     def getDefaultParameters( self ):
00598         return self._defaultParameters
00599 
00600     def __call__( self, process
00601                 , triggerMatchers = None
00602                 , triggerProducer = None
00603                 , sequence        = None
00604                 , hltProcess      = None
00605                 , outputModule    = None
00606                 , postfix         = None
00607                 ):
00608         if triggerMatchers is None:
00609             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
00610         if triggerProducer is None:
00611             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
00612         if sequence is None:
00613             sequence = self._defaultParameters[ 'sequence' ].value
00614         if hltProcess is None:
00615             hltProcess = self._defaultParameters[ 'hltProcess' ].value
00616         if outputModule is None:
00617             outputModule = self._defaultParameters[ 'outputModule' ].value
00618         if postfix is None:
00619             postfix = self._defaultParameters[ 'postfix' ].value
00620         self.setParameter( 'triggerMatchers', triggerMatchers )
00621         self.setParameter( 'triggerProducer', triggerProducer )
00622         self.setParameter( 'sequence'       , sequence )
00623         self.setParameter( 'hltProcess'     , hltProcess )
00624         self.setParameter( 'outputModule'   , outputModule )
00625         self.setParameter( 'postfix'        , postfix )
00626         self.apply( process )
00627 
00628     def toolCode( self, process ):
00629         triggerMatchers = self._parameters[ 'triggerMatchers' ].value
00630         triggerProducer = self._parameters[ 'triggerProducer' ].value
00631         sequence        = self._parameters[ 'sequence' ].value
00632         hltProcess      = self._parameters[ 'hltProcess' ].value
00633         outputModule    = self._parameters[ 'outputModule' ].value
00634         postfix         = self._parameters[ 'postfix' ].value
00635 
00636         # Build dictionary of known input collections
00637         dictPatObjects = { 'Photons'  : 'PATTriggerMatchPhotonEmbedder'
00638                          , 'Electrons': 'PATTriggerMatchElectronEmbedder'
00639                          , 'Muons'    : 'PATTriggerMatchMuonEmbedder'
00640                          , 'Taus'     : 'PATTriggerMatchTauEmbedder'
00641                          , 'Jets'     : 'PATTriggerMatchJetEmbedder'
00642                          , 'METs'     : 'PATTriggerMatchMETEmbedder'
00643                          }
00644         listPatSteps   = [ 'pat', 'selectedPat', 'cleanPat' ]
00645         listJetAlgos   = [ 'IC5', 'SC5', 'KT4', 'KT6', 'AK5' ]
00646         listJetTypes   = [ 'Calo', 'PF', 'JPT' ]
00647         dictEmbedders  = {}
00648         for objects in dictPatObjects.keys():
00649             steps = len( listPatSteps )
00650             if objects is 'METs':
00651                 steps = 1
00652             for step in range( steps ):
00653                 coll = listPatSteps[ step ] + objects
00654                 dictEmbedders[ coll ]           = dictPatObjects[ objects ]
00655                 dictEmbedders[ coll + postfix ] = dictPatObjects[ objects ]
00656                 if objects is 'Jets':
00657                     for jetAlgo in listJetAlgos:
00658                         for jetType in listJetTypes:
00659                             jetColl = coll + jetAlgo + jetType
00660                             dictEmbedders[ jetColl ]           = dictPatObjects[ objects ]
00661                             dictEmbedders[ jetColl + postfix ] = dictPatObjects[ objects ]
00662 
00663         # Build dictionary of matchers and switch on PAT trigger matching if needed
00664         dictConfig = {}
00665         matchingOn = False
00666         for matcher in triggerMatchers:
00667             trigMchMod = getattr( process, matcher )
00668             if trigMchMod.src.value() in dictConfig:
00669                 dictConfig[ trigMchMod.src.value() ] += [ matcher ]
00670             else:
00671                 dictConfig[ trigMchMod.src.value() ] = [ matcher ]
00672             if matcher not in _modulesInSequence( process, sequence ) and not matchingOn:
00673                 print '%s():'%( self._label )
00674                 print '    PAT trigger matching switched on automatically using'
00675                 print '    switchOnTriggerMatchingStandAlone( process, %s, %s, %s, %s, %s )'%( hltProcess, triggerMatchers, triggerProducer, sequence, outputModule )
00676                 print _longLine
00677                 switchOnTriggerMatchingStandAlone( process, triggerMatchers, triggerProducer, sequence, hltProcess, '', postfix )
00678                 matchingOn = True
00679 
00680         # Maintain configurations
00681         patTriggerEventContent = []
00682         for srcInput in dictConfig.keys():
00683             if dictEmbedders.has_key( srcInput ):
00684                 # Configure embedder module
00685                 dictIndex = srcInput
00686                 srcInput += postfix
00687                 if dictEmbedders.has_key( srcInput ):
00688                     label = srcInput + 'TriggerMatch'
00689                     if label in _modulesInSequence( process, sequence ):
00690                         print '%s():'%( self._label )
00691                         print '    PAT trigger match embedder %s exists already in sequence %s'%( label, sequence )
00692                         print '    ==> entry moved to proper place'
00693                         print _longLine
00694                         removeIfInSequence( process, label, sequence + 'Trigger' )
00695                     module         = cms.EDProducer( dictEmbedders[ dictIndex ] )
00696                     module.src     = cms.InputTag( srcInput )
00697                     module.matches = cms.VInputTag( dictConfig[ dictIndex ] )
00698                     setattr( process, label, module )
00699                     trigEmbMod = getattr( process, label )
00700                     index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
00701                     getattr( process, sequence + 'Trigger' ).insert( index, trigEmbMod )
00702                     # Add event content
00703                     patTriggerEventContent += [ 'drop *_%s_*_*'%( srcInput )
00704                                               , 'keep *_%s_*_%s'%( label, process.name_() )
00705                                               ]
00706                 else:
00707                     print '%s():'%( self._label )
00708                     print '    Invalid new input source for trigger match embedding'
00709                     print '    ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ dictIndex ] )
00710                     print _longLine
00711             else:
00712                 print '%s():'%( self._label )
00713                 print '    Invalid input source for trigger match embedding'
00714                 print '    ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ srcInput ] )
00715                 print _longLine
00716         if outputModule is not '':
00717             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
00718 
00719 switchOnTriggerMatchEmbedding = SwitchOnTriggerMatchEmbedding()
00720 
00721 
00722 class RemoveCleaningFromTriggerMatching( ConfigToolBase ):
00723     """  Removes cleaning from already existing PAT trigger matching/embedding configuration
00724     RemoveCleaningFromTriggerMatching( [cms.Process], outputModule = 'out' )
00725     - [cms.Process]  : the 'cms.Process'
00726     - sequence       : name of sequence to use;
00727                        optional, default: 'patDefaultSequence'
00728     - outputModule   : output module label;
00729                        empty label indicates no output;
00730                        optional, default: 'out'
00731     Using None as any argument restores its default value.
00732     """
00733     _label             = 'removeCleaningFromTriggerMatching'
00734     _defaultParameters = dicttypes.SortedKeysDict()
00735 
00736     def __init__( self ):
00737         ConfigToolBase.__init__( self )
00738         self.addParameter( self._defaultParameters, 'sequence'    , _defaultSequence    , _defaultSequenceComment )
00739         self.addParameter( self._defaultParameters, 'outputModule', _defaultOutputModule, _defaultOutputModuleComment )
00740         self._parameters = copy.deepcopy( self._defaultParameters )
00741         self._comment = ""
00742 
00743     def getDefaultParameters( self ):
00744         return self._defaultParameters
00745 
00746     def __call__( self, process
00747                 , sequence     = None
00748                 , outputModule = None
00749                 ):
00750         if sequence is None:
00751             sequence = self._defaultParameters[ 'sequence' ].value
00752         if outputModule is None:
00753             outputModule = self._defaultParameters[ 'outputModule' ].value
00754         self.setParameter( 'sequence'    , sequence )
00755         self.setParameter( 'outputModule', outputModule )
00756         self.apply( process )
00757 
00758     def toolCode( self, process ):
00759         sequence     = self._parameters[ 'sequence' ].value
00760         outputModule = self._parameters[ 'outputModule' ].value
00761 
00762         # Maintain configurations
00763         listMatchers = [ 'PATTriggerMatcherDRLessByR'
00764                        , 'PATTriggerMatcherDRDPtLessByR'
00765                        , 'PATTriggerMatcherDRLessByPt'
00766                        , 'PATTriggerMatcherDRDPtLessByPt'
00767                        , 'PATTriggerMatcherDEtaLessByDR'
00768                        , 'PATTriggerMatcherDEtaLessByDEta'
00769                        ]
00770         listEmbedders = [ 'PATTriggerMatchPhotonEmbedder'
00771                         , 'PATTriggerMatchElectronEmbedder'
00772                         , 'PATTriggerMatchMuonEmbedder'
00773                         , 'PATTriggerMatchTauEmbedder'
00774                         , 'PATTriggerMatchJetEmbedder'
00775                         , 'PATTriggerMatchMETEmbedder'
00776                         ]
00777         modules = _modulesInSequence( process, sequence )
00778         oldModules = []
00779         oldSources = []
00780         # input source labels
00781         for module in modules:
00782             if hasattr( process, module ):
00783                 trigMod = getattr( process, module )
00784                 if trigMod.type_() in listMatchers:
00785                     if trigMod.src.value()[ : 8 ] == 'cleanPat':
00786                         trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
00787                         if trigMod.label()[ : 5 ] == 'clean':
00788                             oldModules += [ trigMod.label() ]
00789                             setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
00790                 if trigMod.type_() in listEmbedders:
00791                     if trigMod.src.value()[ : 8 ] == 'cleanPat':
00792                         oldSources += [ trigMod.src.getModuleLabel() ]
00793                         trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
00794                         if trigMod.label()[ : 5 ] == 'clean':
00795                             oldModules += [ trigMod.label() ]
00796                             setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
00797         # matcher labels
00798         for module in modules:
00799             if hasattr( process, module ):
00800                 trigMod = getattr( process, module )
00801                 if trigMod.type_() == 'PATTriggerEventProducer':
00802                     matchers = getattr( trigMod, 'patTriggerMatches' )
00803                     matchers = self._renameMatchers( matchers, oldModules )
00804                 elif trigMod.type_() in listEmbedders:
00805                     matchers = getattr( trigMod, 'matches' )
00806                     matchers = self._renameMatchers( matchers, oldModules )
00807 
00808         # Maintain event content
00809         if outputModule is not '':
00810             patTriggerEventContent = getattr( process, outputModule ).outputCommands
00811             for statement in range( len( patTriggerEventContent ) ):
00812                 for module in oldModules:
00813                     if module in patTriggerEventContent[ statement ]:
00814                         patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
00815                 for source in oldSources:
00816                     if source in patTriggerEventContent[ statement ] and 'drop' in patTriggerEventContent[ statement ]:
00817                         patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
00818         print '%s():'%( self._label )
00819         print '    Input from cleaning has been switched to input from selection;'
00820         print '    matcher and embedder modules have been renamed accordingly.'
00821         print _longLine
00822 
00823     def _renameMatchers( self, matchers, oldModules ):
00824         for matcher in range( len( matchers ) ):
00825             if matchers[ matcher ] in oldModules:
00826                 if matchers[ matcher ][ : 5 ] == 'clean':
00827                      matchers[ matcher ] = matchers[ matcher ].replace( 'clean', 'selected' )
00828         return matchers
00829 
00830 removeCleaningFromTriggerMatching = RemoveCleaningFromTriggerMatching()