From 787c2fbe4793e6c68ed88724962e5f9f3d29c94e Mon Sep 17 00:00:00 2001
From: Johannes Schumann <johannes.schumann@fau.de>
Date: Wed, 11 Mar 2020 18:31:34 +0100
Subject: [PATCH] Tests for output objects and corrected read to four-vector

---
 km3buu/data/test-data/FinalEvents.dat |  5 +++
 km3buu/output.py                      |  9 ++---
 km3buu/tests/test_output.py           | 49 +++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 4 deletions(-)
 create mode 100644 km3buu/data/test-data/FinalEvents.dat
 create mode 100644 km3buu/tests/test_output.py

diff --git a/km3buu/data/test-data/FinalEvents.dat b/km3buu/data/test-data/FinalEvents.dat
new file mode 100644
index 0000000..185a209
--- /dev/null
+++ b/km3buu/data/test-data/FinalEvents.dat
@@ -0,0 +1,5 @@
+# 1:Run  2:Event  3:ID 4:Charge     5:perweight   6:position(1)   7:position(2)   8:position(3)   9:momentum(0)  10:momentum(1)  11:momentum(2)  12:momentum(3)     13:history  14:production_ID  15:enu
+      1      1    901     -1  6.154773E-01  0.000000E+00  0.000000E+00  0.000000E+00  5.050394E-01  2.619802E-02  3.290991E-01  3.821936E-01          0   2  1.000000E+00
+      1      1      1      1  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  9.380000E-01  0.000000E+00  0.000000E+00  0.000000E+00          0   2  1.000000E+00
+      1      1    101      1  6.154773E-01  0.000000E+00  0.000000E+00  0.000000E+00  3.918515E-01 -1.150792E-01  2.178788E-02  3.475423E-01    1000002   2  1.000000E+00
+      1      1      1      1  6.154773E-01  0.000000E+00  0.000000E+00  0.000000E+00  1.041109E+00  8.888118E-02 -3.508870E-01  2.702641E-01    1000002   2  1.000000E+00
diff --git a/km3buu/output.py b/km3buu/output.py
index 9ed7b96..3abda4b 100644
--- a/km3buu/output.py
+++ b/km3buu/output.py
@@ -52,10 +52,11 @@ class FinalEvents:
                        ('id', np.int32), ('charge', np.float64),
                        ('perweight', np.float64), ('x', np.float64),
                        ('y', np.float64), ('z', np.float64),
-                       ('p_x', np.float64), ('p_y', np.float64),
-                       ('p_z', np.float64), ('history', np.int32),
-                       ('pID', np.int32), ('energy', np.float64)])
-        return np.genfromtxt(StringIO('\n'.join(s)), dtype=dt)[::1]
+                       ('p_t', np.float64), ('p_x', np.float64),
+                       ('p_y', np.float64), ('p_z', np.float64),
+                       ('history', np.int32), ('pID', np.int32),
+                       ('energy', np.float64)])
+        return np.genfromtxt(StringIO('\n'.join(s)), dtype=dt)
 
     def __len__(self):
         return len(self._line_pos)
diff --git a/km3buu/tests/test_output.py b/km3buu/tests/test_output.py
new file mode 100644
index 0000000..1da0ad4
--- /dev/null
+++ b/km3buu/tests/test_output.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# coding=utf-8
+# Filename: test_output.py
+
+__author__ = "Johannes Schumann"
+__copyright__ = "Copyright 2020, Johannes Schumann and the KM3NeT collaboration."
+__credits__ = []
+__license__ = "MIT"
+__maintainer__ = "Johannes Schumann"
+__email__ = "jschumann@km3net.de"
+__status__ = "Development"
+
+import unittest
+import numpy as np
+from km3buu.output import *
+from os import listdir
+from os.path import abspath, join, dirname
+
+TESTDATA_DIR = abspath(join(dirname(__file__), "../data/test-data/"))
+
+
+class TestFinalEvents(unittest.TestCase):
+    def setUp(self):
+        self.filename = join(TESTDATA_DIR, "FinalEvents.dat")
+        self.final_events = FinalEvents(self.filename)
+
+    def test_values(self):
+        assert self.final_events[0]["id"] == 901
+        assert self.final_events[0]["charge"] == -1
+        self.assertAlmostEqual(self.final_events[0]["perweight"], 6.154773e-1)
+        self.assertAlmostEqual(self.final_events[0]["p_t"], 5.050394e-1)
+        self.assertAlmostEqual(self.final_events[0]["p_x"], 2.619802e-2)
+        self.assertAlmostEqual(self.final_events[0]["p_y"], 3.290991e-1)
+        self.assertAlmostEqual(self.final_events[0]["p_z"], 3.821936e-1)
+        self.assertAlmostEqual(self.final_events[0]["energy"], 1.0)
+        assert self.final_events[3]["id"] == 1
+        assert self.final_events[3]["charge"] == 1
+        self.assertAlmostEqual(self.final_events[3]["perweight"], 6.154773e-1)
+
+    def test_index(self):
+        assert self.final_events[0] is not None
+
+    def test_slicing(self):
+        assert self.final_events[0:2] is not None
+
+
+class TestGiBUUOutput(unittest.TestCase):
+    def setUp(self):
+        pass
-- 
GitLab