CMS 3D CMS Logo

HiCoreTools.py
Go to the documentation of this file.
1 from __future__ import print_function
3 
5 
7 
8  """ Remove pat object production steps which rely on RECO event
9  content
10  """
11  _label='restrictInputToAOD'
12  _defaultParameters=dicttypes.SortedKeysDict()
13  def __init__(self):
14  ConfigToolBase.__init__(self)
15  self.addParameter(self._defaultParameters,'names',['All'], "list of collection names; supported are 'Photons', 'Electrons',, 'Muons', 'Taus', 'Jets', 'METs', 'All'", allowedValues=['Photons','Electrons', 'Muons', 'Taus', 'Jets', 'METs', 'All'])
16  self._parameters=copy.deepcopy(self._defaultParameters)
17  self._comment = ""
18 
20  return self._defaultParameters
21 
22  def __call__(self,process,
23  names = None) :
24  if names is None:
25  names=self._defaultParameters['names'].value
26  self.setParameter('names',names)
27  self.apply(process)
28 
29  def toolCode(self, process):
30  names=self._parameters['names'].value
31  for obj in range(len(names)):
32  print("---------------------------------------------------------------------")
33  print("WARNING: the following additional information can only be used on ")
34  print(" RECO format:")
35  if( names[obj] == 'Photons' or names[obj] == 'All' ):
36  print(" * nothing needs to be done for Photons")
37  if( names[obj] == 'Electrons' or names[obj] == 'All' ):
38  print(" * nothing needs to be done for Electrons")
39  if( names[obj] == 'Muons' or names[obj] == 'All' ):
40  print(" * nothing needs to be done for Muons")
41  if( names[obj] == 'Taus' or names[obj] == 'All' ):
42  print(" * nothing needs to be done for Taus")
43  if( names[obj] == 'Jets' or names[obj] == 'All' ):
44  print(" * nothing needs to be done for Jets")
45  if( names[obj] == 'METs' or names[obj] == 'All' ):
46  print(" * nothing needs to be done for METs")
47  print("---------------------------------------------------------------------")
48 
49 restrictInputToAOD=RestrictInputToAOD()
50 
51 
53 
54  """ Remove monte carlo matching from a given collection or all PAT
55  candidate collections:
56  """
57  _label='removeMCMatching'
58  _defaultParameters=dicttypes.SortedKeysDict()
59  def __init__(self):
60  ConfigToolBase.__init__(self)
61  self.addParameter(self._defaultParameters,'names',['All'], "collection name; supported are 'Photons','Muons', 'Taus', 'Jets', 'METs', 'All', 'PFAll','PFTaus','PFMuons'", allowedValues=['Photons','Muons', 'Taus', 'Jets', 'METs', 'All', 'PFAll','PFTaus','PFMuons'])
62  self.addParameter(self._defaultParameters,'postfix',"", "postfix of default sequence")
63  self._parameters=copy.deepcopy(self._defaultParameters)
64  self._comment = ""
65 
67  return self._defaultParameters
68 
69  def __call__(self,process,
70  names = None,
71  postfix = None) :
72  if names is None:
73  names=self._defaultParameters['names'].value
74  if postfix is None:
75  postfix=self._defaultParameters['postfix'].value
76  self.setParameter('names',names)
77  self.setParameter('postfix',postfix)
78  self.apply(process)
79 
80  def toolCode(self, process):
81  names=self._parameters['names'].value
82  postfix=self._parameters['postfix'].value
83 
84  print("************** MC dependence removal ************")
85  for obj in range(len(names)):
86  if( names[obj] == 'Photons' or names[obj] == 'All' ):
87  print("removing MC dependencies for photons")
88  _removeMCMatchingForPATObject(process, 'photonMatch', 'patPhotons', postfix)
89  if( names[obj] == 'Muons' or names[obj] == 'All' ):
90  print("removing MC dependencies for muons")
91  _removeMCMatchingForPATObject(process, 'muonMatch', 'patMuons', postfix)
92  if( names[obj] == 'Taus' or names[obj] == 'All' ):
93  print("removing MC dependencies for taus")
94  _removeMCMatchingForPATObject(process, 'tauMatch', 'patTaus', postfix)
95  ## remove mc extra modules for taus
96  getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(
97  applyPostfix(process, "tauGenJets", postfix))
98  getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(
99  applyPostfix(process, "tauGenJetsSelectorAllHadrons", postfix))
100  getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(
101  applyPostfix(process, "tauGenJetMatch", postfix))
102  ## remove mc extra configs for taus
103  tauProducer = getattr(process, 'patTaus'+postfix)
104  tauProducer.addGenJetMatch = False
105  tauProducer.embedGenJetMatch = False
106  tauProducer.genJetMatch = ''
107  if( names[obj] == 'Jets' ):# or names[obj] == 'All' ):
108  print("removing MC dependencies for jets")
109  ## remove mc extra modules for jets
110  getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(
111  applyPostfix(process, "patJetPartonMatch", postfix))
112  getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(
113  applyPostfix(process, "patJetGenJetMatch", postfix))
114 # getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(
115 # applyPostfix(process, "patJetFlavourId", postfix))
116  ## remove mc extra configs for jets
117  jetProducer = getattr(process, jetCollectionString()+postfix)
118  jetProducer.addGenPartonMatch = False
119  jetProducer.embedGenPartonMatch = False
120  jetProducer.genPartonMatch = ''
121  jetProducer.addGenJetMatch = False
122  jetProducer.genJetMatch = ''
123  jetProducer.getJetMCFlavour = False
124  jetProducer.JetPartonMapSource = ''
125  ## adjust output
126  # process.out.outputCommands.append("drop *_selectedPatJets*_genJets_*")
127 
128  if( names[obj] == 'METs' or names[obj] == 'All' ):
129  ## remove mc extra configs for jets
130  metProducer = getattr(process, 'patMETs'+postfix)
131  metProducer.addGenMET = False
132  metProducer.genMETSource = ''
133 
134 removeMCMatching=RemoveMCMatching()
135 
136 def _removeMCMatchingForPATObject(process, matcherName, producerName, postfix=""):
137  ## remove mcMatcher from the default sequence
138  objectMatcher = getattr(process, matcherName+postfix)
139  if (producerName=='pfPatMuons'or producerName=='pfPatTaus'):
140  #no idea what this should do: there is no other occurance of 'PFPATafterPAT' in CMSSW other than here...
141  getattr(process,"PFPATafterPAT"+postfix).remove(objectMatcher)
142  if (producerName=='patMuons'or producerName=='patTaus'or
143  producerName=='patPhotons' or producerName=='patElectrons'):
144  getattr(process,"patHeavyIonDefaultSequence"+postfix).remove(objectMatcher)
145  ## straighten photonProducer
146  objectProducer = getattr(process, producerName+postfix)
147  objectProducer.addGenMatch = False
148  objectProducer.embedGenMatch = False
149  objectProducer.genParticleMatch = ''
150 
151 
153 
154  """ Remove all PAT objects from the default sequence but a specific one
155  """
156  _label='removeAllPATObjectsBut'
157  _defaultParameters=dicttypes.SortedKeysDict()
158  def __init__(self):
159  ConfigToolBase.__init__(self)
160  self.addParameter(self._defaultParameters,'names',self._defaultValue, "list of collection names; supported are 'Photons', 'Electrons', 'Muons', 'Taus', 'Jets', 'METs'", Type=list, allowedValues=['Photons', 'Electrons', 'Muons', 'Taus', 'Jets', 'METs'])
161  self.addParameter(self._defaultParameters,'outputInProcess',True, "indicate whether there is an output module specified for the process (default is True) ")
162  self._parameters=copy.deepcopy(self._defaultParameters)
163  self._comment = ""
164 
166  return self._defaultParameters
167 
168  def __call__(self,process,
169  names = None,
170  outputInProcess = None) :
171  if names is None:
172  names=self._defaultParameters['names'].value
173  if outputInProcess is None:
174  outputInProcess=self._defaultParameters['outputInProcess'].value
175  self.setParameter('names',names)
176  self.setParameter('outputInProcess',outputInProcess)
177  self.apply(process)
178 
179  def toolCode(self, process):
180  names=self._parameters['names'].value
181  outputInProcess=self._parameters['outputInProcess'].value
182 
183  removeTheseObjectCollections = ['Photons', 'Electrons', 'Muons', 'Taus', 'Jets', 'METs']
184  for obj in range(len(names)):
185  removeTheseObjectCollections.remove(names[obj])
186  removeSpecificPATObjects(process, removeTheseObjectCollections, outputInProcess)
187 
188 removeAllPATObjectsBut=RemoveAllPATObjectsBut()
189 
190 
192 
193  """ Remove a specific PAT object from the default sequence
194  """
195  _label='removeSpecificPATObjects'
196  _defaultParameters=dicttypes.SortedKeysDict()
197  def __init__(self):
198  ConfigToolBase.__init__(self)
199  self.addParameter(self._defaultParameters,'names',self._defaultValue, "list of collection names; supported are 'Photons', 'Electrons', 'Muons', 'Taus', 'Jets', 'METs'", Type=list, allowedValues=['Photons', 'Electrons', 'Muons', 'Taus', 'Jets', 'METs'])
200  self.addParameter(self._defaultParameters,'outputInProcess',True,"indicate whether there is an output module specified for the process (default is True)" )
201  self.addParameter(self._defaultParameters,'postfix',"", "postfix of default sequence")
202  self._parameters=copy.deepcopy(self._defaultParameters)
203  self._comment = ""
204 
206  return self._defaultParameters
207 
208  def __call__(self,process,
209  names = None,
210  outputInProcess = None,
211  postfix = None) :
212  if names is None:
213  names=self._defaultParameters['names'].value
214  if outputInProcess is None:
215  outputInProcess=self._defaultParameters['outputInProcess'].value
216  if postfix is None:
217  postfix=self._defaultParameters['postfix'].value
218  self.setParameter('names',names)
219  self.setParameter('outputInProcess',outputInProcess)
220  self.setParameter('postfix',postfix)
221  self.apply(process)
222 
223  def toolCode(self, process):
224  names=self._parameters['names'].value
225  outputInProcess=self._parameters['outputInProcess'].value
226  postfix=self._parameters['postfix'].value
227  ## remove pre object production steps from the default sequence
228 
229  for obj in range(len(names)):
230  if( names[obj] == 'Photons' ):
231  removeIfInSequence(process, 'patPhotonIsolation', "patHeavyIonDefaultSequence", postfix)
232  removeIfInSequence(process, 'photonMatch', "patHeavyIonDefaultSequence", postfix)
233  if( names[obj] == 'Muons' ):
234  removeIfInSequence(process, 'muonMatch', "patHeavyIonDefaultSequence", postfix)
235  if( names[obj] == 'Taus' ):
236  removeIfInSequence(process, 'patPFCandidateIsoDepositSelection', "patHeavyIonDefaultSequence", postfix)
237  removeIfInSequence(process, 'patPFTauIsolation', "patHeavyIonDefaultSequence", postfix)
238  removeIfInSequence(process, 'tauMatch', "patHeavyIonDefaultSequence", postfix)
239  removeIfInSequence(process, 'tauGenJets', "patHeavyIonDefaultSequence", postfix)
240  removeIfInSequence(process, 'tauGenJetsSelectorAllHadrons', "patHeavyIonDefaultSequence", postfix)
241  removeIfInSequence(process, 'tauGenJetMatch', "patHeavyIonDefaultSequence", postfix)
242  if( names[obj] == 'Jets' ):
243  removeIfInSequence(process, 'patJetCharge', "patHeavyIonDefaultSequence", postfix)
244  removeIfInSequence(process, 'patJetCorrections', "patHeavyIonDefaultSequence", postfix)
245  removeIfInSequence(process, 'patJetPartonMatch', "patHeavyIonDefaultSequence", postfix)
246  removeIfInSequence(process, 'patJetGenJetMatch', "patHeavyIonDefaultSequence", postfix)
247 # removeIfInSequence(process, 'patJetFlavourId', "patHeavyIonDefaultSequence", postfix)
248  if( names[obj] == 'METs' ):
249  removeIfInSequence(process, 'patMETCorrections', "patHeavyIonDefaultSequence", postfix)
250 
251  ## remove object production steps from the default sequence
252  if( names[obj] == 'METs' ):
253  process.patCandidates.remove( getattr(process, 'pat'+names[obj]) )
254  else:
255  if( names[obj] == 'Jets' ):
256  applyPostfix(process,"patCandidates",postfix).remove(
257  getattr(process, jetCollectionString()+postfix) )
258  applyPostfix(process,"selectedPatCandidates",postfix).remove(
259  getattr(process, jetCollectionString('selected')+postfix) )
260  applyPostfix(process,"countPatCandidates",postfix).remove(
261  getattr(process, jetCollectionString('count')+postfix) )
262  else:
263  applyPostfix(process,"patCandidates",postfix).remove(
264  getattr(process, 'pat'+names[obj]+postfix) )
265  applyPostfix(process,"selectedPatCandidates",postfix).remove(
266  getattr(process, 'selectedPat'+names[obj]+postfix) )
267  applyPostfix(process,"countPatCandidates",postfix).remove(
268  getattr(process, 'countPat'+names[obj]+postfix) )
269  ## in the case of leptons, the lepton counter must be modified as well
270  if( names[obj] == 'Electrons' ):
271  print('removed from lepton counter: electrons')
272  applyPostfix(process,"countPatLeptons",postfix).countElectrons = False
273  elif( names[obj] == 'Muons' ):
274  print('removed from lepton counter: muons')
275  applyPostfix(process,"countPatLeptons",postfix).countMuons = False
276  elif( names[obj] == 'Taus' ):
277  print('removed from lepton counter: taus')
278  applyPostfix(process,"countPatLeptons",postfix).countTaus = False
279  ## remove from summary
280  if( names[obj] == 'METs' ):
281  applyPostfix(process,"patCandidateSummary",postfix).candidates.remove(
282  cms.InputTag('pat'+names[obj]+postfix) )
283  else:
284  if( names[obj] == 'Jets' ):
285  applyPostfix(process,"patCandidateSummary",postfix).candidates.remove(
286  cms.InputTag(jetCollectionString()+postfix) )
287  applyPostfix(process,"selectedPatCandidateSummary",postfix).candidates.remove(
288  cms.InputTag(jetCollectionString('selected')+postfix) )
289  applyPostfix(process,"cleanPatCandidateSummary",postfix).candidates.remove(
290  cms.InputTag(jetCollectionString('clean')+postfix) )
291  else:
292  applyPostfix(process,"patCandidateSummary",postfix).candidates.remove(
293  cms.InputTag('pat'+names[obj]+postfix) )
294  applyPostfix(process,"selectedPatCandidateSummary",postfix).candidates.remove(
295  cms.InputTag('selectedPat'+names[obj]+postfix) )
296  getattr(process,"cleanPatCandidateSummary"+postfix).candidates.remove(
297  cms.InputTag('cleanPat'+names[obj]+postfix) )
298  ## remove cleaning for the moment; in principle only the removed object
299  ## could be taken out of the checkOverlaps PSet
300  if ( outputInProcess ):
301  print("---------------------------------------------------------------------")
302  print("INFO : some objects have been removed from the sequence. Switching ")
303  print(" off PAT cross collection cleaning, as it might be of limited")
304  print(" sense now. If you still want to keep object collection cross")
305  print(" cleaning within PAT you need to run and configure it by hand")
306  removeCleaning(process)
307 
308 
309 removeSpecificPATObjects=RemoveSpecificPATObjects()
310 
311 
313 
314  """ remove PAT cleaning from the default sequence:
315  """
316  _label='removeCleaning'
317  _defaultParameters=dicttypes.SortedKeysDict()
318  def __init__(self):
319  ConfigToolBase.__init__(self)
320  self.addParameter(self._defaultParameters,'outputInProcess',True,"indicate whether there is an output module specified for the process (default is True)" )
321  self.addParameter(self._defaultParameters,'postfix',"", "postfix of default sequence")
322  self._parameters=copy.deepcopy(self._defaultParameters)
323  self._comment = ""
324 
326  return self._defaultParameters
327 
328  def __call__(self,process,
329  outputInProcess = None,
330  postfix = None) :
331  if outputInProcess is None:
332  outputInProcess=self._defaultParameters['outputInProcess'].value
333  if postfix is None:
334  postfix=self._defaultParameters['postfix'].value
335 
336  self.setParameter('outputInProcess',outputInProcess)
337  self.setParameter('postfix',postfix)
338 
339  self.apply(process)
340 
341  def toolCode(self, process):
342  outputInProcess=self._parameters['outputInProcess'].value
343  postfix=self._parameters['postfix'].value
344 
345  ## adapt single object counters
346  for m in listModules(applyPostfix(process,"countPatCandidates",postfix)):
347  if hasattr(m, 'src'): m.src = m.src.value().replace('cleanPat','selectedPat')
348 
349  ## adapt lepton counter
350  countLept = applyPostfix(process,"countPatLeptons",postfix)
351  countLept.electronSource = countLept.electronSource.value().replace('cleanPat','selectedPat')
352  countLept.muonSource = countLept.muonSource.value().replace('cleanPat','selectedPat')
353  countLept.tauSource = countLept.tauSource.value().replace('cleanPat','selectedPat')
354  getattr(process, "patHeavyIonDefaultSequence"+postfix).remove(
355  applyPostfix(process,"cleanPatCandidates",postfix)
356  )
357  if ( outputInProcess ):
358  print("---------------------------------------------------------------------")
359  print("INFO : cleaning has been removed. Switch output from clean PAT ")
360  print(" candidates to selected PAT candidates.")
361  ## add selected layer1 objects to the pat output
362  from PhysicsTools.PatAlgos.patEventContent_cff import patEventContentNoCleaning
363  process.out.outputCommands = patEventContentNoCleaning
364 
365 
366 removeCleaning=RemoveCleaning()
367 
368 
370 
371  """ Add PAT cleaning from the default sequence
372  """
373  _label='addCleaning'
374  _defaultParameters=dicttypes.SortedKeysDict()
375  def __init__(self):
376  ConfigToolBase.__init__(self)
377  self.addParameter(self._defaultParameters,'outputInProcess',True, "")
378  self._parameters=copy.deepcopy(self._defaultParameters)
379  self._comment = ""
380 
382  return self._defaultParameters
383 
384  def __call__(self,process,
385  outputInProcess = None):
386  if outputInProcess is None:
387  outputInProcess=self._defaultParameters['outputInProcess'].value
388 
389  self.setParameter('outputInProcess',outputInProcess)
390  self.apply(process)
391 
392  def toolCode(self, process):
393  outputInProcess=self._parameters['outputInProcess'].value
394 
395  ## adapt single object counters
396  process.patHeavyIonDefaultSequence.replace(process.countPatCandidates, process.cleanPatCandidates * process.countPatCandidates)
397  for m in listModules(process.countPatCandidates):
398  if hasattr(m, 'src'): m.src = m.src.value().replace('selectedPat','cleanPat')
399  ## adapt lepton counter
400  countLept = process.countPatLeptons
401  countLept.electronSource = countLept.electronSource.value().replace('selectedPat','cleanPat')
402  countLept.muonSource = countLept.muonSource.value().replace('selectedPat','cleanPat')
403  countLept.tauSource = countLept.tauSource.value().replace('selectedPat','cleanPat')
404  if ( outputInProcess ):
405  print("---------------------------------------------------------------------")
406  print("INFO : cleaning has been added. Switch output from selected PAT ")
407  print(" candidates to clean PAT candidates.")
408  ## add clean layer1 objects to the pat output
409  from PhysicsTools.PatAlgos.patEventContent_cff import patEventContent
410  process.out.outputCommands = patEventContent
411 
412 addCleaning=AddCleaning()
def __call__(self, process, names=None, outputInProcess=None)
Definition: HiCoreTools.py:170
def __call__(self, process, outputInProcess=None, postfix=None)
Definition: HiCoreTools.py:330
def replace(string, replacements)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def removeIfInSequence(process, target, sequenceLabel, postfix="")
Definition: helpers.py:123
def _removeMCMatchingForPATObject(process, matcherName, producerName, postfix="")
Definition: HiCoreTools.py:136
def applyPostfix(process, label, postfix)
Definition: helpers.py:115
def listModules(sequence)
Definition: helpers.py:207
def toolCode(self, process)
Definition: HiCoreTools.py:341
def __call__(self, process, outputInProcess=None)
Definition: HiCoreTools.py:385
def toolCode(self, process)
Definition: HiCoreTools.py:29
def __call__(self, process, names=None, postfix=None)
Definition: HiCoreTools.py:71
def __call__(self, process, names=None, outputInProcess=None, postfix=None)
Definition: HiCoreTools.py:211
def getDefaultParameters(self)
Definition: HiCoreTools.py:381
def jetCollectionString(prefix='', algo='', type='')
Definition: helpers.py:217
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:212
def toolCode(self, process)
Definition: HiCoreTools.py:80
def toolCode(self, process)
Definition: HiCoreTools.py:392
def __call__(self, process, names=None)
Definition: HiCoreTools.py:23