CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 = cms.untracked.bool(False)
18  if hasattr(cutflow,'isPOGApproved'):
19  isPOGApproved = cutflow.isPOGApproved
20  del cutflow.isPOGApproved
21  vidproducer.physicsObjectIDs.append(
22  cms.PSet( idDefinition = cutflow,
23  isPOGApproved = isPOGApproved,
24  idMD5 = cms.string(cutflow_md5) )
25  )
26 # sys.stderr.write('Added ID \'%s\' to %s\n'%(cutflow.idName.value(),vidproducer.label()))
27 
28 def addVIDSelectionToPATProducer(patProducer,idProducer,idName,addUserData=True):
29  patProducerIDs = None
30  userDatas = None
31  for key in patProducer.__dict__.keys():
32  if 'IDSources' in key:
33  patProducerIDs = getattr(patProducer,key)
34  if 'userData' in key:
35  userDatas = getattr(patProducer,key)
36  if patProducerIDs is None:
37  raise Exception('StrangePatModule','%s does not have ID sources!'%patProducer.label())
38  if userDatas is None:
39  raise Exception('StrangePatModule','%s does not have UserData sources!'%patProducer.label())
40  setattr(patProducerIDs,idName,cms.InputTag('%s:%s'%(idProducer,idName)))
41  if( addUserData ):
42  if( len(userDatas.userClasses.src) == 1 and
43  type(userDatas.userClasses.src[0]) is str and
44  userDatas.userClasses.src[0] == '' ):
45  userDatas.userClasses.src = cms.VInputTag(cms.InputTag('%s:%s'%(idProducer,idName)))
46  else:
47  userDatas.userClasses.src.append(cms.InputTag('%s:%s'%(idProducer,idName)))
48  sys.stderr.write('\t--- %s:%s added to %s\n'%(idProducer,idName,patProducer.label()))
49 
50 def setupAllVIDIdsInModule(process,id_module_name,setupFunction,patProducer=None,addUserData=True):
51 # idmod = importlib.import_module(id_module_name)
52  idmod= __import__(id_module_name, globals(), locals(), ['idName','cutFlow'])
53  for name in dir(idmod):
54  item = getattr(idmod,name)
55  if hasattr(item,'idName') and hasattr(item,'cutFlow'):
56  setupFunction(process,item,patProducer,addUserData)
57 
58 # Supported data formats defined via "enum"
59 class DataFormat:
60  AOD = 1
61  MiniAOD = 2
62 
63 ####
64 # Electrons
65 ####
66 
67 #turns on the VID electron ID producer, possibly with extra options
68 # for PAT and/or MINIAOD
69 def switchOnVIDElectronIdProducer(process, dataFormat):
70  process.load('RecoEgamma.ElectronIdentification.egmGsfElectronIDs_cff')
71  #*always* reset to an empty configuration
72  if( len(process.egmGsfElectronIDs.physicsObjectIDs) > 0 ):
73  process.egmGsfElectronIDs.physicsObjectIDs = cms.VPSet()
74  dataFormatString = "Undefined"
75  if dataFormat == DataFormat.AOD:
76  # No reconfiguration is required, default settings are for AOD
77  dataFormatString = "AOD"
78  elif dataFormat == DataFormat.MiniAOD:
79  # If we are dealing with MiniAOD, we overwrite the electron collection
80  # name appropriately, for the fragment we just loaded above.
81  process.egmGsfElectronIDs.physicsObjectSrc = cms.InputTag('slimmedElectrons')
82  dataFormatString = "MiniAOD"
83  else:
84  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
85  #
86 # sys.stderr.write('Added \'egmGsfElectronIDs\' to process definition (%s format)!\n' % dataFormatString)
87 
88 def setupVIDElectronSelection(process,cutflow,patProducer=None,addUserData=True):
89  if not hasattr(process,'egmGsfElectronIDs'):
90  raise Exception('VIDProducerNotAvailable','egmGsfElectronIDs producer not available in process!')
91  setupVIDSelection(process.egmGsfElectronIDs,cutflow)
92  #add to PAT electron producer if available or specified
93  if hasattr(process,'patElectrons') or patProducer is not None:
94  if patProducer is None:
95  patProducer = process.patElectrons
96  idName = cutflow.idName.value()
97  addVIDSelectionToPATProducer(patProducer,'egmGsfElectronIDs',idName,addUserData)
98 
99  #we know cutflow has a name otherwise an exception would have been thrown in setupVIDSelection
100  #run this for all heep IDs except V60 standard for which it is not needed
101  #it is needed for V61 and V70
102  if (cutflow.idName.value().find("HEEP")!=-1 and
103  cutflow.idName.value()!="heepElectronID-HEEPV60"):
104 
105  #not the ideal way but currently the easiest
106  useMiniAOD = process.egmGsfElectronIDs.physicsObjectSrc == cms.InputTag('slimmedElectrons')
107 
109  addHEEPProducersToSeq(process=process,seq=process.egmGsfElectronIDSequence,
110  insertIndex=process.egmGsfElectronIDSequence.index(process.egmGsfElectronIDs),
111  useMiniAOD=useMiniAOD)
112 
113 ####
114 # Muons
115 ####
116 
117 #turns on the VID electron ID producer, possibly with extra options
118 # for PAT and/or MINIAOD
119 def switchOnVIDMuonIdProducer(process, dataFormat):
120  process.load('RecoMuon.MuonIdentification.muoMuonIDs_cff')
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):
159  process.load('RecoEgamma.PhotonIdentification.egmPhotonIDs_cff')
160  #*always* reset to an empty configuration
161  if( len(process.egmPhotonIDs.physicsObjectIDs) > 0 ):
162  process.egmPhotonIDs.physicsObjectIDs = cms.VPSet()
163  dataFormatString = "Undefined"
164  if dataFormat == DataFormat.AOD:
165  # No reconfiguration is required, default settings are for AOD
166  dataFormatString = "AOD"
167  elif dataFormat == DataFormat.MiniAOD:
168  # If we are dealing with MiniAOD, we overwrite the electron collection
169  # name appropriately, for the fragment we just loaded above.
170  process.egmPhotonIDs.physicsObjectSrc = cms.InputTag('slimmedPhotons')
171  dataFormatString = "MiniAOD"
172  else:
173  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
174  #
175  sys.stderr.write('Added \'egmPhotonIDs\' to process definition (%s format)!\n' % dataFormatString)
176 
177 def setupVIDPhotonSelection(process,cutflow,patProducer=None,addUserData=True):
178  if not hasattr(process,'egmPhotonIDs'):
179  raise Exception('VIDProducerNotAvailable','egmPhotonIDs producer not available in process!\n')
180  setupVIDSelection(process.egmPhotonIDs,cutflow)
181  #add to PAT photon producer if available or specified
182  if hasattr(process,'patPhotons') or patProducer is not None:
183  if patProducer is None:
184  patProducer = process.patPhotons
185  idName = cutflow.idName.value()
186  addVIDSelectionToPATProducer(patProducer,'egmPhotonIDs',idName,addUserData)
187 
188 
def setupVIDMuonSelection
def setupAllVIDIdsInModule
Definition: vid_id_tools.py:50
def switchOnVIDElectronIdProducer
Definition: vid_id_tools.py:69
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
def addVIDSelectionToPATProducer
Definition: vid_id_tools.py:28
def switchOnVIDMuonIdProducer
Muons.
def setupVIDPhotonSelection
def switchOnVIDPhotonIdProducer
Photons.
if(dp >Float(M_PI)) dp-
dbl *** dir
Definition: mlp_gen.cc:35
def setupVIDElectronSelection
Definition: vid_id_tools.py:88
def setupVIDSelection
Definition: vid_id_tools.py:11