Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions Detectors/gconfig/src/VecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,40 @@ 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<double>(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) {
mWouldEnter = entering;
mWouldExit = !entering;
mNextPoint = globalPoint + step * direction;
mNextDir = toDir(direction);
if (entering) {
mNextState = mStepState;
}
Expand All @@ -209,9 +217,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)
{
Expand Down Expand Up @@ -301,7 +309,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;

Expand All @@ -310,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);
Expand Down Expand Up @@ -379,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
Expand All @@ -391,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;
Expand Down
8 changes: 8 additions & 0 deletions Detectors/gconfig/src/VecGeomG4Navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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
Expand All @@ -120,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;
Expand Down
108 changes: 108 additions & 0 deletions Detectors/gconfig/test/testVecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ std::vector<Visit> 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<std::string> afterBoundaries(G4Navigator& nav, const double* p, const double* d, int turnAt)
{
std::vector<std::string> 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<Visit> withTGeo(const double* p, const double* d)
{
std::vector<Visit> visits;
Expand Down Expand Up @@ -204,3 +230,85 @@ 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<double> u(-1., 1.);
std::normal_distribution<double> 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<int>(straight.size()); ++k) {
const auto turned = afterBoundaries(*f.navigator, p, d, k);
++boundaries;
if (turned.size() != static_cast<std::size_t>(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);
}

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);
}
Loading