Skip to content

Commit f7ab99b

Browse files
sawenzelclaude
andcommitted
Locate a boundary crossing along the step direction in the VecGeom navigator
This fixes a problem in the VecGeom navigator and adds a unit test. - The locate after a crossing used the direction after the step, which multiple scattering can turn back into the volume just left. - The track was then put into the mother of two touching layers with a zero-length step, where electrons below the cut of the air were killed. - The crossing is now located along the direction of the step that found it, as the Geant4 navigator does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c2c75dc commit f7ab99b

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎Detectors/gconfig/src/VecGeomG4Navigator.cxx‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
190190
mWouldEnter = entering;
191191
mWouldExit = !entering;
192192
mNextPoint = globalPoint + step * direction;
193+
mNextDir = toDir(direction);
193194
if (entering) {
194195
mNextState = mStepState;
195196
}
@@ -209,9 +210,9 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
209210
/// the surface of and heading out of, then look down again from there, never back into the volume
210211
/// just left, which stays blocked for the next step. Bounded, because popping and descending can
211212
/// meet another flush face. It never pops to or above minLevel: a daughter the step entered stays
212-
/// entered, because the step decided that with the chord direction, and the direction a locate gets
213-
/// under a field is the momentum. Nor does it descend back into `avoid`, the volume the crossing
214-
/// exited: on a face a helix touches tangentially chord and momentum disagree about the side.
213+
/// entered, as the step decided. Nor does it descend back into `avoid`, the volume the crossing
214+
/// exited. Both guard the decision of the step against a locate direction that disagrees with it
215+
/// about the side of the face.
215216
void VecGeomG4Navigator::leaveFlushVolumes(const V3& point, const V3& dir, int minLevel,
216217
vecgeom::NavigationState const* avoid)
217218
{
@@ -301,7 +302,10 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
301302
const bool crossing = onBoundary && (mWouldEnter || mWouldExit) && direction != nullptr;
302303
const bool entering = crossing && mWouldEnter;
303304
const V3 p = toVG(point);
304-
const V3 dir = direction != nullptr ? toDir(*direction) : V3(0., 0., 0.);
305+
// A crossing is located along the direction of the step that found the boundary. The direction given
306+
// here is the one after the step, which multiple scattering can have turned back into the volume left;
307+
// the track then belongs to what lies behind the face, as G4Navigator does, not to the mother of the two.
308+
const V3 dir = crossing ? mNextDir : (direction != nullptr ? toDir(*direction) : V3(0., 0., 0.));
305309
const double push = (onBoundary && direction != nullptr) ? boundaryPush(p, dir) : 0.;
306310
const V3 q = p + push * dir;
307311

‎Detectors/gconfig/src/VecGeomG4Navigator.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
107107
bool mWouldEnter = false; ///< the last ComputeStep ends by entering a daughter
108108
bool mWouldExit = false; ///< the last ComputeStep ends by leaving the current volume
109109
G4ThreeVector mNextPoint{-1e8, -1e8, -1e8};
110+
vecgeom::Vector3D<double> mNextDir{0., 0., 1.}; ///< direction of the step that found the boundary
110111
G4ThreeVector mLastLocatedPoint{-1e8, -1e8, -1e8};
111112
bool mLocatedOnBoundary = false;
112113
G4ThreeVector mSafetyOrig{-1e8, -1e8, -1e8}; ///< the last point a safety was computed for

‎Detectors/gconfig/test/testVecGeomG4Navigator.cxx‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ std::vector<Visit> withNavigator(G4Navigator& nav, const double* p, const double
143143
return visits;
144144
}
145145

146+
/// Where the navigator is after each boundary along a ray. The locate after boundary `turnAt` gets the
147+
/// reversed direction, as after a scattering at the face, and the walk ends there.
148+
std::vector<std::string> afterBoundaries(G4Navigator& nav, const double* p, const double* d, int turnAt)
149+
{
150+
std::vector<std::string> paths;
151+
G4ThreeVector point(p[0] * cm, p[1] * cm, p[2] * cm);
152+
const G4ThreeVector dir(d[0], d[1], d[2]);
153+
auto* pv = nav.LocateGlobalPointAndSetup(point, &dir, false, false);
154+
for (int k = 0; k < 10000 && pv != nullptr; ++k) {
155+
G4double safety = 0.;
156+
const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety);
157+
if (!(step < 1.e6 * cm)) {
158+
break;
159+
}
160+
point += step * dir;
161+
nav.SetGeometricallyLimitedStep();
162+
const G4ThreeVector after = k == turnAt ? -dir : dir;
163+
pv = nav.LocateGlobalPointAndSetup(point, &after, true, false);
164+
paths.push_back(pv != nullptr ? touchablePath(nav) : "");
165+
if (k == turnAt) {
166+
break;
167+
}
168+
}
169+
return paths;
170+
}
171+
146172
std::vector<Visit> withTGeo(const double* p, const double* d)
147173
{
148174
std::vector<Visit> visits;
@@ -204,3 +230,36 @@ BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits)
204230
// every face here coincides with others, and none of them is a place to stop
205231
BOOST_CHECK_EQUAL(zeroSteps, 0);
206232
}
233+
234+
BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered)
235+
{
236+
auto& f = fixture();
237+
std::mt19937_64 gen(2);
238+
std::uniform_real_distribution<double> u(-1., 1.);
239+
std::normal_distribution<double> g;
240+
int boundaries = 0, different = 0;
241+
std::string firstDifference;
242+
for (int r = 0; r < 2000; ++r) {
243+
const double p[3] = {9.9 * u(gen), 9.9 * u(gen), 9.9 * u(gen)};
244+
double d[3] = {g(gen), g(gen), 0.3 * g(gen)};
245+
const double norm = std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
246+
for (auto& x : d) {
247+
x /= norm;
248+
}
249+
const auto straight = afterBoundaries(*f.navigator, p, d, -1);
250+
for (int k = 0; k < static_cast<int>(straight.size()); ++k) {
251+
const auto turned = afterBoundaries(*f.navigator, p, d, k);
252+
++boundaries;
253+
if (turned.size() != static_cast<std::size_t>(k) + 1 || turned.back() != straight[k]) {
254+
++different;
255+
if (firstDifference.empty()) {
256+
firstDifference = Form("ray %d boundary %d: %s, without the turn %s", r, k, turned.empty() ? "-" : turned.back().c_str(), straight[k].c_str());
257+
}
258+
}
259+
}
260+
}
261+
BOOST_TEST_MESSAGE("boundaries " << boundaries << " " << firstDifference);
262+
BOOST_CHECK_GT(boundaries, 10000);
263+
// the step decides which volume is entered, not the direction the track has after it
264+
BOOST_CHECK_MESSAGE(different == 0, different << " boundaries where the turn changed the volume; " << firstDifference);
265+
}

0 commit comments

Comments
 (0)