File indexing completed on 2025-02-23 09:20:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include "DetectorConstruction.hh"
0034 #include "PrimaryGeneratorAction.hh"
0035 #include "PrimaryGeneratorMessenger.hh"
0036
0037 #include <G4ParticleTable.hh>
0038 #include <G4Event.hh>
0039 #include <G4SystemOfUnits.hh>
0040 #include <G4ParticleGun.hh>
0041 #include <G4GeneralParticleSource.hh>
0042 #include <Randomize.hh>
0043 #include <G4RunManager.hh>
0044 #include <G4PhysicalConstants.hh>
0045
0046 using namespace std;
0047
0048 PrimaryGeneratorAction::PrimaryGeneratorAction()
0049 : fGun(nullptr),
0050 gunRadius(0), gunMeanEnergy(0), gunStdEnergy(0),
0051 energy(0),
0052 particle(nullptr)
0053 {
0054 priGenMessenger = new PrimaryGeneratorMessenger(this);
0055 G4int n_particle = 1;
0056 fGun = new G4ParticleGun(n_particle);
0057 }
0058
0059 PrimaryGeneratorAction::~PrimaryGeneratorAction()
0060 {
0061 delete priGenMessenger;
0062 delete fGun;
0063 }
0064
0065 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0066 {
0067 GenerateFromRandom();
0068
0069 fGun -> SetParticlePosition(position);
0070 fGun -> SetParticleMomentumDirection((G4ParticleMomentum)direction);
0071 fGun -> SetParticleEnergy(energy * MeV);
0072 fGun -> SetParticleDefinition(particle);
0073
0074 fGun -> GeneratePrimaryVertex(anEvent);
0075 }
0076
0077 void PrimaryGeneratorAction::GenerateFromRandom()
0078 {
0079 DetectorConstruction* detector = (DetectorConstruction*)
0080 G4RunManager::GetRunManager() -> GetUserDetectorConstruction();
0081
0082 G4double zPos = -(detector -> GetAccOriginPosition()) - 5.*mm;
0083 G4double alpha, rho, phi, sinTheta, cosTheta;
0084
0085 sinTheta = G4RandGauss::shoot(0., 0.003);
0086 cosTheta = sqrt(1-sinTheta*sinTheta);
0087 phi = twopi * G4UniformRand();
0088 rho = gunRadius * G4UniformRand();
0089 alpha = twopi * G4UniformRand();
0090
0091 particle = G4ParticleTable::GetParticleTable()->FindParticle("e-");
0092 direction = {sinTheta * cos(phi), sinTheta * sin(phi), cosTheta};
0093 position = {rho * sin(alpha), rho * cos(alpha), zPos};
0094 energy = G4RandGauss::shoot(gunMeanEnergy, gunStdEnergy);
0095 }
0096
0097 void PrimaryGeneratorAction::SetGunRadius(G4double value)
0098 {
0099 gunRadius = value;
0100 }
0101
0102 void PrimaryGeneratorAction::SetGunMeanEnergy(G4double value)
0103 {
0104 gunMeanEnergy = value;
0105 }
0106
0107 void PrimaryGeneratorAction::SetGunStdEnergy(G4double value)
0108 {
0109 gunStdEnergy = value;
0110 }