CMS 3D CMS Logo

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