Confine particles

Currently, I am working on a project, involving the generation of particles in four spheres, all sharing the same center located at coordinates (0,0,0) but with different radii. My goal is to confine these particles within a precise volume defined by the spheres, for example : 1000000 particles between Rmin = 0.001 mm and Rmax = 0,002 mm

I have explored a few approaches to achieve this goal, including using the /gps/pos/confine command in a .mac file. However, I have not been able to obtain the desired results with this method.
I also tried
posx = (2*G4UniformRand()-1)Rmax;
posy = (2
posz = (2
r = std::sqrt(pow(posx,2)+pow(posy,2)+pow(posz,2));
But, it always take random position, outside of the desired range.
Capture d’écran 2023-10-16 à 11.27.57

You need to use spherical coordinates.

What you need to do is randomly generate 2 angles, Then generate a random radius. You can constrict this radius to be within whichever sphere you want.

You will also have to scale your radius by r = sqrt(random()), or else you will have more points closer to the center of your sphere

1 Like

examples/extended/eventgenerator/particleGun/src : lines ~50-55 and ~69-79

You may prefer to use GeneralParticleGun. See Geant4 doc.

1 Like

Thank you, i will try that

Thank you so much, i’ll try that too