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