How to implement a large number of sources via GPS

Hello!
I am new to Geant4.
I want to simulate a large number of sources (30 gamma emitting cylinders) using GPS, where only the position and orientation of the sources change.

Is there a way to do it by reading the positions from an external file, or in any way simplifying the process of defining the numerous sources, in GPS?

@misubia

GPS already take care of positions and number of emitters as source,
below example may help you to understand,

# Gamma source-1
/gps/source/add 1.
/gps/particle gamma ## or your choice of the source
/gps/pos/centre X Y Z cm
/gps/direction 0 0 1 
.....
....
..

# Gamma source-2
/gps/source/add 1.
/gps/particle gamma ## or your choice of the source
/gps/pos/centre X Y Z cm
/gps/direction 0 0 1 
.....
....
..

# Gamma source-3
/gps/source/add 1.
/gps/particle gamma ## or your choice of the source
/gps/pos/centre X Y Z cm
/gps/direction 0 0 1 
.....
....
..

# And so on 

GPS macro commands can be find here :point_down:

Also you may look exgps example,
this macro may help you
test31.mac.txt (1.8 KB)

VRS

1 Like

Thank you!
Is there any way to optimize the generation of the sources?
For example, n sources of the same nature, where only their position changes (/gps/pos/centre) so that the macro command reads the info for positioning from an external file.

@misubia

When you say an extrenal file, you can define positions in x,y,x format and can call in primary generator action class.

Maybe this snapshot help you.

//its constrcutor
PrimaryGeneratorAction::PrimaryGeneratorAction(G4ParticleGun* gun)
: G4VUserPrimaryGeneratorAction(), fparticlegun(gun)
{
    auto source = define your desired source
    mygun->SetParticleDefinition(source);
    
    // Read positions from txt file
    ReadPositions("pos.txt");
}

Create a function to retrieve information extrenally.

void PrimaryGeneratorAction::ReadPositions(const std::string& filename)
{
    std::ifstream infile(filename);
    std::string line;
    while (std::getline(infile, line))
    {
        std::istringstream iss(line);
        G4double x, y, z;
        if (!(iss >> x >> y >> z)) { break; } // Error
        positions.push_back(G4ThreeVector(x, y, z));
    }
}

call defined sources all together.

void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
    for (const auto& pos : positions)
    {
        fparticlegun->SetParticlePosition(pos);
        fparticlegun->GeneratePrimaryVertex(anEvent);
    }
} 

VRS