|
| 1 | +import pyhepmc as hep |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +def test_no_vertex_info(): |
| 7 | + px = py = pz = en = m = np.linspace(0, 1, 4) |
| 8 | + |
| 9 | + pid = np.arange(4) + 1 |
| 10 | + sta = np.zeros(4, dtype=np.int32) |
| 11 | + parents = [(0, 0), (1, 1), (0, 0), (0, 0)] |
| 12 | + hev = hep.GenEvent() |
| 13 | + hev.from_hepevt(0, px, py, pz, en, m, pid, sta, parents) |
| 14 | + assert len(hev.vertices) == 1 |
| 15 | + assert len(hev.particles) == 4 |
| 16 | + |
| 17 | + |
| 18 | +def test_parents_range_exceeding_particle_range(): |
| 19 | + px = py = pz = en = m = np.linspace(0, 1, 6) |
| 20 | + pid = np.arange(6) + 1 |
| 21 | + sta = np.zeros(6, dtype=np.int32) |
| 22 | + parents = [(0, 0), (1, 1), (2, 0), (3, 5), (4, 10), (3, 5)] |
| 23 | + with pytest.raises(RuntimeError): |
| 24 | + hep.GenEvent().from_hepevt(0, px, py, pz, en, m, pid, sta, parents) |
| 25 | + |
| 26 | + |
| 27 | +def test_invalid_length_of_parents(): |
| 28 | + px = py = pz = en = m = np.linspace(0, 1, 3) |
| 29 | + pid = np.arange(3) + 1 |
| 30 | + sta = np.zeros(3, dtype=np.int32) |
| 31 | + parents = [(0, 0), (1, 2)] |
| 32 | + with pytest.raises(RuntimeError): |
| 33 | + hep.GenEvent().from_hepevt(0, px, py, pz, en, m, pid, sta, parents) |
| 34 | + |
| 35 | + |
| 36 | +def test_inverted_parents_range(): |
| 37 | + px = py = pz = en = m = vx = vy = vz = vt = np.linspace(0, 1, 4) |
| 38 | + pid = np.arange(4) + 1 |
| 39 | + sta = np.zeros(4, dtype=np.int32) |
| 40 | + # inverted range is not an error (2, 1) will be converted to (1, 2) |
| 41 | + parents = [(0, 0), (2, 1), (3, 3), (3, 3)] |
| 42 | + hev = hep.GenEvent() |
| 43 | + hev.from_hepevt(0, px, py, pz, en, m, pid, sta, parents) |
| 44 | + expected = [[0, 1], [2]] |
| 45 | + got = [[p.id - 1 for p in v.particles_in] for v in hev.vertices] |
| 46 | + assert expected == got |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("bad", ([-4, 1], [1, -4])) |
| 50 | +def test_negative_parents_range(bad): |
| 51 | + px = py = pz = en = m = vx = vy = vz = vt = np.linspace(0, 1, 4) |
| 52 | + pid = np.arange(4) + 1 |
| 53 | + sta = np.zeros(4, dtype=np.int32) |
| 54 | + # inverted range is not an error (2, 1) will be converted to (1, 2) |
| 55 | + parents = [(0, 0), bad, (3, 3), (3, 3)] |
| 56 | + with pytest.raises(RuntimeError): |
| 57 | + hep.GenEvent().from_hepevt(0, px, py, pz, en, m, pid, sta, parents) |
0 commit comments