From 2c15fc28bda7c7a0e71068961a72b3e1b9140bfc Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Wed, 2 Dec 2026 12:37:34 +0100 Subject: [PATCH 1/2] Fix the safety clip with the parallel world in the VecGeom navigator This fixes the safety of the relocating VecGeom navigator with the parallel world and adds a unit test. - The nodes were searched only up to the caller's bound, and the safety never exceeded it. - Tracks far from any node were therefore transported differently, which caused the deficit of TPC tracks with at least 150 clusters. - The node search now goes up to the full safety. https://its.cern.ch/jira/browse/O2-7358 --- Detectors/gconfig/src/VecGeomG4Navigator.cxx | 4 +- .../gconfig/test/testVecGeomG4Navigator.cxx | 52 ++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.cxx b/Detectors/gconfig/src/VecGeomG4Navigator.cxx index 50dac99347630..370867f11e14f 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.cxx +++ b/Detectors/gconfig/src/VecGeomG4Navigator.cxx @@ -592,8 +592,8 @@ G4double VecGeomG4Navigator::ComputeSafety(const G4ThreeVector& globalPoint, con } double safety = boundedSafety(mCurState, globalPoint, proposedMaxLength); if (mParallelWorld != nullptr && !mInParallelNode && safety > 0.) { - // Up to the caller's bound only; the bound returned for a farther node is a valid lower bound. - safety = mParallelWorld->safetyToIn(toVG(globalPoint), std::min(safety, proposedMaxLength) * kG4ToVG) * kVGToG4; + // Up to the full safety, so that a node farther away leaves it as it is without the parallel world. + safety = mParallelWorld->safetyToIn(toVG(globalPoint), safety * kG4ToVG) * kVGToG4; } if (safety < 0.) { ++mNegativeSafetyCount; diff --git a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx index 6efbbf1d5963d..0a393bd4446cc 100644 --- a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx +++ b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx @@ -13,7 +13,8 @@ /// side by side that fill their mother, each a stack of tiles that reach the tower's faces, as in a sampling /// calorimeter. A ray has to visit the same volumes in the same order, and never stop for a zero-length step /// in a mother it only touches. Every case runs with the default locate and with the lean one. The propagating -/// navigator has to answer a zero safety only on the face it crossed. +/// navigator has to answer a zero safety only on the face it crossed. A parallel world node far from a point +/// must not change its safety. #define BOOST_TEST_MODULE Test SimSetup VecGeomG4Navigator #define BOOST_TEST_MAIN @@ -26,6 +27,7 @@ #include "../src/VecGeomG4Map.h" #include "../src/VecGeomG4Navigator.h" #include "../src/VecGeomG4PropagatingNavigator.h" +#include "../src/VecGeomParallelWorld.h" #include "TG4RootDetectorConstruction.h" @@ -35,6 +37,9 @@ #include "G4VExceptionHandler.hh" #include "G4VPhysicalVolume.hh" +#include +#include + #include #include #include @@ -52,6 +57,7 @@ using o2::simsetup::VecGeomG4Map; using o2::simsetup::VecGeomG4Navigator; using o2::simsetup::VecGeomG4PropagatingNavigator; +using o2::simsetup::VecGeomParallelWorld; namespace { @@ -61,6 +67,8 @@ struct Fixture { std::unique_ptr navigator; std::unique_ptr lean; // the same with G4.vecgeomLeanLocate std::unique_ptr propagating; + VecGeomParallelWorld parallelWorld; // one node: the first lead tile of module 3 + std::unique_ptr leanParallel; // the lean navigator with that parallel world Fixture() { @@ -99,6 +107,14 @@ struct Fixture { lean->SetWorldVolume(world); propagating = std::make_unique(map, false); propagating->SetWorldVolume(world); + vecgeom::NavigationState node; + node.Clear(); + vecgeom::GlobalLocator::LocateGlobalPoint(vecgeom::GeoManager::Instance().GetWorld(), + vecgeom::Vector3D(35., 0., -8.75), node, true); + BOOST_REQUIRE_EQUAL(node.Top()->GetLogicalVolume()->GetName(), std::string("LEAD")); + parallelWorld.build({node}); + leanParallel = std::make_unique(map, 1.e-9, false, ¶llelWorld, true); + leanParallel->SetWorldVolume(world); } }; @@ -529,3 +545,37 @@ BOOST_AUTO_TEST_CASE(a_resumed_track_after_a_crossing_is_not_on_a_boundary) nav.ResetHierarchyAndLocate(inPaper, up, *touchable); BOOST_CHECK_CLOSE(nav.ComputeSafety(inPaper), 0.5 * mm, 1.e-4); } + +BOOST_AUTO_TEST_CASE(a_far_parallel_world_node_does_not_change_the_safety) +{ + auto& plain = withSetup(*fixture().lean); + auto& withNode = *fixture().leanParallel; + std::mt19937_64 gen(5); + std::uniform_real_distribution u(-99., 99.); + int checked = 0, different = 0; + std::string firstDifference; + for (int i = 0; i < 5000 && checked < 1000; ++i) { + // in the world volume, at least 1 cm from every module + const G4ThreeVector p(u(gen) * cm, u(gen) * cm, u(gen) * cm); + const bool nearModules = std::abs(p.x()) < 11. * cm && std::abs(p.y()) < 11. * cm && std::abs(p.z()) < 11. * cm; + const bool nearModule3 = std::abs(p.x() - 40. * cm) < 11. * cm && std::abs(p.y()) < 6. * cm && std::abs(p.z()) < 11. * cm; + if (nearModules || nearModule3) { + continue; + } + ++checked; + const G4ThreeVector dir(0., 0., 1.); + for (double bound : {0.1 * mm, 1. * mm, 10. * mm, 1.e4 * mm}) { + plain.LocateGlobalPointAndSetup(p, &dir, false, false); + withNode.LocateGlobalPointAndSetup(p, &dir, false, false); + const double expected = plain.ComputeSafety(p, bound); + const double found = withNode.ComputeSafety(p, bound); + if (std::abs(found - expected) > 1.e-9 * mm) { + if (different++ == 0) { + firstDifference = Form("at (%g, %g, %g) mm, bound %g mm: %g mm, without the node %g mm", p.x(), p.y(), p.z(), bound, found, expected); + } + } + } + } + BOOST_CHECK_GT(checked, 500); + BOOST_CHECK_MESSAGE(different == 0, different << " safeties changed by the node; " << firstDifference); +} From fa3bf42b5d54018b21e2808030f85f64e35ec7c5 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Wed, 2 Dec 2026 12:37:34 +0100 Subject: [PATCH 2/2] Fix the zero-step exit after a boundary crossing in the VecGeom navigator This fixes a spurious zero-length step after a crossing in the relocating VecGeom navigator and adds unit tests. - In a field Geant4 can end a step slightly short of the face it crosses. - The lean locate finds the next volume beyond the face, so the next step from Geant4's point was a zero-step exit and a 1 nm visit to the mother. - At the TPC inner field cage this gave a spurious TPC track reference for about 8 % of slow near-radial protons. - Such a zero step is now redone from where the step direction enters the volume. https://its.cern.ch/jira/browse/O2-7355 --- Detectors/gconfig/src/VecGeomG4Navigator.cxx | 35 ++++++- Detectors/gconfig/src/VecGeomG4Navigator.h | 12 ++- .../gconfig/test/testVecGeomG4Navigator.cxx | 98 ++++++++++++++++++- 3 files changed, 140 insertions(+), 5 deletions(-) diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.cxx b/Detectors/gconfig/src/VecGeomG4Navigator.cxx index 370867f11e14f..c84be50c28506 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.cxx +++ b/Detectors/gconfig/src/VecGeomG4Navigator.cxx @@ -66,7 +66,8 @@ VecGeomG4Navigator::~VecGeomG4Navigator() LOG(info) << "VecGeom navigation: zero steps " << mZeroStepCount << ", stuck pushes " << mStuckPushCount << ", abandoned " << mAbandonCount << ", negative safeties " << mNegativeSafetyCount << ", unmappable touchables " << mUnmappableHistoryCount << ", relocated resumes " - << mRelocatedResumeCount << ", missing exit normals " << mNoNormalCount; + << mRelocatedResumeCount << ", missing exit normals " << mNoNormalCount << ", crossings restepped " + << mCrossingRestepCount; if (mParallelWorld != nullptr) { LOG(info) << "VecGeom parallel world: " << mParallelEntries << " steps ended on a node, " << mParallelRelocated << " locates overridden"; @@ -115,6 +116,14 @@ double VecGeomG4Navigator::boundaryPush(const V3& point, const V3& dir) return std::max(rounding, std::min(mPushDepth / cosn, kMaxPush)); } +double VecGeomG4Navigator::distanceToCurrentVolume(const V3& point, const V3& dir) const +{ + vecgeom::Transformation3D m; + mCurState.TopMatrix(m); + return mCurState.Top()->GetUnplacedVolume()->DistanceToIn(m.Transform(point), m.TransformDirection(dir), + vecgeom::kInfLength); +} + G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const G4ThreeVector& direction, const G4double proposedStepLength, G4double& newSafety) { @@ -161,6 +170,27 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const double safety = 0.; double vgStep = navigator->ComputeStepAndSafety(toVG(globalPoint), toDir(direction), limit, mStepState, calcSafety, safety, true); + // Under a field a step can end a little short of the face it crosses. The volume the lean locate found from the + // pushed point then does not hold the point and VecGeom gives a zero step out of it. If the direction reaches the + // volume just ahead, that exit is spurious: step from where the direction enters it. + if (vgStep < 0.05 * kCarTolerance * kG4ToVG && mLeanLocate && samePoint(globalPoint, mPushedFrom) && + mStepState.GetCurrentLevel() <= mCurState.GetCurrentLevel()) { + const V3 p = toVG(globalPoint), dir = toDir(direction); + const double toFace = distanceToCurrentVolume(p, dir); + if (toFace >= 0. && toFace < std::min(kMaxFaceShortfall, limit)) { + mPathScratch = mStepState; + mStepState = mCurState; + double unused = 0.; + const double across = + navigator->ComputeStepAndSafety(p + toFace * dir, dir, limit - toFace, mStepState, false, unused, true); + if (across > 0.) { + vgStep = toFace + across; + ++mCrossingRestepCount; + } else { + mStepState = mPathScratch; + } + } + } // Outside the parallel world its nodes limit the step and the safety like daughters of every volume; // inside a node they are not seen, as in TGeo. A node at the distance of the main boundary is left to // the main hierarchy, which is safe because the locate descends from what was entered and then asks the @@ -379,6 +409,8 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe mLocatedOnBoundary = false; mLastLocatedPoint = point; mLastLocatedDir = direction != nullptr ? *direction : G4ThreeVector(0., 0., 0.); + // Kept by a locate within the volume, which the field propagator makes at this point before its chords. + mPushedFrom = push > 0. ? point : kNoPoint; mSafetyOrig = kNoPoint; clearLastExited(); @@ -516,6 +548,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVect mLocatedOnBoundary = false; mLastLocatedPoint = point; mLastLocatedDir = direction; + mPushedFrom = kNoPoint; mSafetyOrig = kNoPoint; resetZeroSteps(); fHistory = *history.GetHistory(); diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.h b/Detectors/gconfig/src/VecGeomG4Navigator.h index 5193db80b48f9..b4035603a5cc2 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.h +++ b/Detectors/gconfig/src/VecGeomG4Navigator.h @@ -100,6 +100,8 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase /// How far (cm) a boundary point is pushed along the direction before it is located. Keeps the /// global normal of the face in mPushNormal, for the exit block. double boundaryPush(const V3& point, const V3& dir); + /// The distance (cm) along \a dir from \a point, outside the current volume, to where it enters it. + double distanceToCurrentVolume(const V3& point, const V3& dir) const; /// The normal of the surface the last geometry-limited ComputeStep ended on, global, unit length. bool computeExitNormal(const G4ThreeVector& point, V3& globalNormal) const; @@ -130,6 +132,9 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase vecgeom::Vector3D mNextDir{0., 0., 1.}; ///< direction of the step that found the boundary G4ThreeVector mLastLocatedPoint{-1e8, -1e8, -1e8}; bool mLocatedOnBoundary = false; + G4ThreeVector mPushedFrom{-1e8, -1e8, -1e8}; ///< the point the last locate pushed before locating + /// How far short of the face (cm along the step) a point that the lean locate pushed past it can be. + static constexpr double kMaxFaceShortfall = 1.e-4; G4ThreeVector mSafetyOrig{-1e8, -1e8, -1e8}; ///< the last point a safety was computed for double mLastSafety = 0.; ///< mm double mLastSafetyLimit = kInfinity; ///< mm; mLastSafety is exact below it, a lower bound above @@ -175,9 +180,10 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase long mUnmappableHistoryCount = 0; long mRelocatedResumeCount = 0; long mNoNormalCount = 0; - long mParallelEntries = 0; ///< steps that ended on a parallel world node - long mParallelRelocated = 0; ///< locates the parallel world overrode - long mPushRestartCount = 0; ///< runs of zero steps restarted after a push + long mParallelEntries = 0; ///< steps that ended on a parallel world node + long mParallelRelocated = 0; ///< locates the parallel world overrode + long mPushRestartCount = 0; ///< runs of zero steps restarted after a push + long mCrossingRestepCount = 0; ///< steps after a crossing redone from the face ahead }; } // namespace o2::simsetup diff --git a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx index 0a393bd4446cc..1827c62063b99 100644 --- a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx +++ b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx @@ -14,7 +14,8 @@ /// calorimeter. A ray has to visit the same volumes in the same order, and never stop for a zero-length step /// in a mother it only touches. Every case runs with the default locate and with the lean one. The propagating /// navigator has to answer a zero safety only on the face it crossed. A parallel world node far from a point -/// must not change its safety. +/// must not change its safety, and the step after a crossing that ends short of the face must not be a zero-step +/// exit. #define BOOST_TEST_MODULE Test SimSetup VecGeomG4Navigator #define BOOST_TEST_MAIN @@ -579,3 +580,98 @@ BOOST_AUTO_TEST_CASE(a_far_parallel_world_node_does_not_change_the_safety) BOOST_CHECK_GT(checked, 500); BOOST_CHECK_MESSAGE(different == 0, different << " safeties changed by the node; " << firstDifference); } + +/// TGeo's answer for a point short of a face: the volume behind the face, the distance across it from the point, +/// and the volume after it. TGeo starts 0.1 um further back, so that a point on the face is not ambiguous. +struct Across { + std::string entered, after; + double length; // mm +}; +Across acrossWithTGeo(const G4ThreeVector& p, const G4ThreeVector& d) +{ + constexpr double back = 1.e-5; // cm + const G4ThreeVector s = p / cm - back * d; + gGeoManager->InitTrack(s.x(), s.y(), s.z(), d.x(), d.y(), d.z()); + gGeoManager->FindNextBoundaryAndStep(); + double length = gGeoManager->GetStep() - back; + Across a; + a.entered = gGeoManager->GetPath(); + gGeoManager->FindNextBoundaryAndStep(); + length += gGeoManager->GetStep(); + a.after = gGeoManager->GetPath(); + a.length = length * cm; + return a; +} + +BOOST_AUTO_TEST_CASE(the_step_after_a_crossing_short_of_the_face_is_not_a_zero_step_exit) +{ + // Under a field Geant4 can end a step a little short of the face it crosses, and the next step can have another + // direction. The lean locate finds the next volume from the point pushed past the face; the next step, from + // Geant4's point, must cross that volume along its own direction, as far as TGeo says. + struct Crossing { + std::string name; + G4ThreeVector start, dir; // the step that reaches the face + std::vector next; // directions of the step after it + }; + const G4ThreeVector down = G4ThreeVector(0.02, 0.01, -1.).unit(), side = G4ThreeVector(1., 0.01, 0.).unit(); + const std::vector crossings = { + // from a paper tile into its lead tile, a daughter + {"into a daughter", G4ThreeVector(-5. * cm, -5. * cm, -8.35 * cm), down, {down, G4ThreeVector(0.5, 0.3, -1.).unit(), G4ThreeVector(1.5, 0., -1.).unit()}}, + // from a paper tile of tower 1 into the flush paper tile of tower 2, a daughter of a sibling: an exit, then out + // of the module into the world + {"into a sibling's daughter", G4ThreeVector(-1. * cm, -5. * cm, -8.32 * cm), side, {side, G4ThreeVector(1., 0.35, 0.).unit(), G4ThreeVector(1., -0.2, 0.01).unit()}}}; + auto& nav = withSetup(*fixture().lean); + for (auto const& c : crossings) { + // the field propagator locates within the volume before its chords + for (bool withinVolume : {false, true}) { + for (std::size_t k = 0; k < c.next.size(); ++k) { + for (double shortBy : {0., 1.e-9 * cm, 1.e-8 * cm, 1.e-7 * cm, 5.e-7 * cm}) { + BOOST_TEST_CONTEXT(c.name << (withinVolume ? ", located within the volume" : "") << ", next direction " << k + << ", short of the face by " << shortBy << " mm") + { + G4ThreeVector point = c.start; + nav.LocateGlobalPointAndSetup(point, &c.dir, false, false); + G4double safety = 0.; + const double toFace = nav.ComputeStep(point, c.dir, 1.e6 * cm, safety); + point += (toFace - shortBy) * c.dir; + const G4ThreeVector& dir = c.next[k]; + const auto expected = acrossWithTGeo(point, dir); + nav.SetGeometricallyLimitedStep(); + nav.LocateGlobalPointAndSetup(point, &dir, true, false); + BOOST_REQUIRE_EQUAL(touchablePath(nav), expected.entered); + if (withinVolume) { + nav.LocateGlobalPointWithinVolume(point); + } + const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety); + BOOST_CHECK_SMALL(step - expected.length, 1.e-8 * mm); // TGeo rounds a crossing to 1e-10 cm + point += step * dir; + nav.SetGeometricallyLimitedStep(); + nav.LocateGlobalPointAndSetup(point, &dir, true, false); + BOOST_CHECK_EQUAL(touchablePath(nav), expected.after); + } + } + } + } + } +} + +BOOST_AUTO_TEST_CASE(a_track_turned_back_at_a_face_it_ended_short_of_leaves_at_once) +{ + // The same crossing with the next step turned back: the track never reaches the volume ahead, so the step is a + // zero-step exit, as before. + auto& nav = withSetup(*fixture().lean); + const G4ThreeVector down = G4ThreeVector(0.02, 0.01, -1.).unit(); + for (double shortBy : {1.e-8 * cm, 1.e-7 * cm, 5.e-7 * cm}) { + BOOST_TEST_CONTEXT("short of the face by " << shortBy << " mm") + { + G4ThreeVector point(-5. * cm, -5. * cm, -8.35 * cm); + nav.LocateGlobalPointAndSetup(point, &down, false, false); + G4double safety = 0.; + point += (nav.ComputeStep(point, down, 1.e6 * cm, safety) - shortBy) * down; + nav.SetGeometricallyLimitedStep(); + const G4ThreeVector back = -down; + nav.LocateGlobalPointAndSetup(point, &back, true, false); + BOOST_CHECK_LT(nav.ComputeStep(point, back, 1.e6 * cm, safety), 1.e-10 * mm); + } + } +}