How to set different particles in one run

Hi all,

I want to simulate events with different primary particle status in one single run. Is it possible to do it in Geant4?

I read the examples, and find many of them use different run to simulate different particle guns. In their .mac files, they set up different /gun/particle and then /run/beamon, like B5:

# Turn off randomization 
/B5/generator/randomizePrimary false
/B5/generator/sigmaMomentum 0. 
/B5/generator/sigmaAngle 0.
/run/printProgress 5
/B5/detector/armAngle 30. deg
/gun/particle proton
/B5/generator/momentum 100. GeV
/B5/field/value 100. tesla
/run/beamOn 5
/B5/detector/armAngle 60. deg
/gun/particle pi+
/B5/generator/momentum 100. GeV
/B5/field/value 200. tesla
/run/beamOn 5

Thanks in advance.

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.

1 Like

Thanks for the quick reply!

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.