Skip to content

Commit

Permalink
feat: Python bindings for Geant4 manager and handle (#2500)
Browse files Browse the repository at this point in the history
This enables us to inspect the current handle of Geant4 and therefore adjust the log level from python which is useful while debugging.
  • Loading branch information
andiwand committed Oct 5, 2023
1 parent a3013a0 commit f2c21c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct Geant4Handle {
/// Set logging consistently across common Geant4 modules
///
/// Convenience method which calls into Geant4Manager
void tweekLogging(int level) const;
void tweakLogging(int level) const;
};

/// Allows easy instantiation of a Geant4Handle object
Expand All @@ -61,7 +61,9 @@ class Geant4Manager {
static Geant4Manager &instance();

/// Set logging consistently across common Geant4 modules
static void tweekLogging(G4RunManager &runManager, int level);
static void tweakLogging(G4RunManager &runManager, int level);

std::shared_ptr<Geant4Handle> currentHandle() const;

/// This can only be called once due to Geant4 limitations
std::shared_ptr<Geant4Handle> createHandle(int logLevel,
Expand Down
10 changes: 7 additions & 3 deletions Examples/Algorithms/Geant4/src/Geant4Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ Geant4Handle::Geant4Handle(int _logLevel,

Geant4Handle::~Geant4Handle() = default;

void Geant4Handle::tweekLogging(int level) const {
Geant4Manager::tweekLogging(*runManager, level);
void Geant4Handle::tweakLogging(int level) const {
Geant4Manager::tweakLogging(*runManager, level);
}

Geant4Manager& Geant4Manager::instance() {
static Geant4Manager manager;
return manager;
}

void Geant4Manager::tweekLogging(G4RunManager& runManager, int level) {
void Geant4Manager::tweakLogging(G4RunManager& runManager, int level) {
runManager.SetVerboseLevel(level);
G4EventManager::GetEventManager()->SetVerboseLevel(level);
G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(
Expand All @@ -77,6 +77,10 @@ void Geant4Manager::tweekLogging(G4RunManager& runManager, int level) {
#endif
}

std::shared_ptr<Geant4Handle> Geant4Manager::currentHandle() const {
return m_handle.lock();
}

std::shared_ptr<Geant4Handle> Geant4Manager::createHandle(
int logLevel, const std::string& physicsList) {
return createHandle(logLevel, createPhysicsList(physicsList), physicsList);
Expand Down
9 changes: 8 additions & 1 deletion Examples/Python/src/Geant4Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ PYBIND11_MODULE(ActsPythonBindingsGeant4, mod) {
std::shared_ptr<DetectorConstructionFactory>>(
mod, "DetectorConstructionFactory");

py::class_<Geant4Handle, std::shared_ptr<Geant4Handle>>(mod, "Geant4Handle");
py::class_<Geant4Manager, std::unique_ptr<Geant4Manager, py::nodelete>>(
mod, "Geant4Manager")
.def_static("instance", &Geant4Manager::instance,
py::return_value_policy::reference)
.def("currentHandle", &Geant4Manager::currentHandle);

py::class_<Geant4Handle, std::shared_ptr<Geant4Handle>>(mod, "Geant4Handle")
.def("tweakLogging", &Geant4Handle::tweakLogging);

{
using Algorithm = Geant4SimulationBase;
Expand Down

0 comments on commit f2c21c2

Please sign in to comment.