|
| 1 | +import typing |
1 | 2 | import warnings |
2 | 3 | from collections.abc import Callable |
3 | 4 | from unittest import mock |
|
17 | 18 | Gaussian, |
18 | 19 | Linear, |
19 | 20 | Lorentzian, |
| 21 | + MuonMomentum, |
20 | 22 | NegativeTrapezoid, |
21 | 23 | Polynomial, |
22 | 24 | SlitScan, |
@@ -823,3 +825,132 @@ def test_guess_given_flat_data(self): |
823 | 825 | outp = NegativeTrapezoid.guess()(x, y) |
824 | 826 | # check that with flat data gradient guess is 0 |
825 | 827 | assert outp["gradient"] == pytest.approx(0.0, rel=1e-2) |
| 828 | + |
| 829 | + |
| 830 | +class TestMuonMomentum: |
| 831 | + values: typing.ClassVar[dict[str, float]] = { |
| 832 | + "x0": 21.89, |
| 833 | + "w": 0.301, |
| 834 | + "r": 110, |
| 835 | + "b": 3.37, |
| 836 | + "p": 1.82, |
| 837 | + } |
| 838 | + x: typing.ClassVar[npt.NDArray[np.float64]] = np.linspace(20.5, 23, 15, dtype=np.float64) |
| 839 | + |
| 840 | + out: typing.ClassVar[npt.NDArray[np.float64]] = MuonMomentum.model().func( |
| 841 | + x=x, |
| 842 | + x0=values["x0"], |
| 843 | + w=values["w"], |
| 844 | + r=values["r"], |
| 845 | + b=values["b"], |
| 846 | + p=values["p"], |
| 847 | + ) |
| 848 | + |
| 849 | + class TestMuonMomentumModel: |
| 850 | + def test_muon_momentum_model_w(self): |
| 851 | + out2 = MuonMomentum.model().func( |
| 852 | + x=TestMuonMomentum.x, |
| 853 | + x0=TestMuonMomentum.values["x0"], |
| 854 | + w=TestMuonMomentum.values["w"] + 1, |
| 855 | + r=TestMuonMomentum.values["r"], |
| 856 | + b=TestMuonMomentum.values["b"], |
| 857 | + p=TestMuonMomentum.values["p"], |
| 858 | + ) |
| 859 | + |
| 860 | + assert np.mean(TestMuonMomentum.out) < np.mean(out2) |
| 861 | + |
| 862 | + def test_muon_momentum_model_b(self): |
| 863 | + out2 = MuonMomentum.model().func( |
| 864 | + x=TestMuonMomentum.x, |
| 865 | + x0=TestMuonMomentum.values["x0"], |
| 866 | + w=TestMuonMomentum.values["w"], |
| 867 | + r=TestMuonMomentum.values["r"], |
| 868 | + b=TestMuonMomentum.values["b"] + 1, |
| 869 | + p=TestMuonMomentum.values["p"], |
| 870 | + ) |
| 871 | + |
| 872 | + assert np.min(TestMuonMomentum.out) < np.min(out2) |
| 873 | + |
| 874 | + def test_muon_momentum_model_r(self): |
| 875 | + out2 = MuonMomentum.model().func( |
| 876 | + x=TestMuonMomentum.x, |
| 877 | + x0=TestMuonMomentum.values["x0"], |
| 878 | + w=TestMuonMomentum.values["w"], |
| 879 | + r=TestMuonMomentum.values["r"] + 10, |
| 880 | + b=TestMuonMomentum.values["b"], |
| 881 | + p=TestMuonMomentum.values["p"], |
| 882 | + ) |
| 883 | + |
| 884 | + assert np.max(TestMuonMomentum.out) - np.min(TestMuonMomentum.out) < np.max( |
| 885 | + out2 |
| 886 | + ) - np.min(out2) |
| 887 | + |
| 888 | + def test_muon_momentum_model_x0(self): |
| 889 | + out2 = MuonMomentum.model().func( |
| 890 | + x=TestMuonMomentum.x, |
| 891 | + x0=TestMuonMomentum.values["x0"] + 1, |
| 892 | + w=TestMuonMomentum.values["w"], |
| 893 | + r=TestMuonMomentum.values["r"], |
| 894 | + b=TestMuonMomentum.values["b"], |
| 895 | + p=TestMuonMomentum.values["p"], |
| 896 | + ) |
| 897 | + |
| 898 | + assert ( |
| 899 | + TestMuonMomentum.x[np.argmax(TestMuonMomentum.out)] |
| 900 | + < TestMuonMomentum.x[np.argmax(out2)] |
| 901 | + ) |
| 902 | + assert ( |
| 903 | + TestMuonMomentum.x[np.argmin(TestMuonMomentum.out)] |
| 904 | + < TestMuonMomentum.x[np.argmin(out2)] |
| 905 | + ) |
| 906 | + |
| 907 | + class TestMuonMomentumGuess: |
| 908 | + def test_muon_momentum_guess_b(self): |
| 909 | + x = np.array([0, 1, 2], dtype=np.float64) |
| 910 | + y = np.array([20, 20, 10], dtype=np.float64) |
| 911 | + |
| 912 | + out = MuonMomentum.guess()(x, y) |
| 913 | + |
| 914 | + assert out["b"].value is not None |
| 915 | + assert np.min(y) == out["b"].value |
| 916 | + |
| 917 | + def test_muon_momentum_guess_r(self): |
| 918 | + x = np.array([0, 1, 2], dtype=np.float64) |
| 919 | + y = np.array([20, 20, 10], dtype=np.float64) |
| 920 | + |
| 921 | + out = MuonMomentum.guess()(x, y) |
| 922 | + |
| 923 | + assert out["r"].value is not None |
| 924 | + assert np.max(y) - np.min(y) == out["r"].value |
| 925 | + |
| 926 | + def test_muon_momentum_guess_w(self): |
| 927 | + x = np.array([0, 1, 2], dtype=np.float64) |
| 928 | + y = np.array([20, 20, 10], dtype=np.float64) |
| 929 | + x1 = np.array([0, 1, 1.5], dtype=np.float64) |
| 930 | + |
| 931 | + out = MuonMomentum.guess()(x, y) |
| 932 | + out1 = MuonMomentum.guess()(x1, y) |
| 933 | + |
| 934 | + assert out["w"].value is not None |
| 935 | + assert out1["w"].value is not None |
| 936 | + assert out["w"].value > out1["w"].value |
| 937 | + |
| 938 | + def test_muon_momentum_guess_x0(self): |
| 939 | + x = np.array([0, 1, 2], dtype=np.float64) |
| 940 | + y = np.array([20, 20, 10], dtype=np.float64) |
| 941 | + x1 = np.array([3, 4, 5], dtype=np.float64) |
| 942 | + |
| 943 | + out = MuonMomentum.guess()(x, y) |
| 944 | + out1 = MuonMomentum.guess()(x1, y) |
| 945 | + |
| 946 | + assert out["x0"].value is not None |
| 947 | + assert out1["x0"].value is not None |
| 948 | + assert out1["x0"].value > out["x0"].value |
| 949 | + |
| 950 | + def test_muon_momentum_guess_x0_noslope(self): |
| 951 | + x = np.array([0, 1, 2], dtype=np.float64) |
| 952 | + y = np.array([20, 20, 20], dtype=np.float64) |
| 953 | + |
| 954 | + out = MuonMomentum.guess()(x, y) |
| 955 | + |
| 956 | + assert out["x0"].value == x[-1] |
0 commit comments