The Gaudi Framework  v36r6 (b1ee9983)
issue_212.py
Go to the documentation of this file.
1 
12 import os
13 from collections import defaultdict
14 from traceback import format_exc
15 from unittest import TestCase
16 
17 FILENAME = f"{__name__}.root"
18 
19 
20 def config():
21  import GaudiConfig2.Configurables as C
22  import GaudiConfig2.Configurables.Gaudi.Tests.Histograms.MultiDimLayout as T
23 
24  algs = []
25  tools = []
26  svcs = []
27 
28  Alg = T.TestAlg
29  algs.append(Alg("Alg"))
30 
31  svcs.append(C.Gaudi.Histograming.Sink.Root(FileName=FILENAME))
32  svcs.append(C.Gaudi.Monitoring.MessageSvcSink())
33 
34  yield from algs
35  yield from tools
36  yield from svcs
37 
38  yield C.ApplicationMgr(
39  EvtMax=1,
40  EvtSel="NONE",
41  TopAlg=algs,
42  ExtSvc=svcs,
43  )
44 
45  # make sure the histogram file is not already there
46  if os.path.exists(FILENAME):
47  os.remove(FILENAME)
48 
49 
50 def check(causes, result):
51  result["root_output_file"] = FILENAME
52 
53  if not os.path.exists(FILENAME):
54  causes.append("missing histogram file")
55  return False
56 
57  try:
58  import ROOT
59 
60  f = ROOT.TFile.Open(FILENAME)
61 
62  # get the 3 expected histograms
63  histos = []
64  for i in range(1, 4):
65  h = f.Get(f"Alg/h{i}")
66  if not h:
67  k = f.GetKey(f"Alg/h{i}")
68  if k:
69  h = k.ReadObj()
70 
71  assert h, f"missing histogram Alg/h{i}"
72  histos.append(h)
73 
74  h1, h2, h3 = histos
75  # mimic the C++ filling loop to validate the content against expectation
76  value = 0.0
77  expected = defaultdict(dict)
78  found = defaultdict(dict)
79  for x in [i - 0.5 for i in range(12)]:
80  value += 1
81  expected["h1"][x] = value
82  found["h1"][x] = h1.GetBinContent(h1.FindBin(x))
83  for y in [i - 0.5 for i in range(12)]:
84  value += 1
85  expected["h2"][(x, y)] = value
86  found["h2"][(x, y)] = h2.GetBinContent(h2.FindBin(x, y))
87  for z in [i - 0.5 for i in range(12)]:
88  value += 1
89  expected["h3"][(x, y, z)] = value
90  found["h3"][(x, y, z)] = h3.GetBinContent(h3.FindBin(x, y, z))
91 
92  # this is a trick to piggyback on TestCase dict diff report
93  t = TestCase()
94  diffs = {}
95  for name in expected:
96  try:
97  t.assertEqual(expected[name], found[name])
98  except AssertionError as err:
99  diffs[name] = str(err).splitlines()[0]
100 
101  if diffs:
102  causes.append("histograms content")
103  for name in diffs:
104  result[f"{name}_diff"] = diffs[name]
105 
106  except AssertionError as err:
107  causes.append(str(err))
108  return False
109 
110  except Exception as err:
111  causes.append("failure reading histogram file")
112  result["python_exception"] = result.Quote(format_exc())
113  return False
114 
115  return True
GaudiTests.Histograms.issue_212.check
def check(causes, result)
Definition: issue_212.py:50
GaudiConfig2.Configurables
Definition: Configurables.py:1
GaudiTests.Histograms.issue_212.config
def config()
Definition: issue_212.py:20
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:102