_Geant4 Version:geant4-11-02-patch-01
_Operating System:windows/linux
_Compiler/Version:VS
_CMake Version:3.29
Hi I’m trying to simulate electrons hitting a phantom, the electrons data is stored in an external file from where the primary generator reads the file and scores the hits in the phantom. My problem is the program is executing very slowly. Below is piece of code for PG and the external file. Could someone please help me and explain why the code is executing very slowly? The following is the code snippet. Thanks
PrimaryGeneratorAction::PrimaryGeneratorAction(DetectorConstruction* DC)
: G4VUserPrimaryGeneratorAction(), fParticleGun(0), fDetector(DC), fCurrentIndex(0)
{
G4int n_particle = 1;
fParticleGun = new G4ParticleGun(n_particle);
// Load the phase space file
LoadPhaseSpaceFile("............./build/filtered_electron_data.txt");
}
PrimaryGeneratorAction::~PrimaryGeneratorAction()
{
delete fParticleGun; }
void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
if (fCurrentIndex >= fParticleData.size()) {
G4cerr << "No more particles in the phase space file" << G4endl;
return; }
const ParticleData& data = fParticleData[fCurrentIndex++];
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
G4ParticleDefinition* particle;
if (data.particleGroupID == 11) {
particle = particleTable->FindParticle("e-");
}
else {
particle = particleTable->FindParticle("gamma"); }
fParticleGun->SetParticleDefinition(particle);
fParticleGun->SetParticlePosition(G4ThreeVector(data.x * mm, data.y * mm, data.z * mm));
//fParticleGun->SetParticleMomentumDirection(G4ThreeVector(data.px, data.py, data.pz).unit());
//fParticleGun->SetParticleTime(data.time * ns);
fParticleGun->SetParticleMomentum(G4ThreeVector(data.px, data.py, data.pz)*MeV);
fParticleGun->SetNumberOfParticles(data.weight);
fParticleGun->GeneratePrimaryVertex(anEvent); }
void PrimaryGeneratorAction::LoadPhaseSpaceFile(const std::string& filename)
{ std::ifstream infile(filename);
if (!infile.is_open()) {
G4cerr << "Error: could not open the phase space file " << filename << G4endl;
return;}
std::string line;
while (std::getline(infile, line)) {
if (line.empty()) continue;
std::istringstream iss(line);
ParticleData data;
iss >> data.x >> data.y >> data.z
>> data.px >> data.py >> data.pz
>> data.time >> data.particleGroupID
>> data.eventID >> data.trackID >> data.parentID >> data.weight;
fParticleData.push_back(data);
}
infile.close();
G4cout << "Loaded " << fParticleData.size() << " particles from " << filename << G4endl;}