The Gaudi Framework  v36r11 (bdb84f5f)
compareRootHistos Namespace Reference

Functions

def rec (o, path=None, lst=None)
 
def composition (t)
 
def comparePaths (t1, t2)
 
def bin2binIdentity (h1, h2)
 
def compareHistos (t1, t2, state, checkBin2BinIdentity)
 
def extractBlacklist (listString)
 

Variables

 backupArgv = sys.argv[:]
 
 argv
 
list gRegexBlackList = []
 
list histos = ["TH1D", "TH1F", "TH2D", "TH2F", "TProfile"]
 
string ref = "REFERENCE"
 
string test = "TEST"
 
string usage = "usage: %prog testFile.root referenceFile.root [options]"
 
 parser = OptionParser()
 
 dest
 
 help
 
 action
 
 default
 
 options
 
 args
 
 testFile
 
 referenceFile
 
 tfs = TFile(testFile, "REC")
 
 tfp = TFile(referenceFile, "REC")
 
def lref = rec(tfs)
 
def ltest = rec(tfp)
 
 dref = dict([(n, o) for n, o in lref])
 
 dtest = dict([(n, o) for n, o in ltest])
 
tuple ts = (ref, dref)
 
tuple tp = (test, dtest)
 
def state = comparePaths(ts, tp)
 
def retval = compareHistos(ts, tp, state, checkBin2BinIdentity=options.bin2bin)
 

Function Documentation

◆ bin2binIdentity()

def compareRootHistos.bin2binIdentity (   h1,
  h2 
)

Definition at line 181 of file compareRootHistos.py.

181 def bin2binIdentity(h1, h2):
182  def getNbins(h):
183  biny = h.GetNbinsY()
184  if biny > 1:
185  biny += 1
186  binz = h.GetNbinsZ()
187  if binz > 1:
188  binz += 1
189  return (h.GetNbinsX() + 1) * (biny) * (binz)
190 
191  nbins = getNbins(h1)
192  diffbins = 0
193  for ibin in range(0, nbins):
194  h1bin = h1.GetBinContent(ibin)
195  h2bin = h2.GetBinContent(ibin)
196  diffbins += h1bin != h2bin
197  return diffbins
198 
199 
200 # =============================================================================
201 # Method : compareHistos( t1, t2 )
202 #
203 # @param t1, t2 : a tuple of ( type, d ) where type is either 'REFERENCE' or 'TEST'
204 # and d is a dictionary of ROOT objects, with each key = ROOT path
205 #
206 # function : compare the histograms in Reference/Test ROOT files. First, go through each
207 # dict to collect the histos (ignore TDirectory objects, etc). Then the histos
208 # in the test file (experimental) are compared to their equivalents in the
209 # reference file (definitely correct) using 3 methods.
210 # 1) The entries are checked, they should be equal
211 # 2) If entries are equal, check the Integral(); should be equal
212 # 3) If integrals are equal, check the KolmogorovTest() ; should be 1
213 # 4) If identity flag is there and KS test is performed, perform bin2bin identity test
214 # Arguments t1 and t2 are checked and the test/reference auto-detected
215 #

◆ compareHistos()

def compareRootHistos.compareHistos (   t1,
  t2,
  state,
  checkBin2BinIdentity 
)

Definition at line 216 of file compareRootHistos.py.

216 def compareHistos(t1, t2, state, checkBin2BinIdentity):
217 
218  (
219  ((referenceObjects, referenceHistos), (parallObjects, parallHistos)),
220  (uniqueSerPaths, uniqueSerHistos),
221  (uniqueParPaths, uniqueParHistos),
222  mh,
223  ) = state
224 
225  # deduce which one is test, which reference
226  if t1[0] == ref:
227  ds = t1[1]
228  dp = t2[1]
229  elif t2[0] == ref:
230  ds = t2[1]
231  dp = t1[1]
232  else:
233  print("Neither tuple is Reference Root file reference?")
234  return
235 
236  # histocount, objectcount for test/reference
237  hcp = 0
238  pHistos = []
239  hcs = 0
240  sHistos = []
241 
242  omit = [re.compile(regex) for regex in gRegexBlackList]
243 
244  # find the histos in the reference file
245  for k in ds.keys():
246  if not any(regex.search(k) != None for regex in omit):
247  if ds[k].__class__.__name__ in histos:
248  hcs += 1
249  sHistos.append(k)
250  # same for test
251  for k in dp.keys():
252  if not any(regex.search(k) != None for regex in omit):
253  if dp[k].__class__.__name__ in histos:
254  hcp += 1
255  pHistos.append(k)
256 
257  cEntries = 0
258  xEntries = 0
259  diffEntries = []
260  cIntegrals = 0
261  xIntegrals = 0
262  diffIntegrals = []
263  passedKol = 0
264  failedKol = 0
265  diffKols = []
266  zeroIntegrals = 0
267  passedIdentity = 0
268  failedIdentity = 0
269  diffIdentity = []
270  identityDiffBins = {}
271  kTested = 0
272  kTestResults = {}
273  notfound = 0
274  integralMatch = 0
275  otherTest = 0
276  zeroIntegralMatch = 0
277  for h in sHistos:
278  if h in pHistos:
279  # matching histos to check
280  cEntries += 1
281  sh = ds[h]
282  ph = dp[h]
283  # first check entries
284  if sh.GetEntries() != ph.GetEntries():
285  diffEntries.append(h)
286  xEntries += 1
287  continue
288  # check for (non-zero sum of bin error) && (non-zero integrals) for K-Test
289  sBinError = 0.0
290  pBinError = 0.0
291  for i in range(sh.GetNbinsX()):
292  sBinError += sh.GetBinError(i)
293  for i in range(ph.GetNbinsX()):
294  pBinError += ph.GetBinError(i)
295  sint = sh.Integral()
296  pint = ph.Integral()
297  doKS = (bool(sint) and bool(pint)) and (sBinError > 0 and pBinError > 0)
298  if checkBin2BinIdentity and doKS:
299  diffBins = bin2binIdentity(sh, ph)
300  if diffBins == 0:
301  passedIdentity += 1
302  else:
303  failedIdentity += 1
304  diffIdentity.append(h)
305  identityDiffBins[h] = diffBins
306  if (bool(sint) and bool(pint)) and (sBinError > 0 and pBinError > 0):
307  kTested += 1
308  kTest = sh.KolmogorovTest(ph)
309  kTestResults[h] = kTest
310  if int(kTest):
311  passedKol += 1
312  else:
313  # ; print 'KTest result : ', kTest
314  failedKol += 1
315  diffKols.append(h)
316  else:
317  # try the integral test?
318  otherTest += 1
319  if all((sint, pint)) and (sint == pint):
320  integralMatch += 1
321  elif sint == pint:
322  zeroIntegralMatch += 1
323  else:
324  diffIntegrals.append(h)
325  xIntegrals += 1
326  else:
327  notfound += 1
328  print("not found? ", h)
329 
330  # report on Failed Entry-Checks
331  print("\n\n" + "-" * 80)
332  print("Summary of histos with different Entries")
333  print("-" * 80)
334  if diffEntries:
335  diffEntries.sort()
336  for e in diffEntries:
337  print(
338  "\t\t\t%s:\t%i != %i"
339  % (e, int(ds[e].GetEntries()), int(dp[e].GetEntries()))
340  )
341  print("-" * 80)
342 
343  # report on Failed Kolmogorov Tests
344  print("\n\n" + "-" * 60)
345  print("Summary of histos which failed Kolmogorov Test")
346  print("-" * 60)
347  if diffKols:
348  diffKols.sort()
349  for e in diffKols:
350  result = kTestResults[e] # DP Calculated twice ARGH!!
351  print(
352  "%s\t\t%s :\tK-Test Result :\t %5.16f" % (ds[e].ClassName(), e, result)
353  )
354  print("-" * 60)
355 
356  # report on Failed Integral Checks
357  print("\n\n" + "-" * 60)
358  print("Summary of histos which failed Integral Check")
359  print("-" * 60)
360  if diffIntegrals:
361  diffIntegrals.sort()
362  for e in diffIntegrals:
363  diff = dp[e].Integral() - ds[e].Integral()
364  pc = (diff * 100) / ds[e].Integral()
365  print(
366  "%s\t\t%s:\t Diff = %5.6f\tPercent Diff to Reference : %5.6f "
367  % (ds[e].ClassName(), e, diff, pc)
368  )
369  print("-" * 60 + "\n")
370  print("=" * 80 + "\n")
371 
372  # Report on failed bin2bin identity
373  if checkBin2BinIdentity:
374  # report on b2b checks
375  print("\n\n" + "-" * 80)
376  print("Summary of histos with at least one bin with different Entries")
377  print("-" * 80)
378  if diffIdentity:
379  diffIdentity.sort()
380  for e in diffIdentity:
381  print(
382  "%s\t\t%s: %i different bins"
383  % (ds[e].ClassName(), e, identityDiffBins[e])
384  )
385  print("-" * 80)
386 
387  print("\n" + "=" * 80)
388  print("Comparison : Reference/Test ROOT Histo files")
389  print("\n\t\tReference\tTest")
390  print(
391  "\tObjects : %i\t%i\t\t( p-s = %i )"
392  % (referenceObjects, parallObjects, parallObjects - referenceObjects)
393  )
394  print(
395  "\tHistos : %i\t%i\t\t( p-s = %i )"
396  % (referenceHistos, parallHistos, parallHistos - referenceHistos)
397  )
398  print("\t __________")
399  print(
400  "\tTotal : %i\t%i\n"
401  % (referenceHistos + referenceObjects, parallHistos + parallObjects)
402  )
403  print(
404  "Objects/Histos unique to Reference File : %i / %i"
405  % (len(uniqueSerPaths) - uniqueSerHistos, uniqueSerHistos)
406  )
407  print(
408  "Objects/Histos unique to Test File : %i / %i"
409  % (len(uniqueParPaths) - uniqueParHistos, uniqueParHistos)
410  )
411  print("\nMatching Histograms valid for Comparison : %i" % (mh))
412  print("\nOmissions' patterns : ")
413  for entry in gRegexBlackList:
414  print("\t%s" % (entry))
415  print(
416  "\nHistograms for Comparison (after Omissions) : %i"
417  % (mh - len(gRegexBlackList))
418  )
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))
425  print("\t\t ____")
426  print("\t\tTested : %i" % (cEntries))
427 
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
433  if retval != 0:
434  print("\nThe two sets of histograms were not identical")
435  print("\n" + "=" * 80)
436  return retval
437 
438 
439 # =============================================================================
440 
441 

◆ comparePaths()

def compareRootHistos.comparePaths (   t1,
  t2 
)

Definition at line 101 of file compareRootHistos.py.

101 def comparePaths(t1, t2):
102  if t1[0] == ref:
103  ds = t1[1]
104  dp = t2[1]
105  elif t2[0] == ref:
106  ds = t2[1]
107  dp = t1[1]
108  else:
109  print("Neither tuple is Reference Root file reference?")
110  return
111 
112  dsks = ds.keys()
113  dpks = dp.keys()
114  dsks.sort()
115  dpks.sort()
116 
117  sset = set(dsks)
118  pset = set(dpks)
119  os, hs = composition((ref, ds))
120  op, hp = composition((test, dp))
121  print("\n" + "=" * 80)
122  print("Comparison of Paths : Reference vs Test ROOT files")
123  print("-" * 80)
124  print(
125  "Number of paths in Reference file : %i (objects, histos) = ( %i, %i )"
126  % (len(dsks), os, hs)
127  )
128  print(
129  "Number of paths in Test file : %i (objects, histos) = ( %i, %i )"
130  % (len(dpks), op, hp)
131  )
132  matching = sset.intersection(pset)
133  matchingHistos = 0
134  for n in matching:
135  if ds[n].__class__.__name__ in histos:
136  matchingHistos += 1
137  print("\nMatching paths : %i" % (len(matching)))
138  uSer = sset - pset
139  # work out histos unique to test file
140  uniqueReferenceHistos = 0
141  for n in uSer:
142  if ds[n].__class__.__name__ in histos:
143  uniqueReferenceHistos += 1
144  print(
145  "Paths unique to Reference file : %i ( %i Histos )"
146  % (len(uSer), uniqueReferenceHistos)
147  )
148  if uSer:
149  for n in uSer:
150  print("\t%s : \t%s" % (ds[n], n))
151  uPar = pset - sset
152  uniqueTestHistos = 0
153  for n in uPar:
154  if dp[n].__class__.__name__ in histos:
155  uniqueTestHistos += 1
156  print(
157  "Paths unique to Test file : %i ( %i Histos )" % (len(uPar), uniqueTestHistos)
158  )
159  if uPar:
160  for n in uPar:
161  print("\t%s : \t%s" % (dp[n], n))
162  print("Matching Histos to test : %i" % (matchingHistos))
163  print("=" * 80 + "\n")
164  return (
165  ((os, hs), (op, hp)),
166  (uSer, uniqueReferenceHistos),
167  (uPar, uniqueTestHistos),
168  matchingHistos,
169  )
170 
171 
172 # =============================================================================
173 
174 # =============================================================================
175 # Method : bin2binIdentity(h1,h2)
176 #
177 # @param h1, h2 : The two histogtams to compare
178 # function : Return the number of different bins
179 
180 

◆ composition()

def compareRootHistos.composition (   t)

Definition at line 73 of file compareRootHistos.py.

73 def composition(t):
74  typ, d = t
75  hists = 0
76  objs = 0
77  for k in d.keys():
78  if d[k].__class__.__name__ in histos:
79  hists += 1
80  else:
81  objs += 1
82  return objs, hists
83 
84 
85 # =============================================================================
86 
87 # =============================================================================
88 # Method : comparePaths( t1, t2 )
89 #
90 # @param t1, t2 : a tuple of ( type, d ) where type is either 'REFERENCE' or 'TEST'
91 # and d is a dictionary of ROOT objects, with each key = ROOT path
92 #
93 # function : compare the paths between the two histo files. If the files are identical, they
94 # should have the same set of paths. The Test file should definitely have the
95 # same paths as the Reference. Perhaps the Reference file will have some more paths due
96 # to extra histos added as part of Application Sequencer finalisation
97 # Arguments t1 and t2 are checked and the test/reference auto-detected
98 #
99 
100 

◆ extractBlacklist()

def compareRootHistos.extractBlacklist (   listString)

Definition at line 442 of file compareRootHistos.py.

442 def extractBlacklist(listString):
443  global gRegexBlackList
444  if listString:
445  for blackRegexp in listString.split(","):
446  gRegexBlackList.append(blackRegexp)
447  else:
448  gBlackList = []
449 
450 
451 # =============================================================================
452 

◆ rec()

def compareRootHistos.rec (   o,
  path = None,
  lst = None 
)

Definition at line 44 of file compareRootHistos.py.

44 def rec(o, path=None, lst=None):
45  if not path:
46  path = "/stat"
47  lst = []
48  else:
49  path = path + "/" + o.GetName()
50  lst.append((path, o))
51  if "GetListOfKeys" in dir(o):
52  keys = o.GetListOfKeys()
53  for k in keys:
54  name = k.GetName()
55  rec(o.Get(name), path, lst)
56  else:
57  pass
58  return lst
59 
60 
61 # =============================================================================
62 
63 # =============================================================================
64 # Method : composition( t )
65 #
66 # @param t : a tuple of ( type, d ) where type is either 'REFERENCE' or 'TEST'
67 # and d is a dictionary of ROOT objects, with each key = ROOT path
68 #
69 # function : deduce the composition, (objects/histos) counts
70 #
71 
72 

Variable Documentation

◆ action

compareRootHistos.action

Definition at line 464 of file compareRootHistos.py.

◆ args

compareRootHistos.args

Definition at line 469 of file compareRootHistos.py.

◆ argv

compareRootHistos.argv

Definition at line 20 of file compareRootHistos.py.

◆ backupArgv

compareRootHistos.backupArgv = sys.argv[:]

Definition at line 19 of file compareRootHistos.py.

◆ default

compareRootHistos.default

Definition at line 466 of file compareRootHistos.py.

◆ dest

compareRootHistos.dest

Definition at line 458 of file compareRootHistos.py.

◆ dref

compareRootHistos.dref = dict([(n, o) for n, o in lref])

Definition at line 489 of file compareRootHistos.py.

◆ dtest

compareRootHistos.dtest = dict([(n, o) for n, o in ltest])

Definition at line 490 of file compareRootHistos.py.

◆ gRegexBlackList

list compareRootHistos.gRegexBlackList = []

Definition at line 26 of file compareRootHistos.py.

◆ help

compareRootHistos.help

Definition at line 459 of file compareRootHistos.py.

◆ histos

list compareRootHistos.histos = ["TH1D", "TH1F", "TH2D", "TH2F", "TProfile"]

Definition at line 27 of file compareRootHistos.py.

◆ lref

def compareRootHistos.lref = rec(tfs)

Definition at line 486 of file compareRootHistos.py.

◆ ltest

def compareRootHistos.ltest = rec(tfp)

Definition at line 487 of file compareRootHistos.py.

◆ options

compareRootHistos.options

Definition at line 469 of file compareRootHistos.py.

◆ parser

compareRootHistos.parser = OptionParser()

Definition at line 455 of file compareRootHistos.py.

◆ ref

string compareRootHistos.ref = "REFERENCE"

Definition at line 28 of file compareRootHistos.py.

◆ referenceFile

compareRootHistos.referenceFile

Definition at line 478 of file compareRootHistos.py.

◆ retval

def compareRootHistos.retval = compareHistos(ts, tp, state, checkBin2BinIdentity=options.bin2bin)

Definition at line 503 of file compareRootHistos.py.

◆ state

def compareRootHistos.state = comparePaths(ts, tp)

Definition at line 500 of file compareRootHistos.py.

◆ test

string compareRootHistos.test = "TEST"

Definition at line 29 of file compareRootHistos.py.

◆ testFile

compareRootHistos.testFile

Definition at line 478 of file compareRootHistos.py.

◆ tfp

compareRootHistos.tfp = TFile(referenceFile, "REC")

Definition at line 482 of file compareRootHistos.py.

◆ tfs

compareRootHistos.tfs = TFile(testFile, "REC")

Definition at line 480 of file compareRootHistos.py.

◆ tp

tuple compareRootHistos.tp = (test, dtest)

Definition at line 493 of file compareRootHistos.py.

◆ ts

tuple compareRootHistos.ts = (ref, dref)

Definition at line 492 of file compareRootHistos.py.

◆ usage

string compareRootHistos.usage = "usage: %prog testFile.root referenceFile.root [options]"

Definition at line 454 of file compareRootHistos.py.

AlgSequencer.all
all
Definition: AlgSequencer.py:61
compareRootHistos.composition
def composition(t)
Definition: compareRootHistos.py:73
compareRootHistos.compareHistos
def compareHistos(t1, t2, state, checkBin2BinIdentity)
Definition: compareRootHistos.py:216
compareRootHistos.rec
def rec(o, path=None, lst=None)
Definition: compareRootHistos.py:44
compareRootHistos.comparePaths
def comparePaths(t1, t2)
Definition: compareRootHistos.py:101
compareRootHistos.extractBlacklist
def extractBlacklist(listString)
Definition: compareRootHistos.py:442
compareRootHistos.bin2binIdentity
def bin2binIdentity(h1, h2)
Definition: compareRootHistos.py:181
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:102