The simplest way to do this is to write your own primary generator action. At each event, you could throw a random value to choose which particle type (e.g., from a list constructed via macro commands) to throw.
Another method might be to write a “within run” macro (the second argument to /run/beamOn) and have that macro change the /gun/particle during the run.
Do you mean write my own G4VUserPrimaryGeneratorAction::GeneratePrimaries method?
I am considering initialize a data object that stores the information of particles I want to simulate at MyPrimaryGeneratorAction constructor.
And then at GeneratePrimaries, select the data by index event->GetEventID(), using the selected data to setup fParticleGun and call fParticleGun->GeneratePrimaryVertex(event) at last.
Is it a feasible approach?
BTW, I find that by using G4HEPEvtInterface, I can easily read an external data file. But this interface seems can not read particle position. Is there an easy way to give particle position in G4HEPEvtInterface?
Yup, exactly. You can carry a G4ParticleGun data member, and let it do all of the work with vertices and particles and whatnot.
Yeah, if you want to pre-construct a table of which aprticles you want for each event, that sounds like the right way to go. If you want to configure it via a macro file, you could write a Messenger class for your primary generator action.
I’m afraid I’ve never used the G4HEPEvtInterface; from reading the documentation, it seems to me that may be too heavy-weight of an interface if you just want a list of particle names or PDG codes. If you’re wanting to read in all the kinematics of your primary or multiple primaries, then that’s definitely better than writing your own.