Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:37

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "PrimaryGeneratorAction.hpp"
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 
0013 #include <stdexcept>
0014 
0015 #include <G4Event.hh>
0016 #include <G4ParticleDefinition.hh>
0017 #include <G4ParticleGun.hh>
0018 #include <G4RandomDirection.hh>
0019 #include <G4UnitsTable.hh>
0020 #include <Randomize.hh>
0021 
0022 namespace ActsExamples::Geant4::HepMC3 {
0023 
0024 PrimaryGeneratorAction* PrimaryGeneratorAction::s_instance = nullptr;
0025 
0026 PrimaryGeneratorAction::PrimaryGeneratorAction(G4int randomSeed1,
0027                                                G4int randomSeed2)
0028     : G4VUserPrimaryGeneratorAction(), m_particleGun(nullptr) {
0029   // Configure the run
0030   if (s_instance != nullptr) {
0031     throw std::logic_error("Attempted to duplicate a singleton");
0032   } else {
0033     s_instance = this;
0034   }
0035 
0036   // Configure the gun
0037   G4int nofParticles = 1;
0038   m_particleGun = std::make_unique<G4ParticleGun>(nofParticles);
0039 
0040   // Prepare the particle table
0041   m_particleTable = G4ParticleTable::GetParticleTable();
0042 
0043   // Set the random seeds
0044   CLHEP::HepRandom::getTheEngine()->setSeed(randomSeed1, randomSeed2);
0045 }
0046 
0047 PrimaryGeneratorAction::~PrimaryGeneratorAction() {
0048   s_instance = nullptr;
0049 }
0050 
0051 PrimaryGeneratorAction* PrimaryGeneratorAction::instance() {
0052   // Static access function via G4RunManager
0053   return s_instance;
0054 }
0055 
0056 void PrimaryGeneratorAction::prepareParticleGun(
0057     const ActsExamples::SimParticle& part) {
0058   constexpr double convertLength = CLHEP::mm / Acts::UnitConstants::mm;
0059   constexpr double convertEnergy = CLHEP::GeV / Acts::UnitConstants::GeV;
0060 
0061   // Particle type
0062   G4ParticleDefinition* particle = m_particleTable->FindParticle(part.pdg());
0063   m_particleGun->SetParticleDefinition(particle);
0064   // Particle properties
0065   const auto pos = part.position() * convertLength;
0066   const auto dir = part.direction();
0067   m_particleGun->SetParticlePosition({pos[0], pos[1], pos[2]});
0068   m_particleGun->SetParticleMomentum(part.absoluteMomentum() * convertEnergy);
0069   m_particleGun->SetParticleMomentumDirection({dir[0], dir[1], dir[2]});
0070 }
0071 
0072 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
0073   // Produce the event
0074   m_particleGun->GeneratePrimaryVertex(anEvent);
0075 }
0076 
0077 }  // namespace ActsExamples::Geant4::HepMC3