Check if source particle is in a specific volume

I am working on a project trying to calculate the deposited dose in spherical cells which are inside cylindrical wells. In fact, I want to emit source particles from inside the cells, so what I have done is to recuperate the positions of the cells in different tables from and use them in thus by comparing separately each emitted source particle with each cell, however, this spends a lot of time to finalize a single simulation because I am simulating around1000 cells.
So what I am asking for please, is there any other possibility of accelerating these comparisons, or even an example that tries to apply the same idea?
Thanks in advance for any help

I’m not sure I’ve completely understood the setup, but from your description it sounds like (and correct me if I’m wrong!):

  • Each sphere has a known position in the world coordinates of the geometry
  • Each sphere has the same radius
  • The source particle start positions are uniformly distributed within each sphere
  • Each sphere has the same activity (so equally likely to generate a particle)

and you’re trying to efficiently sample starting points for your source particles?

Thank you for your answer,
In fact, at the beginning of the simulation, the 1000 cells are positioned randomly, but once started, their positions are fixed during the whole simulation. After that, in the primarygenerator, again the source particles are generated randomly one by one. If the generated particle is outside the 1000 cells, then a new particle is generated trying to be in one of the 1000 cells. On the other hand, if the generated particle is inside one of the cells then we accept it and the simulation can continue.
So to generate a single particle a lot of comparisons have to be done, and what I am trying to ask is how can I determine efficiently whether the source is inside one of the cells or not ?

Do not hesitate to ask if there are still incomprehensible ideas

Thanks! Even though the sphere positions are random, if you are able to store these (in world coordinates), then it should be possible to implement an exact sampling with the following algorithm (in somewhat pseudo-code):

// assume that the PrimaryGeneratorAction has a pointer to to DetectorConstruction
G4double sphereRadius = detectorConstruction->GetSphereRadius();
auto& vectorOfSpherePositions = detectorConstruction->GetSpherePositions();

// assume that particles are equally likely from any sphere, so choose one uniformly
auto numberOfSpheres = vectorOfSpheres.size();
auto sampledSphereIndex = G4RandFlat::shootInt(numberOfSpheres - 1);

// Sample a uniform random position in a sphere centered 0,0,0 - implementation of "SamplePointInSphere" not provided
G4ThreeVector offset = SamplePointInSphere(sphereRadius);

// Start point is then just the centre of the sphere sampled from the vector plus the offset
G4ThreeVector start = vectorOfSpherePositions[sampledSphereIndex] + offset;

// use "start" as needed...

thank you very much for your time, i will first tyr to understand it :sweat_smile:, then i will try to apply it to verify what it gives.