CMS 3D CMS Logo

vid_id_tools.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
3 from PhysicsTools.SelectorUtils.centralIDRegistry import central_id_registry
4 
5 import sys
6 
7 #keep python2.6 compatibility for computing
8 #import importlib
9 
10 #general simple tools for various object types
11 def setupVIDSelection(vidproducer,cutflow):
12  if not hasattr(cutflow,'idName'):
13  raise Exception('InvalidVIDCutFlow', 'The cutflow configuation provided is malformed and does not have a specified name!')
14  if not hasattr(cutflow,'cutFlow'):
15  raise Exception('InvalidVIDCutFlow', 'The cutflow configuration provided is malformed and does not have a specific cutflow!')
16  cutflow_md5 = central_id_registry.getMD5FromName(cutflow.idName)
17  isPOGApproved = False
18  if hasattr(cutflow,'isPOGApproved'):
19  isPOGApproved = cutflow.isPOGApproved.value()
20  vidproducer.physicsObjectIDs.append(
21  cms.PSet( idDefinition = cutflow,
22  isPOGApproved = cms.untracked.bool(isPOGApproved),
23  idMD5 = cms.string(cutflow_md5) )
24  )
25 # sys.stderr.write('Added ID \'%s\' to %s\n'%(cutflow.idName.value(),vidproducer.label()))
26 
27 def addVIDSelectionToPATProducer(patProducer,idProducer,idName,addUserData=True):
28  patProducerIDs = None
29  userDatas = None
30  for key in patProducer.__dict__.keys():
31  if 'IDSources' in key:
32  patProducerIDs = getattr(patProducer,key)
33  if 'userData' in key:
34  userDatas = getattr(patProducer,key)
35  if patProducerIDs is None:
36  raise Exception('StrangePatModule','%s does not have ID sources!'%patProducer.label())
37  if userDatas is None:
38  raise Exception('StrangePatModule','%s does not have UserData sources!'%patProducer.label())
39  setattr(patProducerIDs,idName,cms.InputTag('%s:%s'%(idProducer,idName)))
40  if( addUserData ):
41  if( len(userDatas.userClasses.src) == 1 and
42  type(userDatas.userClasses.src[0]) is str and
43  userDatas.userClasses.src[0] == '' ):
44  userDatas.userClasses.src = cms.VInputTag(cms.InputTag('%s:%s'%(idProducer,idName)))
45  else:
46  userDatas.userClasses.src.append(cms.InputTag('%s:%s'%(idProducer,idName)))
47  sys.stderr.write('\t--- %s:%s added to %s\n'%(idProducer,idName,patProducer.label()))
48 
49 def setupAllVIDIdsInModule(process,id_module_name,setupFunction,patProducer=None,addUserData=True,task=None):
50 # idmod = importlib.import_module(id_module_name)
51  idmod= __import__(id_module_name, globals(), locals(), ['idName','cutFlow'])
52  for name in dir(idmod):
53  item = getattr(idmod,name)
54  if hasattr(item,'idName') and hasattr(item,'cutFlow'):
55  setupFunction(process,item,patProducer,addUserData,task)
56 
57 # Supported data formats defined via "enum"
58 from PhysicsTools.SelectorUtils.tools.DataFormat import DataFormat
59 
60 ####
61 # Electrons
62 ####
63 
64 #turns on the VID electron ID producer, possibly with extra options
65 # for PAT and/or MINIAOD
66 def switchOnVIDElectronIdProducer(process, dataFormat, task=None):
67  process.load('RecoEgamma.ElectronIdentification.egmGsfElectronIDs_cff')
68  if task is not None:
69  task.add(process.egmGsfElectronIDTask)
70  #*always* reset to an empty configuration
71  if( len(process.egmGsfElectronIDs.physicsObjectIDs) > 0 ):
72  process.egmGsfElectronIDs.physicsObjectIDs = cms.VPSet()
73  dataFormatString = "Undefined"
74  if dataFormat == DataFormat.AOD:
75  # No reconfiguration is required, default settings are for AOD
76  dataFormatString = "AOD"
77  elif dataFormat == DataFormat.MiniAOD:
78  # If we are dealing with MiniAOD, we overwrite the electron collection
79  # name appropriately, for the fragment we just loaded above.
80  process.egmGsfElectronIDs.physicsObjectSrc = cms.InputTag('slimmedElectrons')
81  dataFormatString = "MiniAOD"
82  else:
83  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
84  #
85 # sys.stderr.write('Added \'egmGsfElectronIDs\' to process definition (%s format)!\n' % dataFormatString)
86 
87 def setupVIDElectronSelection(process,cutflow,patProducer=None,addUserData=True,task=None):
88  if not hasattr(process,'egmGsfElectronIDs'):
89  raise Exception('VIDProducerNotAvailable','egmGsfElectronIDs producer not available in process!')
90  setupVIDSelection(process.egmGsfElectronIDs,cutflow)
91  #add to PAT electron producer if available or specified
92  if hasattr(process,'patElectrons') or patProducer is not None:
93  if patProducer is None:
94  patProducer = process.patElectrons
95  idName = cutflow.idName.value()
96  addVIDSelectionToPATProducer(patProducer,'egmGsfElectronIDs',idName,addUserData)
97 
98  #we know cutflow has a name otherwise an exception would have been thrown in setupVIDSelection
99  #run this for all heep IDs except V60 standard for which it is not needed
100  #it is needed for V61 and V70
101  if (cutflow.idName.value().find("HEEP")!=-1 and
102  cutflow.idName.value()!="heepElectronID-HEEPV60"):
103 
104  #not the ideal way but currently the easiest
105  useMiniAOD = process.egmGsfElectronIDs.physicsObjectSrc == cms.InputTag('slimmedElectrons')
106 
108  addHEEPProducersToSeq(process=process,seq=process.egmGsfElectronIDSequence,
109  useMiniAOD=useMiniAOD,task=task)
110 
111 ####
112 # Muons
113 ####
114 
115 #turns on the VID electron ID producer, possibly with extra options
116 # for PAT and/or MINIAOD
117 def switchOnVIDMuonIdProducer(process, dataFormat, task=None):
118  process.load('RecoMuon.MuonIdentification.muoMuonIDs_cff')
119  if task is not None:
120  task.add(process.muoMuonIDTask)
121  #*always* reset to an empty configuration
122  if( len(process.muoMuonIDs.physicsObjectIDs) > 0 ):
123  process.muoMuonIDs.physicsObjectIDs = cms.VPSet()
124  dataFormatString = "Undefined"
125  if dataFormat == DataFormat.AOD:
126  # No reconfiguration is required, default settings are for AOD
127  dataFormatString = "AOD"
128  elif dataFormat == DataFormat.MiniAOD:
129  # If we are dealing with MiniAOD, we overwrite the muon collection
130  # name appropriately, for the fragment we just loaded above.
131  process.muoMuonIDs.physicsObjectSrc = cms.InputTag('slimmedMuons')
132  dataFormatString = "MiniAOD"
133  else:
134  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
135  #
136 # sys.stderr.write('Added \'muoMuonIDs\' to process definition (%s format)!\n' % dataFormatString)
137 
138 def setupVIDMuonSelection(process,cutflow,patProducer=None):
139  moduleName = "muoMuonIDs"
140  if not hasattr(process, moduleName):
141  raise Exception("VIDProducerNotAvailable", "%s producer not available in process!" % moduleName)
142  module = getattr(process, moduleName)
143 
144  setupVIDSelection(module, cutflow)
145  #add to PAT electron producer if available or specified
146  #if hasattr(process,'patMuons') or patProducer is not None:
147  # if patProducer is None:
148  # patProducer = process.patMuons
149  # idName = cutflow.idName.value()
150  # addVIDSelectionToPATProducer(patProducer, moduleName, idName)
151 
152 ####
153 # Photons
154 ####
155 
156 #turns on the VID photon ID producer, possibly with extra options
157 # for PAT and/or MINIAOD
158 def switchOnVIDPhotonIdProducer(process, dataFormat, task=None):
159  from RecoEgamma.PhotonIdentification.egmPhotonIDs_cff import loadEgmIdSequence
160  # Set up the ID task and sequence appropriate for this data format
161  loadEgmIdSequence(process,dataFormat)
162  if task is not None:
163  task.add(process.egmPhotonIDTask)
164  #*always* reset to an empty configuration
165  if( len(process.egmPhotonIDs.physicsObjectIDs) > 0 ):
166  process.egmPhotonIDs.physicsObjectIDs = cms.VPSet()
167  dataFormatString = "Undefined"
168  if dataFormat == DataFormat.AOD:
169  # No reconfiguration is required, default settings are for AOD
170  dataFormatString = "AOD"
171  elif dataFormat == DataFormat.MiniAOD:
172  # If we are dealing with MiniAOD, we overwrite the electron collection
173  # name appropriately, for the fragment we just loaded above.
174  process.egmPhotonIDs.physicsObjectSrc = cms.InputTag('slimmedPhotons')
175  dataFormatString = "MiniAOD"
176  else:
177  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
178  #
179 # sys.stderr.write('Added \'egmPhotonIDs\' to process definition (%s format)!\n' % dataFormatString)
180 
181 def setupVIDPhotonSelection(process,cutflow,patProducer=None,addUserData=True,task=None):
182  if not hasattr(process,'egmPhotonIDs'):
183  raise Exception('VIDProducerNotAvailable','egmPhotonIDs producer not available in process!\n')
184  setupVIDSelection(process.egmPhotonIDs,cutflow)
185  #add to PAT photon producer if available or specified
186  if hasattr(process,'patPhotons') or patProducer is not None:
187  if patProducer is None:
188  patProducer = process.patPhotons
189  idName = cutflow.idName.value()
190  addVIDSelectionToPATProducer(patProducer,'egmPhotonIDs',idName,addUserData)
191 
192 
def addHEEPProducersToSeq(process, seq, useMiniAOD, task=None)
def switchOnVIDMuonIdProducer(process, dataFormat, task=None)
Muons.
def switchOnVIDElectronIdProducer(process, dataFormat, task=None)
Electrons.
Definition: vid_id_tools.py:66
def setupAllVIDIdsInModule(process, id_module_name, setupFunction, patProducer=None, addUserData=True, task=None)
Definition: vid_id_tools.py:49
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
def setupVIDSelection(vidproducer, cutflow)
Definition: vid_id_tools.py:11
def setupVIDElectronSelection(process, cutflow, patProducer=None, addUserData=True, task=None)
Definition: vid_id_tools.py:87
def loadEgmIdSequence(process, dataFormat)
def setupVIDMuonSelection(process, cutflow, patProducer=None)
if(dp >Float(M_PI)) dp-
def switchOnVIDPhotonIdProducer(process, dataFormat, task=None)
Photons.
dbl *** dir
Definition: mlp_gen.cc:35
def addVIDSelectionToPATProducer(patProducer, idProducer, idName, addUserData=True)
Definition: vid_id_tools.py:27
def setupVIDPhotonSelection(process, cutflow, patProducer=None, addUserData=True, task=None)