From 4a413fa5cc8d4973f8b05e4e5fda7cea9ed619c4 Mon Sep 17 00:00:00 2001
From: Johannes Schumann <johannes.schumann@fau.de>
Date: Wed, 27 Jul 2022 13:55:54 +0000
Subject: [PATCH] Resolve "Add detector center option to geometry"

---
 km3buu/geometry.py            | 13 ++++++++++---
 km3buu/tests/test_geometry.py | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/km3buu/geometry.py b/km3buu/geometry.py
index c1126c4..87b98f4 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 292cac2..0b6b880 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)
-- 
GitLab