Skip to content

Commit 8290fe7

Browse files
committed
Add tests for TH2 and TH3 arithmetic operations
1 parent 40e9f9b commit 8290fe7

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

bindings/pyroot/pythonizations/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_branch ttree_branch.py PYTHON_DEPS numpy)
5555
ROOT_ADD_PYUNITTEST(pyroot_pyz_th1_operators th1_operators.py)
5656
ROOT_ADD_PYUNITTEST(pyroot_pyz_th1_fillN th1_fillN.py PYTHON_DEPS numpy)
5757
ROOT_ADD_PYUNITTEST(pyroot_pyz_th2 th2.py)
58+
ROOT_ADD_PYUNITTEST(pyroot_pyz_th3 th3.py)
5859

5960
# TGraph, TGraph2D and error subclasses pythonizations
6061
ROOT_ADD_PYUNITTEST(pyroot_pyz_tgraph_getters tgraph_getters.py)

bindings/pyroot/pythonizations/test/th2.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,42 @@ def test_GetBinError(self):
1717
self.assertEqual(h.GetBinErrorLow(1, 1), 2)
1818

1919

20-
if __name__ == '__main__':
20+
class TH2Operations(unittest.TestCase):
21+
"""
22+
Test TH2D arithmetic operations
23+
"""
24+
25+
def setUp(self):
26+
self.h1 = ROOT.TH2D("h1", "h1", 2, 0, 2, 2, 0, 2)
27+
self.h2 = ROOT.TH2D("h2", "h2", 2, 0, 2, 2, 0, 2)
28+
29+
self.h1.Fill(0.5, 0.5, 2.0)
30+
self.h2.Fill(0.5, 0.5, 3.0)
31+
32+
def test_addition(self):
33+
hsum = self.h1 + self.h2
34+
self.assertAlmostEqual(hsum.GetBinContent(1, 1), 5.0)
35+
36+
def test_subtraction(self):
37+
hdiff = self.h2 - self.h1
38+
self.assertAlmostEqual(hdiff.GetBinContent(1, 1), 1.0)
39+
40+
def test_multiplication(self):
41+
hprod = self.h1 * self.h2
42+
self.assertAlmostEqual(hprod.GetBinContent(1, 1), 6.0)
43+
44+
def test_division(self):
45+
hdiv = self.h2 / self.h1
46+
self.assertAlmostEqual(hdiv.GetBinContent(1, 1), 1.5)
47+
48+
def test_scalar_multiplication_left(self):
49+
hscaled = 2.0 * self.h1
50+
self.assertAlmostEqual(hscaled.GetBinContent(1, 1), 4.0)
51+
52+
def test_scalar_multiplication_right(self):
53+
hscaled = self.h1 * 2.0
54+
self.assertAlmostEqual(hscaled.GetBinContent(1, 1), 4.0)
55+
56+
57+
if __name__ == "__main__":
2158
unittest.main()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
3+
import ROOT
4+
5+
6+
class TH3Operations(unittest.TestCase):
7+
"""
8+
Test TH3D arithmetic operations
9+
"""
10+
11+
def setUp(self):
12+
self.h1 = ROOT.TH3D("h1", "h1", 2, 0, 2, 2, 0, 2, 2, 0, 2)
13+
self.h2 = ROOT.TH3D("h2", "h2", 2, 0, 2, 2, 0, 2, 2, 0, 2)
14+
15+
self.h1.Fill(0.5, 0.5, 0.5, 2.0)
16+
self.h2.Fill(0.5, 0.5, 0.5, 3.0)
17+
18+
def test_addition(self):
19+
hsum = self.h1 + self.h2
20+
self.assertAlmostEqual(hsum.GetBinContent(1, 1, 1), 5.0)
21+
22+
def test_subtraction(self):
23+
hdiff = self.h2 - self.h1
24+
self.assertAlmostEqual(hdiff.GetBinContent(1, 1, 1), 1.0)
25+
26+
def test_multiplication(self):
27+
hprod = self.h1 * self.h2
28+
self.assertAlmostEqual(hprod.GetBinContent(1, 1, 1), 6.0)
29+
30+
def test_division(self):
31+
hdiv = self.h2 / self.h1
32+
self.assertAlmostEqual(hdiv.GetBinContent(1, 1, 1), 1.5)
33+
34+
def test_scalar_multiplication_left(self):
35+
hscaled = 2.0 * self.h1
36+
self.assertAlmostEqual(hscaled.GetBinContent(1, 1, 1), 4.0)
37+
38+
def test_scalar_multiplication_right(self):
39+
hscaled = self.h1 * 2.0
40+
self.assertAlmostEqual(hscaled.GetBinContent(1, 1, 1), 4.0)
41+
42+
43+
if __name__ == "__main__":
44+
unittest.main()

0 commit comments

Comments
 (0)