diff --git a/km3buu/geometry.py b/km3buu/geometry.py index 83add12e4f41eb320c71a9ec61ae9471989f706a..fa14cd7cd9fd43ee22037c6b26f2d3943ebbff0d 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 87f539680d1d5f4f938519a20f3c05e6b4a3949a..ee4c209d9a9d0f7ab194d1e9c26666bb0cd5d5a9 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))