After an extensive research with AI, I think I have pinpointed the problem.
The corrupted angle seems to be a Geant4 bug: an unsynchronized data race on the
G4SPSAngDistribution::Theta / Phi class members, which are shared by all
MTS worker threads when using /gps/ang/type user.
The racy code (Geant4 11.4.3, source/event/src/G4SPSAngDistribution.cc)
void G4SPSAngDistribution::GenerateUserDefFlux(G4ParticleMomentum& mom)
{
...
else if(UserDistType == "theta")
{
Theta = 10.; // (1) sentinel write, unlocked
while(Theta > MaxTheta || Theta < MinTheta) // (2) shared read
{
Theta = GenerateUserDefTheta(); // (3) CDF sample write, unlocked
}
Phi = 10.;
while(Phi > MaxPhi || Phi < MinPhi) { ... }
}
...
px = -std::sin(Theta) * std::cos(Phi); // (4) shared reads again,
py = -std::sin(Theta) * std::sin(Phi); // three separate lines,
pz = -std::cos(Theta); // each may see a new value
...
}
All worker threads share one G4SPSAngDistribution object, and none of the
Theta/Phi accesses are locked. In MTS, thread B’s momentum can therefore
be computed with a Theta written by thread A:
- Cross-thread values: B’s primary gets A’s CDF-sampled theta. Measured:
of 244 corrupted events in a 200k-event run (0.12 %), 112 of the corrupted
values are exactly the theta sampled by an other thread’s event, 10 are
the same thread’s previous event’s value.
- The delta at $\theta \approx 2.5664$: if the shared member holds another
thread’s sentinel value 10. (rad) when pz = -cos(Theta) is evaluated, the
primary gets $\arccos(-\cos 10) = 2.56637$ — a constant independent of any
RNG draw. 31 of the corrupted events carry exactly this value. This is the
visible peak in the $\theta \in [2.55, 2.6]$ bin.
- Ghost values: the three momentum lines can each read a different value
of the shared members, so pmag = sqrt(px^2+py^2+pz^2) != 1 and the stored
theta becomes acos(pz/pmag) — a value that matches no RNG draw at all
(the remaining ~100 corrupted events).
Why only in MTS: with one thread nobody else writes Theta/Phi, so every
read sees the thread’s own value. Why it looks deterministic: Geant4 MTS
re-seeds the engine per event from a seed stream, and on a quiet machine the
four workers run in lockstep, reproducing the same interleaving run after run
(the two buggy 200k runs are bit-identical, all 200k thetas).
Possible Fix
gps_Theta_Phi_thread_safety.patch: replaces the shared Theta/Phi members with function-local variables in GenerateUserDefFlux (all three UserDistType branches), and the shared Phi write in
GenerateIsotropicFlux / GenerateCosineLawFlux (same latent race for those GPS modes). GenerateOne already copies through a local momentum in 11.4.x, so no further change is needed. The member declaration stays (unused) to keep the header/ABI untouched.
Tested with a full 11.4.3 rebuild of Geant4 the app relinked against it, 4 threads, 200k events: zero corrupted thetas.
--- a/source/event/src/G4SPSAngDistribution.cc 2026-06-17 13:13:07.000000000 +0200
+++ b/source/event/src/G4SPSAngDistribution.cc 2026-11-05 17:28:22.230528093 +0100
@@ -350,9 +350,9 @@
sintheta = std::sqrt(1. - costheta*costheta);
rndm2 = angRndm->GenRandPhi();
- Phi = MinPhi + (MaxPhi - MinPhi) * rndm2;
- sinphi = std::sin(Phi);
- cosphi = std::cos(Phi);
+ G4double phi = MinPhi + (MaxPhi - MinPhi) * rndm2; // thread-safe local (was shared member)
+ sinphi = std::sin(phi);
+ cosphi = std::cos(phi);
px = -sintheta * cosphi;
py = -sintheta * sinphi;
@@ -436,9 +436,9 @@
costheta = std::sqrt(1. -sintheta*sintheta);
rndm2 = angRndm->GenRandPhi();
- Phi = MinPhi + (MaxPhi - MinPhi) * rndm2;
- sinphi = std::sin(Phi);
- cosphi = std::cos(Phi);
+ G4double phi = MinPhi + (MaxPhi - MinPhi) * rndm2; // thread-safe local (was shared member)
+ sinphi = std::sin(phi);
+ cosphi = std::cos(phi);
px = -sintheta * cosphi;
py = -sintheta * sinphi;
@@ -520,6 +520,7 @@
void G4SPSAngDistribution::GenerateUserDefFlux(G4ParticleMomentum& mom)
{
G4double rndm, px, py, pz, pmag;
+ G4double theta, phi; // thread-safe locals (were shared members Theta/Phi, racy in MTS)
if(UserDistType == "NULL")
{
@@ -527,48 +528,48 @@
}
else if(UserDistType == "theta")
{
- Theta = 10.;
- while(Theta > MaxTheta || Theta < MinTheta)
+ theta = 10.;
+ while(theta > MaxTheta || theta < MinTheta)
{
- Theta = GenerateUserDefTheta();
+ theta = GenerateUserDefTheta();
}
- Phi = 10.;
- while(Phi > MaxPhi || Phi < MinPhi)
+ phi = 10.;
+ while(phi > MaxPhi || phi < MinPhi)
{
rndm = angRndm->GenRandPhi();
- Phi = twopi * rndm;
+ phi = twopi * rndm;
}
}
else if(UserDistType == "phi")
{
- Theta = 10.;
- while(Theta > MaxTheta || Theta < MinTheta)
+ theta = 10.;
+ while(theta > MaxTheta || theta < MinTheta)
{
rndm = angRndm->GenRandTheta();
- Theta = std::acos(1. - (2. * rndm));
+ theta = std::acos(1. - (2. * rndm));
}
- Phi = 10.;
- while(Phi > MaxPhi || Phi < MinPhi)
+ phi = 10.;
+ while(phi > MaxPhi || phi < MinPhi)
{
- Phi = GenerateUserDefPhi();
+ phi = GenerateUserDefPhi();
}
}
else if(UserDistType == "both")
{
- Theta = 10.;
- while(Theta > MaxTheta || Theta < MinTheta)
+ theta = 10.;
+ while(theta > MaxTheta || theta < MinTheta)
{
- Theta = GenerateUserDefTheta();
+ theta = GenerateUserDefTheta();
}
- Phi = 10.;
- while(Phi > MaxPhi || Phi < MinPhi)
+ phi = 10.;
+ while(phi > MaxPhi || phi < MinPhi)
{
- Phi = GenerateUserDefPhi();
+ phi = GenerateUserDefPhi();
}
}
- px = -std::sin(Theta) * std::cos(Phi);
- py = -std::sin(Theta) * std::sin(Phi);
- pz = -std::cos(Theta);
+ px = -std::sin(theta) * std::cos(phi);
+ py = -std::sin(theta) * std::sin(phi);
+ pz = -std::cos(theta);
pmag = std::sqrt((px*px) + (py*py) + (pz*pz));
Let me know if additional details are needed.
E.