Skip to content

Commit

Permalink
fix: Move Geant4 actions into namespaces (#882)
Browse files Browse the repository at this point in the history
Previously these had the same symbol names between ExamplesGeant4 and ExamplesGeant4HepMC3. This is fine if these libraries are never linked together, but if that happens, it leads to crashes due to incompatible clashes. This moves both sets of classes into namespaces, to avoid clashes.

Fixes #874
  • Loading branch information
paulgessinger committed Jul 15, 2021
1 parent a99ae08 commit fa09c85
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GeantinoRecording final : public BareAlgorithm {
/// The number of tracks per event.
size_t tracksPerEvent = 0;
/// Configuration of the generator action
PrimaryGeneratorAction::Config generationConfig;
Geant4::PrimaryGeneratorAction::Config generationConfig;
};

GeantinoRecording(Config&& cfg, Acts::Logging::Level lvl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class G4ParticleGun;
class G4Event;

namespace ActsExamples {
namespace ActsExamples::Geant4 {

/// @class PrimaryGeneratorAction
///
Expand Down Expand Up @@ -91,4 +91,4 @@ class PrimaryGeneratorAction final : public G4VUserPrimaryGeneratorAction {
G4ThreeVector m_direction;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
4 changes: 3 additions & 1 deletion Examples/Algorithms/Geant4/src/EventAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "SteppingAction.hpp"

using namespace ActsExamples;
namespace ActsExamples::Geant4 {

EventAction* EventAction::s_instance = nullptr;

Expand Down Expand Up @@ -71,3 +71,5 @@ const std::vector<Acts::RecordedMaterialTrack>& EventAction::materialTracks()
const {
return m_materialTracks;
}

} // namespace ActsExamples::Geant4
4 changes: 2 additions & 2 deletions Examples/Algorithms/Geant4/src/EventAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <G4UserEventAction.hh>
#include <globals.hh>

namespace ActsExamples {
namespace ActsExamples::Geant4 {

class SteppingAction;

Expand Down Expand Up @@ -64,4 +64,4 @@ class EventAction final : public G4UserEventAction {
std::vector<Acts::RecordedMaterialTrack> m_materialTracks;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
1 change: 1 addition & 0 deletions Examples/Algorithms/Geant4/src/GeantinoRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "SteppingAction.hpp"

using namespace ActsExamples;
using namespace ActsExamples::Geant4;

GeantinoRecording::GeantinoRecording(GeantinoRecording::Config&& cfg,
Acts::Logging::Level lvl)
Expand Down
4 changes: 3 additions & 1 deletion Examples/Algorithms/Geant4/src/PrimaryGeneratorAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <G4UnitsTable.hh>
#include <Randomize.hh>

using namespace ActsExamples;
namespace ActsExamples::Geant4 {

PrimaryGeneratorAction* PrimaryGeneratorAction::s_instance = nullptr;

Expand Down Expand Up @@ -84,3 +84,5 @@ void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
m_particleGun->SetParticlePosition(m_position);
m_particleGun->GeneratePrimaryVertex(anEvent);
}

} // namespace ActsExamples::Geant4
4 changes: 3 additions & 1 deletion Examples/Algorithms/Geant4/src/RunAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "EventAction.hpp"

using namespace ActsExamples;
namespace ActsExamples::Geant4 {

RunAction* RunAction::s_instance = nullptr;

Expand Down Expand Up @@ -50,3 +50,5 @@ void RunAction::EndOfRunAction(const G4Run* aRun) {
<< "\n------------------------------------------------------------\n"
<< G4endl;
}

} // namespace ActsExamples::Geant4
4 changes: 2 additions & 2 deletions Examples/Algorithms/Geant4/src/RunAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class G4Run;

namespace ActsExamples {
namespace ActsExamples::Geant4 {

/// @class RunAction
///
Expand Down Expand Up @@ -45,4 +45,4 @@ class RunAction final : public G4UserRunAction {
static RunAction* s_instance;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
4 changes: 3 additions & 1 deletion Examples/Algorithms/Geant4/src/SteppingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <G4Material.hh>
#include <G4Step.hh>

using namespace ActsExamples;
namespace ActsExamples::Geant4 {

SteppingAction* SteppingAction::s_instance = nullptr;

Expand Down Expand Up @@ -136,3 +136,5 @@ void SteppingAction::clear() {
m_materialSteps.clear();
m_trackSteps.clear();
}

} // namespace ActsExamples::Geant4
4 changes: 2 additions & 2 deletions Examples/Algorithms/Geant4/src/SteppingAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <G4UserSteppingAction.hh>
#include <globals.hh>

namespace ActsExamples {
namespace ActsExamples::Geant4 {

/// @class SteppingAction
///
Expand Down Expand Up @@ -62,4 +62,4 @@ class SteppingAction final : public G4UserSteppingAction {
ActsExamples::SimHitContainer::sequence_type m_trackSteps;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#include <memory>
#include <mutex>

#include <G4VUserDetectorConstruction.hh>
#include <HepMC3/GenEvent.h>

#include "G4VUserDetectorConstruction.hh"

class G4RunManager;

namespace ActsExamples {
Expand Down
25 changes: 14 additions & 11 deletions Examples/Algorithms/Geant4HepMC/src/EventAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ void followOutgoingParticles(HepMC3::GenEvent& event,
}
} // namespace

ActsExamples::EventAction* ActsExamples::EventAction::s_instance = nullptr;
namespace ActsExamples::Geant4::HepMC3 {

ActsExamples::EventAction* ActsExamples::EventAction::instance() {
EventAction* EventAction::s_instance = nullptr;

EventAction* EventAction::instance() {
// Static acces function via G4RunManager
return s_instance;
}

ActsExamples::EventAction::EventAction(std::vector<std::string> processFilter)
EventAction::EventAction(std::vector<std::string> processFilter)
: G4UserEventAction(), m_processFilter(std::move(processFilter)) {
if (s_instance) {
throw std::logic_error("Attempted to duplicate a singleton");
Expand All @@ -136,17 +138,17 @@ ActsExamples::EventAction::EventAction(std::vector<std::string> processFilter)
}
}

ActsExamples::EventAction::~EventAction() {
EventAction::~EventAction() {
s_instance = nullptr;
}

void ActsExamples::EventAction::BeginOfEventAction(const G4Event*) {
void EventAction::BeginOfEventAction(const G4Event*) {
SteppingAction::instance()->clear();
m_event = HepMC3::GenEvent(HepMC3::Units::GEV, HepMC3::Units::MM);
m_event.add_beam_particle(std::make_shared<HepMC3::GenParticle>());
m_event = ::HepMC3::GenEvent(::HepMC3::Units::GEV, ::HepMC3::Units::MM);
m_event.add_beam_particle(std::make_shared<::HepMC3::GenParticle>());
}

void ActsExamples::EventAction::EndOfEventAction(const G4Event*) {
void EventAction::EndOfEventAction(const G4Event*) {
// Fast exit if the event is empty
if (m_event.vertices().empty()) {
return;
Expand All @@ -156,10 +158,11 @@ void ActsExamples::EventAction::EndOfEventAction(const G4Event*) {
followOutgoingParticles(m_event, currentVertex, m_processFilter);
}

void ActsExamples::EventAction::clear() {
void EventAction::clear() {
SteppingAction::instance()->clear();
}

HepMC3::GenEvent& ActsExamples::EventAction::event() {
::HepMC3::GenEvent& EventAction::event() {
return m_event;
}
}
} // namespace ActsExamples::Geant4::HepMC3
8 changes: 4 additions & 4 deletions Examples/Algorithms/Geant4HepMC/src/EventAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <HepMC3/GenEvent.h>
#include <globals.hh>

namespace ActsExamples {
namespace ActsExamples::Geant4::HepMC3 {

/// The EventAction class is the realization of the Geant4 class
/// G4UserEventAction and is writing out the collected RecordedMaterialTrack
Expand Down Expand Up @@ -44,14 +44,14 @@ class EventAction final : public G4UserEventAction {
void clear();

/// Getter of the created HepMC3 event
HepMC3::GenEvent& event();
::HepMC3::GenEvent& event();

private:
/// Instance of the EventAction
static EventAction* s_instance;
/// The current HepMC3 event
HepMC3::GenEvent m_event;
::HepMC3::GenEvent m_event;
/// List of processes that can be combined to a single vertex
std::vector<std::string> m_processFilter;
};
} // namespace ActsExamples
} // namespace ActsExamples::Geant4::HepMC3
19 changes: 12 additions & 7 deletions Examples/Algorithms/Geant4HepMC/src/EventRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <stdexcept>

#include <FTFP_BERT.hh>
#include <G4RunManager.hh>
#include <G4VUserDetectorConstruction.hh>
#include <HepMC3/GenParticle.h>

#include "EventAction.hpp"
Expand Down Expand Up @@ -46,13 +48,14 @@ ActsExamples::EventRecording::EventRecording(
/// Now set up the Geant4 simulation
m_runManager->SetUserInitialization(m_cfg.detectorConstruction.release());
m_runManager->SetUserInitialization(new FTFP_BERT);
m_runManager->SetUserAction(new ActsExamples::RunAction());
m_runManager->SetUserAction(new ActsExamples::Geant4::HepMC3::RunAction());
m_runManager->SetUserAction(
new ActsExamples::EventAction(m_cfg.processesCombine));
new ActsExamples::Geant4::HepMC3::EventAction(m_cfg.processesCombine));
m_runManager->SetUserAction(
new ActsExamples::PrimaryGeneratorAction(m_cfg.seed1, m_cfg.seed2));
new ActsExamples::Geant4::HepMC3::PrimaryGeneratorAction(m_cfg.seed1,
m_cfg.seed2));
m_runManager->SetUserAction(
new ActsExamples::SteppingAction(m_cfg.processesReject));
new ActsExamples::Geant4::HepMC3::SteppingAction(m_cfg.processesReject));
m_runManager->Initialize();
}

Expand All @@ -72,18 +75,20 @@ ActsExamples::ProcessCode ActsExamples::EventRecording::execute(

for (const auto& part : initialParticles) {
// Prepare the particle gun
ActsExamples::PrimaryGeneratorAction::instance()->prepareParticleGun(part);
ActsExamples::Geant4::HepMC3::PrimaryGeneratorAction::instance()
->prepareParticleGun(part);

// Begin with the simulation
m_runManager->BeamOn(1);

// Test if the event was aborted
if (SteppingAction::instance()->eventAborted()) {
if (Geant4::HepMC3::SteppingAction::instance()->eventAborted()) {
continue;
}

// Set event start time
HepMC3::GenEvent event = ActsExamples::EventAction::instance()->event();
HepMC3::GenEvent event =
ActsExamples::Geant4::HepMC3::EventAction::instance()->event();
HepMC3::FourVector shift(0., 0., 0., part.time() / Acts::UnitConstants::mm);
event.shift_position_by(shift);

Expand Down
20 changes: 11 additions & 9 deletions Examples/Algorithms/Geant4HepMC/src/PrimaryGeneratorAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
#include <G4UnitsTable.hh>
#include <Randomize.hh>

ActsExamples::PrimaryGeneratorAction*
ActsExamples::PrimaryGeneratorAction::s_instance = nullptr;
namespace ActsExamples::Geant4::HepMC3 {

ActsExamples::PrimaryGeneratorAction::PrimaryGeneratorAction(G4int randomSeed1,
G4int randomSeed2)
PrimaryGeneratorAction* PrimaryGeneratorAction::s_instance = nullptr;

PrimaryGeneratorAction::PrimaryGeneratorAction(G4int randomSeed1,
G4int randomSeed2)
: G4VUserPrimaryGeneratorAction(), m_particleGun(nullptr) {
// Configure the run
if (s_instance) {
Expand All @@ -43,17 +44,16 @@ ActsExamples::PrimaryGeneratorAction::PrimaryGeneratorAction(G4int randomSeed1,
CLHEP::HepRandom::getTheEngine()->setSeed(randomSeed1, randomSeed2);
}

ActsExamples::PrimaryGeneratorAction::~PrimaryGeneratorAction() {
PrimaryGeneratorAction::~PrimaryGeneratorAction() {
s_instance = nullptr;
}

ActsExamples::PrimaryGeneratorAction*
ActsExamples::PrimaryGeneratorAction::instance() {
PrimaryGeneratorAction* PrimaryGeneratorAction::instance() {
// Static acces function via G4RunManager
return s_instance;
}

void ActsExamples::PrimaryGeneratorAction::prepareParticleGun(
void PrimaryGeneratorAction::prepareParticleGun(
const ActsExamples::SimParticle& part) {
constexpr double convertLength = CLHEP::mm / Acts::UnitConstants::mm;
constexpr double convertEnergy = CLHEP::GeV / Acts::UnitConstants::GeV;
Expand All @@ -69,7 +69,9 @@ void ActsExamples::PrimaryGeneratorAction::prepareParticleGun(
m_particleGun->SetParticleMomentumDirection({dir[0], dir[1], dir[2]});
}

void ActsExamples::PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
// Produce the event
m_particleGun->GeneratePrimaryVertex(anEvent);
}

} // namespace ActsExamples::Geant4::HepMC3
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class G4Event;

namespace ActsExamples {

namespace Geant4::HepMC3 {

/// The PrimaryGeneratorAction is the implementation of the Geant4
/// class G4VUserPrimaryGeneratorAction. It generates a random direction
/// and shoots a particle.
Expand Down Expand Up @@ -51,5 +53,6 @@ class PrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction {
/// The Geant4 particle table
G4ParticleTable* m_particleTable;
};
} // namespace Geant4::HepMC3

} // namespace ActsExamples
4 changes: 3 additions & 1 deletion Examples/Algorithms/Geant4HepMC/src/RunAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "EventAction.hpp"

using namespace ActsExamples;
namespace ActsExamples::Geant4::HepMC3 {

RunAction* RunAction::s_instance = nullptr;

Expand All @@ -40,3 +40,5 @@ void RunAction::BeginOfRunAction(const G4Run* /*unused*/) {
}

void RunAction::EndOfRunAction(const G4Run* /*unused*/) {}

} // namespace ActsExamples::Geant4::HepMC3
4 changes: 2 additions & 2 deletions Examples/Algorithms/Geant4HepMC/src/RunAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class G4Run;

namespace ActsExamples {
namespace ActsExamples::Geant4::HepMC3 {

/// The RunAction class is the implementation of the
/// Geant4 class G4UserRunAction. It initiates the run
Expand All @@ -41,4 +41,4 @@ class RunAction final : public G4UserRunAction {
static RunAction* s_instance;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4::HepMC3

0 comments on commit fa09c85

Please sign in to comment.