The Gaudi Framework  v30r3 (a5ef0a68)
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')
 
 lref = rec(tfs)
 
 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)
 
 state = comparePaths(ts, tp)
 
 retval = compareHistos(ts, tp, state, checkBin2BinIdentity=options.bin2bin)
 

Function Documentation

def compareRootHistos.bin2binIdentity (   h1,
  h2 
)

Definition at line 149 of file compareRootHistos.py.

149 def bin2binIdentity(h1, h2):
150  def getNbins(h):
151  biny = h.GetNbinsY()
152  if biny > 1:
153  biny += 1
154  binz = h.GetNbinsZ()
155  if binz > 1:
156  binz += 1
157  return (h.GetNbinsX() + 1) * (biny) * (binz)
158 
159  nbins = getNbins(h1)
160  diffbins = 0
161  for ibin in xrange(0, nbins):
162  h1bin = h1.GetBinContent(ibin)
163  h2bin = h2.GetBinContent(ibin)
164  diffbins += (h1bin != h2bin)
165  return diffbins
166 
167 
168 # =============================================================================
169 # Method : compareHistos( t1, t2 )
170 #
171 # @param t1, t2 : a tuple of ( type, d ) where type is either 'REFERENCE' or 'TEST'
172 # and d is a dictionary of ROOT objects, with each key = ROOT path
173 #
174 # function : compare the histograms in Reference/Test ROOT files. First, go through each
175 # dict to collect the histos (ignore TDirectory objects, etc). Then the histos
176 # in the test file (experimental) are compared to their equivalents in the
177 # reference file (definitely correct) using 3 methods.
178 # 1) The entries are checked, they should be equal
179 # 2) If entries are equal, check the Integral(); should be equal
180 # 3) If integrals are equal, check the KolmogorovTest() ; should be 1
181 # 4) If identity flag is there and KS test is performed, perform bin2bin identity test
182 # Arguments t1 and t2 are checked and the test/reference auto-detected
183 #
def bin2binIdentity(h1, h2)
def compareRootHistos.compareHistos (   t1,
  t2,
  state,
  checkBin2BinIdentity 
)

Definition at line 184 of file compareRootHistos.py.

184 def compareHistos(t1, t2, state, checkBin2BinIdentity):
185 
186  (((referenceObjects, referenceHistos), (parallObjects, parallHistos)),
187  (uniqueSerPaths, uniqueSerHistos), (uniqueParPaths, uniqueParHistos), mh) = state
188 
189  # deduce which one is test, which reference
190  if t1[0] == ref:
191  ds = t1[1]
192  dp = t2[1]
193  elif t2[0] == ref:
194  ds = t2[1]
195  dp = t1[1]
196  else:
197  print 'Neither tuple is Reference Root file reference?'
198  return
199 
200  # histocount, objectcount for test/reference
201  hcp = 0
202  pHistos = []
203  hcs = 0
204  sHistos = []
205 
206  omit = [re.compile(regex) for regex in gRegexBlackList]
207 
208  # find the histos in the reference file
209  for k in ds.keys():
210  if not any(regex.search(k) != None for regex in omit):
211  if ds[k].__class__.__name__ in histos:
212  hcs += 1
213  sHistos.append(k)
214  # same for test
215  for k in dp.keys():
216  if not any(regex.search(k) != None for regex in omit):
217  if dp[k].__class__.__name__ in histos:
218  hcp += 1
219  pHistos.append(k)
220 
221  cEntries = 0
222  xEntries = 0
223  diffEntries = []
224  cIntegrals = 0
225  xIntegrals = 0
226  diffIntegrals = []
227  passedKol = 0
228  failedKol = 0
229  diffKols = []
230  zeroIntegrals = 0
231  passedIdentity = 0
232  failedIdentity = 0
233  diffIdentity = []
234  identityDiffBins = {}
235  kTested = 0
236  kTestResults = {}
237  notfound = 0
238  integralMatch = 0
239  otherTest = 0
240  zeroIntegralMatch = 0
241  for h in sHistos:
242  if h in pHistos:
243  # matching histos to check
244  cEntries += 1
245  sh = ds[h]
246  ph = dp[h]
247  # first check entries
248  if sh.GetEntries() != ph.GetEntries():
249  diffEntries.append(h)
250  xEntries += 1
251  continue
252  # check for (non-zero sum of bin error) && (non-zero integrals) for K-Test
253  sBinError = 0.0
254  pBinError = 0.0
255  for i in xrange(sh.GetNbinsX()):
256  sBinError += sh.GetBinError(i)
257  for i in xrange(ph.GetNbinsX()):
258  pBinError += ph.GetBinError(i)
259  sint = sh.Integral()
260  pint = ph.Integral()
261  doKS = (bool(sint) and bool(pint)) and (
262  sBinError > 0 and pBinError > 0)
263  if checkBin2BinIdentity and doKS:
264  diffBins = bin2binIdentity(sh, ph)
265  if diffBins == 0:
266  passedIdentity += 1
267  else:
268  failedIdentity += 1
269  diffIdentity.append(h)
270  identityDiffBins[h] = diffBins
271  if (bool(sint) and bool(pint)) and (sBinError > 0 and pBinError > 0):
272  kTested += 1
273  kTest = sh.KolmogorovTest(ph)
274  kTestResults[h] = kTest
275  if int(kTest):
276  passedKol += 1
277  else:
278  # ; print 'KTest result : ', kTest
279  failedKol += 1
280  diffKols.append(h)
281  else:
282  # try the integral test?
283  otherTest += 1
284  if all((sint, pint)) and (sint == pint):
285  integralMatch += 1
286  elif (sint == pint):
287  zeroIntegralMatch += 1
288  else:
289  diffIntegrals.append(h)
290  xIntegrals += 1
291  else:
292  notfound += 1
293  print 'not found? ', h
294 
295  # report on Failed Entry-Checks
296  print '\n\n' + '-' * 80
297  print 'Summary of histos with different Entries'
298  print '-' * 80
299  if diffEntries:
300  diffEntries.sort()
301  for e in diffEntries:
302  print '\t\t\t%s:\t%i != %i' % (
303  e, int(ds[e].GetEntries()), int(dp[e].GetEntries()))
304  print '-' * 80
305 
306  # report on Failed Kolmogorov Tests
307  print '\n\n' + '-' * 60
308  print 'Summary of histos which failed Kolmogorov Test'
309  print '-' * 60
310  if diffKols:
311  diffKols.sort()
312  for e in diffKols:
313  result = kTestResults[e] # DP Calculated twice ARGH!!
314  print '%s\t\t%s :\tK-Test Result :\t %5.16f' % (
315  ds[e].ClassName(), e, result)
316  print '-' * 60
317 
318  # report on Failed Integral Checks
319  print '\n\n' + '-' * 60
320  print 'Summary of histos which failed Integral Check'
321  print '-' * 60
322  if diffIntegrals:
323  diffIntegrals.sort()
324  for e in diffIntegrals:
325  diff = dp[e].Integral() - ds[e].Integral()
326  pc = (diff * 100) / ds[e].Integral()
327  print '%s\t\t%s:\t Diff = %5.6f\tPercent Diff to Reference : %5.6f ' % (
328  ds[e].ClassName(), e, diff, pc)
329  print '-' * 60 + '\n'
330  print '=' * 80 + '\n'
331 
332  # Report on failed bin2bin identity
333  if checkBin2BinIdentity:
334  # report on b2b checks
335  print '\n\n' + '-' * 80
336  print 'Summary of histos with at least one bin with different Entries'
337  print '-' * 80
338  if diffIdentity:
339  diffIdentity.sort()
340  for e in diffIdentity:
341  print '%s\t\t%s: %i different bins' % (
342  ds[e].ClassName(), e, identityDiffBins[e])
343  print '-' * 80
344 
345  print '\n' + '=' * 80
346  print 'Comparison : Reference/Test ROOT Histo files'
347  print '\n\t\tReference\tTest'
348  print '\tObjects : %i\t%i\t\t( p-s = %i )' % (referenceObjects,
349  parallObjects, parallObjects - referenceObjects)
350  print '\tHistos : %i\t%i\t\t( p-s = %i )' % (referenceHistos,
351  parallHistos, parallHistos - referenceHistos)
352  print '\t __________'
353  print '\tTotal : %i\t%i\n' % (
354  referenceHistos + referenceObjects, parallHistos + parallObjects)
355  print 'Objects/Histos unique to Reference File : %i / %i' % (
356  len(uniqueSerPaths) - uniqueSerHistos, uniqueSerHistos)
357  print 'Objects/Histos unique to Test File : %i / %i' % (
358  len(uniqueParPaths) - uniqueParHistos, uniqueParHistos)
359  print '\nMatching Histograms valid for Comparison : %i' % (mh)
360  print '\nOmissions\' patterns : '
361  for entry in gRegexBlackList:
362  print '\t%s' % (entry)
363  print '\nHistograms for Comparison (after Omissions) : %i' % (
364  mh - len(gRegexBlackList))
365  print '\n\tHISTOGRAM TESTS : '
366  print '\t\tKOLMOGOROV TEST : %i' % (kTested)
367  print '\t\tINTEGRAL TEST : %i' % (otherTest)
368  print '\t\tENTRIES TEST : %i' % (xEntries)
369  if checkBin2BinIdentity:
370  print '\t\tBIN2BIN TEST : %i' % (passedIdentity)
371  print '\t\t ____'
372  print '\t\tTested : %i' % (cEntries)
373 
374  print '\n\tDISCREPANCIES : '
375  print '\t\tK-Test : %i' % (failedKol)
376  print '\t\tIntegrals : %i' % (xIntegrals)
377  print '\t\tEntries : %i' % (xEntries)
378  retval = failedKol + xIntegrals + xEntries + failedIdentity
379  if retval != 0:
380  print '\nThe two sets of histograms were not identical'
381  print '\n' + '=' * 80
382  return retval
383 
384 # =============================================================================
385 
386 
def compareHistos(t1, t2, state, checkBin2BinIdentity)
def bin2binIdentity(h1, h2)
GAUDI_API double Integral(const Genfun::AbsFunction &function, const double a, const double b, const GaudiMath::Integration::Type type=GaudiMath::Integration::Adaptive, const GaudiMath::Integration::KronrodRule rule=GaudiMath::Integration::Default, const double epsabs=1.e-10, const double epsrel=1.e-7, const size_t size=1000)
Definition: Integral.cpp:26
def compareRootHistos.comparePaths (   t1,
  t2 
)

Definition at line 83 of file compareRootHistos.py.

83 def comparePaths(t1, t2):
84  if t1[0] == ref:
85  ds = t1[1]
86  dp = t2[1]
87  elif t2[0] == ref:
88  ds = t2[1]
89  dp = t1[1]
90  else:
91  print 'Neither tuple is Reference Root file reference?'
92  return
93 
94  dsks = ds.keys()
95  dpks = dp.keys()
96  dsks.sort()
97  dpks.sort()
98 
99  sset = set(dsks)
100  pset = set(dpks)
101  os, hs = composition((ref, ds))
102  op, hp = composition((test, dp))
103  print '\n' + '=' * 80
104  print 'Comparison of Paths : Reference vs Test ROOT files'
105  print '-' * 80
106  print 'Number of paths in Reference file : %i (objects, histos) = ( %i, %i )' % (
107  len(dsks), os, hs)
108  print 'Number of paths in Test file : %i (objects, histos) = ( %i, %i )' % (
109  len(dpks), op, hp)
110  matching = sset.intersection(pset)
111  matchingHistos = 0
112  for n in matching:
113  if ds[n].__class__.__name__ in histos:
114  matchingHistos += 1
115  print '\nMatching paths : %i' % (len(matching))
116  uSer = sset - pset
117  # work out histos unique to test file
118  uniqueReferenceHistos = 0
119  for n in uSer:
120  if ds[n].__class__.__name__ in histos:
121  uniqueReferenceHistos += 1
122  print 'Paths unique to Reference file : %i ( %i Histos )' % (
123  len(uSer), uniqueReferenceHistos)
124  if uSer:
125  for n in uSer:
126  print '\t%s : \t%s' % (ds[n], n)
127  uPar = pset - sset
128  uniqueTestHistos = 0
129  for n in uPar:
130  if dp[n].__class__.__name__ in histos:
131  uniqueTestHistos += 1
132  print 'Paths unique to Test file : %i ( %i Histos )' % (
133  len(uPar), uniqueTestHistos)
134  if uPar:
135  for n in uPar:
136  print '\t%s : \t%s' % (dp[n], n)
137  print 'Matching Histos to test : %i' % (matchingHistos)
138  print '=' * 80 + '\n'
139  return (((os, hs), (op, hp)), (uSer, uniqueReferenceHistos), (uPar, uniqueTestHistos), matchingHistos)
140 # =============================================================================
141 
142 # =============================================================================
143 # Method : bin2binIdentity(h1,h2)
144 #
145 # @param h1, h2 : The two histogtams to compare
146 # function : Return the number of different bins
147 
148 
def comparePaths(t1, t2)
def compareRootHistos.composition (   t)

Definition at line 57 of file compareRootHistos.py.

57 def composition(t):
58  typ, d = t
59  hists = 0
60  objs = 0
61  for k in d.keys():
62  if d[k].__class__.__name__ in histos:
63  hists += 1
64  else:
65  objs += 1
66  return objs, hists
67 # =============================================================================
68 
69 # =============================================================================
70 # Method : comparePaths( t1, t2 )
71 #
72 # @param t1, t2 : a tuple of ( type, d ) where type is either 'REFERENCE' or 'TEST'
73 # and d is a dictionary of ROOT objects, with each key = ROOT path
74 #
75 # function : compare the paths between the two histo files. If the files are identical, they
76 # should have the same set of paths. The Test file should definitely have the
77 # same paths as the Reference. Perhaps the Reference file will have some more paths due
78 # to extra histos added as part of Application Sequencer finalisation
79 # Arguments t1 and t2 are checked and the test/reference auto-detected
80 #
81 
82 
def compareRootHistos.extractBlacklist (   listString)

Definition at line 387 of file compareRootHistos.py.

387 def extractBlacklist(listString):
388  global gRegexBlackList
389  if listString:
390  for blackRegexp in listString.split(","):
391  gRegexBlackList.append(blackRegexp)
392  else:
393  gBlackList = []
394 
395 # =============================================================================
396 
397 
def extractBlacklist(listString)
def compareRootHistos.rec (   o,
  path = None,
  lst = None 
)

Definition at line 30 of file compareRootHistos.py.

30 def rec(o, path=None, lst=None):
31  if not path:
32  path = '/stat'
33  lst = []
34  else:
35  path = path + '/' + o.GetName()
36  lst.append((path, o))
37  if 'GetListOfKeys' in dir(o):
38  keys = o.GetListOfKeys()
39  for k in keys:
40  name = k.GetName()
41  rec(o.Get(name), path, lst)
42  else:
43  pass
44  return lst
45 # =============================================================================
46 
47 # =============================================================================
48 # Method : composition( t )
49 #
50 # @param t : a tuple of ( type, d ) where type is either 'REFERENCE' or 'TEST'
51 # and d is a dictionary of ROOT objects, with each key = ROOT path
52 #
53 # function : deduce the composition, (objects/histos) counts
54 #
55 
56 
def rec(o, path=None, lst=None)

Variable Documentation

compareRootHistos.action

Definition at line 405 of file compareRootHistos.py.

compareRootHistos.args

Definition at line 407 of file compareRootHistos.py.

compareRootHistos.argv

Definition at line 7 of file compareRootHistos.py.

compareRootHistos.backupArgv = sys.argv[:]

Definition at line 6 of file compareRootHistos.py.

compareRootHistos.default

Definition at line 405 of file compareRootHistos.py.

compareRootHistos.dest

Definition at line 401 of file compareRootHistos.py.

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

Definition at line 427 of file compareRootHistos.py.

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

Definition at line 428 of file compareRootHistos.py.

list compareRootHistos.gRegexBlackList = []

Definition at line 12 of file compareRootHistos.py.

compareRootHistos.help

Definition at line 402 of file compareRootHistos.py.

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

Definition at line 13 of file compareRootHistos.py.

compareRootHistos.lref = rec(tfs)

Definition at line 424 of file compareRootHistos.py.

compareRootHistos.ltest = rec(tfp)

Definition at line 425 of file compareRootHistos.py.

compareRootHistos.options

Definition at line 407 of file compareRootHistos.py.

compareRootHistos.parser = OptionParser()

Definition at line 400 of file compareRootHistos.py.

string compareRootHistos.ref = 'REFERENCE'

Definition at line 14 of file compareRootHistos.py.

compareRootHistos.referenceFile

Definition at line 416 of file compareRootHistos.py.

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

Definition at line 441 of file compareRootHistos.py.

compareRootHistos.state = comparePaths(ts, tp)

Definition at line 438 of file compareRootHistos.py.

string compareRootHistos.test = 'TEST'

Definition at line 15 of file compareRootHistos.py.

compareRootHistos.testFile

Definition at line 416 of file compareRootHistos.py.

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

Definition at line 420 of file compareRootHistos.py.

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

Definition at line 418 of file compareRootHistos.py.

tuple compareRootHistos.tp = (test, dtest)

Definition at line 431 of file compareRootHistos.py.

tuple compareRootHistos.ts = (ref, dref)

Definition at line 430 of file compareRootHistos.py.

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

Definition at line 399 of file compareRootHistos.py.