CMS 3D CMS Logo

earlyDeleteSettings_cff.py
Go to the documentation of this file.
1 # Abstract all early deletion settings here
2 
3 import collections
4 
5 import FWCore.ParameterSet.Config as cms
6 
7 from RecoTracker.Configuration.customiseEarlyDeleteForSeeding import customiseEarlyDeleteForSeeding
8 from RecoTracker.Configuration.customiseEarlyDeleteForMkFit import customiseEarlyDeleteForMkFit
9 from RecoTracker.Configuration.customiseEarlyDeleteForCKF import customiseEarlyDeleteForCKF
10 from CommonTools.ParticleFlow.Isolation.customiseEarlyDeleteForCandIsoDeposits import customiseEarlyDeleteForCandIsoDeposits
11 
12 def _hasInputTagModuleLabel(process, pset, psetModLabel, moduleLabels, result):
13  for name in pset.parameterNames_():
14  value = getattr(pset,name)
15  if isinstance(value, cms.PSet):
16  _hasInputTagModuleLabel(process, value, psetModLabel, moduleLabels, result)
17  elif isinstance(value, cms.VPSet):
18  for ps in value:
19  _hasInputTagModuleLabel(process, ps, psetModLabel, moduleLabels, result)
20  elif isinstance(value, cms.VInputTag):
21  for t in value:
22  t2 = t
23  if not isinstance(t, cms.InputTag):
24  t2 = cms.InputTag(t2)
25  for i,moduleLabel in enumerate(moduleLabels):
26  if result[i]: continue #no need
27  if t2.getModuleLabel() == moduleLabel:
28  result[i]=True
29  elif isinstance(value, cms.InputTag):
30  for i,moduleLabel in enumerate(moduleLabels):
31  if result[i]: continue #no need
32  if value.getModuleLabel() == moduleLabel:
33  result[i]=True
34  elif isinstance(value, cms.string) and name == "refToPSet_":
35  try:
36  ps = getattr(process, value.value())
37  except AttributeError:
38  raise RuntimeError("Module %s has a 'PSet(refToPSet_ = cms.string(\"%s\"))', but the referenced-to PSet does not exist in the Process." % (psetModLabel, value.value()))
39  _hasInputTagModuleLabel(process, ps, psetModLabel, moduleLabels, result)
40 
41 
42 def customiseEarlyDelete(process):
43  # Mapping label -> [branches]
44  # for the producers whose products are to be deleted early
45  products = collections.defaultdict(list)
46 
47  products = customiseEarlyDeleteForSeeding(process, products)
48  products = customiseEarlyDeleteForMkFit(process, products)
49  products = customiseEarlyDeleteForCKF(process, products)
50 
51  products = customiseEarlyDeleteForCandIsoDeposits(process, products)
52 
53  # Set process.options.canDeleteEarly
54  if not hasattr(process.options, "canDeleteEarly"):
55  process.options.canDeleteEarly = cms.untracked.vstring()
56 
57  branchSet = set()
58  for branches in products.values():
59  for branch in branches:
60  branchSet.add(branch)
61  branchList = sorted(branchSet)
62  process.options.canDeleteEarly.extend(branchList)
63 
64  # LogErrorHarvester should not wait for deleted items
65  for prod in process.producers_().values():
66  if prod.type_() == "LogErrorHarvester":
67  if not hasattr(prod,'excludeModules'):
68  prod.excludeModules = cms.untracked.vstring()
69  t = prod.excludeModules.value()
70  t.extend([b.split('_')[1] for b in branchList])
71  prod.excludeModules = t
72 
73  # Find the consumers
74  producers=[]
75  branchesList=[]
76  for producer, branches in products.items():
77  producers.append(producer)
78  branchesList.append(branches)
79 
80  for moduleType in [process.producers_(), process.filters_(), process.analyzers_()]:
81  for name, module in moduleType.items():
82  result=[]
83  for producer in producers:
84  result.append(False)
85 
86  _hasInputTagModuleLabel(process, module, name, producers, result)
87  for i in range(len(result)):
88  if result[i]:
89  #if it exists it might be optional or empty, both evaluate to False
90  if hasattr(module, "mightGet") and module.mightGet:
91  module.mightGet.extend(branchesList[i])
92  else:
93  module.mightGet = cms.untracked.vstring(branchesList[i])
94  return process
95 
96 
97 if __name__=="__main__":
98  import unittest
99 
100  class TestHasInputTagModuleLabel(unittest.TestCase):
101  def setUp(self):
102  """Nothing to do """
103  None
105  p = cms.Process("A")
106  p.pset = cms.PSet(a=cms.InputTag("a"),a2=cms.untracked.InputTag("a2"))
107  p.prod = cms.EDProducer("Producer",
108  foo = cms.InputTag("foo"),
109  foo2 = cms.InputTag("foo2", "instance"),
110  foo3 = cms.InputTag("foo3", "instance", "PROCESS"),
111  foo4 = cms.untracked.InputTag("foo4"),
112  nested = cms.PSet(
113  bar = cms.InputTag("bar"),
114  bar2 = cms.untracked.InputTag("bar2"),
115  ),
116  nested2 = cms.untracked.PSet(
117  bar3 = cms.untracked.InputTag("bar3"),
118  ),
119  flintstones = cms.VPSet(
120  cms.PSet(fred=cms.InputTag("fred")),
121  cms.PSet(wilma=cms.InputTag("wilma"))
122  ),
123  flintstones2 = cms.VPSet(
124  cms.PSet(fred2=cms.untracked.InputTag("fred2")),
125  cms.PSet(wilma2=cms.InputTag("wilma2"))
126  ),
127  ref = cms.PSet(
128  refToPSet_ = cms.string("pset")
129  ),
130  ref2 = cms.untracked.PSet(
131  refToPSet_ = cms.string("pset")
132  ),
133  )
134  p.prod2 = cms.EDProducer("Producer2",
135  foo = cms.PSet(
136  refToPSet_ = cms.string("nonexistent")
137  )
138  )
139 
140  result=[False,False,False,False,False,False,False,False,False,False,False,False,False,False]
141  _hasInputTagModuleLabel(p, p.prod, "prod", ["foo","foo2","foo3","bar","fred","wilma","a","foo4","bar2","bar3","fred2","wilma2","a2","joe"], result)
142  for i in range (0,13):
143  self.assert_(result[i])
144  self.assert_(not result[13])
145 
146  result = [False]
147  self.assertRaises(RuntimeError, _hasInputTagModuleLabel, p, p.prod2, "prod2", ["foo"], result)
148 
149  unittest.main()
def _hasInputTagModuleLabel(process, pset, psetModLabel, moduleLabels, result)