File indexing completed on 2025-02-23 09:22:18
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
0044 #include "PrimaryGeneratorAction.hh"
0045
0046 #include "DetectorConstruction.hh"
0047 #include "PrimaryGeneratorMessenger.hh"
0048
0049 #include "G4ParticleDefinition.hh"
0050 #include "G4ParticleGun.hh"
0051 #include "G4ParticleTable.hh"
0052 #include "G4PhysicalConstants.hh"
0053 #include "G4SystemOfUnits.hh"
0054 #include "Randomize.hh"
0055
0056
0057
0058
0059 PrimaryGeneratorAction::PrimaryGeneratorAction(DetectorConstruction* pDet) : fDetector(pDet)
0060 {
0061 InitializeMe();
0062 }
0063
0064
0065
0066 void PrimaryGeneratorAction::InitializeMe()
0067 {
0068 fVerbose = fDetector->GetVerbose();
0069 fMessenger = new PrimaryGeneratorMessenger(this);
0070 fParticleGun = new G4ParticleGun();
0071 fCounter = 0;
0072 fX0 = 0.0;
0073 fY0 = 0.0;
0074 fZ0 = 0.0;
0075 fSigmaX = 1.5 * mm;
0076 fSigmaY = 1.5 * mm;
0077 fSigmaZ = 0.0;
0078 fSigmaE = 0.0;
0079 fRMax2 = 2.5 * 2.5 * mm * mm;
0080 fSigmaTheta = 0.0;
0081
0082 fMinCosTheta = 2.0;
0083 SetBeamEnergy(50.0 * MeV);
0084 fPosition = G4ThreeVector(fX0, fY0, fZ0);
0085 fDirection = G4ThreeVector(0.0, 0.0, 1.0);
0086 fGauss = true;
0087 }
0088
0089
0090
0091 PrimaryGeneratorAction::~PrimaryGeneratorAction()
0092 {
0093 delete fParticleGun;
0094 delete fMessenger;
0095 }
0096
0097
0098
0099 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0100 {
0101 fCounter++;
0102
0103
0104 G4double x = fX0;
0105 G4double y = fY0;
0106 G4double z = fDetector->GetGeneratorPosZ();
0107 do {
0108 if (0.0 < fSigmaX) {
0109 x = G4RandGauss::shoot(fX0, fSigmaX);
0110 }
0111 if (0.0 < fSigmaY) {
0112 y = G4RandGauss::shoot(fY0, fSigmaY);
0113 }
0114 } while (x * x + y * y > fRMax2);
0115
0116 fPosition = G4ThreeVector(x, y, z);
0117 fParticleGun->SetParticlePosition(fPosition);
0118
0119
0120 G4double ux = fDirection.x();
0121 G4double uy = fDirection.y();
0122 G4double uz = fDirection.z();
0123
0124
0125 if (1.0 > fMinCosTheta) {
0126 uz = fMinCosTheta + (1.0 - fMinCosTheta) * G4UniformRand();
0127 ux = std::sqrt((1.0 - uz) * (1.0 + uz));
0128 }
0129 else if (fSigmaTheta > 0.0) {
0130 ux = G4RandGauss::shoot(0.0, fSigmaTheta);
0131 uz = std::sqrt((1.0 - ux) * (1.0 + ux));
0132 }
0133
0134 G4double phi = twopi * G4UniformRand();
0135 uy = ux;
0136 ux *= std::cos(phi);
0137 uy *= std::sin(phi);
0138 fDirection = G4ThreeVector(ux, uy, uz);
0139
0140 fParticleGun->SetParticleMomentumDirection(fDirection);
0141
0142
0143 G4double kinEnergy = fEnergy;
0144
0145 if (fGauss == "flatE") {
0146 kinEnergy = fEnergy - fSigmaE + 2. * fSigmaE * G4UniformRand();
0147 }
0148 else if (0.0 < fSigmaE) {
0149 kinEnergy = fEnergy + G4RandGauss::shoot(0.0, fSigmaE);
0150 }
0151 fParticleGun->SetParticleEnergy(kinEnergy);
0152
0153 if (fVerbose > 0) {
0154 G4ParticleDefinition* particle = fParticleGun->GetParticleDefinition();
0155 G4String particleName = particle->GetParticleName();
0156 G4cout << "Event# " << fCounter << " Beam particle is generated by PrimaryGeneratorAction "
0157 << G4endl;
0158 G4cout << "ParticleName= " << particleName << " PDGcode= " << particle->GetPDGEncoding()
0159 << std::setprecision(5) << " KinEnergy(GeV)= " << fEnergy / GeV
0160 << " x(mm)= " << x / mm << " y(mm)= " << y / mm << " z(mm)= " << z / mm
0161 << " ux= " << ux << " uy= " << uy << " uz= " << uz << G4endl;
0162 }
0163
0164 fParticleGun->GeneratePrimaryVertex(anEvent);
0165 }
0166
0167
0168
0169 void PrimaryGeneratorAction::SetBeamEnergy(G4double val)
0170 {
0171 fEnergy = val;
0172 if (fEnergy < fDetector->GetMaxEnergy()) fDetector->SetMaxEnergy(fEnergy);
0173 }
0174
0175