Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HDembinski committed Jul 12, 2022
1 parent 9a7c932 commit 8927657
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 344 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ else()
add_subdirectory(extern/pybind11)
endif()

file(GLOB SOURCES_A "src/*.cpp")
file(GLOB SOURCES_B "extern/HepMC3/src/*.cc")
file(GLOB SOURCES "src/*.cpp")
file(GLOB SOURCES2 "extern/HepMC3/src/*.cc")

pybind11_add_module(_core MODULE ${SOURCES_A} ${SOURCES_B})
pybind11_add_module(_core MODULE ${SOURCES} ${SOURCES2})
target_include_directories(_core PRIVATE extern/HepMC3/include)

if(MSVC)
target_compile_options(_core PRIVATE /bigobj /D _CRT_SECURE_NO_WARNINGS)
target_compile_options(_core PRIVATE /bigobj)
else()
# ignore warnings raised by HepMC3 code
target_compile_options(_core PRIVATE
-fvisibility=hidden
-Wall
# -Wno-unused-value
-Wno-self-assign-overloaded
)
endif()
Expand Down
67 changes: 0 additions & 67 deletions src/core.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "hepevt_wrapper.h" // patched version of HEPEVT_wrapper.h
#include "pybind.h"

#include "HepMC3/FourVector.h"
Expand Down Expand Up @@ -603,72 +602,6 @@ PYBIND11_MODULE(_core, m) {
// py::class_<GenParticleData>(m, "GenParticleData");
// py::class_<GenVertexData>(m, "GenVertexData");

// this class is here to allow unit testing of HEPEVT converters
py::class_<HEPEVT>(m, "HEPEVT")
.def(py::init<>())
.def_readwrite("event_number", &HEPEVT::nevhep)
.def_readwrite("nentries", &HEPEVT::nhep)
.def("status",
[](py::object self) {
auto &evt = py::cast<HEPEVT &>(self);
return py::array_t<int>(evt.nhep, evt.isthep, self);
})
.def("pid",
[](py::object self) {
auto &evt = py::cast<HEPEVT &>(self);
return py::array_t<int>(evt.nhep, evt.idhep, self);
})
.def("parents",
[](py::object self) {
auto &evt = py::cast<HEPEVT &>(self);
return py::array_t<int>({evt.nhep, 2}, &evt.jmohep[0][0], self);
})
.def("children",
[](py::object self) {
auto &evt = py::cast<HEPEVT &>(self);
return py::array_t<int>({evt.nhep, 2}, &evt.jdahep[0][0], self);
})
.def("pm",
[](py::object self) {
auto &evt = py::cast<HEPEVT &>(self);
return py::array_t<momentum_t>({evt.nhep, 5}, &evt.phep[0][0],
self);
})
.def("v",
[](py::object self) {
auto &evt = py::cast<HEPEVT &>(self);
return py::array_t<momentum_t>({evt.nhep, 4}, &evt.vhep[0][0],
self);
})
.def("clear",
[](HEPEVT &self) {
HEPEVT_Wrapper::set_hepevt_address((char *)&self);
HEPEVT_Wrapper::zero_everything();
})
.def("from_genevent",
[](HEPEVT &self, const GenEvent &evt) {
HEPEVT_Wrapper::set_hepevt_address((char *)&self);
HEPEVT_Wrapper::GenEvent_to_HEPEVT(&evt);
})
.def("to_genevent",
[](const HEPEVT &self, GenEvent &evt) {
HEPEVT_Wrapper::set_hepevt_address((char *)&self);
HEPEVT_Wrapper::HEPEVT_to_GenEvent(&evt);
})
.def("__str__",
[](const HEPEVT &self) {
HEPEVT_Wrapper::set_hepevt_address((char *)&self);
std::ostringstream os;
HEPEVT_Wrapper::print_hepevt(os);
return os.str();
})
.def_property_readonly(
"ptr", [](const HEPEVT &self) { return (std::uintptr_t)&self; })
.def_property_readonly("object_size",
[](const HEPEVT &self) { return sizeof(HEPEVT); })
.def_property_readonly_static("max_size",
[](py::object) { return NMXHEP; });

m.def("content", [](const GenEvent &event) {
std::ostringstream os;
HepMC3::Print::content(os, event);
Expand Down
175 changes: 76 additions & 99 deletions src/io.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include "pybind.h"

#include "HepMC3/GenRunInfo.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/WriterAsciiHepMC2.h"
#include "HepMC3/WriterHEPEVT.h"
#include "HepMC3/ReaderAscii.h"
#include "HepMC3/ReaderAsciiHepMC2.h"
#include "HepMC3/ReaderLHEF.h"
#include "HepMC3/ReaderHEPEVT.h"
#include "HepMC3/ReaderLHEF.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/WriterAsciiHepMC2.h"
#include "HepMC3/WriterHEPEVT.h"

#ifdef HEPMC3_ROOTIO
#include "HepMC3/ReaderRootTree.h"
#include "HepMC3/ReaderRoot.h"
#include "HepMC3/ReaderRootTree.h"
#endif

#include <string>
#include <sstream>
#include <memory>
#include <sstream>
#include <string>

using namespace HepMC3;

Expand All @@ -31,101 +31,78 @@ using ReaderRootTreePtr = std::shared_ptr<ReaderRootTree>;
using ReaderRootPtr = std::shared_ptr<ReaderRoot>;
#endif

void register_io(py::module& m) {

// this class is here to simplify unit testing of Readers and Writers
py::class_<std::stringstream>(m, "stringstream")
.def(py::init<>())
.def(py::init<std::string>())
.def("__str__", (std::string (std::stringstream::*)() const) &std::stringstream::str)
METH(flush, std::stringstream)
METH(write, std::stringstream)
METH(read, std::stringstream)
;

py::class_<ReaderAscii, ReaderAsciiPtr>(m, "ReaderAscii")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream&>())
METH(read_event, ReaderAscii)
METH(failed, ReaderAscii)
METH(close, ReaderAscii)
;

py::class_<ReaderAsciiHepMC2, ReaderAsciiHepMC2Ptr>(m, "ReaderAsciiHepMC2")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream&>())
METH(read_event, ReaderAsciiHepMC2)
METH(failed, ReaderAsciiHepMC2)
METH(close, ReaderAsciiHepMC2)
;

py::class_<ReaderLHEF, ReaderLHEFPtr>(m, "ReaderLHEF")
.def(py::init<const std::string>(), "filename"_a)
//This will be enabled a bit later: .def(py::init<std::stringstream&>())
METH(read_event, ReaderLHEF)
METH(failed, ReaderLHEF)
METH(close, ReaderLHEF)
;

py::class_<ReaderHEPEVT, ReaderHEPEVTPtr>(m, "ReaderHEPEVT")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream&>())
.def("read_event", (bool (ReaderHEPEVT::*)(GenEvent&)) &ReaderHEPEVT::read_event)
.def("read_event", (bool (ReaderHEPEVT::*)(GenEvent&, bool)) &ReaderHEPEVT::read_event)
METH(failed, ReaderHEPEVT)
METH(close, ReaderHEPEVT)
;

py::class_<WriterAscii>(m, "WriterAscii")
.def(py::init<const std::string&, GenRunInfoPtr>(),
"filename"_a, "run"_a = nullptr)
.def(py::init<std::stringstream&, GenRunInfoPtr>(),
"ostringstream"_a, "run"_a = nullptr,
py::keep_alive<1, 2>())
METH(write_event, WriterAscii)
METH(write_run_info, WriterAscii)
METH(failed, WriterAscii)
METH(close, WriterAscii)
PROP(precision, WriterAscii)
;

py::class_<WriterAsciiHepMC2>(m, "WriterAsciiHepMC2")
.def(py::init<const std::string&, GenRunInfoPtr>(),
"filename"_a, "run"_a = nullptr)
.def(py::init<std::stringstream&, GenRunInfoPtr>(),
"ostringstream"_a, "run"_a = nullptr,
py::keep_alive<1, 2>())
METH(write_event, WriterAsciiHepMC2)
METH(write_run_info, WriterAsciiHepMC2)
METH(failed, WriterAsciiHepMC2)
METH(close, WriterAsciiHepMC2)
PROP(precision, WriterAsciiHepMC2)
;

py::class_<WriterHEPEVT>(m, "WriterHEPEVT")
.def(py::init<const std::string&>(),"filename"_a)
METH(write_event, WriterHEPEVT)
METH(failed, WriterHEPEVT)
METH(close, WriterHEPEVT)
;
void register_io(py::module &m) {

// this class is here to simplify unit testing of Readers and Writers
py::class_<std::stringstream>(m, "stringstream")
.def(py::init<>())
.def(py::init<std::string>())
.def("__str__", (std::string(std::stringstream::*)() const) &
std::stringstream::str) METH(flush, std::stringstream)
METH(write, std::stringstream) METH(read, std::stringstream);

py::class_<ReaderAscii, ReaderAsciiPtr>(m, "ReaderAscii")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream &>()) METH(read_event, ReaderAscii)
METH(failed, ReaderAscii) METH(close, ReaderAscii);

py::class_<ReaderAsciiHepMC2, ReaderAsciiHepMC2Ptr>(m, "ReaderAsciiHepMC2")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream &>()) METH(read_event, ReaderAsciiHepMC2)
METH(failed, ReaderAsciiHepMC2) METH(close, ReaderAsciiHepMC2);

py::class_<ReaderLHEF, ReaderLHEFPtr>(m, "ReaderLHEF")
.def(py::init<const std::string>(), "filename"_a)
// This will be enabled a bit later: .def(py::init<std::stringstream&>())
METH(read_event, ReaderLHEF) METH(failed, ReaderLHEF)
METH(close, ReaderLHEF);

py::class_<ReaderHEPEVT, ReaderHEPEVTPtr>(m, "ReaderHEPEVT")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream &>())
.def("read_event",
(bool(ReaderHEPEVT::*)(GenEvent &)) & ReaderHEPEVT::read_event)
.def("read_event",
(bool(ReaderHEPEVT::*)(GenEvent &, bool)) & ReaderHEPEVT::read_event)
METH(failed, ReaderHEPEVT) METH(close, ReaderHEPEVT);

py::class_<WriterAscii>(m, "WriterAscii")
.def(py::init<const std::string &, GenRunInfoPtr>(), "filename"_a,
"run"_a = nullptr)
.def(py::init<std::stringstream &, GenRunInfoPtr>(), "ostringstream"_a,
"run"_a = nullptr, py::keep_alive<1, 2>())
METH(write_event, WriterAscii) METH(write_run_info, WriterAscii)
METH(failed, WriterAscii) METH(close, WriterAscii)
PROP(precision, WriterAscii);

py::class_<WriterAsciiHepMC2>(m, "WriterAsciiHepMC2")
.def(py::init<const std::string &, GenRunInfoPtr>(), "filename"_a,
"run"_a = nullptr)
.def(py::init<std::stringstream &, GenRunInfoPtr>(), "ostringstream"_a,
"run"_a = nullptr, py::keep_alive<1, 2>())
METH(write_event, WriterAsciiHepMC2)
METH(write_run_info, WriterAsciiHepMC2)
METH(failed, WriterAsciiHepMC2) METH(close, WriterAsciiHepMC2)
PROP(precision, WriterAsciiHepMC2);

py::class_<WriterHEPEVT>(m, "WriterHEPEVT")
.def(py::init<const std::string &>(), "filename"_a)
METH(write_event, WriterHEPEVT) METH(failed, WriterHEPEVT)
METH(close, WriterHEPEVT);

#ifdef HEPMC3_ROOTIO

py::class_<ReaderRootTree, ReaderRootTreePtr>(m, "ReaderRootTree")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<const std::string, const std::string, const std::string>(), "filename"_a,"treename"_a,"branchname"_a)
METH(read_event, ReaderRootTree)
METH(failed, ReaderRootTree)
METH(close, ReaderRootTree)
;

py::class_<ReaderRoot, ReaderRootPtr>(m, "ReaderRoot")
.def(py::init<const std::string>(), "filename"_a)
METH(read_event, ReaderRoot)
METH(failed, ReaderRoot)
METH(close, ReaderRoot)
;
py::class_<ReaderRootTree, ReaderRootTreePtr>(m, "ReaderRootTree")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<const std::string, const std::string, const std::string>(),
"filename"_a, "treename"_a, "branchname"_a)
METH(read_event, ReaderRootTree) METH(failed, ReaderRootTree)
METH(close, ReaderRootTree);

#endif
py::class_<ReaderRoot, ReaderRootPtr>(m, "ReaderRoot")
.def(py::init<const std::string>(), "filename"_a)
METH(read_event, ReaderRoot) METH(failed, ReaderRoot)
METH(close, ReaderRoot);

#endif
}
13 changes: 0 additions & 13 deletions src/pyhepmc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import sys
import os
from pathlib import Path

module_dir = Path(__file__).parent
print(module_dir)
for p in module_dir.glob("*"):
print(p)
# windows only loads dlls from "trusted" locations
if sys.platform.startswith("win"):
os.add_dll_directory(module_dir)

from ._core import * # noqa
from ._io import ( # noqa
ReaderAscii,
Expand All @@ -19,7 +7,6 @@
WriterAscii,
WriterAsciiHepMC2,
WriterHEPEVT,
fill_genevent_from_hepevt,
pyhepmc_open as open,
)
from ._version import version as __version__ # noqa

0 comments on commit 8927657

Please sign in to comment.