CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
trigTools.py
Go to the documentation of this file.
2 
4 from PhysicsTools.PatAlgos.patEventContent_cff import patTriggerL1RefsEventContent
5 
6 _defaultTriggerMatchers = [ 'cleanMuonTriggerMatchHLTMu20'
7  , 'cleanMuonTriggerMatchHLTDoubleMu6'
8  , 'cleanPhotonTriggerMatchHLTPhoton26IsoVLPhoton18'
9  , 'cleanElectronTriggerMatchHLTEle27CaloIdVTCaloIsoTTrkIdTTrkIsoT'
10  , 'cleanTauTriggerMatchHLTDoubleIsoPFTau20Trk5'
11  , 'cleanJetTriggerMatchHLTJet240'
12  , 'metTriggerMatchHLTMET100'
13  , 'cleanMuonTriggerMatchHLTMu17CentralJet30'
14  , 'cleanJetTriggerMatchHLTMu17CentralJet30'
15  ]
16 _defaultTriggerProducer = 'patTrigger'
17 _defaultTriggerEventProducer = 'patTriggerEvent'
18 _defaultSequence = 'patDefaultSequence'
19 _defaultHltProcess = 'HLT'
20 _defaultOutputModule = 'out'
21 _defaultPostfix = ''
22 
23 _defaultTriggerMatchersComment = "Trigger matcher modules' labels, default: ..."
24 _defaultTriggerProducerComment = "PATTriggerProducer module label, default: %s"%( _defaultTriggerProducer )
25 _defaultTriggerEventProducerComment = "PATTriggerEventProducer module label, default: %s"%( _defaultTriggerEventProducer )
26 _defaultSequenceComment = "Name of sequence to use, default: %s"%( _defaultSequence )
27 _defaultHltProcessComment = "HLT process name, default: %s"%( _defaultHltProcess )
28 _defaultOutputModuleComment = "Output module label, empty label indicates no output, default: %s"%( _defaultOutputModule )
29 _defaultPostfixComment = "Postfix to apply to PAT module labels, default: %s"%( _defaultPostfix )
30 
31 _longLine = '---------------------------------------------------------------------'
32 
33 
34 def _modulesInSequence( process, sequenceLabel ):
35  return [ m.label() for m in listModules( getattr( process, sequenceLabel ) ) ]
36 
37 
38 def _addEventContent( outputCommands, eventContent ):
39  # add new entry to event content
40  for content in eventContent:
41  if content not in outputCommands:
42  outputCommands += [ content ]
43  # check for obsolete entries
44  listToRemove = []
45  for i in range( len( outputCommands ) ):
46  if i in listToRemove:
47  continue
48  command = outputCommands[ i ]
49  if command[ : 4 ] == 'keep':
50  dropCommand = command.replace( 'keep ', 'drop ' )
51  for j in range( i + 1, len( outputCommands ) ):
52  testCommand = outputCommands[ j ]
53  if testCommand == command:
54  listToRemove += [ j ]
55  elif testCommand == dropCommand:
56  listToRemove += [ i, j ]
57  break
58  # copy entries excl. obsolete ones
59  newOutputCommands = cms.untracked.vstring()
60  for i in range( len( outputCommands ) ):
61  if i not in listToRemove:
62  newOutputCommands += [ outputCommands[ i ] ]
63  # return result
64  return newOutputCommands
65 
66 
68  """ Enables trigger information in PAT
69  SwitchOnTrigger( [cms.Process], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out' )
70  - [cms.Process] : the 'cms.Process'
71  - triggerProducer : PATTriggerProducer module label;
72  optional, default: 'patTrigger'
73  - triggerEventProducer: PATTriggerEventProducer module label;
74  optional, default: 'patTriggerEvent'
75  - sequence : name of sequence to use;
76  optional, default: 'patDefaultSequence'
77  - hltProcess : HLT process name;
78  optional, default: 'HLT'
79  - outputModule : output module label;
80  empty label indicates no output;
81  optional, default: 'out'
82  Using None as any argument restores its default value.
83  """
84  _label = 'switchOnTrigger'
85  _defaultParameters = dicttypes.SortedKeysDict()
86 
87  def __init__( self ):
88  ConfigToolBase.__init__( self )
89  self.addParameter( self._defaultParameters, 'triggerProducer' , _defaultTriggerProducer , _defaultTriggerProducerComment )
90  self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
91  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
92  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
93  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
94  self._parameters = copy.deepcopy( self._defaultParameters )
95  self._comment = ""
96 
97  def getDefaultParameters( self ):
98  return self._defaultParameters
99 
100  def __call__( self, process
101  , triggerProducer = None
102  , triggerEventProducer = None
103  , sequence = None
104  , hltProcess = None
105  , outputModule = None
106  ):
107  if triggerProducer is None:
108  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
109  if triggerEventProducer is None:
110  triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
111  if sequence is None:
112  sequence = self._defaultParameters[ 'sequence' ].value
113  if hltProcess is None:
114  hltProcess = self._defaultParameters[ 'hltProcess' ].value
115  if outputModule is None:
116  outputModule = self._defaultParameters[ 'outputModule' ].value
117  self.setParameter( 'triggerProducer' , triggerProducer )
118  self.setParameter( 'triggerEventProducer', triggerEventProducer )
119  self.setParameter( 'sequence' , sequence )
120  self.setParameter( 'hltProcess' , hltProcess )
121  self.setParameter( 'outputModule' , outputModule )
122  self.apply( process )
123 
124  def toolCode( self, process ):
125  triggerProducer = self._parameters[ 'triggerProducer' ].value
126  triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
127  sequence = self._parameters[ 'sequence' ].value
128  hltProcess = self._parameters[ 'hltProcess' ].value
129  outputModule = self._parameters[ 'outputModule' ].value
130 
131  # Load default producers from existing config files, if needed
132  if not hasattr( process, triggerProducer ):
133  if triggerProducer is self.getDefaultParameters()[ 'triggerProducer' ].value:
134  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi" )
135  if not hasattr( process, triggerEventProducer ):
136  if triggerEventProducer is self.getDefaultParameters()[ 'triggerEventProducer' ].value:
137  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerEventProducer_cfi" )
138 
139  # Maintain configurations
140  prodSequence = getattr( process, sequence )
141  trigProdMod = getattr( process, triggerProducer )
142  trigProdMod.processName = hltProcess
143  if triggerProducer in _modulesInSequence( process, sequence ):
144  print '%s():'%( self._label )
145  print ' PATTriggerProducer module %s exists already in sequence %s'%( triggerProducer, sequence )
146  print ' ==> entry re-used'
147  if trigProdMod.onlyStandAlone.value() is True:
148  trigProdMod.onlyStandAlone = False
149  print ' configuration parameter automatically changed'
150  print ' PATTriggerProducer %s.onlyStandAlone --> %s'%( triggerProducer, trigProdMod.onlyStandAlone )
151  print _longLine
152  else:
153  # Sequence arithmetics for PATTriggerProducer module
154  if hasattr( process, sequence + 'Trigger' ):
155  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
156  getattr( process, sequence + 'Trigger' ).insert( index, trigProdMod )
157  else:
158  patTriggerSequence = cms.Sequence( trigProdMod )
159  setattr( process, sequence + 'Trigger', patTriggerSequence )
160  prodSequence *= getattr( process, sequence + 'Trigger' )
161  trigEvtProdMod = getattr( process, triggerEventProducer )
162  trigEvtProdMod.processName = hltProcess
163  if hasattr( trigEvtProdMod, 'patTriggerProducer' ):
164  trigEvtProdMod.patTriggerProducer = triggerProducer
165  else:
166  trigEvtProdMod.patTriggerProducer = cms.InputTag( triggerProducer )
167  if triggerEventProducer in _modulesInSequence( process, sequence ):
168  print '%s():'%( self._label )
169  print ' PATTriggerEventProducer module %s exists already in sequence %s'%( triggerEventProducer, sequence )
170  print ' ==> entry re-used'
171  print _longLine
172  else:
173  # Sequence arithmetics for PATTriggerEventProducer module
174  if hasattr( process, sequence + 'TriggerEvent' ):
175  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
176  getattr( process, sequence + 'TriggerEvent' ).insert( index, trigEvtProdMod )
177  else:
178  patTriggerEventSequence = cms.Sequence( trigEvtProdMod )
179  setattr( process, sequence + 'TriggerEvent', patTriggerEventSequence )
180  prodSequence *= getattr( process, sequence + 'TriggerEvent' )
181 
182  # Add event content
183  if outputModule is not '':
184  patTriggerEventContent = [ 'keep patTriggerObjects_%s_*_%s'%( triggerProducer, process.name_() )
185  , 'keep patTriggerFilters_%s_*_%s'%( triggerProducer, process.name_() )
186  , 'keep patTriggerPaths_%s_*_%s'%( triggerProducer, process.name_() )
187  , 'keep patTriggerEvent_%s_*_%s'%( triggerEventProducer, process.name_() )
188  ]
189  if hasattr( trigProdMod, 'addL1Algos' ) and trigProdMod.addL1Algos.value() is True:
190  patTriggerEventContent += [ 'keep patTriggerConditions_%s_*_%s'%( triggerProducer, process.name_() )
191  , 'keep patTriggerAlgorithms_%s_*_%s'%( triggerProducer, process.name_() )
192  ]
193  if hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True:
194  patTriggerEventContent += patTriggerL1RefsEventContent
195  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
196 
197 switchOnTrigger = SwitchOnTrigger()
198 
199 
201  """ Enables trigger information in PAT, limited to stand-alone trigger objects
202  SwitchOnTriggerStandAlone( [cms.Process], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out' )
203  - [cms.Process] : the 'cms.Process'
204  - triggerProducer : PATTriggerProducer module label;
205  optional, default: 'patTrigger'
206  - sequence : name of sequence to use;
207  optional, default: 'patDefaultSequence'
208  - hltProcess : HLT process name;
209  optional, default: 'HLT'
210  - outputModule : output module label;
211  empty label indicates no output;
212  optional, default: 'out'
213  Using None as any argument restores its default value.
214  """
215  _label = 'switchOnTriggerStandAlone'
216  _defaultParameters = dicttypes.SortedKeysDict()
217 
218  def __init__( self ):
219  ConfigToolBase.__init__( self )
220  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
221  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
222  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
223  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
224  self._parameters = copy.deepcopy( self._defaultParameters )
225  self._comment = ""
226 
227  def getDefaultParameters( self ):
228  return self._defaultParameters
229 
230  def __call__( self, process
231  , triggerProducer = None
232  , sequence = None
233  , hltProcess = None
234  , outputModule = None
235  ):
236  if triggerProducer is None:
237  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
238  if sequence is None:
239  sequence = self._defaultParameters[ 'sequence' ].value
240  if hltProcess is None:
241  hltProcess = self._defaultParameters[ 'hltProcess' ].value
242  if outputModule is None:
243  outputModule = self._defaultParameters[ 'outputModule' ].value
244  self.setParameter( 'triggerProducer', triggerProducer )
245  self.setParameter( 'sequence' , sequence )
246  self.setParameter( 'hltProcess' , hltProcess )
247  self.setParameter( 'outputModule' , outputModule )
248  self.apply( process )
249 
250  def toolCode( self, process ):
251  triggerProducer = self._parameters[ 'triggerProducer' ].value
252  sequence = self._parameters[ 'sequence' ].value
253  hltProcess = self._parameters[ 'hltProcess' ].value
254  outputModule = self._parameters[ 'outputModule' ].value
255 
256  # Load default producer from existing config file, if needed
257  if not hasattr( process, triggerProducer ):
258  if triggerProducer is self.getDefaultParameters()[ 'triggerProducer' ].value:
259  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi" )
260 
261  # Maintain configuration
262  prodSequence = getattr( process, sequence )
263  trigProdMod = getattr( process, triggerProducer )
264  trigProdMod.processName = hltProcess
265  if triggerProducer in _modulesInSequence( process, sequence ):
266  print '%s():'%( self._label )
267  print ' PATTriggerProducer module %s exists already in sequence %s'%( triggerProducer, sequence )
268  print ' ==> entry re-used'
269  print _longLine
270  else:
271  # Sequence arithmetics for PATTriggerProducer module
272  if trigProdMod.onlyStandAlone.value() is False:
273  trigProdMod.onlyStandAlone = True
274  print '%s():'%( self._label )
275  print ' configuration parameter automatically changed'
276  print ' PATTriggerProducer %s.onlyStandAlone --> %s'%( triggerProducer, trigProdMod.onlyStandAlone )
277  print _longLine
278  if hasattr( process, sequence + 'Trigger' ):
279  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
280  getattr( process, sequence + 'Trigger' ).insert( index, trigProdMod )
281  else:
282  patTriggerSequence = cms.Sequence( trigProdMod )
283  setattr( process, sequence + 'Trigger', patTriggerSequence )
284  prodSequence *= getattr( process, sequence + 'Trigger' )
285 
286  # Add event content
287  if outputModule is not '':
288  patTriggerEventContent = [ 'keep patTriggerObjectStandAlones_%s_*_%s'%( triggerProducer, process.name_() )
289  ]
290  if hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True:
291  patTriggerEventContent += patTriggerL1RefsEventContent
292  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
293 
294 switchOnTriggerStandAlone = SwitchOnTriggerStandAlone()
295 
296 
298  """ Enables trigger matching in PAT
299  SwitchOnTriggerMatching( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
300  - [cms.Process] : the 'cms.Process'
301  - triggerMatchers : PAT trigger matcher module labels (list)
302  optional; default: defined in 'triggerMatchingDefaultSequence'
303  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
304  - triggerProducer : PATTriggerProducer module label;
305  optional, default: 'patTrigger'
306  - triggerEventProducer: PATTriggerEventProducer module label;
307  optional, default: 'patTriggerEvent'
308  - sequence : name of sequence to use;
309  optional, default: 'patDefaultSequence'
310  - hltProcess : HLT process name;
311  optional, default: 'HLT'
312  - outputModule : output module label;
313  empty label indicates no output;
314  optional, default: 'out'
315  - postfix : postfix to apply to PAT module labels;
316  optional, default: ''
317  Using None as any argument restores its default value.
318  """
319  _label = 'switchOnTriggerMatching'
320  _defaultParameters = dicttypes.SortedKeysDict()
321 
322  def __init__( self ):
323  ConfigToolBase.__init__( self )
324  self.addParameter( self._defaultParameters, 'triggerMatchers' , _defaultTriggerMatchers , _defaultTriggerMatchersComment )
325  self.addParameter( self._defaultParameters, 'triggerProducer' , _defaultTriggerProducer , _defaultTriggerProducerComment )
326  self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
327  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
328  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
329  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
330  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
331  self._parameters = copy.deepcopy( self._defaultParameters )
332  self._comment = ""
333 
334  def getDefaultParameters( self ):
335  return self._defaultParameters
336 
337  def __call__( self, process
338  , triggerMatchers = None
339  , triggerProducer = None
340  , triggerEventProducer = None
341  , sequence = None
342  , hltProcess = None
343  , outputModule = None
344  , postfix = None
345  ):
346  if triggerMatchers is None:
347  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
348  if triggerProducer is None:
349  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
350  if triggerEventProducer is None:
351  triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
352  if sequence is None:
353  sequence = self._defaultParameters[ 'sequence' ].value
354  if hltProcess is None:
355  hltProcess = self._defaultParameters[ 'hltProcess' ].value
356  if outputModule is None:
357  outputModule = self._defaultParameters[ 'outputModule' ].value
358  if postfix is None:
359  postfix = self._defaultParameters[ 'postfix' ].value
360  self.setParameter( 'triggerMatchers' , triggerMatchers )
361  self.setParameter( 'triggerProducer' , triggerProducer )
362  self.setParameter( 'triggerEventProducer', triggerEventProducer )
363  self.setParameter( 'sequence' , sequence )
364  self.setParameter( 'hltProcess' , hltProcess )
365  self.setParameter( 'outputModule' , outputModule )
366  self.setParameter( 'postfix' , postfix )
367  self.apply( process )
368 
369  def toolCode( self, process ):
370  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
371  triggerProducer = self._parameters[ 'triggerProducer' ].value
372  triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
373  sequence = self._parameters[ 'sequence' ].value
374  hltProcess = self._parameters[ 'hltProcess' ].value
375  outputModule = self._parameters[ 'outputModule' ].value
376  postfix = self._parameters[ 'postfix' ].value
377 
378  # Load default producers from existing config file, if needed
379  if not hasattr( process, 'triggerMatchingDefaultSequence' ):
380  for matcher in triggerMatchers:
381  if matcher in self.getDefaultParameters()[ 'triggerMatchers' ].value:
382  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcher_cfi" )
383  break
384 
385  # Switch on PAT trigger information if needed
386  if ( triggerProducer not in _modulesInSequence( process, sequence ) or triggerEventProducer not in _modulesInSequence( process, sequence ) ):
387  print '%s():'%( self._label )
388  print ' PAT trigger production switched on automatically using'
389  print ' switchOnTrigger( process, %s, %s, %s, %s, %s )'%( hltProcess, triggerProducer, triggerEventProducer, sequence, outputModule )
390  print _longLine
391  switchOnTrigger( process, triggerProducer, triggerEventProducer, sequence, hltProcess, outputModule )
392 
393  # Maintain configurations
394  prodSequence = getattr( process, sequence )
395  trigEvtProdMod = getattr( process, triggerEventProducer )
396  if trigEvtProdMod.patTriggerProducer.value() is not triggerProducer:
397  print '%s():'%( self._label )
398  print ' Configuration conflict found'
399  print ' triggerProducer = %s'%( triggerProducer )
400  print ' differs from'
401  print ' %s.patTriggerProducer = %s'%( triggerEventProducer, trigEvtProdMod.patTriggerProducer )
402  print ' parameter automatically changed'
403  print ' ==> triggerProducer --> %s'%( trigEvtProdMod.patTriggerProducer )
404  triggerProducer = trigEvtProdMod.patTriggerProducer
405  for matcher in triggerMatchers:
406  trigMchMod = getattr( process, matcher )
407  trigMchMod.src = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
408  trigMchMod.matched = triggerProducer
409  if matcher in _modulesInSequence( process, sequence ):
410  print '%s():'%( self._label )
411  print ' PAT trigger matcher %s exists already in sequence %s'%( matcher, sequence )
412  print ' ==> entry re-used'
413  print _longLine
414  else:
415  # Sequence arithmetics for PAT trigger matcher modules
416  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
417  getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
418  matchers = getattr( trigEvtProdMod, 'patTriggerMatches' )
419  if len( matchers ) > 0:
420  print '%s():'%( self._label )
421  print ' PAT trigger matchers already attached to existing PATTriggerEventProducer %s'%( triggerEventProducer )
422  print ' configuration parameters automatically changed'
423  for matcher in matchers:
424  trigMchMod = getattr( process, matcher )
425  if trigMchMod.matched.value() is not triggerProducer:
426  removeIfInSequence( process, matcher, sequence + 'Trigger' )
427  trigMchMod.matched = triggerProducer
428  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
429  getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
430  print ' PAT trigger matcher %s.matched --> %s'%( matcher, trigMchMod.matched )
431  print _longLine
432  else:
433  trigEvtProdMod.patTriggerMatches = cms.VInputTag()
434  for matcher in triggerMatchers:
435  trigEvtProdMod.patTriggerMatches.append( cms.InputTag( matcher ) )
436 
437  # Add event content
438  if outputModule is not '':
439  patTriggerEventContent = []
440  for matcher in triggerMatchers:
441  patTriggerEventContent += [ 'keep patTriggerObjectsedmAssociation_%s_%s_%s'%( triggerEventProducer, matcher, process.name_() )
442  , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
443  ]
444  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
445 
446 switchOnTriggerMatching = SwitchOnTriggerMatching()
447 
448 
450  """ Enables trigger matching in PAT
451  SwitchOnTriggerMatchingStandAlone( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
452  - [cms.Process] : the 'cms.Process'
453  - triggerMatchers: PAT trigger matcher module labels (list)
454  optional; default: defined in 'triggerMatchingDefaultSequence'
455  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
456  - triggerProducer: PATTriggerProducer module label;
457  optional, default: 'patTrigger'
458  - sequence : name of sequence to use;
459  optional, default: 'patDefaultSequence'
460  - hltProcess : HLT process name;
461  optional, default: 'HLT'
462  - outputModule : output module label;
463  empty label indicates no output;
464  optional, default: 'out'
465  - postfix : postfix to apply to PAT module labels;
466  optional, default: ''
467  Using None as any argument restores its default value.
468  """
469  _label = 'switchOnTriggerMatchingStandAlone'
470  _defaultParameters = dicttypes.SortedKeysDict()
471 
472  def __init__( self ):
473  ConfigToolBase.__init__( self )
474  self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
475  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
476  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
477  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
478  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
479  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
480  self._parameters = copy.deepcopy( self._defaultParameters )
481  self._comment = ""
482 
483  def getDefaultParameters( self ):
484  return self._defaultParameters
485 
486  def __call__( self, process
487  , triggerMatchers = None
488  , triggerProducer = None
489  , sequence = None
490  , hltProcess = None
491  , outputModule = None
492  , postfix = None
493  ):
494  if triggerMatchers is None:
495  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
496  if triggerProducer is None:
497  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
498  if sequence is None:
499  sequence = self._defaultParameters[ 'sequence' ].value
500  if hltProcess is None:
501  hltProcess = self._defaultParameters[ 'hltProcess' ].value
502  if outputModule is None:
503  outputModule = self._defaultParameters[ 'outputModule' ].value
504  if postfix is None:
505  postfix = self._defaultParameters[ 'postfix' ].value
506  self.setParameter( 'triggerMatchers', triggerMatchers )
507  self.setParameter( 'triggerProducer', triggerProducer )
508  self.setParameter( 'sequence' , sequence )
509  self.setParameter( 'hltProcess' , hltProcess )
510  self.setParameter( 'outputModule' , outputModule )
511  self.setParameter( 'postfix' , postfix )
512  self.apply( process )
513 
514  def toolCode( self, process ):
515  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
516  triggerProducer = self._parameters[ 'triggerProducer' ].value
517  sequence = self._parameters[ 'sequence' ].value
518  hltProcess = self._parameters[ 'hltProcess' ].value
519  outputModule = self._parameters[ 'outputModule' ].value
520  postfix = self._parameters[ 'postfix' ].value
521 
522  # Load default producers from existing config file, if needed
523  if not hasattr( process, 'triggerMatchingDefaultSequence' ):
524  for matcher in triggerMatchers:
525  if matcher in self.getDefaultParameters()[ 'triggerMatchers' ].value:
526  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcher_cfi" )
527  break
528 
529  # Switch on PAT trigger information if needed
530  if triggerProducer not in _modulesInSequence( process, sequence ):
531  print '%s():'%( self._label )
532  print ' PAT trigger production switched on automatically using'
533  print ' switchOnTriggerStandAlone( process, %s, %s, %s, %s )'%( hltProcess, triggerProducer, sequence, outputModule )
534  print _longLine
535  switchOnTriggerStandAlone( process, triggerProducer, sequence, hltProcess, outputModule )
536 
537  # Maintain configurations
538  for matcher in triggerMatchers:
539  trigMchMod = getattr( process, matcher )
540  trigMchMod.src = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
541  trigMchMod.matched = triggerProducer
542  if matcher in _modulesInSequence( process, sequence ):
543  print '%s():'%( self._label )
544  print ' PAT trigger matcher %s exists already in sequence %s'%( matcher, sequence )
545  print ' ==> entry re-used'
546  print _longLine
547  else:
548  # Sequence arithmetics for PAT trigger matcher modules
549  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
550  getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
551 
552  # Add event content
553  if outputModule is not '':
554  patTriggerEventContent = []
555  for matcher in triggerMatchers:
556  patTriggerEventContent += [ 'keep patTriggerObjectStandAlonesedmAssociation_%s_*_%s'%( matcher, process.name_() )
557  , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
558  ]
559  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
560 
561 switchOnTriggerMatchingStandAlone = SwitchOnTriggerMatchingStandAlone()
562 
563 
565  """ Enables embedding of trigger matches into PAT objects
566  SwitchOnTriggerMatchEmbedding( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
567  - [cms.Process] : the 'cms.Process'
568  - triggerMatchers: PAT trigger matcher module labels (list)
569  optional; default: defined in 'triggerMatchingDefaultSequence'
570  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
571  - triggerProducer: PATTriggerProducer module label;
572  optional, default: 'patTrigger'
573  - sequence : name of sequence to use;
574  optional, default: 'patDefaultSequence'
575  - hltProcess : HLT process name;
576  optional, default: 'HLT'
577  - outputModule : output module label;
578  empty label indicates no output;
579  optional, default: 'out'
580  - postfix : postfix to apply to PAT module labels;
581  optional, default: ''
582  Using None as any argument restores its default value.
583  """
584  _label = 'switchOnTriggerMatchEmbedding'
585  _defaultParameters = dicttypes.SortedKeysDict()
586 
587  def __init__( self ):
588  ConfigToolBase.__init__( self )
589  self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
590  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
591  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
592  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
593  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
594  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
595  self._parameters = copy.deepcopy( self._defaultParameters )
596  self._comment = ""
597 
598  def getDefaultParameters( self ):
599  return self._defaultParameters
600 
601  def __call__( self, process
602  , triggerMatchers = None
603  , triggerProducer = None
604  , sequence = None
605  , hltProcess = None
606  , outputModule = None
607  , postfix = None
608  ):
609  if triggerMatchers is None:
610  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
611  if triggerProducer is None:
612  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
613  if sequence is None:
614  sequence = self._defaultParameters[ 'sequence' ].value
615  if hltProcess is None:
616  hltProcess = self._defaultParameters[ 'hltProcess' ].value
617  if outputModule is None:
618  outputModule = self._defaultParameters[ 'outputModule' ].value
619  if postfix is None:
620  postfix = self._defaultParameters[ 'postfix' ].value
621  self.setParameter( 'triggerMatchers', triggerMatchers )
622  self.setParameter( 'triggerProducer', triggerProducer )
623  self.setParameter( 'sequence' , sequence )
624  self.setParameter( 'hltProcess' , hltProcess )
625  self.setParameter( 'outputModule' , outputModule )
626  self.setParameter( 'postfix' , postfix )
627  self.apply( process )
628 
629  def toolCode( self, process ):
630  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
631  triggerProducer = self._parameters[ 'triggerProducer' ].value
632  sequence = self._parameters[ 'sequence' ].value
633  hltProcess = self._parameters[ 'hltProcess' ].value
634  outputModule = self._parameters[ 'outputModule' ].value
635  postfix = self._parameters[ 'postfix' ].value
636 
637  # Build dictionary of known input collections
638  dictPatObjects = { 'Photons' : 'PATTriggerMatchPhotonEmbedder'
639  , 'Electrons': 'PATTriggerMatchElectronEmbedder'
640  , 'Muons' : 'PATTriggerMatchMuonEmbedder'
641  , 'Taus' : 'PATTriggerMatchTauEmbedder'
642  , 'Jets' : 'PATTriggerMatchJetEmbedder'
643  , 'METs' : 'PATTriggerMatchMETEmbedder'
644  }
645  listPatSteps = [ 'pat', 'selectedPat', 'cleanPat' ]
646  listJetAlgos = [ 'IC5', 'SC5', 'KT4', 'KT6', 'AK5' ]
647  listJetTypes = [ 'Calo', 'PF', 'JPT' ]
648  dictEmbedders = {}
649  for objects in dictPatObjects.keys():
650  steps = len( listPatSteps )
651  if objects is 'METs':
652  steps = 1
653  for step in range( steps ):
654  coll = listPatSteps[ step ] + objects
655  dictEmbedders[ coll ] = dictPatObjects[ objects ]
656  dictEmbedders[ coll + postfix ] = dictPatObjects[ objects ]
657  if objects is 'Jets':
658  for jetAlgo in listJetAlgos:
659  for jetType in listJetTypes:
660  jetColl = coll + jetAlgo + jetType
661  dictEmbedders[ jetColl ] = dictPatObjects[ objects ]
662  dictEmbedders[ jetColl + postfix ] = dictPatObjects[ objects ]
663 
664  # Build dictionary of matchers and switch on PAT trigger matching if needed
665  dictConfig = {}
666  matchingOn = False
667  for matcher in triggerMatchers:
668  trigMchMod = getattr( process, matcher )
669  if trigMchMod.src.value() in dictConfig:
670  dictConfig[ trigMchMod.src.value() ] += [ matcher ]
671  else:
672  dictConfig[ trigMchMod.src.value() ] = [ matcher ]
673  if matcher not in _modulesInSequence( process, sequence ) and not matchingOn:
674  print '%s():'%( self._label )
675  print ' PAT trigger matching switched on automatically using'
676  print ' switchOnTriggerMatchingStandAlone( process, %s, %s, %s, %s, %s )'%( hltProcess, triggerMatchers, triggerProducer, sequence, outputModule )
677  print _longLine
678  switchOnTriggerMatchingStandAlone( process, triggerMatchers, triggerProducer, sequence, hltProcess, '', postfix )
679  matchingOn = True
680 
681  # Maintain configurations
682  patTriggerEventContent = []
683  for srcInput in dictConfig.keys():
684  if dictEmbedders.has_key( srcInput ):
685  # Configure embedder module
686  dictIndex = srcInput
687  srcInput += postfix
688  if dictEmbedders.has_key( srcInput ):
689  label = srcInput + 'TriggerMatch'
690  if label in _modulesInSequence( process, sequence ):
691  print '%s():'%( self._label )
692  print ' PAT trigger match embedder %s exists already in sequence %s'%( label, sequence )
693  print ' ==> entry moved to proper place'
694  print _longLine
695  removeIfInSequence( process, label, sequence + 'Trigger' )
696  module = cms.EDProducer( dictEmbedders[ dictIndex ] )
697  module.src = cms.InputTag( srcInput )
698  module.matches = cms.VInputTag( dictConfig[ dictIndex ] )
699  setattr( process, label, module )
700  trigEmbMod = getattr( process, label )
701  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
702  getattr( process, sequence + 'Trigger' ).insert( index, trigEmbMod )
703  # Add event content
704  patTriggerEventContent += [ 'drop *_%s_*_*'%( srcInput )
705  , 'keep *_%s_*_%s'%( label, process.name_() )
706  ]
707  else:
708  print '%s():'%( self._label )
709  print ' Invalid new input source for trigger match embedding'
710  print ' ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ dictIndex ] )
711  print _longLine
712  else:
713  print '%s():'%( self._label )
714  print ' Invalid input source for trigger match embedding'
715  print ' ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ srcInput ] )
716  print _longLine
717  if outputModule is not '':
718  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
719 
720 switchOnTriggerMatchEmbedding = SwitchOnTriggerMatchEmbedding()
721 
722 
724  """ Removes cleaning from already existing PAT trigger matching/embedding configuration
725  RemoveCleaningFromTriggerMatching( [cms.Process], outputModule = 'out' )
726  - [cms.Process] : the 'cms.Process'
727  - sequence : name of sequence to use;
728  optional, default: 'patDefaultSequence'
729  - outputModule : output module label;
730  empty label indicates no output;
731  optional, default: 'out'
732  Using None as any argument restores its default value.
733  """
734  _label = 'removeCleaningFromTriggerMatching'
735  _defaultParameters = dicttypes.SortedKeysDict()
736 
737  def __init__( self ):
738  ConfigToolBase.__init__( self )
739  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
740  self.addParameter( self._defaultParameters, 'outputModule', _defaultOutputModule, _defaultOutputModuleComment )
741  self._parameters = copy.deepcopy( self._defaultParameters )
742  self._comment = ""
743 
744  def getDefaultParameters( self ):
745  return self._defaultParameters
746 
747  def __call__( self, process
748  , sequence = None
749  , outputModule = None
750  ):
751  if sequence is None:
752  sequence = self._defaultParameters[ 'sequence' ].value
753  if outputModule is None:
754  outputModule = self._defaultParameters[ 'outputModule' ].value
755  self.setParameter( 'sequence' , sequence )
756  self.setParameter( 'outputModule', outputModule )
757  self.apply( process )
758 
759  def toolCode( self, process ):
760  sequence = self._parameters[ 'sequence' ].value
761  outputModule = self._parameters[ 'outputModule' ].value
762 
763  # Maintain configurations
764  listMatchers = [ 'PATTriggerMatcherDRLessByR'
765  , 'PATTriggerMatcherDRDPtLessByR'
766  , 'PATTriggerMatcherDRLessByPt'
767  , 'PATTriggerMatcherDRDPtLessByPt'
768  , 'PATTriggerMatcherDEtaLessByDR'
769  , 'PATTriggerMatcherDEtaLessByDEta'
770  ]
771  listEmbedders = [ 'PATTriggerMatchPhotonEmbedder'
772  , 'PATTriggerMatchElectronEmbedder'
773  , 'PATTriggerMatchMuonEmbedder'
774  , 'PATTriggerMatchTauEmbedder'
775  , 'PATTriggerMatchJetEmbedder'
776  , 'PATTriggerMatchMETEmbedder'
777  ]
778  modules = _modulesInSequence( process, sequence )
779  oldModules = []
780  oldSources = []
781  # input source labels
782  for module in modules:
783  if hasattr( process, module ):
784  trigMod = getattr( process, module )
785  if trigMod.type_() in listMatchers:
786  if trigMod.src.value()[ : 8 ] == 'cleanPat':
787  trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
788  if trigMod.label()[ : 5 ] == 'clean':
789  oldModules += [ trigMod.label() ]
790  setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
791  if trigMod.type_() in listEmbedders:
792  if trigMod.src.value()[ : 8 ] == 'cleanPat':
793  oldSources += [ trigMod.src.getModuleLabel() ]
794  trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
795  if trigMod.label()[ : 5 ] == 'clean':
796  oldModules += [ trigMod.label() ]
797  setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
798  # matcher labels
799  for module in modules:
800  if hasattr( process, module ):
801  trigMod = getattr( process, module )
802  if trigMod.type_() == 'PATTriggerEventProducer':
803  matchers = getattr( trigMod, 'patTriggerMatches' )
804  matchers = self._renameMatchers( matchers, oldModules )
805  elif trigMod.type_() in listEmbedders:
806  matchers = getattr( trigMod, 'matches' )
807  matchers = self._renameMatchers( matchers, oldModules )
808 
809  # Maintain event content
810  if outputModule is not '':
811  patTriggerEventContent = getattr( process, outputModule ).outputCommands
812  for statement in range( len( patTriggerEventContent ) ):
813  for module in oldModules:
814  if module in patTriggerEventContent[ statement ]:
815  patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
816  for source in oldSources:
817  if source in patTriggerEventContent[ statement ] and 'drop' in patTriggerEventContent[ statement ]:
818  patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
819  print '%s():'%( self._label )
820  print ' Input from cleaning has been switched to input from selection;'
821  print ' matcher and embedder modules have been renamed accordingly.'
822  print _longLine
823 
824  def _renameMatchers( self, matchers, oldModules ):
825  for matcher in range( len( matchers ) ):
826  if matchers[ matcher ] in oldModules:
827  if matchers[ matcher ][ : 5 ] == 'clean':
828  matchers[ matcher ] = matchers[ matcher ].replace( 'clean', 'selected' )
829  return matchers
830 
831 removeCleaningFromTriggerMatching = RemoveCleaningFromTriggerMatching()
def listModules
Definition: helpers.py:208
tuple switchOnTriggerMatchingStandAlone
Definition: trigTools.py:561
def replace
Definition: linker.py:10
tuple switchOnTriggerStandAlone
Definition: trigTools.py:294
def _modulesInSequence
Definition: trigTools.py:34
def _addEventContent
Definition: trigTools.py:38
tuple switchOnTrigger
Definition: trigTools.py:197
def removeIfInSequence
Definition: helpers.py:20
bool insert(Storage &, ItemType *, const IdTag &)