Skip to content

Commit 01e6aa8

Browse files
sawenzelclaude
andcommitted
Fix the safety after a crossing in the propagating VecGeom navigator
This fixes the safety of the propagating VecGeom navigator (G4.vecgeomNavigator=kPropagated) and adds unit tests. - ComputeSafety answered 0 at every point until the next within-volume locate after a crossing, so the end point of the first step in a new volume had no safety. - It now answers 0 only at the point where the crossing was located and at the end point of a step that ends on a boundary, as VecGeomG4Navigator does. - The locate now consumes fWasLimitedByGeometry as G4Navigator does, so a new track is no longer taken for a crossing with the previous track's flags. - ResetHierarchyAndLocate now clears the crossing state left by the previous track. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 125348a commit 01e6aa8

3 files changed

Lines changed: 92 additions & 17 deletions

File tree

‎Detectors/gconfig/src/VecGeomG4PropagatingNavigator.cxx‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ namespace
3232
/// Geant4 abandons a track after ~50 zero steps, so a stalled step is nudged forward by this much
3333
/// (in Geant4 units) rather than returned as zero.
3434
const double kNudge = 1.e-3;
35+
36+
/// Two points closer than 1e-10 mm are the same point.
37+
constexpr double kSamePointDist2 = 1.e-20;
38+
/// A located point within 1e-8 mm of the end point of the last step is that end point.
39+
constexpr double kStepEndDist2 = 1.e-16;
40+
41+
bool samePoint(const G4ThreeVector& a, const G4ThreeVector& b) { return a.diff2(b) < kSamePointDist2; }
3542
} // namespace
3643

3744
namespace o2::simsetup
@@ -76,7 +83,7 @@ G4double VecGeomG4PropagatingNavigator::ComputeStep(const G4ThreeVector& globalP
7683
// The adopted state marks the volume the crossing left. As in Geant4, it is blocked in the first
7784
// step after the exit only, and only while the direction points away from it; a track turning
7885
// back into it must see its boundary.
79-
const bool block = mExitBlockPending && globalPoint.diff2(mLocatedPoint) < 1.e-20 &&
86+
const bool block = mExitBlockPending && samePoint(globalPoint, mLocatedPoint) &&
8087
directionLeaves(mPrevState, toVG(globalPoint), toDir(direction));
8188
mExitBlockPending = false;
8289
if (!block) {
@@ -130,11 +137,14 @@ G4VPhysicalVolume* VecGeomG4PropagatingNavigator::ResetHierarchyAndLocate(const
130137
{
131138
// Geant4 hands back a touchable it saved earlier, e.g. when resuming a track whose secondaries
132139
// were followed first. The VecGeom state is rebuilt from it.
140+
fWasLimitedByGeometry = false;
133141
fEnteredDaughter = false;
134142
fExitedMother = false;
135143
mWouldEnter = false;
136144
mWouldExit = false;
137145
mOnBoundary = false;
146+
mCrossed = false;
147+
mExitBlockPending = false;
138148
mHaveNextState = false;
139149
mLastSafety = -1.;
140150
fHistory = *history.GetHistory();
@@ -150,35 +160,35 @@ G4VPhysicalVolume* VecGeomG4PropagatingNavigator::LocateGlobalPointAndSetup(cons
150160
const G4ThreeVector*,
151161
const G4bool relativeSearch, const G4bool)
152162
{
153-
bool onBoundary = fWasLimitedByGeometry;
154-
if (mHaveNextState && point.diff2(mNextPoint) < 1.e-16) {
155-
onBoundary = true;
156-
}
163+
// As in G4Navigator, only a relative search can cross a boundary, and the locate consumes the flag.
164+
const bool onBoundary = relativeSearch && (fWasLimitedByGeometry || (mHaveNextState && point.diff2(mNextPoint) < kStepEndDist2));
165+
fWasLimitedByGeometry = false;
157166

158167
mPrevState = mCurState;
159168
mLocatedPoint = point;
160169
mExitBlockPending = false;
161170
mLastSafety = -1.;
162171

163-
if (!mForceReInit && relativeSearch && onBoundary && mHaveNextState) {
172+
if (!mForceReInit && onBoundary && mHaveNextState) {
164173
// The state on the far side of the boundary was already worked out, and relocated, by the step
165174
// that found it. Adopting it is cheaper than locating again.
166175
mCurState = mNextState;
167176
mExitBlockPending = mWouldExit;
168-
} else if (mForceReInit || !relativeSearch || onBoundary) {
177+
} else {
169178
mCurState.Clear();
170179
vecgeom::GlobalLocator::LocateGlobalPoint(vecgeom::GeoManager::Instance().GetWorld(), toVG(point), mCurState, true);
171180
mForceReInit = false;
172181
++mGlobalRelocates;
173182
}
174-
// Otherwise the point only moved inside the volume the state already names.
175183

176184
auto* target = updateHistory(mCurState);
177185
mCrossed = onBoundary;
178-
if (onBoundary) {
179-
fExitedMother = mWouldExit;
180-
fEnteredDaughter = mWouldEnter;
181-
mOnBoundary = true;
186+
mOnBoundary = onBoundary;
187+
fExitedMother = onBoundary && mWouldExit;
188+
fEnteredDaughter = onBoundary && mWouldEnter;
189+
if (!onBoundary) {
190+
// Kept after a crossing only, where GetGlobalExitNormal still reads mWouldExit.
191+
mWouldEnter = mWouldExit = false;
182192
}
183193
mHaveNextState = false;
184194
return target;
@@ -199,7 +209,14 @@ void VecGeomG4PropagatingNavigator::LocateGlobalPointWithinVolume(const G4ThreeV
199209
G4double VecGeomG4PropagatingNavigator::ComputeSafety(const G4ThreeVector& globalPoint, const G4double proposedMaxLength,
200210
const G4bool)
201211
{
202-
if (mZeroSafety || mOnBoundary || mCrossed || fEnteredDaughter || fExitedMother || mWouldEnter || mWouldExit) {
212+
if (mZeroSafety) {
213+
return 0.;
214+
}
215+
// Zero only on the face: where a crossing was located, or where the last step ends on a boundary.
216+
if (mCrossed && samePoint(globalPoint, mLocatedPoint)) {
217+
return 0.;
218+
}
219+
if ((mWouldEnter || mWouldExit) && samePoint(globalPoint, mNextPoint)) {
203220
return 0.;
204221
}
205222
if (mCurState.Top() == nullptr) {

‎Detectors/gconfig/src/VecGeomG4PropagatingNavigator.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ namespace o2::simsetup
1919

2020
/// A lighter VecGeom navigator for Geant4, selected with G4.vecgeomNavigator=kPropagated. ComputeStep
2121
/// uses VecGeom's combined step-and-relocate, and a boundary locate adopts the state that step
22-
/// propagated instead of locating again. Safety is zero for the whole step after a crossing, a stuck
23-
/// step is nudged forward, and the exit normal follows the direction of motion. It does less work per
24-
/// step and per crossing than VecGeomG4Navigator.
22+
/// propagated instead of locating again. Safety is zero only on the face of a crossing, a stuck step
23+
/// is nudged forward, and the exit normal follows the direction of motion. It does less work per step
24+
/// and per crossing than VecGeomG4Navigator.
2525
class VecGeomG4PropagatingNavigator : public VecGeomG4NavigatorBase
2626
{
2727
public:

‎Detectors/gconfig/test/testVecGeomG4Navigator.cxx‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/// The relocating VecGeom navigator against TGeo on a geometry of coinciding faces: two copies of a tower
1313
/// side by side that fill their mother, each a stack of tiles that reach the tower's faces, as in a sampling
1414
/// calorimeter. A ray has to visit the same volumes in the same order, and never stop for a zero-length step
15-
/// in a mother it only touches. Every case runs with the default locate and with the lean one.
15+
/// in a mother it only touches. Every case runs with the default locate and with the lean one. The propagating
16+
/// navigator has to answer a zero safety only on the face it crossed.
1617

1718
#define BOOST_TEST_MODULE Test SimSetup VecGeomG4Navigator
1819
#define BOOST_TEST_MAIN
@@ -24,6 +25,7 @@
2425

2526
#include "../src/VecGeomG4Map.h"
2627
#include "../src/VecGeomG4Navigator.h"
28+
#include "../src/VecGeomG4PropagatingNavigator.h"
2729

2830
#include "TG4RootDetectorConstruction.h"
2931

@@ -49,6 +51,7 @@
4951

5052
using o2::simsetup::VecGeomG4Map;
5153
using o2::simsetup::VecGeomG4Navigator;
54+
using o2::simsetup::VecGeomG4PropagatingNavigator;
5255

5356
namespace
5457
{
@@ -57,6 +60,7 @@ struct Fixture {
5760
VecGeomG4Map map;
5861
std::unique_ptr<VecGeomG4Navigator> navigator;
5962
std::unique_ptr<VecGeomG4Navigator> lean; // the same with G4.vecgeomLeanLocate
63+
std::unique_ptr<VecGeomG4PropagatingNavigator> propagating;
6064

6165
Fixture()
6266
{
@@ -93,6 +97,8 @@ struct Fixture {
9397
navigator->SetWorldVolume(world);
9498
lean = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false, nullptr, true);
9599
lean->SetWorldVolume(world);
100+
propagating = std::make_unique<VecGeomG4PropagatingNavigator>(map, false);
101+
propagating->SetWorldVolume(world);
96102
}
97103
};
98104

@@ -180,6 +186,24 @@ std::vector<std::string> afterBoundaries(G4Navigator& nav, const double* p, cons
180186
return paths;
181187
}
182188

189+
/// A new track in the paper of the first tile, 1 mm above its lead, steps down into the lead; returns the crossing point.
190+
G4ThreeVector enterLead(G4Navigator& nav)
191+
{
192+
const G4ThreeVector down(0., 0., -1.);
193+
G4ThreeVector point(-5. * cm, -5. * cm, -8.35 * cm);
194+
nav.LocateGlobalPointAndSetup(point, &down, false, false);
195+
G4double safety = 0.;
196+
point += nav.ComputeStep(point, down, 1.e6 * cm, safety) * down;
197+
nav.SetGeometricallyLimitedStep();
198+
nav.LocateGlobalPointAndSetup(point, &down, true, false);
199+
BOOST_REQUIRE(nav.EnteredDaughterVolume());
200+
BOOST_REQUIRE_EQUAL(touchablePath(nav).substr(touchablePath(nav).rfind('/')), "/LEAD_1");
201+
return point;
202+
}
203+
204+
/// A point in the paper of the fourth tile, 0.5 mm above its lower face and 1.5 mm below its lead.
205+
const G4ThreeVector inPaper(-5. * cm, -5. * cm, -1.70 * cm);
206+
183207
std::vector<Visit> withTGeo(const double* p, const double* d)
184208
{
185209
std::vector<Visit> visits;
@@ -458,3 +482,37 @@ BOOST_AUTO_TEST_CASE(a_track_pushed_from_place_to_place_is_aborted_after_its_fou
458482
}
459483
}
460484
}
485+
486+
BOOST_AUTO_TEST_CASE(the_propagating_navigator_has_a_zero_safety_only_on_the_face_it_crossed)
487+
{
488+
auto& nav = *fixture().propagating;
489+
const G4ThreeVector down(0., 0., -1.);
490+
const G4ThreeVector face = enterLead(nav);
491+
BOOST_CHECK_EQUAL(nav.ComputeSafety(face), 0.);
492+
// a 0.2 mm step into the lead; Geant4 asks for the safety at its end before the next locate
493+
G4double safety = 0.;
494+
BOOST_REQUIRE_GT(nav.ComputeStep(face, down, 0.2 * mm, safety), 0.2 * mm);
495+
BOOST_CHECK_CLOSE(nav.ComputeSafety(face + 0.2 * mm * down), 0.2 * mm, 1.e-4);
496+
}
497+
498+
BOOST_AUTO_TEST_CASE(a_new_track_after_a_crossing_is_not_on_a_boundary)
499+
{
500+
auto& nav = *fixture().propagating;
501+
enterLead(nav);
502+
const G4ThreeVector up(0., 0., 1.);
503+
nav.LocateGlobalPointAndSetup(inPaper, &up, false, false);
504+
BOOST_CHECK(!nav.EnteredDaughterVolume());
505+
BOOST_CHECK(!nav.ExitedMotherVolume());
506+
BOOST_CHECK_CLOSE(nav.ComputeSafety(inPaper), 0.5 * mm, 1.e-4);
507+
}
508+
509+
BOOST_AUTO_TEST_CASE(a_resumed_track_after_a_crossing_is_not_on_a_boundary)
510+
{
511+
auto& nav = *fixture().propagating;
512+
const G4ThreeVector up(0., 0., 1.);
513+
nav.LocateGlobalPointAndSetup(inPaper, &up, false, false);
514+
std::unique_ptr<G4TouchableHistory> touchable(nav.CreateTouchableHistory());
515+
enterLead(nav);
516+
nav.ResetHierarchyAndLocate(inPaper, up, *touchable);
517+
BOOST_CHECK_CLOSE(nav.ComputeSafety(inPaper), 0.5 * mm, 1.e-4);
518+
}

0 commit comments

Comments
 (0)