CMS 3D CMS Logo

mvaPhotonID_tools.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 from PhysicsTools.SelectorUtils.centralIDRegistry import central_id_registry
3 from os import path
4 
5 weightFileBaseDir = "RecoEgamma/PhotonIdentification/data/MVA"
6 
7 # division between barrel and endcap
8 ebeeSplit = 1.479
9 
10 # This MVA implementation class name
11 mvaClassName = "PhotonMVAEstimator"
12 
13 # The locatoins of value maps with the actual MVA values and categories
14 # for all particles.
15 # The names for the maps are "<module name>:<MVA class name>Values"
16 # and "<module name>:<MVA class name>Categories"
17 mvaProducerModuleLabel = "photonMVAValueMapProducer"
18 
19 # =======================================================
20 # Define simple containers for MVA cut values and related
21 # =======================================================
22 
24  """
25  This is a container class to hold MVA cut values for a 2-category MVA
26  as well as the names of the value maps that contain the MVA values computed
27  for all particles in a producer upstream.
28  """
29  def __init__(self,
30  idName,
31  mvaValueMapName,
32  mvaCategoriesMapName,
33  cutCategory0,
34  cutCategory1
35  ):
36  self.idName = idName
37  self.mvaValueMapName = mvaValueMapName
38  self.mvaCategoriesMapName = mvaCategoriesMapName
39  self.cutCategory0 = cutCategory0
40  self.cutCategory1 = cutCategory1
41 
42  def getCutValues(self):
43  return [self.cutCategory0, self.cutCategory1]
44 
45 # ==============================================================
46 # Define the complete MVA cut sets
47 # ==============================================================
48 
50  """
51  This function configures the full cms.PSet for a VID ID and returns it.
52  The inputs: an object of the class PhoMVA_2Categories_WP or similar
53  that contains all necessary parameters for this MVA.
54  """
55  parameterSet = cms.PSet(
56  #
57  idName = cms.string( mvaWP.idName ),
58  cutFlow = cms.VPSet(
59  cms.PSet( cutName = cms.string("PhoMVACut"),
60  mvaCuts = cms.vdouble( mvaWP.getCutValues() ),
61  mvaValueMapName = cms.InputTag( mvaWP.mvaValueMapName ),
62  mvaCategoriesMapName =cms.InputTag( mvaWP.mvaCategoriesMapName ),
63  needsAdditionalProducts = cms.bool(True),
64  isIgnored = cms.bool(False)
65  )
66  )
67  )
68  #
69  return parameterSet
70 
71 # ===============================================
72 # High level function to create a two category ID
73 # ===============================================
74 
75 # mvaTag:
76 # The mvaTag is an extra string attached to the names of the products
77 # such as ValueMaps that needs to distinguish cases when the same MVA estimator
78 # class is used with different tuning/weights
79 # variablesFile:
80 # The file listing the variables used in this MVA
81 # weightFiles:
82 # The weight files in the order EB first, then EE
83 # wpConfig:
84 # A dictionary with the names and cut values of the working points
85 # addKwargsForValueProducer:
86 # Additional keyword parameters passed to the producer config
87 
88 def configureFullVIDMVAPhoID(mvaTag, variablesFile, weightFiles, wpConfig, **addKwargsForValueProducer):
89 
90  mvaValueMapName = mvaProducerModuleLabel + ":" + mvaClassName + mvaTag + "Values"
91  mvaCategoriesMapName = mvaProducerModuleLabel + ":" + mvaClassName + mvaTag + "Categories"
92 
93  # Create the PSet that will be fed to the MVA value map producer
94  producer_config = cms.PSet(
95  mvaName = cms.string(mvaClassName),
96  mvaTag = cms.string(mvaTag),
97  weightFileNames = cms.vstring(*weightFiles),
98  variableDefinition = cms.string(variablesFile),
99  ebeeSplit = cms.double(ebeeSplit),
100  **addKwargsForValueProducer
101  )
102 
103  # Create the VPset's for VID cuts
104  VID_config = {}
105  for wpc in wpConfig:
106  idName = wpc["idName"]
107  VID_config[idName] = configureVIDMVAPhoID_V1(
109  idName = idName,
110  mvaValueMapName = mvaValueMapName, # map with MVA values for all particles
111  mvaCategoriesMapName = mvaCategoriesMapName, # map with category index for all particles
112  cutCategory0 = wpc["cuts"]["EB"], # EB
113  cutCategory1 = wpc["cuts"]["EE"] # EE
114  )
115  )
116 
117  configs = {"producer_config": producer_config, "VID_config": VID_config}
118  return configs
def configureFullVIDMVAPhoID(mvaTag, variablesFile, weightFiles, wpConfig, addKwargsForValueProducer)
def __init__(self, idName, mvaValueMapName, mvaCategoriesMapName, cutCategory0, cutCategory1)
def configureVIDMVAPhoID_V1(mvaWP)