CMS 3D CMS Logo

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