File indexing completed on 2026-09-15 08:29:04
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 #include "PrimaryGeneratorAction.hh"
0030
0031 #include "DetectorConstruction.hh"
0032
0033 #include "G4Electron.hh"
0034 #include "G4Event.hh"
0035 #include "G4Gamma.hh"
0036 #include "G4ParticleDefinition.hh"
0037 #include "G4ParticleGun.hh"
0038 #include "G4ParticleTable.hh"
0039 #include "G4PhysicalConstants.hh"
0040 #include "G4RandomDirection.hh"
0041 #include "G4RunManager.hh"
0042 #include "G4StateManager.hh"
0043 #include "G4SystemOfUnits.hh"
0044 #include "Randomize.hh"
0045
0046
0047
0048 PrimaryGeneratorAction::PrimaryGeneratorAction() {
0049 fDetector = dynamic_cast<const DetectorConstruction *>(
0050 G4RunManager::GetRunManager()->GetUserDetectorConstruction());
0051 fParticleGun = std::make_unique<G4ParticleGun>(1);
0052 }
0053
0054
0055
0056 PrimaryGeneratorAction::~PrimaryGeneratorAction() {
0057 G4StateManager::GetStateManager()->DeregisterDependent(this);
0058 }
0059
0060
0061
0062 void PrimaryGeneratorAction::GeneratePrimaries(G4Event *anEvent) {
0063 const G4double R = fDetector->GetNPRadius();
0064 G4double Ryz = 9999;
0065
0066
0067
0068
0069 constexpr G4double scale = 1.;
0070
0071 G4double xpos = 0, ypos = 0, zpos = 0;
0072 while (!(Ryz < R)) {
0073 ypos = (2. * G4UniformRand() - 1.) * R;
0074 zpos = (2. * G4UniformRand() - 1.) * R;
0075 Ryz = std::sqrt(ypos * ypos + zpos * zpos);
0076 }
0077 xpos = std::sqrt(R * R - (ypos * ypos + zpos * zpos));
0078 fParticleGun->SetParticlePosition(G4ThreeVector(-xpos * scale,
0079 ypos * scale,
0080 zpos * scale));
0081 fParticleGun->SetParticleMomentumDirection(G4ThreeVector(1, 0, 0));
0082
0083 fParticleGun->GeneratePrimaryVertex(anEvent);
0084 }
0085
0086
0087
0088 G4bool PrimaryGeneratorAction::Notify(G4ApplicationState requestedState) {
0089 if (requestedState == G4State_Idle) {
0090 if (fParticleGun != nullptr) return true;
0091 const G4double R = fDetector->GetNPRadius();
0092 G4double Ryz = 9999;
0093
0094
0095
0096
0097 fParticleGun->SetParticleMomentumDirection(G4ThreeVector(-1, 0, 0));
0098
0099 while (!(Ryz < R)) {
0100 const G4double ypos = (2. * G4UniformRand() - 1.) * R;
0101 const G4double zpos = (2. * G4UniformRand() - 1.) * R;
0102 Ryz = std::sqrt(ypos * ypos + zpos * zpos);
0103 const G4double xpos = std::sqrt(R * R - (ypos * ypos + zpos * zpos));
0104 fParticleGun->SetParticlePosition(G4ThreeVector(-xpos, ypos, zpos));
0105 }
0106
0107
0108 }
0109
0110 return true;
0111 }