Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ExceptionHandler.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4ExceptionHandler implementation
27//
28// Author: M.Asai - August 2002
29// --------------------------------------------------------------------
30
31#include "G4ExceptionHandler.hh"
33
34#include "G4EventManager.hh"
35#include "G4Material.hh"
37#include "G4RunManager.hh"
38#include "G4RunManagerKernel.hh"
39#include "G4StateManager.hh"
40#include "G4Step.hh"
41#include "G4StepPoint.hh"
42#include "G4SteppingManager.hh"
43#include "G4String.hh"
44#include "G4Track.hh"
45#include "G4TrackingManager.hh"
46#include "G4UnitsTable.hh"
47#include "G4VPhysicalVolume.hh"
48#include "G4VProcess.hh"
49#include "G4ios.hh"
50
51#include <cstdlib>
52
53// --------------------------------------------------------------------
56
57// --------------------------------------------------------------------
60
61// --------------------------------------------------------------------
63{
64 return (this == &right);
65}
66
67// --------------------------------------------------------------------
69{
70 return (this != &right);
71}
72
73// --------------------------------------------------------------------
74G4bool G4ExceptionHandler::Notify(const char* originOfException, const char* exceptionCode,
75 G4ExceptionSeverity severity, const char* description)
76{
77 static const G4String es_banner =
78 "\n-------- EEEE ------- G4Exception-START -------- EEEE -------\n";
79 static const G4String ee_banner =
80 "\n-------- EEEE -------- G4Exception-END --------- EEEE -------\n";
81 static const G4String ws_banner =
82 "\n-------- WWWW ------- G4Exception-START -------- WWWW -------\n";
83 static const G4String we_banner =
84 "\n-------- WWWW -------- G4Exception-END --------- WWWW -------\n";
85 std::ostringstream message;
86 message << "*** G4Exception : " << exceptionCode << G4endl
87 << " issued by : " << originOfException << G4endl << description << G4endl;
88 G4bool abortionForCoreDump = false;
90 switch (severity) {
91 case FatalException:
92 G4cerr << es_banner << message.str() << "*** Fatal Exception *** core dump ***" << G4endl;
93 DumpTrackInfo();
94 G4cerr << ee_banner << G4endl;
95 abortionForCoreDump = true;
96 break;
98 G4cerr << es_banner << message.str() << "*** Fatal Error In Argument *** core dump ***"
99 << G4endl;
100 DumpTrackInfo();
101 G4cerr << ee_banner << G4endl;
102 abortionForCoreDump = true;
103 break;
104 case RunMustBeAborted:
105 if (aps == G4State_GeomClosed || aps == G4State_EventProc) {
106 G4cerr << es_banner << message.str() << "*** Run Must Be Aborted ***" << G4endl;
107 DumpTrackInfo();
108 G4cerr << ee_banner << G4endl;
110 }
111 abortionForCoreDump = false;
112 break;
114 if (aps == G4State_EventProc) {
115 G4cerr << es_banner << message.str() << "*** Event Must Be Aborted ***" << G4endl;
116 DumpTrackInfo();
117 G4cerr << ee_banner << G4endl;
119 }
120 abortionForCoreDump = false;
121 break;
122 case JustWarning:
123 if(IfPrint(exceptionCode))
124 {
125 std::ostringstream wmessage;
126 wmessage << "*** G4Exception : " << exceptionCode << G4endl
127 << " issued by : " << originOfException << G4endl << description << G4endl;
128 G4cout << ws_banner << wmessage.str() << "*** This is just a warning message. ***" << we_banner
129 << G4endl;
130 }
131 abortionForCoreDump = false;
132 break;
133 default:
134 abortionForCoreDump = false;
135 break;
136 }
137 return abortionForCoreDump;
138}
139
140// --------------------------------------------------------------------
141void G4ExceptionHandler::DumpTrackInfo()
142{
143 const G4Track* theTrack = nullptr;
144 const G4Step* theStep = nullptr;
145 if (G4StateManager::GetStateManager()->GetCurrentState() == G4State_EventProc) {
146 G4SteppingManager* steppingMgr =
148 theTrack = steppingMgr->GetfTrack();
149 theStep = steppingMgr->GetfStep();
150 }
151
152 if (theTrack == nullptr) {
153 G4cerr << " **** Track information is not available at this moment" << G4endl;
154 }
155 else {
156 G4cerr << "G4Track (" << theTrack << ") - track ID = " << theTrack->GetTrackID()
157 << ", parent ID = " << theTrack->GetParentID() << G4endl;
158 G4cerr << " Particle type : " << theTrack->GetParticleDefinition()->GetParticleName();
159 if (theTrack->GetCreatorProcess() != nullptr) {
160 G4cerr << " - creator process : " << theTrack->GetCreatorProcess()->GetProcessName()
161 << ", creator model : " << theTrack->GetCreatorModelName() << G4endl;
162 }
163 else {
164 G4cerr << " - creator process : not available" << G4endl;
165 }
166 G4cerr << " Kinetic energy : " << G4BestUnit(theTrack->GetKineticEnergy(), "Energy")
167 << " - Momentum direction : " << theTrack->GetMomentumDirection() << G4endl;
168 }
169
170 if (theStep == nullptr) {
171 G4cerr << " **** Step information is not available at this moment" << G4endl;
172 }
173 else {
174 G4cerr << " Step length : " << G4BestUnit(theStep->GetStepLength(), "Length")
175 << " - total energy deposit : " << G4BestUnit(theStep->GetTotalEnergyDeposit(), "Energy")
176 << G4endl;
177 G4cerr << " Pre-step point : " << theStep->GetPreStepPoint()->GetPosition();
178 G4cerr << " - Physical volume : ";
179 if (theStep->GetPreStepPoint()->GetPhysicalVolume() != nullptr) {
181 if (theStep->GetPreStepPoint()->GetMaterial() != nullptr) {
182 G4cerr << " (" << theStep->GetPreStepPoint()->GetMaterial()->GetName() << ")";
183 }
184 else {
185 G4cerr << " (material not available)";
186 }
187 }
188 else {
189 G4cerr << "not available";
190 }
191 G4cerr << G4endl;
192 if (theStep->GetPreStepPoint()->GetProcessDefinedStep() != nullptr) {
193 G4cerr << " - defined by : "
195 << " - step status : " << theStep->GetPreStepPoint()->GetStepStatus() << G4endl;
196 }
197 else {
198 G4cerr << " - defined by : not available" << G4endl;
199 }
200 G4cerr << " Post-step point : " << theStep->GetPostStepPoint()->GetPosition();
201 G4cerr << " - Physical volume : ";
202 if (theStep->GetPostStepPoint()->GetPhysicalVolume() != nullptr) {
204 if (theStep->GetPostStepPoint()->GetMaterial() != nullptr) {
205 G4cerr << " (" << theStep->GetPostStepPoint()->GetMaterial()->GetName() << ")";
206 }
207 else {
208 G4cerr << " (material not available)";
209 }
210 }
211 else {
212 G4cerr << "not available";
213 }
214 G4cerr << G4endl;
215 if (theStep->GetPostStepPoint()->GetProcessDefinedStep() != nullptr) {
216 G4cerr << " - defined by : "
218 << " - step status : " << theStep->GetPostStepPoint()->GetStepStatus() << G4endl;
219 }
220 else {
221 G4cerr << " - defined by : not available" << G4endl;
222 }
223 G4cerr << " *** Note: Step information might not be properly updated." << G4endl;
224 }
225}
226
227// --------------------------------------------------------------------
228G4bool G4ExceptionHandler::IfPrint(const char* errCode)
229{
230 static const G4String ws_banner =
231 "\n-------- WWWW ------- G4Exception-START -------- WWWW -------\n";
232 static const G4String we_banner =
233 "\n-------- WWWW -------- G4Exception-END --------- WWWW -------\n";
234 auto ec = fWarnCount.find(errCode);
235 if(ec!=fWarnCount.end())
236 {
237 if(ec->second>0)
238 {
239 ec->second--;
240 if(ec->second==0)
241 {
242 std::ostringstream wmessage;
243 wmessage << "*** G4Exception " << errCode << " has reached the maximum number." << G4endl
244 << "This warning message won't be displayed any longer.";
245 G4cout << ws_banner << wmessage.str() << we_banner
246 << G4endl;
247 }
248 }
249 else
250 { return false; }
251 }
252 if(fTotalWarnCount>0)
253 {
254 fTotalWarnCount--;
255 if(fTotalWarnCount==0)
256 {
257 std::ostringstream wmessage;
258 wmessage << "*** G4Exception warning messages reached the maximum number." << G4endl
259 << "Warning messages won't be displayed any longer.";
260 G4cout << ws_banner << wmessage.str() << we_banner
261 << G4endl;
262 }
263 return true;
264 }
265 return false;
266}
267
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
G4ExceptionSeverity
@ JustWarning
@ FatalException
@ FatalErrorInArgument
@ RunMustBeAborted
@ EventMustBeAborted
#define G4BestUnit(a, b)
bool G4bool
Definition G4Types.hh:86
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
G4bool operator==(const G4ExceptionHandler &right) const
G4bool operator!=(const G4ExceptionHandler &right) const
G4bool Notify(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description) override
const G4String & GetName() const
const G4String & GetParticleName() const
static G4RunManagerKernel * GetRunManagerKernel()
G4TrackingManager * GetTrackingManager() const
virtual void AbortRun(G4bool softAbort=false)
static G4RunManager * GetRunManager()
virtual void AbortEvent()
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
G4StepStatus GetStepStatus() const
const G4VProcess * GetProcessDefinedStep() const
G4Material * GetMaterial() const
const G4ThreeVector & GetPosition() const
G4VPhysicalVolume * GetPhysicalVolume() const
G4StepPoint * GetPreStepPoint() const
G4double GetStepLength() const
G4double GetTotalEnergyDeposit() const
G4StepPoint * GetPostStepPoint() const
G4int GetTrackID() const
const G4ParticleDefinition * GetParticleDefinition() const
const G4VProcess * GetCreatorProcess() const
const G4String GetCreatorModelName() const
const G4ThreeVector & GetMomentumDirection() const
G4double GetKineticEnergy() const
G4int GetParentID() const
G4SteppingManager * GetSteppingManager() const
const G4String & GetName() const
const G4String & GetProcessName() const