Generate Particles with uniform momentum distribution

_Geant4 Version: 7.4
_Operating System: Linux-CentOS_7
_Compiler/Version: gcc_4.8-x86_64
#_CMake Version:

I would need to generate protons with uniform momentum distribution. By default Geant is simulating particles with uniform kinetic energy. After some investigation, I found the script
where it seems particle with uniform kinetic energies are generated. Then, I changed it to have uniform generation of momenta. However, I don’t get the momentum distribution I would need for the generated protons.
I guess I don’t have to compile Geant after this change in, right?

What would you suggest?
Thanks a lot! Pilar

If you open up any of the basic examples there will be a some files that have PrimaryGeneratorAction in them. These files define how the particle is generated, including their momentum direction. In example B5 the line used to define the particle momentum is direction,


The function will take in any G4ThreeVector, so you just need to define one with your desired momentum. I think this method should be a bit easier than how you were trying, but I could be wrong.