File indexing completed on 2023-03-17 11:28:47
0001
0002
0003 from __future__ import print_function
0004 from builtins import range
0005 import FWCore.ParameterSet.Config as cms
0006 import sys
0007 import os
0008 import math
0009 import re
0010 import Validation.RecoTau.RecoTauValidation_cfi as validation
0011 from optparse import OptionParser
0012 from ROOT import *
0013
0014 __author__ = "Mauro Verzetti (mauro.verzetti@cern.ch) and Lucia Perrini (lucia.perrini@cern.ch)"
0015 __doc__ = """Script to plot the content of a Validation .root file and compare it to a different file:\n\n
0016 Usage: MultipleCompare.py -T testFile -R refFile [options] [search strings that you want to apply '*' is supported as special character]"""
0017
0018 def LoadCommandlineOptions(argv):
0019 sys.argv = argv
0020 parser = OptionParser(description=__doc__)
0021 parser.add_option('--myhelp',metavar='', action="store_true",help='prints this output message',dest='help',default = False)
0022 parser.add_option('--TestFile','-T',metavar='testFile', type=str,help='Sets the test file',dest='test',default = '')
0023 parser.add_option('--RefFile','-R',metavar='refFile', type=str,help='Sets the reference file',dest='ref',default = None)
0024 parser.add_option('--output','-o',metavar='outputFile', type=str,help='Sets the output file',dest='out',default = 'MultipleCompare.png')
0025 parser.add_option('--logScaleY',action="store_true", dest="logScaleY", default=False, help="Sets the log scale in the plot (Y axis)")
0026 parser.add_option('--logScaleX',action="store_true", dest="logScaleX", default=False, help="Sets the log scale in the plot (X axis)")
0027 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)")
0028 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)
0029 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)
0030 parser.add_option('--sampleLabel','-s',metavar='sampleLabel', type=str,help='Sets the label to indicate the sample used',dest='sampleLabel',default = None)
0031 parser.add_option('--maxLogX',metavar='number', type=float,help='Sets the maximum of the scale in log scale both in the main and in the sub pad (requires --logScale or -f to work)',dest='maxLogX',default = 100)
0032 parser.add_option('--minLogX',metavar='number', type=float,help='Sets the minimum of the scale in log scale (requires --logScale or -f to work)',dest='minLogX',default = 0.001)
0033 parser.add_option('--minLogY',metavar='number', type=float,help='Sets the minimum of the scale in log scale (requires --logScale or -f to work)',dest='minLogY',default = 0.0001)
0034 parser.add_option('--maxLogY',metavar='number', type=float,help='Sets the maximum of the scale in log scale (requires --logScale or -f to work)',dest='maxLogY',default = 3)
0035 parser.add_option('--minYR',metavar='number', type=float,help='Sets the minimum of the scale in sub pad',dest='minYR',default = 0)
0036 parser.add_option('--maxYR',metavar='number', type=float,help='Sets the maximum of the scale in sub pad',dest='maxYR',default = 1.2)
0037
0038
0039
0040
0041 parser.add_option('--logDiv',action="store_true", dest="logDiv", default=False, help="Sets the log scale in the plot")
0042 parser.add_option('--normalize',action="store_true", dest="normalize", default=False, help="plot normalized")
0043 parser.add_option('--maxRange',metavar='number',type=float, dest="maxRange", default=1.6, help="Sets the maximum range in linear plots")
0044 parser.add_option('--maxXaxis',metavar='number',type=float, dest="maxXaxis", default=800, help="Sets the maximum range on x axis in the main pad")
0045 parser.add_option('--minXaxis',metavar='number',type=float,help="Sets the minimum range on x axis in the main pad",dest="minXaxis", default=-3)
0046 parser.add_option('--maxYaxis',metavar='number',type=float, dest="maxYaxis", default=2, help="Sets the maximum range on Y axis in the main pad")
0047 parser.add_option('--minYaxis',metavar='number',type=float, dest="minYaxis", default=0, help="Sets the minimum range on Y axis in the main pad")
0048 parser.add_option('--rebin', dest="rebin", type=int, default=-1, help="Sets the rebinning scale")
0049 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)
0050
0051
0052 (options,toPlot) = parser.parse_args()
0053 if options.help:
0054 parser.print_help()
0055 sys.exit(0)
0056 return [options, toPlot]
0057
0058 def GetContent(dir):
0059 tempList = dir.GetListOfKeys()
0060 retList = []
0061 for it in range(0,tempList.GetSize()):
0062 retList.append(tempList.At(it).ReadObj())
0063 return retList
0064
0065 def MapDirStructure( directory, dirName, objectList ):
0066 dirContent = GetContent(directory)
0067 for entry in dirContent:
0068 if isinstance(entry, TDirectory) or isinstance(entry, TDirectoryFile):
0069 subdirName = os.path.join(dirName,entry.GetName())
0070 MapDirStructure(entry, subdirName,objectList)
0071 else:
0072 pathname = os.path.join(dirName,entry.GetName())
0073 objectList.append(pathname)
0074
0075 def Match(required, got):
0076 for part in required.split('*'):
0077 if got.find(part) == -1:
0078 return False
0079 return True
0080
0081 def Divide(hNum,hDen):
0082 ret = hNum.Clone('Division')
0083 ret.GetYaxis().SetTitle('Ratio')
0084 for binI in range(hNum.GetNbinsX()+1):
0085 denVal = hDen.GetBinContent(binI)
0086 denErr = hDen.GetBinError(binI)
0087 numErr = hNum.GetBinError(binI)
0088 numVal = hNum.GetBinContent(binI)
0089 if denVal == 0:
0090 ret.SetBinContent(binI,0)
0091 ret.SetBinError(binI,0)
0092 else:
0093 ret.SetBinContent(binI,numVal/denVal)
0094 if numVal==0:
0095 ret.SetBinError(binI,1)
0096 else:
0097 ret.SetBinError(binI,(numVal/denVal)*math.sqrt(math.pow(numErr/numVal,2) + math.pow(denErr/denVal,2) ) )
0098 return ret
0099
0100 def DetermineHistType(name):
0101
0102 type = ''
0103 label = ''
0104 prefix = ''
0105
0106 matches = re.match(r'.*/(.*)_(.*)_(.*)', name)
0107 if matches:
0108 prefix = matches.group(1)
0109 label = matches.group(3)
0110 knowntypes = (['pTRatio','SumPt','Size'])
0111 for knowntype in knowntypes:
0112 if matches.group(2) == knowntype:
0113 type = knowntype
0114 if not type:
0115 type = 'Eff'
0116 else:
0117 type = 'Eff'
0118
0119 prefixParts = prefix.partition('Discrimination')
0120 if prefixParts[2] != '':
0121 prefix = prefixParts[2]
0122 prefixParts = prefix.partition('By')
0123 if prefixParts[2] != '':
0124 prefix = prefixParts[2]
0125
0126
0127 return [type, label, prefix]
0128
0129 def DrawTitle(text):
0130 title = TLatex()
0131 title.SetNDC()
0132 title.SetTextAlign(12)
0133 title.SetTextSize(.035)
0134 leftMargin = gStyle.GetPadLeftMargin()
0135 topMargin = 1 - 0.5*gStyle.GetPadTopMargin()
0136 title.DrawLatex(leftMargin, topMargin, text)
0137
0138 def DrawBranding(options, label=''):
0139 if options.branding != None or label != '':
0140 text = TLatex()
0141 text.SetNDC();
0142 text.SetTextAlign(11)
0143 text.SetTextSize(.025)
0144 text.SetTextColor(13)
0145 if options.out.find(".eps")!=-1:
0146 text.SetTextAngle(-91.0)
0147 else:
0148 text.SetTextAngle(-90.0)
0149 rightMargin = 1 - gStyle.GetPadRightMargin()
0150 topMargin = 1 - gStyle.GetPadTopMargin()
0151 if label!='':
0152 label += ': '
0153 text.DrawLatex(rightMargin+.01, topMargin+0.025, label+options.branding);
0154
0155
0156 def FindParents(histoPath):
0157 root = histoPath[:histoPath.find('_')]
0158 par = histoPath[histoPath.find('Eff')+3:]
0159 validationPlots = validation.proc.efficiencies.plots._Parameterizable__parameterNames
0160 found =0
0161 num = ''
0162 den = ''
0163 for efficiency in validationPlots:
0164 effpset = getattr(validation.proc.efficiencies.plots,efficiency)
0165 effName = effpset.efficiency.value()
0166 effNameCut = effName[effName.find('_'):effName.find('#')]
0167 if effNameCut in histoPath:
0168 if found == 1:
0169 print('More than one pair of parents found for ' + histopath + ':')
0170 assert(False)
0171 num = root + effpset.numerator.value()[effName.find('_'):].replace('#PAR#',par)
0172 den = root + effpset.denominator.value()[effName.find('_'):].replace('#PAR#',par)
0173 found += 1
0174 return [num,den]
0175
0176 def Rebin(tfile, histoPath, rebinVal):
0177 parents = FindParents(histoPath)
0178 num = tfile.Get(parents[0])
0179 if not isinstance(num, TH1F):
0180 print('Looking for ' + num)
0181 print('Plot now found! What the hell are you doing? Exiting...')
0182 sys.exit()
0183 denSingle = tfile.Get(parents[1])
0184 if not isinstance(denSingle, TH1F):
0185 print('Looking for '+denSingle)
0186 print('Plot now found! What the hell are you doing? Exiting...')
0187 sys.exit()
0188 num.Rebin(rebinVal)
0189 den = denSingle.Rebin(rebinVal,'denClone')
0190 retVal = num.Clone(histoPath+'Rebin%s'%rebinVal)
0191
0192
0193
0194 retVal.Divide(num,den,1,1,'B')
0195 return retVal
0196
0197 def findRange(hists, min0=-1, max0=-1):
0198 if len(hists) < 1:
0199 return
0200
0201 min = min0
0202 max = max0
0203 if min0 == -1 or max0 == -1:
0204 for hist in hists:
0205 if min0 == -1:
0206
0207 minTmp = getMinimumIncludingErrors(hist)
0208 if minTmp < min or min == -1:
0209 min = minTmp
0210 if max0 == -1:
0211 maxTmp = getMaximumIncludingErrors(hist)
0212 if maxTmp > max or max == -1:
0213 max = maxTmp
0214 return [min, max]
0215
0216 def optimizeRangeMainPad(argv, pad, hists, maxLogX_, minX_, maxX_, maxLogY_, minY_, maxY_):
0217 pad.Update()
0218 if pad.GetLogy():
0219 if maxLogY_ > 0:
0220 maxLogY = maxLogY_
0221 else:
0222 maxLogY = -1
0223 minY, maxY = findRange(hists, -1, maxLogY)
0224 else:
0225 minY, maxY = findRange(hists, minY_, maxY_)
0226
0227 if pad.GetLogy():
0228 if minY == 0:
0229 minY = 0.001
0230 else:
0231 if minY < 0.7:
0232 minY = minY
0233 if maxY <= 1.1 and maxY > 0.7:
0234 maxY = 1.2
0235 hists[0].SetAxisRange(minY, maxY, "Y")
0236
0237 if pad.GetLogx():
0238 if maxLogX_ > 0:
0239 maxLogX = maxLogX_
0240 else:
0241 maxLogX = -1
0242 minX, maxX = findRange(hists, -1, maxLogX)
0243 else:
0244 minX, maxX = findRange(hists, minX_, maxX_)
0245
0246 if pad.GetLogx():
0247 if minX == 0:
0248 minX = 0.001
0249 else:
0250 if minX < 0.7:
0251 minX = minX
0252 if maxX <= 1.1 and maxX > 0.7:
0253 maxX = 1.2
0254 hists[0].SetAxisRange(minX, maxX, "X")
0255
0256 def optimizeRangeSubPad(argv, pad, hists, maxLogX_, minX_, maxX_, minYRatio_, maxYRatio_):
0257 pad.Update()
0258 if pad.GetLogx():
0259 if maxLogX_ > 0:
0260 maxLogX = maxLogX_
0261 else:
0262 maxLogX = -1
0263 minX, maxX = findRange(hists, -1, maxLogX)
0264 else:
0265 minX, maxX = findRange(hists, minX_, maxX_)
0266 if pad.GetLogx():
0267 if minX == 0:
0268 minX = 0.001
0269 else:
0270 if minX < 0.7:
0271 minX = minX
0272 if maxX <= 1.1 and maxX > 0.7:
0273 maxX = 1.2
0274 hists[0].SetAxisRange(minX, maxX, "X")
0275
0276 min = -1
0277 max = -1
0278 if minYRatio_ > 0:
0279 min = minYRatio_
0280 if maxYRatio_ > 0:
0281 max = maxYRatio_
0282 min, max = findRange(hists, min, max)
0283 if max > 2:
0284 max = 2
0285 hists[0].SetAxisRange(min, max, "Y")
0286
0287 def getMaximumIncludingErrors(hist):
0288
0289 distance = 1.
0290 max = -1
0291 pos = 0
0292 for i in range(1, hist.GetNbinsX()):
0293 if hist.GetBinContent(i) > max:
0294 max = hist.GetBinContent(i)
0295 pos = i
0296 return max + distance*hist.GetBinError(pos)
0297
0298 def getMinimumIncludingErrors(hist):
0299
0300
0301 distance = 1.
0302 min = -1
0303 pos = 0
0304 for i in range(1, hist.GetNbinsX()):
0305 if hist.GetBinContent(i)<=0.:
0306 continue
0307 if hist.GetBinContent(i) < min or min==-1:
0308 min = hist.GetBinContent(i)
0309 pos = i
0310 if min < 0:
0311 min = 0
0312 return min - distance*hist.GetBinError(pos)
0313
0314
0315 def main(argv=None):
0316 if argv is None:
0317 argv = sys.argv
0318
0319 options, toPlot = LoadCommandlineOptions(argv)
0320
0321 gROOT.SetStyle('Plain')
0322 gROOT.SetBatch()
0323 gStyle.SetPalette(1)
0324 gStyle.SetOptStat(0)
0325 gStyle.SetPadGridX(True)
0326 gStyle.SetPadGridY(True)
0327 gStyle.SetOptTitle(0)
0328 gStyle.SetPadTopMargin(0.1)
0329 gStyle.SetPadBottomMargin(0.1)
0330 gStyle.SetPadLeftMargin(0.13)
0331 gStyle.SetPadRightMargin(0.07)
0332
0333
0334 testFile = TFile(options.test)
0335 refFile = None
0336 if options.ref != None:
0337 refFile = TFile(options.ref)
0338
0339
0340 plotList = []
0341 MapDirStructure( testFile,'',plotList)
0342
0343 histoList = []
0344 for plot in toPlot:
0345 for path in plotList:
0346 if Match(plot.lower(),path.lower()):
0347 histoList.append(path)
0348
0349
0350
0351 print(histoList)
0352
0353 if len(histoList)<1:
0354 print('\tError: Please specify at least one histogram.')
0355 if len(toPlot)>0:
0356 print('Check your plot list:', toPlot)
0357 sys.exit()
0358
0359
0360
0361 histType, label, prefix = DetermineHistType(histoList[0])
0362
0363
0364 scaleToIntegral = False
0365 if options.normalize:
0366 scaleToIntegral = True
0367
0368 ylabel = 'Efficiency'
0369
0370 if options.fakeRate:
0371 ylabel = 'Fake rate'
0372
0373 drawStats = False
0374 if histType=='pTRatio' and len(histoList)<3:
0375 drawStats = True
0376
0377
0378 x1 = 0.33
0379 x2 = 1-gStyle.GetPadRightMargin()
0380 y2 = 1-gStyle.GetPadTopMargin()
0381 lineHeight = .055
0382 if len(histoList) == 1:
0383 lineHeight = .05
0384 y1 = y2 - lineHeight*len(histoList)
0385 legend = TLegend(x1,y1,x2,y2)
0386 legend.SetHeader(label)
0387 legend.SetFillColor(0)
0388 legend.SetTextSize(0.032)
0389 if drawStats:
0390 y2 = y1
0391 y1 = y2 - .07*len(histoList)
0392 statsBox = TPaveText(x1,y1,x2,y2,"NDC")
0393 statsBox.SetFillColor(0)
0394 statsBox.SetTextAlign(12)
0395 statsBox.SetMargin(0.05)
0396 statsBox.SetBorderSize(1)
0397
0398
0399 canvas = TCanvas('MultiPlot','MultiPlot',validation.standardDrawingStuff.canvasSizeX.value(),832)
0400 effPad = TPad('effPad','effPad',0.01,0.35,0.99,0.99)
0401 effPad.SetBottomMargin(0.0)
0402
0403
0404
0405 effPad.Draw()
0406 header = ''
0407 if options.sampleLabel != None:
0408 header += 'Sample: '+options.sampleLabel
0409 if options.testLabel != None:
0410 header += ' Dots: '+options.testLabel
0411 if options.refLabel != None:
0412 header += ' Line: '+options.refLabel
0413 DrawTitle(header)
0414 DrawBranding(options)
0415 diffPad = TPad('diffPad','diffPad',0.01,0.01,0.99,0.32)
0416 diffPad.SetTopMargin(0.00);
0417 diffPad.SetBottomMargin(0.30);
0418 diffPad.Draw()
0419 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]
0420 first = True
0421 divHistos = []
0422 statTemplate = '%s Mean: %.3f RMS: %.3f'
0423 testHs = []
0424 refHs = []
0425 for histoPath,color in zip(histoList,colors):
0426 if(options.rebin == -1):
0427 testH = testFile.Get(histoPath)
0428 else:
0429 testH = Rebin(testFile,histoPath,options.rebin)
0430 if not isinstance(testH, TH1F):
0431 print('Looking for '+histoPath)
0432 print('Test plot now found! What the hell are you doing? Exiting...')
0433 sys.exit()
0434 testHs.append(testH)
0435 xAx = histoPath[histoPath.find('Eff')+len('Eff'):]
0436 effPad.cd()
0437 if not testH.GetXaxis().GetTitle():
0438 if hasattr(validation.standardDrawingStuff.xAxes,xAx):
0439 testH.GetXaxis().SetTitle( getattr(validation.standardDrawingStuff.xAxes,xAx).xAxisTitle.value())
0440 if not testH.GetYaxis().GetTitle():
0441 testH.GetYaxis().SetTitle(ylabel)
0442 if label!='':
0443 testH.GetXaxis().SetTitle(label+': '+testH.GetXaxis().GetTitle())
0444 testH.GetXaxis().SetTitleOffset(1.1)
0445 testH.GetXaxis().SetRangeUser(options.minXaxis,options.maxXaxis)
0446 testH.GetYaxis().SetTitleOffset(1.1)
0447
0448
0449 testH.SetMarkerSize(1)
0450 testH.SetMarkerStyle(20)
0451 testH.SetMarkerColor(color)
0452 if histType == 'Eff':
0453 legend.AddEntry(testH,histoPath[histoPath.rfind('/')+1:histoPath.find(histType)],'p')
0454 else:
0455 legend.AddEntry(testH,DetermineHistType(histoPath)[2],'p')
0456 if drawStats:
0457 text = statsBox.AddText(statTemplate % ('Dots',testH.GetMean(), testH.GetRMS()) )
0458 text.SetTextColor(color)
0459 if first:
0460 first = False
0461 if options.logScaleY:
0462 effPad.SetLogy()
0463 if options.logScaleX:
0464 effPad.SetLogx()
0465 diffPad.SetLogx()
0466 if scaleToIntegral:
0467 if testH.GetEntries() > 0:
0468 if not testH.GetSumw2N():
0469 testH.Sumw2()
0470 testH.DrawNormalized('ex0 P')
0471 else:
0472 print("--> Warning! You tried to normalize a histogram which seems to be already scaled properly. Draw it unscaled.")
0473 scaleToIntegral = False
0474 testH.Draw('ex0')
0475 else:
0476 testH.Draw('ex0')
0477 else:
0478 if scaleToIntegral:
0479 if testH.GetEntries() > 0:
0480 testH.DrawNormalized('same p')
0481 else:
0482 testH.Draw('same ex0 l')
0483 if refFile == None:
0484 continue
0485 if(options.rebin == -1):
0486 refH = refFile.Get(histoPath)
0487 else:
0488 refH = Rebin(refFile,histoPath,options.rebin)
0489 if not isinstance(refH, TH1F):
0490 continue
0491 refHs.append(refH)
0492 refH.SetLineColor(color)
0493 refH.SetLineWidth(1)
0494 if scaleToIntegral:
0495 if testH.GetEntries() > 0:
0496 refH.DrawNormalized('same hist')
0497 else:
0498 refH.DrawCopy('same hist')
0499 if drawStats:
0500 text = statsBox.AddText(statTemplate % ('Line',refH.GetMean(), refH.GetRMS()) )
0501 text.SetTextColor(color)
0502
0503
0504 if scaleToIntegral:
0505 entries = testH.GetEntries()
0506 if entries > 0:
0507 testH.Scale(1./entries)
0508 entries = refH.GetEntries()
0509 refH.Sumw2()
0510 if entries > 0:
0511 refH.Scale(1./entries)
0512 refH.Draw('same hist')
0513 divHistos.append(Divide(testH,refH))
0514
0515 if options.maxLogY > 0:
0516 maxlY=options.maxLogY
0517 if options.maxLogX > 0:
0518 maxlX=options.maxLogX
0519
0520 tmpHists = []
0521 tmpHists.extend(testHs)
0522 tmpHists.extend(refHs)
0523 optimizeRangeMainPad(argv, effPad, tmpHists, maxlX, options.minXaxis, options.maxXaxis, maxlY, options.minYaxis, options.maxYaxis)
0524
0525 firstD = True
0526 if refFile != None:
0527 for histo,color in zip(divHistos,colors):
0528 diffPad.cd()
0529 histo.SetMarkerSize(1)
0530 histo.SetMarkerStyle(20)
0531 histo.SetMarkerColor(color)
0532 histo.GetYaxis().SetLabelSize(0.07)
0533 histo.GetYaxis().SetTitleOffset(0.75)
0534 histo.GetYaxis().SetTitleSize(0.08)
0535 histo.GetXaxis().SetLabelSize(0.08)
0536 histo.GetXaxis().SetTitleSize(0.08)
0537
0538
0539
0540 if firstD:
0541 histo.Draw('ex0')
0542 firstD = False
0543 else:
0544 histo.Draw('same ex0')
0545 diffPad.Update()
0546
0547 if options.maxLogX > 0:
0548 maxlX=options.maxLogX
0549 optimizeRangeSubPad(argv, diffPad, divHistos, maxlX, options.minXaxis, options.maxXaxis, options.minYR, options.maxYR)
0550
0551 effPad.cd()
0552 legend.Draw()
0553
0554 if drawStats:
0555 statsBox.Draw()
0556
0557 canvas.Print(options.out)
0558
0559
0560 if __name__ == '__main__':
0561 sys.exit(main())