How to bias source distribution to reduce runtime?

I have a detector that’s essentially a pinhole camera, and I’m finding it difficult to run simulations in a reasonable time due to the size of my pinhole. How can I reduce the number of unnecessary computations ie events that have no chance of hitting my detector? I’ve been playing around with setting min and max theta and phi in macros for my sources, to limited success. Is there a better way?

Are you doing only electromagnetic interactions, or do you need to have hadronic (nuclear) interactions as well?

For purely EM simulations, Geant4 has a “reverse MC” which is designed for this kind of use case (tiny detector in a gigantic source).

If your world volume is non-scattering (G4_Galactic, not air or water or rock), then you could also write your source in such a way that it aims the primaries at your pinhole. You would not be able to easily compute a flux from that, since you’d be biasing the source distribution (you’d have to do a geometric calculation to get your expected flux). But it would dramatically increase your successful event rate.

I’m doing some scintillation simulations. Can the reverse MC handle generation of optical photons?

The world volume is technically air, but the density’s so low scattering isn’t a significant factor. I’ve mostly used macros in source definitions, would I be able to use particleGun inside my PrimaryGeneratorAction to aim the source at my pinhole?

Not directly. The overview description just says “the RMC method is implemented in Geant4 only for some electromagnetic processes,” but it’s set up where you define a boundary between the reverse and “forward” (a.k.a. regular) tracking, so inside that region you can have your scintillator, optics, etc.

Yes. That’s what we do in our own simulation. Custom (and sometimes complicated) code to select position and direction information, or energy spectra, or whatever, then we call G4ParticleGun to set the kinematics and add a primary particle to the event.