Exemplo n.º 1
0
def charged_indices(pids):
    """ Indicate charged tracks which can have their vertices reconstructed.

    Parameters
    ----------
    pids : listlike of int
        monte carlo particle ids of the particles;
        https://pdg.lbl.gov/2007/reviews/montecarlorpp.pdf
    """
    identifier = PDGNames.Identities()
    indices = [i for i, pid in enumerate(pids) if identifier.charges[pid]]
    return indices
Exemplo n.º 2
0
def test_Identities():
    ids = PDGNames.Identities()
    str(ids)  # check the string method runs
    known_particles = [{'id': 5, 'name': 'b', 'charge': -1/3, 'spin':1/2},
                       {'id': -5, 'name': 'bbar', 'charge': 1/3, 'spin':1/2},
                       {'id': 25, 'name': 'h0', 'charge': 0, 'spin': 0}]
    for particle in known_particles:
        found = ids[particle['id']]
        for key in particle:
            assert particle[key] == found[key], f"For test particle {particle} found {found}"
    assert 'cbar' == ids.antiNames[4]
    assert 'cbar' == ids.antiNames[-4]
    assert 2/3 == ids.charges[4]
    assert -2/3 == ids.charges[-4]
Exemplo n.º 3
0
def test_ReadHepmc():
    check_lines()
    hepmc_file = os.path.join(data_dir, "mini.hepmc")
    n_events = 4
    # try starting from 0
    hepmc1 = ReadHepmc.Hepmc(hepmc_file, 0, n_events)
    # try startng from 1
    start2 = 1
    hepmc2 = ReadHepmc.Hepmc(hepmc_file, start=start2, stop=n_events)
    idents = PDGNames.Identities()
    all_ids = set(idents.particle_data[:, idents.columns["id"]])
    for event_n in range(n_events):  # loop over out selection of events
        hepmc1.selected_event = event_n
        # sanity checks on the components
        assert hepmc1.Event_n == event_n
        # again for the other one
        check_event_consistancy(hepmc1, all_ids)
        try:
            hepmc2.selected_event = event_n
            assert hepmc2.Event_n == event_n + 1
        except (IndexError, ValueError):
            assert event_n + start2 == n_events
            continue  # reach the end of 1
        check_event_consistancy(hepmc2, all_ids)
Exemplo n.º 4
0
def void_test_ReadHepmc2():
    hepmc_file = os.path.join(data_dir, "billy_tag_2_pythia8_events.hepmc")
    n_events = 4
    hepmc = ReadHepmc.Hepmc(hepmc_file, 0, n_events)
    idents = PDGNames.Identities()
    mom_diff = []
    all_ids = set(idents.particle_data[:, idents.columns["id"]])
    for event_n in range(n_events):  # loop over out selection of events
        hepmc.selected_event = event_n
        # sanity checks on the components
        assert hepmc.Event_n == event_n
        # check the beam particles
        beam_idx1 = np.where(hepmc.Particle_barcode == hepmc.Barcode_beam_particle1)[0][0]
        beam_idx2 = np.where(hepmc.Particle_barcode == hepmc.Barcode_beam_particle2)[0][0]
        assert hepmc.Is_root[beam_idx1]
        assert hepmc.Is_root[beam_idx2]
        assert len(hepmc.Parents[beam_idx1]) == 0
        assert len(hepmc.Parents[beam_idx2]) == 0
        # valid PID
        assert set(np.abs(hepmc.MCPID)).issubset(all_ids)
        # conservation of momentum at the vertices
        parent_p4 = np.zeros(4)
        child_p4 = np.zeros(4)
        issues = 0
        relations = 0
        conserved_multiplicity = []
        unconserved_multiplicity = []
        for v_barcode in hepmc.Vertex_barcode:
            parent_p4[:] = 0
            child_p4[:] = 0
            parents_idx = np.where(hepmc.End_vertex_barcode == v_barcode)[0]
            children_idx = np.where(hepmc.Start_vertex_barcode == v_barcode)[0]
            if len(parents_idx) == 0:
                for cidx in children_idx:
                    assert hepmc.Is_root[cidx]
            elif len(children_idx) == 0:
                for pidx in parents_idx:
                    assert hepmc.Is_leaf[pidx]
            else:
                for pidx in parents_idx:
                    parent_p4 += (hepmc.Energy[pidx], hepmc.Px[pidx], hepmc.Py[pidx], hepmc.Pz[pidx])
                for cidx in children_idx:
                    child_p4 += (hepmc.Energy[cidx], hepmc.Px[cidx], hepmc.Py[cidx], hepmc.Pz[cidx])
                relations += 1
                mom_diff.append(parent_p4 - child_p4)
                try:
                    tst.assert_allclose(parent_p4[1:], child_p4[1:], rtol=0.05, atol=0.001)
                    conserved_multiplicity.append(len(children_idx) + len(parents_idx))
                except AssertionError as e:
                    if issues < 1 and event_n==0:
                        parent_barcodes = hepmc.Particle_barcode[parents_idx]
                        children_barcodes = hepmc.Particle_barcode[children_idx]
                        print(f"Vertex {v_barcode} has incoming particles with barcodes {parent_barcodes} and outgoing particles with barcodes {children_barcodes}")
                        print(f"The incoming total momentum is {parent_p4}")
                        print(f"The outgoing total momentum is {child_p4}")
                    issues += 1
                    unconserved_multiplicity.append(len(children_idx) + len(parents_idx))
        print(f"Event {event_n}")
        print(f"non-conservation {issues}; {100*issues/relations:.1f}%")
        print(f"multipicity, conserved {np.mean(conserved_multiplicity):.2f}, unconserved {np.mean(unconserved_multiplicity):.2f}")
    return np.array(mom_diff)
Exemplo n.º 5
0
def void_test_ReadHepmc():
    hepmc_file = os.path.join(data_dir, "mini.hepmc")
    n_events = 1
    hepmc = ReadHepmc.Hepmc(hepmc_file, 0, n_events)
    idents = PDGNames.Identities()
    all_ids = set(idents.particle_data[:, idents.columns["id"]])
    for event_n in range(n_events):  # loop over out selection of events
        hepmc.selected_event = event_n
        # sanity checks on the components
        assert hepmc.Event_n == event_n
        # check the beam particles
        beam_idx1 = np.where(hepmc.Particle_barcode == hepmc.Barcode_beam_particle1)[0][0]
        beam_idx2 = np.where(hepmc.Particle_barcode == hepmc.Barcode_beam_particle2)[0][0]
        assert hepmc.Is_root[beam_idx1]
        assert hepmc.Is_root[beam_idx2]
        assert len(hepmc.Parents[beam_idx1]) == 0
        assert len(hepmc.Parents[beam_idx2]) == 0
        # valid PID
        assert set(np.abs(hepmc.MCPID)).issubset(all_ids)
        # conservation of momentum and parent child reflection
        num_particles = len(hepmc.MCPID)
        parent_p4 = np.zeros(4)
        child_p4 = np.zeros(4)
        self_p4 = np.zeros(4)
        children_issues = 0
        children_relations = 0
        parent_issues = 0
        parent_relations = 0
        conserved_parent_multiplicity = []
        conserved_children_multiplicity = []
        unconserved_parent_multiplicity = []
        unconserved_children_multiplicity = []
        for idx in range(num_particles):  # loop over particles in event
            parent_p4[:] = 0
            child_p4[:] = 0
            self_p4[:] = (hepmc.Energy[idx], hepmc.Px[idx], hepmc.Py[idx], hepmc.Pz[idx])
            if not hepmc.Is_root[idx]:
                parent_relations += 1
                # we have already checked the parents of roots
                parent_idx = hepmc.Parents[idx]
                assert len(parent_idx) > 0
                for pidx in parent_idx:
                    assert idx in hepmc.Children[pidx]
                    parent_p4 += (hepmc.Energy[pidx], hepmc.Px[pidx], hepmc.Py[pidx], hepmc.Pz[pidx])
                    # if these parents have children besides this child
                    # subtract their momentum out
                    for cidx in hepmc.Children[pidx]:
                        if cidx == idx:
                            continue
                        else:
                            parent_p4 -= (hepmc.Energy[cidx], hepmc.Px[cidx], hepmc.Py[cidx], hepmc.Pz[cidx])
                try:
                    tst.assert_allclose(parent_p4[1:], self_p4[1:], rtol=0.05, atol=0.001)
                    conserved_parent_multiplicity.append(len(parent_idx))
                except AssertionError as e:
                    #print(f"Event {event_n}, particle {idx}, parents; {e}")
                    parent_issues += 1
                    unconserved_parent_multiplicity.append(len(parent_idx))
            if hepmc.Is_leaf[idx]:
                assert len(hepmc.Children[idx]) == 0
            else:
                children_relations += 1
                children_idx = hepmc.Children[idx]
                assert len(children_idx) > 0
                for cidx in children_idx:
                    assert idx in hepmc.Parents[cidx]
                    child_p4 += (hepmc.Energy[cidx], hepmc.Px[cidx], hepmc.Py[cidx], hepmc.Pz[cidx])
                    # if these children have parents besides this one
                    # subtract their momentum out
                    for pidx in hepmc.Parents[cidx]:
                        if pidx == idx:
                            continue
                        else:
                            child_p4 -= (hepmc.Energy[pidx], hepmc.Px[pidx], hepmc.Py[pidx], hepmc.Pz[pidx])
                try:
                    tst.assert_allclose(child_p4[1:], self_p4[1:], rtol=0.05, atol = 0.001)
                    conserved_children_multiplicity.append(len(children_idx))
                except AssertionError as e:
                    #print(f"Event {event_n}, particle {idx}, children; {e}")
                    children_issues += 1
                    unconserved_children_multiplicity.append(len(children_idx))
        print(f"Event {event_n}")
        print(f"Children non-conservation {children_issues}; {100*children_issues/children_relations:.1f}%")
        print(f"Parent non-conservation {parent_issues}; {100*parent_issues/parent_relations:.1f}%")
        print(f"Any non-conservation {100*(parent_issues+children_issues)/(children_relations+parent_relations):.1f}%")
        print(f"Children multipicity, conserved {np.mean(conserved_children_multiplicity):.2f}, unconserved {np.mean(unconserved_children_multiplicity):.2f}")
        print(f"Parent multipicity, conserved {np.mean(conserved_parent_multiplicity):.2f}, unconserved {np.mean(unconserved_parent_multiplicity):.2f}")