Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Geant4 geometry exception to be just a warning #35579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion SimG4Core/Application/interface/ExceptionHandler.h
Expand Up @@ -18,7 +18,7 @@

class ExceptionHandler : public G4VExceptionHandler {
public:
explicit ExceptionHandler();
explicit ExceptionHandler(double th);
~ExceptionHandler() override;

int operator==(const ExceptionHandler &right) const { return (this == &right); }
Expand All @@ -31,6 +31,9 @@ class ExceptionHandler : public G4VExceptionHandler {

ExceptionHandler(const ExceptionHandler &) = delete;
ExceptionHandler &operator=(const ExceptionHandler &right) = delete;

private:
double m_eth;
};

#endif
1 change: 1 addition & 0 deletions SimG4Core/Application/python/g4SimHits_cfi.py
Expand Up @@ -85,6 +85,7 @@
StorePhysicsTables = cms.untracked.bool(False),
RestorePhysicsTables = cms.untracked.bool(False),
UseParametrisedEMPhysics = cms.untracked.bool(True),
ThresholdForGeometryExceptions = cms.double(0.01), ## in GeV
CheckGeometry = cms.untracked.bool(False),
OnlySDs = cms.vstring('ZdcSensitiveDetector', 'TotemT2ScintSensitiveDetector', 'TotemSensitiveDetector', 'RomanPotSensitiveDetector', 'PLTSensitiveDetector', 'MuonSensitiveDetector', 'MtdSensitiveDetector', 'BCM1FSensitiveDetector', 'EcalSensitiveDetector', 'CTPPSSensitiveDetector', 'BSCSensitiveDetector', 'CTPPSDiamondSensitiveDetector', 'FP420SensitiveDetector', 'BHMSensitiveDetector', 'CastorSensitiveDetector', 'CaloTrkProcessing', 'HcalSensitiveDetector', 'TkAccumulatingSensitiveDetector'),
G4CheckOverlap = cms.untracked.PSet(
Expand Down
37 changes: 35 additions & 2 deletions SimG4Core/Application/src/ExceptionHandler.cc
Expand Up @@ -3,10 +3,13 @@

#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "G4EventManager.hh"
#include "G4TrackingManager.hh"
#include "G4Track.hh"
#include "globals.hh"
#include <sstream>

ExceptionHandler::ExceptionHandler() {}
ExceptionHandler::ExceptionHandler(double th) : m_eth(th) {}

ExceptionHandler::~ExceptionHandler() {}

Expand All @@ -19,13 +22,43 @@ bool ExceptionHandler::Notify(const char* exceptionOrigin,
static const G4String ws_banner = "\n-------- WWWW ------- G4Exception-START -------- WWWW -------\n";
static const G4String we_banner = "\n-------- WWWW -------- G4Exception-END --------- WWWW -------\n";

const G4Track* track = G4EventManager::GetEventManager()->GetTrackingManager()->GetTrack();
double ekin = m_eth;

std::stringstream message;
message << "*** G4Exception : " << exceptionCode << "\n"
<< " issued by : " << exceptionOrigin << "\n"
<< description;

// part of exception happens outside tracking loop
if (nullptr != track) {
ekin = track->GetKineticEnergy();
message << "\n"
<< "TrackID=" << track->GetTrackID() << " ParentID=" << track->GetParentID() << " "
<< track->GetParticleDefinition()->GetParticleName() << "; Ekin(MeV)=" << ekin / CLHEP::MeV
<< "; time(ns)=" << track->GetGlobalTime() / CLHEP::ns << "; status=" << track->GetTrackStatus()
<< "\n position(mm): " << track->GetPosition() << "; direction: " << track->GetMomentumDirection();
const G4VPhysicalVolume* vol = track->GetVolume();
if (nullptr != vol) {
message << "\n PhysicalVolume: " << vol->GetName() << "; material: " << track->GetMaterial()->GetName();
}
message << "\n stepNumber=" << track->GetCurrentStepNumber()
<< "; stepLength(mm)=" << track->GetStepLength() / CLHEP::mm << "; weight=" << track->GetWeight();
const G4VProcess* proc = track->GetCreatorProcess();
if (nullptr != proc) {
message << "; creatorProcess: " << proc->GetProcessName() << "; modelID=" << track->GetCreatorModelID();
}
}
message << "\n";

G4ExceptionSeverity localSeverity = severity;
G4String code = G4String(*exceptionCode);
if (ekin < m_eth && code == "GeomNav0003") {
localSeverity = JustWarning;
}

std::stringstream ss;
switch (severity) {
switch (localSeverity) {
case FatalException:
case FatalErrorInArgument:
case RunMustBeAborted:
Expand Down
3 changes: 2 additions & 1 deletion SimG4Core/Application/src/RunManagerMT.cc
Expand Up @@ -85,7 +85,8 @@ RunManagerMT::RunManagerMT(edm::ParameterSet const& p)
m_currentRun = nullptr;

m_kernel = new G4MTRunManagerKernel();
G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler());
double th = p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler(th));
m_check = p.getUntrackedParameter<bool>("CheckGeometry", false);
}

Expand Down
3 changes: 2 additions & 1 deletion SimG4Core/Application/src/RunManagerMTWorker.cc
Expand Up @@ -300,7 +300,8 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm:
}

// Define G4 exception handler
G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler());
double th = m_p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler(th));

// Set the geometry for the worker, share from master
auto worldPV = runManagerMaster->world().GetWorldVolume();
Expand Down