Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-27 22:37:27

0001 from __future__ import print_function
0002 from builtins import range
0003 from PhysicsTools.PatAlgos.tools.ConfigToolBase import *
0004 
0005 from PhysicsTools.PatAlgos.tools.helpers import *
0006 from PhysicsTools.PatAlgos.patEventContent_cff import patTriggerL1RefsEventContent
0007 
0008 from PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi import _exampleTriggerMatchers
0009 _defaultTriggerProducer      = 'patTrigger'
0010 _defaultTriggerEventProducer = 'patTriggerEvent'
0011 _defaultPath                 = ''
0012 _defaultHltProcess           = 'HLT'
0013 _defaultOutputModule         = 'out'
0014 _defaultPostfix              = ''
0015 
0016 _defaultTriggerMatchersComment      = "Trigger matcher modules' labels, default: ..."
0017 _defaultTriggerProducerComment      = "PATTriggerProducer module label, default: %s"%( _defaultTriggerProducer )
0018 _defaultTriggerEventProducerComment = "PATTriggerEventProducer module label, default: %s"%( _defaultTriggerEventProducer )
0019 _defaultPathComment                 = "Name of path to use, default: %s"%( _defaultPath )
0020 _defaultHltProcessComment           = "HLT process name, default: %s"%( _defaultHltProcess )
0021 _defaultOutputModuleComment         = "Output module label, empty label indicates no output, default: %s"%( _defaultOutputModule )
0022 _defaultPostfixComment              = "Postfix to apply to PAT module labels, default: %s"%( _defaultPostfix )
0023 
0024 _longLine = '---------------------------------------------------------------------'
0025 
0026 
0027 def _modulesInPath( process, pathLabel ):
0028     return [ m.label() for m in listModules( getattr( process, pathLabel ) ) ]
0029 
0030 
0031 def _addEventContent( outputCommands, eventContent ):
0032     # add new entry to event content
0033     for content in eventContent:
0034         if content not in outputCommands:
0035             outputCommands += [ content ]
0036     # check for obsolete entries
0037     listToRemove = []
0038     for i in range( len( outputCommands ) ):
0039         if i in listToRemove:
0040             continue
0041         command = outputCommands[ i ]
0042         if command[ : 4 ] == 'keep':
0043             dropCommand = command.replace( 'keep ', 'drop ' )
0044             for j in range( i + 1, len( outputCommands ) ):
0045                 testCommand = outputCommands[ j ]
0046                 if testCommand == command:
0047                     listToRemove += [ j ]
0048                 elif testCommand == dropCommand:
0049                     listToRemove += [ i, j ]
0050                     break
0051     # copy entries excl. obsolete ones
0052     newOutputCommands = cms.untracked.vstring()
0053     for i in range( len( outputCommands ) ):
0054         if i not in listToRemove:
0055             newOutputCommands += [ outputCommands[ i ] ]
0056     # return result
0057     return newOutputCommands
0058 
0059 
0060 class SwitchOnTrigger( ConfigToolBase ):
0061     """  Enables trigger information in PAT
0062     SwitchOnTrigger( [cms.Process], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', path = '', hltProcess = 'HLT', outputModule = 'out' )
0063     - [cms.Process]       : the 'cms.Process'
0064     - triggerProducer     : PATTriggerProducer module label;
0065                             optional, default: 'patTrigger'
0066     - triggerEventProducer: PATTriggerEventProducer module label;
0067                             optional, default: 'patTriggerEvent'
0068     - path                : name of path to use;
0069                             optional, default: ''
0070     - hltProcess          : HLT process name;
0071                             optional, default: 'HLT'
0072     - outputModule        : output module label;
0073                             empty label indicates no output;
0074                             optional, default: 'out'
0075     Using None as any argument restores its default value.
0076     """
0077     _label             = 'switchOnTrigger'
0078     _defaultParameters = dicttypes.SortedKeysDict()
0079 
0080     def __init__( self ):
0081         ConfigToolBase.__init__( self )
0082         self.addParameter( self._defaultParameters, 'triggerProducer'     , _defaultTriggerProducer     , _defaultTriggerProducerComment )
0083         self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
0084         self.addParameter( self._defaultParameters, 'path'                , _defaultPath                , _defaultPathComment )
0085         self.addParameter( self._defaultParameters, 'hltProcess'          , _defaultHltProcess          , _defaultHltProcessComment )
0086         self.addParameter( self._defaultParameters, 'outputModule'        , _defaultOutputModule        , _defaultOutputModuleComment )
0087         self._parameters = copy.deepcopy( self._defaultParameters )
0088         self._comment = ""
0089 
0090     def getDefaultParameters( self ):
0091         return self._defaultParameters
0092 
0093     def __call__( self, process
0094                 , triggerProducer      = None
0095                 , triggerEventProducer = None
0096                 , path                 = None
0097                 , hltProcess           = None
0098                 , outputModule         = None
0099                 ):
0100         if triggerProducer is None:
0101             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
0102         if triggerEventProducer is None:
0103             triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
0104         if path is None:
0105             path = self._defaultParameters[ 'path' ].value
0106         if hltProcess is None:
0107             hltProcess = self._defaultParameters[ 'hltProcess' ].value
0108         if outputModule is None:
0109             outputModule = self._defaultParameters[ 'outputModule' ].value
0110         self.setParameter( 'triggerProducer'     , triggerProducer )
0111         self.setParameter( 'triggerEventProducer', triggerEventProducer )
0112         self.setParameter( 'path'                , path )
0113         self.setParameter( 'hltProcess'          , hltProcess )
0114         self.setParameter( 'outputModule'        , outputModule )
0115         self.apply( process )
0116 
0117     def toolCode( self, process ):
0118         triggerProducer      = self._parameters[ 'triggerProducer' ].value
0119         triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
0120         path                 = self._parameters[ 'path' ].value
0121         hltProcess           = self._parameters[ 'hltProcess' ].value
0122         outputModule         = self._parameters[ 'outputModule' ].value
0123 
0124         task = getPatAlgosToolsTask(process)
0125 
0126         # Load default producers from existing config files, if needed
0127         if not hasattr( process, triggerProducer ):
0128             from PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi import patTrigger
0129             addToProcessAndTask(triggerProducer, patTrigger.clone(), process, task)
0130         else:
0131             print('%s():'%( self._label ))
0132             print('    PATTriggerProducer module \'%s\' exists already in process'%( triggerProducer ))
0133             print('    ==> entry re-used')
0134             print(_longLine)
0135         if not hasattr( process, triggerEventProducer ):
0136             from PhysicsTools.PatAlgos.triggerLayer1.triggerEventProducer_cfi import patTriggerEvent
0137             addToProcessAndTask(triggerEventProducer, patTriggerEvent.clone(), process, task)
0138         else:
0139             print('%s():'%( self._label ))
0140             print('    PATTriggerEventProducer module \'%s\' exists already in process'%( triggerEventProducer ))
0141             print('    ==> entry re-used')
0142             print(_longLine)
0143 
0144         # Maintain configurations
0145         trigProdMod             = getattr( process, triggerProducer )
0146         trigProdMod.processName = hltProcess
0147         if trigProdMod.onlyStandAlone.value() is True:
0148             trigProdMod.onlyStandAlone = False
0149             print('    configuration parameter automatically changed')
0150             print('    PATTriggerProducer %s.onlyStandAlone --> %s'%( triggerProducer, trigProdMod.onlyStandAlone ))
0151             print(_longLine)
0152         trigEvtProdMod                    = getattr( process, triggerEventProducer )
0153         trigEvtProdMod.processName        = hltProcess
0154         trigEvtProdMod.patTriggerProducer = cms.InputTag( triggerProducer )
0155         if path != '':
0156             if not hasattr( process, path ):
0157                 prodPath = cms.Path( trigProdMod + trigEvtProdMod )
0158                 setattr( process, path, prodPath )
0159                 print('%s():'%( self._label ))
0160                 print('    Path \'%s\' does not exist in process'%( path ))
0161                 print('    ==> created')
0162                 print(_longLine)
0163             # Try to get the order right, but cannot deal with all possible cases.
0164             # Simply rely on the exclusive usage of these tools without manual intervention.
0165             else:
0166                 if not triggerProducer in _modulesInPath( process, path ):
0167                     prodPath = getattr( process, path )
0168                     prodPath += trigProdMod
0169                 if not triggerEventProducer in _modulesInPath( process, path ):
0170                     prodPath = getattr( process, path )
0171                     prodPath += trigEvtProdMod
0172 
0173         # Add event content
0174         if outputModule != '':
0175             patTriggerEventContent = [ 'keep patTriggerObjects_%s_*_%s'%( triggerProducer, process.name_() )
0176                                      , 'keep patTriggerFilters_%s_*_%s'%( triggerProducer, process.name_() )
0177                                      , 'keep patTriggerPaths_%s_*_%s'%( triggerProducer, process.name_() )
0178                                      , 'keep patTriggerEvent_%s_*_%s'%( triggerEventProducer, process.name_() )
0179                                      ]
0180             if ( hasattr( trigProdMod, 'addL1Algos' ) and trigProdMod.addL1Algos.value() is True ):
0181                 patTriggerEventContent += [ 'keep patTriggerConditions_%s_*_%s'%( triggerProducer, process.name_() )
0182                                           , 'keep patTriggerAlgorithms_%s_*_%s'%( triggerProducer, process.name_() )
0183                                           ]
0184             if ( hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True ):
0185                 patTriggerEventContent += patTriggerL1RefsEventContent
0186             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
0187 
0188 switchOnTrigger = SwitchOnTrigger()
0189 
0190 
0191 class SwitchOnTriggerStandAlone( ConfigToolBase ):
0192     """  Enables trigger information in PAT, limited to stand-alone trigger objects
0193     SwitchOnTriggerStandAlone( [cms.Process], triggerProducer = 'patTrigger', path = '', hltProcess = 'HLT', outputModule = 'out' )
0194     - [cms.Process]       : the 'cms.Process'
0195     - triggerProducer     : PATTriggerProducer module label;
0196                             optional, default: 'patTrigger'
0197     - path                : name of path to use;
0198                             optional, default: ''
0199     - hltProcess          : HLT process name;
0200                             optional, default: 'HLT'
0201     - outputModule        : output module label;
0202                             empty label indicates no output;
0203                             optional, default: 'out'
0204     Using None as any argument restores its default value.
0205     """
0206     _label             = 'switchOnTriggerStandAlone'
0207     _defaultParameters = dicttypes.SortedKeysDict()
0208 
0209     def __init__( self ):
0210         ConfigToolBase.__init__( self )
0211         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
0212         self.addParameter( self._defaultParameters, 'path'           , _defaultPath           , _defaultPathComment )
0213         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
0214         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
0215         self._parameters = copy.deepcopy( self._defaultParameters )
0216         self._comment = ""
0217 
0218     def getDefaultParameters( self ):
0219         return self._defaultParameters
0220 
0221     def __call__( self, process
0222                 , triggerProducer      = None
0223                 , path                 = None
0224                 , hltProcess           = None
0225                 , outputModule         = None
0226                 ):
0227         if triggerProducer is None:
0228             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
0229         if path is None:
0230             path = self._defaultParameters[ 'path' ].value
0231         if hltProcess is None:
0232             hltProcess = self._defaultParameters[ 'hltProcess' ].value
0233         if outputModule is None:
0234             outputModule = self._defaultParameters[ 'outputModule' ].value
0235         self.setParameter( 'triggerProducer', triggerProducer )
0236         self.setParameter( 'path'           , path )
0237         self.setParameter( 'hltProcess'     , hltProcess )
0238         self.setParameter( 'outputModule'   , outputModule )
0239         self.apply( process )
0240 
0241     def toolCode( self, process ):
0242 
0243         task = getPatAlgosToolsTask(process)
0244 
0245         triggerProducer = self._parameters[ 'triggerProducer' ].value
0246         path            = self._parameters[ 'path' ].value
0247         hltProcess      = self._parameters[ 'hltProcess' ].value
0248         outputModule    = self._parameters[ 'outputModule' ].value
0249 
0250         # Load default producer from existing config file, if needed
0251         if not hasattr( process, triggerProducer ):
0252             from PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi import patTrigger
0253             addToProcessAndTask(triggerProducer, patTrigger.clone( onlyStandAlone = True ), process, task)
0254         else:
0255             print('%s():'%( self._label ))
0256             print('    PATTriggerProducer module \'%s\' exists already in process'%( triggerProducer ))
0257             print('    ==> entry re-used')
0258             print(_longLine)
0259 
0260         # Maintain configuration
0261         trigProdMod             = getattr( process, triggerProducer )
0262         trigProdMod.processName = hltProcess
0263         if path != '':
0264             if not hasattr( process, path ):
0265                 prodPath = cms.Path( trigProdMod )
0266                 setattr( process, path, prodPath )
0267                 print('%s():'%( self._label ))
0268                 print('    Path \'%s\' does not exist in process'%( path ))
0269                 print('    ==> created')
0270                 print(_longLine)
0271             elif not triggerProducer in _modulesInPath( process, path ):
0272                 prodPath = getattr( process, path )
0273                 prodPath += trigProdMod
0274 
0275         # Add event content
0276         if outputModule != '':
0277             patTriggerEventContent = [ 'keep patTriggerObjectStandAlones_%s_*_%s'%( triggerProducer, process.name_() )
0278                                      ]
0279             if ( hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True ):
0280                 patTriggerEventContent += patTriggerL1RefsEventContent
0281             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
0282 
0283 switchOnTriggerStandAlone = SwitchOnTriggerStandAlone()
0284 
0285 
0286 class SwitchOnTriggerMatching( ConfigToolBase ):
0287     """  Enables trigger matching in PAT
0288     SwitchOnTriggerMatching( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', path = '', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
0289     - [cms.Process]       : the 'cms.Process'
0290     - triggerMatchers     : PAT trigger matcher module labels (list)
0291                             optional; default: defined in '_exampleTriggerMatchers'
0292                             (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcherExamples_cfi.py)
0293     - triggerProducer     : PATTriggerProducer module label;
0294                             optional, default: 'patTrigger'
0295     - triggerEventProducer: PATTriggerEventProducer module label;
0296                             optional, default: 'patTriggerEvent'
0297     - path                : name of path to use;
0298                             optional, default: 'patDefaultSequence'
0299     - hltProcess          : HLT process name;
0300                             optional, default: 'HLT'
0301     - outputModule        : output module label;
0302                             empty label indicates no output;
0303                             optional, default: 'out'
0304     - postfix             : postfix to apply to PAT module labels;
0305                             optional, default: ''
0306     Using None as any argument restores its default value.
0307     """
0308     _label             = 'switchOnTriggerMatching'
0309     _defaultParameters = dicttypes.SortedKeysDict()
0310 
0311     def __init__( self ):
0312         ConfigToolBase.__init__( self )
0313         self.addParameter( self._defaultParameters, 'triggerMatchers'     , _exampleTriggerMatchers     , _defaultTriggerMatchersComment )
0314         self.addParameter( self._defaultParameters, 'exampleMatchers'     , False                       , '' )
0315         self.addParameter( self._defaultParameters, 'triggerProducer'     , _defaultTriggerProducer     , _defaultTriggerProducerComment )
0316         self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
0317         self.addParameter( self._defaultParameters, 'path'                , _defaultPath                , _defaultPathComment )
0318         self.addParameter( self._defaultParameters, 'hltProcess'          , _defaultHltProcess          , _defaultHltProcessComment )
0319         self.addParameter( self._defaultParameters, 'outputModule'        , _defaultOutputModule        , _defaultOutputModuleComment )
0320         self.addParameter( self._defaultParameters, 'postfix'             , _defaultPostfix             , _defaultPostfixComment )
0321         self._parameters = copy.deepcopy( self._defaultParameters )
0322         self._comment = ""
0323 
0324     def getDefaultParameters( self ):
0325         return self._defaultParameters
0326 
0327     def __call__( self, process
0328                 , triggerMatchers      = None
0329                 , triggerProducer      = None
0330                 , triggerEventProducer = None
0331                 , path                 = None
0332                 , hltProcess           = None
0333                 , outputModule         = None
0334                 , postfix              = None
0335                 ):
0336         if triggerMatchers is None:
0337             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
0338             self.setParameter( 'exampleMatchers', True )
0339         if triggerProducer is None:
0340             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
0341         if triggerEventProducer is None:
0342             triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
0343         if path is None:
0344             path = self._defaultParameters[ 'path' ].value
0345         if hltProcess is None:
0346             hltProcess = self._defaultParameters[ 'hltProcess' ].value
0347         if outputModule is None:
0348             outputModule = self._defaultParameters[ 'outputModule' ].value
0349         if postfix is None:
0350             postfix = self._defaultParameters[ 'postfix' ].value
0351         self.setParameter( 'triggerMatchers'     , triggerMatchers )
0352         self.setParameter( 'triggerProducer'     , triggerProducer )
0353         self.setParameter( 'triggerEventProducer', triggerEventProducer )
0354         self.setParameter( 'path'                , path )
0355         self.setParameter( 'hltProcess'          , hltProcess )
0356         self.setParameter( 'outputModule'        , outputModule )
0357         self.setParameter( 'postfix'             , postfix )
0358         self.apply( process )
0359 
0360     def toolCode( self, process ):
0361         triggerMatchers      = self._parameters[ 'triggerMatchers' ].value
0362         exampleMatchers      = self._parameters[ 'exampleMatchers' ].value
0363         triggerProducer      = self._parameters[ 'triggerProducer' ].value
0364         triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
0365         path                 = self._parameters[ 'path' ].value
0366         hltProcess           = self._parameters[ 'hltProcess' ].value
0367         outputModule         = self._parameters[ 'outputModule' ].value
0368         postfix              = self._parameters[ 'postfix' ].value
0369 
0370         # Load default producers from existing config file, if needed
0371         task = getPatAlgosToolsTask(process)
0372         if exampleMatchers:
0373             process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
0374             task.add(process.triggerMatcherExamplesTask)
0375         # Switch on PAT trigger information if needed
0376         if not hasattr( process, triggerEventProducer ):
0377             print('%s():'%( self._label ))
0378             print('    PAT trigger production switched on automatically using')
0379             print('    switchOnTrigger( process, \'%s\', \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerProducer, triggerEventProducer, path, outputModule ))
0380             print(_longLine)
0381             switchOnTrigger( process, triggerProducer, triggerEventProducer, path, hltProcess, outputModule )
0382 
0383         # Maintain configurations
0384         trigEvtProdMod = getattr( process, triggerEventProducer )
0385         triggerMatchersKnown = []
0386         for matcher in triggerMatchers:
0387             if not hasattr( process, matcher ):
0388                 print('%s():'%( self._label ))
0389                 print('    Matcher \'%s\' not known to process'%( matcher ))
0390                 print('    ==> skipped')
0391                 print(_longLine)
0392                 continue
0393             triggerMatchersKnown.append( matcher )
0394             trigMchMod         = getattr( process, matcher )
0395             trigMchMod.src     = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
0396             trigMchMod.matched = triggerProducer
0397         matchers = getattr( trigEvtProdMod, 'patTriggerMatches' )
0398         if len( matchers ) > 0:
0399             print('%s():'%( self._label ))
0400             print('    PAT trigger matchers already attached to existing PATTriggerEventProducer \'%s\''%( triggerEventProducer ))
0401             print('    configuration parameters automatically changed')
0402             for matcher in matchers:
0403                 trigMchMod = getattr( process, matcher )
0404                 if trigMchMod.matched.value() is not triggerProducer:
0405                     trigMchMod.matched = triggerProducer
0406                     print('    PAT trigger matcher %s.matched --> %s'%( matcher, trigMchMod.matched ))
0407             print(_longLine)
0408         else:
0409             trigEvtProdMod.patTriggerMatches = cms.VInputTag()
0410         for matcher in triggerMatchersKnown:
0411             trigEvtProdMod.patTriggerMatches.append( cms.InputTag( matcher ) )
0412 
0413         # Add event content
0414         if outputModule != '':
0415             patTriggerEventContent = []
0416             for matcher in triggerMatchersKnown:
0417                 patTriggerEventContent += [ 'keep patTriggerObjectsedmAssociation_%s_%s_%s'%( triggerEventProducer, matcher, process.name_() )
0418                                           , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
0419                                           ]
0420             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
0421 
0422 switchOnTriggerMatching = SwitchOnTriggerMatching()
0423 
0424 
0425 class SwitchOnTriggerMatchingStandAlone( ConfigToolBase ):
0426     """  Enables trigger matching in PAT
0427     SwitchOnTriggerMatchingStandAlone( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', path = '', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
0428     - [cms.Process]  : the 'cms.Process'
0429     - triggerMatchers: PAT trigger matcher module labels (list)
0430                        optional; default: defined in 'triggerMatchingDefaultSequence'
0431                        (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcherExamples_cfi.py)
0432     - triggerProducer: PATTriggerProducer module label;
0433                        optional, default: 'patTrigger'
0434     - path           : name of path to use;
0435                        optional, default: ''
0436     - hltProcess     : HLT process name;
0437                        optional, default: 'HLT'
0438     - outputModule   : output module label;
0439                        empty label indicates no output;
0440                        optional, default: 'out'
0441     - postfix        : postfix to apply to PAT module labels;
0442                        optional, default: ''
0443     Using None as any argument restores its default value.
0444     """
0445     _label             = 'switchOnTriggerMatchingStandAlone'
0446     _defaultParameters = dicttypes.SortedKeysDict()
0447 
0448     def __init__( self ):
0449         ConfigToolBase.__init__( self )
0450         self.addParameter( self._defaultParameters, 'triggerMatchers', _exampleTriggerMatchers, _defaultTriggerMatchersComment )
0451         self.addParameter( self._defaultParameters, 'exampleMatchers', False                  , '' )
0452         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
0453         self.addParameter( self._defaultParameters, 'path'           , _defaultPath           , _defaultPathComment )
0454         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
0455         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
0456         self.addParameter( self._defaultParameters, 'postfix'        , _defaultPostfix        , _defaultPostfixComment )
0457         self._parameters = copy.deepcopy( self._defaultParameters )
0458         self._comment = ""
0459 
0460     def getDefaultParameters( self ):
0461         return self._defaultParameters
0462 
0463     def __call__( self, process
0464                 , triggerMatchers = None
0465                 , triggerProducer = None
0466                 , path            = None
0467                 , hltProcess      = None
0468                 , outputModule    = None
0469                 , postfix         = None
0470                 ):
0471         if triggerMatchers is None:
0472             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
0473             self.setParameter( 'exampleMatchers', True )
0474         if triggerProducer is None:
0475             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
0476         if path is None:
0477             path = self._defaultParameters[ 'path' ].value
0478         if hltProcess is None:
0479             hltProcess = self._defaultParameters[ 'hltProcess' ].value
0480         if outputModule is None:
0481             outputModule = self._defaultParameters[ 'outputModule' ].value
0482         if postfix is None:
0483             postfix = self._defaultParameters[ 'postfix' ].value
0484         self.setParameter( 'triggerMatchers', triggerMatchers )
0485         self.setParameter( 'triggerProducer', triggerProducer )
0486         self.setParameter( 'path'           , path )
0487         self.setParameter( 'hltProcess'     , hltProcess )
0488         self.setParameter( 'outputModule'   , outputModule )
0489         self.setParameter( 'postfix'        , postfix )
0490         self.apply( process )
0491 
0492     def toolCode( self, process ):
0493         triggerMatchers = self._parameters[ 'triggerMatchers' ].value
0494         exampleMatchers = self._parameters[ 'exampleMatchers' ].value
0495         triggerProducer = self._parameters[ 'triggerProducer' ].value
0496         path            = self._parameters[ 'path' ].value
0497         hltProcess      = self._parameters[ 'hltProcess' ].value
0498         outputModule    = self._parameters[ 'outputModule' ].value
0499         postfix         = self._parameters[ 'postfix' ].value
0500 
0501         # Load default producers from existing config file, if needed
0502         task = getPatAlgosToolsTask(process)
0503         if exampleMatchers:
0504             process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
0505             task.add(process.triggerMatcherExamplesTask)
0506 
0507         # Switch on PAT trigger information if needed
0508         if not hasattr( process, triggerProducer ):
0509             print('%s():'%( self._label ))
0510             print('    PAT trigger production switched on automatically using')
0511             print('    switchOnTriggerStandAlone( process, \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerProducer, path, outputModule ))
0512             print(_longLine)
0513             switchOnTriggerStandAlone( process, triggerProducer, path, hltProcess, outputModule )
0514 
0515         # Maintain configurations
0516         triggerMatchersKnown = []
0517         for matcher in triggerMatchers:
0518             if not hasattr( process, matcher ):
0519                 print('%s():'%( self._label ))
0520                 print('    Matcher \'%s\' not known to process'%( matcher ))
0521                 print('    ==> skipped')
0522                 print(_longLine)
0523                 continue
0524             triggerMatchersKnown.append( matcher )
0525             trigMchMod         = getattr( process, matcher )
0526             trigMchMod.src     = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
0527             trigMchMod.matched = triggerProducer
0528 
0529         # Add event content
0530         if outputModule != '':
0531             patTriggerEventContent = []
0532             for matcher in triggerMatchersKnown:
0533                 patTriggerEventContent += [ 'keep patTriggerObjectStandAlonesedmAssociation_%s_*_%s'%( matcher, process.name_() )
0534                                           , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
0535                                           ]
0536             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
0537 
0538 switchOnTriggerMatchingStandAlone = SwitchOnTriggerMatchingStandAlone()
0539 
0540 
0541 class SwitchOnTriggerMatchEmbedding( ConfigToolBase ):
0542     """  Enables embedding of trigger matches into PAT objects
0543     SwitchOnTriggerMatchEmbedding( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', path = '', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
0544     - [cms.Process]  : the 'cms.Process'
0545     - triggerMatchers: PAT trigger matcher module labels (list)
0546                        optional; default: defined in 'triggerMatchingDefaultSequence'
0547                        (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcherExamples_cfi.py)
0548     - triggerProducer: PATTriggerProducer module label;
0549                        optional, default: 'patTrigger'
0550     - path           : name of path to use;
0551                        optional, default: ''
0552     - hltProcess     : HLT process name;
0553                        optional, default: 'HLT'
0554     - outputModule   : output module label;
0555                        empty label indicates no output;
0556                        optional, default: 'out'
0557     - postfix        : postfix to apply to PAT module labels;
0558                        optional, default: ''
0559     Using None as any argument restores its default value.
0560     """
0561     _label             = 'switchOnTriggerMatchEmbedding'
0562     _defaultParameters = dicttypes.SortedKeysDict()
0563 
0564     def __init__( self ):
0565         ConfigToolBase.__init__( self )
0566         self.addParameter( self._defaultParameters, 'triggerMatchers', _exampleTriggerMatchers, _defaultTriggerMatchersComment )
0567         self.addParameter( self._defaultParameters, 'exampleMatchers', False                  , '' )
0568         self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
0569         self.addParameter( self._defaultParameters, 'path'           , _defaultPath           , _defaultPathComment )
0570         self.addParameter( self._defaultParameters, 'hltProcess'     , _defaultHltProcess     , _defaultHltProcessComment )
0571         self.addParameter( self._defaultParameters, 'outputModule'   , _defaultOutputModule   , _defaultOutputModuleComment )
0572         self.addParameter( self._defaultParameters, 'postfix'        , _defaultPostfix        , _defaultPostfixComment )
0573         self._parameters = copy.deepcopy( self._defaultParameters )
0574         self._comment = ""
0575 
0576     def getDefaultParameters( self ):
0577         return self._defaultParameters
0578 
0579     def __call__( self, process
0580                 , triggerMatchers = None
0581                 , triggerProducer = None
0582                 , path            = None
0583                 , hltProcess      = None
0584                 , outputModule    = None
0585                 , postfix         = None
0586                 ):
0587         if triggerMatchers is None:
0588             triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
0589             self.setParameter( 'exampleMatchers', True )
0590         if triggerProducer is None:
0591             triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
0592         if path is None:
0593             path = self._defaultParameters[ 'path' ].value
0594         if hltProcess is None:
0595             hltProcess = self._defaultParameters[ 'hltProcess' ].value
0596         if outputModule is None:
0597             outputModule = self._defaultParameters[ 'outputModule' ].value
0598         if postfix is None:
0599             postfix = self._defaultParameters[ 'postfix' ].value
0600         self.setParameter( 'triggerMatchers', triggerMatchers )
0601         self.setParameter( 'triggerProducer', triggerProducer )
0602         self.setParameter( 'path'           , path )
0603         self.setParameter( 'hltProcess'     , hltProcess )
0604         self.setParameter( 'outputModule'   , outputModule )
0605         self.setParameter( 'postfix'        , postfix )
0606         self.apply( process )
0607 
0608     def toolCode( self, process ):
0609         triggerMatchers = self._parameters[ 'triggerMatchers' ].value
0610         exampleMatchers = self._parameters[ 'exampleMatchers' ].value
0611         triggerProducer = self._parameters[ 'triggerProducer' ].value
0612         path            = self._parameters[ 'path' ].value
0613         hltProcess      = self._parameters[ 'hltProcess' ].value
0614         outputModule    = self._parameters[ 'outputModule' ].value
0615         postfix         = self._parameters[ 'postfix' ].value
0616 
0617         # Load default producers from existing config file, if needed
0618         task = getPatAlgosToolsTask(process)
0619         if exampleMatchers:
0620             process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
0621             task.add(process.triggerMatcherExamplesTask)
0622 
0623         # Build dictionary of allowed input producers
0624         dictPatProducers = { 'PATPhotonCleaner'  : 'PATTriggerMatchPhotonEmbedder'
0625                            , 'PATElectronCleaner': 'PATTriggerMatchElectronEmbedder'
0626                            , 'PATMuonCleaner'    : 'PATTriggerMatchMuonEmbedder'
0627                            , 'PATTauCleaner'     : 'PATTriggerMatchTauEmbedder'
0628                            , 'PATJetCleaner'     : 'PATTriggerMatchJetEmbedder'
0629                            , 'PATMETCleaner'     : 'PATTriggerMatchMETEmbedder'
0630 #                            , 'PATGenericParticleCleaner'     : ''
0631 #                            , 'PATPFParticleCleaner'     : ''
0632                            , 'PATPhotonSelector'  : 'PATTriggerMatchPhotonEmbedder'
0633                            , 'PATElectronSelector': 'PATTriggerMatchElectronEmbedder'
0634                            , 'PATMuonSelector'    : 'PATTriggerMatchMuonEmbedder'
0635                            , 'PATTauSelector'     : 'PATTriggerMatchTauEmbedder'
0636                            , 'PATJetSelector'     : 'PATTriggerMatchJetEmbedder'
0637                            , 'PATMETSelector'     : 'PATTriggerMatchMETEmbedder'
0638 #                            , 'PATGenericParticleSelector'     : ''
0639 #                            , 'PATPFParticleSelector'     : ''
0640 #                            , 'PATCompositeCandidateSelector'     : ''
0641                            , 'PATPhotonRefSelector'  : 'PATTriggerMatchPhotonEmbedder'
0642                            , 'PATElectronRefSelector': 'PATTriggerMatchElectronEmbedder'
0643                            , 'PATMuonRefSelector'    : 'PATTriggerMatchMuonEmbedder'
0644                            , 'PATTauRefSelector'     : 'PATTriggerMatchTauEmbedder'
0645                            , 'PATJetRefSelector'     : 'PATTriggerMatchJetEmbedder'
0646                            , 'PATMETRefSelector'     : 'PATTriggerMatchMETEmbedder'
0647 #                            , 'PATGenericParticleRefSelector'     : ''
0648 #                            , 'PATPFParticleRefSelector'     : ''
0649 #                            , 'PATCompositeCandidateRefSelector'     : ''
0650                            , 'PATPhotonProducer'  : 'PATTriggerMatchPhotonEmbedder'
0651                            , 'PATElectronProducer': 'PATTriggerMatchElectronEmbedder'
0652                            , 'PATMuonProducer'    : 'PATTriggerMatchMuonEmbedder'
0653                            , 'PATTauProducer'     : 'PATTriggerMatchTauEmbedder'
0654                            , 'PATJetProducer'     : 'PATTriggerMatchJetEmbedder'
0655                            , 'PATMETProducer'     : 'PATTriggerMatchMETEmbedder'
0656 #                            , 'PATGenericParticleProducer'     : ''
0657 #                            , 'PATPFParticleProducer'     : ''
0658 #                            , 'PATCompositeCandidateProducer'     : ''
0659                            , 'MuonSelectorVertex': 'PATTriggerMatchMuonEmbedder'
0660                            }
0661 
0662         # Switch on PAT trigger matching if needed
0663         dictConfig = {}
0664         if not hasattr( process, triggerProducer ):
0665             if exampleMatchers:
0666                 print('%s():'%( self._label ))
0667                 print('    PAT trigger matching switched on automatically using')
0668                 print('    switchOnTriggerMatchingStandAlone( process, \'%s\', None, \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerProducer, path, outputModule, postfix ))
0669                 print(_longLine)
0670                 switchOnTriggerMatchingStandAlone( process, None, triggerProducer, path, hltProcess, '', postfix ) # Do not store intermediate output collections.
0671             else:
0672                 print('%s():'%( self._label ))
0673                 print('    PAT trigger matching switched on automatically using')
0674                 print('    switchOnTriggerMatchingStandAlone( process, \'%s\', %s, \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerMatchers, triggerProducer, path, outputModule, postfix ))
0675                 print(_longLine)
0676                 switchOnTriggerMatchingStandAlone( process, triggerMatchers, triggerProducer, path, hltProcess, '', postfix ) # Do not store intermediate output collections.
0677         elif exampleMatchers:
0678             process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
0679             task.add(process.triggerMatcherExamplesTask)
0680 
0681         # Build dictionary of matchers
0682         for matcher in triggerMatchers:
0683             if not hasattr( process, matcher ):
0684                 print('%s():'%( self._label ))
0685                 print('    PAT trigger matcher \'%s\' not known to process'%( matcher ))
0686                 print('    ==> skipped')
0687                 print(_longLine)
0688                 continue
0689             trigMchMod = getattr( process, matcher )
0690             patObjProd = getattr( process, trigMchMod.src.value() + postfix )
0691             if trigMchMod.src.value() in dictConfig:
0692                 dictConfig[ patObjProd.type_() ] += [ matcher ]
0693             else:
0694                 dictConfig[ patObjProd.type_() ] = [ matcher ]
0695 
0696         # Maintain configurations
0697         patTriggerEventContent = []
0698         for patObjProdType in dictConfig.keys():
0699             if patObjProdType in dictPatProducers:
0700                 for matcher in dictConfig[ patObjProdType ]:
0701                     trigMchMod = getattr( process, matcher )
0702                     patObjProd = getattr( process, trigMchMod.src.value() + postfix )
0703                     # Configure embedder module
0704                     label = patObjProd.label_() + 'TriggerMatch' # hardcoded default
0705                     if hasattr( process, label ):
0706                         print('%s():'%( self._label ))
0707                         print('    PAT trigger match embedder \'%s\' exists already in process'%( label ))
0708                         print('    ==> entry re-used')
0709                         print(_longLine)
0710                         module = getattr( process, label )
0711                         if not module.type_() is dictPatProducers[ patObjProdType ]:
0712                             print('%s():'%( self._label ))
0713                             print('    Configuration conflict for PAT trigger match embedder \'%s\''%( label ))
0714                             print('    - exists as %s'%( module.type_() ))
0715                             print('    - requested as %s by \'%s\''%( dictPatProducers[ patObjProdType ], matcher ))
0716                             print('    ==> skipped')
0717                             print(_longLine)
0718                             continue
0719                         if not module.src.value() is trigMchMod.src.value() + postfix:
0720                             print('%s():'%( self._label ))
0721                             print('    Configuration conflict for PAT trigger match embedder \'%s\''%( label ))
0722                             print('    - exists for input %s'%( module.src.value() ))
0723                             print('    - requested for input %s by \'%s\''%( trigMchMod.src.value() + postfix, matcher ))
0724                             print('    ==> skipped')
0725                             print(_longLine)
0726                             continue
0727                         module.matches.append( cms.InputTag( matcher ) )
0728                     else:
0729                         module         = cms.EDProducer( dictPatProducers[ patObjProdType ] )
0730                         module.src     = cms.InputTag( patObjProd.label_() )
0731                         module.matches = cms.VInputTag( matcher )
0732                         addToProcessAndTask(label, module, process, task)
0733                     # Add event content
0734                     patTriggerEventContent += [ 'drop *_%s_*_*'%( patObjProd.label_() )
0735                                               , 'keep *_%s_*_%s'%( label, process.name_() )
0736                                               ]
0737             else:
0738                 print('%s():'%( self._label ))
0739                 print('    Invalid input source for trigger match embedding')
0740                 print('    ==> %s with matchers \'%s\' is skipped'%( patObjProdType, dictConfig[ patObjProdType ] ))
0741                 print(_longLine)
0742         if outputModule != '':
0743             getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
0744 
0745 switchOnTriggerMatchEmbedding = SwitchOnTriggerMatchEmbedding()