CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
customizeHLTforNewJetCorrectors.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 from FWCore.ParameterSet.Mixins import _ParameterTypeBase
3 
4 _oldToNewProds = {'CaloJetCorrectionProducer':'CorrectedCaloJetProducer','PFJetCorrectionProducer':'CorrectedPFJetProducer'}
5 
6 def _findModulesToChange(producers, prodsToFind):
7  """Based on the type, find the labels for modules we need to change
8  """
9  modulesToChange = []
10  for label,mod in producers.iteritems():
11  if mod.type_() in prodsToFind:
12  modulesToChange.append(label)
13  return modulesToChange
14 
15 def _findCorrectorsUsedByProducer(process,producer):
16  """Starting from a EDProducer, find which old jet correctors are used
17  """
18  correctors =[]
19  for x in producer.correctors:
20  _findCorrectorsRecursive(process,x,correctors)
21  return correctors
22 
23 def _findCorrectorsRecursive(process, name, correctors):
24  """Find all Correctors used by the corrector 'name'
25  """
26  m=getattr(process,name)
27  if m.type_() == 'JetCorrectionESChain':
28  for n in m.correctors:
29  _findCorrectorsRecursive(process,n,correctors)
30  #we want all the dependent modules first
31  correctors.append(name)
32 
33 def _correctorNameChanger(oldName):
34  """Based on the label from the old correct, determine what label to use for the new corrector
35  """
36  post1 = "CorrectionESProducer"
37  if oldName[-1*len(post1):] == post1:
38  return oldName[:-1*len(post1)]+"Corrector"
39  post2 = "Correction"
40  if oldName[-1*len(post2):] == post2:
41  return oldName[:-1*len(post2)]+"Corrector"
42  return None
43 
44 def _translateParameters(oldModule, newModule):
45  """Appropriately transform the parameters from the old module to the new module.
46  This includes changing 'correctors' from a vstring to a VInputTag
47  """
48  for n in dir(oldModule):
49  p = getattr(oldModule,n)
50  if isinstance(p,cms._ParameterTypeBase):
51  if n == 'appendToDataLabel':
52  continue
53  if n == "correctors" and isinstance(p,cms.vstring):
54  p = cms.VInputTag( (cms.InputTag( _correctorNameChanger(tag) ) for tag in p))
55  setattr(newModule,n,p)
56  return newModule
57 
58 
59 def _makeNewCorrectorFromOld(oldCorrector):
60  """Based on the old ESProducer corrector, create the equivalent EDProducer
61  """
62  oldToNewCorrectors_ = {'JetCorrectionESChain':'ChainedJetCorrectorProducer', 'LXXXCorrectionESProducer':'LXXXCorrectorProducer', 'L1FastjetCorrectionESProducer':'L1FastjetCorrectorProducer'}
63  type = oldToNewCorrectors_[oldCorrector.type_()]
64  corrector = cms.EDProducer(type)
65  return _translateParameters(oldCorrector,corrector)
66 
67 def _makeNewProducerFroOld(oldProducer):
68  """Based on the old EDProducer which used a corrector from the EventSetup, create the appropriate EDProducer which gets the corrector from the Event.
69  """
70  type = _oldToNewProds[oldProducer.type_()]
71  newProd = cms.EDProducer(type)
72  return _translateParameters(oldProducer,newProd)
73 
74 def _buildSequenceOfCorrectors(process, correctorNames):
75  """Using the dependencies between correctors, construct the appropriate cms.Sequence.
76  """
77  modSequence = None
78  for n in correctorNames:
79  mod = getattr(process,n)
80  newLabel = _correctorNameChanger(n)
81  newMod = _makeNewCorrectorFromOld(mod)
82  setattr(process,newLabel,newMod)
83  if not modSequence:
84  modSequence = newMod
85  else:
86  modSequence += newMod
87  return cms.Sequence(modSequence)
88 
90  modulesToChange = _findModulesToChange(process.producers, [x for x in _oldToNewProds.iterkeys()])
91 
92 
93  oldCorrectorsSequence = set()
94  oldCorrectors = set()
95  oldCorrectorsToNewSequence = {}
96  for m in modulesToChange:
97  correctors = _findCorrectorsUsedByProducer(process, getattr(process,m))
98  oldCorrectors.update( correctors )
99  stringOfOldCorrectorNames = ",".join(correctors)
100  if stringOfOldCorrectorNames not in oldCorrectorsSequence:
101  seq = _buildSequenceOfCorrectors(process,correctors)
102  #need to attach to process since some code looks for labels for all items in a path
103  setattr(process,"correctorSeqFor"+_correctorNameChanger(correctors[-1]), seq)
104  oldCorrectorsSequence.add(stringOfOldCorrectorNames)
105  oldCorrectorsToNewSequence[stringOfOldCorrectorNames] = seq
106 
107  #replace the old module
108  oldModule = getattr(process,m)
109  newModule = _makeNewProducerFroOld(oldModule)
110  setattr(process,m,newModule)
111 
112  #need to insert the new sequence
113  seq = oldCorrectorsToNewSequence[stringOfOldCorrectorNames]
114  for p in process.paths.itervalues():
115  if m in p.moduleNames():
116  if not p.replace(newModule,seq+newModule):
117  print "failed to replace ",m, "in path ", p.label_(), p
118 
119  #now remove the old correctors
120  for m in oldCorrectors:
121  delattr(process,m)
122 
123  return process
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
dbl *** dir
Definition: mlp_gen.cc:35