ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MMPrimaryGeneratorAction.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MMPrimaryGeneratorAction.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 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 
11 #include <stdexcept>
12 
13 #include "G4Event.hh"
14 #include "G4ParticleDefinition.hh"
15 #include "G4ParticleGun.hh"
16 #include "G4ParticleTable.hh"
17 #include "G4RandomDirection.hh"
18 #include "G4UnitsTable.hh"
19 #include "Randomize.hh"
20 
23 
25  const G4String& particleName, G4double energy, G4int randomSeed1,
26  G4int randomSeed2)
27  : G4VUserPrimaryGeneratorAction(), fParticleGun(nullptr) {
28  // configure the run
29  if (fgInstance) {
30  throw std::logic_error("Attempted to duplicate a singleton");
31  } else {
32  fgInstance = this;
33  }
34  G4int nofParticles = 1;
35  fParticleGun = std::make_unique<G4ParticleGun>(nofParticles);
36 
37  // default particle kinematic
39  G4ParticleDefinition* particle = particleTable->FindParticle(particleName);
40  fParticleGun->SetParticleDefinition(particle);
41  fParticleGun->SetParticleEnergy(energy);
43 
44  // set the random seeds
45  CLHEP::HepRandom::getTheEngine()->setSeed(randomSeed1, randomSeed2);
46 }
47 
49  fgInstance = nullptr;
50 }
51 
54  // Static acces function via G4RunManager
55  return fgInstance;
56 }
57 
59  // this function is called at the begining of event
60  G4double phi = -M_PI + G4UniformRand() * 2. * M_PI;
62  // build a direction
63  m_direction =
64  G4ThreeVector(cos(phi) * sin(theta), sin(phi) * sin(theta), cos(theta));
65  m_position = G4ThreeVector(
66  0., 0., 0.);
67  // set to the particle gun and
68  fParticleGun->SetParticleMomentumDirection(m_direction);
69  fParticleGun->SetParticlePosition(m_position);
70 
71  fParticleGun->GeneratePrimaryVertex(anEvent);
72 }