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 
67 class SwitchOnTrigger( ConfigToolBase ):
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 
200 class SwitchOnTriggerStandAlone( ConfigToolBase ):
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 
297 class SwitchOnTriggerMatching( ConfigToolBase ):
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  trigEvtProdMod.patTriggerMatches += triggerMatchers
435 
436  # Add event content
437  if outputModule is not '':
438  patTriggerEventContent = []
439  for matcher in triggerMatchers:
440  patTriggerEventContent += [ 'keep patTriggerObjectsedmAssociation_%s_%s_%s'%( triggerEventProducer, matcher, process.name_() )
441  , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
442  ]
443  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
444 
445 switchOnTriggerMatching = SwitchOnTriggerMatching()
446 
447 
448 class SwitchOnTriggerMatchingStandAlone( ConfigToolBase ):
449  """ Enables trigger matching in PAT
450  SwitchOnTriggerMatchingStandAlone( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
451  - [cms.Process] : the 'cms.Process'
452  - triggerMatchers: PAT trigger matcher module labels (list)
453  optional; default: defined in 'triggerMatchingDefaultSequence'
454  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
455  - triggerProducer: PATTriggerProducer module label;
456  optional, default: 'patTrigger'
457  - sequence : name of sequence to use;
458  optional, default: 'patDefaultSequence'
459  - hltProcess : HLT process name;
460  optional, default: 'HLT'
461  - outputModule : output module label;
462  empty label indicates no output;
463  optional, default: 'out'
464  - postfix : postfix to apply to PAT module labels;
465  optional, default: ''
466  Using None as any argument restores its default value.
467  """
468  _label = 'switchOnTriggerMatchingStandAlone'
469  _defaultParameters = dicttypes.SortedKeysDict()
470 
471  def __init__( self ):
472  ConfigToolBase.__init__( self )
473  self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
474  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
475  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
476  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
477  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
478  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
479  self._parameters = copy.deepcopy( self._defaultParameters )
480  self._comment = ""
481 
482  def getDefaultParameters( self ):
483  return self._defaultParameters
484 
485  def __call__( self, process
486  , triggerMatchers = None
487  , triggerProducer = None
488  , sequence = None
489  , hltProcess = None
490  , outputModule = None
491  , postfix = None
492  ):
493  if triggerMatchers is None:
494  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
495  if triggerProducer is None:
496  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
497  if sequence is None:
498  sequence = self._defaultParameters[ 'sequence' ].value
499  if hltProcess is None:
500  hltProcess = self._defaultParameters[ 'hltProcess' ].value
501  if outputModule is None:
502  outputModule = self._defaultParameters[ 'outputModule' ].value
503  if postfix is None:
504  postfix = self._defaultParameters[ 'postfix' ].value
505  self.setParameter( 'triggerMatchers', triggerMatchers )
506  self.setParameter( 'triggerProducer', triggerProducer )
507  self.setParameter( 'sequence' , sequence )
508  self.setParameter( 'hltProcess' , hltProcess )
509  self.setParameter( 'outputModule' , outputModule )
510  self.setParameter( 'postfix' , postfix )
511  self.apply( process )
512 
513  def toolCode( self, process ):
514  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
515  triggerProducer = self._parameters[ 'triggerProducer' ].value
516  sequence = self._parameters[ 'sequence' ].value
517  hltProcess = self._parameters[ 'hltProcess' ].value
518  outputModule = self._parameters[ 'outputModule' ].value
519  postfix = self._parameters[ 'postfix' ].value
520 
521  # Load default producers from existing config file, if needed
522  if not hasattr( process, 'triggerMatchingDefaultSequence' ):
523  for matcher in triggerMatchers:
524  if matcher in self.getDefaultParameters()[ 'triggerMatchers' ].value:
525  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcher_cfi" )
526  break
527 
528  # Switch on PAT trigger information if needed
529  if triggerProducer not in _modulesInSequence( process, sequence ):
530  print '%s():'%( self._label )
531  print ' PAT trigger production switched on automatically using'
532  print ' switchOnTriggerStandAlone( process, %s, %s, %s, %s )'%( hltProcess, triggerProducer, sequence, outputModule )
533  print _longLine
534  switchOnTriggerStandAlone( process, triggerProducer, sequence, hltProcess, outputModule )
535 
536  # Maintain configurations
537  for matcher in triggerMatchers:
538  trigMchMod = getattr( process, matcher )
539  trigMchMod.src = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
540  trigMchMod.matched = triggerProducer
541  if matcher in _modulesInSequence( process, sequence ):
542  print '%s():'%( self._label )
543  print ' PAT trigger matcher %s exists already in sequence %s'%( matcher, sequence )
544  print ' ==> entry re-used'
545  print _longLine
546  else:
547  # Sequence arithmetics for PAT trigger matcher modules
548  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
549  getattr( process, sequence + 'Trigger' ).insert( index, trigMchMod )
550 
551  # Add event content
552  if outputModule is not '':
553  patTriggerEventContent = []
554  for matcher in triggerMatchers:
555  patTriggerEventContent += [ 'keep patTriggerObjectStandAlonesedmAssociation_%s_*_%s'%( matcher, process.name_() )
556  , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
557  ]
558  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
559 
560 switchOnTriggerMatchingStandAlone = SwitchOnTriggerMatchingStandAlone()
561 
562 
563 class SwitchOnTriggerMatchEmbedding( ConfigToolBase ):
564  """ Enables embedding of trigger matches into PAT objects
565  SwitchOnTriggerMatchEmbedding( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', sequence = 'patDefaultSequence', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
566  - [cms.Process] : the 'cms.Process'
567  - triggerMatchers: PAT trigger matcher module labels (list)
568  optional; default: defined in 'triggerMatchingDefaultSequence'
569  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcher_cfi.py)
570  - triggerProducer: PATTriggerProducer module label;
571  optional, default: 'patTrigger'
572  - sequence : name of sequence to use;
573  optional, default: 'patDefaultSequence'
574  - hltProcess : HLT process name;
575  optional, default: 'HLT'
576  - outputModule : output module label;
577  empty label indicates no output;
578  optional, default: 'out'
579  - postfix : postfix to apply to PAT module labels;
580  optional, default: ''
581  Using None as any argument restores its default value.
582  """
583  _label = 'switchOnTriggerMatchEmbedding'
584  _defaultParameters = dicttypes.SortedKeysDict()
585 
586  def __init__( self ):
587  ConfigToolBase.__init__( self )
588  self.addParameter( self._defaultParameters, 'triggerMatchers', _defaultTriggerMatchers, _defaultTriggerMatchersComment )
589  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
590  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
591  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
592  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
593  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
594  self._parameters = copy.deepcopy( self._defaultParameters )
595  self._comment = ""
596 
597  def getDefaultParameters( self ):
598  return self._defaultParameters
599 
600  def __call__( self, process
601  , triggerMatchers = None
602  , triggerProducer = None
603  , sequence = None
604  , hltProcess = None
605  , outputModule = None
606  , postfix = None
607  ):
608  if triggerMatchers is None:
609  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
610  if triggerProducer is None:
611  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
612  if sequence is None:
613  sequence = self._defaultParameters[ 'sequence' ].value
614  if hltProcess is None:
615  hltProcess = self._defaultParameters[ 'hltProcess' ].value
616  if outputModule is None:
617  outputModule = self._defaultParameters[ 'outputModule' ].value
618  if postfix is None:
619  postfix = self._defaultParameters[ 'postfix' ].value
620  self.setParameter( 'triggerMatchers', triggerMatchers )
621  self.setParameter( 'triggerProducer', triggerProducer )
622  self.setParameter( 'sequence' , sequence )
623  self.setParameter( 'hltProcess' , hltProcess )
624  self.setParameter( 'outputModule' , outputModule )
625  self.setParameter( 'postfix' , postfix )
626  self.apply( process )
627 
628  def toolCode( self, process ):
629  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
630  triggerProducer = self._parameters[ 'triggerProducer' ].value
631  sequence = self._parameters[ 'sequence' ].value
632  hltProcess = self._parameters[ 'hltProcess' ].value
633  outputModule = self._parameters[ 'outputModule' ].value
634  postfix = self._parameters[ 'postfix' ].value
635 
636  # Build dictionary of known input collections
637  dictPatObjects = { 'Photons' : 'PATTriggerMatchPhotonEmbedder'
638  , 'Electrons': 'PATTriggerMatchElectronEmbedder'
639  , 'Muons' : 'PATTriggerMatchMuonEmbedder'
640  , 'Taus' : 'PATTriggerMatchTauEmbedder'
641  , 'Jets' : 'PATTriggerMatchJetEmbedder'
642  , 'METs' : 'PATTriggerMatchMETEmbedder'
643  }
644  listPatSteps = [ 'pat', 'selectedPat', 'cleanPat' ]
645  listJetAlgos = [ 'IC5', 'SC5', 'KT4', 'KT6', 'AK5' ]
646  listJetTypes = [ 'Calo', 'PF', 'JPT' ]
647  dictEmbedders = {}
648  for objects in dictPatObjects.keys():
649  steps = len( listPatSteps )
650  if objects is 'METs':
651  steps = 1
652  for step in range( steps ):
653  coll = listPatSteps[ step ] + objects
654  dictEmbedders[ coll ] = dictPatObjects[ objects ]
655  dictEmbedders[ coll + postfix ] = dictPatObjects[ objects ]
656  if objects is 'Jets':
657  for jetAlgo in listJetAlgos:
658  for jetType in listJetTypes:
659  jetColl = coll + jetAlgo + jetType
660  dictEmbedders[ jetColl ] = dictPatObjects[ objects ]
661  dictEmbedders[ jetColl + postfix ] = dictPatObjects[ objects ]
662 
663  # Build dictionary of matchers and switch on PAT trigger matching if needed
664  dictConfig = {}
665  matchingOn = False
666  for matcher in triggerMatchers:
667  trigMchMod = getattr( process, matcher )
668  if trigMchMod.src.value() in dictConfig:
669  dictConfig[ trigMchMod.src.value() ] += [ matcher ]
670  else:
671  dictConfig[ trigMchMod.src.value() ] = [ matcher ]
672  if matcher not in _modulesInSequence( process, sequence ) and not matchingOn:
673  print '%s():'%( self._label )
674  print ' PAT trigger matching switched on automatically using'
675  print ' switchOnTriggerMatchingStandAlone( process, %s, %s, %s, %s, %s )'%( hltProcess, triggerMatchers, triggerProducer, sequence, outputModule )
676  print _longLine
677  switchOnTriggerMatchingStandAlone( process, triggerMatchers, triggerProducer, sequence, hltProcess, '', postfix )
678  matchingOn = True
679 
680  # Maintain configurations
681  patTriggerEventContent = []
682  for srcInput in dictConfig.keys():
683  if dictEmbedders.has_key( srcInput ):
684  # Configure embedder module
685  dictIndex = srcInput
686  srcInput += postfix
687  if dictEmbedders.has_key( srcInput ):
688  label = srcInput + 'TriggerMatch'
689  if label in _modulesInSequence( process, sequence ):
690  print '%s():'%( self._label )
691  print ' PAT trigger match embedder %s exists already in sequence %s'%( label, sequence )
692  print ' ==> entry moved to proper place'
693  print _longLine
694  removeIfInSequence( process, label, sequence + 'Trigger' )
695  module = cms.EDProducer( dictEmbedders[ dictIndex ] )
696  module.src = cms.InputTag( srcInput )
697  module.matches = cms.VInputTag( dictConfig[ dictIndex ] )
698  setattr( process, label, module )
699  trigEmbMod = getattr( process, label )
700  index = len( getattr( process, sequence + 'Trigger' ).moduleNames() )
701  getattr( process, sequence + 'Trigger' ).insert( index, trigEmbMod )
702  # Add event content
703  patTriggerEventContent += [ 'drop *_%s_*_*'%( srcInput )
704  , 'keep *_%s_*_%s'%( label, process.name_() )
705  ]
706  else:
707  print '%s():'%( self._label )
708  print ' Invalid new input source for trigger match embedding'
709  print ' ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ dictIndex ] )
710  print _longLine
711  else:
712  print '%s():'%( self._label )
713  print ' Invalid input source for trigger match embedding'
714  print ' ==> %s with matchers %s is skipped'%( srcInput, dictConfig[ srcInput ] )
715  print _longLine
716  if outputModule is not '':
717  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
718 
719 switchOnTriggerMatchEmbedding = SwitchOnTriggerMatchEmbedding()
720 
721 
722 class RemoveCleaningFromTriggerMatching( ConfigToolBase ):
723  """ Removes cleaning from already existing PAT trigger matching/embedding configuration
724  RemoveCleaningFromTriggerMatching( [cms.Process], outputModule = 'out' )
725  - [cms.Process] : the 'cms.Process'
726  - sequence : name of sequence to use;
727  optional, default: 'patDefaultSequence'
728  - outputModule : output module label;
729  empty label indicates no output;
730  optional, default: 'out'
731  Using None as any argument restores its default value.
732  """
733  _label = 'removeCleaningFromTriggerMatching'
734  _defaultParameters = dicttypes.SortedKeysDict()
735 
736  def __init__( self ):
737  ConfigToolBase.__init__( self )
738  self.addParameter( self._defaultParameters, 'sequence' , _defaultSequence , _defaultSequenceComment )
739  self.addParameter( self._defaultParameters, 'outputModule', _defaultOutputModule, _defaultOutputModuleComment )
740  self._parameters = copy.deepcopy( self._defaultParameters )
741  self._comment = ""
742 
743  def getDefaultParameters( self ):
744  return self._defaultParameters
745 
746  def __call__( self, process
747  , sequence = None
748  , outputModule = None
749  ):
750  if sequence is None:
751  sequence = self._defaultParameters[ 'sequence' ].value
752  if outputModule is None:
753  outputModule = self._defaultParameters[ 'outputModule' ].value
754  self.setParameter( 'sequence' , sequence )
755  self.setParameter( 'outputModule', outputModule )
756  self.apply( process )
757 
758  def toolCode( self, process ):
759  sequence = self._parameters[ 'sequence' ].value
760  outputModule = self._parameters[ 'outputModule' ].value
761 
762  # Maintain configurations
763  listMatchers = [ 'PATTriggerMatcherDRLessByR'
764  , 'PATTriggerMatcherDRDPtLessByR'
765  , 'PATTriggerMatcherDRLessByPt'
766  , 'PATTriggerMatcherDRDPtLessByPt'
767  , 'PATTriggerMatcherDEtaLessByDR'
768  , 'PATTriggerMatcherDEtaLessByDEta'
769  ]
770  listEmbedders = [ 'PATTriggerMatchPhotonEmbedder'
771  , 'PATTriggerMatchElectronEmbedder'
772  , 'PATTriggerMatchMuonEmbedder'
773  , 'PATTriggerMatchTauEmbedder'
774  , 'PATTriggerMatchJetEmbedder'
775  , 'PATTriggerMatchMETEmbedder'
776  ]
777  modules = _modulesInSequence( process, sequence )
778  oldModules = []
779  oldSources = []
780  # input source labels
781  for module in modules:
782  if hasattr( process, module ):
783  trigMod = getattr( process, module )
784  if trigMod.type_() in listMatchers:
785  if trigMod.src.value()[ : 8 ] == 'cleanPat':
786  trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
787  if trigMod.label()[ : 5 ] == 'clean':
788  oldModules += [ trigMod.label() ]
789  setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
790  if trigMod.type_() in listEmbedders:
791  if trigMod.src.value()[ : 8 ] == 'cleanPat':
792  oldSources += [ trigMod.src.getModuleLabel() ]
793  trigMod.src = trigMod.src.value().replace( 'cleanPat', 'selectedPat' )
794  if trigMod.label()[ : 5 ] == 'clean':
795  oldModules += [ trigMod.label() ]
796  setattr( process, trigMod.label().replace( 'clean', 'selected' ), trigMod )
797  # matcher labels
798  for module in modules:
799  if hasattr( process, module ):
800  trigMod = getattr( process, module )
801  if trigMod.type_() == 'PATTriggerEventProducer':
802  matchers = getattr( trigMod, 'patTriggerMatches' )
803  matchers = self._renameMatchers( matchers, oldModules )
804  elif trigMod.type_() in listEmbedders:
805  matchers = getattr( trigMod, 'matches' )
806  matchers = self._renameMatchers( matchers, oldModules )
807 
808  # Maintain event content
809  if outputModule is not '':
810  patTriggerEventContent = getattr( process, outputModule ).outputCommands
811  for statement in range( len( patTriggerEventContent ) ):
812  for module in oldModules:
813  if module in patTriggerEventContent[ statement ]:
814  patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
815  for source in oldSources:
816  if source in patTriggerEventContent[ statement ] and 'drop' in patTriggerEventContent[ statement ]:
817  patTriggerEventContent[ statement ] = patTriggerEventContent[ statement ].replace( 'clean', 'selected' )
818  print '%s():'%( self._label )
819  print ' Input from cleaning has been switched to input from selection;'
820  print ' matcher and embedder modules have been renamed accordingly.'
821  print _longLine
822 
823  def _renameMatchers( self, matchers, oldModules ):
824  for matcher in range( len( matchers ) ):
825  if matchers[ matcher ] in oldModules:
826  if matchers[ matcher ][ : 5 ] == 'clean':
827  matchers[ matcher ] = matchers[ matcher ].replace( 'clean', 'selected' )
828  return matchers
829 
830 removeCleaningFromTriggerMatching = RemoveCleaningFromTriggerMatching()
def listModules
Definition: helpers.py:203
tuple switchOnTriggerMatchingStandAlone
Definition: trigTools.py:560
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 &)