-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
4,666 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Examples | |
:maxdepth: 1 | ||
|
||
examples/basics | ||
examples/processing |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <HepMC3/GenEvent.h> | ||
#include <HepMC3/GenParticle.h> | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
using namespace HepMC3; | ||
|
||
void register_bench(py::module& m) { | ||
m.def("_sum_energy_of_protons", [](GenEvent& event) { | ||
double esum = 0; | ||
for (auto&& p : event.particles()) { | ||
if (std::abs(p->pdg_id()) != 2212) continue; | ||
if (p->status() != 1) continue; | ||
esum += p->momentum().e(); | ||
} | ||
return esum; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "geneventdata.hpp" | ||
#include <HepMC3/Data/GenEventData.h> | ||
#include <HepMC3/Data/GenParticleData.h> | ||
#include <HepMC3/Data/GenVertexData.h> | ||
|
||
using namespace HepMC3; | ||
|
||
struct ParticleData { | ||
int pid; | ||
int status; | ||
bool is_mass_set; | ||
double mass; | ||
double px, py, pz, e; | ||
}; | ||
|
||
struct VertexData { | ||
int status; | ||
double x, y, z, t; | ||
}; | ||
|
||
void register_geneventdata_dtypes() { | ||
PYBIND11_NUMPY_DTYPE(ParticleData, pid, status, is_mass_set, mass, px, py, pz, e); | ||
PYBIND11_NUMPY_DTYPE(VertexData, status, x, y, z, t); | ||
} | ||
|
||
#define MAKE(name, type) \ | ||
py::object GenEventData_##name(py::object self) { \ | ||
auto& s = py::cast<GenEventData&>(self); \ | ||
py::ssize_t shape[1] = {static_cast<py::ssize_t>(s.name.size())}; \ | ||
auto dt = py::dtype::of<type>(); \ | ||
return py::array(dt, shape, s.name.data(), self); \ | ||
} | ||
|
||
MAKE(weights, double) | ||
MAKE(vertices, VertexData) | ||
MAKE(particles, ParticleData) | ||
MAKE(links1, int) | ||
MAKE(links2, int) | ||
|
||
#define MAKES(name) \ | ||
py::object GenEventData_##name(py::object self) { \ | ||
auto& s = py::cast<GenEventData&>(self); \ | ||
py::list l; \ | ||
for (const auto& str : s.name) l.append(str); \ | ||
return l; \ | ||
} | ||
|
||
MAKES(attribute_id) | ||
MAKES(attribute_name) | ||
MAKES(attribute_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef PYHEPMC_GENEVENTDATA_HPP | ||
#define PYHEPMC_GENEVENTDATA_HPP | ||
|
||
#include "pybind.hpp" | ||
|
||
py::object GenEventData_particles(py::object); | ||
py::object GenEventData_vertices(py::object); | ||
py::object GenEventData_weights(py::object); | ||
py::object GenEventData_links1(py::object); | ||
py::object GenEventData_links2(py::object); | ||
py::object GenEventData_attribute_id(py::object); | ||
py::object GenEventData_attribute_name(py::object); | ||
py::object GenEventData_attribute_string(py::object); | ||
|
||
void register_geneventdata_dtypes(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "numpy_api.hpp" | ||
|
||
#define NP_ARRAY(cls, kind, type, method) \ | ||
.def_property_readonly(#method, [](cls& self) { \ | ||
using namespace HepMC3; \ | ||
const GenEvent& event = py::cast<const GenEvent&>(self.event_); \ | ||
const auto& x = event.kind(); \ | ||
py::array_t<type> a(x.size()); \ | ||
auto a2 = a.mutable_unchecked<1>(); \ | ||
for (size_t i = 0; i < x.size(); ++i) a2[i] = x[i]->method(); \ | ||
return a; \ | ||
}) | ||
|
||
#define NP_ARRAY2(cls, kind, type, method1, method2) \ | ||
.def_property_readonly(#method2, [](cls& self) { \ | ||
using namespace HepMC3; \ | ||
const GenEvent& event = py::cast<const GenEvent&>(self.event_); \ | ||
const auto& x = event.kind(); \ | ||
py::array_t<type> a(x.size()); \ | ||
auto a2 = a.mutable_unchecked<1>(); \ | ||
for (size_t i = 0; i < x.size(); ++i) a2[i] = x[i]->method1().method2(); \ | ||
return a; \ | ||
}) | ||
|
||
void register_numpy_api(py::module& m) { | ||
py::class_<ParticlesAPI>(m, "ParticlesAPI") | ||
.def(py::init<py::object>()) | ||
// clang-format off | ||
NP_ARRAY(ParticlesAPI, particles, int, id) | ||
NP_ARRAY(ParticlesAPI, particles, int, pid) | ||
NP_ARRAY(ParticlesAPI, particles, int, status) | ||
NP_ARRAY(ParticlesAPI, particles, bool, is_generated_mass_set) | ||
NP_ARRAY(ParticlesAPI, particles, double, generated_mass) | ||
NP_ARRAY2(ParticlesAPI, particles, double, momentum, px) | ||
NP_ARRAY2(ParticlesAPI, particles, double, momentum, py) | ||
NP_ARRAY2(ParticlesAPI, particles, double, momentum, pz) | ||
NP_ARRAY2(ParticlesAPI, particles, double, momentum, e) | ||
// clang-format on | ||
; | ||
|
||
py::class_<VerticesAPI>(m, "VerticesAPI") | ||
.def(py::init<py::object>()) | ||
// clang-format off | ||
NP_ARRAY(VerticesAPI, vertices, int, id) | ||
NP_ARRAY(VerticesAPI, vertices, int, status) | ||
NP_ARRAY(VerticesAPI, vertices, bool, has_set_position) | ||
NP_ARRAY2(VerticesAPI, vertices, double, position, x) | ||
NP_ARRAY2(VerticesAPI, vertices, double, position, y) | ||
NP_ARRAY2(VerticesAPI, vertices, double, position, z) | ||
NP_ARRAY2(VerticesAPI, vertices, double, position, t) | ||
// clang-format on | ||
; | ||
|
||
py::class_<NumpyAPI>(m, "NumpyAPI") | ||
.def_property_readonly("particles", | ||
[](NumpyAPI& self) { return ParticlesAPI(self.event_); }) | ||
.def_property_readonly("vertices", | ||
[](NumpyAPI& self) { return VerticesAPI(self.event_); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef PYHEPMC_NUMPY_API_HPP | ||
#define PYHEPMC_NUMPY_API_HPP | ||
|
||
#include <HepMC3/GenEvent.h> | ||
#include <HepMC3/GenParticle.h> | ||
#include <HepMC3/GenVertex.h> | ||
#include <pybind11/numpy.h> | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
|
||
struct NumpyAPI { | ||
py::object event_; | ||
NumpyAPI(py::object event) : event_{event} {} | ||
}; | ||
|
||
struct ParticlesAPI : NumpyAPI { | ||
using NumpyAPI::NumpyAPI; | ||
}; | ||
struct VerticesAPI : NumpyAPI { | ||
using NumpyAPI::NumpyAPI; | ||
}; | ||
|
||
void register_numpy_api(py::module& m); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.