Generate particles in random positions


I want to simulate 1000 muons entering from a surface. I would like to have each of them entering from a different posiziont. For doing so I was following the B1 example, but I’m not sure it is correct.

Here attached there is the code I’m using inside the

: G4VUserPrimaryGeneratorAction(),

G4int n_particle = 1000;
fParticleGun = new G4ParticleGun(n_particle);

// default particle kinematic
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
G4String particleName;
G4ParticleDefinition* particle
	= particleTable->FindParticle(particleName = "mu+");
fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., -1.));
fParticleGun->SetParticleEnergy(10. * GeV);


fParticleGun->SetParticlePosition(G4ThreeVector(0.5 * (G4UniformRand() - 0.5), 0.5 * (G4UniformRand() - 0.5), 0.5)*m);

Pratically I want to simulate a flux entering in the volume.


The code you’ve written will generate 1000 muons in a single event, all starting at the same position and all with the same direction (which may not be consistent with the position). You should change two things:

  1. Generate just one muon per event. Use /run/beamOn N to get N particles, each in it’s own event. Histograms accumulate, so the final result will be what you want, but you’ll have the option of also doing much more detailed studies of how much energy each muon deposited, etc.

  2. Don’t set the direction fixed. Generate the random vector using G4RandomDirection(), and then use that vector for position, and -vector.unit() for direction. That’ll give you muons radially inward from the surface of a sphere.

I’m actuallly pretty new to Geant4 so I don’t really know how to do what you are suggesting.

I suppose so that there is no way so simulate a flux, so more particle hitting a surface, in a unique event?

Thanks for the help

There’s no direct way to do this with G4ParticleGun as all N particles will have the same properties. Given that all of the muons are independent, @mkelsey’s suggestion is the best way to do this, so one muon per event, N events per run. Any accumulated quantities will be equivalent to N muons in one event.

In exampleB1, the lines here: examples/basic/B1/src/ · master · geant4 / geant4 · GitLab randomise the start position, so if your use case also needs a randomised momentum direction, that can be generated and applied to the particle gun with the lines:

G4ThreeVector momentumUnitVector = G4RandomDirection();

My problem is that I’m not accumulating anything, I just want to register all the secondries generated by the passage, but maybe I can get it each event.

For instance, I’m running my code as: main.exe > pathtothefolderwhereIwanttosavetheoutcome

So how can i set the number of events I want? Just in the vis file? Becuase I’m actually not using them, so I was wondering if I could do it from the .cc or .hh files.


I think I succeded in having more events! Thank you very much @mkelsey and @bmorgan for the help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.