Skip to content

Commit 6abbc36

Browse files
sawenzelclaude
andcommitted
Free tracks stuck on a field boundary in the VecGeom navigator
This fixes event aborts of the VecGeom navigator for tracks tangent to a face between a volume with a magnetic field and one without, and adds a unit test. - The field propagator crosses such a face along its chord and the following straight step leaves it again, so the track alternates between the two volumes with zero-length steps. - The stuck-track push was also added to the chord step, which the propagator takes for a miss, so the crossing never happened and the event was aborted after 25 zero steps. - Now only steps along the momentum are lengthened once both kinds have occurred. - The push is capped at 10 um, below the thinnest layers of the detectors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4e0b46e commit 6abbc36

3 files changed

Lines changed: 78 additions & 12 deletions

File tree

‎Detectors/gconfig/src/VecGeomG4Navigator.cxx‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,33 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
164164
// lengthened, after twenty-five the event is aborted.
165165
if (step < 0.05 * kCarTolerance) {
166166
++mZeroStepCount;
167+
// Without a field Geant4 asks along the momentum it located the track with, and takes a lengthened step.
168+
// The field propagator asks along chords and takes a step longer than its chord for a miss.
169+
const bool linear = (direction - mLastLocatedDir).mag2() < 1.e-18;
170+
mZeroLinearSeen = mZeroLinearSeen || linear;
167171
if (++mNzeroSteps >= kActionThresholdNoZeroSteps) {
168-
++mStuckPushCount;
169-
// The push grows from Geant4's 100 kCarTolerance fourfold per try to a micron and doubles from the
170-
// eleventh try on: a track tangent to a curved face stays within the tolerance of it for microns.
171-
const int tries = mNzeroSteps - kActionThresholdNoZeroSteps;
172-
constexpr double kMicron = 1.e-3 * CLHEP::mm;
173-
step += tries < 10 ? std::min(100. * kCarTolerance * std::pow(4., tries), kMicron)
174-
: kMicron * static_cast<double>(2 << (tries - 10));
172+
// A track going back and forth over a face between a medium with a field and one without is tangent
173+
// to the face: the chord crosses it one way and the momentum the other way. The crossing along the
174+
// chord is left at zero length, so that it takes place, and the step along the momentum is lengthened.
175+
if (linear || !mZeroLinearSeen) {
176+
++mStuckPushCount;
177+
// The push grows fourfold per try from 100 kCarTolerance, capped below the thinnest layers (15 um).
178+
constexpr double kMaxStuckPush = 10. * CLHEP::um;
179+
const int tries = mNzeroSteps - kActionThresholdNoZeroSteps;
180+
step += std::min(100. * kCarTolerance * std::pow(4., tries), kMaxStuckPush);
181+
}
175182
if (mNzeroSteps >= kAbandonThresholdNoZeroSteps) {
176183
++mAbandonCount;
177184
std::ostringstream msg;
178185
msg << "Track stuck or not moving: " << mNzeroSteps << " zero steps in " << top->GetLabel() << " at ("
179186
<< globalPoint.x() << ", " << globalPoint.y() << ", " << globalPoint.z() << ") mm. Event aborted, as "
180187
<< "G4Navigator does.";
181-
mNzeroSteps = 0;
188+
resetZeroSteps();
182189
G4Exception("VecGeomG4Navigator::ComputeStep()", "GeomNav0003", EventMustBeAborted, msg.str().c_str());
183190
}
184191
}
185192
} else {
186-
mNzeroSteps = 0;
193+
resetZeroSteps();
187194
}
188195

189196
if (boundaryLimited) {
@@ -314,11 +321,12 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
314321
fExitedMother = false;
315322
mLocatedOnBoundary = false;
316323
mLastLocatedPoint = point;
324+
mLastLocatedDir = direction != nullptr ? *direction : G4ThreeVector(0., 0., 0.);
317325
mSafetyOrig = kNoPoint;
318326
clearLastExited();
319327

320328
if (!relativeSearch) {
321-
mNzeroSteps = 0; // a new track: nothing of the previous one applies
329+
resetZeroSteps(); // a new track: nothing of the previous one applies
322330
locateFromWorld(p);
323331
} else if (topOf(mCurState) == nullptr) {
324332
locateFromWorld(p);
@@ -383,7 +391,8 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
383391
return updateG4History();
384392
}
385393

386-
G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVector& point, const G4ThreeVector&,
394+
G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVector& point,
395+
const G4ThreeVector& direction,
387396
const G4TouchableHistory& history)
388397
{
389398
// A track resumes from a stored touchable, usually a secondary starting where its parent's step
@@ -395,8 +404,9 @@ G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVect
395404
mWouldExit = false;
396405
mLocatedOnBoundary = false;
397406
mLastLocatedPoint = point;
407+
mLastLocatedDir = direction;
398408
mSafetyOrig = kNoPoint;
399-
mNzeroSteps = 0;
409+
resetZeroSteps();
400410
fHistory = *history.GetHistory();
401411
const V3 p = toVG(point);
402412
bool kept = false;

‎Detectors/gconfig/src/VecGeomG4Navigator.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
121121
static constexpr int kActionThresholdNoZeroSteps = 10;
122122
static constexpr int kAbandonThresholdNoZeroSteps = 25;
123123
int mNzeroSteps = 0;
124+
bool mZeroLinearSeen = false; ///< a step along the located momentum occurred in the current run of zero steps
125+
void resetZeroSteps()
126+
{
127+
mNzeroSteps = 0;
128+
mZeroLinearSeen = false;
129+
}
130+
G4ThreeVector mLastLocatedDir; ///< the direction the last locate was given
124131

125132
// What the navigator had to work around, reported at the end.
126133
long mZeroStepCount = 0;

‎Detectors/gconfig/test/testVecGeomG4Navigator.cxx‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,52 @@ BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered)
263263
// the step decides which volume is entered, not the direction the track has after it
264264
BOOST_CHECK_MESSAGE(different == 0, different << " boundaries where the turn changed the volume; " << firstDifference);
265265
}
266+
267+
BOOST_AUTO_TEST_CASE(a_track_tangent_to_a_field_boundary_is_freed_along_its_momentum)
268+
{
269+
// A track on the top face of a lead tile, as under a field: the chord points into the tile, the momentum out of it.
270+
auto& nav = *fixture().navigator;
271+
const G4ThreeVector face(-5. * cm, -5. * cm, -8.45 * cm);
272+
const G4ThreeVector chord = G4ThreeVector(1., 0., -1.e-3).unit();
273+
const G4ThreeVector momentum = G4ThreeVector(1., 0., 1.e-3).unit();
274+
G4double safety = 0.;
275+
// reach the face along the chord from the paper above, so the track ends in the lead
276+
G4ThreeVector point = face - 1. * mm * chord;
277+
nav.LocateGlobalPointAndSetup(point, &chord, false, false);
278+
const double first = nav.ComputeStep(point, chord, 1.e6 * cm, safety);
279+
BOOST_REQUIRE_CLOSE(first, 1. * mm, 1.e-6);
280+
point += first * chord;
281+
nav.SetGeometricallyLimitedStep();
282+
nav.LocateGlobalPointAndSetup(point, &momentum, true, false);
283+
BOOST_REQUIRE_EQUAL(touchablePath(nav).substr(touchablePath(nav).rfind('/')), "/LEAD_1");
284+
285+
// Geant4 alternates a straight step along the momentum, which leaves the lead, and a chord step, which enters it again.
286+
int zeroSteps = 0, pushedChords = 0;
287+
bool moved = false, freed = false;
288+
for (int k = 0; k < 24 && !freed; ++k) {
289+
const bool alongMomentum = k % 2 == 0;
290+
const G4ThreeVector& dir = alongMomentum ? momentum : chord;
291+
const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety);
292+
BOOST_TEST_MESSAGE("step " << k << (alongMomentum ? " momentum " : " chord ") << step << " mm in " << touchablePath(nav));
293+
if (step < 1.e-10 * mm) {
294+
++zeroSteps;
295+
} else if (alongMomentum) {
296+
// the lengthened momentum step moves the track off the face
297+
point += step * dir;
298+
moved = true;
299+
} else if (moved) {
300+
// the chord reaches the lead only after a real step: the track is off the face
301+
freed = true;
302+
} else {
303+
++pushedChords;
304+
break;
305+
}
306+
nav.SetGeometricallyLimitedStep();
307+
nav.LocateGlobalPointAndSetup(point, &momentum, true, false);
308+
}
309+
BOOST_TEST_MESSAGE("zero steps " << zeroSteps);
310+
BOOST_CHECK_GE(zeroSteps, 10);
311+
// a lengthened chord step is a miss for the field propagator, which then never crosses
312+
BOOST_CHECK_EQUAL(pushedChords, 0);
313+
BOOST_CHECK(freed);
314+
}

0 commit comments

Comments
 (0)