Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-23 16:00:29

0001 #include "SimG4Core/Application/interface/ExceptionHandler.h"
0002 #include "SimG4Core/Physics/interface/CMSG4TrackInterface.h"
0003 
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/Utilities/interface/Exception.h"
0006 
0007 #include "G4EventManager.hh"
0008 #include "G4TrackingManager.hh"
0009 #include "G4Track.hh"
0010 #include "globals.hh"
0011 #include <sstream>
0012 
0013 ExceptionHandler::ExceptionHandler(double th, bool tr) : m_eth(th), m_trace(tr) {}
0014 
0015 bool ExceptionHandler::Notify(const char* exceptionOrigin,
0016                               const char* exceptionCode,
0017                               G4ExceptionSeverity severity,
0018                               const char* description) {
0019   static const G4String es_banner = "\n-------- EEEE ------- G4Exception-START -------- EEEE -------\n";
0020   static const G4String ee_banner = "\n-------- EEEE -------- G4Exception-END --------- EEEE -------\n";
0021   static const G4String ws_banner = "\n-------- WWWW ------- G4Exception-START -------- WWWW -------\n";
0022   static const G4String we_banner = "\n-------- WWWW -------- G4Exception-END --------- WWWW -------\n";
0023 
0024   auto ig4 = CMSG4TrackInterface::instance();
0025   const G4Track* track = ig4->getCurrentTrack();
0026   int id = ig4->getThreadID();
0027   double ekin = m_eth;
0028 
0029   std::stringstream message;
0030   message << "*** G4Exception : " << exceptionCode << "\n"
0031           << "      issued by : " << exceptionOrigin << " in threadID=" << id << "\n"
0032           << description;
0033 
0034   // part of exception happens outside tracking loop
0035   if (nullptr != track) {
0036     ekin = track->GetKineticEnergy();
0037     message << "\n CMS info: "
0038             << "TrackID=" << track->GetTrackID() << " ParentID=" << track->GetParentID() << "  "
0039             << track->GetParticleDefinition()->GetParticleName() << "; Ekin(MeV)=" << ekin / CLHEP::MeV
0040             << "; time(ns)=" << track->GetGlobalTime() / CLHEP::ns << "; status=" << track->GetTrackStatus()
0041             << "\n   position(mm): " << track->GetPosition() << "; direction: " << track->GetMomentumDirection();
0042     const G4VPhysicalVolume* vol = track->GetVolume();
0043     if (nullptr != vol) {
0044       message << "\n   PhysicalVolume: " << vol->GetName() << ";";
0045       const G4LogicalVolume* lv = vol->GetLogicalVolume();
0046       if (nullptr != lv) {
0047         message << " material: " << lv->GetMaterial()->GetName();
0048       }
0049     }
0050     message << "\n   stepNumber=" << track->GetCurrentStepNumber()
0051             << "; stepLength(mm)=" << track->GetStepLength() / CLHEP::mm << "; weight=" << track->GetWeight();
0052     const G4VProcess* proc = track->GetCreatorProcess();
0053     if (nullptr != proc) {
0054       message << "; creatorProcess: " << proc->GetProcessName() << "; modelID=" << track->GetCreatorModelID();
0055     }
0056   }
0057   message << " \n";
0058 
0059   G4ExceptionSeverity localSeverity = severity;
0060   std::stringstream mescode;
0061   mescode << exceptionCode << "\n";
0062   G4String code;
0063   mescode >> code;
0064 
0065   // track should be killed, const_cast is allowed in this place
0066   if (ekin < m_eth && (code == "GeomNav0003" || code == "GeomField0003")) {
0067     localSeverity = JustWarning;
0068     const_cast<G4Track*>(track)->SetTrackStatus(fStopAndKill);
0069     ++m_number;
0070   }
0071 
0072   bool res = false;
0073   if (localSeverity == JustWarning) {
0074     if (m_number < 20)
0075       edm::LogWarning("SimG4CoreApplication")
0076           << ws_banner << message.str() << "*** This is just a warning message. ***" << we_banner;
0077   } else {
0078     edm::LogWarning("SimG4CoreApplication") << es_banner << message.str() << ee_banner;
0079     throw cms::Exception("Geant4 fatal exception");
0080     res = m_trace;
0081   }
0082   return res;
0083 }