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.
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):
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( 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):
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)
56 
57 # Supported data formats defined via "enum"
58 class DataFormat:
59  AOD = 1
60  MiniAOD = 2
61 
62 ####
63 # Electrons
64 ####
65 
66 #turns on the VID electron ID producer, possibly with extra options
67 # for PAT and/or MINIAOD
68 def switchOnVIDElectronIdProducer(process, dataFormat):
69  process.load('RecoEgamma.ElectronIdentification.egmGsfElectronIDs_cff')
70  dataFormatString = "Undefined"
71  if dataFormat == DataFormat.AOD:
72  # No reconfiguration is required, default settings are for AOD
73  dataFormatString = "AOD"
74  elif dataFormat == DataFormat.MiniAOD:
75  # If we are dealing with MiniAOD, we overwrite the electron collection
76  # name appropriately, for the fragment we just loaded above.
77  process.egmGsfElectronIDs.physicsObjectSrc = cms.InputTag('slimmedElectrons')
78  dataFormatString = "MiniAOD"
79  else:
80  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
81  #
82  sys.stderr.write('Added \'egmGsfElectronIDs\' to process definition (%s format)!\n' % dataFormatString)
83 
84 def setupVIDElectronSelection(process,cutflow,patProducer=None):
85  if not hasattr(process,'egmGsfElectronIDs'):
86  raise Exception('VIDProducerNotAvailable','egmGsfElectronIDs producer not available in process!')
87  setupVIDSelection(process.egmGsfElectronIDs,cutflow)
88  #add to PAT electron producer if available or specified
89  if hasattr(process,'patElectrons') or patProducer is not None:
90  if patProducer is None:
91  patProducer = process.patElectrons
92  idName = cutflow.idName.value()
93  addVIDSelectionToPATProducer(patProducer,'egmGsfElectronIDs',idName)
94 
95 ####
96 # Muons
97 ####
98 
99 #turns on the VID electron ID producer, possibly with extra options
100 # for PAT and/or MINIAOD
101 def switchOnVIDMuonIdProducer(process, dataFormat):
102  process.load('RecoMuon.MuonIdentification.muoMuonIDs_cff')
103  dataFormatString = "Undefined"
104  if dataFormat == DataFormat.AOD:
105  # No reconfiguration is required, default settings are for AOD
106  dataFormatString = "AOD"
107  elif dataFormat == DataFormat.MiniAOD:
108  # If we are dealing with MiniAOD, we overwrite the muon collection
109  # name appropriately, for the fragment we just loaded above.
110  process.muoMuonIDs.physicsObjectSrc = cms.InputTag('slimmedMuons')
111  dataFormatString = "MiniAOD"
112  else:
113  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
114  #
115  sys.stderr.write('Added \'muoMuonIDs\' to process definition (%s format)!\n' % dataFormatString)
116 
117 def setupVIDMuonSelection(process,cutflow,patProducer=None):
118  moduleName = "muoMuonIDs"
119  if not hasattr(process, moduleName):
120  raise Exception("VIDProducerNotAvailable", "%s producer not available in process!" % moduleName)
121  setupVIDSelection(getattr(process, moduleName), cutflow)
122  #add to PAT electron producer if available or specified
123  #if hasattr(process,'patMuons') or patProducer is not None:
124  # if patProducer is None:
125  # patProducer = process.patMuons
126  # idName = cutflow.idName.value()
127  # addVIDSelectionToPATProducer(patProducer, moduleName, idName)
128 
129 ####
130 # Photons
131 ####
132 
133 #turns on the VID photon ID producer, possibly with extra options
134 # for PAT and/or MINIAOD
135 def switchOnVIDPhotonIdProducer(process, dataFormat):
136  process.load('RecoEgamma.PhotonIdentification.egmPhotonIDs_cff')
137  dataFormatString = "Undefined"
138  if dataFormat == DataFormat.AOD:
139  # No reconfiguration is required, default settings are for AOD
140  dataFormatString = "AOD"
141  elif dataFormat == DataFormat.MiniAOD:
142  # If we are dealing with MiniAOD, we overwrite the electron collection
143  # name appropriately, for the fragment we just loaded above.
144  process.egmPhotonIDs.physicsObjectSrc = cms.InputTag('slimmedPhotons')
145  dataFormatString = "MiniAOD"
146  else:
147  raise Exception('InvalidVIDDataFormat', 'The requested data format is different from AOD or MiniAOD')
148  #
149  sys.stderr.write('Added \'egmPhotonIDs\' to process definition (%s format)!\n' % dataFormatString)
150 
151 def setupVIDPhotonSelection(process,cutflow,patProducer=None):
152  if not hasattr(process,'egmPhotonIDs'):
153  raise Exception('VIDProducerNotAvailable','egmPhotonIDs producer not available in process!\n')
154  setupVIDSelection(process.egmPhotonIDs,cutflow)
155  #add to PAT photon producer if available or specified
156  if hasattr(process,'patPhotons') or patProducer is not None:
157  if patProducer is None:
158  patProducer = process.patPhotons
159  idName = cutflow.idName.value()
160  addVIDSelectionToPATProducer(patProducer,'egmPhotonIDs',idName)
161 
162 
def setupVIDMuonSelection
def setupAllVIDIdsInModule
Definition: vid_id_tools.py:49
def switchOnVIDElectronIdProducer
Definition: vid_id_tools.py:68
def addVIDSelectionToPATProducer
Definition: vid_id_tools.py:28
def switchOnVIDMuonIdProducer
Muons.
def setupVIDPhotonSelection
def switchOnVIDPhotonIdProducer
Photons.
dbl *** dir
Definition: mlp_gen.cc:35
if(conf.exists("allCellsPositionCalc"))
def setupVIDElectronSelection
Definition: vid_id_tools.py:84
def setupVIDSelection
Definition: vid_id_tools.py:11