Commit f918f36c authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Register exception handler for GEant4

(cherry picked from commit 149d7cf6)
parent 9b6efda3
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#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"
@@ -44,6 +45,9 @@ 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";
+35 −0
Original line number Diff line number Diff line
/**
 * @file
 * @brief Set of Geant4 utilities for framework integration
 * @copyright Copyright (c) 2021 CERN and the Allpix Squared authors.
 * This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
 * In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an
 * Intergovernmental Organization or submit itself to any jurisdiction.
 */

#ifndef ALLPIX_GEANT4_EXCEPTIONS_H
#define ALLPIX_GEANT4_EXCEPTIONS_H

#include "core/module/exceptions.h"

#include <G4VExceptionHandler.hh>

namespace allpix {

    /**
     * @brief Exception handler for Geant4
     *
     * This class is registered with the G4StateManager and handles exceptions thrown in the Geant4 framework. It simply
     * constructs an \ref allpix::ModuleError exception and throws it, for the frameworkto take further action such as
     * terminating the run
     */
    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));
        };
    };

} // namespace allpix

#endif /* ALLPIX_GEANT4_EXCEPTIONS_H */