The randomness of G4GeneralParticleSource

Hi all,
I use the G4GeneralParticleSource class and UI comands to define a particle source with energy spectrum.
If N particles are simulated, and the source has energy spectrum(probability density) f(E), then the number of particles generated with energy E is f(E)*N or f(E)*N with a gauss or poisson smear? From the codes of G4GeneralParticleSource, it seems the number of partilces with energy E is f(E)*E, without any randomness. Is it possible to add a poisson (gauss) randomness to the energy distributions.

Absolutely. Check the General Particle Source documentation. You can implement an arbitrary PDF by writing your own GeneratePrimaries.

Hi @phirippu, I defined a PDF by UI commands /gps/hist/point energy intensity. what I wanna is that add a poisson randomness to the spectrum of the source.

I do not understand what you mean. If your PDF is already defined, what is about Poisson? Usually, Poisson statistics is in the temporal domain.

can you check that again? in my simulations there is randomness, i.e., the number of particles with energy E will vary from run to run…
(do you maybe seed the random number generator at the begin of each run with the same seed?)

Hi @weller , maybe you are right. I have set G4RunManager::GetRunManager()->SetRandomNumberStore(false); By the way, How to you set the random seeds?

This should give you general guidance: Global Usage Classes — Book For Application Developers 11.0 documentation

To put unique values into the “seeds” array, many people do things like call time(NULL) at the start of their job, or use the UUID library.

set the random seeds to make each run different

//set the random engine
CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine());
G4long seed = time(NULL);    //use system time as seed
CLHEP::HepRandom::setTheSeed(seed); // set the random seed

This works, but it’s not good if you’re submitting mutiple batch jobs at the same time (you can fairly easily get the same timestamp on more than one compute node if your jobs are dispatched in bundles). There are more complicated solutions, involving combining the time and host IP, or libUUID, or other techniques.

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