EIC 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) 2017-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 
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 
21 using namespace ActsExamples;
22 
24 
26  return s_instance;
27 }
28 
30  : G4VUserPrimaryGeneratorAction(),
31  m_cfg(cfg),
32  m_particleGun(std::make_unique<G4ParticleGun>(1)) {
33  if (s_instance) {
34  throw std::logic_error(
35  "Attempted to duplicate the PrimaryGeneratorAction singleton");
36  } else {
37  s_instance = this;
38  }
39 
40  // default particle kinematic
41  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
42  G4ParticleDefinition* particle =
43  particleTable->FindParticle(m_cfg.particleName);
44  m_particleGun->SetParticleDefinition(particle);
45  m_particleGun->SetParticleEnergy(m_cfg.energy);
46  G4UnitDefinition::PrintUnitsTable();
47 
48  // set the random seeds
49  CLHEP::HepRandom::getTheEngine()->setSeed(m_cfg.randomSeed1,
51 }
52 
54  s_instance = nullptr;
55 }
56 
58  // this function is called at the begining of event
59  G4double phi =
60  m_cfg.phiRange.first +
61  G4UniformRand() * (m_cfg.phiRange.second - m_cfg.phiRange.first);
62  G4double theta = 0;
63  if (m_cfg.samplingVariable == "theta") {
64  G4double thetaMin = 2 * atan(exp(-m_cfg.etaRange.first));
65  G4double thetaMax = 2 * atan(exp(-m_cfg.etaRange.second));
66  theta = thetaMin + G4UniformRand() * (thetaMax - thetaMin);
67  } else if (m_cfg.samplingVariable == "eta") {
68  G4double eta =
69  m_cfg.etaRange.first +
70  G4UniformRand() * (m_cfg.etaRange.second - m_cfg.etaRange.first);
71  theta = 2 * atan(exp(-eta));
72  } else {
73  throw std::invalid_argument("Unknow sampling variable");
74  }
75  // build a direction
76  m_direction =
77  G4ThreeVector(cos(phi) * sin(theta), sin(phi) * sin(theta), cos(theta));
78  m_position =
79  G4ThreeVector(G4RandGauss::shoot(m_cfg.vertexPosX, m_cfg.vertexSigmaX),
80  G4RandGauss::shoot(m_cfg.vertexPosY, m_cfg.vertexSigmaY),
81  G4RandGauss::shoot(m_cfg.vertexPosZ, m_cfg.vertexSigmaZ));
82  // set to the particle gun and
83  m_particleGun->SetParticleMomentumDirection(m_direction);
84  m_particleGun->SetParticlePosition(m_position);
85  m_particleGun->GeneratePrimaryVertex(anEvent);
86 }