12 from __future__
import print_function
16 from optparse
import OptionParser
19 backupArgv = sys.argv[:]
21 from ROOT
import TFile
27 histos = [
"TH1D",
"TH1F",
"TH2D",
"TH2F",
"TProfile"]
44 def rec(o, path=None, lst=None):
49 path = path +
"/" + o.GetName()
51 if "GetListOfKeys" in dir(o):
52 keys = o.GetListOfKeys()
55 rec(o.Get(name), path, lst)
78 if d[k].__class__.__name__
in histos:
109 print(
"Neither tuple is Reference Root file reference?")
121 print(
"\n" +
"=" * 80)
122 print(
"Comparison of Paths : Reference vs Test ROOT files")
125 "Number of paths in Reference file : %i (objects, histos) = ( %i, %i )"
126 % (len(dsks), os, hs)
129 "Number of paths in Test file : %i (objects, histos) = ( %i, %i )"
130 % (len(dpks), op, hp)
132 matching = sset.intersection(pset)
135 if ds[n].__class__.__name__
in histos:
137 print(
"\nMatching paths : %i" % (len(matching)))
140 uniqueReferenceHistos = 0
142 if ds[n].__class__.__name__
in histos:
143 uniqueReferenceHistos += 1
145 "Paths unique to Reference file : %i ( %i Histos )"
146 % (len(uSer), uniqueReferenceHistos)
150 print(
"\t%s : \t%s" % (ds[n], n))
154 if dp[n].__class__.__name__
in histos:
155 uniqueTestHistos += 1
157 "Paths unique to Test file : %i ( %i Histos )" % (len(uPar), uniqueTestHistos)
161 print(
"\t%s : \t%s" % (dp[n], n))
162 print(
"Matching Histos to test : %i" % (matchingHistos))
163 print(
"=" * 80 +
"\n")
165 ((os, hs), (op, hp)),
166 (uSer, uniqueReferenceHistos),
167 (uPar, uniqueTestHistos),
189 return (h.GetNbinsX() + 1) * (biny) * (binz)
193 for ibin
in range(0, nbins):
194 h1bin = h1.GetBinContent(ibin)
195 h2bin = h2.GetBinContent(ibin)
196 diffbins += h1bin != h2bin
219 ((referenceObjects, referenceHistos), (parallObjects, parallHistos)),
220 (uniqueSerPaths, uniqueSerHistos),
221 (uniqueParPaths, uniqueParHistos),
233 print(
"Neither tuple is Reference Root file reference?")
242 omit = [re.compile(regex)
for regex
in gRegexBlackList]
246 if not any(regex.search(k) !=
None for regex
in omit):
247 if ds[k].__class__.__name__
in histos:
252 if not any(regex.search(k) !=
None for regex
in omit):
253 if dp[k].__class__.__name__
in histos:
270 identityDiffBins = {}
276 zeroIntegralMatch = 0
284 if sh.GetEntries() != ph.GetEntries():
285 diffEntries.append(h)
291 for i
in range(sh.GetNbinsX()):
292 sBinError += sh.GetBinError(i)
293 for i
in range(ph.GetNbinsX()):
294 pBinError += ph.GetBinError(i)
297 doKS = (bool(sint)
and bool(pint))
and (sBinError > 0
and pBinError > 0)
298 if checkBin2BinIdentity
and doKS:
304 diffIdentity.append(h)
305 identityDiffBins[h] = diffBins
306 if (bool(sint)
and bool(pint))
and (sBinError > 0
and pBinError > 0):
308 kTest = sh.KolmogorovTest(ph)
309 kTestResults[h] = kTest
319 if all((sint, pint))
and (sint == pint):
322 zeroIntegralMatch += 1
324 diffIntegrals.append(h)
328 print(
"not found? ", h)
331 print(
"\n\n" +
"-" * 80)
332 print(
"Summary of histos with different Entries")
336 for e
in diffEntries:
338 "\t\t\t%s:\t%i != %i"
339 % (e, int(ds[e].GetEntries()), int(dp[e].GetEntries()))
344 print(
"\n\n" +
"-" * 60)
345 print(
"Summary of histos which failed Kolmogorov Test")
350 result = kTestResults[e]
352 "%s\t\t%s :\tK-Test Result :\t %5.16f" % (ds[e].ClassName(), e, result)
357 print(
"\n\n" +
"-" * 60)
358 print(
"Summary of histos which failed Integral Check")
362 for e
in diffIntegrals:
363 diff = dp[e].Integral() - ds[e].Integral()
364 pc = (diff * 100) / ds[e].Integral()
366 "%s\t\t%s:\t Diff = %5.6f\tPercent Diff to Reference : %5.6f "
367 % (ds[e].ClassName(), e, diff, pc)
369 print(
"-" * 60 +
"\n")
370 print(
"=" * 80 +
"\n")
373 if checkBin2BinIdentity:
375 print(
"\n\n" +
"-" * 80)
376 print(
"Summary of histos with at least one bin with different Entries")
380 for e
in diffIdentity:
382 "%s\t\t%s: %i different bins"
383 % (ds[e].ClassName(), e, identityDiffBins[e])
387 print(
"\n" +
"=" * 80)
388 print(
"Comparison : Reference/Test ROOT Histo files")
389 print(
"\n\t\tReference\tTest")
391 "\tObjects : %i\t%i\t\t( p-s = %i )"
392 % (referenceObjects, parallObjects, parallObjects - referenceObjects)
395 "\tHistos : %i\t%i\t\t( p-s = %i )"
396 % (referenceHistos, parallHistos, parallHistos - referenceHistos)
398 print(
"\t __________")
401 % (referenceHistos + referenceObjects, parallHistos + parallObjects)
404 "Objects/Histos unique to Reference File : %i / %i"
405 % (len(uniqueSerPaths) - uniqueSerHistos, uniqueSerHistos)
408 "Objects/Histos unique to Test File : %i / %i"
409 % (len(uniqueParPaths) - uniqueParHistos, uniqueParHistos)
411 print(
"\nMatching Histograms valid for Comparison : %i" % (mh))
412 print(
"\nOmissions' patterns : ")
413 for entry
in gRegexBlackList:
414 print(
"\t%s" % (entry))
416 "\nHistograms for Comparison (after Omissions) : %i"
417 % (mh - len(gRegexBlackList))
419 print(
"\n\tHISTOGRAM TESTS : ")
420 print(
"\t\tKOLMOGOROV TEST : %i" % (kTested))
421 print(
"\t\tINTEGRAL TEST : %i" % (otherTest))
422 print(
"\t\tENTRIES TEST : %i" % (xEntries))
423 if checkBin2BinIdentity:
424 print(
"\t\tBIN2BIN TEST : %i" % (passedIdentity))
426 print(
"\t\tTested : %i" % (cEntries))
428 print(
"\n\tDISCREPANCIES : ")
429 print(
"\t\tK-Test : %i" % (failedKol))
430 print(
"\t\tIntegrals : %i" % (xIntegrals))
431 print(
"\t\tEntries : %i" % (xEntries))
432 retval = failedKol + xIntegrals + xEntries + failedIdentity
434 print(
"\nThe two sets of histograms were not identical")
435 print(
"\n" +
"=" * 80)
443 global gRegexBlackList
445 for blackRegexp
in listString.split(
","):
446 gRegexBlackList.append(blackRegexp)
453 if __name__ ==
"__main__":
454 usage =
"usage: %prog testFile.root referenceFile.root [options]"
455 parser = OptionParser()
459 help=
'Comma separated list of regexps matching histograms to skip (for example -b"MemoryTool,ProcTime")',
467 help=
"Check for bin to bin identity",
469 (options, args) = parser.parse_args()
472 print(
"Wrong number of rootfiles. Usage:")
478 testFile, referenceFile = args
480 tfs = TFile(testFile,
"REC")
481 print(
"opening Test File : %s" % (testFile))
482 tfp = TFile(referenceFile,
"REC")
483 print(
"opening Reference File : %s" % (referenceFile))
489 dref = dict([(n, o)
for n, o
in lref])
490 dtest = dict([(n, o)
for n, o
in ltest])