G4-DESCSS/src/PrimaryGeneratorAction.cpp

87 lines
2.2 KiB
C++

#include "PrimaryGeneratorAction.h"
#include "G4PhysicalConstants.hh"
#include "G4SystemOfUnits.hh"
#include "Randomize.hh"
PrimaryGeneratorAction<G4GeneralParticleSource>::PrimaryGeneratorAction() {
fParticleGun = new G4GeneralParticleSource();
}
PrimaryGeneratorAction<G4ParticleGun>::PrimaryGeneratorAction() {
fParticleGun = new G4ParticleGun();
}
template <typename T>
PrimaryGeneratorAction<T>::~PrimaryGeneratorAction() {
delete fParticleGun;
}
std::string line;
G4double M = 931.5;
G4double C1, C2, C3, C4, sum;
std::ifstream modelFile("F:/Project/Geant4/DESCSS/assets/model.txt");
G4double randomPhi() {
G4double v = G4UniformRand();
return 2 * pi * v;
}
G4double randomTheta() {
G4double u = G4UniformRand();
return std::acos(2 * u - 1);
}
G4ThreeVector randomPos(G4double rho) {
G4double phi = randomPhi();
G4double theta = randomTheta();
G4double x = rho * std::sin(theta) * std::cos(phi);
G4double y = rho * std::sin(theta) * std::sin(phi);
G4double z = rho * std::cos(theta);
return G4ThreeVector(x, y, z);
}
G4ThreeVector randomDir() {
G4double phi = randomPhi();
G4double theta = randomTheta();
G4double x = -std::sin(theta) * std::cos(phi);
G4double y = -std::sin(theta) * std::sin(phi);
G4double z = -std::cos(theta);
return G4ThreeVector(x, y, z);
}
G4double ITM(G4double Emin, G4double Emax, G4double (*f)(G4double)) {
G4double x = G4UniformRand();
return (*f)(x);
}
G4double ARM(G4double Emin, G4double Emax, G4double (*f)(G4double)) {
G4double x, y;
while (true) {
x = G4UniformRand() * (Emax - Emin) + Emin;
y = G4UniformRand();
if (y <= (*f)(x)) break;
}
return x;
}
void PrimaryGeneratorAction<G4GeneralParticleSource>::GeneratePrimaries(G4Event* e) {
fParticleGun->GeneratePrimaryVertex(e);
}
void PrimaryGeneratorAction<G4ParticleGun>::GeneratePrimaries(G4Event* e) {
G4double E = 0;
G4double R = 15. * m;
G4ThreeVector pos = G4ThreeVector(); // randomPos(R);
G4ThreeVector dir = G4ThreeVector(); // randomDir();
fParticleGun->SetParticleEnergy(E);
fParticleGun->SetParticlePosition(pos);
fParticleGun->SetParticleMomentumDirection(dir);
fParticleGun->GeneratePrimaryVertex(e);
}