Skip to content

Commit

Permalink
Add Setup class (#48)
Browse files Browse the repository at this point in the history
Closes #45
  • Loading branch information
HDembinski authored Oct 12, 2022
1 parent eb6a3e6 commit b861baa
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 10 deletions.
3 changes: 2 additions & 1 deletion generate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def handle_compounddef(results, node):
if getter not in results:
continue
results[prop] = results[getter] + results[setter]
del results[getter]
if prop != getter:
del results[getter]
del results[setter]

# merge trivially duplicated entries
Expand Down
14 changes: 14 additions & 0 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,20 @@ PYBIND11_MODULE(_core, m) {
},
DOC(Print.listing));

m.def("_Setup_print_errors", &Setup::print_errors, DOC(Setup.print_errors));

m.def("_Setup_set_print_errors", &Setup::set_print_errors,
DOC(Setup.set_print_errors));

m.def("_Setup_print_warnings", &Setup::print_warnings, DOC(Setup.print_warnings));

m.def("_Setup_set_print_warnings", &Setup::set_print_warnings,
DOC(Setup.set_print_warnings));

m.def("_Setup_debug_level", &Setup::debug_level, DOC(Setup.debug_level));

m.def("_Setup_set_debug_level", &Setup::set_debug_level, DOC(Setup.set_debug_level));

FUNC(equal_particle_sets);
FUNC(equal_vertex_sets);

Expand Down
19 changes: 17 additions & 2 deletions src/from_hepevt.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "pybind.hpp"
#include <HepMC3/Errors.h>
#include <HepMC3/GenEvent.h>
#include <HepMC3/GenParticle.h>
#include <HepMC3/GenVertex.h>
Expand Down Expand Up @@ -36,6 +37,19 @@ void normalize(int& m1, int& m2) {

namespace HepMC3 {

void modded_add_particle_in(int vid, GenVertexPtr v, GenParticlePtr p) {
// Particle can only have one end vertex in HepMC3. Default is to reassign so
// that the later end vertex gets the particle. We change this here so that
// the first vertex keeps the particle. This is safer, because otherwise the
// first vertex may end up without incoming particles.
if (p->end_vertex()) {
// clang-format off
HEPMC3_DEBUG(10,"from_hepevt: vertex -" << vid << ": skipping incoming particle " << p->id() << " which already has end_vertex " << p->end_vertex()->id());
// clang-format on
} else
v->add_particle_in(p);
}

void connect_parents_and_children(GenEvent& event, bool parents,
py::array_t<int> parents_or_children, py::object vx,
py::object vy, py::object vz, py::object vt) {
Expand Down Expand Up @@ -126,13 +140,14 @@ void connect_parents_and_children(GenEvent& event, bool parents,
}

GenVertexPtr v{new GenVertex(pos)};
int vid = event.vertices().size();

if (parents) {
for (int k = m1; k < m2; ++k) v->add_particle_in(particles.at(k));
for (int k = m1; k < m2; ++k) modded_add_particle_in(vid, v, particles.at(k));
for (const auto k : co) v->add_particle_out(particles.at(k));
} else {
for (int k = m1; k < m2; ++k) v->add_particle_out(particles.at(k));
for (const auto k : co) v->add_particle_in(particles.at(k));
for (const auto k : co) modded_add_particle_in(vid, v, particles.at(k));
}

event.add_vertex(v);
Expand Down
34 changes: 32 additions & 2 deletions src/pyhepmc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
Missing functionality
---------------------
- Not yet implemented: ``GenParticleData``, ``GenVertexData``, ``ReaderMT``,
``Setup``. These will be added in the future.
- Not yet implemented: ``GenParticleData``, ``GenVertexData``, ``ReaderMT``.
These will be added in the future.
"""
from sys import version_info
Expand All @@ -51,6 +51,12 @@
equal_particle_sets,
content,
listing,
_Setup_print_errors,
_Setup_set_print_errors,
_Setup_print_warnings,
_Setup_set_print_warnings,
_Setup_debug_level,
_Setup_set_debug_level,
delta_phi,
delta_eta,
delta_r2_eta,
Expand Down Expand Up @@ -80,6 +86,7 @@
"equal_particle_sets",
"content",
"listing",
"Setup",
"delta_phi",
"delta_eta",
"delta_r2_eta",
Expand All @@ -90,6 +97,29 @@
"open",
)


class _Setup:
print_errors = property(
lambda s: _Setup_print_errors(),
lambda s, v: _Setup_set_print_errors(v),
"Whether to print errors or not.",
)

print_warnings = property(
lambda s: _Setup_print_warnings(),
lambda s, v: _Setup_set_print_warnings(v),
"Whether to print warnings or not.",
)

debug_level = property(
lambda s: _Setup_debug_level(),
lambda s, v: _Setup_set_debug_level(v),
"Access debug level.",
)


Setup = _Setup()

try:
from .view import to_dot as _to_dot

Expand Down
Loading

0 comments on commit b861baa

Please sign in to comment.