Can you update a GPS particle after generation, but before a run

I have a question about generating particles using GPS options. For my particular problem I am building particles on a circle, with some x/y-biasing. I am isotropically sampling the polar angle theta between 0 and some small angle, and isotropically sampling the azimuthal angle phi over the full range.

My issue is that I would like to restrict the beam so that you don’t get particles generated towards the central z-axis, only parallel to the z-axis, or pointing away from it. In the diagram below, I would like to allow green particles, but disallow red particles. I could do this if I was able to set the angle phi for a given particle that is generated, so that phi equals the angle defined by the (x, y) position of the particle on the circle.

Is there a way to do this using the macros? I don’t think so, but I’d be interested to see if it is.

If this can’t be done with macros, is it possible to do the following?

  1. Generate a random primary using the GPS macro definitions
  2. Update that particle’s momentum direction after it is generated, but before it starts its run
  3. Complete the run.

I know I can “roll my own” particle gun, but I’m trying to see if I can avoid reinventing the wheel.

I found a way to do this via ChatGPT. If you guys haven’t tried it yet, it actually knows about Geant4 and can help with some coding.

  1. Generate the Primary Particle with GPS: First, use GPS commands (usually in a macro file or directly in your code) to define the general properties of the primary particles.
  2. Modify the Particle’s Property in PrimaryGeneratorAction: Override the GeneratePrimaries method in your PrimaryGeneratorAction class. After generating the primary particle with GPS, you can retrieve and modify its properties. Here’s a simplified example:

cppCopy code

#include "G4GeneralParticleSource.hh"
#include "G4ParticleTable.hh"
#include "PrimaryGeneratorAction.hh"

    particleSource = new G4GeneralParticleSource();
    // ... other initializations ...

void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
    G4PrimaryVertex* primaryVertex = anEvent->GetPrimaryVertex();
        G4PrimaryParticle* primaryParticle = primaryVertex->GetPrimary();
            // Now you can modify the particle's properties, for example:
            primaryParticle->SetKineticEnergy(/* new energy value */);
            // ... other modifications ...

In this code, particleSource->GeneratePrimaryVertex(anEvent) is used to generate the primary particle as defined by GPS. Then, you retrieve the primary vertex and particle from the event. After getting the primary particle, you can modify its properties, such as kinetic energy, momentum, etc.

1 Like


According to the documentation, and I understand correctly the issue, I think it is doable using the UI commands of GPS, particularly /gps/hist/ to setup a custom angular distribution or biasing

You may find many examples of GPS under the directory: