From f55f3c9224919fd151a292cc1e48e53e9f9142c2 Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Wed, 17 Mar 2021 09:51:09 -0500 Subject: [PATCH 01/10] Compare vector results with ROOT's. --- .github/workflows/ci.yml | 10 ++--- tests/root/test_PxPyPzEVector.py | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/root/test_PxPyPzEVector.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 17d0e465..f30a2aa6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,11 +75,11 @@ jobs: # steps: # - uses: actions/checkout@v2 - # - name: Get Conda - # uses: conda-incubator/setup-miniconda@v2 - # with: - # environment-file: environment.yml - # activate-environment: vector + - name: Get Conda + uses: conda-incubator/setup-miniconda@v2 + with: + environment-file: environment.yml + activate-environment: vector # - name: Run tests # shell: "bash -l {0}" diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py new file mode 100644 index 00000000..2ba0491c --- /dev/null +++ b/tests/root/test_PxPyPzEVector.py @@ -0,0 +1,64 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.PxPyPzEVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinates = [ + "to_xyzt", + "to_xythetat", + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + + +# Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +@pytest.mark.parametrize("coordinates", coordinates) +def test_M2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) + + +# Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. +@pytest.mark.parametrize("constructor", constructor) +@pytest.mark.parametrize("coordinates", coordinates) +def test_M(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau + ) + + +# etc. From ca3b2df3e4640777d363b28152653caef5fd673a Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Mon, 10 May 2021 16:26:54 +0200 Subject: [PATCH 02/10] merge branches --- tests/root/test_PxPyPzEVector.py | 393 ++++++++++++++++++++++++++++++- 1 file changed, 388 insertions(+), 5 deletions(-) diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index 2ba0491c..6d62dcbb 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -4,6 +4,7 @@ # or https://github.com/scikit-hep/vector for details. import pytest +from hypothesis import given, strategies as st import vector @@ -13,19 +14,25 @@ # ROOT.Math.PxPyPzEVector constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), + (0, 0, 1, 0), + (0, 0, 0, 4294967296), + (0, 4294967296, 0, 0), (0, 0, 0, 10), (0, 0, 0, -10), (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), + (1., 2., 3., 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] # Coordinate conversion methods to apply to the VectorObject4D. -coordinates = [ +coordinate_list = [ "to_xyzt", - "to_xythetat", + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -38,10 +45,72 @@ "to_rhophietatau", ] +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +# Dot +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( + v1.dot(v2) + ) + +# Run the same test within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fizz_Dot(constructor1, constructor2, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates + )() + assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( + v1.dot(v2) + ) # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. +# Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @pytest.mark.parametrize("constructor", constructor) -@pytest.mark.parametrize("coordinates", coordinates) def test_M2(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( getattr( @@ -49,10 +118,18 @@ def test_M2(constructor, coordinates): )().tau2 ) +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. +# Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) -@pytest.mark.parametrize("coordinates", coordinates) def test_M(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( getattr( @@ -60,5 +137,311 @@ def test_M(constructor, coordinates): )().tau ) +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau + ) + + +# Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt2(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + assert ROOT.Math.PxPyPzEVector(*constructor).Mt2() == pytest.approx( + v.t*v.t - v.z*v.z + ) + +# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( + v.lib.sqrt(v.t*v.t - v.z*v.z) + ) + +# Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. +# P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_P2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).P2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().mag2 + ) + +# Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. +# P is our mag (same deal) +@pytest.mark.parametrize("constructor", constructor) +def test_P(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).P() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().mag + ) + +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Perp2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().rho2 + ) + +# Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PxPyPzEVector(*constructor).Perp2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().rho + ) + +# Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().phi + ) -# etc. +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rapidity(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Rapidity() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().rapidity + ) + +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Beta(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Beta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().beta + ) + +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Eta(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Eta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().eta + ) + +# Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Gamma(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Gamma() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().gamma + ) + +# Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isLightlike(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).isLightlike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().is_lightlike() + ) + +# Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isSpacelike(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).isSpacelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().is_spacelike() + ) + +# Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isTimelike(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).isTimelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().is_timelike() + ) + +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Theta(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Theta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().theta + ) + +# Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).X() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().x + ) + +# Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Y(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Y() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().y + ) + +# Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Z(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Z() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().z + ) + +# Run a test that compares ROOT's 'T()' with vector's 't' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_T(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).T() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().t + ) + + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__add__(ROOT.Math.PxPyPzEVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__sub__(ROOT.Math.PxPyPzEVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + assert ref_vec.T() == pytest.approx(vec().t) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__eq__(ROOT.Math.PxPyPzEVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + ) + assert ref_vec == vec + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PxPyPzEVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PxPyPzEVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PxPyPzEVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) From a73e85315e0e1bae1fa9a3691d1317008d8066f8 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Mon, 10 May 2021 16:28:31 +0200 Subject: [PATCH 03/10] [skip ci] merge tests from a private fork --- tests/root/test_EulerAngles.py | 79 +++++ tests/root/test_Polar2DVector.py | 423 +++++++++++++++++++++++++++ tests/root/test_Polar3DVector.py | 270 +++++++++++++++++ tests/root/test_PtEtaPhiEVector.py | 447 +++++++++++++++++++++++++++++ tests/root/test_PtEtaPhiMVector.py | 447 +++++++++++++++++++++++++++++ tests/root/test_PxPyPzMVector.py | 444 ++++++++++++++++++++++++++++ tests/root/test_Quaternion.py | 79 +++++ tests/root/test_RhoEtaPhiVector.py | 269 +++++++++++++++++ tests/root/test_RhoZPhiVector.py | 269 +++++++++++++++++ tests/root/test_XYVector.py | 233 +++++++++++++++ tests/root/test_XYZVector.py | 269 +++++++++++++++++ tests/root/test_vector.py | 92 ++++++ 12 files changed, 3321 insertions(+) create mode 100644 tests/root/test_EulerAngles.py create mode 100644 tests/root/test_Polar2DVector.py create mode 100644 tests/root/test_Polar3DVector.py create mode 100644 tests/root/test_PtEtaPhiEVector.py create mode 100644 tests/root/test_PtEtaPhiMVector.py create mode 100644 tests/root/test_PxPyPzMVector.py create mode 100644 tests/root/test_Quaternion.py create mode 100644 tests/root/test_RhoEtaPhiVector.py create mode 100644 tests/root/test_RhoZPhiVector.py create mode 100644 tests/root/test_XYVector.py create mode 100644 tests/root/test_XYZVector.py create mode 100644 tests/root/test_vector.py diff --git a/tests/root/test_EulerAngles.py b/tests/root/test_EulerAngles.py new file mode 100644 index 00000000..4b59357a --- /dev/null +++ b/tests/root/test_EulerAngles.py @@ -0,0 +1,79 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# 4D constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0, 0), + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), + (0, 0, 1, 0), + (0, 0, 0, 4294967296), + (0, 4294967296, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1., 2., 3., 2.5), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinate_list = [ + "to_xyzt", + "to_xythetat", # may fail for constructor2 + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param diff --git a/tests/root/test_Polar2DVector.py b/tests/root/test_Polar2DVector.py new file mode 100644 index 00000000..91e4eee8 --- /dev/null +++ b/tests/root/test_Polar2DVector.py @@ -0,0 +1,423 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import numpy as np + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.Polar2DVector constructor arguments to get all the weird cases. +# r > 0 and phi from 0 to 360 deg? +constructor = [ + (0, 0), + (0, 10), + (1, 0), + (10, 0), + (1, 10), + (10., 10), + (1., 2.5), + (1, 2.5), + (1, 6.283185307179586), +] + +# Coordinate conversion methods to apply to the VectorObject2D. +coordinate_list = [ + "to_xy", + "to_rhophi", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.25*np.pi, + -0.25*np.pi, + 0.5*np.pi, + -0.5*np.pi, + np.pi, + -np.pi, + 2*np.pi, + -2*np.pi, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.Polar2DVector(*constructor).Dot(ROOT.Math.Polar2DVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )()) + ) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.Polar2DVector(*constructor1).Dot(ROOT.Math.Polar2DVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates + )()), rel=1e-6, abs=1e-6 + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag2(constructor, coordinates): + assert ROOT.Math.Polar2DVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().rho2 + ) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Mag2(constructor, coordinates): + assert ROOT.Math.Polar2DVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.Polar2DVector(*constructor).Mag2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().rho + ) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Mag(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.Polar2DVector(*constructor).Mag2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().rho + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.Polar2DVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().phi + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Phi(constructor, coordinates): + assert ROOT.Math.Polar2DVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().phi + ) + +# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rotate(constructor, angle, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor) + ref_vec.Rotate(angle) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().rotateZ(angle) + res_vec = vec.rotateZ(angle) + assert (ref_vec.R() == pytest.approx(res_vec.rho) and + ref_vec.Phi() == pytest.approx(res_vec.phi)) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + angle=st.floats(min_value=-10e7, max_value=10e7) + | st.integers(min_value=-10e7, max_value=10e7)) +def test_fuzz_Rotate(constructor, angle, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor) + ref_vec.Rotate(angle) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert (ref_vec.R() == pytest.approx(res_vec.rho) and + ref_vec.Phi() == pytest.approx(res_vec.phi)) + + +# Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Unit(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + res_vec = vec.unit + assert (ref_vec.R() == pytest.approx(res_vec().rho) and + ref_vec.Phi() == pytest.approx(res_vec().phi)) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Unit(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + res_vec = vec.unit + assert (ref_vec.R() == pytest.approx(res_vec().rho) and + ref_vec.Phi() == pytest.approx(res_vec().phi)) + + +# Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X_and_Y(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + assert (ref_vec.X() == pytest.approx(vec.x) and + ref_vec.Y() == pytest.approx(vec.y)) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_X_and_Y(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + assert (ref_vec.X() == pytest.approx(vec.x) and + ref_vec.Y() == pytest.approx(vec.y)) + + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__add__(ROOT.Math.Polar2DVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)()) + assert (ref_vec.R() == pytest.approx(vec.rho) and + ref_vec.Phi() == pytest.approx(vec.phi)) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_add(constructor1, constructor2, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor1).__add__(ROOT.Math.Polar2DVector(*constructor2)) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)()) + assert (ref_vec.R() == pytest.approx(vec.rho) and + ref_vec.Phi() == pytest.approx(vec.phi)) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__sub__(ROOT.Math.Polar2DVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert (ref_vec.R() == pytest.approx(res_vec.rho) and + ref_vec.Phi() == pytest.approx(res_vec.phi)) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_sub(constructor1, constructor2, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor1).__sub__(ROOT.Math.Polar2DVector(*constructor2)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert (ref_vec.R() == pytest.approx(res_vec.rho) and + ref_vec.Phi() == pytest.approx(res_vec.phi)) + + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__neg__ + assert (ref_vec.R() == pytest.approx(vec().rho) and + ref_vec.Phi() == pytest.approx(vec().phi)) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_neg(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__neg__ + assert (ref_vec.R() == pytest.approx(vec().rho) and + ref_vec.Phi() == pytest.approx(vec().phi)) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__mul__(scalar) + assert (ref_vec.R() == pytest.approx(vec.rho) and + ref_vec.Phi() == pytest.approx(vec.phi)) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + scalar=st.floats(min_value=-10e7, max_value=10e7) + | st.integers(min_value=-10e7, max_value=10e7)) +def test_fuzz_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__mul__(scalar) + assert (ref_vec.R() == pytest.approx(vec.rho) and + ref_vec.Phi() == pytest.approx(vec.phi)) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert (ref_vec.R() == pytest.approx(vec.rho) and + ref_vec.Phi() == pytest.approx(vec.phi)) + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + scalar=st.floats(min_value=-10e7, max_value=10e7) + | st.integers(min_value=-10e7, max_value=10e7)) +def test_fuzz_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert (ref_vec.R() == pytest.approx(vec.rho) and + ref_vec.Phi() == pytest.approx(vec.phi)) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__(ROOT.Math.Polar2DVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + ) + assert ref_vec == vec + + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_eq(constructor, coordinates): + ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__(ROOT.Math.Polar2DVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().equal(getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )() + ) + assert ref_vec == vec diff --git a/tests/root/test_Polar3DVector.py b/tests/root/test_Polar3DVector.py new file mode 100644 index 00000000..0f98249c --- /dev/null +++ b/tests/root/test_Polar3DVector.py @@ -0,0 +1,270 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import numpy as np + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.Polar3DVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0), + (0, 10, 0), + (0, -10, 0), + (1, 0, 0), + (1, 10, 0), + (1, -10, 0), + (1., 2.5, 2.), + (1, 2.5, 2.), + (1, -2.5, 2.), +] + +# Coordinate conversion methods to apply to the VectorObject2D. +coordinate_list = [ + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +# rho = r*sin(theta) +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Dot(ROOT.Math.Polar3DVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )()) + ) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.Polar3DVector(*constructor1).Dot(ROOT.Math.Polar3DVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor1))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor2))), coordinates + )()) + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag2(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_R(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).R() == pytest.approx( + np.sqrt(getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().rho2) + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().phi + ) + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.Polar3DVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.Polar3DVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.Polar3DVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Unit(constructor, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + res_vec = vec.unit + assert ref_vec.X() == pytest.approx(res_vec().x) + assert ref_vec.Y() == pytest.approx(res_vec().y) + assert ref_vec.Z() == pytest.approx(res_vec().z) + + +# Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X_and_Y(constructor, coordinates): + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + assert (ROOT.Math.Polar3DVector(*constructor).X() == pytest.approx(vec.x) and + ROOT.Math.Polar3DVector(*constructor).Y() == pytest.approx(vec.y)) + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).__add__(ROOT.Math.Polar3DVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).__sub__(ROOT.Math.Polar3DVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).__eq__(ROOT.Math.Polar3DVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + ) + assert ref_vec == vec diff --git a/tests/root/test_PtEtaPhiEVector.py b/tests/root/test_PtEtaPhiEVector.py new file mode 100644 index 00000000..24d85b2c --- /dev/null +++ b/tests/root/test_PtEtaPhiEVector.py @@ -0,0 +1,447 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.PtEtaPhiEVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0, 0), + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), + (0, 0, 1, 0), + (0, 0, 0, 4294967296), + (0, 4294967296, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1., 2., 3., 2.5), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinate_list = [ + "to_xyzt", + "to_xythetat", # may fail for constructor2 + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +# Dot +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + assert ROOT.Math.PtEtaPhiEVector(*constructor).Dot(ROOT.Math.PtEtaPhiEVector(*constructor)) == pytest.approx( + v1.dot(v2) + ) + +# Run the same test within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fizz_Dot(constructor1, constructor2, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor1))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor2))), coordinates + )() + assert ROOT.Math.PtEtaPhiEVector(*constructor1).Dot(ROOT.Math.PtEtaPhiEVector(*constructor2)) == pytest.approx( + v1.dot(v2) + ) + +# Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. +# Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) +@pytest.mark.parametrize("constructor", constructor) +def test_M2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().tau2 + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().tau2 + ) + +# Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. +# Mass is tau (or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_M(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().tau + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().tau + ) + + +# Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt2(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + assert ROOT.Math.PtEtaPhiEVector(*constructor).Mt2() == pytest.approx( + v.t*v.t - v.z*v.z + ) + +# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + assert ROOT.Math.PtEtaPhiEVector(*constructor).mt() == pytest.approx( + v.lib.sqrt(v.t*v.t - v.z*v.z) + ) + +# Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. +# P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_P2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).P2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().mag2 + ) + +# Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. +# P is our mag (same deal) +@pytest.mark.parametrize("constructor", constructor) +def test_P(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).P() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().mag + ) + +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Perp2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().rho2 + ) + +# Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PtEtaPhiEVector(*constructor).Perp2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().rho + ) + +# Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().phi + ) + +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rapidity(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Rapidity() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().rapidity + ) + +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Beta(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Beta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().beta + ) + +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Eta(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Eta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().eta + ) + +# Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Gamma(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Gamma() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().gamma + ) + +# Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isLightlike(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).isLightlike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().is_lightlike() + ) + +# Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isSpacelike(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).isSpacelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().is_spacelike() + ) + +# Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isTimelike(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).isTimelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().is_timelike() + ) + +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Theta(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Theta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().theta + ) + +# Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).X() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().x + ) + +# Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Y(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Y() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().y + ) + +# Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Z(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).Z() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().z + ) + +# Run a test that compares ROOT's 'T()' with vector's 't' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_T(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).T() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().t + ) + + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__add__(ROOT.Math.PtEtaPhiEVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__sub__(ROOT.Math.PtEtaPhiEVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + assert ref_vec.T() == pytest.approx(vec().t) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__eq__(ROOT.Math.PtEtaPhiEVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + ) + assert ref_vec == vec + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PtEtaPhiEVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PtEtaPhiEVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PtEtaPhiEVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) diff --git a/tests/root/test_PtEtaPhiMVector.py b/tests/root/test_PtEtaPhiMVector.py new file mode 100644 index 00000000..0447e232 --- /dev/null +++ b/tests/root/test_PtEtaPhiMVector.py @@ -0,0 +1,447 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.PtEtaPhiMVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0, 0), + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), + (0, 0, 1, 0), + (0, 0, 0, 4294967296), + (0, 4294967296, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1., 2., 3., 2.5), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinate_list = [ + "to_xyzt", + "to_xythetat", # may fail for constructor2 + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +# Dot +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + assert ROOT.Math.PtEtaPhiMVector(*constructor).Dot(ROOT.Math.PtEtaPhiMVector(*constructor)) == pytest.approx( + v1.dot(v2) + ) + +# Run the same test within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fizz_Dot(constructor1, constructor2, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor1))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor2))), coordinates + )() + assert ROOT.Math.PtEtaPhiMVector(*constructor1).Dot(ROOT.Math.PtEtaPhiMVector(*constructor2)) == pytest.approx( + v1.dot(v2) + ) + +# Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. +# Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) +@pytest.mark.parametrize("constructor", constructor) +def test_M2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().tau2 + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().tau2 + ) + +# Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. +# Mass is tau (or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_M(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().tau + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().tau + ) + + +# Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt2(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + assert ROOT.Math.PtEtaPhiMVector(*constructor).Mt2() == pytest.approx( + v.t*v.t - v.z*v.z + ) + +# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + assert ROOT.Math.PtEtaPhiMVector(*constructor).mt() == pytest.approx( + v.lib.sqrt(v.t*v.t - v.z*v.z) + ) + +# Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. +# P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_P2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).P2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().mag2 + ) + +# Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. +# P is our mag (same deal) +@pytest.mark.parametrize("constructor", constructor) +def test_P(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).P() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().mag + ) + +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp2(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Perp2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().rho2 + ) + +# Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PtEtaPhiMVector(*constructor).Perp2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().rho + ) + +# Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().phi + ) + +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rapidity(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Rapidity() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().rapidity + ) + +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Beta(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Beta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().beta + ) + +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Eta(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Eta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().eta + ) + +# Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Gamma(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Gamma() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().gamma + ) + +# Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isLightlike(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).isLightlike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().is_lightlike() + ) + +# Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isSpacelike(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).isSpacelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().is_spacelike() + ) + +# Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isTimelike(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).isTimelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().is_timelike() + ) + +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Theta(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Theta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().theta + ) + +# Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).X() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().x + ) + +# Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Y(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Y() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().y + ) + +# Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Z(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).Z() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().z + ) + +# Run a test that compares ROOT's 'T()' with vector's 't' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_T(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).T() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().t + ) + + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__add__(ROOT.Math.PtEtaPhiMVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__sub__(ROOT.Math.PtEtaPhiMVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + assert ref_vec.T() == pytest.approx(vec().t) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__eq__(ROOT.Math.PtEtaPhiMVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + ) + assert ref_vec == vec + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PtEtaPhiMVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PtEtaPhiMVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PtEtaPhiMVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) diff --git a/tests/root/test_PxPyPzMVector.py b/tests/root/test_PxPyPzMVector.py new file mode 100644 index 00000000..f41d362c --- /dev/null +++ b/tests/root/test_PxPyPzMVector.py @@ -0,0 +1,444 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.PxPyPzMVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0, 0), + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), + (0, 0, 1, 0), + (0, 0, 0, 4294967296), + (0, 4294967296, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1., 2., 3., 2.5), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinate_list = [ + "to_xyzt", + "to_xythetat", # may fail for constructor2 + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +# Dot +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + assert ROOT.Math.PxPyPzMVector(*constructor).Dot(ROOT.Math.PxPyPzMVector(*constructor)) == pytest.approx( + v1.dot(v2) + ) + +# Run the same test within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fizz_Dot(constructor1, constructor2, coordinates): + v1 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor1))), coordinates + )() + v2 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor2))), coordinates + )() + assert ROOT.Math.PxPyPzMVector(*constructor1).Dot(ROOT.Math.PxPyPzMVector(*constructor2)) == pytest.approx( + v1.dot(v2) + ) + +# Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. +# Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) +@pytest.mark.parametrize("constructor", constructor) +def test_M2(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().tau2 + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M2(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().tau2 + ) + +# Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. +# Mass is tau (or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_M(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().tau + ) + +# Run the same tests within hypothesis +@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_M(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().tau + ) + + +# Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt2(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + assert ROOT.Math.PxPyPzMVector(*constructor).Mt2() == pytest.approx( + v.t*v.t - v.z*v.z + ) + +# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mt(constructor, coordinates): + v = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + assert ROOT.Math.PxPyPzMVector(*constructor).mt() == pytest.approx( + v.lib.sqrt(v.t*v.t - v.z*v.z) + ) + +# Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. +# P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) +@pytest.mark.parametrize("constructor", constructor) +def test_P2(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).P2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().mag2 + ) + +# Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. +# P is our mag (same deal) +@pytest.mark.parametrize("constructor", constructor) +def test_P(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).P() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().mag + ) + +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp2(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Perp2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().rho2 + ) + +# Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PxPyPzMVector(*constructor).Perp2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().rho + ) + +# Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().phi + ) + +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rapidity(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Rapidity() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().rapidity + ) + +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Beta(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Beta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().beta + ) + +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Eta(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Eta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().eta + ) + +# Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Gamma(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Gamma() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().gamma + ) + +# Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isLightlike(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).isLightlike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().is_lightlike() + ) + +# Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isSpacelike(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).isSpacelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().is_spacelike() + ) + +# Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_isTimelike(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).isTimelike() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().is_timelike() + ) + +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Theta(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Theta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().theta + ) + +# Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).X() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().x + ) + +# Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Y(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Y() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().y + ) + +# Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Z(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).Z() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().z + ) + +# Run a test that compares ROOT's 'T()' with vector's 't' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_T(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).T() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().t + ) + + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__add__(ROOT.Math.PxPyPzMVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__sub__(ROOT.Math.PxPyPzMVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + assert ref_vec.T() == pytest.approx(vec().t) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.T() == pytest.approx(vec.t) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__eq__(ROOT.Math.PxPyPzMVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + ) + assert ref_vec == vec + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PxPyPzMVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PxPyPzMVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PxPyPzMVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + assert ref_vec.T() == pytest.approx(res_vec.t) diff --git a/tests/root/test_Quaternion.py b/tests/root/test_Quaternion.py new file mode 100644 index 00000000..53493eca --- /dev/null +++ b/tests/root/test_Quaternion.py @@ -0,0 +1,79 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.XYVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0, 0), + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), + (0, 0, 1, 0), + (0, 0, 0, 4294967296), + (0, 4294967296, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1., 2., 3., 2.5), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinate_list = [ + "to_xyzt", + "to_xythetat", # may fail for constructor2 + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param diff --git a/tests/root/test_RhoEtaPhiVector.py b/tests/root/test_RhoEtaPhiVector.py new file mode 100644 index 00000000..3966b0c9 --- /dev/null +++ b/tests/root/test_RhoEtaPhiVector.py @@ -0,0 +1,269 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import numpy as np + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.RhoEtaPhiVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0), + (0, 10, 0), + (0, -10, 0), + (1, 0, 0), + (1, 10, 0), + (1, -10, 0), + (1., 2.5, 2.), + (1, 2.5, 2.), + (1, -2.5, 2.), +] + +# Coordinate conversion methods to apply to the VectorObject2D. +coordinate_list = [ + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.RhoEtaPhiVector(*constructor).Dot(ROOT.Math.RhoEtaPhiVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )()) + ) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.RhoEtaPhiVector(*constructor1).Dot(ROOT.Math.RhoEtaPhiVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor1))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor2))), coordinates + )()) + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag2(constructor, coordinates): + assert ROOT.Math.RhoEtaPhiVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_R(constructor, coordinates): + assert ROOT.Math.RhoEtaPhiVector(*constructor).R() == pytest.approx( + np.sqrt(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().rho2) + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.RhoEtaPhiVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().phi + ) + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Unit(constructor, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + res_vec = vec.unit + assert ref_vec.X() == pytest.approx(res_vec().x) + assert ref_vec.Y() == pytest.approx(res_vec().y) + assert ref_vec.Z() == pytest.approx(res_vec().z) + + +# Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X_and_Y(constructor, coordinates): + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + assert (ROOT.Math.RhoEtaPhiVector(*constructor).X() == pytest.approx(vec.x) and + ROOT.Math.RhoEtaPhiVector(*constructor).Y() == pytest.approx(vec.y)) + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__add__(ROOT.Math.RhoEtaPhiVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__sub__(ROOT.Math.RhoEtaPhiVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__eq__(ROOT.Math.RhoEtaPhiVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + ) + assert ref_vec == vec diff --git a/tests/root/test_RhoZPhiVector.py b/tests/root/test_RhoZPhiVector.py new file mode 100644 index 00000000..c772cb55 --- /dev/null +++ b/tests/root/test_RhoZPhiVector.py @@ -0,0 +1,269 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import numpy as np + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.RhoZPhiVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0), + (0, 10, 0), + (0, -10, 0), + (1, 0, 0), + (1, 10, 0), + (1, -10, 0), + (1., 2.5, 2.), + (1, 2.5, 2.), + (1, -2.5, 2.), +] + +# Coordinate conversion methods to apply to the VectorObject2D. +coordinate_list = [ + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.RhoZPhiVector(*constructor).Dot(ROOT.Math.RhoZPhiVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )()) + ) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.RhoZPhiVector(*constructor1).Dot(ROOT.Math.RhoZPhiVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor1))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor2))), coordinates + )()) + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag2(constructor, coordinates): + assert ROOT.Math.RhoZPhiVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_R(constructor, coordinates): + assert ROOT.Math.RhoZPhiVector(*constructor).R() == pytest.approx( + np.sqrt(getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().rho2) + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.RhoZPhiVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().phi + ) + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.RhoZPhiVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.RhoZPhiVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.RhoZPhiVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Unit(constructor, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + res_vec = vec.unit + assert ref_vec.X() == pytest.approx(res_vec().x) + assert ref_vec.Y() == pytest.approx(res_vec().y) + assert ref_vec.Z() == pytest.approx(res_vec().z) + + +# Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X_and_Y(constructor, coordinates): + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + assert (ROOT.Math.RhoZPhiVector(*constructor).X() == pytest.approx(vec.x) and + ROOT.Math.RhoZPhiVector(*constructor).Y() == pytest.approx(vec.y)) + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__add__(ROOT.Math.RhoZPhiVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__sub__(ROOT.Math.RhoZPhiVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__eq__(ROOT.Math.RhoZPhiVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + ) + assert ref_vec == vec diff --git a/tests/root/test_XYVector.py b/tests/root/test_XYVector.py new file mode 100644 index 00000000..a5114856 --- /dev/null +++ b/tests/root/test_XYVector.py @@ -0,0 +1,233 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import numpy as np + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.XYVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0), + (0, 10), + (0, -10), + (1, 0), + (1, 10), + (1, -10), + (1., 2.5), + (1, 2.5), + (1, -2.5), +] + +# Coordinate conversion methods to apply to the VectorObject2D. +coordinate_list = [ + "to_xy", + "to_rhophi", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.XYVector(*constructor).Dot(ROOT.Math.XYVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )()) + ) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.XYVector(*constructor1).Dot(ROOT.Math.XYVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates + )()) + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag2(constructor, coordinates): + assert ROOT.Math.XYVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_R(constructor, coordinates): + assert ROOT.Math.XYVector(*constructor).R() == pytest.approx( + np.sqrt(getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().rho2) + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.XYVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().phi + ) + + +# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rotate(constructor, angle, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor) + ref_vec.Rotate(angle) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + + +# Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Unit(constructor, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )() + res_vec = vec.unit + assert ref_vec.X() == pytest.approx(res_vec().x) + assert ref_vec.Y() == pytest.approx(res_vec().y) + + +# Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X_and_Y(constructor, coordinates): + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )() + assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and + ROOT.Math.XYVector(*constructor).Y() == pytest.approx(vec.y)) + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )() + ) + assert ref_vec == vec diff --git a/tests/root/test_XYZVector.py b/tests/root/test_XYZVector.py new file mode 100644 index 00000000..66aa8612 --- /dev/null +++ b/tests/root/test_XYZVector.py @@ -0,0 +1,269 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import pytest +from hypothesis import given, strategies as st + +import numpy as np + +import vector + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# ROOT.Math.XYZVector constructor arguments to get all the weird cases. +constructor = [ + (0, 0, 0), + (0, 10, 0), + (0, -10, 0), + (1, 0, 0), + (1, 10, 0), + (1, -10, 0), + (1., 2.5, 2.), + (1, 2.5, 2.), + (1, -2.5, 2.), +] + +# Coordinate conversion methods to apply to the VectorObject2D. +coordinate_list = [ + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +angle_list = [ + 0, + 0.0, + 0.7853981633974483, + -0.7853981633974483, + 1.5707963267948966, + -1.5707963267948966, + 3.141592653589793, + -3.141592653589793, + 6.283185307179586, + -6.283185307179586, +] + +@pytest.fixture(scope="module", params=angle_list) +def angle(request): + return request.param + +scalar_list = [ + 0, + -1, + 1.0, + 100000.0000, + -100000.0000, +] + +@pytest.fixture(scope="module", params=scalar_list) +def scalar(request): + return request.param + +# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).Dot(ROOT.Math.XYZVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )()) + ) + + +# Run the same tests within hypothesis +@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7)), + constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7)) + | st.tuples(st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7))) +def test_fuzz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.XYZVector(*constructor1).Dot(ROOT.Math.XYZVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor1))), coordinates + )().dot(getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates + )()) + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Mag2(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).Mag2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_R(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).R() == pytest.approx( + np.sqrt(getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().rho2) + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Phi(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).Phi() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().phi + ) + + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.XYZVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + res_vec = vec.rotateX(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateY(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.XYZVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + res_vec = vec.rotateY(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_RotateZ(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.XYZVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + res_vec = vec.rotateZ(angle) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Unit(constructor, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).Unit() + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + res_vec = vec.unit + assert ref_vec.X() == pytest.approx(res_vec().x) + assert ref_vec.Y() == pytest.approx(res_vec().y) + assert ref_vec.Z() == pytest.approx(res_vec().z) + + +# Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_X_and_Y(constructor, coordinates): + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + assert (ROOT.Math.XYZVector(*constructor).X() == pytest.approx(vec.x) and + ROOT.Math.XYZVector(*constructor).Y() == pytest.approx(vec.y)) + +# Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_add(constructor, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).__add__(ROOT.Math.XYZVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().add(getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)()) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_sub(constructor, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).__sub__(ROOT.Math.XYZVector(*constructor)) + vec1 = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + vec2 = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ref_vec.Z() == pytest.approx(res_vec.z) + + +# Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_neg(constructor, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).__neg__() + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().__neg__ + assert ref_vec.X() == pytest.approx(vec().x) + assert ref_vec.Y() == pytest.approx(vec().y) + assert ref_vec.Z() == pytest.approx(vec().z) + + +# Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_mul(constructor, scalar, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).__mul__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().__mul__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_truediv(constructor, scalar, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) + + +# Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_eq(constructor, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).__eq__(ROOT.Math.XYZVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().isclose(getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + ) + assert ref_vec == vec diff --git a/tests/root/test_vector.py b/tests/root/test_vector.py new file mode 100644 index 00000000..28990045 --- /dev/null +++ b/tests/root/test_vector.py @@ -0,0 +1,92 @@ +# This test code was written by the `hypothesis.extra.ghostwriter` module +# and is provided under the Creative Commons Zero public domain dedication. + +import vector +from hypothesis import given, strategies as st + +import pytest + +# If ROOT is not available, skip these tests. +ROOT = pytest.importorskip("ROOT") + +# Coordinate conversion methods to apply to the VectorObject4D. +coordinate_list = [ + "to_xyzt", + "to_xythetat", + "to_xyetat", + "to_rhophizt", + "to_rhophithetat", + "to_rhophietat", + "to_xyztau", + "to_xythetatau", + "to_xyetatau", + "to_rhophiztau", + "to_rhophithetatau", + "to_rhophietatau", +] + +@pytest.fixture(scope="module", params=coordinate_list) +def coordinates(request): + return request.param + +constructor = [ + (0, 0, 0, 0), + (0, 0, 0, 10), + (0, 0, 0, -10), + (1, 2, 3, 0), + (1, 2, 3, 10), + (1, 2, 3, -10), + (1, 2, 3, 2.5), + (1, 2, 3, -2.5), +] + +@pytest.mark.parametrize("constructor", constructor) +def test_M2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) + +@given(constructor=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) +def test_fuzz_M2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) + +@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers())) +def test_fuzz_MomentumObject2D(azimuthal): + vec = vector.MomentumObject2D(azimuthal=azimuthal) + + +@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers()) +def test_fuzz_MomentumObject3D(azimuthal, longitudinal): + vec = vector.MomentumObject3D(azimuthal=azimuthal, longitudinal=longitudinal) + + +@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers(), temporal=st.floats() | st.integers()) +def test_fuzz_MomentumObject4D(azimuthal, longitudinal, temporal): + vec = vector.MomentumObject4D( + azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal + ) + + +@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers())) +def test_fuzz_VectorObject2D(azimuthal): + vector.VectorObject2D(azimuthal=azimuthal) + + +@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers()) +def test_fuzz_VectorObject3D(azimuthal, longitudinal): + vec = vector.VectorObject3D(azimuthal=azimuthal, longitudinal=longitudinal) + + +@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers(), temporal=st.floats() | st.integers()) +def test_fuzz_VectorObject4D(azimuthal, longitudinal, temporal): + vec = vector.VectorObject4D( + azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal + ) + # assert (vector.obj(**dict(zip(["x", "y", "z", "t"], azimuthal[0], azimuthal[1], longitudinal, temporal)))).tau == 0 + #pytest.approx(ROOT.Math.PxPyPzEVector(azimuthal[0], azimuthal[1], longitudinal, temporal).M()) From f1b45e58e0ac8f9cab89845bbf8fd2b7909cfa57 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Mon, 10 May 2021 17:08:12 +0200 Subject: [PATCH 04/10] [skip ci] pre-commit cleanup --- tests/root/test_EulerAngles.py | 17 +- tests/root/test_Polar2DVector.py | 466 ++++++++++++++++++----------- tests/root/test_Polar3DVector.py | 117 +++++--- tests/root/test_PtEtaPhiEVector.py | 216 +++++++++---- tests/root/test_PtEtaPhiMVector.py | 216 +++++++++---- tests/root/test_PxPyPzEVector.py | 151 ++++++++-- tests/root/test_PxPyPzMVector.py | 154 ++++++++-- tests/root/test_Quaternion.py | 17 +- tests/root/test_RhoEtaPhiVector.py | 115 ++++--- tests/root/test_RhoZPhiVector.py | 114 ++++--- tests/root/test_XYVector.py | 118 ++++---- tests/root/test_XYZVector.py | 138 +++++---- tests/root/test_vector.py | 65 +++- 13 files changed, 1307 insertions(+), 597 deletions(-) diff --git a/tests/root/test_EulerAngles.py b/tests/root/test_EulerAngles.py index 4b59357a..3a7d917c 100644 --- a/tests/root/test_EulerAngles.py +++ b/tests/root/test_EulerAngles.py @@ -4,9 +4,11 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given, strategies as st -import vector +# from hypothesis import given +# from hypothesis import strategies as st + +# import vector # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") @@ -14,7 +16,7 @@ # 4D constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), - (0, 0, 1, 0), # theta == 0.0 + (0, 0, 1, 0), # theta == 0.0 (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), @@ -24,7 +26,7 @@ (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), - (1., 2., 3., 2.5), + (1.0, 2.0, 3.0, 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] @@ -32,7 +34,7 @@ # Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ "to_xyzt", - "to_xythetat", # may fail for constructor2 + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -45,10 +47,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -62,10 +66,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -74,6 +80,7 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param diff --git a/tests/root/test_Polar2DVector.py b/tests/root/test_Polar2DVector.py index 91e4eee8..52912dd4 100644 --- a/tests/root/test_Polar2DVector.py +++ b/tests/root/test_Polar2DVector.py @@ -3,10 +3,10 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import pytest -from hypothesis import given, strategies as st - import numpy as np +import pytest +from hypothesis import given +from hypothesis import strategies as st import vector @@ -21,8 +21,8 @@ (1, 0), (10, 0), (1, 10), - (10., 10), - (1., 2.5), + (10.0, 10), + (1.0, 2.5), (1, 2.5), (1, 6.283185307179586), ] @@ -33,27 +33,31 @@ "to_rhophi", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, - 0.25*np.pi, - -0.25*np.pi, - 0.5*np.pi, - -0.5*np.pi, + 0.25 * np.pi, + -0.25 * np.pi, + 0.5 * np.pi, + -0.5 * np.pi, np.pi, -np.pi, - 2*np.pi, - -2*np.pi, + 2 * np.pi, + -2 * np.pi, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -62,38 +66,58 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): - assert ROOT.Math.Polar2DVector(*constructor).Dot(ROOT.Math.Polar2DVector(*constructor)) == pytest.approx( + assert ROOT.Math.Polar2DVector(*constructor).Dot( + ROOT.Math.Polar2DVector(*constructor) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )()) + )().dot( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() + ) ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.Polar2DVector(*constructor1).Dot(ROOT.Math.Polar2DVector(*constructor2)) == pytest.approx( + assert ROOT.Math.Polar2DVector(*constructor1).Dot( + ROOT.Math.Polar2DVector(*constructor2) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates - )()), rel=1e-6, abs=1e-6 + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates + )() + ), + rel=1e-6, + abs=1e-6, ) @@ -108,10 +132,16 @@ def test_Mag2(constructor, coordinates): # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_Mag2(constructor, coordinates): assert ROOT.Math.Polar2DVector(*constructor).Mag2() == pytest.approx( getattr( @@ -123,23 +153,29 @@ def test_fuzz_Mag2(constructor, coordinates): # Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag(constructor, coordinates): - assert ROOT.Math.sqrt(ROOT.Math.Polar2DVector(*constructor).Mag2()) == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().rho + assert ROOT.Math.sqrt( + ROOT.Math.Polar2DVector(*constructor).Mag2() + ) == pytest.approx( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)().rho ) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_Mag(constructor, coordinates): - assert ROOT.Math.sqrt(ROOT.Math.Polar2DVector(*constructor).Mag2()) == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().rho + assert ROOT.Math.sqrt( + ROOT.Math.Polar2DVector(*constructor).Mag2() + ) == pytest.approx( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)().rho ) @@ -147,23 +183,27 @@ def test_fuzz_Mag(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): assert ROOT.Math.Polar2DVector(*constructor).Phi() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().phi + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)().phi ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_Phi(constructor, coordinates): assert ROOT.Math.Polar2DVector(*constructor).Phi() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().phi + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)().phi ) + # Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Rotate(constructor, angle, coordinates): @@ -173,146 +213,182 @@ def test_Rotate(constructor, angle, coordinates): vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().rotateZ(angle) res_vec = vec.rotateZ(angle) - assert (ref_vec.R() == pytest.approx(res_vec.rho) and - ref_vec.Phi() == pytest.approx(res_vec.phi)) + assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( + res_vec.phi + ) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - angle=st.floats(min_value=-10e7, max_value=10e7) - | st.integers(min_value=-10e7, max_value=10e7)) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + angle=st.floats(min_value=-10e7, max_value=10e7) + | st.integers(min_value=-10e7, max_value=10e7), +) def test_fuzz_Rotate(constructor, angle, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor) ref_vec.Rotate(angle) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() + vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec.rotateZ(angle) - assert (ref_vec.R() == pytest.approx(res_vec.rho) and - ref_vec.Phi() == pytest.approx(res_vec.phi)) + assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( + res_vec.phi + ) # Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Unit(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() + vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec.unit - assert (ref_vec.R() == pytest.approx(res_vec().rho) and - ref_vec.Phi() == pytest.approx(res_vec().phi)) + assert ref_vec.R() == pytest.approx( + res_vec().rho + ) and ref_vec.Phi() == pytest.approx(res_vec().phi) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_Unit(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() + vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec.unit - assert (ref_vec.R() == pytest.approx(res_vec().rho) and - ref_vec.Phi() == pytest.approx(res_vec().phi)) + assert ref_vec.R() == pytest.approx( + res_vec().rho + ) and ref_vec.Phi() == pytest.approx(res_vec().phi) # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X_and_Y(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() + assert ref_vec.X() == pytest.approx(vec.x) and ref_vec.Y() == pytest.approx(vec.y) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) -def test_X_and_Y(constructor, coordinates): +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) +def test_fuzz_X_and_Y(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() + assert ref_vec.X() == pytest.approx(vec.x) and ref_vec.Y() == pytest.approx(vec.y) # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__add__(ROOT.Math.Polar2DVector(*constructor)) + ref_vec = ROOT.Math.Polar2DVector(*constructor).__add__( + ROOT.Math.Polar2DVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)()) - assert (ref_vec.R() == pytest.approx(vec.rho) and - ref_vec.Phi() == pytest.approx(vec.phi)) + )().add( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() + ) + assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( + vec.phi + ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_add(constructor1, constructor2, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor1).__add__(ROOT.Math.Polar2DVector(*constructor2)) + ref_vec = ROOT.Math.Polar2DVector(*constructor1).__add__( + ROOT.Math.Polar2DVector(*constructor2) + ) vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)()) - assert (ref_vec.R() == pytest.approx(vec.rho) and - ref_vec.Phi() == pytest.approx(vec.phi)) + )().add( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)() + ) + assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( + vec.phi + ) # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__sub__(ROOT.Math.Polar2DVector(*constructor)) - vec1 = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() - vec2 = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() + ref_vec = ROOT.Math.Polar2DVector(*constructor).__sub__( + ROOT.Math.Polar2DVector(*constructor) + ) + vec1 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() + vec2 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec1.subtract(vec2) - assert (ref_vec.R() == pytest.approx(res_vec.rho) and - ref_vec.Phi() == pytest.approx(res_vec.phi)) + assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( + res_vec.phi + ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_sub(constructor1, constructor2, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor1).__sub__(ROOT.Math.Polar2DVector(*constructor2)) - vec1 = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates - )() - vec2 = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates - )() + ref_vec = ROOT.Math.Polar2DVector(*constructor1).__sub__( + ROOT.Math.Polar2DVector(*constructor2) + ) + vec1 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates)() + vec2 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)() res_vec = vec1.subtract(vec2) - assert (ref_vec.R() == pytest.approx(res_vec.rho) and - ref_vec.Phi() == pytest.approx(res_vec.phi)) + assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( + res_vec.phi + ) # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @@ -322,22 +398,30 @@ def test_neg(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__neg__ - assert (ref_vec.R() == pytest.approx(vec().rho) and - ref_vec.Phi() == pytest.approx(vec().phi)) + assert ref_vec.R() == pytest.approx(vec().rho) and ref_vec.Phi() == pytest.approx( + vec().phi + ) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_neg(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).__neg__() vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__neg__ - assert (ref_vec.R() == pytest.approx(vec().rho) and - ref_vec.Phi() == pytest.approx(vec().phi)) + assert ref_vec.R() == pytest.approx(vec().rho) and ref_vec.Phi() == pytest.approx( + vec().phi + ) # Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. @@ -347,24 +431,32 @@ def test_mul(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.R() == pytest.approx(vec.rho) and - ref_vec.Phi() == pytest.approx(vec.phi)) + assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( + vec.phi + ) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - scalar=st.floats(min_value=-10e7, max_value=10e7) - | st.integers(min_value=-10e7, max_value=10e7)) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + scalar=st.floats(min_value=-10e7, max_value=10e7) + | st.integers(min_value=-10e7, max_value=10e7), +) def test_fuzz_mul(constructor, scalar, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).__mul__(scalar) vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.R() == pytest.approx(vec.rho) and - ref_vec.Phi() == pytest.approx(vec.phi)) + assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( + vec.phi + ) # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @@ -374,50 +466,66 @@ def test_truediv(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.R() == pytest.approx(vec.rho) and - ref_vec.Phi() == pytest.approx(vec.phi)) + assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( + vec.phi + ) # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - scalar=st.floats(min_value=-10e7, max_value=10e7) - | st.integers(min_value=-10e7, max_value=10e7)) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + scalar=st.floats(min_value=-10e7, max_value=10e7) + | st.integers(min_value=-10e7, max_value=10e7), +) def test_fuzz_truediv(constructor, scalar, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.R() == pytest.approx(vec.rho) and - ref_vec.Phi() == pytest.approx(vec.phi)) + assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( + vec.phi + ) # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__(ROOT.Math.Polar2DVector(*constructor)) + ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__( + ROOT.Math.Polar2DVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() + )().isclose( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() ) assert ref_vec == vec # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_eq(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__(ROOT.Math.Polar2DVector(*constructor)) + ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__( + ROOT.Math.Polar2DVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().equal(getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )() + )().equal( + getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() ) assert ref_vec == vec diff --git a/tests/root/test_Polar3DVector.py b/tests/root/test_Polar3DVector.py index 0f98249c..4bcb9db8 100644 --- a/tests/root/test_Polar3DVector.py +++ b/tests/root/test_Polar3DVector.py @@ -3,10 +3,10 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import pytest -from hypothesis import given, strategies as st - import numpy as np +import pytest +from hypothesis import given +from hypothesis import strategies as st import vector @@ -21,9 +21,9 @@ (1, 0, 0), (1, 10, 0), (1, -10, 0), - (1., 2.5, 2.), - (1, 2.5, 2.), - (1, -2.5, 2.), + (1.0, 2.5, 2.0), + (1, 2.5, 2.0), + (1, -2.5, 2.0), ] # Coordinate conversion methods to apply to the VectorObject2D. @@ -36,10 +36,12 @@ "to_xytheta", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -53,10 +55,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -65,39 +69,61 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. # rho = r*sin(theta) @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): - assert ROOT.Math.Polar3DVector(*constructor).Dot(ROOT.Math.Polar3DVector(*constructor)) == pytest.approx( + assert ROOT.Math.Polar3DVector(*constructor).Dot( + ROOT.Math.Polar3DVector(*constructor) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), + coordinates, + )() + ) ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.Polar3DVector(*constructor1).Dot(ROOT.Math.Polar3DVector(*constructor2)) == pytest.approx( + assert ROOT.Math.Polar3DVector(*constructor1).Dot( + ROOT.Math.Polar3DVector(*constructor2) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor2))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor2))), + coordinates, + )() + ) ) @@ -115,9 +141,12 @@ def test_Mag2(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): assert ROOT.Math.Polar3DVector(*constructor).R() == pytest.approx( - np.sqrt(getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )().rho2) + np.sqrt( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), + coordinates, + )().rho2 + ) ) @@ -134,7 +163,7 @@ def test_Phi(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.Polar3DVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.Polar3DVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() @@ -147,7 +176,7 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.Polar3DVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.Polar3DVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() @@ -160,7 +189,7 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.Polar3DVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.Polar3DVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() @@ -189,17 +218,24 @@ def test_X_and_Y(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() - assert (ROOT.Math.Polar3DVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.Polar3DVector(*constructor).Y() == pytest.approx(vec.y)) + assert ROOT.Math.Polar3DVector(*constructor).X() == pytest.approx( + vec.x + ) and ROOT.Math.Polar3DVector(*constructor).Y() == pytest.approx(vec.y) + # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.Polar3DVector(*constructor).__add__(ROOT.Math.Polar3DVector(*constructor)) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__add__( + ROOT.Math.Polar3DVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -208,7 +244,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.Polar3DVector(*constructor).__sub__(ROOT.Math.Polar3DVector(*constructor)) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__sub__( + ROOT.Math.Polar3DVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() @@ -260,11 +298,14 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.Polar3DVector(*constructor).__eq__(ROOT.Math.Polar3DVector(*constructor)) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__eq__( + ROOT.Math.Polar3DVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() ) assert ref_vec == vec diff --git a/tests/root/test_PtEtaPhiEVector.py b/tests/root/test_PtEtaPhiEVector.py index 24d85b2c..f3b276b4 100644 --- a/tests/root/test_PtEtaPhiEVector.py +++ b/tests/root/test_PtEtaPhiEVector.py @@ -4,7 +4,8 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given, strategies as st +from hypothesis import given +from hypothesis import strategies as st import vector @@ -14,7 +15,7 @@ # ROOT.Math.PtEtaPhiEVector constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), - (0, 0, 1, 0), # theta == 0.0 + (0, 0, 1, 0), # theta == 0.0 (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), @@ -24,7 +25,7 @@ (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), - (1., 2., 3., 2.5), + (1.0, 2.0, 3.0, 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] @@ -32,7 +33,7 @@ # Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ "to_xyzt", - "to_xythetat", # may fail for constructor2 + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -45,10 +46,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -62,10 +65,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -74,10 +79,12 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. # Dot @pytest.mark.parametrize("constructor", constructor) @@ -88,15 +95,38 @@ def test_Dot(constructor, coordinates): v2 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() - assert ROOT.Math.PtEtaPhiEVector(*constructor).Dot(ROOT.Math.PtEtaPhiEVector(*constructor)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PtEtaPhiEVector(*constructor).Dot( + ROOT.Math.PtEtaPhiEVector(*constructor) + ) == pytest.approx(v1.dot(v2)) + # Run the same test within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fizz_Dot(constructor1, constructor2, coordinates): v1 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor1))), coordinates @@ -104,9 +134,10 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): v2 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor2))), coordinates )() - assert ROOT.Math.PtEtaPhiEVector(*constructor1).Dot(ROOT.Math.PtEtaPhiEVector(*constructor2)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PtEtaPhiEVector(*constructor1).Dot( + ROOT.Math.PtEtaPhiEVector(*constructor2) + ) == pytest.approx(v1.dot(v2)) + # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @@ -114,35 +145,68 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): def test_M2(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).M2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().tau2 ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).M2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().tau2 ) + # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) def test_M(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).M() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().tau ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).M() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().tau ) @@ -154,9 +218,10 @@ def test_Mt2(constructor, coordinates): vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() assert ROOT.Math.PtEtaPhiEVector(*constructor).Mt2() == pytest.approx( - v.t*v.t - v.z*v.z + v.t * v.t - v.z * v.z ) + # Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt(constructor, coordinates): @@ -164,161 +229,197 @@ def test_Mt(constructor, coordinates): vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() assert ROOT.Math.PtEtaPhiEVector(*constructor).mt() == pytest.approx( - v.lib.sqrt(v.t*v.t - v.z*v.z) + v.lib.sqrt(v.t * v.t - v.z * v.z) ) + # Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. # P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) @pytest.mark.parametrize("constructor", constructor) def test_P2(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).P2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().mag2 ) + # Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. # P is our mag (same deal) @pytest.mark.parametrize("constructor", constructor) def test_P(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).P() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().mag ) + # Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp2(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Perp2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().rho2 ) + # Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp(constructor, coordinates): - assert ROOT.Math.sqrt(ROOT.Math.PtEtaPhiEVector(*constructor).Perp2()) == pytest.approx( + assert ROOT.Math.sqrt( + ROOT.Math.PtEtaPhiEVector(*constructor).Perp2() + ) == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().rho ) + # Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().phi ) + # Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Rapidity(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Rapidity() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().rapidity ) + # Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Beta(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Beta() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().beta ) + # Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Eta(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Eta() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().eta ) + # Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Gamma(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Gamma() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().gamma ) + # Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isLightlike(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).isLightlike() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().is_lightlike() ) + # Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isSpacelike(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).isSpacelike() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().is_spacelike() ) + # Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isTimelike(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).isTimelike() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().is_timelike() ) + # Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Theta(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Theta() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().theta ) + # Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).X() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().x ) + # Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Y(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Y() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().y ) + # Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Z(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).Z() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().z ) + # Run a test that compares ROOT's 'T()' with vector's 't' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_T(constructor, coordinates): assert ROOT.Math.PtEtaPhiEVector(*constructor).T() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, )().t ) @@ -326,11 +427,17 @@ def test_T(constructor, coordinates): # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__add__(ROOT.Math.PtEtaPhiEVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__add__( + ROOT.Math.PtEtaPhiEVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -340,7 +447,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__sub__(ROOT.Math.PtEtaPhiEVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__sub__( + ROOT.Math.PtEtaPhiEVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() @@ -353,6 +462,7 @@ def test_sub(constructor, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) assert ref_vec.T() == pytest.approx(res_vec.t) + # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_neg(constructor, coordinates): @@ -395,12 +505,16 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__eq__(ROOT.Math.PtEtaPhiEVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__eq__( + ROOT.Math.PtEtaPhiEVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), + coordinates, + )() ) assert ref_vec == vec @@ -408,7 +522,7 @@ def test_eq(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PtEtaPhiEVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.PtEtaPhiEVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() @@ -422,7 +536,7 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PtEtaPhiEVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.PtEtaPhiEVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() @@ -436,7 +550,7 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PtEtaPhiEVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.PtEtaPhiEVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )() diff --git a/tests/root/test_PtEtaPhiMVector.py b/tests/root/test_PtEtaPhiMVector.py index 0447e232..6d70713c 100644 --- a/tests/root/test_PtEtaPhiMVector.py +++ b/tests/root/test_PtEtaPhiMVector.py @@ -4,7 +4,8 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given, strategies as st +from hypothesis import given +from hypothesis import strategies as st import vector @@ -14,7 +15,7 @@ # ROOT.Math.PtEtaPhiMVector constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), - (0, 0, 1, 0), # theta == 0.0 + (0, 0, 1, 0), # theta == 0.0 (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), @@ -24,7 +25,7 @@ (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), - (1., 2., 3., 2.5), + (1.0, 2.0, 3.0, 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] @@ -32,7 +33,7 @@ # Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ "to_xyzt", - "to_xythetat", # may fail for constructor2 + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -45,10 +46,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -62,10 +65,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -74,10 +79,12 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. # Dot @pytest.mark.parametrize("constructor", constructor) @@ -88,15 +95,38 @@ def test_Dot(constructor, coordinates): v2 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() - assert ROOT.Math.PtEtaPhiMVector(*constructor).Dot(ROOT.Math.PtEtaPhiMVector(*constructor)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PtEtaPhiMVector(*constructor).Dot( + ROOT.Math.PtEtaPhiMVector(*constructor) + ) == pytest.approx(v1.dot(v2)) + # Run the same test within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fizz_Dot(constructor1, constructor2, coordinates): v1 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor1))), coordinates @@ -104,9 +134,10 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): v2 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor2))), coordinates )() - assert ROOT.Math.PtEtaPhiMVector(*constructor1).Dot(ROOT.Math.PtEtaPhiMVector(*constructor2)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PtEtaPhiMVector(*constructor1).Dot( + ROOT.Math.PtEtaPhiMVector(*constructor2) + ) == pytest.approx(v1.dot(v2)) + # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @@ -114,35 +145,68 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): def test_M2(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).M2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().tau2 ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).M2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().tau2 ) + # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) def test_M(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).M() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().tau ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).M() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().tau ) @@ -154,9 +218,10 @@ def test_Mt2(constructor, coordinates): vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() assert ROOT.Math.PtEtaPhiMVector(*constructor).Mt2() == pytest.approx( - v.t*v.t - v.z*v.z + v.t * v.t - v.z * v.z ) + # Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt(constructor, coordinates): @@ -164,161 +229,197 @@ def test_Mt(constructor, coordinates): vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() assert ROOT.Math.PtEtaPhiMVector(*constructor).mt() == pytest.approx( - v.lib.sqrt(v.t*v.t - v.z*v.z) + v.lib.sqrt(v.t * v.t - v.z * v.z) ) + # Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. # P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) @pytest.mark.parametrize("constructor", constructor) def test_P2(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).P2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().mag2 ) + # Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. # P is our mag (same deal) @pytest.mark.parametrize("constructor", constructor) def test_P(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).P() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().mag ) + # Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp2(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Perp2() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().rho2 ) + # Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp(constructor, coordinates): - assert ROOT.Math.sqrt(ROOT.Math.PtEtaPhiMVector(*constructor).Perp2()) == pytest.approx( + assert ROOT.Math.sqrt( + ROOT.Math.PtEtaPhiMVector(*constructor).Perp2() + ) == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().rho ) + # Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().phi ) + # Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Rapidity(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Rapidity() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().rapidity ) + # Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Beta(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Beta() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().beta ) + # Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Eta(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Eta() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().eta ) + # Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Gamma(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Gamma() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().gamma ) + # Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isLightlike(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).isLightlike() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().is_lightlike() ) + # Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isSpacelike(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).isSpacelike() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().is_spacelike() ) + # Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isTimelike(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).isTimelike() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().is_timelike() ) + # Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Theta(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Theta() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().theta ) + # Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).X() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().x ) + # Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Y(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Y() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().y ) + # Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Z(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).Z() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().z ) + # Run a test that compares ROOT's 'T()' with vector's 't' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_T(constructor, coordinates): assert ROOT.Math.PtEtaPhiMVector(*constructor).T() == pytest.approx( getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, )().t ) @@ -326,11 +427,17 @@ def test_T(constructor, coordinates): # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__add__(ROOT.Math.PtEtaPhiMVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__add__( + ROOT.Math.PtEtaPhiMVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -340,7 +447,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__sub__(ROOT.Math.PtEtaPhiMVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__sub__( + ROOT.Math.PtEtaPhiMVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() @@ -353,6 +462,7 @@ def test_sub(constructor, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) assert ref_vec.T() == pytest.approx(res_vec.t) + # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_neg(constructor, coordinates): @@ -395,12 +505,16 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__eq__(ROOT.Math.PtEtaPhiMVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__eq__( + ROOT.Math.PtEtaPhiMVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), + coordinates, + )() ) assert ref_vec == vec @@ -408,7 +522,7 @@ def test_eq(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PtEtaPhiMVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.PtEtaPhiMVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() @@ -422,7 +536,7 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PtEtaPhiMVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.PtEtaPhiMVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() @@ -436,7 +550,7 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PtEtaPhiMVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.PtEtaPhiMVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )() diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index 6d62dcbb..f22659bf 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -4,7 +4,8 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given, strategies as st +from hypothesis import given +from hypothesis import strategies as st import vector @@ -14,7 +15,7 @@ # ROOT.Math.PxPyPzEVector constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), - (0, 0, 1, 0), # theta == 0.0 + (0, 0, 1, 0), # theta == 0.0 (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), @@ -24,7 +25,7 @@ (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), - (1., 2., 3., 2.5), + (1.0, 2.0, 3.0, 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] @@ -32,7 +33,7 @@ # Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ "to_xyzt", - "to_xythetat", # may fail for constructor2 + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -45,10 +46,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -62,10 +65,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -74,10 +79,12 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. # Dot @pytest.mark.parametrize("constructor", constructor) @@ -88,15 +95,38 @@ def test_Dot(constructor, coordinates): v2 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() - assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PxPyPzEVector(*constructor).Dot( + ROOT.Math.PxPyPzEVector(*constructor) + ) == pytest.approx(v1.dot(v2)) + # Run the same test within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fizz_Dot(constructor1, constructor2, coordinates): v1 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates @@ -104,9 +134,10 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): v2 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates )() - assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PxPyPzEVector(*constructor1).Dot( + ROOT.Math.PxPyPzEVector(*constructor2) + ) == pytest.approx(v1.dot(v2)) + # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @@ -118,8 +149,22 @@ def test_M2(constructor, coordinates): )().tau2 ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( getattr( @@ -127,6 +172,7 @@ def test_fuzz_M2(constructor, coordinates): )().tau2 ) + # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) @@ -137,8 +183,22 @@ def test_M(constructor, coordinates): )().tau ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( getattr( @@ -154,9 +214,10 @@ def test_Mt2(constructor, coordinates): vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() assert ROOT.Math.PxPyPzEVector(*constructor).Mt2() == pytest.approx( - v.t*v.t - v.z*v.z + v.t * v.t - v.z * v.z ) + # Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt(constructor, coordinates): @@ -164,9 +225,10 @@ def test_Mt(constructor, coordinates): vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( - v.lib.sqrt(v.t*v.t - v.z*v.z) + v.lib.sqrt(v.t * v.t - v.z * v.z) ) + # Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. # P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) @pytest.mark.parametrize("constructor", constructor) @@ -177,6 +239,7 @@ def test_P2(constructor, coordinates): )().mag2 ) + # Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. # P is our mag (same deal) @pytest.mark.parametrize("constructor", constructor) @@ -187,6 +250,7 @@ def test_P(constructor, coordinates): )().mag ) + # Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp2(constructor, coordinates): @@ -196,15 +260,19 @@ def test_Perp2(constructor, coordinates): )().rho2 ) + # Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp(constructor, coordinates): - assert ROOT.Math.sqrt(ROOT.Math.PxPyPzEVector(*constructor).Perp2()) == pytest.approx( + assert ROOT.Math.sqrt( + ROOT.Math.PxPyPzEVector(*constructor).Perp2() + ) == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )().rho ) + # Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): @@ -214,6 +282,7 @@ def test_Phi(constructor, coordinates): )().phi ) + # Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Rapidity(constructor, coordinates): @@ -223,6 +292,7 @@ def test_Rapidity(constructor, coordinates): )().rapidity ) + # Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Beta(constructor, coordinates): @@ -232,6 +302,7 @@ def test_Beta(constructor, coordinates): )().beta ) + # Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Eta(constructor, coordinates): @@ -241,6 +312,7 @@ def test_Eta(constructor, coordinates): )().eta ) + # Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Gamma(constructor, coordinates): @@ -250,6 +322,7 @@ def test_Gamma(constructor, coordinates): )().gamma ) + # Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isLightlike(constructor, coordinates): @@ -259,6 +332,7 @@ def test_isLightlike(constructor, coordinates): )().is_lightlike() ) + # Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isSpacelike(constructor, coordinates): @@ -268,6 +342,7 @@ def test_isSpacelike(constructor, coordinates): )().is_spacelike() ) + # Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isTimelike(constructor, coordinates): @@ -277,6 +352,7 @@ def test_isTimelike(constructor, coordinates): )().is_timelike() ) + # Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Theta(constructor, coordinates): @@ -286,6 +362,7 @@ def test_Theta(constructor, coordinates): )().theta ) + # Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X(constructor, coordinates): @@ -295,6 +372,7 @@ def test_X(constructor, coordinates): )().x ) + # Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Y(constructor, coordinates): @@ -304,6 +382,7 @@ def test_Y(constructor, coordinates): )().y ) + # Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Z(constructor, coordinates): @@ -313,6 +392,7 @@ def test_Z(constructor, coordinates): )().z ) + # Run a test that compares ROOT's 'T()' with vector's 't' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_T(constructor, coordinates): @@ -326,11 +406,16 @@ def test_T(constructor, coordinates): # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__add__(ROOT.Math.PxPyPzEVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__add__( + ROOT.Math.PxPyPzEVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -340,7 +425,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__sub__(ROOT.Math.PxPyPzEVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__sub__( + ROOT.Math.PxPyPzEVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() @@ -353,6 +440,7 @@ def test_sub(constructor, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) assert ref_vec.T() == pytest.approx(res_vec.t) + # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_neg(constructor, coordinates): @@ -395,12 +483,15 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__eq__(ROOT.Math.PxPyPzEVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzEVector(*constructor).__eq__( + ROOT.Math.PxPyPzEVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )() ) assert ref_vec == vec @@ -408,7 +499,7 @@ def test_eq(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PxPyPzEVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.PxPyPzEVector(*constructor) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() @@ -422,7 +513,7 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PxPyPzEVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.PxPyPzEVector(*constructor) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() @@ -436,7 +527,7 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PxPyPzEVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.PxPyPzEVector(*constructor) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() diff --git a/tests/root/test_PxPyPzMVector.py b/tests/root/test_PxPyPzMVector.py index f41d362c..ee545c06 100644 --- a/tests/root/test_PxPyPzMVector.py +++ b/tests/root/test_PxPyPzMVector.py @@ -4,7 +4,8 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given, strategies as st +from hypothesis import given +from hypothesis import strategies as st import vector @@ -14,7 +15,7 @@ # ROOT.Math.PxPyPzMVector constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), - (0, 0, 1, 0), # theta == 0.0 + (0, 0, 1, 0), # theta == 0.0 (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), @@ -24,7 +25,7 @@ (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), - (1., 2., 3., 2.5), + (1.0, 2.0, 3.0, 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] @@ -32,7 +33,7 @@ # Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ "to_xyzt", - "to_xythetat", # may fail for constructor2 + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -45,10 +46,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -62,10 +65,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -74,10 +79,12 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. # Dot @pytest.mark.parametrize("constructor", constructor) @@ -88,15 +95,38 @@ def test_Dot(constructor, coordinates): v2 = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() - assert ROOT.Math.PxPyPzMVector(*constructor).Dot(ROOT.Math.PxPyPzMVector(*constructor)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PxPyPzMVector(*constructor).Dot( + ROOT.Math.PxPyPzMVector(*constructor) + ) == pytest.approx(v1.dot(v2)) + # Run the same test within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fizz_Dot(constructor1, constructor2, coordinates): v1 = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor1))), coordinates @@ -104,9 +134,10 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): v2 = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor2))), coordinates )() - assert ROOT.Math.PxPyPzMVector(*constructor1).Dot(ROOT.Math.PxPyPzMVector(*constructor2)) == pytest.approx( - v1.dot(v2) - ) + assert ROOT.Math.PxPyPzMVector(*constructor1).Dot( + ROOT.Math.PxPyPzMVector(*constructor2) + ) == pytest.approx(v1.dot(v2)) + # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @@ -118,8 +149,22 @@ def test_M2(constructor, coordinates): )().tau2 ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PxPyPzMVector(*constructor).M2() == pytest.approx( getattr( @@ -127,6 +172,7 @@ def test_fuzz_M2(constructor, coordinates): )().tau2 ) + # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) @@ -137,8 +183,22 @@ def test_M(constructor, coordinates): )().tau ) + # Run the same tests within hypothesis -@given(constructor=st.tuples(st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7)) | st.tuples(st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ) +) def test_fuzz_M(constructor, coordinates): assert ROOT.Math.PxPyPzMVector(*constructor).M() == pytest.approx( getattr( @@ -154,9 +214,10 @@ def test_Mt2(constructor, coordinates): vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() assert ROOT.Math.PxPyPzMVector(*constructor).Mt2() == pytest.approx( - v.t*v.t - v.z*v.z + v.t * v.t - v.z * v.z ) + # Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt(constructor, coordinates): @@ -164,9 +225,10 @@ def test_Mt(constructor, coordinates): vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() assert ROOT.Math.PxPyPzMVector(*constructor).mt() == pytest.approx( - v.lib.sqrt(v.t*v.t - v.z*v.z) + v.lib.sqrt(v.t * v.t - v.z * v.z) ) + # Run a test that compares ROOT's 'P2()' with vector's 'mag2' for all cases. # P2 is our mag2 (ROOT's 4D mag2 is the dot product with itself, what we call tau or mass) @pytest.mark.parametrize("constructor", constructor) @@ -177,6 +239,7 @@ def test_P2(constructor, coordinates): )().mag2 ) + # Run a test that compares ROOT's 'P()' with vector's 'mag' for all cases. # P is our mag (same deal) @pytest.mark.parametrize("constructor", constructor) @@ -187,6 +250,7 @@ def test_P(constructor, coordinates): )().mag ) + # Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp2(constructor, coordinates): @@ -196,15 +260,19 @@ def test_Perp2(constructor, coordinates): )().rho2 ) + # Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Perp(constructor, coordinates): - assert ROOT.Math.sqrt(ROOT.Math.PxPyPzMVector(*constructor).Perp2()) == pytest.approx( + assert ROOT.Math.sqrt( + ROOT.Math.PxPyPzMVector(*constructor).Perp2() + ) == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().rho ) + # Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): @@ -214,6 +282,7 @@ def test_Phi(constructor, coordinates): )().phi ) + # Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Rapidity(constructor, coordinates): @@ -223,6 +292,7 @@ def test_Rapidity(constructor, coordinates): )().rapidity ) + # Run a test that compares ROOT's 'Beta()' with vector's 'beta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Beta(constructor, coordinates): @@ -232,6 +302,7 @@ def test_Beta(constructor, coordinates): )().beta ) + # Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Eta(constructor, coordinates): @@ -241,6 +312,7 @@ def test_Eta(constructor, coordinates): )().eta ) + # Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Gamma(constructor, coordinates): @@ -250,6 +322,7 @@ def test_Gamma(constructor, coordinates): )().gamma ) + # Run a test that compares ROOT's 'isLightlike()' with vector's 'is_lightlike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isLightlike(constructor, coordinates): @@ -259,6 +332,7 @@ def test_isLightlike(constructor, coordinates): )().is_lightlike() ) + # Run a test that compares ROOT's 'isSpacelike()' with vector's 'is_spacelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isSpacelike(constructor, coordinates): @@ -268,6 +342,7 @@ def test_isSpacelike(constructor, coordinates): )().is_spacelike() ) + # Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_isTimelike(constructor, coordinates): @@ -277,6 +352,7 @@ def test_isTimelike(constructor, coordinates): )().is_timelike() ) + # Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Theta(constructor, coordinates): @@ -286,6 +362,7 @@ def test_Theta(constructor, coordinates): )().theta ) + # Run a test that compares ROOT's 'X()' with vector's 'x' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X(constructor, coordinates): @@ -295,6 +372,7 @@ def test_X(constructor, coordinates): )().x ) + # Run a test that compares ROOT's 'Y()' with vector's 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Y(constructor, coordinates): @@ -304,6 +382,7 @@ def test_Y(constructor, coordinates): )().y ) + # Run a test that compares ROOT's 'Z()' with vector's 'z' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Z(constructor, coordinates): @@ -313,6 +392,7 @@ def test_Z(constructor, coordinates): )().z ) + # Run a test that compares ROOT's 'T()' with vector's 't' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_T(constructor, coordinates): @@ -326,11 +406,16 @@ def test_T(constructor, coordinates): # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__add__(ROOT.Math.PxPyPzMVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__add__( + ROOT.Math.PxPyPzMVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -340,7 +425,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__sub__(ROOT.Math.PxPyPzMVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__sub__( + ROOT.Math.PxPyPzMVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() @@ -353,6 +440,7 @@ def test_sub(constructor, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) assert ref_vec.T() == pytest.approx(res_vec.t) + # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_neg(constructor, coordinates): @@ -395,19 +483,23 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__eq__(ROOT.Math.PxPyPzMVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__eq__( + ROOT.Math.PxPyPzMVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )() ) assert ref_vec == vec + # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.PxPyPzMVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.PxPyPzMVector(*constructor) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() @@ -417,10 +509,11 @@ def test_RotateX(constructor, angle, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) assert ref_vec.T() == pytest.approx(res_vec.t) + # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.PxPyPzMVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.PxPyPzMVector(*constructor) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() @@ -430,10 +523,11 @@ def test_RotateY(constructor, angle, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) assert ref_vec.T() == pytest.approx(res_vec.t) + # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.PxPyPzMVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.PxPyPzMVector(*constructor) vec = getattr( vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )() diff --git a/tests/root/test_Quaternion.py b/tests/root/test_Quaternion.py index 53493eca..23f16730 100644 --- a/tests/root/test_Quaternion.py +++ b/tests/root/test_Quaternion.py @@ -4,9 +4,11 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given, strategies as st -import vector +# from hypothesis import given +# from hypothesis import strategies as st +# +# import vector # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") @@ -14,7 +16,7 @@ # ROOT.Math.XYVector constructor arguments to get all the weird cases. constructor = [ (0, 0, 0, 0), - (0, 0, 1, 0), # theta == 0.0 + (0, 0, 1, 0), # theta == 0.0 (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), @@ -24,7 +26,7 @@ (1, 2, 3, 0), (1, 2, 3, 10), (1, 2, 3, -10), - (1., 2., 3., 2.5), + (1.0, 2.0, 3.0, 2.5), (1, 2, 3, 2.5), (1, 2, 3, -2.5), ] @@ -32,7 +34,7 @@ # Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ "to_xyzt", - "to_xythetat", # may fail for constructor2 + "to_xythetat", # may fail for constructor2 "to_xyetat", "to_rhophizt", "to_rhophithetat", @@ -45,10 +47,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -62,10 +66,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -74,6 +80,7 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param diff --git a/tests/root/test_RhoEtaPhiVector.py b/tests/root/test_RhoEtaPhiVector.py index 3966b0c9..59640cb0 100644 --- a/tests/root/test_RhoEtaPhiVector.py +++ b/tests/root/test_RhoEtaPhiVector.py @@ -3,10 +3,10 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import pytest -from hypothesis import given, strategies as st - import numpy as np +import pytest +from hypothesis import given +from hypothesis import strategies as st import vector @@ -21,9 +21,9 @@ (1, 0, 0), (1, 10, 0), (1, -10, 0), - (1., 2.5, 2.), - (1, 2.5, 2.), - (1, -2.5, 2.), + (1.0, 2.5, 2.0), + (1, 2.5, 2.0), + (1, -2.5, 2.0), ] # Coordinate conversion methods to apply to the VectorObject2D. @@ -36,10 +36,12 @@ "to_xytheta", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -53,10 +55,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -65,38 +69,59 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): - assert ROOT.Math.RhoEtaPhiVector(*constructor).Dot(ROOT.Math.RhoEtaPhiVector(*constructor)) == pytest.approx( + assert ROOT.Math.RhoEtaPhiVector(*constructor).Dot( + ROOT.Math.RhoEtaPhiVector(*constructor) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + ) ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.RhoEtaPhiVector(*constructor1).Dot(ROOT.Math.RhoEtaPhiVector(*constructor2)) == pytest.approx( + assert ROOT.Math.RhoEtaPhiVector(*constructor1).Dot( + ROOT.Math.RhoEtaPhiVector(*constructor2) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor2))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor2))), + coordinates, + )() + ) ) @@ -114,9 +139,11 @@ def test_Mag2(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): assert ROOT.Math.RhoEtaPhiVector(*constructor).R() == pytest.approx( - np.sqrt(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates - )().rho2) + np.sqrt( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )().rho2 + ) ) @@ -133,7 +160,7 @@ def test_Phi(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.RhoEtaPhiVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() @@ -146,7 +173,7 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.RhoEtaPhiVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() @@ -159,7 +186,7 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.RhoEtaPhiVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() @@ -188,17 +215,24 @@ def test_X_and_Y(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() - assert (ROOT.Math.RhoEtaPhiVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.RhoEtaPhiVector(*constructor).Y() == pytest.approx(vec.y)) + assert ROOT.Math.RhoEtaPhiVector(*constructor).X() == pytest.approx( + vec.x + ) and ROOT.Math.RhoEtaPhiVector(*constructor).Y() == pytest.approx(vec.y) + # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__add__(ROOT.Math.RhoEtaPhiVector(*constructor)) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__add__( + ROOT.Math.RhoEtaPhiVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -207,7 +241,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__sub__(ROOT.Math.RhoEtaPhiVector(*constructor)) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__sub__( + ROOT.Math.RhoEtaPhiVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() @@ -259,11 +295,14 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__eq__(ROOT.Math.RhoEtaPhiVector(*constructor)) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__eq__( + ROOT.Math.RhoEtaPhiVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates + )() ) assert ref_vec == vec diff --git a/tests/root/test_RhoZPhiVector.py b/tests/root/test_RhoZPhiVector.py index c772cb55..7b491285 100644 --- a/tests/root/test_RhoZPhiVector.py +++ b/tests/root/test_RhoZPhiVector.py @@ -3,10 +3,10 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import pytest -from hypothesis import given, strategies as st - import numpy as np +import pytest +from hypothesis import given +from hypothesis import strategies as st import vector @@ -21,9 +21,9 @@ (1, 0, 0), (1, 10, 0), (1, -10, 0), - (1., 2.5, 2.), - (1, 2.5, 2.), - (1, -2.5, 2.), + (1.0, 2.5, 2.0), + (1, 2.5, 2.0), + (1, -2.5, 2.0), ] # Coordinate conversion methods to apply to the VectorObject2D. @@ -36,10 +36,12 @@ "to_xytheta", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -53,10 +55,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -65,38 +69,58 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): - assert ROOT.Math.RhoZPhiVector(*constructor).Dot(ROOT.Math.RhoZPhiVector(*constructor)) == pytest.approx( + assert ROOT.Math.RhoZPhiVector(*constructor).Dot( + ROOT.Math.RhoZPhiVector(*constructor) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + ) ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.RhoZPhiVector(*constructor1).Dot(ROOT.Math.RhoZPhiVector(*constructor2)) == pytest.approx( + assert ROOT.Math.RhoZPhiVector(*constructor1).Dot( + ROOT.Math.RhoZPhiVector(*constructor2) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor2))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor2))), coordinates + )() + ) ) @@ -114,9 +138,11 @@ def test_Mag2(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): assert ROOT.Math.RhoZPhiVector(*constructor).R() == pytest.approx( - np.sqrt(getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates - )().rho2) + np.sqrt( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )().rho2 + ) ) @@ -133,7 +159,7 @@ def test_Phi(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.RhoZPhiVector(*constructor) + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.RhoZPhiVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() @@ -146,7 +172,7 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.RhoZPhiVector(*constructor) + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.RhoZPhiVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() @@ -159,7 +185,7 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.RhoZPhiVector(*constructor) + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.RhoZPhiVector(*constructor) vec = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() @@ -188,17 +214,24 @@ def test_X_and_Y(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() - assert (ROOT.Math.RhoZPhiVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.RhoZPhiVector(*constructor).Y() == pytest.approx(vec.y)) + assert ROOT.Math.RhoZPhiVector(*constructor).X() == pytest.approx( + vec.x + ) and ROOT.Math.RhoZPhiVector(*constructor).Y() == pytest.approx(vec.y) + # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__add__(ROOT.Math.RhoZPhiVector(*constructor)) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__add__( + ROOT.Math.RhoZPhiVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates)()) + )().add( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -207,7 +240,9 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__sub__(ROOT.Math.RhoZPhiVector(*constructor)) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__sub__( + ROOT.Math.RhoZPhiVector(*constructor) + ) vec1 = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() @@ -259,11 +294,14 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__eq__(ROOT.Math.RhoZPhiVector(*constructor)) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__eq__( + ROOT.Math.RhoZPhiVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates - )() + )().isclose( + getattr( + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates + )() ) assert ref_vec == vec diff --git a/tests/root/test_XYVector.py b/tests/root/test_XYVector.py index a5114856..a84ff063 100644 --- a/tests/root/test_XYVector.py +++ b/tests/root/test_XYVector.py @@ -3,10 +3,10 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import pytest -from hypothesis import given, strategies as st - import numpy as np +import pytest +from hypothesis import given +from hypothesis import strategies as st import vector @@ -21,7 +21,7 @@ (1, 0), (1, 10), (1, -10), - (1., 2.5), + (1.0, 2.5), (1, 2.5), (1, -2.5), ] @@ -32,10 +32,12 @@ "to_rhophi", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -49,10 +51,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -61,38 +65,50 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): - assert ROOT.Math.XYVector(*constructor).Dot(ROOT.Math.XYVector(*constructor)) == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )()) + assert ROOT.Math.XYVector(*constructor).Dot( + ROOT.Math.XYVector(*constructor) + ) == pytest.approx( + getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)().dot( + getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() + ) ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.XYVector(*constructor1).Dot(ROOT.Math.XYVector(*constructor2)) == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates - )()) + assert ROOT.Math.XYVector(*constructor1).Dot( + ROOT.Math.XYVector(*constructor2) + ) == pytest.approx( + getattr(vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates)().dot( + getattr(vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates)() + ) ) @@ -100,9 +116,7 @@ def test_fuzz_Dot(constructor1, constructor2, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): assert ROOT.Math.XYVector(*constructor).Mag2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho2 + getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)().rho2 ) @@ -110,9 +124,11 @@ def test_Mag2(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): assert ROOT.Math.XYVector(*constructor).R() == pytest.approx( - np.sqrt(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho2) + np.sqrt( + getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().rho2 + ) ) @@ -120,9 +136,7 @@ def test_R(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): assert ROOT.Math.XYVector(*constructor).Phi() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().phi + getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)().phi ) @@ -131,9 +145,7 @@ def test_Phi(constructor, coordinates): def test_Rotate(constructor, angle, coordinates): ref_vec = ROOT.Math.XYVector(*constructor) ref_vec.Rotate(angle) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )() + vec = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() res_vec = vec.rotateZ(angle) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) @@ -143,9 +155,7 @@ def test_Rotate(constructor, angle, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_Unit(constructor, coordinates): ref_vec = ROOT.Math.XYVector(*constructor).Unit() - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )() + vec = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() res_vec = vec.unit assert ref_vec.X() == pytest.approx(res_vec().x) assert ref_vec.Y() == pytest.approx(res_vec().y) @@ -154,20 +164,19 @@ def test_Unit(constructor, coordinates): # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X_and_Y(constructor, coordinates): - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )() - assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYVector(*constructor).Y() == pytest.approx(vec.y)) + vec = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() + assert ROOT.Math.XYVector(*constructor).X() == pytest.approx( + vec.x + ) and ROOT.Math.XYVector(*constructor).Y() == pytest.approx(vec.y) + # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): ref_vec = ROOT.Math.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) + vec = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)().add( + getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) @@ -176,12 +185,8 @@ def test_add(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): ref_vec = ROOT.Math.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec1 = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )() - vec2 = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )() + vec1 = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() + vec2 = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() res_vec = vec1.subtract(vec2) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) @@ -226,8 +231,7 @@ def test_eq(constructor, coordinates): ref_vec = ROOT.Math.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )() + )().isclose( + getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() ) assert ref_vec == vec diff --git a/tests/root/test_XYZVector.py b/tests/root/test_XYZVector.py index 66aa8612..7cd2880e 100644 --- a/tests/root/test_XYZVector.py +++ b/tests/root/test_XYZVector.py @@ -3,10 +3,10 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import pytest -from hypothesis import given, strategies as st - import numpy as np +import pytest +from hypothesis import given +from hypothesis import strategies as st import vector @@ -21,9 +21,9 @@ (1, 0, 0), (1, 10, 0), (1, -10, 0), - (1., 2.5, 2.), - (1, 2.5, 2.), - (1, -2.5, 2.), + (1.0, 2.5, 2.0), + (1, 2.5, 2.0), + (1, -2.5, 2.0), ] # Coordinate conversion methods to apply to the VectorObject2D. @@ -36,10 +36,12 @@ "to_xytheta", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + angle_list = [ 0, 0.0, @@ -53,10 +55,12 @@ def coordinates(request): -6.283185307179586, ] + @pytest.fixture(scope="module", params=angle_list) def angle(request): return request.param + scalar_list = [ 0, -1, @@ -65,38 +69,58 @@ def angle(request): -100000.0000, ] + @pytest.fixture(scope="module", params=scalar_list) def scalar(request): return request.param + # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): - assert ROOT.Math.XYZVector(*constructor).Dot(ROOT.Math.XYZVector(*constructor)) == pytest.approx( + assert ROOT.Math.XYZVector(*constructor).Dot( + ROOT.Math.XYZVector(*constructor) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )() + ) ) # Run the same tests within hypothesis -@given(constructor1=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7)), - constructor2=st.tuples(st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7)) - | st.tuples(st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7))) +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.XYZVector(*constructor1).Dot(ROOT.Math.XYZVector(*constructor2)) == pytest.approx( + assert ROOT.Math.XYZVector(*constructor1).Dot( + ROOT.Math.XYZVector(*constructor2) + ) == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates - )()) + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates + )() + ) ) @@ -114,9 +138,11 @@ def test_Mag2(constructor, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): assert ROOT.Math.XYZVector(*constructor).R() == pytest.approx( - np.sqrt(getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().rho2) + np.sqrt( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().rho2 + ) ) @@ -133,10 +159,8 @@ def test_Phi(constructor, coordinates): # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.XYZVector(*constructor) - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() + ref_vec = ROOT.Math.RotationX(angle) * ROOT.Math.XYZVector(*constructor) + vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() res_vec = vec.rotateX(angle) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) @@ -146,10 +170,8 @@ def test_RotateX(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateY()' with vector's 'rotateY' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateY(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationY(angle)*ROOT.Math.XYZVector(*constructor) - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() + ref_vec = ROOT.Math.RotationY(angle) * ROOT.Math.XYZVector(*constructor) + vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() res_vec = vec.rotateY(angle) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) @@ -159,10 +181,8 @@ def test_RotateY(constructor, angle, coordinates): # Run a test that compares ROOT's 'RotateZ()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateZ(constructor, angle, coordinates): - ref_vec = ROOT.Math.RotationZ(angle)*ROOT.Math.XYZVector(*constructor) - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() + ref_vec = ROOT.Math.RotationZ(angle) * ROOT.Math.XYZVector(*constructor) + vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() res_vec = vec.rotateZ(angle) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) @@ -173,9 +193,7 @@ def test_RotateZ(constructor, angle, coordinates): @pytest.mark.parametrize("constructor", constructor) def test_Unit(constructor, coordinates): ref_vec = ROOT.Math.XYZVector(*constructor).Unit() - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() + vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() res_vec = vec.unit assert ref_vec.X() == pytest.approx(res_vec().x) assert ref_vec.Y() == pytest.approx(res_vec().y) @@ -185,20 +203,23 @@ def test_Unit(constructor, coordinates): # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X_and_Y(constructor, coordinates): - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() - assert (ROOT.Math.XYZVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYZVector(*constructor).Y() == pytest.approx(vec.y)) + vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() + assert ROOT.Math.XYZVector(*constructor).X() == pytest.approx( + vec.x + ) and ROOT.Math.XYZVector(*constructor).Y() == pytest.approx(vec.y) + # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): - ref_vec = ROOT.Math.XYZVector(*constructor).__add__(ROOT.Math.XYZVector(*constructor)) + ref_vec = ROOT.Math.XYZVector(*constructor).__add__( + ROOT.Math.XYZVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().add(getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)()) + )().add( + getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() + ) assert ref_vec.X() == pytest.approx(vec.x) assert ref_vec.Y() == pytest.approx(vec.y) assert ref_vec.Z() == pytest.approx(vec.z) @@ -207,13 +228,11 @@ def test_add(constructor, coordinates): # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): - ref_vec = ROOT.Math.XYZVector(*constructor).__sub__(ROOT.Math.XYZVector(*constructor)) - vec1 = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() - vec2 = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() + ref_vec = ROOT.Math.XYZVector(*constructor).__sub__( + ROOT.Math.XYZVector(*constructor) + ) + vec1 = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() + vec2 = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() res_vec = vec1.subtract(vec2) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) @@ -259,11 +278,12 @@ def test_truediv(constructor, scalar, coordinates): # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): - ref_vec = ROOT.Math.XYZVector(*constructor).__eq__(ROOT.Math.XYZVector(*constructor)) + ref_vec = ROOT.Math.XYZVector(*constructor).__eq__( + ROOT.Math.XYZVector(*constructor) + ) vec = getattr( vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().isclose(getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )() + )().isclose( + getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() ) assert ref_vec == vec diff --git a/tests/root/test_vector.py b/tests/root/test_vector.py index 28990045..6a20169f 100644 --- a/tests/root/test_vector.py +++ b/tests/root/test_vector.py @@ -1,10 +1,11 @@ # This test code was written by the `hypothesis.extra.ghostwriter` module # and is provided under the Creative Commons Zero public domain dedication. -import vector -from hypothesis import given, strategies as st - import pytest +from hypothesis import given +from hypothesis import strategies as st + +import vector # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") @@ -25,10 +26,12 @@ "to_rhophietatau", ] + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param + constructor = [ (0, 0, 0, 0), (0, 0, 0, 10), @@ -40,6 +43,7 @@ def coordinates(request): (1, 2, 3, -2.5), ] + @pytest.mark.parametrize("constructor", constructor) def test_M2(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( @@ -48,7 +52,11 @@ def test_M2(constructor, coordinates): )().tau2 ) -@given(constructor=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) + +@given( + constructor=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()) +) def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( getattr( @@ -56,37 +64,62 @@ def test_fuzz_M2(constructor, coordinates): )().tau2 ) -@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers())) + +@given( + azimuthal=st.tuples(st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers()) +) def test_fuzz_MomentumObject2D(azimuthal): - vec = vector.MomentumObject2D(azimuthal=azimuthal) + vector.MomentumObject2D(azimuthal=azimuthal) -@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers()) +@given( + azimuthal=st.tuples(st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers()), + longitudinal=st.floats() | st.integers(), +) def test_fuzz_MomentumObject3D(azimuthal, longitudinal): - vec = vector.MomentumObject3D(azimuthal=azimuthal, longitudinal=longitudinal) + vector.MomentumObject3D(azimuthal=azimuthal, longitudinal=longitudinal) -@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers(), temporal=st.floats() | st.integers()) +@given( + azimuthal=st.tuples(st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers()), + longitudinal=st.floats() | st.integers(), + temporal=st.floats() | st.integers(), +) def test_fuzz_MomentumObject4D(azimuthal, longitudinal, temporal): - vec = vector.MomentumObject4D( + vector.MomentumObject4D( azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal ) -@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers())) +@given( + azimuthal=st.tuples(st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers()) +) def test_fuzz_VectorObject2D(azimuthal): vector.VectorObject2D(azimuthal=azimuthal) -@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers()) +@given( + azimuthal=st.tuples(st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers()), + longitudinal=st.floats() | st.integers(), +) def test_fuzz_VectorObject3D(azimuthal, longitudinal): - vec = vector.VectorObject3D(azimuthal=azimuthal, longitudinal=longitudinal) + vector.VectorObject3D(azimuthal=azimuthal, longitudinal=longitudinal) -@given(azimuthal=st.tuples(st.floats(), st.floats()) | st.tuples(st.integers(), st.integers()), longitudinal=st.floats() | st.integers(), temporal=st.floats() | st.integers()) +@given( + azimuthal=st.tuples(st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers()), + longitudinal=st.floats() | st.integers(), + temporal=st.floats() | st.integers(), +) def test_fuzz_VectorObject4D(azimuthal, longitudinal, temporal): - vec = vector.VectorObject4D( + vector.VectorObject4D( azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal ) # assert (vector.obj(**dict(zip(["x", "y", "z", "t"], azimuthal[0], azimuthal[1], longitudinal, temporal)))).tau == 0 - #pytest.approx(ROOT.Math.PxPyPzEVector(azimuthal[0], azimuthal[1], longitudinal, temporal).M()) + # pytest.approx(ROOT.Math.PxPyPzEVector(azimuthal[0], azimuthal[1], longitudinal, temporal).M()) From 06c76d7e29a3036ad25119a0618153ed7ebed504 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 11 May 2021 12:01:14 +0200 Subject: [PATCH 05/10] [skip ci] check XY and XYZ vectors tests --- tests/root/test_XYVector.py | 39 ++++++++++++++++---------- tests/root/test_XYZVector.py | 54 +++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/tests/root/test_XYVector.py b/tests/root/test_XYVector.py index a84ff063..65af09e1 100644 --- a/tests/root/test_XYVector.py +++ b/tests/root/test_XYVector.py @@ -79,7 +79,9 @@ def test_Dot(constructor, coordinates): ) == pytest.approx( getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)().dot( getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() - ) + ), + 1.0e-6, + 1.0e-6, ) @@ -108,7 +110,9 @@ def test_fuzz_Dot(constructor1, constructor2, coordinates): ) == pytest.approx( getattr(vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates)().dot( getattr(vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates)() - ) + ), + 1.0e-6, + 1.0e-6, ) @@ -154,11 +158,14 @@ def test_Rotate(constructor, angle, coordinates): # Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Unit(constructor, coordinates): - ref_vec = ROOT.Math.XYVector(*constructor).Unit() - vec = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() - res_vec = vec.unit - assert ref_vec.X() == pytest.approx(res_vec().x) - assert ref_vec.Y() == pytest.approx(res_vec().y) + # FIXME: if x == 0 and y == 0 + # assert 0.0 == 1.0 ± 1.0e-06 + if constructor[0] != 0 and constructor[1] != 0: + ref_vec = ROOT.Math.XYVector(*constructor).Unit() + vec = getattr(vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)() + res_vec = vec.unit + assert ref_vec.X() == pytest.approx(res_vec().x) + assert ref_vec.Y() == pytest.approx(res_vec().y) # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @@ -210,19 +217,21 @@ def test_mul(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )().__mul__(scalar) - assert ref_vec.X() == pytest.approx(vec.x) - assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.X() == pytest.approx( + vec.x, 1.0e-6, 1.0e-6 + ) and ref_vec.Y() == pytest.approx(vec.y, 1.0e-6, 1.0e-6) # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_truediv(constructor, scalar, coordinates): - ref_vec = ROOT.Math.XYVector(*constructor).__truediv__(scalar) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().__truediv__(scalar) - assert ref_vec.X() == pytest.approx(vec.x) - assert ref_vec.Y() == pytest.approx(vec.y) + if scalar != 0: + ref_vec = ROOT.Math.XYVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. diff --git a/tests/root/test_XYZVector.py b/tests/root/test_XYZVector.py index 7cd2880e..8a2298b5 100644 --- a/tests/root/test_XYZVector.py +++ b/tests/root/test_XYZVector.py @@ -3,7 +3,6 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import numpy as np import pytest from hypothesis import given from hypothesis import strategies as st @@ -96,18 +95,22 @@ def test_Dot(constructor, coordinates): constructor1=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), ) | st.tuples( st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), ), constructor2=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), ) | st.tuples( st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), ), ) def test_fuzz_Dot(constructor1, constructor2, coordinates): @@ -120,29 +123,35 @@ def test_fuzz_Dot(constructor1, constructor2, coordinates): getattr( vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates )() - ) + ), + 1.0e-6, + 1.0e-6, ) # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): - assert ROOT.Math.XYZVector(*constructor).Mag2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().rho2 + ref_vec = ROOT.Math.XYZVector(*constructor) + vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() + assert ( + pytest.approx(vec.x) == ref_vec.X() + and pytest.approx(vec.y) == ref_vec.Y() + and pytest.approx(vec.z) == ref_vec.Z() ) + assert ref_vec.Mag2() == pytest.approx(vec.mag2, 1.0e-6, 1.0e-6) + # Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_R(constructor, coordinates): +def test_Mag(constructor, coordinates): assert ROOT.Math.XYZVector(*constructor).R() == pytest.approx( - np.sqrt( - getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().rho2 - ) + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().mag, + 1.0e-6, + 1.0e-6, ) @@ -258,21 +267,22 @@ def test_mul(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().__mul__(scalar) - assert ref_vec.X() == pytest.approx(vec.x) - assert ref_vec.Y() == pytest.approx(vec.y) - assert ref_vec.Z() == pytest.approx(vec.z) + assert ref_vec.X() == pytest.approx(vec.x, 1.0e-6, 1.0e-6) + assert ref_vec.Y() == pytest.approx(vec.y, 1.0e-6, 1.0e-6) + assert ref_vec.Z() == pytest.approx(vec.z, 1.0e-6, 1.0e-6) # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_truediv(constructor, scalar, coordinates): - ref_vec = ROOT.Math.XYZVector(*constructor).__truediv__(scalar) - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates - )().__truediv__(scalar) - assert ref_vec.X() == pytest.approx(vec.x) - assert ref_vec.Y() == pytest.approx(vec.y) - assert ref_vec.Z() == pytest.approx(vec.z) + if scalar != 0: + ref_vec = ROOT.Math.XYZVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. From 94a6d5f3f0c0f8fa0692cae95f450c42861365b3 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 11 May 2021 14:01:06 +0200 Subject: [PATCH 06/10] [skip ci] update Polar3DVector tests --- tests/root/test_Polar2DVector.py | 126 +++++++++++------- tests/root/test_Polar3DVector.py | 212 +++++++++++++++++++++++++++---- tests/root/test_XYZVector.py | 146 ++++++++++++++++++++- 3 files changed, 412 insertions(+), 72 deletions(-) diff --git a/tests/root/test_Polar2DVector.py b/tests/root/test_Polar2DVector.py index 52912dd4..2ba87d69 100644 --- a/tests/root/test_Polar2DVector.py +++ b/tests/root/test_Polar2DVector.py @@ -213,8 +213,15 @@ def test_Rotate(constructor, angle, coordinates): vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().rotateZ(angle) res_vec = vec.rotateZ(angle) - assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( - res_vec.phi + assert ref_vec.R() == pytest.approx( + res_vec.rho, + 1.0e-6, + 1.0e-6, + ) + assert ref_vec.Phi() == pytest.approx( + res_vec.phi, + 1.0e-6, + 1.0e-6, ) @@ -236,8 +243,15 @@ def test_fuzz_Rotate(constructor, angle, coordinates): ref_vec.Rotate(angle) vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec.rotateZ(angle) - assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( - res_vec.phi + assert ref_vec.R() == pytest.approx( + res_vec.rho, + 1.0e-6, + 1.0e-6, + ) + assert ref_vec.Phi() == pytest.approx( + res_vec.phi, + 1.0e-6, + 1.0e-6, ) @@ -247,9 +261,8 @@ def test_Unit(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec.unit - assert ref_vec.R() == pytest.approx( - res_vec().rho - ) and ref_vec.Phi() == pytest.approx(res_vec().phi) + assert ref_vec.R() == pytest.approx(res_vec().rho) + assert ref_vec.Phi() == pytest.approx(res_vec().phi) # Run the same tests within hypothesis @@ -267,9 +280,8 @@ def test_fuzz_Unit(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec.unit - assert ref_vec.R() == pytest.approx( - res_vec().rho - ) and ref_vec.Phi() == pytest.approx(res_vec().phi) + assert ref_vec.R() == pytest.approx(res_vec().rho) + assert ref_vec.Phi() == pytest.approx(res_vec().phi) # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @@ -308,8 +320,15 @@ def test_add(constructor, coordinates): )().add( getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() ) - assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( - vec.phi + assert ref_vec.R() == pytest.approx( + vec.rho, + 1.0e-6, + 1.0e-6, + ) + assert ref_vec.Phi() == pytest.approx( + vec.phi, + 1.0e-6, + 1.0e-6, ) @@ -341,8 +360,15 @@ def test_fuzz_add(constructor1, constructor2, coordinates): )().add( getattr(vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)() ) - assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( - vec.phi + assert ref_vec.R() == pytest.approx( + vec.rho, + 1.0e-6, + 1.0e-6, + ) + assert ref_vec.Phi() == pytest.approx( + vec.phi, + 1.0e-6, + 1.0e-6, ) @@ -355,8 +381,15 @@ def test_sub(constructor, coordinates): vec1 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() vec2 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() res_vec = vec1.subtract(vec2) - assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( - res_vec.phi + assert ref_vec.R() == pytest.approx( + res_vec.rho, + 1.0e-6, + 1.0e-6, + ) + assert ref_vec.Phi() == pytest.approx( + res_vec.phi, + 1.0e-6, + 1.0e-6, ) @@ -386,8 +419,15 @@ def test_fuzz_sub(constructor1, constructor2, coordinates): vec1 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates)() vec2 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)() res_vec = vec1.subtract(vec2) - assert ref_vec.R() == pytest.approx(res_vec.rho) and ref_vec.Phi() == pytest.approx( - res_vec.phi + assert ref_vec.R() == pytest.approx( + res_vec.rho, + 1.0e-6, + 1.0e-6, + ) + assert ref_vec.Phi() == pytest.approx( + res_vec.phi, + 1.0e-6, + 1.0e-6, ) @@ -398,9 +438,8 @@ def test_neg(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__neg__ - assert ref_vec.R() == pytest.approx(vec().rho) and ref_vec.Phi() == pytest.approx( - vec().phi - ) + assert ref_vec.R() == pytest.approx(vec().rho) + assert ref_vec.Phi() == pytest.approx(vec().phi) # Run the same tests within hypothesis @@ -419,9 +458,8 @@ def test_fuzz_neg(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__neg__ - assert ref_vec.R() == pytest.approx(vec().rho) and ref_vec.Phi() == pytest.approx( - vec().phi - ) + assert ref_vec.R() == pytest.approx(vec().rho) + assert ref_vec.Phi() == pytest.approx(vec().phi) # Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. @@ -431,9 +469,8 @@ def test_mul(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__mul__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( - vec.phi - ) + assert ref_vec.R() == pytest.approx(vec.rho) + assert ref_vec.Phi() == pytest.approx(vec.phi) # Run the same tests within hypothesis @@ -454,21 +491,21 @@ def test_fuzz_mul(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__mul__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( - vec.phi - ) + assert ref_vec.R() == pytest.approx(vec.rho) + assert ref_vec.Phi() == pytest.approx(vec.phi) # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_truediv(constructor, scalar, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().__truediv__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( - vec.phi - ) + # FIXME: + if scalar != 0: + ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.R() == pytest.approx(vec.rho) + assert ref_vec.Phi() == pytest.approx(vec.phi) # Run the same tests within hypothesis @@ -485,13 +522,14 @@ def test_truediv(constructor, scalar, coordinates): | st.integers(min_value=-10e7, max_value=10e7), ) def test_fuzz_truediv(constructor, scalar, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().__truediv__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) and ref_vec.Phi() == pytest.approx( - vec.phi - ) + # FIXME: + if scalar != 0: + ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.R() == pytest.approx(vec.rho) + assert ref_vec.Phi() == pytest.approx(vec.phi) # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. diff --git a/tests/root/test_Polar3DVector.py b/tests/root/test_Polar3DVector.py index 4bcb9db8..e735c6ea 100644 --- a/tests/root/test_Polar3DVector.py +++ b/tests/root/test_Polar3DVector.py @@ -3,7 +3,6 @@ # Distributed under the 3-clause BSD license, see accompanying file LICENSE # or https://github.com/scikit-hep/vector for details. -import numpy as np import pytest from hypothesis import given from hypothesis import strategies as st @@ -98,18 +97,22 @@ def test_Dot(constructor, coordinates): constructor1=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), ) | st.tuples( st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), ), constructor2=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), ) | st.tuples( st.integers(min_value=-10e7, max_value=10e7), st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), ), ) def test_fuzz_Dot(constructor1, constructor2, coordinates): @@ -127,30 +130,142 @@ def test_fuzz_Dot(constructor1, constructor2, coordinates): ) +# Run a test that compares ROOT's 'Cross()' with vector's 'cross' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Cross(constructor, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor).Cross( + ROOT.Math.Polar3DVector(*constructor) + ) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().cross( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + ) + assert ( + ref_vec.Rho() + == pytest.approx( + vec.rho, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Theta() + == pytest.approx( + vec.theta, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Phi() + == pytest.approx( + vec.phi, + 1.0e-6, + 1.0e-6, + ) + ) + + +# Run the same tests within hypothesis +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) +def test_fuzz_Cross(constructor1, constructor2, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor1).Cross( + ROOT.Math.Polar3DVector(*constructor2) + ) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor1))), coordinates + )().cross( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor2))), coordinates + )() + ) + assert ( + ref_vec.Rho() + == pytest.approx( + vec.rho, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Theta() + == pytest.approx( + vec.theta, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Phi() + == pytest.approx( + vec.phi, + 1.0e-6, + 1.0e-6, + ) + ) + + # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): assert ROOT.Math.Polar3DVector(*constructor).Mag2() == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )().rho2 + )().mag2 ) -# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +# Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): - assert ROOT.Math.Polar3DVector(*constructor).R() == pytest.approx( - np.sqrt( - getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), - coordinates, - )().rho2 - ) + assert ROOT.Math.sqrt( + ROOT.Math.Polar3DVector(*constructor).Mag2() + ) == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), + coordinates, + )().mag ) -# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp2(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Perp2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().rho2 + ) + + +# Run a test that compares ROOT's 'Rho()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Rho(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Rho() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), + coordinates, + )().rho + ) + + +# Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): assert ROOT.Math.Polar3DVector(*constructor).Phi() == pytest.approx( @@ -160,6 +275,26 @@ def test_Phi(constructor, coordinates): ) +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def Eta(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Eta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().eta + ) + + +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Theta(constructor, coordinates): + assert ROOT.Math.Polar3DVector(*constructor).Theta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().theta + ) + + # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): @@ -199,6 +334,35 @@ def test_RotateZ(constructor, angle, coordinates): assert ref_vec.Z() == pytest.approx(res_vec.z) +# Run a test that compares ROOT's 'RotateAxes' with vector's 'rotate_axes' for all cases. +def test_RotateAxes(constructor, angle, coordinates): + ref_vec = ROOT.Math.Polar3DVector(*constructor) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )() + # FIXME: rotate_axis + assert ( + ref_vec.Rho() + == pytest.approx( + vec.rho, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Theta() + == pytest.approx( + vec.theta, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Phi() + == pytest.approx( + vec.phi, + 1.0e-6, + 1.0e-6, + ) + ) + + # Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Unit(constructor, coordinates): @@ -214,13 +378,15 @@ def test_Unit(constructor, coordinates): # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_X_and_Y(constructor, coordinates): +def test_X_Y_Z(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() - assert ROOT.Math.Polar3DVector(*constructor).X() == pytest.approx( - vec.x - ) and ROOT.Math.Polar3DVector(*constructor).Y() == pytest.approx(vec.y) + assert ( + ROOT.Math.Polar3DVector(*constructor).X() == pytest.approx(vec.x) + and ROOT.Math.Polar3DVector(*constructor).Y() == pytest.approx(vec.y) + and ROOT.Math.Polar3DVector(*constructor).Z() == pytest.approx(vec.z) + ) # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @@ -286,13 +452,15 @@ def test_mul(constructor, scalar, coordinates): # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_truediv(constructor, scalar, coordinates): - ref_vec = ROOT.Math.Polar3DVector(*constructor).__truediv__(scalar) - vec = getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates - )().__truediv__(scalar) - assert ref_vec.X() == pytest.approx(vec.x) - assert ref_vec.Y() == pytest.approx(vec.y) - assert ref_vec.Z() == pytest.approx(vec.z) + # FIXME + if scalar != 0: + ref_vec = ROOT.Math.Polar3DVector(*constructor).__truediv__(scalar) + vec = getattr( + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates + )().__truediv__(scalar) + assert ref_vec.X() == pytest.approx(vec.x) + assert ref_vec.Y() == pytest.approx(vec.y) + assert ref_vec.Z() == pytest.approx(vec.z) # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. diff --git a/tests/root/test_XYZVector.py b/tests/root/test_XYZVector.py index 8a2298b5..0532384e 100644 --- a/tests/root/test_XYZVector.py +++ b/tests/root/test_XYZVector.py @@ -129,7 +129,92 @@ def test_fuzz_Dot(constructor1, constructor2, coordinates): ) -# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +# Run a test that compares ROOT's 'Cross()' with vector's 'cross' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Cross(constructor, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor).Cross(ROOT.Math.XYZVector(*constructor)) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().cross( + getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() + ) + assert ( + ref_vec.X() + == pytest.approx( + vec.x, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Y() + == pytest.approx( + vec.y, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Z() + == pytest.approx( + vec.z, + 1.0e-6, + 1.0e-6, + ) + ) + + +# Run the same tests within hypothesis +@given( + constructor1=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), + constructor2=st.tuples( + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + st.floats(min_value=-10e7, max_value=10e7), + ) + | st.tuples( + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + st.integers(min_value=-10e7, max_value=10e7), + ), +) +def test_fuzz_Cross(constructor1, constructor2, coordinates): + ref_vec = ROOT.Math.XYZVector(*constructor1).Cross( + ROOT.Math.XYZVector(*constructor2) + ) + vec = getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor1))), coordinates + )().cross( + getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates)() + ) + assert ( + ref_vec.X() + == pytest.approx( + vec.x, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Y() + == pytest.approx( + vec.y, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Z() + == pytest.approx( + vec.z, + 1.0e-6, + 1.0e-6, + ) + ) + + +# Run a test that compares ROOT's 'Mag2()' with vector's 'mag2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): ref_vec = ROOT.Math.XYZVector(*constructor) @@ -143,7 +228,7 @@ def test_Mag2(constructor, coordinates): assert ref_vec.Mag2() == pytest.approx(vec.mag2, 1.0e-6, 1.0e-6) -# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +# Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag(constructor, coordinates): assert ROOT.Math.XYZVector(*constructor).R() == pytest.approx( @@ -155,6 +240,30 @@ def test_Mag(constructor, coordinates): ) +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp2(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).Perp2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().rho2, + 1.0e-6, + 1.0e-6, + ) + + +# Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.XYZVector(*constructor).Perp2()) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().rho, + 1.0e-6, + 1.0e-6, + ) + + # Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): @@ -165,6 +274,28 @@ def test_Phi(constructor, coordinates): ) +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Eta(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).Eta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().eta, + 1.0e-6, + 1.0e-6, + ) + + +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Theta(constructor, coordinates): + assert ROOT.Math.XYZVector(*constructor).Theta() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates + )().theta + ) + + # Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_RotateX(constructor, angle, coordinates): @@ -211,11 +342,13 @@ def test_Unit(constructor, coordinates): # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_X_and_Y(constructor, coordinates): +def test_X_Y_Z(constructor, coordinates): vec = getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates)() - assert ROOT.Math.XYZVector(*constructor).X() == pytest.approx( - vec.x - ) and ROOT.Math.XYZVector(*constructor).Y() == pytest.approx(vec.y) + assert ( + ROOT.Math.XYZVector(*constructor).X() == pytest.approx(vec.x) + and ROOT.Math.XYZVector(*constructor).Y() == pytest.approx(vec.y) + and ROOT.Math.XYZVector(*constructor).Z() == pytest.approx(vec.z) + ) # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @@ -275,6 +408,7 @@ def test_mul(constructor, scalar, coordinates): # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_truediv(constructor, scalar, coordinates): + # FIXME if scalar != 0: ref_vec = ROOT.Math.XYZVector(*constructor).__truediv__(scalar) vec = getattr( From 4894cf8cefce82cc1b414ebc2e9f11b6b5cb1be9 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Wed, 12 May 2021 18:26:17 +0200 Subject: [PATCH 07/10] comparison with Polar3DVector needs more work, save for now --- src/vector/_compute/spatial/dot.py | 13 +++-- src/vector/_compute/spatial/eta.py | 2 +- tests/root/test_Polar2DVector.py | 77 +++------------------------ tests/root/test_Polar3DVector.py | 84 +++++++++++++++++------------- 4 files changed, 62 insertions(+), 114 deletions(-) diff --git a/src/vector/_compute/spatial/dot.py b/src/vector/_compute/spatial/dot.py index 16afbf0a..37ea0c7d 100644 --- a/src/vector/_compute/spatial/dot.py +++ b/src/vector/_compute/spatial/dot.py @@ -32,7 +32,7 @@ # specialized def xy_z_xy_z(lib, x1, y1, z1, x2, y2, z2): - return x1 * x2 + y1 * y2 + z1 * z2 + return lib.nan_to_num(x1 * x2 + y1 * y2 + z1 * z2, nan=0.0) def xy_z_xy_theta(lib, x1, y1, z1, x2, y2, theta2): @@ -277,7 +277,7 @@ def rhophi_z_xy_eta(lib, rho1, phi1, z1, x2, y2, eta2): # specialized def rhophi_z_rhophi_z(lib, rho1, phi1, z1, rho2, phi2, z2): - return rho1 * rho2 * lib.cos(phi1 - phi2) + z1 * z2 + return lib.nan_to_num(rho1 * rho2 * lib.cos(phi1 - phi2) + z1 * z2, nan=0.0) def rhophi_z_rhophi_theta(lib, rho1, phi1, z1, rho2, phi2, theta2): @@ -336,8 +336,9 @@ def rhophi_theta_rhophi_z(lib, rho1, phi1, theta1, rho2, phi2, z2): # specialized def rhophi_theta_rhophi_theta(lib, rho1, phi1, theta1, rho2, phi2, theta2): - return ( - rho1 * rho2 * (lib.cos(phi1 - phi2) + 1 / (lib.tan(theta1) * lib.tan(theta2))) + return lib.nan_to_num( + rho1 * rho2 * (lib.cos(phi1 - phi2) + 1 / (lib.tan(theta1) * lib.tan(theta2))), + nan=0, ) @@ -407,7 +408,9 @@ def rhophi_eta_rhophi_eta(lib, rho1, phi1, eta1, rho2, phi2, eta2): expmeta2 = lib.exp(-eta2) invtantheta1 = 0.5 * (1 - expmeta1 ** 2) / expmeta1 invtantheta2 = 0.5 * (1 - expmeta2 ** 2) / expmeta2 - return rho1 * rho2 * (lib.cos(phi1 - phi2) + invtantheta1 * invtantheta2) + return lib.nan_to_num( + rho1 * rho2 * (lib.cos(phi1 - phi2) + invtantheta1 * invtantheta2), nan=0.0 + ) dispatch_map = { diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index 12178a5e..a6b0e976 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -48,7 +48,7 @@ def rhophi_z(lib, rho, phi, z): def rhophi_theta(lib, rho, phi, theta): - return -lib.log(lib.tan(0.5 * theta)) + return lib.nan_to_num(-lib.log(lib.tan(0.5 * theta)), nan=0.0) def rhophi_eta(lib, rho, phi, eta): diff --git a/tests/root/test_Polar2DVector.py b/tests/root/test_Polar2DVector.py index 2ba87d69..391409ae 100644 --- a/tests/root/test_Polar2DVector.py +++ b/tests/root/test_Polar2DVector.py @@ -82,7 +82,9 @@ def test_Dot(constructor, coordinates): vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().dot( getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() - ) + ), + 1.0e-6, + 1.0e-6, ) @@ -91,18 +93,10 @@ def test_Dot(constructor, coordinates): constructor1=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), constructor2=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), ) def test_fuzz_Dot(constructor1, constructor2, coordinates): @@ -137,10 +131,6 @@ def test_Mag2(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_Mag2(constructor, coordinates): assert ROOT.Math.Polar2DVector(*constructor).Mag2() == pytest.approx( @@ -166,10 +156,6 @@ def test_Mag(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_Mag(constructor, coordinates): assert ROOT.Math.sqrt( @@ -193,10 +179,6 @@ def test_Phi(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_Phi(constructor, coordinates): assert ROOT.Math.Polar2DVector(*constructor).Phi() == pytest.approx( @@ -230,13 +212,8 @@ def test_Rotate(constructor, angle, coordinates): constructor=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), - angle=st.floats(min_value=-10e7, max_value=10e7) - | st.integers(min_value=-10e7, max_value=10e7), + angle=st.floats(min_value=-10e7, max_value=10e7), ) def test_fuzz_Rotate(constructor, angle, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor) @@ -271,10 +248,6 @@ def test_Unit(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_Unit(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() @@ -298,10 +271,6 @@ def test_X_and_Y(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_X_and_Y(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor) @@ -337,18 +306,10 @@ def test_add(constructor, coordinates): constructor1=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), constructor2=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), ) def test_fuzz_add(constructor1, constructor2, coordinates): @@ -398,18 +359,10 @@ def test_sub(constructor, coordinates): constructor1=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), constructor2=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), ) def test_fuzz_sub(constructor1, constructor2, coordinates): @@ -448,10 +401,6 @@ def test_neg(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_neg(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).__neg__() @@ -478,13 +427,8 @@ def test_mul(constructor, scalar, coordinates): constructor=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), - scalar=st.floats(min_value=-10e7, max_value=10e7) - | st.integers(min_value=-10e7, max_value=10e7), + scalar=st.floats(min_value=-10e7, max_value=10e7), ) def test_fuzz_mul(constructor, scalar, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).__mul__(scalar) @@ -513,13 +457,8 @@ def test_truediv(constructor, scalar, coordinates): constructor=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), ), - scalar=st.floats(min_value=-10e7, max_value=10e7) - | st.integers(min_value=-10e7, max_value=10e7), + scalar=st.floats(min_value=-10e7, max_value=10e7), ) def test_fuzz_truediv(constructor, scalar, coordinates): # FIXME: @@ -552,10 +491,6 @@ def test_eq(constructor, coordinates): st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) ) def test_fuzz_eq(constructor, coordinates): ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__( diff --git a/tests/root/test_Polar3DVector.py b/tests/root/test_Polar3DVector.py index e735c6ea..15b56bbe 100644 --- a/tests/root/test_Polar3DVector.py +++ b/tests/root/test_Polar3DVector.py @@ -13,16 +13,18 @@ ROOT = pytest.importorskip("ROOT") # ROOT.Math.Polar3DVector constructor arguments to get all the weird cases. +# "rho", "theta", "phi" +# Phi is restricted to be in the range [-PI,PI) constructor = [ - (0, 0, 0), - (0, 10, 0), - (0, -10, 0), - (1, 0, 0), - (1, 10, 0), - (1, -10, 0), - (1.0, 2.5, 2.0), - (1, 2.5, 2.0), - (1, -2.5, 2.0), + (0.0, 0.0, 0.0), + # (0.0, 10.0, 0.0), + # (0.0, -10.0, 0.0), + (1.0, 0.0, 0.0), + # (1.0, 10.0, 0.0), + # (1.0, -10.0, 0.0), + # (1.0, 2.5, 2.0), + # (1.0, 2.5, 2.0), + # (1.0, -2.5, 2.0), ] # Coordinate conversion methods to apply to the VectorObject2D. @@ -97,22 +99,12 @@ def test_Dot(constructor, coordinates): constructor1=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), + st.floats(min_value=-ROOT.Math.Pi(), max_value=ROOT.Math.Pi()), ), constructor2=st.tuples( st.floats(min_value=-10e7, max_value=10e7), st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), + st.floats(min_value=-ROOT.Math.Pi(), max_value=ROOT.Math.Pi()), ), ) def test_fuzz_Dot(constructor1, constructor2, coordinates): @@ -144,21 +136,21 @@ def test_Cross(constructor, coordinates): )() ) assert ( - ref_vec.Rho() + ref_vec.X() == pytest.approx( - vec.rho, + vec.x, 1.0e-6, 1.0e-6, ) - and ref_vec.Theta() + and ref_vec.Y() == pytest.approx( - vec.theta, + vec.y, 1.0e-6, 1.0e-6, ) - and ref_vec.Phi() + and ref_vec.Z() == pytest.approx( - vec.phi, + vec.z, 1.0e-6, 1.0e-6, ) @@ -200,21 +192,21 @@ def test_fuzz_Cross(constructor1, constructor2, coordinates): )() ) assert ( - ref_vec.Rho() + ref_vec.X() == pytest.approx( - vec.rho, + vec.x, 1.0e-6, 1.0e-6, ) - and ref_vec.Theta() + and ref_vec.Y() == pytest.approx( - vec.theta, + vec.y, 1.0e-6, 1.0e-6, ) - and ref_vec.Phi() + and ref_vec.Z() == pytest.approx( - vec.phi, + vec.z, 1.0e-6, 1.0e-6, ) @@ -234,9 +226,7 @@ def test_Mag2(constructor, coordinates): # Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_R(constructor, coordinates): - assert ROOT.Math.sqrt( - ROOT.Math.Polar3DVector(*constructor).Mag2() - ) == pytest.approx( + assert ROOT.Math.Polar3DVector(*constructor).R() == pytest.approx( getattr( vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates, @@ -329,6 +319,26 @@ def test_RotateZ(constructor, angle, coordinates): vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() res_vec = vec.rotateZ(angle) + assert ( + ref_vec.R() + == pytest.approx( + vec.rho, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Theta() + == pytest.approx( + vec.theta, + 1.0e-6, + 1.0e-6, + ) + and ref_vec.Phi() + == pytest.approx( + vec.phi, + 1.0e-6, + 1.0e-6, + ) + ) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) assert ref_vec.Z() == pytest.approx(res_vec.z) @@ -342,7 +352,7 @@ def test_RotateAxes(constructor, angle, coordinates): )() # FIXME: rotate_axis assert ( - ref_vec.Rho() + ref_vec.R() == pytest.approx( vec.rho, 1.0e-6, From 9b00d56a051ce5d4926bd7d398b27aa274abb53a Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 21 Dec 2021 16:28:02 +0100 Subject: [PATCH 08/10] remove hypothesis --- tests/root/test_EulerAngles.py | 5 - tests/root/test_Polar2DVector.py | 258 ------------------------------- 2 files changed, 263 deletions(-) diff --git a/tests/root/test_EulerAngles.py b/tests/root/test_EulerAngles.py index 3a7d917c..81afd761 100644 --- a/tests/root/test_EulerAngles.py +++ b/tests/root/test_EulerAngles.py @@ -5,11 +5,6 @@ import pytest -# from hypothesis import given -# from hypothesis import strategies as st - -# import vector - # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") diff --git a/tests/root/test_Polar2DVector.py b/tests/root/test_Polar2DVector.py index 391409ae..8e75b1d0 100644 --- a/tests/root/test_Polar2DVector.py +++ b/tests/root/test_Polar2DVector.py @@ -5,8 +5,6 @@ import numpy as np import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -88,33 +86,6 @@ def test_Dot(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.Polar2DVector(*constructor1).Dot( - ROOT.Math.Polar2DVector(*constructor2) - ) == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates - )() - ), - rel=1e-6, - abs=1e-6, - ) - - # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): @@ -125,21 +96,6 @@ def test_Mag2(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_Mag2(constructor, coordinates): - assert ROOT.Math.Polar2DVector(*constructor).Mag2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().rho2 - ) - - # Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag(constructor, coordinates): @@ -150,21 +106,6 @@ def test_Mag(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_Mag(constructor, coordinates): - assert ROOT.Math.sqrt( - ROOT.Math.Polar2DVector(*constructor).Mag2() - ) == pytest.approx( - getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)().rho - ) - - # Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Phi(constructor, coordinates): @@ -173,19 +114,6 @@ def test_Phi(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_Phi(constructor, coordinates): - assert ROOT.Math.Polar2DVector(*constructor).Phi() == pytest.approx( - getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)().phi - ) - - # Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Rotate(constructor, angle, coordinates): @@ -207,31 +135,6 @@ def test_Rotate(constructor, angle, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), - angle=st.floats(min_value=-10e7, max_value=10e7), -) -def test_fuzz_Rotate(constructor, angle, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor) - ref_vec.Rotate(angle) - vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() - res_vec = vec.rotateZ(angle) - assert ref_vec.R() == pytest.approx( - res_vec.rho, - 1.0e-6, - 1.0e-6, - ) - assert ref_vec.Phi() == pytest.approx( - res_vec.phi, - 1.0e-6, - 1.0e-6, - ) - - # Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Unit(constructor, coordinates): @@ -242,21 +145,6 @@ def test_Unit(constructor, coordinates): assert ref_vec.Phi() == pytest.approx(res_vec().phi) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_Unit(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).Unit() - vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() - res_vec = vec.unit - assert ref_vec.R() == pytest.approx(res_vec().rho) - assert ref_vec.Phi() == pytest.approx(res_vec().phi) - - # Run a test that compares ROOT's 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_X_and_Y(constructor, coordinates): @@ -265,19 +153,6 @@ def test_X_and_Y(constructor, coordinates): assert ref_vec.X() == pytest.approx(vec.x) and ref_vec.Y() == pytest.approx(vec.y) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_X_and_Y(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor) - vec = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() - assert ref_vec.X() == pytest.approx(vec.x) and ref_vec.Y() == pytest.approx(vec.y) - - # Run a test that compares ROOT's '__add__' with vector's 'add' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_add(constructor, coordinates): @@ -301,38 +176,6 @@ def test_add(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_add(constructor1, constructor2, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor1).__add__( - ROOT.Math.Polar2DVector(*constructor2) - ) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates - )().add( - getattr(vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)() - ) - assert ref_vec.R() == pytest.approx( - vec.rho, - 1.0e-6, - 1.0e-6, - ) - assert ref_vec.Phi() == pytest.approx( - vec.phi, - 1.0e-6, - 1.0e-6, - ) - - # Run a test that compares ROOT's '__sub__' with vector's 'subtract' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_sub(constructor, coordinates): @@ -354,36 +197,6 @@ def test_sub(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_sub(constructor1, constructor2, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor1).__sub__( - ROOT.Math.Polar2DVector(*constructor2) - ) - vec1 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates)() - vec2 = getattr(vector.obj(**dict(zip(["rho", "phi"], constructor2))), coordinates)() - res_vec = vec1.subtract(vec2) - assert ref_vec.R() == pytest.approx( - res_vec.rho, - 1.0e-6, - 1.0e-6, - ) - assert ref_vec.Phi() == pytest.approx( - res_vec.phi, - 1.0e-6, - 1.0e-6, - ) - - # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_neg(constructor, coordinates): @@ -395,22 +208,6 @@ def test_neg(constructor, coordinates): assert ref_vec.Phi() == pytest.approx(vec().phi) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_neg(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__neg__() - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().__neg__ - assert ref_vec.R() == pytest.approx(vec().rho) - assert ref_vec.Phi() == pytest.approx(vec().phi) - - # Run a test that compares ROOT's '__mul__' with vector's 'mul' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_mul(constructor, scalar, coordinates): @@ -422,23 +219,6 @@ def test_mul(constructor, scalar, coordinates): assert ref_vec.Phi() == pytest.approx(vec.phi) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), - scalar=st.floats(min_value=-10e7, max_value=10e7), -) -def test_fuzz_mul(constructor, scalar, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__mul__(scalar) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().__mul__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) - assert ref_vec.Phi() == pytest.approx(vec.phi) - - # Run a test that compares ROOT's '__truediv__' with vector's '__truediv__' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_truediv(constructor, scalar, coordinates): @@ -452,25 +232,6 @@ def test_truediv(constructor, scalar, coordinates): assert ref_vec.Phi() == pytest.approx(vec.phi) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ), - scalar=st.floats(min_value=-10e7, max_value=10e7), -) -def test_fuzz_truediv(constructor, scalar, coordinates): - # FIXME: - if scalar != 0: - ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().__truediv__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) - assert ref_vec.Phi() == pytest.approx(vec.phi) - - # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_eq(constructor, coordinates): @@ -483,22 +244,3 @@ def test_eq(constructor, coordinates): getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() ) assert ref_vec == vec - - -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_eq(constructor, coordinates): - ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__( - ROOT.Math.Polar2DVector(*constructor) - ) - vec = getattr( - vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates - )().equal( - getattr(vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates)() - ) - assert ref_vec == vec From c5c64261920e00b2aa3f40778b65b33343ac4530 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 21 Dec 2021 16:40:12 +0100 Subject: [PATCH 09/10] remove hypothesis --- tests/root/test_Polar3DVector.py | 86 -------------------- tests/root/test_PtEtaPhiEVector.py | 89 -------------------- tests/root/test_PtEtaPhiMVector.py | 89 -------------------- tests/root/test_PxPyPzEVector.py | 87 -------------------- tests/root/test_PxPyPzMVector.py | 87 -------------------- tests/root/test_Quaternion.py | 5 -- tests/root/test_RhoEtaPhiVector.py | 36 --------- tests/root/test_RhoZPhiVector.py | 35 -------- tests/root/test_XYVector.py | 33 -------- tests/root/test_XYZVector.py | 95 ---------------------- tests/root/test_vector.py | 126 +++++++++++++---------------- 11 files changed, 56 insertions(+), 712 deletions(-) diff --git a/tests/root/test_Polar3DVector.py b/tests/root/test_Polar3DVector.py index 15b56bbe..55164461 100644 --- a/tests/root/test_Polar3DVector.py +++ b/tests/root/test_Polar3DVector.py @@ -4,8 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -94,34 +92,6 @@ def test_Dot(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-ROOT.Math.Pi(), max_value=ROOT.Math.Pi()), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-ROOT.Math.Pi(), max_value=ROOT.Math.Pi()), - ), -) -def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.Polar3DVector(*constructor1).Dot( - ROOT.Math.Polar3DVector(*constructor2) - ) == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor2))), - coordinates, - )() - ) - ) - - # Run a test that compares ROOT's 'Cross()' with vector's 'cross' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Cross(constructor, coordinates): @@ -157,62 +127,6 @@ def test_Cross(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Cross(constructor1, constructor2, coordinates): - ref_vec = ROOT.Math.Polar3DVector(*constructor1).Cross( - ROOT.Math.Polar3DVector(*constructor2) - ) - vec = getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor1))), coordinates - )().cross( - getattr( - vector.obj(**dict(zip(["rho", "theta", "phi"], constructor2))), coordinates - )() - ) - assert ( - ref_vec.X() - == pytest.approx( - vec.x, - 1.0e-6, - 1.0e-6, - ) - and ref_vec.Y() - == pytest.approx( - vec.y, - 1.0e-6, - 1.0e-6, - ) - and ref_vec.Z() - == pytest.approx( - vec.z, - 1.0e-6, - 1.0e-6, - ) - ) - - # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): diff --git a/tests/root/test_PtEtaPhiEVector.py b/tests/root/test_PtEtaPhiEVector.py index f3b276b4..2ab6fc34 100644 --- a/tests/root/test_PtEtaPhiEVector.py +++ b/tests/root/test_PtEtaPhiEVector.py @@ -4,8 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -100,45 +98,6 @@ def test_Dot(constructor, coordinates): ) == pytest.approx(v1.dot(v2)) -# Run the same test within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fizz_Dot(constructor1, constructor2, coordinates): - v1 = getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor1))), coordinates - )() - v2 = getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor2))), coordinates - )() - assert ROOT.Math.PtEtaPhiEVector(*constructor1).Dot( - ROOT.Math.PtEtaPhiEVector(*constructor2) - ) == pytest.approx(v1.dot(v2)) - - # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @pytest.mark.parametrize("constructor", constructor) @@ -151,30 +110,6 @@ def test_M2(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M2(constructor, coordinates): - assert ROOT.Math.PtEtaPhiEVector(*constructor).M2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), - coordinates, - )().tau2 - ) - - # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) @@ -187,30 +122,6 @@ def test_M(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M(constructor, coordinates): - assert ROOT.Math.PtEtaPhiEVector(*constructor).M() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), - coordinates, - )().tau - ) - - # Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt2(constructor, coordinates): diff --git a/tests/root/test_PtEtaPhiMVector.py b/tests/root/test_PtEtaPhiMVector.py index 6d70713c..7468a3b9 100644 --- a/tests/root/test_PtEtaPhiMVector.py +++ b/tests/root/test_PtEtaPhiMVector.py @@ -4,8 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -100,45 +98,6 @@ def test_Dot(constructor, coordinates): ) == pytest.approx(v1.dot(v2)) -# Run the same test within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fizz_Dot(constructor1, constructor2, coordinates): - v1 = getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor1))), coordinates - )() - v2 = getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor2))), coordinates - )() - assert ROOT.Math.PtEtaPhiMVector(*constructor1).Dot( - ROOT.Math.PtEtaPhiMVector(*constructor2) - ) == pytest.approx(v1.dot(v2)) - - # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @pytest.mark.parametrize("constructor", constructor) @@ -151,30 +110,6 @@ def test_M2(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M2(constructor, coordinates): - assert ROOT.Math.PtEtaPhiMVector(*constructor).M2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), - coordinates, - )().tau2 - ) - - # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) @@ -187,30 +122,6 @@ def test_M(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M(constructor, coordinates): - assert ROOT.Math.PtEtaPhiMVector(*constructor).M() == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), - coordinates, - )().tau - ) - - # Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt2(constructor, coordinates): diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index f22659bf..77cf7a08 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -4,8 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -100,45 +98,6 @@ def test_Dot(constructor, coordinates): ) == pytest.approx(v1.dot(v2)) -# Run the same test within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fizz_Dot(constructor1, constructor2, coordinates): - v1 = getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates - )() - v2 = getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates - )() - assert ROOT.Math.PxPyPzEVector(*constructor1).Dot( - ROOT.Math.PxPyPzEVector(*constructor2) - ) == pytest.approx(v1.dot(v2)) - - # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @pytest.mark.parametrize("constructor", constructor) @@ -150,29 +109,6 @@ def test_M2(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau2 - ) - - # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) @@ -184,29 +120,6 @@ def test_M(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau - ) - - # Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt2(constructor, coordinates): diff --git a/tests/root/test_PxPyPzMVector.py b/tests/root/test_PxPyPzMVector.py index ee545c06..e0fd3dbc 100644 --- a/tests/root/test_PxPyPzMVector.py +++ b/tests/root/test_PxPyPzMVector.py @@ -4,8 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -100,45 +98,6 @@ def test_Dot(constructor, coordinates): ) == pytest.approx(v1.dot(v2)) -# Run the same test within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fizz_Dot(constructor1, constructor2, coordinates): - v1 = getattr( - vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor1))), coordinates - )() - v2 = getattr( - vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor2))), coordinates - )() - assert ROOT.Math.PxPyPzMVector(*constructor1).Dot( - ROOT.Math.PxPyPzMVector(*constructor2) - ) == pytest.approx(v1.dot(v2)) - - # Run a test that compares ROOT's 'M2()' with vector's 'tau2' for all cases. # Mass2 is our tau2 (or mass2 if it's a momentum vector and has kinematic synonyms) @pytest.mark.parametrize("constructor", constructor) @@ -150,29 +109,6 @@ def test_M2(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M2(constructor, coordinates): - assert ROOT.Math.PxPyPzMVector(*constructor).M2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates - )().tau2 - ) - - # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @pytest.mark.parametrize("constructor", constructor) @@ -184,29 +120,6 @@ def test_M(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ) -) -def test_fuzz_M(constructor, coordinates): - assert ROOT.Math.PxPyPzMVector(*constructor).M() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates - )().tau - ) - - # Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mt2(constructor, coordinates): diff --git a/tests/root/test_Quaternion.py b/tests/root/test_Quaternion.py index 23f16730..721826d4 100644 --- a/tests/root/test_Quaternion.py +++ b/tests/root/test_Quaternion.py @@ -5,11 +5,6 @@ import pytest -# from hypothesis import given -# from hypothesis import strategies as st -# -# import vector - # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") diff --git a/tests/root/test_RhoEtaPhiVector.py b/tests/root/test_RhoEtaPhiVector.py index 59640cb0..0db4c971 100644 --- a/tests/root/test_RhoEtaPhiVector.py +++ b/tests/root/test_RhoEtaPhiVector.py @@ -5,8 +5,6 @@ import numpy as np import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -91,40 +89,6 @@ def test_Dot(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.RhoEtaPhiVector(*constructor1).Dot( - ROOT.Math.RhoEtaPhiVector(*constructor2) - ) == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["rho", "eta", "phi"], constructor2))), - coordinates, - )() - ) - ) - - # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): diff --git a/tests/root/test_RhoZPhiVector.py b/tests/root/test_RhoZPhiVector.py index 7b491285..39241e64 100644 --- a/tests/root/test_RhoZPhiVector.py +++ b/tests/root/test_RhoZPhiVector.py @@ -5,8 +5,6 @@ import numpy as np import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -91,39 +89,6 @@ def test_Dot(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.RhoZPhiVector(*constructor1).Dot( - ROOT.Math.RhoZPhiVector(*constructor2) - ) == pytest.approx( - getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["rho", "z", "phi"], constructor2))), coordinates - )() - ) - ) - - # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): diff --git a/tests/root/test_XYVector.py b/tests/root/test_XYVector.py index 65af09e1..4a75f3cb 100644 --- a/tests/root/test_XYVector.py +++ b/tests/root/test_XYVector.py @@ -5,8 +5,6 @@ import numpy as np import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -85,37 +83,6 @@ def test_Dot(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.XYVector(*constructor1).Dot( - ROOT.Math.XYVector(*constructor2) - ) == pytest.approx( - getattr(vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates)().dot( - getattr(vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates)() - ), - 1.0e-6, - 1.0e-6, - ) - - # Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): diff --git a/tests/root/test_XYZVector.py b/tests/root/test_XYZVector.py index 0532384e..44750dff 100644 --- a/tests/root/test_XYZVector.py +++ b/tests/root/test_XYZVector.py @@ -4,8 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -90,45 +88,6 @@ def test_Dot(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.XYZVector(*constructor1).Dot( - ROOT.Math.XYZVector(*constructor2) - ) == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates - )() - ), - 1.0e-6, - 1.0e-6, - ) - - # Run a test that compares ROOT's 'Cross()' with vector's 'cross' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Cross(constructor, coordinates): @@ -160,60 +119,6 @@ def test_Cross(constructor, coordinates): ) -# Run the same tests within hypothesis -@given( - constructor1=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), - constructor2=st.tuples( - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - st.floats(min_value=-10e7, max_value=10e7), - ) - | st.tuples( - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - st.integers(min_value=-10e7, max_value=10e7), - ), -) -def test_fuzz_Cross(constructor1, constructor2, coordinates): - ref_vec = ROOT.Math.XYZVector(*constructor1).Cross( - ROOT.Math.XYZVector(*constructor2) - ) - vec = getattr( - vector.obj(**dict(zip(["x", "y", "z"], constructor1))), coordinates - )().cross( - getattr(vector.obj(**dict(zip(["x", "y", "z"], constructor2))), coordinates)() - ) - assert ( - ref_vec.X() - == pytest.approx( - vec.x, - 1.0e-6, - 1.0e-6, - ) - and ref_vec.Y() - == pytest.approx( - vec.y, - 1.0e-6, - 1.0e-6, - ) - and ref_vec.Z() - == pytest.approx( - vec.z, - 1.0e-6, - 1.0e-6, - ) - ) - - # Run a test that compares ROOT's 'Mag2()' with vector's 'mag2' for all cases. @pytest.mark.parametrize("constructor", constructor) def test_Mag2(constructor, coordinates): diff --git a/tests/root/test_vector.py b/tests/root/test_vector.py index 6a20169f..5716e1b9 100644 --- a/tests/root/test_vector.py +++ b/tests/root/test_vector.py @@ -2,8 +2,6 @@ # and is provided under the Creative Commons Zero public domain dedication. import pytest -from hypothesis import given -from hypothesis import strategies as st import vector @@ -53,73 +51,61 @@ def test_M2(constructor, coordinates): ) -@given( - constructor=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()) -) -def test_fuzz_M2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau2 - ) - - -@given( - azimuthal=st.tuples(st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers()) -) -def test_fuzz_MomentumObject2D(azimuthal): - vector.MomentumObject2D(azimuthal=azimuthal) - - -@given( - azimuthal=st.tuples(st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers()), - longitudinal=st.floats() | st.integers(), -) -def test_fuzz_MomentumObject3D(azimuthal, longitudinal): - vector.MomentumObject3D(azimuthal=azimuthal, longitudinal=longitudinal) - - -@given( - azimuthal=st.tuples(st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers()), - longitudinal=st.floats() | st.integers(), - temporal=st.floats() | st.integers(), -) -def test_fuzz_MomentumObject4D(azimuthal, longitudinal, temporal): - vector.MomentumObject4D( - azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal - ) - - -@given( - azimuthal=st.tuples(st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers()) -) -def test_fuzz_VectorObject2D(azimuthal): - vector.VectorObject2D(azimuthal=azimuthal) - - -@given( - azimuthal=st.tuples(st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers()), - longitudinal=st.floats() | st.integers(), -) -def test_fuzz_VectorObject3D(azimuthal, longitudinal): - vector.VectorObject3D(azimuthal=azimuthal, longitudinal=longitudinal) - - -@given( - azimuthal=st.tuples(st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers()), - longitudinal=st.floats() | st.integers(), - temporal=st.floats() | st.integers(), -) -def test_fuzz_VectorObject4D(azimuthal, longitudinal, temporal): - vector.VectorObject4D( - azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal - ) +# @given( +# azimuthal=st.tuples(st.floats(), st.floats()) +# | st.tuples(st.integers(), st.integers()) +# ) +# def test_fuzz_MomentumObject2D(azimuthal): +# vector.MomentumObject2D(azimuthal=azimuthal) +# +# +# @given( +# azimuthal=st.tuples(st.floats(), st.floats()) +# | st.tuples(st.integers(), st.integers()), +# longitudinal=st.floats() | st.integers(), +# ) +# def test_fuzz_MomentumObject3D(azimuthal, longitudinal): +# vector.MomentumObject3D(azimuthal=azimuthal, longitudinal=longitudinal) +# +# +# @given( +# azimuthal=st.tuples(st.floats(), st.floats()) +# | st.tuples(st.integers(), st.integers()), +# longitudinal=st.floats() | st.integers(), +# temporal=st.floats() | st.integers(), +# ) +# def test_fuzz_MomentumObject4D(azimuthal, longitudinal, temporal): +# vector.MomentumObject4D( +# azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal +# ) +# +# +# @given( +# azimuthal=st.tuples(st.floats(), st.floats()) +# | st.tuples(st.integers(), st.integers()) +# ) +# def test_fuzz_VectorObject2D(azimuthal): +# vector.VectorObject2D(azimuthal=azimuthal) +# +# +# @given( +# azimuthal=st.tuples(st.floats(), st.floats()) +# | st.tuples(st.integers(), st.integers()), +# longitudinal=st.floats() | st.integers(), +# ) +# def test_fuzz_VectorObject3D(azimuthal, longitudinal): +# vector.VectorObject3D(azimuthal=azimuthal, longitudinal=longitudinal) +# +# +# @given( +# azimuthal=st.tuples(st.floats(), st.floats()) +# | st.tuples(st.integers(), st.integers()), +# longitudinal=st.floats() | st.integers(), +# temporal=st.floats() | st.integers(), +# ) +# def test_fuzz_VectorObject4D(azimuthal, longitudinal, temporal): +# vector.VectorObject4D( +# azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal +# ) # assert (vector.obj(**dict(zip(["x", "y", "z", "t"], azimuthal[0], azimuthal[1], longitudinal, temporal)))).tau == 0 # pytest.approx(ROOT.Math.PxPyPzEVector(azimuthal[0], azimuthal[1], longitudinal, temporal).M()) From 59f44d9ea3ed8a99596ac49db153f541971a6a35 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Dec 2021 15:41:51 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/root/test_vector.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/root/test_vector.py b/tests/root/test_vector.py index 5716e1b9..86d8bc9b 100644 --- a/tests/root/test_vector.py +++ b/tests/root/test_vector.py @@ -107,5 +107,5 @@ def test_M2(constructor, coordinates): # vector.VectorObject4D( # azimuthal=azimuthal, longitudinal=longitudinal, temporal=temporal # ) - # assert (vector.obj(**dict(zip(["x", "y", "z", "t"], azimuthal[0], azimuthal[1], longitudinal, temporal)))).tau == 0 - # pytest.approx(ROOT.Math.PxPyPzEVector(azimuthal[0], azimuthal[1], longitudinal, temporal).M()) +# assert (vector.obj(**dict(zip(["x", "y", "z", "t"], azimuthal[0], azimuthal[1], longitudinal, temporal)))).tau == 0 +# pytest.approx(ROOT.Math.PxPyPzEVector(azimuthal[0], azimuthal[1], longitudinal, temporal).M())