Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:14

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