General Particle Source using Input File

I am using Geant4.6.p01 on Ubuntu.

I have a list of particle positions and velocities generated from a different source. I would like to use these particles as a source for Geant4 simulations. Looking at the Particle Gun or General Particle Source, it would seem that I can generate particles for a wide range of possibilities, but I can’t find a general source where a file is read and the information regarding each individual particle is determined and then the particle is fired in Geant4 using the parameters read from a file.
One way would be to run Geant4 many times over with one particle simulated each time for each particle in the file. Is there a better and faster way to do this ?

Thanks in advance

Since the input file format is specfic to your application, you’re going to have to write your own code to parse it anyway.

You may write your own {{G4VUserPrimaryGeneratorAction}} subclass, which would contain either a {{G4ParticleGun}} data member, or your own {{G4VPrimaryGenerator}} subclass.

In your GeneratorAction, you’d open the input file. At the start of the event, read the next line (or whatever) of the file, and use the information you read to configure the generator (e.g., {{G4ParticleGun}}) appropriately, to create primaries from that information.

Thanks Mike,
I will get cracking on it.

See my posting here: CSV input file.

But you’ll certainly need to use GPS.

1 Like

Just curious, Tim. Why would you need GPS if you’re reading in the track-by-track kinematics from a file? In our experiment’s framework (CDMS), I set that up using G4ParticleGun, with my generator calling in to set position, momentum, blah blah blah, for each track, and making a primary vertex and primary particle for it.

Hi Mike,

it was a while ago I “hacked” (modified would be too kinder expression) TestEm7 to suit. IIRC I found that GPS was much more versatile than Gun but I’ll happily recant if I’m wrong.

Happy Easter.


Hi, Tim. You are quite right that GPS is much more versatile. Gun is about as simple as you can get – one particle at a time. If you want anything complicated, you have to write your own, and call Gun multiple times, for each track.