CMS 3D CMS Logo

trigTools.py
Go to the documentation of this file.
2 
4 from PhysicsTools.PatAlgos.patEventContent_cff import patTriggerL1RefsEventContent
5 
6 from PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi import _exampleTriggerMatchers
7 _defaultTriggerProducer = 'patTrigger'
8 _defaultTriggerEventProducer = 'patTriggerEvent'
9 _defaultPath = ''
10 _defaultHltProcess = 'HLT'
11 _defaultOutputModule = 'out'
12 _defaultPostfix = ''
13 
14 _defaultTriggerMatchersComment = "Trigger matcher modules' labels, default: ..."
15 _defaultTriggerProducerComment = "PATTriggerProducer module label, default: %s"%( _defaultTriggerProducer )
16 _defaultTriggerEventProducerComment = "PATTriggerEventProducer module label, default: %s"%( _defaultTriggerEventProducer )
17 _defaultPathComment = "Name of path to use, default: %s"%( _defaultPath )
18 _defaultHltProcessComment = "HLT process name, default: %s"%( _defaultHltProcess )
19 _defaultOutputModuleComment = "Output module label, empty label indicates no output, default: %s"%( _defaultOutputModule )
20 _defaultPostfixComment = "Postfix to apply to PAT module labels, default: %s"%( _defaultPostfix )
21 
22 _longLine = '---------------------------------------------------------------------'
23 
24 
25 def _modulesInPath( process, pathLabel ):
26  return [ m.label() for m in listModules( getattr( process, pathLabel ) ) ]
27 
28 
29 def _addEventContent( outputCommands, eventContent ):
30  # add new entry to event content
31  for content in eventContent:
32  if content not in outputCommands:
33  outputCommands += [ content ]
34  # check for obsolete entries
35  listToRemove = []
36  for i in range( len( outputCommands ) ):
37  if i in listToRemove:
38  continue
39  command = outputCommands[ i ]
40  if command[ : 4 ] == 'keep':
41  dropCommand = command.replace( 'keep ', 'drop ' )
42  for j in range( i + 1, len( outputCommands ) ):
43  testCommand = outputCommands[ j ]
44  if testCommand == command:
45  listToRemove += [ j ]
46  elif testCommand == dropCommand:
47  listToRemove += [ i, j ]
48  break
49  # copy entries excl. obsolete ones
50  newOutputCommands = cms.untracked.vstring()
51  for i in range( len( outputCommands ) ):
52  if i not in listToRemove:
53  newOutputCommands += [ outputCommands[ i ] ]
54  # return result
55  return newOutputCommands
56 
57 
59  """ Enables trigger information in PAT
60  SwitchOnTrigger( [cms.Process], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', path = '', hltProcess = 'HLT', outputModule = 'out' )
61  - [cms.Process] : the 'cms.Process'
62  - triggerProducer : PATTriggerProducer module label;
63  optional, default: 'patTrigger'
64  - triggerEventProducer: PATTriggerEventProducer module label;
65  optional, default: 'patTriggerEvent'
66  - path : name of path to use;
67  optional, default: ''
68  - hltProcess : HLT process name;
69  optional, default: 'HLT'
70  - outputModule : output module label;
71  empty label indicates no output;
72  optional, default: 'out'
73  Using None as any argument restores its default value.
74  """
75  _label = 'switchOnTrigger'
76  _defaultParameters = dicttypes.SortedKeysDict()
77 
78  def __init__( self ):
79  ConfigToolBase.__init__( self )
80  self.addParameter( self._defaultParameters, 'triggerProducer' , _defaultTriggerProducer , _defaultTriggerProducerComment )
81  self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
82  self.addParameter( self._defaultParameters, 'path' , _defaultPath , _defaultPathComment )
83  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
84  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
85  self._parameters = copy.deepcopy( self._defaultParameters )
86  self._comment = ""
87 
88  def getDefaultParameters( self ):
89  return self._defaultParameters
90 
91  def __call__( self, process
92  , triggerProducer = None
93  , triggerEventProducer = None
94  , path = None
95  , hltProcess = None
96  , outputModule = None
97  ):
98  if triggerProducer is None:
99  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
100  if triggerEventProducer is None:
101  triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
102  if path is None:
103  path = self._defaultParameters[ 'path' ].value
104  if hltProcess is None:
105  hltProcess = self._defaultParameters[ 'hltProcess' ].value
106  if outputModule is None:
107  outputModule = self._defaultParameters[ 'outputModule' ].value
108  self.setParameter( 'triggerProducer' , triggerProducer )
109  self.setParameter( 'triggerEventProducer', triggerEventProducer )
110  self.setParameter( 'path' , path )
111  self.setParameter( 'hltProcess' , hltProcess )
112  self.setParameter( 'outputModule' , outputModule )
113  self.apply( process )
114 
115  def toolCode( self, process ):
116  triggerProducer = self._parameters[ 'triggerProducer' ].value
117  triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
118  path = self._parameters[ 'path' ].value
119  hltProcess = self._parameters[ 'hltProcess' ].value
120  outputModule = self._parameters[ 'outputModule' ].value
121 
122  task = getPatAlgosToolsTask(process)
123 
124  # Load default producers from existing config files, if needed
125  if not hasattr( process, triggerProducer ):
127  addToProcessAndTask(triggerProducer, patTrigger.clone(), process, task)
128  else:
129  print '%s():'%( self._label )
130  print ' PATTriggerProducer module \'%s\' exists already in process'%( triggerProducer )
131  print ' ==> entry re-used'
132  print _longLine
133  if not hasattr( process, triggerEventProducer ):
135  addToProcessAndTask(triggerEventProducer, patTriggerEvent.clone(), process, task)
136  else:
137  print '%s():'%( self._label )
138  print ' PATTriggerEventProducer module \'%s\' exists already in process'%( triggerEventProducer )
139  print ' ==> entry re-used'
140  print _longLine
141 
142  # Maintain configurations
143  trigProdMod = getattr( process, triggerProducer )
144  trigProdMod.processName = hltProcess
145  if trigProdMod.onlyStandAlone.value() is True:
146  trigProdMod.onlyStandAlone = False
147  print ' configuration parameter automatically changed'
148  print ' PATTriggerProducer %s.onlyStandAlone --> %s'%( triggerProducer, trigProdMod.onlyStandAlone )
149  print _longLine
150  trigEvtProdMod = getattr( process, triggerEventProducer )
151  trigEvtProdMod.processName = hltProcess
152  trigEvtProdMod.patTriggerProducer = cms.InputTag( triggerProducer )
153  if not path is '':
154  if not hasattr( process, path ):
155  prodPath = cms.Path( trigProdMod + trigEvtProdMod )
156  setattr( process, path, prodPath )
157  print '%s():'%( self._label )
158  print ' Path \'%s\' does not exist in process'%( path )
159  print ' ==> created'
160  print _longLine
161  # Try to get the order right, but cannot deal with all possible cases.
162  # Simply rely on the exclusive usage of these tools without manual intervention.
163  else:
164  if not triggerProducer in _modulesInPath( process, path ):
165  prodPath = getattr( process, path )
166  prodPath += trigProdMod
167  if not triggerEventProducer in _modulesInPath( process, path ):
168  prodPath = getattr( process, path )
169  prodPath += trigEvtProdMod
170 
171  # Add event content
172  if outputModule is not '':
173  patTriggerEventContent = [ 'keep patTriggerObjects_%s_*_%s'%( triggerProducer, process.name_() )
174  , 'keep patTriggerFilters_%s_*_%s'%( triggerProducer, process.name_() )
175  , 'keep patTriggerPaths_%s_*_%s'%( triggerProducer, process.name_() )
176  , 'keep patTriggerEvent_%s_*_%s'%( triggerEventProducer, process.name_() )
177  ]
178  if ( hasattr( trigProdMod, 'addL1Algos' ) and trigProdMod.addL1Algos.value() is True ):
179  patTriggerEventContent += [ 'keep patTriggerConditions_%s_*_%s'%( triggerProducer, process.name_() )
180  , 'keep patTriggerAlgorithms_%s_*_%s'%( triggerProducer, process.name_() )
181  ]
182  if ( hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True ):
183  patTriggerEventContent += patTriggerL1RefsEventContent
184  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
185 
186 switchOnTrigger = SwitchOnTrigger()
187 
188 
190  """ Enables trigger information in PAT, limited to stand-alone trigger objects
191  SwitchOnTriggerStandAlone( [cms.Process], triggerProducer = 'patTrigger', path = '', hltProcess = 'HLT', outputModule = 'out' )
192  - [cms.Process] : the 'cms.Process'
193  - triggerProducer : PATTriggerProducer module label;
194  optional, default: 'patTrigger'
195  - path : name of path to use;
196  optional, default: ''
197  - hltProcess : HLT process name;
198  optional, default: 'HLT'
199  - outputModule : output module label;
200  empty label indicates no output;
201  optional, default: 'out'
202  Using None as any argument restores its default value.
203  """
204  _label = 'switchOnTriggerStandAlone'
205  _defaultParameters = dicttypes.SortedKeysDict()
206 
207  def __init__( self ):
208  ConfigToolBase.__init__( self )
209  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
210  self.addParameter( self._defaultParameters, 'path' , _defaultPath , _defaultPathComment )
211  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
212  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
213  self._parameters = copy.deepcopy( self._defaultParameters )
214  self._comment = ""
215 
216  def getDefaultParameters( self ):
217  return self._defaultParameters
218 
219  def __call__( self, process
220  , triggerProducer = None
221  , path = None
222  , hltProcess = None
223  , outputModule = None
224  ):
225  if triggerProducer is None:
226  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
227  if path is None:
228  path = self._defaultParameters[ 'path' ].value
229  if hltProcess is None:
230  hltProcess = self._defaultParameters[ 'hltProcess' ].value
231  if outputModule is None:
232  outputModule = self._defaultParameters[ 'outputModule' ].value
233  self.setParameter( 'triggerProducer', triggerProducer )
234  self.setParameter( 'path' , path )
235  self.setParameter( 'hltProcess' , hltProcess )
236  self.setParameter( 'outputModule' , outputModule )
237  self.apply( process )
238 
239  def toolCode( self, process ):
240 
241  task = getPatAlgosToolsTask(process)
242 
243  triggerProducer = self._parameters[ 'triggerProducer' ].value
244  path = self._parameters[ 'path' ].value
245  hltProcess = self._parameters[ 'hltProcess' ].value
246  outputModule = self._parameters[ 'outputModule' ].value
247 
248  # Load default producer from existing config file, if needed
249  if not hasattr( process, triggerProducer ):
251  addToProcessAndTask(triggerProducer, patTrigger.clone( onlyStandAlone = True ), process, task)
252  else:
253  print '%s():'%( self._label )
254  print ' PATTriggerProducer module \'%s\' exists already in process'%( triggerProducer )
255  print ' ==> entry re-used'
256  print _longLine
257 
258  # Maintain configuration
259  trigProdMod = getattr( process, triggerProducer )
260  trigProdMod.processName = hltProcess
261  if not path is '':
262  if not hasattr( process, path ):
263  prodPath = cms.Path( trigProdMod )
264  setattr( process, path, prodPath )
265  print '%s():'%( self._label )
266  print ' Path \'%s\' does not exist in process'%( path )
267  print ' ==> created'
268  print _longLine
269  elif not triggerProducer in _modulesInPath( process, path ):
270  prodPath = getattr( process, path )
271  prodPath += trigProdMod
272 
273  # Add event content
274  if outputModule is not '':
275  patTriggerEventContent = [ 'keep patTriggerObjectStandAlones_%s_*_%s'%( triggerProducer, process.name_() )
276  ]
277  if ( hasattr( trigProdMod, 'saveL1Refs' ) and trigProdMod.saveL1Refs.value() is True ):
278  patTriggerEventContent += patTriggerL1RefsEventContent
279  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
280 
281 switchOnTriggerStandAlone = SwitchOnTriggerStandAlone()
282 
283 
285  """ Enables trigger matching in PAT
286  SwitchOnTriggerMatching( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', triggerEventProducer = 'patTriggerEvent', path = '', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
287  - [cms.Process] : the 'cms.Process'
288  - triggerMatchers : PAT trigger matcher module labels (list)
289  optional; default: defined in '_exampleTriggerMatchers'
290  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcherExamples_cfi.py)
291  - triggerProducer : PATTriggerProducer module label;
292  optional, default: 'patTrigger'
293  - triggerEventProducer: PATTriggerEventProducer module label;
294  optional, default: 'patTriggerEvent'
295  - path : name of path to use;
296  optional, default: 'patDefaultSequence'
297  - hltProcess : HLT process name;
298  optional, default: 'HLT'
299  - outputModule : output module label;
300  empty label indicates no output;
301  optional, default: 'out'
302  - postfix : postfix to apply to PAT module labels;
303  optional, default: ''
304  Using None as any argument restores its default value.
305  """
306  _label = 'switchOnTriggerMatching'
307  _defaultParameters = dicttypes.SortedKeysDict()
308 
309  def __init__( self ):
310  ConfigToolBase.__init__( self )
311  self.addParameter( self._defaultParameters, 'triggerMatchers' , _exampleTriggerMatchers , _defaultTriggerMatchersComment )
312  self.addParameter( self._defaultParameters, 'exampleMatchers' , False , '' )
313  self.addParameter( self._defaultParameters, 'triggerProducer' , _defaultTriggerProducer , _defaultTriggerProducerComment )
314  self.addParameter( self._defaultParameters, 'triggerEventProducer', _defaultTriggerEventProducer, _defaultTriggerEventProducerComment )
315  self.addParameter( self._defaultParameters, 'path' , _defaultPath , _defaultPathComment )
316  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
317  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
318  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
319  self._parameters = copy.deepcopy( self._defaultParameters )
320  self._comment = ""
321 
322  def getDefaultParameters( self ):
323  return self._defaultParameters
324 
325  def __call__( self, process
326  , triggerMatchers = None
327  , triggerProducer = None
328  , triggerEventProducer = None
329  , path = None
330  , hltProcess = None
331  , outputModule = None
332  , postfix = None
333  ):
334  if triggerMatchers is None:
335  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
336  self.setParameter( 'exampleMatchers', True )
337  if triggerProducer is None:
338  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
339  if triggerEventProducer is None:
340  triggerEventProducer = self._defaultParameters[ 'triggerEventProducer' ].value
341  if path is None:
342  path = self._defaultParameters[ 'path' ].value
343  if hltProcess is None:
344  hltProcess = self._defaultParameters[ 'hltProcess' ].value
345  if outputModule is None:
346  outputModule = self._defaultParameters[ 'outputModule' ].value
347  if postfix is None:
348  postfix = self._defaultParameters[ 'postfix' ].value
349  self.setParameter( 'triggerMatchers' , triggerMatchers )
350  self.setParameter( 'triggerProducer' , triggerProducer )
351  self.setParameter( 'triggerEventProducer', triggerEventProducer )
352  self.setParameter( 'path' , path )
353  self.setParameter( 'hltProcess' , hltProcess )
354  self.setParameter( 'outputModule' , outputModule )
355  self.setParameter( 'postfix' , postfix )
356  self.apply( process )
357 
358  def toolCode( self, process ):
359  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
360  exampleMatchers = self._parameters[ 'exampleMatchers' ].value
361  triggerProducer = self._parameters[ 'triggerProducer' ].value
362  triggerEventProducer = self._parameters[ 'triggerEventProducer' ].value
363  path = self._parameters[ 'path' ].value
364  hltProcess = self._parameters[ 'hltProcess' ].value
365  outputModule = self._parameters[ 'outputModule' ].value
366  postfix = self._parameters[ 'postfix' ].value
367 
368  # Load default producers from existing config file, if needed
369  task = getPatAlgosToolsTask(process)
370  if exampleMatchers:
371  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
372  task.add(process.triggerMatcherExamplesTask)
373  # Switch on PAT trigger information if needed
374  if not hasattr( process, triggerEventProducer ):
375  print '%s():'%( self._label )
376  print ' PAT trigger production switched on automatically using'
377  print ' switchOnTrigger( process, \'%s\', \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerProducer, triggerEventProducer, path, outputModule )
378  print _longLine
379  switchOnTrigger( process, triggerProducer, triggerEventProducer, path, hltProcess, outputModule )
380 
381  # Maintain configurations
382  trigEvtProdMod = getattr( process, triggerEventProducer )
383  triggerMatchersKnown = []
384  for matcher in triggerMatchers:
385  if not hasattr( process, matcher ):
386  print '%s():'%( self._label )
387  print ' Matcher \'%s\' not known to process'%( matcher )
388  print ' ==> skipped'
389  print _longLine
390  continue
391  triggerMatchersKnown.append( matcher )
392  trigMchMod = getattr( process, matcher )
393  trigMchMod.src = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
394  trigMchMod.matched = triggerProducer
395  matchers = getattr( trigEvtProdMod, 'patTriggerMatches' )
396  if len( matchers ) > 0:
397  print '%s():'%( self._label )
398  print ' PAT trigger matchers already attached to existing PATTriggerEventProducer \'%s\''%( triggerEventProducer )
399  print ' configuration parameters automatically changed'
400  for matcher in matchers:
401  trigMchMod = getattr( process, matcher )
402  if trigMchMod.matched.value() is not triggerProducer:
403  trigMchMod.matched = triggerProducer
404  print ' PAT trigger matcher %s.matched --> %s'%( matcher, trigMchMod.matched )
405  print _longLine
406  else:
407  trigEvtProdMod.patTriggerMatches = cms.VInputTag()
408  for matcher in triggerMatchersKnown:
409  trigEvtProdMod.patTriggerMatches.append( cms.InputTag( matcher ) )
410 
411  # Add event content
412  if outputModule is not '':
413  patTriggerEventContent = []
414  for matcher in triggerMatchersKnown:
415  patTriggerEventContent += [ 'keep patTriggerObjectsedmAssociation_%s_%s_%s'%( triggerEventProducer, matcher, process.name_() )
416  , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
417  ]
418  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
419 
420 switchOnTriggerMatching = SwitchOnTriggerMatching()
421 
422 
424  """ Enables trigger matching in PAT
425  SwitchOnTriggerMatchingStandAlone( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', path = '', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
426  - [cms.Process] : the 'cms.Process'
427  - triggerMatchers: PAT trigger matcher module labels (list)
428  optional; default: defined in 'triggerMatchingDefaultSequence'
429  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcherExamples_cfi.py)
430  - triggerProducer: PATTriggerProducer module label;
431  optional, default: 'patTrigger'
432  - path : name of path to use;
433  optional, default: ''
434  - hltProcess : HLT process name;
435  optional, default: 'HLT'
436  - outputModule : output module label;
437  empty label indicates no output;
438  optional, default: 'out'
439  - postfix : postfix to apply to PAT module labels;
440  optional, default: ''
441  Using None as any argument restores its default value.
442  """
443  _label = 'switchOnTriggerMatchingStandAlone'
444  _defaultParameters = dicttypes.SortedKeysDict()
445 
446  def __init__( self ):
447  ConfigToolBase.__init__( self )
448  self.addParameter( self._defaultParameters, 'triggerMatchers', _exampleTriggerMatchers, _defaultTriggerMatchersComment )
449  self.addParameter( self._defaultParameters, 'exampleMatchers', False , '' )
450  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
451  self.addParameter( self._defaultParameters, 'path' , _defaultPath , _defaultPathComment )
452  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
453  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
454  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
455  self._parameters = copy.deepcopy( self._defaultParameters )
456  self._comment = ""
457 
458  def getDefaultParameters( self ):
459  return self._defaultParameters
460 
461  def __call__( self, process
462  , triggerMatchers = None
463  , triggerProducer = None
464  , path = None
465  , hltProcess = None
466  , outputModule = None
467  , postfix = None
468  ):
469  if triggerMatchers is None:
470  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
471  self.setParameter( 'exampleMatchers', True )
472  if triggerProducer is None:
473  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
474  if path is None:
475  path = self._defaultParameters[ 'path' ].value
476  if hltProcess is None:
477  hltProcess = self._defaultParameters[ 'hltProcess' ].value
478  if outputModule is None:
479  outputModule = self._defaultParameters[ 'outputModule' ].value
480  if postfix is None:
481  postfix = self._defaultParameters[ 'postfix' ].value
482  self.setParameter( 'triggerMatchers', triggerMatchers )
483  self.setParameter( 'triggerProducer', triggerProducer )
484  self.setParameter( 'path' , path )
485  self.setParameter( 'hltProcess' , hltProcess )
486  self.setParameter( 'outputModule' , outputModule )
487  self.setParameter( 'postfix' , postfix )
488  self.apply( process )
489 
490  def toolCode( self, process ):
491  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
492  exampleMatchers = self._parameters[ 'exampleMatchers' ].value
493  triggerProducer = self._parameters[ 'triggerProducer' ].value
494  path = self._parameters[ 'path' ].value
495  hltProcess = self._parameters[ 'hltProcess' ].value
496  outputModule = self._parameters[ 'outputModule' ].value
497  postfix = self._parameters[ 'postfix' ].value
498 
499  # Load default producers from existing config file, if needed
500  task = getPatAlgosToolsTask(process)
501  if exampleMatchers:
502  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
503  task.add(process.triggerMatcherExamplesTask)
504 
505  # Switch on PAT trigger information if needed
506  if not hasattr( process, triggerProducer ):
507  print '%s():'%( self._label )
508  print ' PAT trigger production switched on automatically using'
509  print ' switchOnTriggerStandAlone( process, \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerProducer, path, outputModule )
510  print _longLine
511  switchOnTriggerStandAlone( process, triggerProducer, path, hltProcess, outputModule )
512 
513  # Maintain configurations
514  triggerMatchersKnown = []
515  for matcher in triggerMatchers:
516  if not hasattr( process, matcher ):
517  print '%s():'%( self._label )
518  print ' Matcher \'%s\' not known to process'%( matcher )
519  print ' ==> skipped'
520  print _longLine
521  continue
522  triggerMatchersKnown.append( matcher )
523  trigMchMod = getattr( process, matcher )
524  trigMchMod.src = cms.InputTag( trigMchMod.src.getModuleLabel() + postfix )
525  trigMchMod.matched = triggerProducer
526 
527  # Add event content
528  if outputModule is not '':
529  patTriggerEventContent = []
530  for matcher in triggerMatchersKnown:
531  patTriggerEventContent += [ 'keep patTriggerObjectStandAlonesedmAssociation_%s_*_%s'%( matcher, process.name_() )
532  , 'keep *_%s_*_*'%( getattr( process, matcher ).src.value() )
533  ]
534  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
535 
536 switchOnTriggerMatchingStandAlone = SwitchOnTriggerMatchingStandAlone()
537 
538 
540  """ Enables embedding of trigger matches into PAT objects
541  SwitchOnTriggerMatchEmbedding( [cms.Process], triggerMatchers = [default list], triggerProducer = 'patTrigger', path = '', hltProcess = 'HLT', outputModule = 'out', postfix = '' )
542  - [cms.Process] : the 'cms.Process'
543  - triggerMatchers: PAT trigger matcher module labels (list)
544  optional; default: defined in 'triggerMatchingDefaultSequence'
545  (s. PhysicsTools/PatAlgos/python/triggerLayer1/triggerMatcherExamples_cfi.py)
546  - triggerProducer: PATTriggerProducer module label;
547  optional, default: 'patTrigger'
548  - path : name of path to use;
549  optional, default: ''
550  - hltProcess : HLT process name;
551  optional, default: 'HLT'
552  - outputModule : output module label;
553  empty label indicates no output;
554  optional, default: 'out'
555  - postfix : postfix to apply to PAT module labels;
556  optional, default: ''
557  Using None as any argument restores its default value.
558  """
559  _label = 'switchOnTriggerMatchEmbedding'
560  _defaultParameters = dicttypes.SortedKeysDict()
561 
562  def __init__( self ):
563  ConfigToolBase.__init__( self )
564  self.addParameter( self._defaultParameters, 'triggerMatchers', _exampleTriggerMatchers, _defaultTriggerMatchersComment )
565  self.addParameter( self._defaultParameters, 'exampleMatchers', False , '' )
566  self.addParameter( self._defaultParameters, 'triggerProducer', _defaultTriggerProducer, _defaultTriggerProducerComment )
567  self.addParameter( self._defaultParameters, 'path' , _defaultPath , _defaultPathComment )
568  self.addParameter( self._defaultParameters, 'hltProcess' , _defaultHltProcess , _defaultHltProcessComment )
569  self.addParameter( self._defaultParameters, 'outputModule' , _defaultOutputModule , _defaultOutputModuleComment )
570  self.addParameter( self._defaultParameters, 'postfix' , _defaultPostfix , _defaultPostfixComment )
571  self._parameters = copy.deepcopy( self._defaultParameters )
572  self._comment = ""
573 
574  def getDefaultParameters( self ):
575  return self._defaultParameters
576 
577  def __call__( self, process
578  , triggerMatchers = None
579  , triggerProducer = None
580  , path = None
581  , hltProcess = None
582  , outputModule = None
583  , postfix = None
584  ):
585  if triggerMatchers is None:
586  triggerMatchers = self._defaultParameters[ 'triggerMatchers' ].value
587  self.setParameter( 'exampleMatchers', True )
588  if triggerProducer is None:
589  triggerProducer = self._defaultParameters[ 'triggerProducer' ].value
590  if path is None:
591  path = self._defaultParameters[ 'path' ].value
592  if hltProcess is None:
593  hltProcess = self._defaultParameters[ 'hltProcess' ].value
594  if outputModule is None:
595  outputModule = self._defaultParameters[ 'outputModule' ].value
596  if postfix is None:
597  postfix = self._defaultParameters[ 'postfix' ].value
598  self.setParameter( 'triggerMatchers', triggerMatchers )
599  self.setParameter( 'triggerProducer', triggerProducer )
600  self.setParameter( 'path' , path )
601  self.setParameter( 'hltProcess' , hltProcess )
602  self.setParameter( 'outputModule' , outputModule )
603  self.setParameter( 'postfix' , postfix )
604  self.apply( process )
605 
606  def toolCode( self, process ):
607  triggerMatchers = self._parameters[ 'triggerMatchers' ].value
608  exampleMatchers = self._parameters[ 'exampleMatchers' ].value
609  triggerProducer = self._parameters[ 'triggerProducer' ].value
610  path = self._parameters[ 'path' ].value
611  hltProcess = self._parameters[ 'hltProcess' ].value
612  outputModule = self._parameters[ 'outputModule' ].value
613  postfix = self._parameters[ 'postfix' ].value
614 
615  # Load default producers from existing config file, if needed
616  task = getPatAlgosToolsTask(process)
617  if exampleMatchers:
618  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
619  task.add(process.triggerMatcherExamplesTask)
620 
621  # Build dictionary of allowed input producers
622  dictPatProducers = { 'PATPhotonCleaner' : 'PATTriggerMatchPhotonEmbedder'
623  , 'PATElectronCleaner': 'PATTriggerMatchElectronEmbedder'
624  , 'PATMuonCleaner' : 'PATTriggerMatchMuonEmbedder'
625  , 'PATTauCleaner' : 'PATTriggerMatchTauEmbedder'
626  , 'PATJetCleaner' : 'PATTriggerMatchJetEmbedder'
627  , 'PATMETCleaner' : 'PATTriggerMatchMETEmbedder'
628 # , 'PATGenericParticleCleaner' : ''
629 # , 'PATPFParticleCleaner' : ''
630  , 'PATPhotonSelector' : 'PATTriggerMatchPhotonEmbedder'
631  , 'PATElectronSelector': 'PATTriggerMatchElectronEmbedder'
632  , 'PATMuonSelector' : 'PATTriggerMatchMuonEmbedder'
633  , 'PATTauSelector' : 'PATTriggerMatchTauEmbedder'
634  , 'PATJetSelector' : 'PATTriggerMatchJetEmbedder'
635  , 'PATMETSelector' : 'PATTriggerMatchMETEmbedder'
636 # , 'PATGenericParticleSelector' : ''
637 # , 'PATPFParticleSelector' : ''
638 # , 'PATCompositeCandidateSelector' : ''
639  , 'PATPhotonRefSelector' : 'PATTriggerMatchPhotonEmbedder'
640  , 'PATElectronRefSelector': 'PATTriggerMatchElectronEmbedder'
641  , 'PATMuonRefSelector' : 'PATTriggerMatchMuonEmbedder'
642  , 'PATTauRefSelector' : 'PATTriggerMatchTauEmbedder'
643  , 'PATJetRefSelector' : 'PATTriggerMatchJetEmbedder'
644  , 'PATMETRefSelector' : 'PATTriggerMatchMETEmbedder'
645 # , 'PATGenericParticleRefSelector' : ''
646 # , 'PATPFParticleRefSelector' : ''
647 # , 'PATCompositeCandidateRefSelector' : ''
648  , 'PATPhotonProducer' : 'PATTriggerMatchPhotonEmbedder'
649  , 'PATElectronProducer': 'PATTriggerMatchElectronEmbedder'
650  , 'PATMuonProducer' : 'PATTriggerMatchMuonEmbedder'
651  , 'PATTauProducer' : 'PATTriggerMatchTauEmbedder'
652  , 'PATJetProducer' : 'PATTriggerMatchJetEmbedder'
653  , 'PATMETProducer' : 'PATTriggerMatchMETEmbedder'
654 # , 'PATGenericParticleProducer' : ''
655 # , 'PATPFParticleProducer' : ''
656 # , 'PATCompositeCandidateProducer' : ''
657  , 'MuonSelectorVertex': 'PATTriggerMatchMuonEmbedder'
658  }
659 
660  # Switch on PAT trigger matching if needed
661  dictConfig = {}
662  if not hasattr( process, triggerProducer ):
663  if exampleMatchers:
664  print '%s():'%( self._label )
665  print ' PAT trigger matching switched on automatically using'
666  print ' switchOnTriggerMatchingStandAlone( process, \'%s\', None, \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerProducer, path, outputModule, postfix )
667  print _longLine
668  switchOnTriggerMatchingStandAlone( process, None, triggerProducer, path, hltProcess, '', postfix ) # Do not store intermediate output collections.
669  else:
670  print '%s():'%( self._label )
671  print ' PAT trigger matching switched on automatically using'
672  print ' switchOnTriggerMatchingStandAlone( process, \'%s\', %s, \'%s\', \'%s\', \'%s\', \'%s\' )'%( hltProcess, triggerMatchers, triggerProducer, path, outputModule, postfix )
673  print _longLine
674  switchOnTriggerMatchingStandAlone( process, triggerMatchers, triggerProducer, path, hltProcess, '', postfix ) # Do not store intermediate output collections.
675  elif exampleMatchers:
676  process.load( "PhysicsTools.PatAlgos.triggerLayer1.triggerMatcherExamples_cfi" )
677  task.add(process.triggerMatcherExamplesTask)
678 
679  # Build dictionary of matchers
680  for matcher in triggerMatchers:
681  if not hasattr( process, matcher ):
682  print '%s():'%( self._label )
683  print ' PAT trigger matcher \'%s\' not known to process'%( matcher )
684  print ' ==> skipped'
685  print _longLine
686  continue
687  trigMchMod = getattr( process, matcher )
688  patObjProd = getattr( process, trigMchMod.src.value() + postfix )
689  if trigMchMod.src.value() in dictConfig:
690  dictConfig[ patObjProd.type_() ] += [ matcher ]
691  else:
692  dictConfig[ patObjProd.type_() ] = [ matcher ]
693 
694  # Maintain configurations
695  patTriggerEventContent = []
696  for patObjProdType in dictConfig.keys():
697  if patObjProdType in dictPatProducers:
698  for matcher in dictConfig[ patObjProdType ]:
699  trigMchMod = getattr( process, matcher )
700  patObjProd = getattr( process, trigMchMod.src.value() + postfix )
701  # Configure embedder module
702  label = patObjProd.label_() + 'TriggerMatch' # hardcoded default
703  if hasattr( process, label ):
704  print '%s():'%( self._label )
705  print ' PAT trigger match embedder \'%s\' exists already in process'%( label )
706  print ' ==> entry re-used'
707  print _longLine
708  module = getattr( process, label )
709  if not module.type_() is dictPatProducers[ patObjProdType ]:
710  print '%s():'%( self._label )
711  print ' Configuration conflict for PAT trigger match embedder \'%s\''%( label )
712  print ' - exists as %s'%( module.type_() )
713  print ' - requested as %s by \'%s\''%( dictPatProducers[ patObjProdType ], matcher )
714  print ' ==> skipped'
715  print _longLine
716  continue
717  if not module.src.value() is trigMchMod.src.value() + postfix:
718  print '%s():'%( self._label )
719  print ' Configuration conflict for PAT trigger match embedder \'%s\''%( label )
720  print ' - exists for input %s'%( module.src.value() )
721  print ' - requested for input %s by \'%s\''%( trigMchMod.src.value() + postfix, matcher )
722  print ' ==> skipped'
723  print _longLine
724  continue
725  module.matches.append( cms.InputTag( matcher ) )
726  else:
727  module = cms.EDProducer( dictPatProducers[ patObjProdType ] )
728  module.src = cms.InputTag( patObjProd.label_() )
729  module.matches = cms.VInputTag( matcher )
730  addToProcessAndTask(label, module, process, task)
731  # Add event content
732  patTriggerEventContent += [ 'drop *_%s_*_*'%( patObjProd.label_() )
733  , 'keep *_%s_*_%s'%( label, process.name_() )
734  ]
735  else:
736  print '%s():'%( self._label )
737  print ' Invalid input source for trigger match embedding'
738  print ' ==> %s with matchers \'%s\' is skipped'%( patObjProdType, dictConfig[ patObjProdType ] )
739  print _longLine
740  if outputModule is not '':
741  getattr( process, outputModule ).outputCommands = _addEventContent( getattr( process, outputModule ).outputCommands, patTriggerEventContent )
742 
743 switchOnTriggerMatchEmbedding = SwitchOnTriggerMatchEmbedding()
def __call__(self, process, triggerMatchers=None, triggerProducer=None, path=None, hltProcess=None, outputModule=None, postfix=None)
Definition: trigTools.py:584
def __call__(self, process, triggerProducer=None, path=None, hltProcess=None, outputModule=None)
Definition: trigTools.py:224
def __call__(self, process, triggerProducer=None, triggerEventProducer=None, path=None, hltProcess=None, outputModule=None)
Definition: trigTools.py:97
def addToProcessAndTask(label, module, process, task)
Definition: helpers.py:27
def _addEventContent(outputCommands, eventContent)
Definition: trigTools.py:29
def toolCode(self, process)
Definition: trigTools.py:115
def toolCode(self, process)
Definition: trigTools.py:358
def __call__(self, process, triggerMatchers=None, triggerProducer=None, path=None, hltProcess=None, outputModule=None, postfix=None)
Definition: trigTools.py:468
def listModules(sequence)
Definition: helpers.py:201
switchOnTriggerStandAlone
Definition: trigTools.py:281
def __call__(self, process, triggerMatchers=None, triggerProducer=None, triggerEventProducer=None, path=None, hltProcess=None, outputModule=None, postfix=None)
Definition: trigTools.py:333
switchOnTriggerMatchingStandAlone
Definition: trigTools.py:536
def getPatAlgosToolsTask(process)
Definition: helpers.py:12
def _modulesInPath(process, pathLabel)
Definition: trigTools.py:25
def getDefaultParameters(self)
Definition: trigTools.py:88