From 4e0b46e2508aa888ca9174957ab98fc48eac2b98 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Tue, 17 Nov 2026 16:07:57 +0100 Subject: [PATCH 1/2] 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 --- Detectors/gconfig/src/VecGeomG4Navigator.cxx | 12 ++-- Detectors/gconfig/src/VecGeomG4Navigator.h | 1 + .../gconfig/test/testVecGeomG4Navigator.cxx | 59 +++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.cxx b/Detectors/gconfig/src/VecGeomG4Navigator.cxx index 7fdb2c8b1bff6..00758781539fb 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.cxx +++ b/Detectors/gconfig/src/VecGeomG4Navigator.cxx @@ -190,6 +190,7 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const mWouldEnter = entering; mWouldExit = !entering; mNextPoint = globalPoint + step * direction; + mNextDir = toDir(direction); if (entering) { mNextState = mStepState; } @@ -209,9 +210,9 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const /// the surface of and heading out of, then look down again from there, never back into the volume /// just left, which stays blocked for the next step. Bounded, because popping and descending can /// meet another flush face. It never pops to or above minLevel: a daughter the step entered stays -/// entered, because the step decided that with the chord direction, and the direction a locate gets -/// under a field is the momentum. Nor does it descend back into `avoid`, the volume the crossing -/// exited: on a face a helix touches tangentially chord and momentum disagree about the side. +/// entered, as the step decided. Nor does it descend back into `avoid`, the volume the crossing +/// exited. Both guard the decision of the step against a locate direction that disagrees with it +/// about the side of the face. void VecGeomG4Navigator::leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::NavigationState const* avoid) { @@ -301,7 +302,10 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe const bool crossing = onBoundary && (mWouldEnter || mWouldExit) && direction != nullptr; const bool entering = crossing && mWouldEnter; const V3 p = toVG(point); - const V3 dir = direction != nullptr ? toDir(*direction) : V3(0., 0., 0.); + // A crossing is located along the direction of the step that found the boundary. The direction given + // here is the one after the step, which multiple scattering can have turned back into the volume left; + // the track then belongs to what lies behind the face, as G4Navigator does, not to the mother of the two. + const V3 dir = crossing ? mNextDir : (direction != nullptr ? toDir(*direction) : V3(0., 0., 0.)); const double push = (onBoundary && direction != nullptr) ? boundaryPush(p, dir) : 0.; const V3 q = p + push * dir; diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.h b/Detectors/gconfig/src/VecGeomG4Navigator.h index 937f909aeca0c..03c119baac311 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.h +++ b/Detectors/gconfig/src/VecGeomG4Navigator.h @@ -107,6 +107,7 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase bool mWouldEnter = false; ///< the last ComputeStep ends by entering a daughter bool mWouldExit = false; ///< the last ComputeStep ends by leaving the current volume G4ThreeVector mNextPoint{-1e8, -1e8, -1e8}; + vecgeom::Vector3D mNextDir{0., 0., 1.}; ///< direction of the step that found the boundary G4ThreeVector mLastLocatedPoint{-1e8, -1e8, -1e8}; bool mLocatedOnBoundary = false; G4ThreeVector mSafetyOrig{-1e8, -1e8, -1e8}; ///< the last point a safety was computed for diff --git a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx index 85fcd47436dac..4ca6594f9cd8f 100644 --- a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx +++ b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx @@ -143,6 +143,32 @@ std::vector withNavigator(G4Navigator& nav, const double* p, const double return visits; } +/// Where the navigator is after each boundary along a ray. The locate after boundary `turnAt` gets the +/// reversed direction, as after a scattering at the face, and the walk ends there. +std::vector afterBoundaries(G4Navigator& nav, const double* p, const double* d, int turnAt) +{ + std::vector paths; + G4ThreeVector point(p[0] * cm, p[1] * cm, p[2] * cm); + const G4ThreeVector dir(d[0], d[1], d[2]); + auto* pv = nav.LocateGlobalPointAndSetup(point, &dir, false, false); + for (int k = 0; k < 10000 && pv != nullptr; ++k) { + G4double safety = 0.; + const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety); + if (!(step < 1.e6 * cm)) { + break; + } + point += step * dir; + nav.SetGeometricallyLimitedStep(); + const G4ThreeVector after = k == turnAt ? -dir : dir; + pv = nav.LocateGlobalPointAndSetup(point, &after, true, false); + paths.push_back(pv != nullptr ? touchablePath(nav) : ""); + if (k == turnAt) { + break; + } + } + return paths; +} + std::vector withTGeo(const double* p, const double* d) { std::vector visits; @@ -204,3 +230,36 @@ BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits) // every face here coincides with others, and none of them is a place to stop BOOST_CHECK_EQUAL(zeroSteps, 0); } + +BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered) +{ + auto& f = fixture(); + std::mt19937_64 gen(2); + std::uniform_real_distribution u(-1., 1.); + std::normal_distribution g; + int boundaries = 0, different = 0; + std::string firstDifference; + for (int r = 0; r < 2000; ++r) { + const double p[3] = {9.9 * u(gen), 9.9 * u(gen), 9.9 * u(gen)}; + double d[3] = {g(gen), g(gen), 0.3 * g(gen)}; + const double norm = std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); + for (auto& x : d) { + x /= norm; + } + const auto straight = afterBoundaries(*f.navigator, p, d, -1); + for (int k = 0; k < static_cast(straight.size()); ++k) { + const auto turned = afterBoundaries(*f.navigator, p, d, k); + ++boundaries; + if (turned.size() != static_cast(k) + 1 || turned.back() != straight[k]) { + ++different; + if (firstDifference.empty()) { + firstDifference = Form("ray %d boundary %d: %s, without the turn %s", r, k, turned.empty() ? "-" : turned.back().c_str(), straight[k].c_str()); + } + } + } + } + BOOST_TEST_MESSAGE("boundaries " << boundaries << " " << firstDifference); + BOOST_CHECK_GT(boundaries, 10000); + // the step decides which volume is entered, not the direction the track has after it + BOOST_CHECK_MESSAGE(different == 0, different << " boundaries where the turn changed the volume; " << firstDifference); +} From 6abbc36398f03a65ce5d2fe268fc96f5c4f72f0a Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Wed, 18 Nov 2026 09:44:36 +0100 Subject: [PATCH 2/2] 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 --- Detectors/gconfig/src/VecGeomG4Navigator.cxx | 34 ++++++++----- Detectors/gconfig/src/VecGeomG4Navigator.h | 7 +++ .../gconfig/test/testVecGeomG4Navigator.cxx | 49 +++++++++++++++++++ 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.cxx b/Detectors/gconfig/src/VecGeomG4Navigator.cxx index 00758781539fb..e7340cfe799bc 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.cxx +++ b/Detectors/gconfig/src/VecGeomG4Navigator.cxx @@ -164,26 +164,33 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const // lengthened, after twenty-five the event is aborted. if (step < 0.05 * kCarTolerance) { ++mZeroStepCount; + // Without a field Geant4 asks along the momentum it located the track with, and takes a lengthened step. + // The field propagator asks along chords and takes a step longer than its chord for a miss. + const bool linear = (direction - mLastLocatedDir).mag2() < 1.e-18; + mZeroLinearSeen = mZeroLinearSeen || linear; if (++mNzeroSteps >= kActionThresholdNoZeroSteps) { - ++mStuckPushCount; - // The push grows from Geant4's 100 kCarTolerance fourfold per try to a micron and doubles from the - // eleventh try on: a track tangent to a curved face stays within the tolerance of it for microns. - const int tries = mNzeroSteps - kActionThresholdNoZeroSteps; - constexpr double kMicron = 1.e-3 * CLHEP::mm; - step += tries < 10 ? std::min(100. * kCarTolerance * std::pow(4., tries), kMicron) - : kMicron * static_cast(2 << (tries - 10)); + // A track going back and forth over a face between a medium with a field and one without is tangent + // to the face: the chord crosses it one way and the momentum the other way. The crossing along the + // chord is left at zero length, so that it takes place, and the step along the momentum is lengthened. + if (linear || !mZeroLinearSeen) { + ++mStuckPushCount; + // The push grows fourfold per try from 100 kCarTolerance, capped below the thinnest layers (15 um). + constexpr double kMaxStuckPush = 10. * CLHEP::um; + const int tries = mNzeroSteps - kActionThresholdNoZeroSteps; + step += std::min(100. * kCarTolerance * std::pow(4., tries), kMaxStuckPush); + } if (mNzeroSteps >= kAbandonThresholdNoZeroSteps) { ++mAbandonCount; std::ostringstream msg; msg << "Track stuck or not moving: " << mNzeroSteps << " zero steps in " << top->GetLabel() << " at (" << globalPoint.x() << ", " << globalPoint.y() << ", " << globalPoint.z() << ") mm. Event aborted, as " << "G4Navigator does."; - mNzeroSteps = 0; + resetZeroSteps(); G4Exception("VecGeomG4Navigator::ComputeStep()", "GeomNav0003", EventMustBeAborted, msg.str().c_str()); } } } else { - mNzeroSteps = 0; + resetZeroSteps(); } if (boundaryLimited) { @@ -314,11 +321,12 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe fExitedMother = false; mLocatedOnBoundary = false; mLastLocatedPoint = point; + mLastLocatedDir = direction != nullptr ? *direction : G4ThreeVector(0., 0., 0.); mSafetyOrig = kNoPoint; clearLastExited(); if (!relativeSearch) { - mNzeroSteps = 0; // a new track: nothing of the previous one applies + resetZeroSteps(); // a new track: nothing of the previous one applies locateFromWorld(p); } else if (topOf(mCurState) == nullptr) { locateFromWorld(p); @@ -383,7 +391,8 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe return updateG4History(); } -G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVector& point, const G4ThreeVector&, +G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVector& point, + const G4ThreeVector& direction, const G4TouchableHistory& history) { // 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 mWouldExit = false; mLocatedOnBoundary = false; mLastLocatedPoint = point; + mLastLocatedDir = direction; mSafetyOrig = kNoPoint; - mNzeroSteps = 0; + resetZeroSteps(); fHistory = *history.GetHistory(); const V3 p = toVG(point); bool kept = false; diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.h b/Detectors/gconfig/src/VecGeomG4Navigator.h index 03c119baac311..47613fdaa6888 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.h +++ b/Detectors/gconfig/src/VecGeomG4Navigator.h @@ -121,6 +121,13 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase static constexpr int kActionThresholdNoZeroSteps = 10; static constexpr int kAbandonThresholdNoZeroSteps = 25; int mNzeroSteps = 0; + bool mZeroLinearSeen = false; ///< a step along the located momentum occurred in the current run of zero steps + void resetZeroSteps() + { + mNzeroSteps = 0; + mZeroLinearSeen = false; + } + G4ThreeVector mLastLocatedDir; ///< the direction the last locate was given // What the navigator had to work around, reported at the end. long mZeroStepCount = 0; diff --git a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx index 4ca6594f9cd8f..b1cc9b90da0f8 100644 --- a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx +++ b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx @@ -263,3 +263,52 @@ BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered) // the step decides which volume is entered, not the direction the track has after it BOOST_CHECK_MESSAGE(different == 0, different << " boundaries where the turn changed the volume; " << firstDifference); } + +BOOST_AUTO_TEST_CASE(a_track_tangent_to_a_field_boundary_is_freed_along_its_momentum) +{ + // 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. + auto& nav = *fixture().navigator; + const G4ThreeVector face(-5. * cm, -5. * cm, -8.45 * cm); + const G4ThreeVector chord = G4ThreeVector(1., 0., -1.e-3).unit(); + const G4ThreeVector momentum = G4ThreeVector(1., 0., 1.e-3).unit(); + G4double safety = 0.; + // reach the face along the chord from the paper above, so the track ends in the lead + G4ThreeVector point = face - 1. * mm * chord; + nav.LocateGlobalPointAndSetup(point, &chord, false, false); + const double first = nav.ComputeStep(point, chord, 1.e6 * cm, safety); + BOOST_REQUIRE_CLOSE(first, 1. * mm, 1.e-6); + point += first * chord; + nav.SetGeometricallyLimitedStep(); + nav.LocateGlobalPointAndSetup(point, &momentum, true, false); + BOOST_REQUIRE_EQUAL(touchablePath(nav).substr(touchablePath(nav).rfind('/')), "/LEAD_1"); + + // Geant4 alternates a straight step along the momentum, which leaves the lead, and a chord step, which enters it again. + int zeroSteps = 0, pushedChords = 0; + bool moved = false, freed = false; + for (int k = 0; k < 24 && !freed; ++k) { + const bool alongMomentum = k % 2 == 0; + const G4ThreeVector& dir = alongMomentum ? momentum : chord; + const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety); + BOOST_TEST_MESSAGE("step " << k << (alongMomentum ? " momentum " : " chord ") << step << " mm in " << touchablePath(nav)); + if (step < 1.e-10 * mm) { + ++zeroSteps; + } else if (alongMomentum) { + // the lengthened momentum step moves the track off the face + point += step * dir; + moved = true; + } else if (moved) { + // the chord reaches the lead only after a real step: the track is off the face + freed = true; + } else { + ++pushedChords; + break; + } + nav.SetGeometricallyLimitedStep(); + nav.LocateGlobalPointAndSetup(point, &momentum, true, false); + } + BOOST_TEST_MESSAGE("zero steps " << zeroSteps); + BOOST_CHECK_GE(zeroSteps, 10); + // a lengthened chord step is a miss for the field propagator, which then never crosses + BOOST_CHECK_EQUAL(pushedChords, 0); + BOOST_CHECK(freed); +}