diff --git a/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt b/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt index 1cbf7b3cb8f296ad2ca2280b97ac633a0b2ffa75..4118f5f9f7b8eb3b7771d13b47b8a223d5a64974 100644 --- a/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt +++ b/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt @@ -27,6 +27,9 @@ ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_branch ttree_branch.py # TH1 and subclasses pythonizations ROOT_ADD_PYUNITTEST(pyroot_pyz_th1_operators th1_operators.py) +# TGraph, TGraph2D and error subclasses pythonizations +ROOT_ADD_PYUNITTEST(pyroot_pyz_tgraph_getters tgraph_getters.py) + # TCollection and subclasses pythonizations ROOT_ADD_PYUNITTEST(pyroot_pyz_tcollection_len tcollection_len.py) ROOT_ADD_PYUNITTEST(pyroot_pyz_tcollection_listmethods tcollection_listmethods.py) diff --git a/bindings/pyroot_experimental/PyROOT/test/tgraph_getters.py b/bindings/pyroot_experimental/PyROOT/test/tgraph_getters.py new file mode 100644 index 0000000000000000000000000000000000000000..323921c934b261f6cc6ba8e96594b7ae07481921 --- /dev/null +++ b/bindings/pyroot_experimental/PyROOT/test/tgraph_getters.py @@ -0,0 +1,76 @@ +import unittest +import array + +import ROOT + + +class TGraphGetters(unittest.TestCase): + """ + Test for the pythonization of TGraph, TGraph2D and their error + subclasses, in particular of their X,Y,Z coordinates and errors + getters, which sets the size of the returned buffers. + """ + + # Tests + def test_graph(self): + N = 5 + xval, yval = 1, 2 + + ax = array.array('d', map(lambda x: x*xval, range(N))) + ay = array.array('d', map(lambda x: x*yval, range(N))) + + g = ROOT.TGraph(N, ax, ay) + + # x and y are buffers of doubles + x = g.GetX() + y = g.GetY() + + # We can get the size of the buffers + self.assertEqual(len(x), N) + self.assertEqual(len(y), N) + + # The buffers are iterable + self.assertEqual(list(x), list(ax)) + self.assertEqual(list(y), list(ay)) + + def test_graph2derrors(self): + N = 5 + xval, yval, zval = 1, 2, 3 + xerrval, yerrval, zerrval = 0.1, 0.2, 0.3 + + ax = array.array('d', map(lambda x: x*xval, range(N))) + ay = array.array('d', map(lambda x: x*yval, range(N))) + az = array.array('d', map(lambda x: x*zval, range(N))) + aex = array.array('d', map(lambda x: x*xerrval, range(N))) + aey = array.array('d', map(lambda x: x*yerrval, range(N))) + aez = array.array('d', map(lambda x: x*zerrval, range(N))) + + g = ROOT.TGraph2DErrors(N, ax, ay, az, aex, aey, aez) + + # x, y, z, ex, ey and ez are buffers of doubles + x = g.GetX() + y = g.GetY() + z = g.GetZ() + ex = g.GetEX() + ey = g.GetEY() + ez = g.GetEZ() + + # We can get the size of the buffers + self.assertEqual(len(x), N) + self.assertEqual(len(y), N) + self.assertEqual(len(z), N) + self.assertEqual(len(ex), N) + self.assertEqual(len(ey), N) + self.assertEqual(len(ez), N) + + # The buffers are iterable + self.assertEqual(list(x), list(ax)) + self.assertEqual(list(y), list(ay)) + self.assertEqual(list(z), list(az)) + self.assertEqual(list(ex), list(aex)) + self.assertEqual(list(ey), list(aey)) + self.assertEqual(list(ez), list(aez)) + + +if __name__ == '__main__': + unittest.main()