Program executing very slowly

_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;}

Could you please edit you post to format the code snippet correctly as per the guide here: How to post code snippets - #9

It’s awkward to makes sense of what’s going on as it stands.

To expand further, the external electron data file has x, y, z (position), px, py, pz (momentum in x, y and z), time, particle ID, event ID, Track ID, Parent ID and Weight. There was no problem when I was using a random particle generator, but it is very prolonged when data is read from an external file. The output shows the following message while executing,
G4WT0 > G4ParticleGun::e-
G4WT0 > was defined in terms of KineticEnergy: 1GeV
G4WT0 > is now defined in terms Momentum: 0.225881GeV/c
G4WT0 > G4ParticleGun::e-
G4WT0 > was defined in terms of KineticEnergy: 0.225371GeV
G4WT0 > is now defined in terms Momentum: 0.213681GeV/c
G4WT0 > G4ParticleGun::e-
G4WT0 > was defined in terms of KineticEnergy: 0.21317GeV
G4WT0 > is now defined in terms Momentum: 0.22102GeV/c
G4WT0 > G4ParticleGun::e-
G4WT0 > was defined in terms of KineticEnergy: 0.220509GeV
G4WT0 > is now defined in terms Momentum: 0.227758GeV/c