Set spectrum as a particle source

hello,
I have the following energy spectrum, my question is how can I use this spectrum as a source

1 Like

http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/ForApplicationDeveloper/html/GettingStarted/generalParticleSource.html

see also examples/extended/eventgenerator/particleGun : run2
and exgps/macros/README

Thank you all.
It’s helpful

Is it also possible to do this with a particle gun?

Not using G4ParticleGun directly. You can write your own PrimaryGeneratorAction with G4ParticleGun as a data member. Write your own code to throw the energy randomly from the spectrum, and then pass that energy, a random direction, and a particle type into your G4ParticleGun data member.

Of course, GPS has that functionality, and you can configure it with any spectrum you like via macro commands, so it’s more flexible.