Coverage for src/km3pipe/tests/test_h5extractf.py: 100%
43 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 03:14 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 03:14 +0000
1import numpy as np
2import km3net_testdata
3import tempfile
4import h5py
5import km3pipe as kp
6from km3pipe.utils.h5extractf import h5extractf
7from km3pipe.testing import TestCase
10class TestJsireneH5File(TestCase):
11 @classmethod
12 def setUpClass(cls):
13 filename = km3net_testdata.data_path(
14 "offline/mcv5.0.DAT004340.propa.sirene.jte.jchain.aanet.4340.root"
15 )
16 cls.h5file = tempfile.NamedTemporaryFile()
17 h5extractf(filename, outfile=cls.h5file)
19 @classmethod
20 def tearDownClass(cls):
21 cls.h5file.close()
23 def test_datasets_names_and_lengths(self):
24 target = {
25 "event_info": 3,
26 "group_info": 3,
27 "hits": 280,
28 "mc_hits": 598,
29 "mc_tracks": 48,
30 "raw_header": 21,
31 "reco": 2,
32 }
33 with h5py.File(self.h5file, "r") as f:
34 self.assertDictEqual(target, {x: len(f[x]) for x in f})
36 def test_reco_datasets_names_and_lengths(self):
37 target = {"best_jmuon": 3, "tracks": 76}
38 with h5py.File(self.h5file, "r") as f:
39 self.assertDictEqual(target, {x: len(f["reco"][x]) for x in f["reco"]})
41 def test_time_of_first_three_hits(self):
42 with h5py.File(self.h5file, "r") as f:
43 np.testing.assert_array_equal(
44 f["hits"]["time"][:3],
45 [87530475.0, 87526107.0, 87527368.0],
46 )
48 def test_time_of_last_three_hits(self):
49 with h5py.File(self.h5file, "r") as f:
50 np.testing.assert_array_equal(
51 f["hits"]["time"][-3:],
52 [8166085.0, 8165582.0, 8163487.0],
53 )
55 def test_hits_group_ids(self):
56 target = [0] * 86 + [1] * 111 + [2] * 83
57 with h5py.File(self.h5file, "r") as f:
58 np.testing.assert_array_equal(
59 f["hits"]["group_id"],
60 target,
61 )
63 def test_hits_dtype_names(self):
64 target = (
65 "channel_id",
66 "dom_id",
67 "time",
68 "tot",
69 "triggered",
70 "pos_x",
71 "pos_y",
72 "pos_z",
73 "dir_x",
74 "dir_y",
75 "dir_z",
76 "tdc",
77 "group_id",
78 )
79 with h5py.File(self.h5file, "r") as f:
80 self.assertTupleEqual(
81 f["hits"].dtype.names,
82 target,
83 )
85 def test_h5_file_can_be_opened_with_hdf5pump_and_keys_are_correct(self):
86 pump = kp.io.HDF5Pump(filename=self.h5file.name)
87 blob = pump[1]
88 target = {
89 "BestJmuon",
90 "EventInfo",
91 "GroupInfo",
92 "Header",
93 "Hits",
94 "McHits",
95 "McTracks",
96 "RawHeader",
97 "Tracks",
98 }
99 self.assertSetEqual(target, set(blob.keys()))