Skip to content

Commit

Permalink
Add test that run the overlap check on the main geometries.
Browse files Browse the repository at this point in the history
  • Loading branch information
paolafer committed Jan 9, 2024
1 parent 67568a9 commit cb05564
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 1 deletion.
237 changes: 237 additions & 0 deletions source/base/NexusExceptionHandler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
// ----------------------------------------------------------------------------
// nexus | NexusExceptionHandler.cc
//
// This class turns warnings into exceptions, to make the run end.
// It is useful in some tests, or for debugging.
//
// The NEXT Collaboration
// ----------------------------------------------------------------------------

#include <stdlib.h>

#include "NexusExceptionHandler.h"

#include <G4RunManager.hh>
#include <G4StateManager.hh>
#include <G4String.hh>
#include <G4ios.hh>
#include <G4EventManager.hh>
#include <G4Material.hh>
#include <G4ParticleDefinition.hh>
#include <G4RunManagerKernel.hh>
#include <G4Step.hh>
#include <G4StepPoint.hh>
#include <G4SteppingManager.hh>
#include <G4Track.hh>
#include <G4TrackingManager.hh>
#include <G4UnitsTable.hh>
#include <G4VPhysicalVolume.hh>
#include <G4VProcess.hh>

NexusExceptionHandler::NexusExceptionHandler() {}

NexusExceptionHandler::~NexusExceptionHandler() {}

G4bool NexusExceptionHandler::operator==(const NexusExceptionHandler& right) const
{
return (this == &right);
}

G4bool NexusExceptionHandler::operator!=(const NexusExceptionHandler& right) const
{
return (this != &right);
}

G4bool NexusExceptionHandler::Notify(const char* originOfException,
const char* exceptionCode,
G4ExceptionSeverity severity,
const char* description)
{
static const G4String es_banner =
"\n-------- EEEE ------- G4Exception-START -------- EEEE -------\n";
static const G4String ee_banner =
"\n-------- EEEE -------- G4Exception-END --------- EEEE -------\n";
static const G4String ws_banner =
"\n-------- WWWW ------- G4Exception-START -------- WWWW -------\n";
static const G4String we_banner =
"\n-------- WWWW -------- G4Exception-END --------- WWWW -------\n";
std::ostringstream message;
message << "*** G4Exception : " << exceptionCode << G4endl
<< " issued by : " << originOfException << G4endl << description
<< G4endl;
G4bool abortionForCoreDump = false;
G4ApplicationState aps = G4StateManager::GetStateManager()->GetCurrentState();
switch(severity)
{
case FatalException:
G4cerr << es_banner << message.str()
<< "*** Fatal Exception *** core dump ***" << G4endl;
DumpTrackInfo();
G4cerr << ee_banner << G4endl;
abortionForCoreDump = true;
break;
case FatalErrorInArgument:
G4cerr << es_banner << message.str()
<< "*** Fatal Error In Argument *** core dump ***" << G4endl;
DumpTrackInfo();
G4cerr << ee_banner << G4endl;
abortionForCoreDump = true;
break;
case RunMustBeAborted:
if(aps == G4State_GeomClosed || aps == G4State_EventProc)
{
G4cerr << es_banner << message.str() << "*** Run Must Be Aborted ***"
<< G4endl;
DumpTrackInfo();
G4cerr << ee_banner << G4endl;
G4RunManager::GetRunManager()->AbortRun(false);
}
abortionForCoreDump = false;
break;
case EventMustBeAborted:
if(aps == G4State_EventProc)
{
G4cerr << es_banner << message.str() << "*** Event Must Be Aborted ***"
<< G4endl;
DumpTrackInfo();
G4cerr << ee_banner << G4endl;
G4RunManager::GetRunManager()->AbortEvent();
}
abortionForCoreDump = false;
break;
default:
G4cerr << ws_banner << message.str()
<< "*** Fatal Exception *** core dump ***" << G4endl;
G4cerr << "*** This is a warning message that has been turned into exception. ***"
<< G4endl;
DumpTrackInfo();
G4cerr << we_banner << G4endl;
abortionForCoreDump = true;
break;
}
return abortionForCoreDump;
}

void NexusExceptionHandler::DumpTrackInfo()
{
const G4Track* theTrack = nullptr;
const G4Step* theStep = nullptr;
if (G4StateManager::GetStateManager()->GetCurrentState() == G4State_EventProc)
{
G4SteppingManager* steppingMgr = G4RunManagerKernel::GetRunManagerKernel()
->GetTrackingManager()
->GetSteppingManager();
theTrack = steppingMgr->GetfTrack();
theStep = steppingMgr->GetfStep();
}

if (theTrack == nullptr)
{
G4cerr << " **** Track information is not available at this moment"
<< G4endl;
}
else
{
G4cerr << "G4Track (" << theTrack
<< ") - track ID = " << theTrack->GetTrackID()
<< ", parent ID = " << theTrack->GetParentID() << G4endl;
G4cerr << " Particle type : "
<< theTrack->GetParticleDefinition()->GetParticleName();
if(theTrack->GetCreatorProcess())
{
G4cerr << " - creator process : "
<< theTrack->GetCreatorProcess()->GetProcessName()
<< ", creator model : " << theTrack->GetCreatorModelName()
<< G4endl;
}
else
{
G4cerr << " - creator process : not available" << G4endl;
}
G4cerr << " Kinetic energy : "
<< G4BestUnit(theTrack->GetKineticEnergy(), "Energy")
<< " - Momentum direction : " << theTrack->GetMomentumDirection()
<< G4endl;
}

if (theStep == nullptr)
{
G4cerr << " **** Step information is not available at this moment"
<< G4endl;
}
else
{
G4cerr << " Step length : "
<< G4BestUnit(theStep->GetStepLength(), "Length")
<< " - total energy deposit : "
<< G4BestUnit(theStep->GetTotalEnergyDeposit(), "Energy") << G4endl;
G4cerr << " Pre-step point : " << theStep->GetPreStepPoint()->GetPosition();
G4cerr << " - Physical volume : ";
if(theStep->GetPreStepPoint()->GetPhysicalVolume())
{
G4cerr << theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName();
if(theStep->GetPreStepPoint()->GetMaterial())
{
G4cerr << " (" << theStep->GetPreStepPoint()->GetMaterial()->GetName()
<< ")";
}
else
{
G4cerr << " (material not available)";
}
}
else
{
G4cerr << "not available";
}
G4cerr << G4endl;
if(theStep->GetPreStepPoint()->GetProcessDefinedStep())
{
G4cerr
<< " - defined by : "
<< theStep->GetPreStepPoint()->GetProcessDefinedStep()->GetProcessName()
<< " - step status : " << theStep->GetPreStepPoint()->GetStepStatus()
<< G4endl;
}
else
{
G4cerr << " - defined by : not available" << G4endl;
}
G4cerr << " Post-step point : "
<< theStep->GetPostStepPoint()->GetPosition();
G4cerr << " - Physical volume : ";
if(theStep->GetPostStepPoint()->GetPhysicalVolume())
{
G4cerr << theStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
if(theStep->GetPostStepPoint()->GetMaterial())
{
G4cerr << " (" << theStep->GetPostStepPoint()->GetMaterial()->GetName()
<< ")";
}
else
{
G4cerr << " (material not available)";
}
}
else
{
G4cerr << "not available";
}
G4cerr << G4endl;
if(theStep->GetPostStepPoint()->GetProcessDefinedStep())
{
G4cerr << " - defined by : "
<< theStep->GetPostStepPoint()
->GetProcessDefinedStep()
->GetProcessName()
<< " - step status : "
<< theStep->GetPostStepPoint()->GetStepStatus() << G4endl;
}
else
{
G4cerr << " - defined by : not available" << G4endl;
}
G4cerr << " *** Note: Step information might not be properly updated."
<< G4endl;
}
}
41 changes: 41 additions & 0 deletions source/base/NexusExceptionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// ----------------------------------------------------------------------------
// nexus | NexusExceptionHandler.h
//
// This class turns warnings into exceptions, to make the run end.
// It is useful in some tests, or for debugging.
//
// The NEXT Collaboration
// ----------------------------------------------------------------------------

#ifndef NexusExceptionHandler_h
#define NexusExceptionHandler_h

#include "globals.hh"
#include "G4ExceptionSeverity.hh"
#include "G4VExceptionHandler.hh"

class NexusExceptionHandler : public G4VExceptionHandler
{
public:

NexusExceptionHandler();
virtual ~NexusExceptionHandler();
G4bool operator==(const NexusExceptionHandler& right) const;
G4bool operator!=(const NexusExceptionHandler& right) const;

NexusExceptionHandler(const NexusExceptionHandler&) = delete;
NexusExceptionHandler& operator=(const NexusExceptionHandler&) = delete;

virtual G4bool Notify(const char* originOfException,
const char* exceptionCode, G4ExceptionSeverity severity,
const char* description);
// Will be invoked by G4StateManager when G4Exception occurs.
// If TRUE returned, core dump is generated, while if FALSE,
// the program execution continues.

private:

void DumpTrackInfo();
};

#endif
18 changes: 17 additions & 1 deletion source/nexus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// ----------------------------------------------------------------------------

#include "NexusApp.h"
#include "NexusExceptionHandler.h"

#include <G4StateManager.hh>
#include <G4UImanager.hh>
#include <G4UIExecutive.hh>
#include <G4VisExecutive.hh>
Expand Down Expand Up @@ -41,13 +43,15 @@ G4int main(int argc, char** argv)
if (argc < 2) PrintUsage();

G4bool batch = true;
G4bool overlap_check = false;
G4int nevents = 0;
G4int precision = -1;

static struct option long_options[] =
{
{"batch", no_argument, 0, 'b'},
{"interactive", no_argument, 0, 'i'},
{"overlaps", no_argument, 0, 'o'},
{"precision", required_argument, 0, 'p'},
{"nevents", required_argument, 0, 'n'},
{0, 0, 0, 0}
Expand All @@ -59,7 +63,7 @@ G4int main(int argc, char** argv)

// int option_index = 0;
opterr = 0;
c = getopt_long(argc, argv, "bip:n:", long_options, 0);
c = getopt_long(argc, argv, "biop:n:", long_options, 0);

if (c==-1) break; // Exit if we are done reading options

Expand All @@ -73,6 +77,10 @@ G4int main(int argc, char** argv)
batch = false;
break;

case 'o':
overlap_check = true;
break;

case 'p':
precision = atoi(optarg);
break;
Expand Down Expand Up @@ -107,11 +115,19 @@ G4int main(int argc, char** argv)

G4SteppingVerbose::UseBestUnit(precision);

if (overlap_check) {
G4StateManager::GetStateManager()->SetExceptionHandler(new NexusExceptionHandler());
}

NexusApp* app = new NexusApp(macro_filename);
app->Initialize();

G4UImanager* UI = G4UImanager::GetUIpointer();

if (overlap_check) {
UI->ApplyCommand("/geometry/test/run");
}

// if (seed < 0) CLHEP::HepRandom::setTheSeed(time(0));
// else CLHEP::HepRandom::setTheSeed(seed);

Expand Down
Loading

0 comments on commit cb05564

Please sign in to comment.