CMS 3D CMS Logo

ValidationUtils.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 import copy
3 
4 def CreatePlotEntry(analyzer, discriminatorLabel=None, step=True):
5  """CreatePlotEntry(analyzer, discriminatorLabel)\n
6  Creates a PSet with the informations used by TauDQMHistEffProducer\n
7  where to find the numerator and denominator\n
8  where to put the new plot and how to name it\n
9  which variables control"""
10 
11  producer = analyzer.TauProducer.pythonValue()[1:-1]
12  ext = analyzer.ExtensionName.pythonValue()[1:-1]
13  if discriminatorLabel == None:
14  num = 'RecoTauV/%s%s_Matched/%sMatched_vs_#PAR#TauVisible'%(producer,ext,producer)
15  #out = 'RecoTauV/%s%s_Matched/PFJetMatchingEff#PAR#'%(producer,ext)
16  if producer.find('caloReco') != -1:
17  out = 'RecoTauV/%s%s_Matched/CaloJetMatchingEff#PAR#'%(producer,ext)
18  else:
19  out = 'RecoTauV/%s%s_Matched/PFJetMatchingEff#PAR#'%(producer,ext)
20  else:
21  num = 'RecoTauV/%s%s_%s/%s_vs_#PAR#TauVisible'%(producer,ext,discriminatorLabel,discriminatorLabel)
22  if discriminatorLabel.find('DiscriminationBy') != -1:
23  hname = discriminatorLabel[(discriminatorLabel.find('DiscriminationBy')+len('DiscriminationBy')):]
24  else:
25  hname = discriminatorLabel[(discriminatorLabel.find('Discrimination')+len('Discrimination')):]
26  out = 'RecoTauV/%s%s_%s/%sEff#PAR#'%(producer,ext,discriminatorLabel,hname)
27 
28  den = 'RecoTauV/%s%s_ReferenceCollection/nRef_Taus_vs_#PAR#TauVisible'%(producer,ext)
29  ret = cms.PSet(
30  numerator = cms.string(num),
31  denominator = cms.string(den),
32  efficiency = cms.string(out),
33  parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
34  stepByStep = cms.bool(step)
35  )
36  return ret
37 
38 def NameVariable(analyzer, discriminatorLabel=None):
39  """NameVariable(analyzer, discriminatorLabel)\n
40  returns a string with the name of the pset created by CreatePlotEntry"""
41  #This part is messy, there is no way to directly link the producer and the name of the variable. There are more exception than rules! IF THE DISCRIMINATOR NAME CHANGES YOU HAVE TO CHANHE IT HERE TOO!
42  if analyzer.TauProducer.pythonValue()[1:-1] == 'shrinkingConePFTauProducer':
43  if analyzer.ExtensionName.pythonValue()[1:-1] == 'Tanc':
44  first='ShrinkingConeTanc'
45  elif analyzer.ExtensionName.pythonValue()[1:-1] == 'LeadingPion':
46  first='PFTauHighEfficiencyLeadingPion'
47  elif analyzer.ExtensionName.pythonValue()[1:-1] == "":
48  first='PFTauHighEfficiency'
49  else:
50  #print 'Case not found check the available cases in Validation/RecoTau/python/ValidationUtils.py -- NameVariable'
51  first=analyzer.TauProducer.pythonValue()[1:-1]+analyzer.ExtensionName.pythonValue()[1:-1]
52  elif analyzer.TauProducer.pythonValue()[1:-1] == 'hpsPFTauProducer':
53  first='HPS'
54  elif analyzer.TauProducer.pythonValue()[1:-1] == 'hpsTancTaus':
55  first='HPSTanc'+analyzer.ExtensionName.value()
56  elif analyzer.TauProducer.pythonValue()[1:-1] == 'caloRecoTauProducer':
57  first='CaloTau'
58  else:
59  #print 'Case not found check the available cases in Validation/RecoTau/python/ValidationUtils.py -- NameVariable'
60  first=analyzer.TauProducer.pythonValue()[1:-1]+analyzer.ExtensionName.pythonValue()[1:-1]
61 
62  if discriminatorLabel == None:
63  last = 'Matching'
64  else:
65  if discriminatorLabel.find('DiscriminationBy') != -1:
66  last = discriminatorLabel[(discriminatorLabel.find('DiscriminationBy')+len('DiscriminationBy')):]
67  if last.find('TaNCfr') != -1:
68  last = last[len('TaNCfr'):]
69  else:
70  last = discriminatorLabel[(discriminatorLabel.find('DiscriminationAgainst')+len('DiscriminationAgainst')):]+"Rejection"
71 
72  return first+"ID"+last+"Efficiencies"
73 
74 def PlotAnalyzer(pset, analyzer):
75  """PlotAnalyzer(pset, analyzer)\n
76  fills a PSet that contains all the performance plots for a anlyzer\n
77  pset is the PSet to fill/add"""
78 
79  setattr(pset,NameVariable(analyzer),CreatePlotEntry(analyzer))
80 
81  for currentDiscriminator in analyzer.discriminators:
82  label = currentDiscriminator.discriminator.pythonValue()[1:-1]
83  step = currentDiscriminator.plotStep.value()
84  setattr(pset,NameVariable(analyzer,label),CreatePlotEntry(analyzer,label,step))
85 
86 class Scanner(object):
87  """Class to scan a sequence and give a list of analyzer used and a list of their names"""
88  def __init__(self):
89  self._analyzerRef = []
90  def enter(self,visitee):
91  self._analyzerRef.append(visitee)
92  def modules(self):
93  return self._analyzerRef
94  def leave(self, visitee):
95  pass
96 
97 def DisableQCuts(sequence):
98  scanner = Scanner()
99  sequence.visit(scanner)
100  disabled = cms.PSet(
101  isolationQualityCuts = cms.PSet(
102  minTrackHits = cms.uint32(0),
103  minTrackVertexWeight = cms.double(-1),
104  minTrackPt = cms.double(0),
105  maxTrackChi2 = cms.double(9999),
106  minTrackPixelHits = cms.uint32(0),
107  minGammaEt = cms.double(0),
108  maxDeltaZ = cms.double(0.2),
109  maxTransverseImpactParameter = cms.double(9999)
110  ),
111  pvFindingAlgo = cms.string('highestWeightForLeadTrack'),
112  primaryVertexSrc = cms.InputTag("offlinePrimaryVertices"),
113  signalQualityCuts = cms.PSet(
114  minTrackHits = cms.uint32(0),
115  minTrackVertexWeight = cms.double(-1),
116  minTrackPt = cms.double(0),
117  maxTrackChi2 = cms.double(9999),
118  minTrackPixelHits = cms.uint32(0),
119  minGammaEt = cms.double(0),
120  maxDeltaZ = cms.double(0.2),
121  maxTransverseImpactParameter = cms.double(9999)
122  )
123  )
124  for module in scanner.modules():
125  if hasattr(module,'qualityCuts'):
126  setattr(module,'qualityCuts',disabled)
127 
128 
129 def SetPlotSequence(sequence):
130  """SetSequence(seqence)\n
131  This Function return a PSet of the sequence given to be used by TauDQMHistEffProducer"""
132  pset = cms.PSet()
133  scanner = Scanner()
134  sequence.visit(scanner)
135  for analyzer in scanner.modules():#The first one is the sequence itself
136  PlotAnalyzer(pset, analyzer)
137  return pset
138 
139 def SpawnPSet(lArgument, subPset):
140  """SpawnPSet(lArgument, subPset) --> cms.PSet\n
141  lArgument is a list containing a list of three strings/values:\n
142  1-name to give to the spawned pset\n
143  2-variable(s) to be changed\n
144  3-value(s) of the variable(s): SAME LENGTH OF 2-!\n
145  Supported types: int string float(converted to double)"""
146  ret = cms.PSet()
147  for spawn in lArgument:
148  if len(spawn) != 3:
149  print "ERROR! SpawnPSet uses argument of three data\n"
150  print self.__doc__
151  return None
152  if len(spawn[1]) != len(spawn[2]):
153  print "ERROR! Lists of arguments to replace must have the same length"
154  print self.__doc__
155  return None
156  spawnArg = copy.deepcopy(subPset)
157  for par, val in zip(spawn[1],spawn[2]):
158  if isinstance(val, str) :
159  setattr(spawnArg,par,cms.string(val))
160  elif isinstance(val, int) :
161  setattr(spawnArg,par,cms.int32(val))
162  elif isinstance(val, float) :
163  setattr(spawnArg,par,cms.double(val))
164  setattr(ret,spawn[0],spawnArg)
165  return ret
166 
167 def SetpByStep(analyzer, plotPset, useOnly):
168  """SetpByStep(analyzer, plotPset) --> PSet\n
169  This function produces the parameter set stepBystep for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
170  standardEfficiencyOverlay = cms.PSet(
171  parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
172  title = cms.string('TauId step by step efficiencies'),
173  xAxis = cms.string('#PAR#'),
174  yAxis = cms.string('efficiency'),
175  legend = cms.string('efficiency_overlay'),
176  labels = cms.vstring('pt', 'eta')
177  )
178  ret = cms.PSet(
179  standardEfficiencyOverlay,
180  plots = cms.VPSet()
181  )
182  producer = analyzer.TauProducer.pythonValue()[1:-1]
183  ext = analyzer.ExtensionName.pythonValue()[1:-1]
184  keyword = producer + ext + "_"
185  counter = 0
186  tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
187  hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
188  for parName in plotPset.parameterNames_():
189  isToBePlotted = getattr(plotPset,parName).stepByStep.value()
190  if isToBePlotted:
191  effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
192  discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
193  if useOnly == 'tanc':
194  useThis = discriminator in tancDisc
195  elif useOnly == 'hps':
196  useThis = discriminator in hpsDisc
197  else :
198  useThis = True
199  if (effplot.find(keyword) != -1) and useThis:
200  monEl = '#PROCESSDIR#/'+effplot
201  counter = counter + 1
202  drawOpt = 'eff_overlay0%s'%(counter)
203  psetName = effplot[effplot.rfind('/')+1:-8]
204  ret.plots.append(cms.PSet(
205  dqmMonitorElements = cms.vstring(monEl),
206  process = cms.string('test'),
207  drawOptionEntry = cms.string(drawOpt),
208  legendEntry = cms.string(psetName)
209  ))
210  return ret
211 
212 def SpawnDrawJobs(analyzer, plotPset, useOnly=None):
213  """SpwnDrawJobs(analyzer, plotPset) --> cms.PSet\n
214  This function produces the parameter set drawJobs for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
215  standardEfficiencyParameters = cms.PSet(
216  parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
217  xAxis = cms.string('#PAR#'),
218  yAxis = cms.string('efficiency'),
219  legend = cms.string('efficiency'),
220  labels = cms.vstring('pt', 'eta'),
221  drawOptionSet = cms.string('efficiency')
222  )
223  ret = cms.PSet()
224  tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
225  hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
226  producer = analyzer.TauProducer.pythonValue()[1:-1]
227  ext = analyzer.ExtensionName.pythonValue()[1:-1]
228  keyword = producer + ext + "_"
229  for parName in plotPset.parameterNames_():
230  effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
231  discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
232  if useOnly == 'tanc':
233  useThis = discriminator in tancDisc
234  elif useOnly == 'hps':
235  useThis = discriminator in hpsDisc
236  else :
237  useThis = True
238  if (effplot.find(keyword) != -1) and useThis:
239  monEl = '#PROCESSDIR#/'+effplot
240  psetName = effplot[effplot.rfind('/')+1:-5]
241  psetVal = cms.PSet(
242  standardEfficiencyParameters,
243  plots = cms.PSet(
244  dqmMonitorElements = cms.vstring(monEl),
245  processes = cms.vstring('test', 'reference')
246  )
247  )
248  setattr(ret,psetName,psetVal)
249  setattr(ret,'TauIdEffStepByStep',SetpByStep(analyzer, plotPset,useOnly))
250  return ret #control if it's ok
def PlotAnalyzer(pset, analyzer)
def SpawnDrawJobs(analyzer, plotPset, useOnly=None)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def SetpByStep(analyzer, plotPset, useOnly)
def enter(self, visitee)
def SpawnPSet(lArgument, subPset)
def NameVariable(analyzer, discriminatorLabel=None)
def SetPlotSequence(sequence)
def DisableQCuts(sequence)
def CreatePlotEntry(analyzer, discriminatorLabel=None, step=True)
def leave(self, visitee)