diff --git a/km3buu/geometry.py b/km3buu/geometry.py index c1126c414ad6ce4a698db1c8cd32a9c6aa0bb568..87b98f4c503d199b4ed1703dd558b23baad949ad 100644 --- a/km3buu/geometry.py +++ b/km3buu/geometry.py @@ -82,14 +82,21 @@ class CanVolume(DetectorVolume): Cylinder bottom z position zmax: float [m] (default: 476.5) Cylinder top z position + detector_center: tuple [m] (default: (0.0, 0.0) ) + Detector center position in the xy-plane """ - def __init__(self, radius=403.4, zmin=0.0, zmax=476.5): + def __init__(self, + radius=403.4, + zmin=0.0, + zmax=476.5, + detector_center=(0., 0.)): super().__init__() self._radius = radius self._zmin = zmin self._zmax = zmax self._volume = self._calc_volume() + self._detector_center = detector_center def _calc_volume(self): return np.pi * (self._zmax - self._zmin) * np.power(self._radius, 2) @@ -97,8 +104,8 @@ class CanVolume(DetectorVolume): def random_pos(self): r = self._radius * np.sqrt(np.random.uniform(0, 1)) phi = np.random.uniform(0, 2 * np.pi) - pos_x = r * np.cos(phi) - pos_y = r * np.sin(phi) + pos_x = r * np.cos(phi) + self._detector_center[0] + pos_y = r * np.sin(phi) + self._detector_center[1] pos_z = np.random.uniform(self._zmin, self._zmax) return (pos_x, pos_y, pos_z) diff --git a/km3buu/tests/test_geometry.py b/km3buu/tests/test_geometry.py index 292cac2dd3b709a8279c02976db9148471de7b7a..0b6b880057b0eb09f2ede11d6410b1225bc8c162 100644 --- a/km3buu/tests/test_geometry.py +++ b/km3buu/tests/test_geometry.py @@ -17,12 +17,14 @@ import numpy as np class TestGeneralGeometry(unittest.TestCase): + def test_abstract_init(self): with self.assertRaises(TypeError) as ctx: d = DetectorVolume() class TestSphere(unittest.TestCase): + def setUp(self): self.detector_geometry = SphericalVolume(20, (2, 2, 2)) @@ -44,9 +46,25 @@ class TestSphere(unittest.TestCase): class TestCan(unittest.TestCase): + def setUp(self): self.detector_geometry = CanVolume() def test_volume(self): volume = self.detector_geometry.volume self.assertAlmostEqual(volume, 243604084.28, 2) + + def test_position(self): + np.random.seed(1234) + pos = self.detector_geometry.random_pos() + self.assertAlmostEqual(pos[0], -127.07940486491587) + self.assertAlmostEqual(pos[1], -122.54421157149173) + self.assertAlmostEqual(pos[2], 208.57726763689004) + + def test_position(self): + np.random.seed(1234) + detector_geometry = CanVolume(detector_center=(100, 100)) + pos = detector_geometry.random_pos() + self.assertAlmostEqual(pos[0], -27.07940486491587) + self.assertAlmostEqual(pos[1], -22.54421157149173) + self.assertAlmostEqual(pos[2], 208.57726763689004)