CMS 3D CMS Logo

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