Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrimaryGeneratorAction.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PrimaryGeneratorAction.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 
13 #include <stdexcept>
14 
15 #include <G4Event.hh>
16 #include <G4ParticleDefinition.hh>
17 #include <G4ParticleGun.hh>
18 #include <G4RandomDirection.hh>
19 #include <G4UnitsTable.hh>
20 #include <Randomize.hh>
21 
22 namespace ActsExamples::Geant4::HepMC3 {
23 
24 PrimaryGeneratorAction* PrimaryGeneratorAction::s_instance = nullptr;
25 
27  G4int randomSeed2)
28  : G4VUserPrimaryGeneratorAction(), m_particleGun(nullptr) {
29  // Configure the run
30  if (s_instance != nullptr) {
31  throw std::logic_error("Attempted to duplicate a singleton");
32  } else {
33  s_instance = this;
34  }
35 
36  // Configure the gun
37  G4int nofParticles = 1;
38  m_particleGun = std::make_unique<G4ParticleGun>(nofParticles);
39 
40  // Prepare the particle table
41  m_particleTable = G4ParticleTable::GetParticleTable();
42 
43  // Set the random seeds
44  CLHEP::HepRandom::getTheEngine()->setSeed(randomSeed1, randomSeed2);
45 }
46 
48  s_instance = nullptr;
49 }
50 
52  // Static access function via G4RunManager
53  return s_instance;
54 }
55 
57  const ActsExamples::SimParticle& part) {
58  constexpr double convertLength = CLHEP::mm / Acts::UnitConstants::mm;
59  constexpr double convertEnergy = CLHEP::GeV / Acts::UnitConstants::GeV;
60 
61  // Particle type
62  G4ParticleDefinition* particle = m_particleTable->FindParticle(part.pdg());
63  m_particleGun->SetParticleDefinition(particle);
64  // Particle properties
65  const auto pos = part.position() * convertLength;
66  const auto dir = part.direction();
67  m_particleGun->SetParticlePosition({pos[0], pos[1], pos[2]});
68  m_particleGun->SetParticleMomentum(part.absoluteMomentum() * convertEnergy);
69  m_particleGun->SetParticleMomentumDirection({dir[0], dir[1], dir[2]});
70 }
71 
73  // Produce the event
74  m_particleGun->GeneratePrimaryVertex(anEvent);
75 }
76 
77 } // namespace ActsExamples::Geant4::HepMC3