From 69b7f27a9415f5e82d11ad4ab4f39af66e490188 Mon Sep 17 00:00:00 2001 From: Johannes Schumann <johannes.schumann@fau.de> Date: Mon, 16 Jan 2023 03:01:37 +0100 Subject: [PATCH] Add zenith range to spherical volume and add tests --- km3buu/geometry.py | 10 +++++++--- km3buu/tests/test_geometry.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/km3buu/geometry.py b/km3buu/geometry.py index 83add12..fa14cd7 100644 --- a/km3buu/geometry.py +++ b/km3buu/geometry.py @@ -186,13 +186,17 @@ class SphericalVolume(DetectorVolume): center: tuple [m] Coordinate center of the sphere (x, y, z) + zenith: float [1] (default (-1.0, 1.0) + Zenith range given as cos(θ) """ - def __init__(self, radius, coord_origin=(0, 0, 0)): + def __init__(self, radius, coord_origin=(0, 0, 0), zenith=(-1, 1)): super().__init__() self._radius = radius self._coord_origin = coord_origin self._volume = self._calc_volume() - self._solid_angle = 4 * np.pi + self._cosZmin = zenith[0] + self._cosZmax = zenith[1] + self._solid_angle = 2 * np.pi * (self._cosZmax - self._cosZmin) def _calc_volume(self): return 4 / 3 * np.pi * np.power(self._radius, 3) @@ -209,7 +213,7 @@ class SphericalVolume(DetectorVolume): def random_dir(self): phi = np.random.uniform(0, 2 * np.pi) - cos_theta = np.random.uniform(-1, 1) + cos_theta = np.random.uniform(self._cosZmin, self._cosZmax) return (phi, cos_theta) def header_entries(self, nevents=0): diff --git a/km3buu/tests/test_geometry.py b/km3buu/tests/test_geometry.py index 87f5396..ee4c209 100644 --- a/km3buu/tests/test_geometry.py +++ b/km3buu/tests/test_geometry.py @@ -42,6 +42,19 @@ class TestSphere(unittest.TestCase): radius = np.sqrt(np.sum(np.power((np.array(pos) - 2), 2))) assert radius <= 20 + def test_limited_zenith(self): + np.random.seed(1234) + geometry = CanVolume(zenith=(-0.4, 0.5)) + self.assertAlmostEqual(geometry.solid_angle, 5.654866776461628) + direction = geometry.random_dir() + self.assertAlmostEqual(direction[1], 0.15989789393584863) + geometry = CanVolume(zenith=(0.1, 0.3)) + direction = geometry.random_dir() + self.assertAlmostEqual(direction[1], 0.25707171674275386) + geometry = CanVolume(zenith=(-0.3, -0.2)) + direction = geometry.random_dir() + self.assertAlmostEqual(direction[1], -0.2727407394717358) + class TestCan(unittest.TestCase): def setUp(self): @@ -69,6 +82,7 @@ class TestCan(unittest.TestCase): def test_limited_zenith(self): np.random.seed(1234) geometry = CanVolume(zenith=(-0.4, 0.5)) + self.assertAlmostEqual(geometry.solid_angle, 5.654866776461628) direction = geometry.random_dir() self.assertAlmostEqual(direction[1], 0.15989789393584863) geometry = CanVolume(zenith=(0.1, 0.3)) -- GitLab