From 7c54938a4867d45afedb35411045f8f329a88006 Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Wed, 17 Mar 2021 09:51:09 -0500 Subject: [PATCH 1/9] Compare vector results with ROOT's. --- .github/workflows/ci.yml | 18 ++-- tests/root/test_PxPyPzEVector.py | 164 +++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 tests/root/test_PxPyPzEVector.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e0eb24a..f7499159 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,16 +55,16 @@ jobs: - name: Test package run: python -m pytest -ra - # root: - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v2 + root: + runs-on: ubuntu-latest + 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..146ef31c --- /dev/null +++ b/tests/root/test_PxPyPzEVector.py @@ -0,0 +1,164 @@ +# 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.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, 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 + +# 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.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "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.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) +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 + ) + +# 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 '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 'Mt2()' with vector's 'mt2' for all cases. +# Mt2 same for transverse mass: it's only on momentum vectors +@pytest.mark.parametrize("constructor", constructor) +def test_Mt2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).mt2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().mt2 + ) + +# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +# Mt +@pytest.mark.parametrize("constructor", constructor) +def test_Mt(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().mt + ) + +# Run a test that compares ROOT's 'Mag2()' 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_Mag2(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 'Mag()' with vector's 'mag' for all cases. +# P is our mag (same deal) +@pytest.mark.parametrize("constructor", constructor) +def test_P2(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 'Minus()' with vector's 'mag' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Minus(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Minus() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().minus + ) From 4001ca0f8310404843c844e0ad0deddaf2238872 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Mon, 22 Mar 2021 15:42:35 +0100 Subject: [PATCH 2/9] test fixture and hypothesis --- tests/root/test_PxPyPzEVector.py | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index 146ef31c..e99e9182 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -57,7 +57,11 @@ def test_M2(constructor, coordinates): ) # Run the same tests within hypothesis +<<<<<<< HEAD @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(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) +>>>>>>> test fixture and hypothesis def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( getattr( @@ -76,7 +80,11 @@ def test_M(constructor, coordinates): ) # Run the same tests within hypothesis +<<<<<<< HEAD @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(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) +>>>>>>> test fixture and hypothesis def test_fuzz_M(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( getattr( @@ -88,6 +96,7 @@ def test_fuzz_M(constructor, coordinates): # Dot @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): +<<<<<<< HEAD v1 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() @@ -161,4 +170,28 @@ def test_Minus(constructor, coordinates): getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )().minus +======= + assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )()) + ) + +# Run the same test within hypothesis +@given(constructor1=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()), + constructor2=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) +def test_fizz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates + )()) +>>>>>>> test fixture and hypothesis ) From ebed8732ea5825337c061b7c3189eb018a8fbd10 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Mon, 22 Mar 2021 18:13:18 +0100 Subject: [PATCH 3/9] handle nan and overflow errors, add failing test cases --- tests/root/test_PxPyPzEVector.py | 88 +++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index e99e9182..557e68d0 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -16,6 +16,10 @@ (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), +###>>>>>>> handle nan and overflow errors, add failing test cases (0, 0, 0, 10), (0, 0, 0, -10), (1, 2, 3, 0), @@ -42,6 +46,9 @@ "to_rhophietatau", ] +def isNaN(num): + return num!=num + @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param @@ -63,11 +70,27 @@ 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())) >>>>>>> test fixture and hypothesis def test_fuzz_M2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( - getattr( + try: + ref_result = ROOT.Math.PxPyPzEVector(*constructor).M2() + if isNaN(ref_result): + assert isNaN(getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) + else: + assert ref_result == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) + except OverflowError: + try: + getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )().tau2 - ) + raise ValueError + except OverflowError: + return None # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @@ -86,11 +109,27 @@ def test_M(constructor, coordinates): @given(constructor=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) >>>>>>> test fixture and hypothesis 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 - ) + try: + ref_result = ROOT.Math.PxPyPzEVector(*constructor).M() + if isNaN(ref_result): + assert isNaN(getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau + ) + else: + assert ref_result == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau + ) + except OverflowError: + try: + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau + raise ValueError + except OverflowError: + return None # Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. # Dot @@ -186,6 +225,7 @@ def test_Minus(constructor, coordinates): constructor2=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) def test_fizz_Dot(constructor1, constructor2, coordinates): +<<<<<<< HEAD assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates @@ -195,3 +235,35 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): )()) >>>>>>> test fixture and hypothesis ) +======= + try: + ref_result = ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) + if isNaN(ref_result): + assert isNaN(getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates + )()) + ) + else: + assert ref_result == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates + )()) + ) + except OverflowError: + try: + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates + )()) + raise ValueError + except OverflowError: + return None +>>>>>>> handle nan and overflow errors, add failing test cases From acc12c9f948904676124648509ac52719600826e Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Wed, 24 Mar 2021 17:56:31 +0100 Subject: [PATCH 4/9] add XYVector tests for all its methods --- src/vector/_compute/lorentz/dot.py | 6 +- src/vector/_compute/spatial/mag2.py | 1 + tests/root/test_PxPyPzEVector.py | 134 +++++++++++------ tests/root/test_XYVector.py | 216 ++++++++++++++++++++++++++++ 4 files changed, 309 insertions(+), 48 deletions(-) create mode 100644 tests/root/test_XYVector.py diff --git a/src/vector/_compute/lorentz/dot.py b/src/vector/_compute/lorentz/dot.py index 24b554b4..58ac52fc 100644 --- a/src/vector/_compute/lorentz/dot.py +++ b/src/vector/_compute/lorentz/dot.py @@ -109,9 +109,9 @@ def make_conversion( to_t2 = t.rhophi_eta_tau def f(lib, coord11, coord12, coord13, coord14, coord21, coord22, coord23, coord24): - return lib.absolute( - to_t1(lib, coord11, coord12, coord13, coord14) - * to_t2(lib, coord21, coord22, coord23, coord24) + t1 = to_t1(lib, coord11, coord12, coord13, coord14) + t2 = to_t1(lib, coord21, coord22, coord23, coord24) + return (t1 * t2 ) - spatial_dot(lib, coord11, coord12, coord13, coord21, coord22, coord23) dispatch_map[ diff --git a/src/vector/_compute/spatial/mag2.py b/src/vector/_compute/spatial/mag2.py index 32f514e0..a70008cc 100644 --- a/src/vector/_compute/spatial/mag2.py +++ b/src/vector/_compute/spatial/mag2.py @@ -32,6 +32,7 @@ def xy_z(lib, x, y, z): def xy_theta(lib, x, y, theta): + # FIXME?: returns 'nan' when theta == 0.0 return (x ** 2 + y ** 2) / lib.sin(theta) ** 2 diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index 557e68d0..5280e5c0 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -16,10 +16,13 @@ (0, 0, 0, 0), # (0, 0, 1, 0), # theta == 0.0 # (0, 0, -1, 0), +##<<<<<<< HEAD (0, 0, 1, 0), (0, 0, 0, 4294967296), (0, 4294967296, 0, 0), ###>>>>>>> handle nan and overflow errors, add failing test cases +======= +##>>>>>>> add XYVector tests for all its methods (0, 0, 0, 10), (0, 0, 0, -10), (1, 2, 3, 0), @@ -46,9 +49,6 @@ "to_rhophietatau", ] -def isNaN(num): - return num!=num - @pytest.fixture(scope="module", params=coordinate_list) def coordinates(request): return request.param @@ -65,32 +65,20 @@ def test_M2(constructor, coordinates): # Run the same tests within hypothesis <<<<<<< HEAD +<<<<<<< HEAD @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(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) >>>>>>> test fixture and 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))) +>>>>>>> add XYVector tests for all its methods def test_fuzz_M2(constructor, coordinates): - try: - ref_result = ROOT.Math.PxPyPzEVector(*constructor).M2() - if isNaN(ref_result): - assert isNaN(getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau2 - ) - else: - assert ref_result == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau2 - ) - except OverflowError: - try: - getattr( + assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )().tau2 - raise ValueError - except OverflowError: - return None + ) # Run a test that compares ROOT's 'M()' with vector's 'tau' for all cases. # Mass is tau (or mass) @@ -104,44 +92,36 @@ def test_M(constructor, coordinates): # Run the same tests within hypothesis <<<<<<< HEAD +<<<<<<< HEAD @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(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) >>>>>>> test fixture and 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))) +>>>>>>> add XYVector tests for all its methods def test_fuzz_M(constructor, coordinates): - try: - ref_result = ROOT.Math.PxPyPzEVector(*constructor).M() - if isNaN(ref_result): - assert isNaN(getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau - ) - else: - assert ref_result == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau - ) - except OverflowError: - try: - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau - raise ValueError - except OverflowError: - return None + 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 'Dot()' with vector's 'dot' for all cases. # Dot @pytest.mark.parametrize("constructor", constructor) def test_Dot(constructor, coordinates): <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> add XYVector tests for all its methods v1 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() v2 = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() +<<<<<<< HEAD assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( v1.dot(v2) ) @@ -210,15 +190,49 @@ def test_Minus(constructor, coordinates): vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )().minus ======= +======= +>>>>>>> add XYVector tests for all its methods 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 'Mt2()' with vector's 'mt2' for all cases. +# Mt2 same for transverse mass: it's only on momentum vectors +@pytest.mark.parametrize("constructor", constructor) +def test_Mt2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).mt2() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().dot( + )().mt2 + ) + +# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +# Mt +@pytest.mark.parametrize("constructor", constructor) +def test_Mt(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )()) + )().mt ) +<<<<<<< HEAD # Run the same test within hypothesis @given(constructor1=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()), @@ -267,3 +281,33 @@ def test_fizz_Dot(constructor1, constructor2, coordinates): except OverflowError: return None >>>>>>> handle nan and overflow errors, add failing test cases +======= +# Run a test that compares ROOT's 'Mag2()' 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_Mag2(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 'Mag()' with vector's 'mag' for all cases. +# P is our mag (same deal) +@pytest.mark.parametrize("constructor", constructor) +def test_P2(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 'Minus()' with vector's 'mag' for all cases. +@pytest.mark.parametrize("constructor", constructor) +def test_Minus(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Minus() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().minus + ) +>>>>>>> add XYVector tests for all its methods diff --git a/tests/root/test_XYVector.py b/tests/root/test_XYVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_XYVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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 From 8a8d581b81cb01075653183b67924f4bc476d9db Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Fri, 26 Mar 2021 18:47:25 +0100 Subject: [PATCH 5/9] rotate in Z --- tests/root/test_XYVector.py | 123 +++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 44 deletions(-) diff --git a/tests/root/test_XYVector.py b/tests/root/test_XYVector.py index 1c7132cd..dace28ce 100644 --- a/tests/root/test_XYVector.py +++ b/tests/root/test_XYVector.py @@ -6,6 +6,8 @@ import pytest from hypothesis import given, strategies as st +import numpy as np + import vector # If ROOT is not available, skip these tests. @@ -74,6 +76,7 @@ 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)) @@ -92,6 +95,7 @@ def test_fuzz_Dot(constructor1, 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): @@ -101,15 +105,17 @@ def test_Mag2(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( - getattr( + np.sqrt(getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho + )().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): @@ -119,6 +125,7 @@ def test_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): @@ -127,9 +134,14 @@ def test_Rotate(constructor, angle, coordinates): vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) + res_vec = vec.rotateZ(angle) + assert ref_vec.R() == pytest.approx(np.sqrt(vec.x*vec.x + vec.y*vec.y)) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + ref_angle = (ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) + ROOT.Math.Pi()) % (2 * ROOT.Math.Pi()) - ROOT.Math.Pi() + res_angle = (vec.lib.arctan2(res_vec.x, res_vec.y) + vec.lib.pi) % (2 * vec.lib.pi) - vec.lib.pi + assert ref_angle == pytest.approx(res_angle) + # Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. @pytest.mark.parametrize("constructor", constructor) @@ -137,9 +149,11 @@ def test_Unit(constructor, coordinates): ref_vec = ROOT.Math.XYVector(*constructor).Unit() vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) + )() + res_vec = vec.unit + assert ref_vec.R() == pytest.approx(np.sqrt(res_vec().x*res_vec().x + res_vec().y*res_vec().y)) + assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(res_vec().lib.arctan2(res_vec().x, 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) @@ -158,49 +172,70 @@ def test_add(constructor, coordinates): 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) and - ref_vec.Y() == pytest.approx(vec.y)) + assert ref_vec.R() == pytest.approx(vec.rho) + assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, 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)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], 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 '__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) and - 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( + vec1 = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - 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( + )() + vec2 = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + )() + res_vec = vec1.subtract(vec2) + assert ref_vec.R() == pytest.approx(res_vec.rho) + assert ref_vec.X() == pytest.approx(res_vec.x) + assert ref_vec.Y() == pytest.approx(res_vec.y) + assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(res_vec.lib.arctan2(res_vec.x, res_vec.y)) + +# def __neg__(self): +# > raise AssertionError("FIXME") +# E AssertionError: FIXME +# +# # 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.R() == pytest.approx(np.sqrt(vec().x*vec().x + vec().y*vec().y)) +# assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, 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.R() == pytest.approx(vec.rho) +# assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) +# def __mul__(self, other): +# > raise AssertionError("FIXME") +# E AssertionError: FIXME +# +# src/vector/backends/object_.py:85: AssertionError + +# # 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.R() == pytest.approx(vec.rho) +# assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) +# +# def __truediv__(self, other): +# > raise AssertionError("FIXME") +# E AssertionError: FIXME +# +# src/vector/backends/object_.py:100: AssertionError # Run a test that compares ROOT's '__eq__' with vector's 'isclose' for all cases. From 381ef1034416533cdd24e664c076dbbfcc5bb54f Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Tue, 13 Apr 2021 15:15:32 +0200 Subject: [PATCH 6/9] stash local files --- tests/root/test_EulerAngles.py | 216 ++++++++++++++++++++++++++++ tests/root/test_Polar2DVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_Polar3DVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_PtEtaPhiEVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_PtEtaPhiMVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_PxPyPzEVector-my.py | 182 +++++++++++++++++++++++ tests/root/test_PxPyPzMVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_Quaternion.py | 216 ++++++++++++++++++++++++++++ tests/root/test_RhoEtaPhiVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_RhoZPhiVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_XYVector.py | 56 ++++---- tests/root/test_XYZVector.py | 216 ++++++++++++++++++++++++++++ tests/root/test_vector.py | 92 ++++++++++++ 13 files changed, 2462 insertions(+), 28 deletions(-) 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_PxPyPzEVector-my.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_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..1c7132cd --- /dev/null +++ b/tests/root/test_EulerAngles.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_Polar2DVector.py b/tests/root/test_Polar2DVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_Polar2DVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_Polar3DVector.py b/tests/root/test_Polar3DVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_Polar3DVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_PtEtaPhiEVector.py b/tests/root/test_PtEtaPhiEVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_PtEtaPhiEVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_PtEtaPhiMVector.py b/tests/root/test_PtEtaPhiMVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_PtEtaPhiMVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_PxPyPzEVector-my.py b/tests/root/test_PxPyPzEVector-my.py new file mode 100644 index 00000000..3bec73e2 --- /dev/null +++ b/tests/root/test_PxPyPzEVector-my.py @@ -0,0 +1,182 @@ +# 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.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. +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 + +# 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.PxPyPzEVector(*constructor).M2() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau2 + ) + +# Run the same tests within hypothesis +@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 + ) + +# 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.PxPyPzEVector(*constructor).M() == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().tau + ) + +# Run the same tests within hypothesis +@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_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 'Dot()' with vector's 'dot' for all cases. +# Dot +@pytest.mark.parametrize("constructor", constructor) +def test_Dot(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )()) + ) + +# Run the same test within hypothesis +@given(constructor1=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()), + constructor2=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) + | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) +def test_fizz_Dot(constructor1, constructor2, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates + )().dot( + getattr( + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates + )()) + ) + +# # Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. +# # Mt2 same for transverse mass: it's only on momentum vectors +# @pytest.mark.parametrize("constructor", constructor) +# def test_Mt2(constructor, coordinates): +# assert ROOT.Math.PxPyPzEVector(*constructor).Mt2() == pytest.approx( +# getattr( +# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates +# )().mt2 +# ) +# +# # Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. +# # Mt +# @pytest.mark.parametrize("constructor", constructor) +# def test_Mt(constructor, coordinates): +# assert ROOT.Math.PxPyPzEVector(*constructor).Mt() == pytest.approx( +# getattr( +# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates +# )().mt +# ) + +# # Run a test that compares ROOT's 'Mag2()' 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_Mag2(constructor, coordinates): +# assert ROOT.Math.PxPyPzEVector(*constructor).Mag2() == pytest.approx( +# getattr( +# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates +# )().mag2 +# ) + +# # Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. +# # P is our mag (same deal) +# @pytest.mark.parametrize("constructor", constructor) +# def test_Mag(constructor, coordinates): +# assert ROOT.Math.PxPyPzEVector(*constructor).Mag() == pytest.approx( +# getattr( +# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates +# )().mag +# ) + +# 'Minus()' +# 'Mt2()' + + # Perp2/rho2 + # Perp/rho + # Phi + # Eta + # Theta + + # Rapidity + # Beta (scalar) + # Gamma (scalar) + # BoostToCM is our beta3 (it doesn't boost: it returns a velocity vector for which c=1) + # ColinearRapidity (we don't have an equivalent, but perhaps we should) + # Et2 to have a method for transverse energy, you have to construct a vector.obj with momentum coordinates + # Et + + # isLightlike/is_lightlike + # isSpacelike/is_spacelike + # isTimelike/is_timelike + # ROOT's rotateX, rotateY, rotateZ, and rotate_axis are in its VectorUtil namespace (see below) + # so are the boosts + # Unit + # X, Y, Z, T + # __add__ (addition by a vector) + # __sub__ (subtraction by a vector) + # __neg__ (unary negation of a vector) + # __mul__ (multiplication by a scalar) + # __truediv__ (division by a scalar) + # __eq__ (vector equality), but since you're going through different coordinate systems, use isclose +# etc. diff --git a/tests/root/test_PxPyPzMVector.py b/tests/root/test_PxPyPzMVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_PxPyPzMVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_Quaternion.py b/tests/root/test_Quaternion.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_Quaternion.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_RhoEtaPhiVector.py b/tests/root/test_RhoEtaPhiVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_RhoEtaPhiVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_RhoZPhiVector.py b/tests/root/test_RhoZPhiVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_RhoZPhiVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_XYVector.py b/tests/root/test_XYVector.py index dace28ce..03c01cd9 100644 --- a/tests/root/test_XYVector.py +++ b/tests/root/test_XYVector.py @@ -192,44 +192,44 @@ def test_sub(constructor, coordinates): assert ref_vec.Y() == pytest.approx(res_vec.y) assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(res_vec.lib.arctan2(res_vec.x, 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.R() == pytest.approx(np.sqrt(vec().x*vec().x + vec().y*vec().y)) + assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) # def __neg__(self): # > raise AssertionError("FIXME") # E AssertionError: FIXME # -# # 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.R() == pytest.approx(np.sqrt(vec().x*vec().x + vec().y*vec().y)) -# assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, 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.R() == pytest.approx(vec.rho) -# assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, 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.R() == pytest.approx(vec.rho) + assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) # def __mul__(self, other): # > raise AssertionError("FIXME") # E AssertionError: FIXME # # src/vector/backends/object_.py:85: AssertionError -# # 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.R() == pytest.approx(vec.rho) -# assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, 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.R() == pytest.approx(vec.rho) + assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) # # def __truediv__(self, other): # > raise AssertionError("FIXME") diff --git a/tests/root/test_XYZVector.py b/tests/root/test_XYZVector.py new file mode 100644 index 00000000..1c7132cd --- /dev/null +++ b/tests/root/test_XYZVector.py @@ -0,0 +1,216 @@ +# 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, 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( + getattr( + vector.obj(**dict(zip(["x", "y"], 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.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 + )() + vec.rotateZ(angle) + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and + ref_vec.__getattribute__("y")() == pytest.approx(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 + )().unit + assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and + ref_vec.__getattribute__("y")() == pytest.approx(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) and + 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)) + vec = getattr( + vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + )().subtract(getattr( + vector.obj(**dict(zip(["x", "y"], 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 '__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) and + 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) and + 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) and + 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_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 d494b2d6744e7f033fd90c999eaa026c64d34484 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Thu, 15 Apr 2021 12:39:19 +0200 Subject: [PATCH 7/9] rebase --- tests/root/test_EulerAngles.py | 193 ++---------- tests/root/test_Polar2DVector.py | 339 ++++++++++++++++---- tests/root/test_Polar3DVector.py | 192 +++++++----- tests/root/test_PtEtaPhiEVector.py | 421 +++++++++++++++++++------ tests/root/test_PtEtaPhiMVector.py | 421 +++++++++++++++++++------ tests/root/test_PxPyPzEVector-my.py | 182 ----------- tests/root/test_PxPyPzEVector.py | 460 ++++++++++++++++++---------- tests/root/test_PxPyPzMVector.py | 418 +++++++++++++++++++------ tests/root/test_Quaternion.py | 191 ++---------- tests/root/test_RhoEtaPhiVector.py | 191 +++++++----- tests/root/test_RhoZPhiVector.py | 191 +++++++----- tests/root/test_XYVector.py | 44 +-- tests/root/test_XYZVector.py | 191 +++++++----- 13 files changed, 2102 insertions(+), 1332 deletions(-) delete mode 100644 tests/root/test_PxPyPzEVector-my.py diff --git a/tests/root/test_EulerAngles.py b/tests/root/test_EulerAngles.py index 1c7132cd..4b59357a 100644 --- a/tests/root/test_EulerAngles.py +++ b/tests/root/test_EulerAngles.py @@ -11,23 +11,38 @@ # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") -# ROOT.Math.XYVector constructor arguments to get all the weird cases. +# 4D 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), + (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 VectorObject2D. +# Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ - "to_xy", - "to_rhophi", + "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) @@ -62,155 +77,3 @@ def angle(request): @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( - getattr( - vector.obj(**dict(zip(["x", "y"], 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.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 - )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(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 - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(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) and - 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)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], 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 '__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) and - 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) and - 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) and - 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_Polar2DVector.py b/tests/root/test_Polar2DVector.py index 1c7132cd..91e4eee8 100644 --- a/tests/root/test_Polar2DVector.py +++ b/tests/root/test_Polar2DVector.py @@ -6,22 +6,25 @@ 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. +# 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), - (0, -10), (1, 0), + (10, 0), (1, 10), - (1, -10), + (10., 10), (1., 2.5), (1, 2.5), - (1, -2.5), + (1, 6.283185307179586), ] # Coordinate conversion methods to apply to the VectorObject2D. @@ -37,14 +40,14 @@ def coordinates(request): angle_list = [ 0, 0.0, - 0.7853981633974483, - -0.7853981633974483, - 1.5707963267948966, - -1.5707963267948966, - 3.141592653589793, - -3.141592653589793, - 6.283185307179586, - -6.283185307179586, + 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) @@ -66,14 +69,15 @@ def scalar(request): # 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( + assert ROOT.Math.Polar2DVector(*constructor).Dot(ROOT.Math.Polar2DVector(*constructor)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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)) @@ -84,133 +88,336 @@ def test_Dot(constructor, coordinates): | 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( + assert ROOT.Math.Polar2DVector(*constructor1).Dot(ROOT.Math.Polar2DVector(*constructor2)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor1))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates - )()) + 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.XYVector(*constructor).Mag2() == pytest.approx( + 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(["x", "y"], constructor))), coordinates + 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_R(constructor, coordinates): - assert ROOT.Math.XYVector(*constructor).R() == pytest.approx( +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(["x", "y"], constructor))), coordinates + 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.XYVector(*constructor).Phi() == pytest.approx( + 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(["x", "y"], constructor))), coordinates + 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.XYVector(*constructor) + ref_vec = ROOT.Math.Polar2DVector(*constructor) ref_vec.Rotate(angle) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), 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)) + + +# 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 )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) + 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.XYVector(*constructor).Unit() + 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(["x", "y"], constructor))), coordinates - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) + 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(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )() - assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYVector(*constructor).Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.Polar2DVector(*constructor).__add__(ROOT.Math.Polar2DVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.Polar2DVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + 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(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.Polar2DVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.Polar2DVector(*constructor).__eq__(ROOT.Math.Polar2DVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "phi"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 index 1c7132cd..0f98249c 100644 --- a/tests/root/test_Polar3DVector.py +++ b/tests/root/test_Polar3DVector.py @@ -6,28 +6,34 @@ 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. +# ROOT.Math.Polar3DVector 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), + (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_xy", - "to_rhophi", + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", ] @pytest.fixture(scope="module", params=coordinate_list) @@ -64,16 +70,18 @@ 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.XYVector(*constructor).Dot(ROOT.Math.XYVector(*constructor)) == pytest.approx( + assert ROOT.Math.Polar3DVector(*constructor).Dot(ROOT.Math.Polar3DVector(*constructor)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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)) @@ -84,133 +92,179 @@ def test_Dot(constructor, coordinates): | 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( + assert ROOT.Math.Polar3DVector(*constructor1).Dot(ROOT.Math.Polar3DVector(*constructor2)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor1))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates + 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.XYVector(*constructor).Mag2() == pytest.approx( + assert ROOT.Math.Polar3DVector(*constructor).Mag2() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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.XYVector(*constructor).R() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho + 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.Polar3DVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Rotate(constructor, angle, coordinates): - ref_vec = ROOT.Math.XYVector(*constructor) - ref_vec.Rotate(angle) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.Polar3DVector(*constructor) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) + 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.XYVector(*constructor).Unit() + ref_vec = ROOT.Math.Polar3DVector(*constructor).Unit() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) + 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(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )() - assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYVector(*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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__add__(ROOT.Math.Polar3DVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.Polar3DVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.Polar3DVector(*constructor).__eq__(ROOT.Math.Polar3DVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "theta", "phi"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 1c7132cd..24d85b2c 100644 --- a/tests/root/test_PtEtaPhiEVector.py +++ b/tests/root/test_PtEtaPhiEVector.py @@ -11,23 +11,38 @@ # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") -# ROOT.Math.XYVector constructor arguments to get all the weird cases. +# ROOT.Math.PtEtaPhiEVector 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), + (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 VectorObject2D. +# Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ - "to_xy", - "to_rhophi", + "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) @@ -64,153 +79,369 @@ 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): - assert ROOT.Math.XYVector(*constructor).Dot(ROOT.Math.XYVector(*constructor)) == pytest.approx( + 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(["x", "y"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], 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 + )().tau ) # 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( +@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(["x", "y"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates - )()) + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates + )().mag2 ) -# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +# 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_Mag2(constructor, coordinates): - assert ROOT.Math.XYVector(*constructor).Mag2() == pytest.approx( +def test_P(constructor, coordinates): + assert ROOT.Math.PtEtaPhiEVector(*constructor).P() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], 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 )().rho2 ) -# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +# Run a test that compares ROOT's 'Perp()' 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( +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PtEtaPhiEVector(*constructor).Perp2()) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().rho ) -# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +# 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.PtEtaPhiEVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' 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 - )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) +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 'Unit()' with vector's 'unit' for all cases. +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' 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 - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) +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 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' 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)) +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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__add__(ROOT.Math.PtEtaPhiEVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiEVector(*constructor).__eq__(ROOT.Math.PtEtaPhiEVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "t"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 index 1c7132cd..0447e232 100644 --- a/tests/root/test_PtEtaPhiMVector.py +++ b/tests/root/test_PtEtaPhiMVector.py @@ -11,23 +11,38 @@ # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") -# ROOT.Math.XYVector constructor arguments to get all the weird cases. +# ROOT.Math.PtEtaPhiMVector 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), + (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 VectorObject2D. +# Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ - "to_xy", - "to_rhophi", + "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) @@ -64,153 +79,369 @@ 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): - assert ROOT.Math.XYVector(*constructor).Dot(ROOT.Math.XYVector(*constructor)) == pytest.approx( + 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(["x", "y"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], 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 + )().tau ) # 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( +@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(["x", "y"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates - )()) + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates + )().mag2 ) -# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +# 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_Mag2(constructor, coordinates): - assert ROOT.Math.XYVector(*constructor).Mag2() == pytest.approx( +def test_P(constructor, coordinates): + assert ROOT.Math.PtEtaPhiMVector(*constructor).P() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], 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 )().rho2 ) -# Run a test that compares ROOT's 'R()' with vector's 'rho' for all cases. +# Run a test that compares ROOT's 'Perp()' 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( +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PtEtaPhiMVector(*constructor).Perp2()) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().rho ) -# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +# 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.PtEtaPhiMVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' 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 - )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) +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 'Unit()' with vector's 'unit' for all cases. +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' 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 - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) +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 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' 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)) +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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__add__(ROOT.Math.PtEtaPhiMVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.PtEtaPhiMVector(*constructor).__eq__(ROOT.Math.PtEtaPhiMVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi", "tau"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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_PxPyPzEVector-my.py b/tests/root/test_PxPyPzEVector-my.py deleted file mode 100644 index 3bec73e2..00000000 --- a/tests/root/test_PxPyPzEVector-my.py +++ /dev/null @@ -1,182 +0,0 @@ -# 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.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. -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 - -# 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.PxPyPzEVector(*constructor).M2() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau2 - ) - -# Run the same tests within hypothesis -@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 - ) - -# 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.PxPyPzEVector(*constructor).M() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().tau - ) - -# Run the same tests within hypothesis -@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_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 'Dot()' with vector's 'dot' for all cases. -# Dot -@pytest.mark.parametrize("constructor", constructor) -def test_Dot(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )()) - ) - -# Run the same test within hypothesis -@given(constructor1=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()), - constructor2=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) -def test_fizz_Dot(constructor1, constructor2, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates - )()) - ) - -# # Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. -# # Mt2 same for transverse mass: it's only on momentum vectors -# @pytest.mark.parametrize("constructor", constructor) -# def test_Mt2(constructor, coordinates): -# assert ROOT.Math.PxPyPzEVector(*constructor).Mt2() == pytest.approx( -# getattr( -# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates -# )().mt2 -# ) -# -# # Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. -# # Mt -# @pytest.mark.parametrize("constructor", constructor) -# def test_Mt(constructor, coordinates): -# assert ROOT.Math.PxPyPzEVector(*constructor).Mt() == pytest.approx( -# getattr( -# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates -# )().mt -# ) - -# # Run a test that compares ROOT's 'Mag2()' 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_Mag2(constructor, coordinates): -# assert ROOT.Math.PxPyPzEVector(*constructor).Mag2() == pytest.approx( -# getattr( -# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates -# )().mag2 -# ) - -# # Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. -# # P is our mag (same deal) -# @pytest.mark.parametrize("constructor", constructor) -# def test_Mag(constructor, coordinates): -# assert ROOT.Math.PxPyPzEVector(*constructor).Mag() == pytest.approx( -# getattr( -# vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates -# )().mag -# ) - -# 'Minus()' -# 'Mt2()' - - # Perp2/rho2 - # Perp/rho - # Phi - # Eta - # Theta - - # Rapidity - # Beta (scalar) - # Gamma (scalar) - # BoostToCM is our beta3 (it doesn't boost: it returns a velocity vector for which c=1) - # ColinearRapidity (we don't have an equivalent, but perhaps we should) - # Et2 to have a method for transverse energy, you have to construct a vector.obj with momentum coordinates - # Et - - # isLightlike/is_lightlike - # isSpacelike/is_spacelike - # isTimelike/is_timelike - # ROOT's rotateX, rotateY, rotateZ, and rotate_axis are in its VectorUtil namespace (see below) - # so are the boosts - # Unit - # X, Y, Z, T - # __add__ (addition by a vector) - # __sub__ (subtraction by a vector) - # __neg__ (unary negation of a vector) - # __mul__ (multiplication by a scalar) - # __truediv__ (division by a scalar) - # __eq__ (vector equality), but since you're going through different coordinate systems, use isclose -# etc. diff --git a/tests/root/test_PxPyPzEVector.py b/tests/root/test_PxPyPzEVector.py index 5280e5c0..6d62dcbb 100644 --- a/tests/root/test_PxPyPzEVector.py +++ b/tests/root/test_PxPyPzEVector.py @@ -14,15 +14,11 @@ # 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), -##<<<<<<< HEAD + (0, 0, 1, 0), # theta == 0.0 + (0, 0, -1, 0), (0, 0, 1, 0), (0, 0, 0, 4294967296), (0, 4294967296, 0, 0), -###>>>>>>> handle nan and overflow errors, add failing test cases -======= -##>>>>>>> add XYVector tests for all its methods (0, 0, 0, 10), (0, 0, 0, -10), (1, 2, 3, 0), @@ -53,6 +49,65 @@ 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) @@ -64,15 +119,7 @@ def test_M2(constructor, coordinates): ) # Run the same tests within hypothesis -<<<<<<< HEAD -<<<<<<< HEAD -@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(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) ->>>>>>> test fixture and 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))) ->>>>>>> add XYVector tests for all its methods def test_fuzz_M2(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M2() == pytest.approx( getattr( @@ -91,15 +138,7 @@ def test_M(constructor, coordinates): ) # Run the same tests within hypothesis -<<<<<<< HEAD -<<<<<<< HEAD @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(), st.floats(), st.floats(), st.floats()) | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) ->>>>>>> test fixture and 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))) ->>>>>>> add XYVector tests for all its methods def test_fuzz_M(constructor, coordinates): assert ROOT.Math.PxPyPzEVector(*constructor).M() == pytest.approx( getattr( @@ -107,207 +146,302 @@ def test_fuzz_M(constructor, coordinates): )().tau ) -# Run a test that compares ROOT's 'Dot()' with vector's 'dot' for all cases. -# Dot + +# Run a test that compares ROOT's 'Mt2()' with vector's 'mt2' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Dot(constructor, coordinates): -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> add XYVector tests for all its methods - v1 = getattr( +def test_Mt2(constructor, coordinates): + v = getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates )() - v2 = getattr( + 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 )() -<<<<<<< HEAD - assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( - v1.dot(v2) + assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( + v.lib.sqrt(v.t*v.t - v.z*v.z) ) -# 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 '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 'Mt2()' with vector's 'mt2' for all cases. -# Mt2 same for transverse mass: it's only on momentum vectors +# 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_Mt2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).mt2() == pytest.approx( +def test_P(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).P() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mt2 + )().mag ) -# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. -# Mt +# Run a test that compares ROOT's 'Perp2()' with vector's 'rho2' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Mt(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( +def test_Perp2(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Perp2() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mt + )().rho2 ) -# Run a test that compares ROOT's 'Mag2()' 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) +# Run a test that compares ROOT's 'Perp()' with vector's 'rho' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Mag2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).P2() == pytest.approx( +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 - )().mag2 + )().rho ) -# Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. -# P is our mag (same deal) +# Run a test that compares ROOT's 'Phi()' with vector's 'phi' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_P2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).P() == pytest.approx( +def test_Phi(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Phi() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mag + )().phi ) -# Run a test that compares ROOT's 'Minus()' with vector's 'mag' for all cases. +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Minus(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).Minus() == pytest.approx( +def test_Rapidity(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Rapidity() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().minus -======= -======= ->>>>>>> add XYVector tests for all its methods - assert ROOT.Math.PxPyPzEVector(*constructor).Dot(ROOT.Math.PxPyPzEVector(*constructor)) == pytest.approx( - v1.dot(v2) + )().rapidity ) -# 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 '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 'Mt2()' with vector's 'mt2' for all cases. -# Mt2 same for transverse mass: it's only on momentum vectors +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Mt2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).mt2() == pytest.approx( +def test_Eta(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Eta() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mt2 + )().eta ) -# Run a test that compares ROOT's 'Mt()' with vector's 'mt' for all cases. -# Mt +# Run a test that compares ROOT's 'Gamma()' with vector's 'gamma' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Mt(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).mt() == pytest.approx( +def test_Gamma(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Gamma() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mt + )().gamma ) -<<<<<<< HEAD -# Run the same test within hypothesis -@given(constructor1=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers(), st.integers(), st.integers()), - constructor2=st.tuples(st.floats(), st.floats(), st.floats(), st.floats()) - | st.tuples(st.integers(), st.integers(), st.integers(), st.integers())) -def test_fizz_Dot(constructor1, constructor2, coordinates): -<<<<<<< HEAD - assert ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) == pytest.approx( +# 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"], constructor1))), coordinates - )().dot( + 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"], constructor2))), coordinates - )()) ->>>>>>> test fixture and hypothesis + vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates + )().is_spacelike() ) -======= - try: - ref_result = ROOT.Math.PxPyPzEVector(*constructor1).Dot(ROOT.Math.PxPyPzEVector(*constructor2)) - if isNaN(ref_result): - assert isNaN(getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates - )()) - ) - else: - assert ref_result == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates - )()) - ) - except OverflowError: - try: - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor1))), coordinates - )().dot( - getattr( - vector.obj(**dict(zip(["x", "y", "z", "t"], constructor2))), coordinates - )()) - raise ValueError - except OverflowError: - return None ->>>>>>> handle nan and overflow errors, add failing test cases -======= -# Run a test that compares ROOT's 'Mag2()' 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) + +# Run a test that compares ROOT's 'isTimelike()' with vector's 'is_timelike' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Mag2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).P2() == pytest.approx( +def test_isTimelike(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).isTimelike() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mag2 + )().is_timelike() ) -# Run a test that compares ROOT's 'Mag()' with vector's 'mag' for all cases. -# P is our mag (same deal) +# Run a test that compares ROOT's 'Theta()' with vector's 'theta' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_P2(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).P() == pytest.approx( +def test_Theta(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).Theta() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().mag + )().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 'Minus()' with vector's 'mag' for all cases. +# Run a test that compares ROOT's 'T()' with vector's 't' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Minus(constructor, coordinates): - assert ROOT.Math.PxPyPzEVector(*constructor).Minus() == pytest.approx( +def test_T(constructor, coordinates): + assert ROOT.Math.PxPyPzEVector(*constructor).T() == pytest.approx( getattr( vector.obj(**dict(zip(["x", "y", "z", "t"], constructor))), coordinates - )().minus + )().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 + )() ) ->>>>>>> add XYVector tests for all its methods + 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) diff --git a/tests/root/test_PxPyPzMVector.py b/tests/root/test_PxPyPzMVector.py index 1c7132cd..f41d362c 100644 --- a/tests/root/test_PxPyPzMVector.py +++ b/tests/root/test_PxPyPzMVector.py @@ -11,23 +11,38 @@ # If ROOT is not available, skip these tests. ROOT = pytest.importorskip("ROOT") -# ROOT.Math.XYVector constructor arguments to get all the weird cases. +# ROOT.Math.PxPyPzMVector 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), + (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 VectorObject2D. +# Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ - "to_xy", - "to_rhophi", + "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) @@ -64,153 +79,366 @@ 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): - assert ROOT.Math.XYVector(*constructor).Dot(ROOT.Math.XYVector(*constructor)) == pytest.approx( + 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"], constructor))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )()) + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().tau2 ) # 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( +@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"], constructor1))), coordinates - )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates - )()) + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates + )().mag2 ) -# Run a test that compares ROOT's 'Mag2()' with vector's 'rho2' for all cases. +# 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_Mag2(constructor, coordinates): - assert ROOT.Math.XYVector(*constructor).Mag2() == pytest.approx( +def test_P(constructor, coordinates): + assert ROOT.Math.PxPyPzMVector(*constructor).P() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 'R()' with vector's 'rho' for all cases. +# Run a test that compares ROOT's 'Perp()' 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( +def test_Perp(constructor, coordinates): + assert ROOT.Math.sqrt(ROOT.Math.PxPyPzMVector(*constructor).Perp2()) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().rho ) -# Run a test that compares ROOT's 'Phi()' with vector's 'rho' for all cases. +# 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.PxPyPzMVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. +# Run a test that compares ROOT's 'Rapidity()' with vector's 'rapidity' 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 - )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) +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 'Unit()' with vector's 'unit' for all cases. +# Run a test that compares ROOT's 'Beta()' with vector's 'beta' 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 - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) +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 'X()' and 'Y()' with vector's 'x' and 'y' for all cases. +# Run a test that compares ROOT's 'Eta()' with vector's 'eta' 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)) +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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__add__(ROOT.Math.PxPyPzMVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.PxPyPzMVector(*constructor).__eq__(ROOT.Math.PxPyPzMVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z", "tau"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 index 1c7132cd..53493eca 100644 --- a/tests/root/test_Quaternion.py +++ b/tests/root/test_Quaternion.py @@ -13,21 +13,36 @@ # 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), + (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 VectorObject2D. +# Coordinate conversion methods to apply to the VectorObject4D. coordinate_list = [ - "to_xy", - "to_rhophi", + "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) @@ -62,155 +77,3 @@ def angle(request): @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( - getattr( - vector.obj(**dict(zip(["x", "y"], 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.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 - )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(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 - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(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) and - 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)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], 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 '__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) and - 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) and - 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) and - 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_RhoEtaPhiVector.py b/tests/root/test_RhoEtaPhiVector.py index 1c7132cd..3966b0c9 100644 --- a/tests/root/test_RhoEtaPhiVector.py +++ b/tests/root/test_RhoEtaPhiVector.py @@ -6,28 +6,34 @@ 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. +# ROOT.Math.RhoEtaPhiVector 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), + (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_xy", - "to_rhophi", + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", ] @pytest.fixture(scope="module", params=coordinate_list) @@ -66,14 +72,15 @@ def scalar(request): # 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( + assert ROOT.Math.RhoEtaPhiVector(*constructor).Dot(ROOT.Math.RhoEtaPhiVector(*constructor)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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)) @@ -84,133 +91,179 @@ def test_Dot(constructor, coordinates): | 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( + assert ROOT.Math.RhoEtaPhiVector(*constructor1).Dot(ROOT.Math.RhoEtaPhiVector(*constructor2)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor1))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates + 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.XYVector(*constructor).Mag2() == pytest.approx( + assert ROOT.Math.RhoEtaPhiVector(*constructor).Mag2() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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.XYVector(*constructor).R() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho + 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.RhoEtaPhiVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Rotate(constructor, angle, coordinates): - ref_vec = ROOT.Math.XYVector(*constructor) - ref_vec.Rotate(angle) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.RhoEtaPhiVector(*constructor) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) + 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.XYVector(*constructor).Unit() + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).Unit() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) + 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(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )() - assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYVector(*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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__add__(ROOT.Math.RhoEtaPhiVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.RhoEtaPhiVector(*constructor).__eq__(ROOT.Math.RhoEtaPhiVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "eta", "phi"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 1c7132cd..c772cb55 100644 --- a/tests/root/test_RhoZPhiVector.py +++ b/tests/root/test_RhoZPhiVector.py @@ -6,28 +6,34 @@ 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. +# ROOT.Math.RhoZPhiVector 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), + (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_xy", - "to_rhophi", + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", ] @pytest.fixture(scope="module", params=coordinate_list) @@ -66,14 +72,15 @@ def scalar(request): # 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( + assert ROOT.Math.RhoZPhiVector(*constructor).Dot(ROOT.Math.RhoZPhiVector(*constructor)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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)) @@ -84,133 +91,179 @@ def test_Dot(constructor, coordinates): | 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( + assert ROOT.Math.RhoZPhiVector(*constructor1).Dot(ROOT.Math.RhoZPhiVector(*constructor2)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor1))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates + 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.XYVector(*constructor).Mag2() == pytest.approx( + assert ROOT.Math.RhoZPhiVector(*constructor).Mag2() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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.XYVector(*constructor).R() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho + 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.RhoZPhiVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Rotate(constructor, angle, coordinates): - ref_vec = ROOT.Math.XYVector(*constructor) - ref_vec.Rotate(angle) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.RhoZPhiVector(*constructor) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) + 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.XYVector(*constructor).Unit() + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).Unit() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) + 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(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )() - assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYVector(*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.XYVector(*constructor).__add__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__add__(ROOT.Math.RhoZPhiVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.RhoZPhiVector(*constructor).__eq__(ROOT.Math.RhoZPhiVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["rho", "z", "phi"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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 03c01cd9..a5114856 100644 --- a/tests/root/test_XYVector.py +++ b/tests/root/test_XYVector.py @@ -135,12 +135,8 @@ def test_Rotate(constructor, angle, coordinates): vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )() res_vec = vec.rotateZ(angle) - assert ref_vec.R() == pytest.approx(np.sqrt(vec.x*vec.x + vec.y*vec.y)) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) - ref_angle = (ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) + ROOT.Math.Pi()) % (2 * ROOT.Math.Pi()) - ROOT.Math.Pi() - res_angle = (vec.lib.arctan2(res_vec.x, res_vec.y) + vec.lib.pi) % (2 * vec.lib.pi) - vec.lib.pi - assert ref_angle == pytest.approx(res_angle) # Run a test that compares ROOT's 'Unit()' with vector's 'unit' for all cases. @@ -151,8 +147,8 @@ def test_Unit(constructor, coordinates): vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )() res_vec = vec.unit - assert ref_vec.R() == pytest.approx(np.sqrt(res_vec().x*res_vec().x + res_vec().y*res_vec().y)) - assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(res_vec().lib.arctan2(res_vec().x, res_vec().y)) + 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. @@ -172,8 +168,8 @@ def test_add(constructor, coordinates): vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )().add(getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert ref_vec.R() == pytest.approx(vec.rho) - assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) + 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. @@ -187,10 +183,9 @@ def test_sub(constructor, coordinates): vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )() res_vec = vec1.subtract(vec2) - assert ref_vec.R() == pytest.approx(res_vec.rho) assert ref_vec.X() == pytest.approx(res_vec.x) assert ref_vec.Y() == pytest.approx(res_vec.y) - assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(res_vec.lib.arctan2(res_vec.x, res_vec.y)) + # Run a test that compares ROOT's '__neg__' with vector's '__neg__' for all cases. @pytest.mark.parametrize("constructor", constructor) @@ -199,12 +194,9 @@ def test_neg(constructor, coordinates): vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )().__neg__ - assert ref_vec.R() == pytest.approx(np.sqrt(vec().x*vec().x + vec().y*vec().y)) - assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) -# def __neg__(self): -# > raise AssertionError("FIXME") -# E AssertionError: FIXME -# + 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) @@ -213,13 +205,9 @@ def test_mul(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )().__mul__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) - assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) -# def __mul__(self, other): -# > raise AssertionError("FIXME") -# E AssertionError: FIXME -# -# src/vector/backends/object_.py:85: AssertionError + 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) @@ -228,14 +216,8 @@ def test_truediv(constructor, scalar, coordinates): vec = getattr( vector.obj(**dict(zip(["x", "y"], constructor))), coordinates )().__truediv__(scalar) - assert ref_vec.R() == pytest.approx(vec.rho) - assert ROOT.Math.atan2(ref_vec.X(), ref_vec.Y()) == pytest.approx(vec.lib.arctan2(vec.x, vec.y)) -# -# def __truediv__(self, other): -# > raise AssertionError("FIXME") -# E AssertionError: FIXME -# -# src/vector/backends/object_.py:100: AssertionError + 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 1c7132cd..66aa8612 100644 --- a/tests/root/test_XYZVector.py +++ b/tests/root/test_XYZVector.py @@ -6,28 +6,34 @@ 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. +# ROOT.Math.XYZVector 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), + (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_xy", - "to_rhophi", + "to_xyz", + "to_rhophieta", + "to_rhophitheta", + "to_rhophiz", + "to_xyeta", + "to_xytheta", ] @pytest.fixture(scope="module", params=coordinate_list) @@ -66,14 +72,15 @@ def scalar(request): # 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( + assert ROOT.Math.XYZVector(*constructor).Dot(ROOT.Math.XYZVector(*constructor)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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)) @@ -84,133 +91,179 @@ def test_Dot(constructor, coordinates): | 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( + assert ROOT.Math.XYZVector(*constructor1).Dot(ROOT.Math.XYZVector(*constructor2)) == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor1))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor1))), coordinates )().dot(getattr( - vector.obj(**dict(zip(["x", "y"], constructor2))), coordinates + 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.XYVector(*constructor).Mag2() == pytest.approx( + assert ROOT.Math.XYZVector(*constructor).Mag2() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + 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.XYVector(*constructor).R() == pytest.approx( - getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().rho + 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.XYVector(*constructor).Phi() == pytest.approx( + assert ROOT.Math.XYZVector(*constructor).Phi() == pytest.approx( getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().phi ) -# Run a test that compares ROOT's 'Rotate()' with vector's 'rotateZ' for all cases. + +# Run a test that compares ROOT's 'RotateX()' with vector's 'rotateX' for all cases. @pytest.mark.parametrize("constructor", constructor) -def test_Rotate(constructor, angle, coordinates): - ref_vec = ROOT.Math.XYVector(*constructor) - ref_vec.Rotate(angle) +def test_RotateX(constructor, angle, coordinates): + ref_vec = ROOT.Math.RotationX(angle)*ROOT.Math.XYZVector(*constructor) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )() - vec.rotateZ(angle) - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec.x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec.y)) + 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.XYVector(*constructor).Unit() + ref_vec = ROOT.Math.XYZVector(*constructor).Unit() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().unit - assert (ref_vec.__getattribute__("x")() == pytest.approx(vec().x) and - ref_vec.__getattribute__("y")() == pytest.approx(vec().y)) + 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"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )() - assert (ROOT.Math.XYVector(*constructor).X() == pytest.approx(vec.x) and - ROOT.Math.XYVector(*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)) # 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)) + ref_vec = ROOT.Math.XYZVector(*constructor).__add__(ROOT.Math.XYZVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().add(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__sub__(ROOT.Math.XYVector(*constructor)) - vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates - )().subtract(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates)()) - assert (ref_vec.X() == pytest.approx(vec.x) and - ref_vec.Y() == pytest.approx(vec.y)) + 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.XYVector(*constructor).__neg__() + ref_vec = ROOT.Math.XYZVector(*constructor).__neg__() vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().__neg__ - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__mul__(scalar) + ref_vec = ROOT.Math.XYZVector(*constructor).__mul__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().__mul__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__truediv__(scalar) + ref_vec = ROOT.Math.XYZVector(*constructor).__truediv__(scalar) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().__truediv__(scalar) - assert (ref_vec.X() == pytest.approx(vec().x) and - ref_vec.Y() == pytest.approx(vec().y)) + 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.XYVector(*constructor).__eq__(ROOT.Math.XYVector(*constructor)) + ref_vec = ROOT.Math.XYZVector(*constructor).__eq__(ROOT.Math.XYZVector(*constructor)) vec = getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )().isclose(getattr( - vector.obj(**dict(zip(["x", "y"], constructor))), coordinates + vector.obj(**dict(zip(["x", "y", "z"], constructor))), coordinates )() ) assert ref_vec == vec From 2c2e497a32a83809a7f91778a1ec5702cc11a2b2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 Apr 2021 11:04:04 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/vector/_compute/lorentz/dot.py | 5 +- tests/root/test_EulerAngles.py | 14 +- tests/root/test_Polar2DVector.py | 464 ++++++++++++++++++----------- 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 | 14 +- 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 | 55 +++- 14 files changed, 1300 insertions(+), 591 deletions(-) diff --git a/src/vector/_compute/lorentz/dot.py b/src/vector/_compute/lorentz/dot.py index 58ac52fc..d5001ef4 100644 --- a/src/vector/_compute/lorentz/dot.py +++ b/src/vector/_compute/lorentz/dot.py @@ -111,8 +111,9 @@ def make_conversion( def f(lib, coord11, coord12, coord13, coord14, coord21, coord22, coord23, coord24): t1 = to_t1(lib, coord11, coord12, coord13, coord14) t2 = to_t1(lib, coord21, coord22, coord23, coord24) - return (t1 * t2 - ) - spatial_dot(lib, coord11, coord12, coord13, coord21, coord22, coord23) + return (t1 * t2) - spatial_dot( + lib, coord11, coord12, coord13, coord21, coord22, coord23 + ) dispatch_map[ azimuthal1, longitudinal1, temporal1, azimuthal2, longitudinal2, temporal2 diff --git a/tests/root/test_EulerAngles.py b/tests/root/test_EulerAngles.py index 4b59357a..4dd0e666 100644 --- a/tests/root/test_EulerAngles.py +++ b/tests/root/test_EulerAngles.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 @@ # 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 +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,6 +79,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..27c7b8d3 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))) +@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)) + 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..6dbe90a4 100644 --- a/tests/root/test_Quaternion.py +++ b/tests/root/test_Quaternion.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.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 +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,6 +79,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..4a14c80b 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) -@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) -@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( 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) -@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( 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 4d65aca1ebafd19da93490022706285b6158705b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 15 Apr 2021 10:36:58 -0400 Subject: [PATCH 9/9] tests: add hypothesis --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index eb40b550..2eb02665 100644 --- a/setup.py +++ b/setup.py @@ -10,6 +10,7 @@ "awkward": ["awkward>=1.2.0"], "test": [ "pytest>=4.6", + "hypothesis>=6", ], "test_extras": [ "uncompyle6",