CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
MultipleCompare Namespace Reference

Functions

def DetermineHistType
 
def Divide
 
def DrawBranding
 
def DrawTitle
 
def FindParents
 
def findRange
 
def GetContent
 
def getMaximumIncludingErrors
 
def getMinimumIncludingErrors
 
def LoadCommandlineOptions
 
def main
 
def MapDirStructure
 
def Match
 
def optimizeRangeMainPad
 
def optimizeRangeSubPad
 
def Rebin
 

Variables

string __author__ = "Mauro Verzetti (mauro.verzetti@cern.ch)"
 
string __doc__
 

Function Documentation

def MultipleCompare.DetermineHistType (   name)

Definition at line 85 of file MultipleCompare.py.

Referenced by main().

85 
86 def DetermineHistType(name):
87  #automatically derive all plot types in the future?
88  type = ''
89  label = ''
90  prefix = ''
91  #assuming plots name like: tauType_plotType_xAxis or tauType_plotType_selection
92  matches = re.match(r'.*/(.*)_(.*)_(.*)', name)
93  if matches:
94  prefix = matches.group(1)
95  label = matches.group(3)
96  knowntypes = (['pTRatio','SumPt','Size'])
97  for knowntype in knowntypes:
98  if matches.group(2) == knowntype:
99  type = knowntype
100  if not type: #there are plots labelled ..._vs_...
101  type = 'Eff'
102  else:
103  type = 'Eff'
104 
105  prefixParts = prefix.partition('Discrimination')
106  if prefixParts[2] != '':
107  prefix = prefixParts[2]
108  prefixParts = prefix.partition('By')
109  if prefixParts[2] != '':
110  prefix = prefixParts[2]
111 
112  #print 'type is ' + type
113  return [type, label, prefix]
def MultipleCompare.Divide (   hNum,
  hDen 
)

Definition at line 66 of file MultipleCompare.py.

Referenced by FlavourHistograms< T >.divide(), FlavourHistograms2D< T, G >.divide(), main(), and plotscripts.segdiff().

66 
67 def Divide(hNum,hDen):
68  ret = hNum.Clone('Division')
69  ret.GetYaxis().SetTitle('Ratio')
70  for binI in range(hNum.GetNbinsX()):
71  denVal = hDen.GetBinContent(binI)
72  denErr = hDen.GetBinError(binI)
73  numErr = hNum.GetBinError(binI)
74  numVal = hNum.GetBinContent(binI)
75  if denVal == 0:
76  ret.SetBinContent(binI,0)
77  ret.SetBinError(binI,0)
78  else:
79  ret.SetBinContent(binI,numVal/denVal)
80  if numVal==0:
81  ret.SetBinError(binI,1)
82  else:
83  ret.SetBinError(binI,(numVal/denVal)*math.sqrt(math.pow(numErr/numVal,2) + math.pow(denErr/denVal,2) ) )
84  return ret
def MultipleCompare.DrawBranding (   options,
  label = '' 
)

Definition at line 123 of file MultipleCompare.py.

Referenced by main().

124 def DrawBranding(options, label=''):
125  if options.branding != None or label != '':
126  text = TLatex()
127  text.SetNDC();
128  text.SetTextAlign(11)#3*10=right,3*1=top
129  text.SetTextSize(.025)
130  text.SetTextColor(13)
131  if options.out.find(".eps")!=-1:
132  text.SetTextAngle(-91.0)#eps BUG
133  else:
134  text.SetTextAngle(-90.0)
135  rightMargin = 1 - gStyle.GetPadRightMargin()
136  topMargin = 1 - gStyle.GetPadTopMargin()
137  if label!='':
138  label += ': '
139  text.DrawLatex(rightMargin+.01, topMargin+0.025, label+options.branding);
140 
def MultipleCompare.DrawTitle (   text)

Definition at line 114 of file MultipleCompare.py.

Referenced by main().

115 def DrawTitle(text):
116  title = TLatex()
117  title.SetNDC()
118  title.SetTextAlign(12)#3*10=right,3*1=top
119  title.SetTextSize(.035)
120  leftMargin = gStyle.GetPadLeftMargin()
121  topMargin = 1 - 0.5*gStyle.GetPadTopMargin()
122  title.DrawLatex(leftMargin, topMargin, text)
def MultipleCompare.FindParents (   histoPath)

Definition at line 141 of file MultipleCompare.py.

References linker.replace().

Referenced by Rebin().

142 def FindParents(histoPath):
143  root = histoPath[:histoPath.find('_')]
144  par = histoPath[histoPath.find('Eff')+3:]
145  validationPlots = validation.proc.efficiencies.plots._Parameterizable__parameterNames
146  found =0
147  num = ''
148  den = ''
149  for efficiency in validationPlots:
150  effpset = getattr(validation.proc.efficiencies.plots,efficiency)
151  effName = effpset.efficiency.value()
152  effNameCut = effName[effName.find('_'):effName.find('#')]
153  if effNameCut in histoPath:
154  if found == 1:
155  print 'More than one pair of parents found for ' + histopath + ':'
156  assert(False)
157  num = root + effpset.numerator.value()[effName.find('_'):].replace('#PAR#',par)
158  den = root + effpset.denominator.value()[effName.find('_'):].replace('#PAR#',par)
159  found += 1
160  return [num,den]
def replace
Definition: linker.py:10
def MultipleCompare.findRange (   hists,
  min0 = -1,
  max0 = -1 
)

Definition at line 182 of file MultipleCompare.py.

References getMaximumIncludingErrors(), and getMinimumIncludingErrors().

Referenced by optimizeRangeMainPad(), and optimizeRangeSubPad().

183 def findRange(hists, min0=-1, max0=-1):
184  if len(hists) < 1:
185  return
186  #auto ranges if no user value provided
187  min = min0
188  max = max0
189  if min0 == -1 or max0 == -1:
190  for hist in hists:
191  if min0 == -1:
192  #Divide() sets bin to zero if division not possible. Ignore these bins.
193  minTmp = getMinimumIncludingErrors(hist)
194  if minTmp < min or min == -1:
195  min = minTmp
196  if max0 == -1:
197  maxTmp = getMaximumIncludingErrors(hist)
198  if maxTmp > max or max == -1:
199  max = maxTmp
200  return [min, max]
def MultipleCompare.GetContent (   dir)

Definition at line 43 of file MultipleCompare.py.

Referenced by MapDirStructure().

43 
44 def GetContent(dir):
45  tempList = dir.GetListOfKeys()
46  retList = []
47  for it in range(0,tempList.GetSize()):
48  retList.append(tempList.At(it).ReadObj())
49  return retList
def MultipleCompare.getMaximumIncludingErrors (   hist)

Definition at line 233 of file MultipleCompare.py.

Referenced by findRange().

234 def getMaximumIncludingErrors(hist):
235 #find maximum considering also the errors
236  distance = 1.
237  max = -1
238  pos = 0
239  for i in range(1, hist.GetNbinsX()):
240  if hist.GetBinContent(i) > max:#ignore errors here
241  max = hist.GetBinContent(i)
242  pos = i
243  return max + distance*hist.GetBinError(pos)
def MultipleCompare.getMinimumIncludingErrors (   hist)

Definition at line 244 of file MultipleCompare.py.

Referenced by findRange().

245 def getMinimumIncludingErrors(hist):
246  #find minimum considering also the errors
247  #ignoring zero bins
248  distance = 1.
249  min = -1
250  pos = 0
251  for i in range(1, hist.GetNbinsX()):
252  if hist.GetBinContent(i)<=0.:#ignore errors here
253  continue
254  if hist.GetBinContent(i) < min or min==-1:
255  min = hist.GetBinContent(i)
256  pos = i
257  if min < 0:
258  min = 0
259  return min - distance*hist.GetBinError(pos)
260 
def MultipleCompare.LoadCommandlineOptions (   argv)

Definition at line 16 of file MultipleCompare.py.

Referenced by SteerMultipleCompare.main(), and main().

16 
17 def LoadCommandlineOptions(argv):
18  sys.argv = argv
19  parser = OptionParser(description=__doc__)
20  parser.add_option('--myhelp',metavar='', action="store_true",help='prints this output message',dest='help',default = False)
21  parser.add_option('--TestFile','-T',metavar='testFile', type=str,help='Sets the test file',dest='test',default = '')
22  parser.add_option('--RefFile','-R',metavar='refFile', type=str,help='Sets the reference file',dest='ref',default = None)
23  parser.add_option('--output','-o',metavar='outputFile', type=str,help='Sets the output file',dest='out',default = 'MultipleCompare.png')
24  parser.add_option('--logScale',action="store_true", dest="logScale", default=False, help="Sets the log scale in the plot")
25  parser.add_option('--fakeRate','-f',action="store_true", dest="fakeRate", default=False, help="Sets the fake rate options and put the correct label (implies --logScale)")
26  parser.add_option('--testLabel','-t',metavar='testLabel', type=str,help='Sets the label to put in the plots for test file',dest='testLabel',default = None)
27  parser.add_option('--refLabel','-r',metavar='refLabel', type=str,help='Sets the label to put in the plots for ref file',dest='refLabel',default = None)
28  parser.add_option('--maxLog',metavar='number', type=float,help='Sets the maximum of the scale in log scale (requires --logScale or -f to work)',dest='maxLog',default = 3)
29  parser.add_option('--minDiv',metavar='number', type=float,help='Sets the minimum of the scale in the ratio pad',dest='minDiv',default = 0.001)
30  parser.add_option('--maxDiv',metavar='number', type=float,help='Sets the maximum of the scale in the ratio pad',dest='maxDiv',default = 2)
31  parser.add_option('--logDiv',action="store_true", dest="logDiv", default=False, help="Sets the log scale in the plot")
32  parser.add_option('--normalize',action="store_true", dest="normalize", default=False, help="plot normalized")
33  parser.add_option('--maxRange',metavar='number',type=float, dest="maxRange", default=1.6, help="Sets the maximum range in linear plots")
34  parser.add_option('--rebin', dest="rebin", type=int, default=-1, help="Sets the rebinning scale")
35  parser.add_option('--branding','-b',metavar='branding', type=str,help='Define a branding to label the plots (in the top right corner)',dest='branding',default = None)
36  #parser.add_option('--search,-s',metavar='searchStrings', type=str,help='Sets the label to put in the plots for ref file',dest='testLabel',default = None) No idea on how to tell python to use all the strings before a new option, thus moving this from option to argument (but may be empty)
37 
38  (options,toPlot) = parser.parse_args()
39  if options.help:
40  parser.print_help()
41  sys.exit(0)
42  return [options, toPlot]
def MultipleCompare.main (   argv = None)

Definition at line 261 of file MultipleCompare.py.

References DetermineHistType(), Divide(), DrawBranding(), DrawTitle(), reco.if(), LoadCommandlineOptions(), MapDirStructure(), Match(), optimizeRangeMainPad(), optimizeRangeSubPad(), and Rebin().

Referenced by SteerMultipleCompare.plotDefault(), and SteerMultipleCompare.plotOneByOne().

262 def main(argv=None):
263  if argv is None:
264  argv = sys.argv
265 
266  options, toPlot = LoadCommandlineOptions(argv)
267 
268  gROOT.SetStyle('Plain')
269  gROOT.SetBatch()
270  gStyle.SetPalette(1)
271  gStyle.SetOptStat(0)
272  gStyle.SetPadGridX(True)
273  gStyle.SetPadGridY(True)
274  gStyle.SetOptTitle(0)
275  gStyle.SetPadTopMargin(0.1)
276  gStyle.SetPadBottomMargin(0.1)
277  gStyle.SetPadLeftMargin(0.13)
278  gStyle.SetPadRightMargin(0.07)
279 
280 
281  testFile = TFile(options.test)
282  refFile = None
283  if options.ref != None:
284  refFile = TFile(options.ref)
285 
286  #Takes the position of all plots that were produced
287  plotList = []
288  MapDirStructure( testFile,'',plotList)
289 
290  histoList = []
291  for plot in toPlot:
292  for path in plotList:
293  if Match(plot.lower(),path.lower()):
294  histoList.append(path)
295 
296 # print "options: ",options
297 # print "toPlot: ",toPlot
298  print histoList
299 
300  if len(histoList)<1:
301  print '\tError: Please specify at least one histogram.'
302  if len(toPlot)>0:
303  print 'Check your plot list:', toPlot
304  sys.exit()
305 
306 
307  #WARNING: For now the hist type is assumed to be constant over all histos.
308  histType, label, prefix = DetermineHistType(histoList[0])
309  #decide whether or not to scale to an integral of 1
310  #should usually not be used most plot types. they are already normalized.
311  scaleToIntegral = False
312  if options.normalize:
313  scaleToIntegral = True
314 
315  ylabel = 'Efficiency'
316 
317  if options.fakeRate:
318  ylabel = 'Fake rate'
319 
320  drawStats = False
321  if histType=='pTRatio' and len(histoList)<3:
322  drawStats = True
323 
324  #legend = TLegend(0.6,0.83,0.6+0.39,0.83+0.17)
325  x1 = 0.55
326  x2 = 1-gStyle.GetPadRightMargin()
327  y2 = 1-gStyle.GetPadTopMargin()
328  lineHeight = .025
329  if len(histoList) == 1:
330  lineHeight = .05
331  y1 = y2 - lineHeight*len(histoList)
332  legend = TLegend(x1,y1,x2,y2)
333  #legend.SetHeader(label)
334  legend.SetFillColor(0)
335  if drawStats:
336  y2 = y1
337  y1 = y2 - .07*len(histoList)
338  statsBox = TPaveText(x1,y1,x2,y2,"NDC")
339  statsBox.SetFillColor(0)
340  statsBox.SetTextAlign(12)#3*10=right,3*1=top
341  statsBox.SetMargin(0.05)
342  statsBox.SetBorderSize(1)
343 
344 
345  canvas = TCanvas('MultiPlot','MultiPlot',validation.standardDrawingStuff.canvasSizeX.value(),832)
346  effPad = TPad('effPad','effPad',0,0.25,1.,1.,0,0)
347  effPad.SetBottomMargin(0.1);
348  effPad.SetTopMargin(0.1);
349  effPad.SetLeftMargin(0.13);
350  effPad.SetRightMargin(0.07);
351  effPad.Draw()
352  header = ''
353  if options.testLabel != None:
354  header += 'Dots: '+options.testLabel
355  if options.refLabel != None:
356  header += ' Line: '+options.refLabel
357  DrawTitle(header)
358  DrawBranding(options)
359  diffPad = TPad('diffPad','diffPad',0.,0.,1,.25,0,0)
360  diffPad.Draw()
361  colors = [2,3,4,6,5,7,28,1,2,3,4,6,5,7,28,1,2,3,4,6,5,7,28,1,2,3,4,6,5,7,28,1,2,3,4,6,5,7,28,1]
362  first = True
363  divHistos = []
364  statTemplate = '%s Mean: %.3f RMS: %.3f'
365  testHs = []
366  refHs = []
367  for histoPath,color in zip(histoList,colors):
368  if(options.rebin == -1):
369  testH = testFile.Get(histoPath)
370  else:
371  testH = Rebin(testFile,histoPath,options.rebin)
372  if type(testH) != TH1F:
373  print 'Looking for '+histoPath
374  print 'Test plot now found! What the hell are you doing? Exiting...'
375  sys.exit()
376  testHs.append(testH)
377  xAx = histoPath[histoPath.find('Eff')+len('Eff'):]
378  effPad.cd()
379  if not testH.GetXaxis().GetTitle(): #only overwrite label if none already existing
380  if hasattr(validation.standardDrawingStuff.xAxes,xAx):
381  testH.GetXaxis().SetTitle( getattr(validation.standardDrawingStuff.xAxes,xAx).xAxisTitle.value())
382  if not testH.GetYaxis().GetTitle(): #only overwrite label if none already existing
383  testH.GetYaxis().SetTitle(ylabel)
384  if label!='':
385  testH.GetXaxis().SetTitle(label+': '+testH.GetXaxis().GetTitle())
386  testH.GetXaxis().SetTitleOffset(1.1)
387  testH.GetYaxis().SetTitleOffset(1.5)
388  testH.SetMarkerSize(1)
389  testH.SetMarkerStyle(20)
390  testH.SetMarkerColor(color)
391  if histType == 'Eff':
392  legend.AddEntry(testH,histoPath[histoPath.rfind('/')+1:histoPath.find(histType)],'p')
393  else:
394  legend.AddEntry(testH,DetermineHistType(histoPath)[2],'p')
395  if drawStats:
396  text = statsBox.AddText(statTemplate % ('Dots',testH.GetMean(), testH.GetRMS()) )
397  text.SetTextColor(color)
398  if first:
399  first = False
400  if options.logScale:
401  effPad.SetLogy()
402  if scaleToIntegral:
403  if testH.GetEntries() > 0:
404  if not testH.GetSumw2N():
405  testH.Sumw2()
406  testH.DrawNormalized('ex0 P')
407  else:
408  print "--> Warning! You tried to normalize a histogram which seems to be already scaled properly. Draw it unscaled."
409  scaleToIntegral = False
410  testH.Draw('ex0')
411  else:
412  testH.Draw('ex0')
413  else:
414  if scaleToIntegral:
415  if testH.GetEntries() > 0:
416  testH.DrawNormalized('same p')
417  else:
418  testH.Draw('same ex0 l')
419  if refFile == None:
420  continue
421  if(options.rebin == -1):
422  refH = refFile.Get(histoPath)
423  else:
424  refH = Rebin(refFile,histoPath,options.rebin)
425  if type(refH) != TH1F:
426  continue
427  refHs.append(refH)
428  refH.SetLineColor(color)
429  refH.SetLineWidth(1)
430  if scaleToIntegral:
431  if testH.GetEntries() > 0:
432  refH.DrawNormalized('same hist')
433  else:
434  refH.DrawCopy('same hist')
435  if drawStats:
436  text = statsBox.AddText(statTemplate % ('Line',refH.GetMean(), refH.GetRMS()) )
437  text.SetTextColor(color)
438  refH.SetFillColor(color)
439  refH.SetFillStyle(3001)
440  if scaleToIntegral:
441  entries = testH.GetEntries()
442  if entries > 0:
443  testH.Scale(1./entries)
444  entries = refH.GetEntries()
445  refH.Sumw2()
446  if entries > 0:
447  refH.Scale(1./entries)
448  refH.Draw('same e3')
449  divHistos.append(Divide(testH,refH))
450 
451  tmpHists = []
452  tmpHists.extend(testHs)
453  tmpHists.extend(refHs)
454  optimizeRangeMainPad(argv, effPad, tmpHists)
455 
456  firstD = True
457  if refFile != None:
458  for histo,color in zip(divHistos,colors):
459  diffPad.cd()
460  histo.SetMarkerSize(1)
461  histo.SetMarkerStyle(20)
462  histo.SetMarkerColor(color)
463  histo.GetYaxis().SetLabelSize(0.08)
464  histo.GetYaxis().SetTitleOffset(0.6)
465  histo.GetYaxis().SetTitleSize(0.08)
466  histo.GetXaxis().SetLabelSize(0.)
467  histo.GetXaxis().SetTitleSize(0.)
468  if firstD:
469  if options.logDiv:
470  diffPad.SetLogy()
471  histo.Draw('ex0')
472  firstD = False
473  else:
474  histo.Draw('same ex0')
475  diffPad.Update()
476  optimizeRangeSubPad(argv, divHistos)
477 
478  effPad.cd()
479  legend.Draw()
480  if drawStats:
481  statsBox.Draw()
482  canvas.Print(options.out)
483 
if(dp >Float(M_PI)) dp-
def MultipleCompare.MapDirStructure (   directory,
  dirName,
  objectList 
)

Definition at line 50 of file MultipleCompare.py.

References GetContent().

Referenced by SteerMultipleCompare.main(), and main().

50 
51 def MapDirStructure( directory, dirName, objectList ):
52  dirContent = GetContent(directory)
53  for entry in dirContent:
54  if type(entry) is TDirectory or type(entry) is TDirectoryFile:
55  subdirName = os.path.join(dirName,entry.GetName())
56  MapDirStructure(entry, subdirName,objectList)
57  else:
58  pathname = os.path.join(dirName,entry.GetName())
59  objectList.append(pathname)
def MultipleCompare.Match (   required,
  got 
)

Definition at line 60 of file MultipleCompare.py.

Referenced by FWGeometryTableManager.loadGeometry(), SteerMultipleCompare.main(), main(), lhef::JetMatchingMLM.match(), lhef::Matching< Delta >.match(), btag::Matching< Delta >.match(), and MatchJet.matchCollections().

60 
61 def Match(required, got):
62  for part in required.split('*'):
63  if got.find(part) == -1:
64  return False
65  return True
def MultipleCompare.optimizeRangeMainPad (   argv,
  pad,
  hists 
)

Definition at line 201 of file MultipleCompare.py.

References findRange().

Referenced by main().

202 def optimizeRangeMainPad(argv, pad, hists):
203  pad.Update()
204  if pad.GetLogy() and argv.count('maxLog') > 0:
205  maxLog = options.maxLog
206  else:
207  maxLog = -1
208  min, max = findRange(hists, -1, maxLog)
209  if pad.GetLogy():
210  if min == 0:
211  min = 0.001
212  if max < 2:
213  max = 2. #prefere fixed range for easy comparison
214  else:
215  if min < 0.7:
216  min = 0. #start from zero if possible
217  if max <= 1.1 and max > 0.7:
218  max = 1.2 #prefere fixed range for easy comparison
219  hists[0].SetAxisRange(min, max, "Y")
def MultipleCompare.optimizeRangeSubPad (   argv,
  hists 
)

Definition at line 220 of file MultipleCompare.py.

References findRange().

Referenced by main().

221 def optimizeRangeSubPad(argv, hists):
222  min = -1
223  max = -1
224  if argv.count('minDiv') > 0:
225  min = options.minDiv
226  if argv.count('maxDiv') > 0:
227  max = options.maxDiv
228  min, max = findRange(hists, min, max)
229  if max > 2:
230  max = 2 #maximal bound
231  hists[0].SetAxisRange(min, max, "Y")
232 
def MultipleCompare.Rebin (   tfile,
  histoPath,
  rebinVal 
)

Definition at line 161 of file MultipleCompare.py.

References FindParents().

Referenced by histoStyle.histoProducer(), and main().

162 def Rebin(tfile, histoPath, rebinVal):
163  parents = FindParents(histoPath)
164  num = tfile.Get(parents[0])
165  if type(num) != TH1F:
166  print 'Looking for '+num
167  print 'Plot now found! What the hell are you doing? Exiting...'
168  sys.exit()
169  denSingle = tfile.Get(parents[1])
170  if type(denSingle) != TH1F:
171  print 'Looking for '+denSingle
172  print 'Plot now found! What the hell are you doing? Exiting...'
173  sys.exit()
174  num.Rebin(rebinVal)
175  den = denSingle.Rebin(rebinVal,'denClone')
176  retVal = num.Clone(histoPath+'Rebin%s'%rebinVal)
177  #print 'Num : ' + parents[0]
178  #print 'Den : ' +parents[1]
179  #print "NumBins: %s DenBins: %s" % (num.GetNbinsX(), den.GetNbinsX() )
180  retVal.Divide(num,den,1,1,'B')
181  return retVal

Variable Documentation

string MultipleCompare.__author__ = "Mauro Verzetti (mauro.verzetti@cern.ch)"

Definition at line 12 of file MultipleCompare.py.

string MultipleCompare.__doc__
Initial value:
1 = """Script to plot the content of a Validation .root file and compare it to a different file:\n\n
2 Usage: MultipleCompare.py -T testFile -R refFile [options] [search strings that you want to apply '*' is supported as special character]"""

Definition at line 13 of file MultipleCompare.py.