15 from optparse
import OptionParser
18 backupArgv = sys.argv[:]
20 from ROOT
import TFile
26 histos = [
"TH1D",
"TH1F",
"TH2D",
"TH2F",
"TProfile"]
43 def rec(o, path=None, lst=None):
48 path = path +
"/" + o.GetName()
50 if "GetListOfKeys" in dir(o):
51 keys = o.GetListOfKeys()
54 rec(o.Get(name), path, lst)
77 if d[k].__class__.__name__
in histos:
108 print(
"Neither tuple is Reference Root file reference?")
120 print(
"\n" +
"=" * 80)
121 print(
"Comparison of Paths : Reference vs Test ROOT files")
124 "Number of paths in Reference file : %i (objects, histos) = ( %i, %i )"
125 % (len(dsks), os, hs)
128 "Number of paths in Test file : %i (objects, histos) = ( %i, %i )"
129 % (len(dpks), op, hp)
131 matching = sset.intersection(pset)
134 if ds[n].__class__.__name__
in histos:
136 print(
"\nMatching paths : %i" % (len(matching)))
139 uniqueReferenceHistos = 0
141 if ds[n].__class__.__name__
in histos:
142 uniqueReferenceHistos += 1
144 "Paths unique to Reference file : %i ( %i Histos )"
145 % (len(uSer), uniqueReferenceHistos)
149 print(
"\t%s : \t%s" % (ds[n], n))
153 if dp[n].__class__.__name__
in histos:
154 uniqueTestHistos += 1
156 "Paths unique to Test file : %i ( %i Histos )" % (len(uPar), uniqueTestHistos)
160 print(
"\t%s : \t%s" % (dp[n], n))
161 print(
"Matching Histos to test : %i" % (matchingHistos))
162 print(
"=" * 80 +
"\n")
164 ((os, hs), (op, hp)),
165 (uSer, uniqueReferenceHistos),
166 (uPar, uniqueTestHistos),
188 return (h.GetNbinsX() + 1) * (biny) * (binz)
192 for ibin
in range(0, nbins):
193 h1bin = h1.GetBinContent(ibin)
194 h2bin = h2.GetBinContent(ibin)
195 diffbins += h1bin != h2bin
217 ((referenceObjects, referenceHistos), (parallObjects, parallHistos)),
218 (uniqueSerPaths, uniqueSerHistos),
219 (uniqueParPaths, uniqueParHistos),
231 print(
"Neither tuple is Reference Root file reference?")
240 omit = [re.compile(regex)
for regex
in gRegexBlackList]
244 if not any(regex.search(k)
is not None for regex
in omit):
245 if ds[k].__class__.__name__
in histos:
250 if not any(regex.search(k)
is not None for regex
in omit):
251 if dp[k].__class__.__name__
in histos:
266 identityDiffBins = {}
272 zeroIntegralMatch = 0
280 if sh.GetEntries() != ph.GetEntries():
281 diffEntries.append(h)
287 for i
in range(sh.GetNbinsX()):
288 sBinError += sh.GetBinError(i)
289 for i
in range(ph.GetNbinsX()):
290 pBinError += ph.GetBinError(i)
293 doKS = (bool(sint)
and bool(pint))
and (sBinError > 0
and pBinError > 0)
294 if checkBin2BinIdentity
and doKS:
300 diffIdentity.append(h)
301 identityDiffBins[h] = diffBins
302 if (bool(sint)
and bool(pint))
and (sBinError > 0
and pBinError > 0):
304 kTest = sh.KolmogorovTest(ph)
305 kTestResults[h] = kTest
315 if all((sint, pint))
and (sint == pint):
318 zeroIntegralMatch += 1
320 diffIntegrals.append(h)
324 print(
"not found? ", h)
327 print(
"\n\n" +
"-" * 80)
328 print(
"Summary of histos with different Entries")
332 for e
in diffEntries:
334 "\t\t\t%s:\t%i != %i"
335 % (e, int(ds[e].GetEntries()), int(dp[e].GetEntries()))
340 print(
"\n\n" +
"-" * 60)
341 print(
"Summary of histos which failed Kolmogorov Test")
346 result = kTestResults[e]
348 "%s\t\t%s :\tK-Test Result :\t %5.16f" % (ds[e].ClassName(), e, result)
353 print(
"\n\n" +
"-" * 60)
354 print(
"Summary of histos which failed Integral Check")
358 for e
in diffIntegrals:
359 diff = dp[e].Integral() - ds[e].Integral()
360 pc = (diff * 100) / ds[e].Integral()
362 "%s\t\t%s:\t Diff = %5.6f\tPercent Diff to Reference : %5.6f "
363 % (ds[e].ClassName(), e, diff, pc)
365 print(
"-" * 60 +
"\n")
366 print(
"=" * 80 +
"\n")
369 if checkBin2BinIdentity:
371 print(
"\n\n" +
"-" * 80)
372 print(
"Summary of histos with at least one bin with different Entries")
376 for e
in diffIdentity:
378 "%s\t\t%s: %i different bins"
379 % (ds[e].ClassName(), e, identityDiffBins[e])
383 print(
"\n" +
"=" * 80)
384 print(
"Comparison : Reference/Test ROOT Histo files")
385 print(
"\n\t\tReference\tTest")
387 "\tObjects : %i\t%i\t\t( p-s = %i )"
388 % (referenceObjects, parallObjects, parallObjects - referenceObjects)
391 "\tHistos : %i\t%i\t\t( p-s = %i )"
392 % (referenceHistos, parallHistos, parallHistos - referenceHistos)
394 print(
"\t __________")
397 % (referenceHistos + referenceObjects, parallHistos + parallObjects)
400 "Objects/Histos unique to Reference File : %i / %i"
401 % (len(uniqueSerPaths) - uniqueSerHistos, uniqueSerHistos)
404 "Objects/Histos unique to Test File : %i / %i"
405 % (len(uniqueParPaths) - uniqueParHistos, uniqueParHistos)
407 print(
"\nMatching Histograms valid for Comparison : %i" % (mh))
408 print(
"\nOmissions' patterns : ")
409 for entry
in gRegexBlackList:
410 print(
"\t%s" % (entry))
412 "\nHistograms for Comparison (after Omissions) : %i"
413 % (mh - len(gRegexBlackList))
415 print(
"\n\tHISTOGRAM TESTS : ")
416 print(
"\t\tKOLMOGOROV TEST : %i" % (kTested))
417 print(
"\t\tINTEGRAL TEST : %i" % (otherTest))
418 print(
"\t\tENTRIES TEST : %i" % (xEntries))
419 if checkBin2BinIdentity:
420 print(
"\t\tBIN2BIN TEST : %i" % (passedIdentity))
422 print(
"\t\tTested : %i" % (cEntries))
424 print(
"\n\tDISCREPANCIES : ")
425 print(
"\t\tK-Test : %i" % (failedKol))
426 print(
"\t\tIntegrals : %i" % (xIntegrals))
427 print(
"\t\tEntries : %i" % (xEntries))
428 retval = failedKol + xIntegrals + xEntries + failedIdentity
430 print(
"\nThe two sets of histograms were not identical")
431 print(
"\n" +
"=" * 80)
439 global gRegexBlackList
441 for blackRegexp
in listString.split(
","):
442 gRegexBlackList.append(blackRegexp)
449 if __name__ ==
"__main__":
450 usage =
"usage: %prog testFile.root referenceFile.root [options]"
451 parser = OptionParser()
455 help=
'Comma separated list of regexps matching histograms to skip (for example -b"MemoryTool,ProcTime")',
463 help=
"Check for bin to bin identity",
465 (options, args) = parser.parse_args()
468 print(
"Wrong number of rootfiles. Usage:")
474 testFile, referenceFile = args
476 tfs = TFile(testFile,
"REC")
477 print(
"opening Test File : %s" % (testFile))
478 tfp = TFile(referenceFile,
"REC")
479 print(
"opening Reference File : %s" % (referenceFile))
485 dref = dict([(n, o)
for n, o
in lref])
486 dtest = dict([(n, o)
for n, o
in ltest])