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