CMS 3D CMS Logo

customisers.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 ### Various set of customise functions needed for embedding
5 import FWCore.ParameterSet.Config as cms
6 from FWCore.ParameterSet.Utilities import cleanUnscheduled
7 
8 
9 ################################ Customizer for skimming ###########################
10 ### There are four different parts.
11 ##First step is the SELECT (former SKIM) part, where we identfy the events which are good for Embedding. Need to store RAWRECO [RAW is needed for the MERG and RECO for the CLEAN step]
12 ##Second step is the CLEAN input are PAT muons with RECO information. After this step only RAW plus some special collections needed for the MERG step must be saved
13 ##Third step is the SIM. The input is the externalLHEProducer, which must be produced before (At the moment we do it parallel to the CLEAN step). Again only save RAW of the SELECT and only save what is need for the MERG step
14 ##Last step is the MERG step. Which is the usally reconstruction, where the input produces are replaced by merg producer, which mix the SIM and CLEAN inputs.
15 
16 ## Some comments on this approach. All steps runs the RECO sequence until the end in the moment. It would be possible to stop after the all inputs which are needed for the MERG step are generated (which happens at a very early state of the reconstruction. But with this approach we are able to get the RECO (and PAT aka miniAOD) of all four step SELECT (orginal event), SIM, CLEAN and MERGED. Therefor one only needs to SAVE the corresponding output (in cmsDriver change output to RAW -> RAW,RECO,PAT)
17 
18 ####################### Some basic functions ####################
19 ## Helper Class, which summerizes in which step which Producer (Cleaner Merger), should be loaded. It is also usefull to define which collection should be stored for the next step
20 ## E.g What is needed for MERGE must be produce in the CLEAN and SIM step
21 
22 
24  def __init__(self, module_name, manipulator_name, steps = ["SELECT","CLEAN","SIM","MERGE"], instance=[""], merge_prefix = ""):
25  self.module_name = module_name
26  self.manipulator_name = manipulator_name
27  self.steps = steps
28  self.instance = instance
29  self.merger_name = manipulator_name+"ColMerger"
30  self.cleaner_name = manipulator_name+"ColCleaner"
31  self.merge_prefix = merge_prefix
32 
33 
34 
35 
36 to_bemanipulate = []
37 
38 
39 to_bemanipulate.append(module_manipulate(module_name = 'siPixelClusters', manipulator_name = "Pixel", steps = ["SELECT","CLEAN"] ))
40 to_bemanipulate.append(module_manipulate(module_name = 'siStripClusters', manipulator_name = "Strip", steps = ["SELECT","CLEAN"] ))
41 
42 to_bemanipulate.append(module_manipulate(module_name = 'generalTracks', manipulator_name = "Track", steps = ["SIM", "MERGE"]))
43 to_bemanipulate.append(module_manipulate(module_name = 'muons1stStep', manipulator_name = "Muon", steps = ["SIM", "MERGE"]))
44 to_bemanipulate.append(module_manipulate(module_name = 'gedGsfElectronsTmp', manipulator_name = "GsfElectron", steps = ["SIM", "MERGE"]))
45 to_bemanipulate.append(module_manipulate(module_name = 'gedPhotonsTmp', manipulator_name = "Photon", steps = ["SIM", "MERGE"]))
46 to_bemanipulate.append(module_manipulate(module_name = 'particleFlowTmp', manipulator_name = "PF", steps = ["SIM", "MERGE"], instance=["","CleanedHF","CleanedCosmicsMuons","CleanedTrackerAndGlobalMuons","CleanedFakeMuons","CleanedPunchThroughMuons","CleanedPunchThroughNeutralHadrons","AddedMuonsAndHadrons"]))
47 
48 
49 to_bemanipulate.append(module_manipulate(module_name = 'ecalRecHit', manipulator_name = "EcalRecHit", instance= ["EcalRecHitsEB","EcalRecHitsEE"]))
50 to_bemanipulate.append(module_manipulate(module_name = 'ecalPreshowerRecHit', manipulator_name = "EcalRecHit", instance= ["EcalRecHitsES"]))
51 
52 to_bemanipulate.append(module_manipulate(module_name = 'hbheprereco', manipulator_name = "HBHERecHit"))
53 to_bemanipulate.append(module_manipulate(module_name = 'hbhereco', manipulator_name = "HBHERecHit"))
54 to_bemanipulate.append(module_manipulate(module_name = 'zdcreco', manipulator_name = "ZDCRecHit"))
55 
56 to_bemanipulate.append(module_manipulate(module_name = 'horeco', manipulator_name = "HORecHit"))
57 to_bemanipulate.append(module_manipulate(module_name = 'hfreco', manipulator_name = "HFRecHit"))
58 to_bemanipulate.append(module_manipulate(module_name = 'castorreco', manipulator_name = "CastorRecHit"))
59 
60 
61 to_bemanipulate.append(module_manipulate(module_name = 'dt1DRecHits', manipulator_name = "DTRecHit", steps = ["SELECT","CLEAN"] ))
62 to_bemanipulate.append(module_manipulate(module_name = 'dt1DCosmicRecHits', manipulator_name = "DTRecHit", steps = ["SELECT","CLEAN"] ))
63 
64 to_bemanipulate.append(module_manipulate(module_name = 'csc2DRecHits', manipulator_name = "CSCRecHit", steps = ["SELECT","CLEAN"] ))
65 to_bemanipulate.append(module_manipulate(module_name = 'rpcRecHits', manipulator_name = "RPCRecHit", steps = ["SELECT","CLEAN"] ))
66 
67 
68 def modify_outputModules(process, keep_drop_list = [], module_veto_list = [] ):
69  outputModulesList = [key for key,value in process.outputModules.iteritems()]
70  for outputModule in outputModulesList:
71  if outputModule in module_veto_list:
72  continue
73  outputModule = getattr(process, outputModule)
74  for add_element in keep_drop_list:
75  outputModule.outputCommands.extend(add_element)
76  return process
77 
78 
79 
80 ################################ Customizer for Selecting ###########################
81 
82 def keepSelected(dataTier):
83  ret_vstring = cms.untracked.vstring(
84  # "drop *_*_*_"+dataTier,
85  "keep *_patMuonsAfterID_*_"+dataTier,
86  "keep *_slimmedMuons_*_"+dataTier,
87  "keep *_selectedMuonsForEmbedding_*_"+dataTier,
88  "keep recoVertexs_offlineSlimmedPrimaryVertices_*_"+dataTier,
89  "keep *_firstStepPrimaryVertices_*_"+dataTier,
90  "keep *_offlineBeamSpot_*_"+dataTier
91  )
92  for akt_manimod in to_bemanipulate:
93  if "CLEAN" in akt_manimod.steps:
94  ret_vstring.append("keep *_"+akt_manimod.module_name+"_*_"+dataTier)
95  return ret_vstring
96 
97 def customiseSelecting(process,reselect=False):
98  if reselect:
99  process._Process__name = "RESELECT"
100  dataTier="RESELECT"
101  else:
102  process._Process__name = "SELECT"
103  dataTier="SELECT"
104 
105  process.load('TauAnalysis.MCEmbeddingTools.SelectingProcedure_cff')
106  process.patMuonsAfterKinCuts.src = cms.InputTag("slimmedMuons","",dataTier)
107  process.patMuonsAfterID = process.patMuonsAfterLooseID.clone()
108 
109  process.selecting = cms.Path(process.makePatMuonsZmumuSelection)
110  process.schedule.insert(-1, process.selecting)
111 
112  outputModulesList = [key for key,value in process.outputModules.iteritems()]
113  for outputModule in outputModulesList:
114  outputModule = getattr(process, outputModule)
115  outputModule.SelectEvents = cms.untracked.PSet(SelectEvents = cms.vstring("selecting"))
116  outputModule.outputCommands.extend(keepSelected(dataTier))
117 
118  process = customisoptions(process)
119  return modify_outputModules(process,[keepSelected(dataTier)])
120 
122  return customiseSelecting(process,reselect=True)
123 
124 ################################ Customizer for cleaining ###########################
126  ret_vstring = cms.untracked.vstring(
127 # "drop *_*_*_LHEembeddingCLEAN",
128 # "drop *_*_*_CLEAN"
129  )
130 
131  for akt_manimod in to_bemanipulate:
132  if "MERGE" in akt_manimod.steps:
133  ret_vstring.append("keep *_"+akt_manimod.module_name+"_*_LHEembeddingCLEAN")
134  ret_vstring.append("keep *_"+akt_manimod.module_name+"_*_CLEAN")
135  ret_vstring.append("keep *_standAloneMuons_*_LHEembeddingCLEAN")
136  ret_vstring.append("keep *_glbTrackQual_*_LHEembeddingCLEAN")
137  return ret_vstring
138 
139 
140 
141 def customiseCleaning(process, changeProcessname=True,reselect=False):
142  if changeProcessname:
143  process._Process__name = "CLEAN"
144  if reselect:
145  dataTier="RESELECT"
146  else:
147  dataTier="SELECT"
148  ## Needed for the Calo Cleaner, could also be put into a function wich fix the input parameters
149  from TrackingTools.TrackAssociator.default_cfi import TrackAssociatorParameterBlock
150  TrackAssociatorParameterBlock.TrackAssociatorParameters.CSCSegmentCollectionLabel = cms.InputTag("cscSegments","",dataTier)
151  TrackAssociatorParameterBlock.TrackAssociatorParameters.CaloTowerCollectionLabel = cms.InputTag("towerMaker","",dataTier)
152  TrackAssociatorParameterBlock.TrackAssociatorParameters.DTRecSegment4DCollectionLabel = cms.InputTag("dt4DSegments","",dataTier)
153  TrackAssociatorParameterBlock.TrackAssociatorParameters.EBRecHitCollectionLabel = cms.InputTag("ecalRecHit","EcalRecHitsEB",dataTier)
154  TrackAssociatorParameterBlock.TrackAssociatorParameters.EERecHitCollectionLabel = cms.InputTag("ecalRecHit","EcalRecHitsEE",dataTier)
155  TrackAssociatorParameterBlock.TrackAssociatorParameters.HBHERecHitCollectionLabel = cms.InputTag("hbhereco","",dataTier)
156  TrackAssociatorParameterBlock.TrackAssociatorParameters.HORecHitCollectionLabel = cms.InputTag("horeco","",dataTier)
157 
158 
159  MuonImput = cms.InputTag("selectedMuonsForEmbedding","","") ## This are the muon
160  for akt_manimod in to_bemanipulate:
161  if "CLEAN" in akt_manimod.steps:
162  oldCollections_in = cms.VInputTag()
163  for instance in akt_manimod.instance:
164  oldCollections_in.append(cms.InputTag(akt_manimod.module_name,instance,dataTier))
165  setattr(process, akt_manimod.module_name, cms.EDProducer(akt_manimod.cleaner_name,MuonCollection = MuonImput,TrackAssociatorParameters = TrackAssociatorParameterBlock.TrackAssociatorParameters,oldCollection = oldCollections_in))
166  process.ecalPreshowerRecHit.TrackAssociatorParameters.usePreshower = cms.bool(True)
167  process = customisoptions(process)
168  return modify_outputModules(process,[keepSelected(dataTier),keepCleaned()],["MINIAODoutput"])
169 
170 
171 ################################ Customizer for simulaton ###########################
172 def keepLHE():
173  ret_vstring = cms.untracked.vstring()
174  ret_vstring.append("keep *_externalLHEProducer_*_LHEembedding")
175  ret_vstring.append("keep *_externalLHEProducer_*_LHEembeddingCLEAN")
176  return ret_vstring
177 
178 
180  ret_vstring = cms.untracked.vstring()
181  for akt_manimod in to_bemanipulate:
182  if "MERGE" in akt_manimod.steps:
183  ret_vstring.append("keep *_"+akt_manimod.module_name+"_*_SIMembedding")
184  ret_vstring.append("keep *_genParticles_*_SIMembedding")
185  ret_vstring.append("keep *_standAloneMuons_*_SIMembedding")
186  ret_vstring.append("keep *_glbTrackQual_*_SIMembedding")
187  ret_vstring.append("keep *_generator_*_SIMembedding")
188  return ret_vstring
189 
190 
191 
192 
193 def customiseLHE(process, changeProcessname=True,reselect=False):
194  if reselect:
195  dataTier="RESELECT"
196  else:
197  dataTier="SELECT"
198  if changeProcessname:
199  process._Process__name = "LHEembedding"
200  process.load('TauAnalysis.MCEmbeddingTools.EmbeddingLHEProducer_cfi')
201  if reselect:
202  process.externalLHEProducer.vertices=cms.InputTag("offlineSlimmedPrimaryVertices","","RESELECT")
203  process.lheproduction = cms.Path(process.makeexternalLHEProducer)
204  process.schedule.insert(0,process.lheproduction)
205 
206 
207  process = customisoptions(process)
208  return modify_outputModules(process,[keepSelected(dataTier),keepCleaned(), keepLHE()],["MINIAODoutput"])
209 
210 
211 def customiseGenerator(process, changeProcessname=True,reselect=False):
212  if reselect:
213  dataTier="RESELECT"
214  else:
215  dataTier="SELECT"
216  if changeProcessname:
217  process._Process__name = "SIMembedding"
218 
219  ## here correct the vertex collection
220 
221  process.load('TauAnalysis.MCEmbeddingTools.EmbeddingVertexCorrector_cfi')
222  process.VtxSmeared = process.VtxCorrectedToInput.clone()
223  print "Correcting Vertex in genEvent to one from input. Replaced 'VtxSmeared' with the Corrector."
224 
225  # Remove BeamSpot Production, use the one from selected data instead.
226  process.reconstruction.remove(process.offlineBeamSpot)
227 
228  # Disable noise simulation
229  process.mix.digitizers.castor.doNoise = cms.bool(False)
230 
231  process.mix.digitizers.ecal.doESNoise = cms.bool(False)
232  process.mix.digitizers.ecal.doENoise = cms.bool(False)
233 
234  process.mix.digitizers.hcal.doNoise = cms.bool(False)
235  process.mix.digitizers.hcal.doThermalNoise = cms.bool(False)
236  process.mix.digitizers.hcal.doHPDNoise = cms.bool(False)
237 
238  process.mix.digitizers.pixel.AddNoisyPixels = cms.bool(False)
239  process.mix.digitizers.pixel.AddNoise = cms.bool(False)
240 
241  process.mix.digitizers.strip.Noise = cms.bool(False)
242 
243 
244  process = customisoptions(process)
245  ##process = fix_input_tags(process)
246 
247  return modify_outputModules(process,[keepSelected(dataTier),keepCleaned(),keepSimulated()],["AODSIMoutput"])
248 
250  return customiseGenerator(process,reselect=True)
251 
252 ################################ Customizer for merging ###########################
253 def keepMerged(dataTier="SELECT"):
254  ret_vstring = cms.untracked.vstring()
255  ret_vstring.append("drop *_*_*_"+dataTier)
256  ret_vstring.append("keep *_generator_*_SIMembedding")
257  return ret_vstring
258 
259 
260 
261 def customiseMerging(process, changeProcessname=True,reselect=False):
262  if changeProcessname:
263  process._Process__name = "MERGE"
264  if reselect:
265  dataTier="RESELECT"
266  else:
267  dataTier="SELECT"
268 
269 
270  process.source.inputCommands = cms.untracked.vstring()
271  process.source.inputCommands.append("keep *_*_*_*")
272 
273  #process.source.inputCommands.append("drop *_*_*_SELECT")
274  #process.source.inputCommands.append("drop *_*_*_SIMembedding")
275  #process.source.inputCommands.append("drop *_*_*_LHEembeddingCLEAN")
276  #process.source.inputCommands.extend(keepSimulated())
277  #process.source.inputCommands.extend(keepCleaned())
278 
279  process.load('Configuration.StandardSequences.Reconstruction_Data_cff')
280  process.merge_step = cms.Path()
281 
282 
283  for akt_manimod in to_bemanipulate:
284  if "MERGE" in akt_manimod.steps:
285  #if akt_manimod.module_name != 'particleFlowTmp':
286  # continue
287  print akt_manimod.module_name
288  mergCollections_in = cms.VInputTag()
289  for instance in akt_manimod.instance:
290  mergCollections_in.append(cms.InputTag(akt_manimod.merge_prefix+akt_manimod.module_name,instance,"SIMembedding"))
291  mergCollections_in.append(cms.InputTag(akt_manimod.merge_prefix+akt_manimod.module_name,instance,"LHEembeddingCLEAN"))## Mayb make some process history magic which finds out if it was CLEAN or LHEembeddingCLEAN step
292  setattr(process, akt_manimod.module_name, cms.EDProducer(akt_manimod.merger_name,
293  mergCollections = mergCollections_in
294  )
295  )
296  process.merge_step +=getattr(process, akt_manimod.module_name)
297 
298 
299  process.merge_step += process.vertexreco
300  process.unsortedOfflinePrimaryVertices.beamSpotLabel = cms.InputTag("offlineBeamSpot","",dataTier)
301  process.ak4CaloJetsForTrk.srcPVs = cms.InputTag("firstStepPrimaryVertices","",dataTier)
302 
303  process.muons.FillDetectorBasedIsolation = cms.bool(False)
304  process.muons.FillSelectorMaps = cms.bool(False)
305  process.muons.FillShoweringInfo = cms.bool(False)
306  process.muons.FillCosmicsIdMap = cms.bool(False)
307 
308 
309  process.merge_step += process.highlevelreco
310 
311  #process.merge_step.remove(process.reducedEcalRecHitsEE)
312  #process.merge_step.remove(process.reducedEcalRecHitsEB)
313 
314  process.merge_step.remove(process.ak4JetTracksAssociatorExplicit)
315 
316  process.merge_step.remove(process.pfTrack)
317  process.merge_step.remove(process.pfConversions)
318  process.merge_step.remove(process.pfV0)
319  process.merge_step.remove(process.particleFlowDisplacedVertexCandidate)
320  process.merge_step.remove(process.particleFlowDisplacedVertex)
321  process.merge_step.remove(process.pfDisplacedTrackerVertex)
322  process.merge_step.remove(process.pfTrackElec)
323  process.merge_step.remove(process.electronsWithPresel)
324  process.merge_step.remove(process.mvaElectrons)
325  process.merge_step.remove(process.particleFlowBlock)
326  process.merge_step.remove(process.particleFlowEGamma)
327  process.merge_step.remove(process.gedGsfElectronCores)
328  # process.merge_step.remove(process.gedGsfElectronsTmp)
329  process.merge_step.remove(process.gedPhotonCore)
330  process.merge_step.remove(process.ecalDrivenGsfElectronCores)
331  process.merge_step.remove(process.ecalDrivenGsfElectrons)
332  process.merge_step.remove(process.uncleanedOnlyElectronSeeds)
333  process.merge_step.remove(process.uncleanedOnlyAllConversions)
334  process.merge_step.remove(process.uncleanedOnlyPfTrack)
335  process.merge_step.remove(process.uncleanedOnlyPfTrackElec)
336  process.merge_step.remove(process.uncleanedOnlyGsfElectrons)
337  process.merge_step.remove(process.uncleanedOnlyElectronCkfTrackCandidates)
338  process.merge_step.remove(process.cosmicsVeto)
339  process.merge_step.remove(process.cosmicsVetoTrackCandidates)
340  # process.merge_step.remove(process.ecalDrivenGsfElectronCores)
341  # process.merge_step.remove(process.ecalDrivenGsfElectrons)
342  # process.merge_step.remove(process.gedPhotonsTmp)
343  # process.merge_step.remove(process.particleFlowTmp)
344  process.merge_step.remove(process.hcalnoise)
345 
346  process.load('CommonTools.ParticleFlow.genForPF2PAT_cff')
347 
348  process.merge_step += process.genForPF2PATSequence
349 
350  process.schedule.insert(0,process.merge_step)
351  # process.load('PhysicsTools.PatAlgos.slimming.slimmedGenJets_cfi')
352 
353  process = customisoptions(process)
354  return modify_outputModules(process, [keepMerged(dataTier)])
355 
356 def customiseMerging_Reselect(process, changeProcessname=True):
357  return customiseMerging(process, changeProcessname=changeProcessname, reselect=True)
358 
359 ################################ cross Customizers ###########################
360 
361 def customiseLHEandCleaning(process,reselect=False):
362  process._Process__name = "LHEembeddingCLEAN"
363  process = customiseCleaning(process,changeProcessname=False,reselect=reselect)
364  process = customiseLHE(process,changeProcessname=False,reselect=reselect)
365  return process
366 
368  return customiseLHEandCleaning(process,reselect=True)
369 
370 ################################ additionla Customizer ###########################
371 
372 def customisoptions(process):
373  if not hasattr(process, "options"):
374  process.options = cms.untracked.PSet()
375  process.options.emptyRunLumiMode = cms.untracked.string('doNotHandleEmptyRunsAndLumis')
376  return process
377 
378 ############################### MC specific Customizer ###########################
379 
381  process.load("TauAnalysis.MCEmbeddingTools.DYToMuMuGenFilter_cfi")
382  process.ZToMuMuFilter = cms.Path(process.dYToMuMuGenFilter)
383  process.schedule.insert(-1,process.ZToMuMuFilter)
384  return process
385 
386 
387 
388 def fix_input_tags(process, formodules = ["generalTracks","cscSegments","dt4DSegments","rpcRecHits"]):
389  def change_tags_process(test_input):
390  if isinstance(test_input, cms.InputTag):
391  if test_input.getModuleLabel() in formodules:
392  test_input.setProcessName(process._Process__name)
393 
394  def search_for_tags(pset):
395  if isinstance(pset, dict):
396  for key in pset:
397  if isinstance(pset[key], cms.VInputTag):
398  for akt_inputTag in pset[key]:
399  change_tags_process(akt_inputTag)
400  elif isinstance(pset[key], cms.PSet):
401  search_for_tags(pset[key].__dict__)
402  elif isinstance(pset[key], cms.VPSet):
403  for akt_pset in pset[key]:
404  search_for_tags(akt_pset.__dict__)
405  else:
406  change_tags_process(pset[key])
407  else:
408  print "must be python dict not a ",type(pset)
409 
410  for module in process.producers_():
411  search_for_tags(getattr(process, module).__dict__)
412  for module in process.filters_():
413  search_for_tags(getattr(process, module).__dict__)
414  for module in process.analyzers_():
415  search_for_tags(getattr(process, module).__dict__)
416 
417  return process
418 
419 
420 
421 
422 
423 
424 
def customiseSelecting(process, reselect=False)
Definition: customisers.py:97
def customiseLHEandCleaning_Reselect(process)
Definition: customisers.py:367
def customiseSelecting_Reselect(process)
Definition: customisers.py:121
def keepCleaned()
Customizer for cleaining ###########################.
Definition: customisers.py:125
def customiseGenerator(process, changeProcessname=True, reselect=False)
Definition: customisers.py:211
Customizer for skimming ########################### There are four different parts.
Definition: customisers.py:23
def keepLHE()
Customizer for simulaton ###########################.
Definition: customisers.py:172
def keepSimulated()
Definition: customisers.py:179
def keepMerged(dataTier="SELECT")
Customizer for merging ###########################.
Definition: customisers.py:253
def customiseFilterZToMuMu(process)
MC specific Customizer ###########################.
Definition: customisers.py:380
def customiseMerging(process, changeProcessname=True, reselect=False)
Definition: customisers.py:261
def fix_input_tags(process, formodules=["generalTracks", cscSegments, dt4DSegments, rpcRecHits)
Definition: customisers.py:388
def customiseMerging_Reselect(process, changeProcessname=True)
Definition: customisers.py:356
def customiseLHEandCleaning(process, reselect=False)
cross Customizers ###########################
Definition: customisers.py:361
def __init__(self, module_name, manipulator_name, steps=["SELECT", CLEAN, SIM, MERGE, instance=[""], merge_prefix="")
Definition: customisers.py:24
def customisoptions(process)
additionla Customizer ###########################
Definition: customisers.py:372
def modify_outputModules(process, keep_drop_list=[], module_veto_list=[])
Definition: customisers.py:68
def customiseLHE(process, changeProcessname=True, reselect=False)
Definition: customisers.py:193
def keepSelected(dataTier)
Customizer for Selecting ###########################.
Definition: customisers.py:82
def customiseGenerator_Reselect(process)
Definition: customisers.py:249
def customiseCleaning(process, changeProcessname=True, reselect=False)
Definition: customisers.py:141