Commit e63e6c3c authored by Håkan Wennlöf's avatar Håkan Wennlöf
Browse files

Merge branch 'b/exceptions' into 'v2.3-stable'

[v2.3-stable] Rework Geant4 Exception Handlers

See merge request allpix-squared/allpix-squared!828
parents 7887851a 03bf9b3a
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -6,5 +6,6 @@ random_seed = 0

[GeometryBuilderGeant4]

#PASS (ERROR) [I:GeometryBuilderGeant4] Overlapping volumes detected.
#PASS (WARNING) [I:GeometryBuilderGeant4] Caught Geant4 exception GeomVol1002: Overlap with volume already placed
#FAIL FATAL
#LABEL coverage
+2 −11
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
#include <utility>

#include <G4GlobalConfig.hh>
#include <G4StateManager.hh>
#include <G4UImanager.hh>
#include <G4UIterminal.hh>
#include <G4Version.hh>
@@ -34,7 +33,6 @@
#include "core/config/exceptions.h"
#include "core/geometry/GeometryManager.hpp"
#include "core/utils/log.h"
#include "tools/geant4/G4ExceptionHandler.hpp"
#include "tools/geant4/G4LoggingDestination.hpp"
#include "tools/geant4/MTRunManager.hpp"
#include "tools/geant4/RunManager.hpp"
@@ -45,9 +43,6 @@ using namespace ROOT;
GeometryBuilderGeant4Module::GeometryBuilderGeant4Module(Configuration& config, Messenger*, GeometryManager* geo_manager)
    : Module(config), geo_manager_(geo_manager), run_manager_g4_(nullptr) {

    // Set exception handler for Geant4 exceptions:
    G4StateManager::GetStateManager()->SetExceptionHandler(new G4ExceptionHandler());

// Enable multithreading for Geant4 if it has been built with support for it:
#ifdef G4MULTITHREADED
    LOG(INFO) << "Detected Geant4 multithreading capabilities, enabling multithreading support";
@@ -124,20 +119,16 @@ void GeometryBuilderGeant4Module::initialize() {
    check_dataset_g4("G4NEUTRONXSDATA");
#endif

    // Suppress all stdout output due to a part in Geant4 where G4cout is not used
    SUPPRESS_STREAM(std::cout);

    // Create the G4 run manager. If multithreading was requested we use the custom run manager
    // that support calling BeamOn operations in parallel. Otherwise we use default manager.
    if(multithreadingEnabled()) {
        LOG(DEBUG) << "Making a multi-thread RunManager";
        run_manager_g4_ = std::make_unique<MTRunManager>();
    } else {
        LOG(DEBUG) << "Making a single-thread RunManager";
        run_manager_g4_ = std::make_unique<RunManager>();
    }

    // Release stdout again
    RELEASE_STREAM(std::cout);

    // Set the geometry construction to use
    run_manager_g4_->SetUserInitialization(geometry_construction_);

+11 −2
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
#define ALLPIX_GEANT4_EXCEPTIONS_H

#include "core/module/exceptions.h"
#include "core/utils/log.h"

#include <G4VExceptionHandler.hh>

@@ -25,8 +26,16 @@ namespace allpix {
     */
    class G4ExceptionHandler : public G4VExceptionHandler {
    public:
        G4bool Notify(const char*, const char* exceptionCode, G4ExceptionSeverity, const char* description) override {
            throw ModuleError("Caught Geant4 exception " + std::string(exceptionCode) + ": " + std::string(description));
        G4bool Notify(const char*, const char* code, G4ExceptionSeverity severity, const char* description) override {
            std::string message = "Caught Geant4 exception " + std::string(code) + ": " + std::string(description);
            if(severity == G4ExceptionSeverity::JustWarning) {
                LOG(WARNING) << message;
            } else {
                throw ModuleError(message);
            }

            // Continue program execution:
            return false;
        };
    };

+5 −0
Original line number Diff line number Diff line
@@ -11,8 +11,11 @@
#include "G4LoggingDestination.hpp"
#include "WorkerRunManager.hpp"

#include <G4StateManager.hh>
#include <G4UImanager.hh>

#include "tools/geant4/G4ExceptionHandler.hpp"

using namespace allpix;

G4ThreadLocal WorkerRunManager* MTRunManager::worker_run_manager_ = nullptr;
@@ -20,6 +23,8 @@ G4ThreadLocal WorkerRunManager* MTRunManager::worker_run_manager_ = nullptr;
MTRunManager::MTRunManager() {
    G4UImanager* ui_g4 = G4UImanager::GetUIpointer();
    ui_g4->SetCoutDestination(G4LoggingDestination::getInstance());
    // Set exception handler for Geant4 exceptions:
    G4StateManager::GetStateManager()->SetExceptionHandler(new G4ExceptionHandler());
}

void MTRunManager::Run(G4int n_event, uint64_t seed1, uint64_t seed2) { // NOLINT
+6 −0
Original line number Diff line number Diff line
@@ -12,12 +12,18 @@

#include <array>

#include <G4StateManager.hh>
#include <G4UImanager.hh>

#include "tools/geant4/G4ExceptionHandler.hpp"

using namespace allpix;

RunManager::RunManager() {
    G4UImanager* ui_g4 = G4UImanager::GetUIpointer();
    ui_g4->SetCoutDestination(G4LoggingDestination::getInstance());
    // Set exception handler for Geant4 exceptions:
    G4StateManager::GetStateManager()->SetExceptionHandler(new G4ExceptionHandler());
}

void RunManager::Run(G4int n_event, uint64_t seed1, uint64_t seed2) { // NOLINT
Loading