Skip to content

Commit

Permalink
Merge pull request cms-sw#40127 from civanch/fix_SD_crash
Browse files Browse the repository at this point in the history
Protection for negative kinetic energy in Geant4 tracking
  • Loading branch information
cmsbuild authored Nov 23, 2022
2 parents 0bcb76f + e56853c commit d2bfd2c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion SimG4Core/Application/src/ExceptionHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool ExceptionHandler::Notify(const char* exceptionOrigin,
// part of exception happens outside tracking loop
if (nullptr != track) {
ekin = track->GetKineticEnergy();
message << "\n"
message << "\n CMS info: "
<< "TrackID=" << track->GetTrackID() << " ParentID=" << track->GetParentID() << " "
<< track->GetParticleDefinition()->GetParticleName() << "; Ekin(MeV)=" << ekin / CLHEP::MeV
<< "; time(ns)=" << track->GetGlobalTime() / CLHEP::ns << "; status=" << track->GetTrackStatus()
Expand Down
10 changes: 10 additions & 0 deletions SimG4Core/Application/src/SteppingAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ void SteppingAction::UserSteppingAction(const G4Step* aStep) {
G4Track* theTrack = aStep->GetTrack();
TrackStatus tstat = (theTrack->GetTrackStatus() == fAlive) ? sAlive : sKilledByProcess;

if (theTrack->GetKineticEnergy() < 0.0) {
if (nWarnings < 5) {
++nWarnings;
edm::LogWarning("SimG4CoreApplication")
<< "Track #" << theTrack->GetTrackID() << " " << theTrack->GetDefinition()->GetParticleName()
<< " Ekin(MeV)= " << theTrack->GetKineticEnergy() / MeV;
}
theTrack->SetKineticEnergy(0.0);
}

const G4StepPoint* preStep = aStep->GetPreStepPoint();
const G4StepPoint* postStep = aStep->GetPostStepPoint();

Expand Down
11 changes: 10 additions & 1 deletion SimG4Core/SensitiveDetector/src/SensitiveDetector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,16 @@ void SensitiveDetector::setNames(const std::vector<std::string>& hnames) {
}

void SensitiveDetector::NaNTrap(const G4Step* aStep) const {
const G4Track* currentTrk = aStep->GetTrack();
G4Track* currentTrk = aStep->GetTrack();
double ekin = currentTrk->GetKineticEnergy();
if (ekin < 0.0) {
const G4VPhysicalVolume* pCurrentVol = aStep->GetPreStepPoint()->GetPhysicalVolume();
edm::LogWarning("SensitiveDetector") << "Negative kinetic energy Ekin(MeV)=" << ekin / CLHEP::MeV << " of "
<< currentTrk->GetDefinition()->GetParticleName()
<< " trackID= " << currentTrk->GetTrackID() << " inside "
<< pCurrentVol->GetName();
currentTrk->SetKineticEnergy(0.0);
}
const G4ThreeVector& currentPos = currentTrk->GetPosition();
double xyz = currentPos.x() + currentPos.y() + currentPos.z();
const G4ThreeVector& currentMom = currentTrk->GetMomentum();
Expand Down

0 comments on commit d2bfd2c

Please sign in to comment.