test
CMS 3D CMS Logo

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