Skip to content

Commit

Permalink
Update binding and example output to v11.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
HaarigerHarald committed Dec 25, 2023
1 parent 9bc90bc commit 14487f8
Show file tree
Hide file tree
Showing 25 changed files with 156 additions and 174 deletions.
17 changes: 17 additions & 0 deletions source/analysis/pyG4VAnalysisManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <G4VNtupleManager.hh>
#include <G4VFileManager.hh>
#include <G4PlotManager.hh>
#include <G4Version.hh>

#include <tools/histo/hmpi>

Expand All @@ -30,10 +31,17 @@ void export_G4VAnalysisManager(py::module &m)
.def("CloseFile", &G4VAnalysisManager::CloseFile, py::arg("reset") = true,
py::call_guard<py::gil_scoped_release>())

.def("Reset", &G4VAnalysisManager::Reset)
.def("Clear", &G4VAnalysisManager::Clear)
.def("Merge", &G4VAnalysisManager::Merge) // TODO
.def("Plot", &G4VAnalysisManager::Plot)
.def("IsOpenFile", &G4VAnalysisManager::IsOpenFile)

#if G4VERSION_NUMBER >= 1120
.def("SetDefaultFileType", &G4VAnalysisManager::SetDefaultFileType, py::arg("value"))
.def("GetDefaultFileType", &G4VAnalysisManager::GetDefaultFileType)
#endif

.def("SetFileName", &G4VAnalysisManager::SetFileName)
.def("SetHistoDirectoryName", &G4VAnalysisManager::SetHistoDirectoryName)
.def("SetNtupleDirectoryName", &G4VAnalysisManager::SetNtupleDirectoryName)
Expand Down Expand Up @@ -600,6 +608,15 @@ void export_G4VAnalysisManager(py::module &m)
.def("GetP2YAxisIsLog", &G4VAnalysisManager::GetP2YAxisIsLog, py::arg("id"))
.def("GetP2ZAxisIsLog", &G4VAnalysisManager::GetP2ZAxisIsLog, py::arg("id"))

#if G4VERSION_NUMBER >= 1120
.def("DeleteH1", &G4VAnalysisManager::DeleteH1, py::arg("id"), py::arg("keepSetting") = false)
.def("DeleteH2", &G4VAnalysisManager::DeleteH2, py::arg("id"), py::arg("keepSetting") = false)
.def("DeleteH3", &G4VAnalysisManager::DeleteH3, py::arg("id"), py::arg("keepSetting") = false)
.def("DeleteP1", &G4VAnalysisManager::DeleteP1, py::arg("id"), py::arg("keepSetting") = false)
.def("DeleteP2", &G4VAnalysisManager::DeleteP2, py::arg("id"), py::arg("keepSetting") = false)
.def("DeleteNtuple", &G4VAnalysisManager::DeleteNtuple, py::arg("id"), py::arg("clear") = false)
#endif

.def("SetVerboseLevel", &G4VAnalysisManager::SetVerboseLevel, py::arg("verboseLevel"))
.def("GetVerboseLevel", &G4VAnalysisManager::GetVerboseLevel)
.def("GetType", &G4VAnalysisManager::GetType)
Expand Down
1 change: 0 additions & 1 deletion source/event/pyG4EventManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ void export_G4EventManager(py::module &m)
.def("ProcessOneEvent", py::overload_cast<G4TrackVector *, G4Event *>(&G4EventManager::ProcessOneEvent),
py::arg("trackVector"), py::arg("anEvent") = static_cast<G4Event *>(nullptr))

.def("SetNumberOfAdditionalWaitingStacks", &G4EventManager::SetNumberOfAdditionalWaitingStacks, py::arg("iAdd"))
.def(
"SetPrimaryTransformer",
[](G4EventManager &self, py::disown_ptr<G4PrimaryTransformer> tf) { self.SetPrimaryTransformer(tf); },
Expand Down
1 change: 1 addition & 0 deletions source/event/pyG4StackManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <G4VTrajectory.hh>
#include <G4Track.hh>
#include <G4ClassificationOfNewTrack.hh>
#include <G4Event.hh>

#include "typecast.hh"
#include "opaques.hh"
Expand Down
38 changes: 31 additions & 7 deletions source/geant4_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <pybind11/eval.h>

#include <G4coutDestination.hh>
#include <G4strstreambuf.hh>
#include <G4UImanager.hh>
#include <G4LogicalVolumeStore.hh>
#include <G4LogicalVolume.hh>
Expand All @@ -15,6 +14,10 @@
#include <G4Material.hh>
#include <G4RunManager.hh>
#include <G4SurfaceProperty.hh>
#include <G4Version.hh>
#if G4VERSION_NUMBER < 1120
#include <G4strstreambuf.hh>
#endif

#include <cstdlib>
#include <vector>
Expand All @@ -27,27 +30,40 @@ namespace py = pybind11;
class G4PyCoutDestination : public G4coutDestination {

public:
G4int ReceiveG4cout(const G4String &coutString) override
#if G4VERSION_NUMBER >= 1120
G4int ReceiveG4debug(const G4String &msg) override
{
py::gil_scoped_acquire gil;
auto pystdout = py::module_::import("sys").attr("stdout").attr("write");
pystdout(msg);
return 0;
}
#endif

G4int ReceiveG4cout(const G4String &msg) override
{
py::gil_scoped_acquire gil;
auto pystdout = py::module_::import("sys").attr("stdout").attr("write");
pystdout(coutString);
pystdout(msg);
return 0;
}

G4int ReceiveG4cerr(const G4String &cerrString) override
G4int ReceiveG4cerr(const G4String &msg) override
{
py::gil_scoped_acquire gil;
auto pystderr = py::module_::import("sys").attr("stderr").attr("write");
pystderr(cerrString);
pystderr(msg);
return 0;
}
};

class G4NullCoutDestination : public G4coutDestination {
public:
G4int ReceiveG4cout(const G4String &coutString) override { return 0; }
G4int ReceiveG4cerr(const G4String &cerrString) override { return 0; }
#if G4VERSION_NUMBER >= 1120
G4int ReceiveG4debug(const G4String &) override { return 0; }
#endif
G4int ReceiveG4cout(const G4String &) override { return 0; }
G4int ReceiveG4cerr(const G4String &) override { return 0; }
};

static bool endsWith(const std::string &str, const std::string &end)
Expand Down Expand Up @@ -101,8 +117,12 @@ PYBIND11_MODULE(geant4_pybind, m)

G4UImanager::GetUIpointer();
static G4PyCoutDestination pycout = G4PyCoutDestination();
#if G4VERSION_NUMBER >= 1120
G4iosSetDestination(&pycout);
#else
G4coutbuf.SetDestination(&pycout);
G4cerrbuf.SetDestination(&pycout);
#endif

py::module_ atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {
Expand All @@ -112,8 +132,12 @@ PYBIND11_MODULE(geant4_pybind, m)
delete G4RunManager::GetRunManager();

static G4NullCoutDestination nullcout = G4NullCoutDestination();
#if G4VERSION_NUMBER >= 1120
G4iosSetDestination(&nullcout);
#else
G4coutbuf.SetDestination(&nullcout);
G4cerrbuf.SetDestination(&nullcout);
#endif

// Delete everything before the interpreter shuts down to properly clean up python objects
G4LogicalVolumeStore::Clean();
Expand Down
9 changes: 9 additions & 0 deletions source/geometry/magneticfield/pyG4BFieldIntegrationDriver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <G4BFieldIntegrationDriver.hh>
#include <G4MagneticField.hh>
#include <G4MagIntegratorStepper.hh>
#include <G4Version.hh>

#include "typecast.hh"
#include "opaques.hh"
Expand Down Expand Up @@ -67,7 +69,14 @@ class PyG4BFieldIntegrationDriver : public G4BFieldIntegrationDriver, public py:

G4int GetVerboseLevel() const override { PYBIND11_OVERRIDE(G4int, G4BFieldIntegrationDriver, GetVerboseLevel, ); }

#if G4VERSION_NUMBER >= 1120
void OnComputeStep(const G4FieldTrack *track) override
{
PYBIND11_OVERRIDE(void, G4BFieldIntegrationDriver, OnComputeStep, track);
}
#else
void OnComputeStep() override { PYBIND11_OVERRIDE(void, G4BFieldIntegrationDriver, OnComputeStep, ); }
#endif

void OnStartTracking() override { PYBIND11_OVERRIDE(void, G4BFieldIntegrationDriver, OnStartTracking, ); }

Expand Down
9 changes: 9 additions & 0 deletions source/geometry/magneticfield/pyG4MagIntegratorDriver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <pybind11/stl.h>

#include <G4MagIntegratorDriver.hh>
#include <G4MagIntegratorStepper.hh>
#include <G4Version.hh>

#include "typecast.hh"
#include "opaques.hh"
Expand All @@ -20,7 +22,14 @@ class PyG4MagInt_Driver : public G4MagInt_Driver, public py::trampoline_self_lif

void OnStartTracking() override { PYBIND11_OVERRIDE(void, G4MagInt_Driver, OnStartTracking, ); }

#if G4VERSION_NUMBER >= 1120
void OnComputeStep(const G4FieldTrack *track) override
{
PYBIND11_OVERRIDE(void, G4MagInt_Driver, OnComputeStep, track);
}
#else
void OnComputeStep() override { PYBIND11_OVERRIDE(void, G4MagInt_Driver, OnComputeStep, ); }
#endif

G4bool DoesReIntegrate() const override { PYBIND11_OVERRIDE(G4bool, G4MagInt_Driver, DoesReIntegrate, ); }

Expand Down
9 changes: 9 additions & 0 deletions source/geometry/magneticfield/pyG4OldMagIntDriver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <pybind11/stl.h>

#include <G4OldMagIntDriver.hh>
#include <G4MagIntegratorStepper.hh>
#include <G4Version.hh>

#include "typecast.hh"
#include "opaques.hh"
Expand All @@ -20,7 +22,14 @@ class PyG4OldMagIntDriver : public G4OldMagIntDriver, public py::trampoline_self

void OnStartTracking() override { PYBIND11_OVERRIDE(void, G4OldMagIntDriver, OnStartTracking, ); }

#if G4VERSION_NUMBER >= 1120
void OnComputeStep(const G4FieldTrack *track) override
{
PYBIND11_OVERRIDE(void, G4OldMagIntDriver, OnComputeStep, track);
}
#else
void OnComputeStep() override { PYBIND11_OVERRIDE(void, G4OldMagIntDriver, OnComputeStep, ); }
#endif

G4bool DoesReIntegrate() const override { PYBIND11_OVERRIDE(G4bool, G4OldMagIntDriver, DoesReIntegrate, ); }

Expand Down
9 changes: 9 additions & 0 deletions source/geometry/magneticfield/pyG4VIntegrationDriver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <pybind11/stl.h>

#include <G4VIntegrationDriver.hh>
#include <G4MagIntegratorStepper.hh>
#include <G4Version.hh>

#include <limits>

Expand Down Expand Up @@ -41,7 +43,14 @@ class PyG4VIntegrationDriver : public G4VIntegrationDriver, public py::trampolin

G4int GetVerboseLevel() const override { PYBIND11_OVERRIDE_PURE(G4int, G4VIntegrationDriver, GetVerboseLevel, ); }

#if G4VERSION_NUMBER >= 1120
void OnComputeStep(const G4FieldTrack *track) override
{
PYBIND11_OVERRIDE_PURE(void, G4VIntegrationDriver, OnComputeStep, track);
}
#else
void OnComputeStep() override { PYBIND11_OVERRIDE_PURE(void, G4VIntegrationDriver, OnComputeStep, ); }
#endif

void OnStartTracking() override { PYBIND11_OVERRIDE_PURE(void, G4VIntegrationDriver, OnStartTracking, ); }

Expand Down
8 changes: 8 additions & 0 deletions source/geometry/navigation/pyG4ErrorPropagationNavigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <G4Polycone.hh>
#include <G4Polyhedra.hh>
#include <G4Hype.hh>
#include <G4Version.hh>

#include "typecast.hh"
#include "opaques.hh"
Expand Down Expand Up @@ -82,10 +83,17 @@ class PyG4ErrorPropagationNavigator : public G4ErrorPropagationNavigator, public
PYBIND11_OVERRIDE(void, G4ErrorPropagationNavigator, LocateGlobalPointWithinVolume, position);
}

#if G4VERSION_NUMBER >= 1120
G4TouchableHandle CreateTouchableHistoryHandle() const override
{
PYBIND11_OVERRIDE(G4TouchableHandle, G4ErrorPropagationNavigator, CreateTouchableHistoryHandle, );
}
#else
G4TouchableHistoryHandle CreateTouchableHistoryHandle() const override
{
PYBIND11_OVERRIDE(G4TouchableHistoryHandle, G4ErrorPropagationNavigator, CreateTouchableHistoryHandle, );
}
#endif

G4ThreeVector GetLocalExitNormal(G4bool *valid) override
{
Expand Down
8 changes: 8 additions & 0 deletions source/geometry/navigation/pyG4MultiNavigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <G4Polyhedra.hh>
#include <G4Hype.hh>
#include <G4TransportationManager.hh>
#include <G4Version.hh>

#include "typecast.hh"
#include "opaques.hh"
Expand Down Expand Up @@ -77,10 +78,17 @@ class PyG4MultiNavigator : public G4MultiNavigator, public py::trampoline_self_l
PYBIND11_OVERRIDE(G4double, G4MultiNavigator, ComputeSafety, globalpoint, pProposedMaxLength, keepState);
}

#if G4VERSION_NUMBER >= 1120
G4TouchableHandle CreateTouchableHistoryHandle() const override
{
PYBIND11_OVERRIDE(G4TouchableHandle, G4MultiNavigator, CreateTouchableHistoryHandle, );
}
#else
G4TouchableHistoryHandle CreateTouchableHistoryHandle() const override
{
PYBIND11_OVERRIDE(G4TouchableHistoryHandle, G4MultiNavigator, CreateTouchableHistoryHandle, );
}
#endif

G4ThreeVector GetLocalExitNormal(G4bool *obtained) override
{
Expand Down
10 changes: 8 additions & 2 deletions source/geometry/navigation/pyG4Navigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <G4Polycone.hh>
#include <G4Polyhedra.hh>
#include <G4Hype.hh>
#include <G4Version.hh>

#include <limits>

Expand Down Expand Up @@ -78,10 +79,17 @@ class PyG4Navigator : public G4Navigator, public py::trampoline_self_life_suppor
PYBIND11_OVERRIDE(G4double, G4Navigator, ComputeSafety, globalpoint, pProposedMaxLength, keepState);
}

#if G4VERSION_NUMBER >= 1120
G4TouchableHandle CreateTouchableHistoryHandle() const override
{
PYBIND11_OVERRIDE(G4TouchableHandle, G4Navigator, CreateTouchableHistoryHandle, );
}
#else
G4TouchableHistoryHandle CreateTouchableHistoryHandle() const override
{
PYBIND11_OVERRIDE(G4TouchableHistoryHandle, G4Navigator, CreateTouchableHistoryHandle, );
}
#endif

G4ThreeVector GetLocalExitNormal(G4bool *valid) override
{
Expand Down Expand Up @@ -141,8 +149,6 @@ void export_G4Navigator(py::module &m)

.def("GetWorldVolume", &G4Navigator::GetWorldVolume, py::return_value_policy::reference)
.def("SetWorldVolume", &G4Navigator::SetWorldVolume, py::arg("pWorld"))
.def("CreateGRSVolume", &G4Navigator::CreateGRSVolume, py::return_value_policy::reference)
.def("CreateGRSSolid", &G4Navigator::CreateGRSSolid, py::return_value_policy::reference)
.def("CreateTouchableHistory", py::overload_cast<>(&G4Navigator::CreateTouchableHistory, py::const_),
py::return_value_policy::reference)

Expand Down
6 changes: 4 additions & 2 deletions source/geometry/pymodG4geometry.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <G4Version.hh>

#include "typecast.hh"
#include "opaques.hh"

Expand Down Expand Up @@ -112,9 +114,9 @@ void export_modG4geometry(py::module &m)
EXPORT_G4HEADER(G4NavigationHistory, m);
EXPORT_G4HEADER(G4VoxelLimits, m);

#if G4VERSION_NUMBER < 1120
EXPORT_G4HEADER(G4VTouchable, m);
EXPORT_G4HEADER(G4GRSSolid, m);
EXPORT_G4HEADER(G4GRSVolume, m);
#endif
EXPORT_G4HEADER(G4TouchableHistory, m);

EXPORT_G4HEADER(G4VSolid, m);
Expand Down
7 changes: 0 additions & 7 deletions source/geometry/solids/specific/pyG4TwistTubsHypeSide.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ class PyG4TwistTubsHypeSide : public G4TwistTubsHypeSide, public py::trampoline_
PYBIND11_OVERRIDE(G4ThreeVector, G4TwistTubsHypeSide, GetNormal, xx, isGlobal);
}

EInside Inside(const G4ThreeVector &gp) override { PYBIND11_OVERRIDE(EInside, G4TwistTubsHypeSide, Inside, gp); }

G4double GetRhoAtPZ(const G4ThreeVector &p, G4bool isglobal) const override
{
PYBIND11_OVERRIDE(G4double, G4TwistTubsHypeSide, GetRhoAtPZ, p, isglobal);
}

G4ThreeVector SurfacePoint(G4double arg0, G4double arg1, G4bool isGlobal) override
{
PYBIND11_OVERRIDE(G4ThreeVector, G4TwistTubsHypeSide, SurfacePoint, arg0, arg1, isGlobal);
Expand Down
Loading

0 comments on commit 14487f8

Please sign in to comment.