CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  if type(analyzer) is cms.EDAnalyzer:
137  PlotAnalyzer(pset, analyzer)
138  return pset
139 
140 def SpawnPSet(lArgument, subPset):
141  """SpawnPSet(lArgument, subPset) --> cms.PSet\n
142  lArgument is a list containing a list of three strings/values:\n
143  1-name to give to the spawned pset\n
144  2-variable(s) to be changed\n
145  3-value(s) of the variable(s): SAME LENGTH OF 2-!\n
146  Supported types: int string float(converted to double)"""
147  ret = cms.PSet()
148  for spawn in lArgument:
149  if len(spawn) != 3:
150  print "ERROR! SpawnPSet uses argument of three data\n"
151  print self.__doc__
152  return None
153  if len(spawn[1]) != len(spawn[2]):
154  print "ERROR! Lists of arguments to replace must have the same length"
155  print self.__doc__
156  return None
157  spawnArg = copy.deepcopy(subPset)
158  for par, val in zip(spawn[1],spawn[2]):
159  if type(val) is str :
160  setattr(spawnArg,par,cms.string(val))
161  elif type(val) is int :
162  setattr(spawnArg,par,cms.int32(val))
163  elif type(val) is float :
164  setattr(spawnArg,par,cms.double(val))
165  setattr(ret,spawn[0],spawnArg)
166  return ret
167 
168 def SetpByStep(analyzer, plotPset, useOnly):
169  """SetpByStep(analyzer, plotPset) --> PSet\n
170  This function produces the parameter set stepBystep for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
171  standardEfficiencyOverlay = cms.PSet(
172  parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
173  title = cms.string('TauId step by step efficiencies'),
174  xAxis = cms.string('#PAR#'),
175  yAxis = cms.string('efficiency'),
176  legend = cms.string('efficiency_overlay'),
177  labels = cms.vstring('pt', 'eta')
178  )
179  ret = cms.PSet(
180  standardEfficiencyOverlay,
181  plots = cms.VPSet()
182  )
183  producer = analyzer.TauProducer.pythonValue()[1:-1]
184  ext = analyzer.ExtensionName.pythonValue()[1:-1]
185  keyword = producer + ext + "_"
186  counter = 0
187  tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
188  hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
189  for parName in plotPset.parameterNames_():
190  isToBePlotted = getattr(plotPset,parName).stepByStep.value()
191  if isToBePlotted:
192  effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
193  discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
194  if useOnly == 'tanc':
195  useThis = discriminator in tancDisc
196  elif useOnly == 'hps':
197  useThis = discriminator in hpsDisc
198  else :
199  useThis = True
200  if (effplot.find(keyword) != -1) and useThis:
201  monEl = '#PROCESSDIR#/'+effplot
202  counter = counter + 1
203  drawOpt = 'eff_overlay0%s'%(counter)
204  psetName = effplot[effplot.rfind('/')+1:-8]
205  ret.plots.append(cms.PSet(
206  dqmMonitorElements = cms.vstring(monEl),
207  process = cms.string('test'),
208  drawOptionEntry = cms.string(drawOpt),
209  legendEntry = cms.string(psetName)
210  ))
211  return ret
212 
213 def SpawnDrawJobs(analyzer, plotPset, useOnly=None):
214  """SpwnDrawJobs(analyzer, plotPset) --> cms.PSet\n
215  This function produces the parameter set drawJobs for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
216  standardEfficiencyParameters = cms.PSet(
217  parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
218  xAxis = cms.string('#PAR#'),
219  yAxis = cms.string('efficiency'),
220  legend = cms.string('efficiency'),
221  labels = cms.vstring('pt', 'eta'),
222  drawOptionSet = cms.string('efficiency')
223  )
224  ret = cms.PSet()
225  tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
226  hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
227  producer = analyzer.TauProducer.pythonValue()[1:-1]
228  ext = analyzer.ExtensionName.pythonValue()[1:-1]
229  keyword = producer + ext + "_"
230  for parName in plotPset.parameterNames_():
231  effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
232  discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
233  if useOnly == 'tanc':
234  useThis = discriminator in tancDisc
235  elif useOnly == 'hps':
236  useThis = discriminator in hpsDisc
237  else :
238  useThis = True
239  if (effplot.find(keyword) != -1) and useThis:
240  monEl = '#PROCESSDIR#/'+effplot
241  psetName = effplot[effplot.rfind('/')+1:-5]
242  psetVal = cms.PSet(
243  standardEfficiencyParameters,
244  plots = cms.PSet(
245  dqmMonitorElements = cms.vstring(monEl),
246  processes = cms.vstring('test', 'reference')
247  )
248  )
249  setattr(ret,psetName,psetVal)
250  setattr(ret,'TauIdEffStepByStep',SetpByStep(analyzer, plotPset,useOnly))
251  return ret #control if it's ok
list object
Definition: dbtoconf.py:77