Several Particle Guns in a Single Simulation (?)

Dear all,
I am a bleeding beginner, so it is completely possible for me to have missed something obvious. Please keep this in mind.

What I wanted to do is scatter a neutron beam, as coming from a reactor or spallation source from a particle beam going into the opposite direction (starting with protons, but I want to be able to change the nature of the oncoming particles).

To that end, I modified the B1 example, set all logical volumes to vacuum and introduced a second G4ParticleGun instance in B1 PrimaryGeneratorAction. This works and compiles very well.

Changed code sections are:
G4double vacLevel = 1e-7; //1 for normal pressure, 1e-3 for mbar …
density = vacLevel1.25kg/m3;
molWt = 14.0067g/mole;
vac = new G4Material(name=“vacuum”, density, ncomponents=1);
vac->AddElement(elN, natoms=2);

and setting all materials to vac.

and in B1PrimaryGeneratorAction.hh
G4ParticleGun* fneutronGun;

// default particle kinematic for neutrons
G4ParticleDefinition* neutron
= particleTable->FindParticle(particleName=“neutron”);



What I noticed is, that obviously the events are worked through sequentially, i.e. one of the particle guns fires, the particle is tracked until it is out of world and then the other particle gun fires, meaning that the two beams that I want to meet head-on are never in the simulation at the same time.

Is there another way to do this, am I doing this completely wrong or is it something obvious that I missed?

Thanks for any help.


You may not want to instantiate two G4ParticleGuns. They have a UI messenger associated with them (so you could configure them in a macro); with two the commands would all go just to the second one you created.

You can call the same gun multiple times to generate vertices. Thus you could do something like

mygun->SetParticleDefinition(G4Neutron::Definition());   // Don't do Find(string)!
[ etc. ]


and so on, as much as you want. Here, I had the second particle going off at a bit of an angle.

However, your last comment shows that you’re trying to do something impossible in Geant4:

Tracks in Geant4 never interact. That’s not part of the simulation. Particles (tracks) interact with material. That’s it. What you saw in your simulation were the two neutrons (in one event) both doing their own thing. G4 tracks each particle until it’s end, then goes to the next one. Timestamps are applied during tracking, not based on order, so all the primary particles start at T=0.

I already feared this might be the issue.

As an alternative approach, I tried creating a gas as a target, but the materials to choose from are only elements (so I cannot choose electrons or neutrons as a gas).

Is there a way to create materials from not atomic elements?


Not as far as I know. Materials are made from elements are made from isotopes (G4Isotope, not G4Ions), which require Z and A arguments, and there are sanity checks (Z >= 1, A >= 1) to avoid mistakes.

It sounds like you’re wanting to do a collider simulation (beam-on-beam). In that case, you probably want to look into “event generators”, which simulate the physics of collisions (there are many available outside of Geant4). You then take the output of such a generator (typically as part of your own custom G4VUserPrimaryGeneratorAction subclass), and use it to create G4PrimaryParticle and G4PrimaryVertex objects. This is instead of G4ParticleGun.

You are completely right, I already tried to trick the system by removing the sanity check, which only resulted in a huge amount of errors. :wink:

Any pointers on which event generator to use?


Urgh. What are you really trying to simulate?

Spallation neutron sources don’t do colliding beams, they do beam-on-target, which works fine with G4.

HERA (or RHIC’s future incarnation) do proton-electron, and they should have event generators you can use, if you contact them.

Pythia does hadron-hadron interactions.

Yes, neutron sources as such do not collide beams, but we have an experiment where we need to run electron beams grazing at the sample surface and investigate this with neutron scattering. This means the neutron and particle beams will be more or less head on.

I would really like to simulate this setup before we go into further design studies.

Of course this would be nice to be extendable also to light ions later on, therefore the proton beam.

I will have a look into pythia, this looks feasible.

Thanks a lot.