Replacing particle gun with general particle source

In the user guide it says that G4ParticleGun can be replaced with G4GeneralParticleSource by a global search and replace, however I’ve encountered problems with this in practice. For example, in exampleB1, a global search and replace of G4ParticleGun doesn’t work, as G4GeneralParticleSource doesn’t have SetParticleEnergy or SetParticleMomentum.
Are there other differences between ParticleGun and GPS?

As you pointed out, G4GPS doesn’t have SetParticleEnergy/SetParticleMomentum. Correct me if I am wrong, but based on my understanding/experience, G4GeneralParticleSource’s feature like angular distribution, particle types, momentum, etc, can be accessed only by command lines or macro files. Meanwhile, G4ParticleGun “can be used from inside UserPrimaryGeneratorAction to model complex source”.

If you haven’t played with the GPS example in the /example/extended/eventgenerator/exgps, I would recommend going through the code (specifically You can also find the expected results for those examples in this website: This tutorial ( is also great to understand different ways to implement primary particles.

Hope this helps!

Best regards,