diff --git a/pyirf/tests/test_utils.py b/pyirf/tests/test_utils.py index 50d348446..64cbf84b1 100644 --- a/pyirf/tests/test_utils.py +++ b/pyirf/tests/test_utils.py @@ -1,7 +1,8 @@ import numpy as np import astropy.units as u -from astropy.table import QTable +from astropy.table import QTable, Table import pytest +from pyirf.exceptions import MissingColumns, WrongColumnUnit def test_is_scalar(): @@ -31,9 +32,7 @@ def test_cone_solid_angle(): def test_check_table(): - from pyirf.exceptions import MissingColumns, WrongColumnUnit from pyirf.utils import check_table - from astropy.table import Table t = QTable({'bar': [0, 1, 2] * u.TeV}) @@ -54,3 +53,55 @@ def test_check_table(): t = Table({'bar': [0, 1, 2]}) with pytest.raises(WrongColumnUnit): check_table(t, required_units={'bar': u.cm}) + +def test_calculate_theta(): + from pyirf.utils import calculate_theta + + true_az = true_alt = u.Quantity([1.0], u.deg) + t = QTable({'reco_alt': true_alt, 'reco_az': true_az}) + + assert u.isclose( + calculate_theta( + events=t, + assumed_source_az=true_az, + assumed_source_alt=true_alt, + ), + 0.0 * u.deg, + ) + + t = Table({'reco_alt': [1.0], 'reco_az': [1.0]}) + with pytest.raises(WrongColumnUnit): + calculate_theta(t, true_az, true_alt) + +def test_calculate_source_fov_offset(): + from pyirf.utils import calculate_source_fov_offset + + a = u.Quantity([1.0], u.deg) + t = QTable({ + 'pointing_az': a, + 'pointing_alt': a, + 'true_az': a, + 'true_alt': a, + }) + + assert u.isclose(calculate_source_fov_offset(t), 0.0 * u.deg) + +def test_check_histograms(): + from pyirf.binning import create_histogram_table + from pyirf.utils import check_histograms + + events1 = QTable({ + 'reco_energy': [1, 1, 10, 100, 100, 100] * u.TeV, + }) + events2 = QTable({ + 'reco_energy': [100, 100, 100] * u.TeV, + }) + bins = [0.5, 5, 50, 500] * u.TeV + + hist1 = create_histogram_table(events1, bins) + hist2 = create_histogram_table(events2, bins) + check_histograms(hist1, hist2) + + hist3 = create_histogram_table(events1, [0, 10] * u.TeV) + with pytest.raises(ValueError): + check_histograms(hist1, hist3)