File indexing completed on 2026-04-27 07:32:55
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 "G4GenericMessenger.hh"
0032 #include "G4ParticleDefinition.hh"
0033 #include "G4ParticleGun.hh"
0034 #include "G4ParticleTable.hh"
0035 #include "G4SystemOfUnits.hh"
0036 #include "G4ThreeVector.hh"
0037 #include "Randomize.hh"
0038
0039 namespace B5
0040 {
0041
0042
0043
0044 PrimaryGeneratorAction::PrimaryGeneratorAction()
0045 {
0046 G4int nofParticles = 1;
0047 fParticleGun = new G4ParticleGun(nofParticles);
0048
0049 auto particleTable = G4ParticleTable::GetParticleTable();
0050 fPositron = particleTable->FindParticle("e+");
0051 fMuon = particleTable->FindParticle("mu+");
0052 fPion = particleTable->FindParticle("pi+");
0053 fKaon = particleTable->FindParticle("kaon+");
0054 fProton = particleTable->FindParticle("proton");
0055
0056
0057 fParticleGun->SetParticlePosition(G4ThreeVector(0., 0., -8. * m));
0058 fParticleGun->SetParticleDefinition(fPositron);
0059
0060
0061 DefineCommands();
0062 }
0063
0064
0065
0066 PrimaryGeneratorAction::~PrimaryGeneratorAction()
0067 {
0068 delete fParticleGun;
0069 delete fMessenger;
0070 }
0071
0072
0073
0074 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* event)
0075 {
0076 G4ParticleDefinition* particle;
0077 if (fRandomizePrimary) {
0078 auto i = (int)(5. * G4UniformRand());
0079 switch (i) {
0080 case 0:
0081 particle = fPositron;
0082 break;
0083 case 1:
0084 particle = fMuon;
0085 break;
0086 case 2:
0087 particle = fPion;
0088 break;
0089 case 3:
0090 particle = fKaon;
0091 break;
0092 default:
0093 particle = fProton;
0094 break;
0095 }
0096 fParticleGun->SetParticleDefinition(particle);
0097 }
0098 else {
0099 particle = fParticleGun->GetParticleDefinition();
0100 }
0101
0102 auto pp = fMomentum + (G4UniformRand() - 0.5) * fSigmaMomentum;
0103 auto mass = particle->GetPDGMass();
0104 auto ekin = std::sqrt(pp * pp + mass * mass) - mass;
0105 fParticleGun->SetParticleEnergy(ekin);
0106
0107 auto angle = (G4UniformRand() - 0.5) * fSigmaAngle;
0108 fParticleGun->SetParticleMomentumDirection(G4ThreeVector(std::sin(angle), 0., std::cos(angle)));
0109
0110 fParticleGun->GeneratePrimaryVertex(event);
0111 }
0112
0113
0114
0115 void PrimaryGeneratorAction::DefineCommands()
0116 {
0117
0118 fMessenger = new G4GenericMessenger(this, "/B5/generator/", "Primary generator control");
0119
0120
0121 auto& momentumCmd = fMessenger->DeclarePropertyWithUnit("momentum", "GeV", fMomentum,
0122 "Mean momentum of primaries.");
0123 momentumCmd.SetParameterName("p", true);
0124 momentumCmd.SetRange("p>=0.");
0125 momentumCmd.SetDefaultValue("1.");
0126
0127
0128
0129
0130
0131 auto& sigmaMomentumCmd = fMessenger->DeclarePropertyWithUnit(
0132 "sigmaMomentum", "MeV", fSigmaMomentum, "Sigma momentum of primaries.");
0133 sigmaMomentumCmd.SetParameterName("sp", true);
0134 sigmaMomentumCmd.SetRange("sp>=0.");
0135 sigmaMomentumCmd.SetDefaultValue("50.");
0136
0137
0138 auto& sigmaAngleCmd = fMessenger->DeclarePropertyWithUnit("sigmaAngle", "deg", fSigmaAngle,
0139 "Sigma angle divergence of primaries.");
0140 sigmaAngleCmd.SetParameterName("t", true);
0141 sigmaAngleCmd.SetRange("t>=0.");
0142 sigmaAngleCmd.SetDefaultValue("2.");
0143
0144
0145 auto& randomCmd = fMessenger->DeclareProperty("randomizePrimary", fRandomizePrimary);
0146 G4String guidance = "Boolean flag for randomizing primary particle types.\n";
0147 guidance += "In case this flag is false, you can select the primary particle\n";
0148 guidance += " with /gun/particle command.";
0149 randomCmd.SetGuidance(guidance);
0150 randomCmd.SetParameterName("flg", true);
0151 randomCmd.SetDefaultValue("true");
0152 }
0153
0154
0155
0156 }