File indexing completed on 2026-09-19 08:38:14
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
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 #include "PrimaryGeneratorAction.hh"
0044
0045 #include "G4SystemOfUnits.hh"
0046
0047 #include "CommandLineParser.hh"
0048
0049 #include "G4Box.hh"
0050 #include "G4Event.hh"
0051 #include "G4LogicalVolume.hh"
0052 #include "G4LogicalVolumeStore.hh"
0053 #include "G4Orb.hh"
0054 #include "G4ParticleDefinition.hh"
0055 #include "G4ParticleGun.hh"
0056 #include "G4PhysicalConstants.hh"
0057 #include "G4PhysicalVolumeStore.hh"
0058 #include "G4Proton.hh"
0059 #include "G4VPhysicalVolume.hh"
0060 #include "Randomize.hh"
0061
0062 using namespace G4DNAPARSER;
0063
0064
0065
0066 PrimaryGeneratorAction::PrimaryGeneratorAction() : G4VUserPrimaryGeneratorAction()
0067 {
0068 G4int n_particle = 1;
0069 fpParticleGun = new G4ParticleGun(n_particle);
0070
0071 G4ParticleDefinition* particle = G4Proton::Proton();
0072 fpParticleGun->SetParticleDefinition(particle);
0073
0074 fpParticleGun->SetParticleEnergy(10. * MeV);
0075 fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0076 fpParticleGun->SetParticlePosition(G4ThreeVector(0., 0., 0.));
0077 }
0078
0079
0080
0081 PrimaryGeneratorAction::~PrimaryGeneratorAction()
0082 {
0083 delete fpParticleGun;
0084 }
0085
0086
0087
0088 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0089 {
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 G4double mediumRadius = 0.;
0103 G4double boundingXHalfLength = 0.;
0104 G4double boundingYHalfLength = 0.;
0105 G4double boundingZHalfLength = 0.;
0106 G4LogicalVolume* mediumLV = G4LogicalVolumeStore::GetInstance()->GetVolume("Medium");
0107 G4LogicalVolume* boundingLV = G4LogicalVolumeStore::GetInstance()->GetVolume("BoundingSlice");
0108 G4Orb* mediumSphere = 0;
0109 G4Box* boundingSlice = 0;
0110 if (mediumLV && boundingLV) {
0111 mediumSphere = dynamic_cast<G4Orb*>(mediumLV->GetSolid());
0112 boundingSlice = dynamic_cast<G4Box*>(boundingLV->GetSolid());
0113 }
0114 if (mediumSphere && boundingSlice) {
0115 mediumRadius = mediumSphere->GetRadius();
0116 boundingXHalfLength = boundingSlice->GetXHalfLength();
0117 boundingYHalfLength = boundingSlice->GetYHalfLength();
0118 boundingZHalfLength = boundingSlice->GetZHalfLength();
0119
0120
0121
0122
0123 if (CommandLineParser::GetParser()->GetCommandIfActive("-sXY")) {
0124
0125 fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0126
0127 fpParticleGun->SetParticlePosition(G4ThreeVector(
0128 CLHEP::RandFlat::shoot(-boundingXHalfLength, boundingXHalfLength),
0129 CLHEP::RandFlat::shoot(-boundingYHalfLength, boundingYHalfLength), -mediumRadius));
0130 }
0131
0132
0133
0134
0135 else if (CommandLineParser::GetParser()->GetCommandIfActive("-dXY")) {
0136 fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0137 G4double x0, y0, z0;
0138 x0 = 100. * mm;
0139 y0 = 100. * mm;
0140 z0 = -mediumRadius;
0141 while (!(std::sqrt(x0 * x0 + y0 * y0) <= mediumRadius)) {
0142 x0 = CLHEP::RandFlat::shoot(-mediumRadius, mediumRadius);
0143 y0 = CLHEP::RandFlat::shoot(-mediumRadius, mediumRadius);
0144 }
0145 fpParticleGun->SetParticlePosition(G4ThreeVector(x0, y0, z0));
0146 }
0147
0148
0149
0150
0151 else {
0152 G4double cosTheta = 2. * G4UniformRand() - 1;
0153 G4double sinTheta = std::sqrt(1. - cosTheta * cosTheta);
0154 G4double phi = twopi * G4UniformRand();
0155 G4ThreeVector positionStart(mediumRadius * sinTheta * std::cos(phi),
0156 mediumRadius * sinTheta * std::sin(phi), mediumRadius * cosTheta);
0157 fpParticleGun->SetParticlePosition(positionStart);
0158
0159 G4ThreeVector positionDir(boundingXHalfLength * (2. * G4UniformRand() - 1),
0160 boundingYHalfLength * (2. * G4UniformRand() - 1),
0161 boundingZHalfLength * (2. * G4UniformRand() - 1));
0162 fpParticleGun->SetParticleMomentumDirection((positionDir - positionStart).unit());
0163
0164 fGunArea = 4. * pi * mediumRadius * mediumRadius;
0165 }
0166 }
0167 else {
0168 G4cerr << "Bounding slice volume not found!" << G4endl;
0169 G4cerr << "Default particle kinematic used" << G4endl;
0170 }
0171
0172 fpParticleGun->GeneratePrimaryVertex(anEvent);
0173 }