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
2 changes: 2 additions & 0 deletions Common/SimConfig/include/SimConfig/G4Params.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct G4Params : public o2::conf::ConfigurableParamHelper<G4Params> {
EVecGeomNav vecgeomNavigator = EVecGeomNav::kRelocating;
double vecgeomPushDepth = 1.e-9; // cm; how far past a face, measured across it, a boundary
// point is pushed before it is located
bool vecgeomLeanLocate = false; // kRelocating: locate a crossing 10 nm along the step, as VecGeom
// relocates, instead of across the face with the flush-volume loop
bool vecgeomZeroSafety = false; // answer zero to every safety query; conservative, but it
// shortens steps and so changes the random history
bool vecgeomFlattenAssemblies = true; // dissolve TGeo assemblies into their content when converting
Expand Down
85 changes: 62 additions & 23 deletions Detectors/gconfig/src/VecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <VecGeom/base/Transformation3D.h>
#include <VecGeom/management/GeoManager.h>
#include <VecGeom/navigation/BVHNavigatorV.h>
#include <VecGeom/navigation/GlobalLocator.h>
#include <VecGeom/navigation/VNavigator.h>
#include <VecGeom/navigation/VSafetyEstimator.h>
Expand Down Expand Up @@ -48,8 +49,12 @@ namespace o2::simsetup
{

VecGeomG4Navigator::VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety,
VecGeomParallelWorld const* parallelWorld)
: VecGeomG4NavigatorBase(map), mPushDepth(pushDepth), mZeroSafety(zeroSafety), mParallelWorld(parallelWorld)
VecGeomParallelWorld const* parallelWorld, bool leanLocate)
: VecGeomG4NavigatorBase(map),
mPushDepth(pushDepth),
mZeroSafety(zeroSafety),
mLeanLocate(leanLocate),
mParallelWorld(parallelWorld)
{
mEmptyState.Clear();
}
Expand All @@ -64,17 +69,11 @@ VecGeomG4Navigator::~VecGeomG4Navigator()
LOG(info) << "VecGeom parallel world: " << mParallelEntries << " steps ended on a node, " << mParallelRelocated
<< " locates overridden";
}
}

G4VPhysicalVolume* VecGeomG4Navigator::updateG4History()
{
// The history is a function of the VecGeom state alone, so it is rebuilt only when that changed.
if (mHistoryValid && mCurState.HasSamePathAsOther(mHistoryState)) {
return fHistory.GetTopVolume();
if (mPushRestartCount > 0) {
LOG(info) << "VecGeom stuck tracks: " << mPushRestartCount << " runs of zero steps restarted after a push";
}
mHistoryState = mCurState;
mHistoryValid = true;
return historyFromState(mCurState);
LOG(debug) << "VecGeom history: " << mHistoryStepCount << " updated by one level, " << mHistoryRebuildCount
<< " rebuilt";
}

void VecGeomG4Navigator::locateFromWorld(const V3& point)
Expand Down Expand Up @@ -125,7 +124,10 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
if (top == nullptr) { // the track is outside the world
return kInfinity;
}
auto const* navigator = top->GetLogicalVolume()->GetNavigator();
auto const* lvol = top->GetLogicalVolume();
// The lean locate relies on the BVH navigator refusing a daughter the ray leaves, also for small mothers.
auto const* navigator = mLeanLocate && lvol->GetDaughtersp()->size() > 0 ? vecgeom::BVHNavigatorV<>::Instance()
: lvol->GetNavigator();

// On the point a boundary locate left the track on, the safety is zero; a point seen before
// reuses its safety. Otherwise the navigator computes it with the step.
Expand All @@ -141,7 +143,11 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
// while the direction points away from it; a track turning back into it must see its boundary.
mStepState = mCurState;
bool blockExited = false;
if (mExitBlockPending) {
bool leanExitPending = false; // a parallel world node left by the lean locate is blocked by the same rule
if (mExitBlockPending && mLeanLocate) {
leanExitPending = samePoint(globalPoint, mLastLocatedPoint);
mExitBlockPending = false;
} else if (mExitBlockPending) {
const bool block = samePoint(globalPoint, mLastLocatedPoint) &&
(mExitNormalFromPush ? directionLeaves(mPushNormal, toDir(direction))
: directionLeaves(mExitedState, toVG(globalPoint), toDir(direction)));
Expand All @@ -164,6 +170,9 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
const V3 p = toVG(globalPoint);
double pwStep = std::min(std::max(vgStep, 0.), limit);
parallelHit = mParallelWorld->distanceToIn(p, toDir(direction), pwStep, [&](int node) {
if (leanExitPending) {
return mParallelWorld->isInNode(node, mExitedState) && directionLeaves(mExitedState, p, toDir(direction));
}
return blockExited && mParallelWorld->isInNode(node, mExitedState);
});
if (parallelHit >= 0) {
Expand Down Expand Up @@ -192,11 +201,26 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
// lengthened, after twenty-five the event is aborted.
if (step < 0.05 * kCarTolerance) {
++mZeroStepCount;
// A push that took the track off its face and into a volume this run of zero steps did not alternate
// between starts a new run there, with its own chords and momentum. Bounded until the next finite step.
if (mPushPending && mPushRestarts < kMaxPushRestarts &&
(globalPoint - mPushOrigin).mag2() >= 0.25 * mPushLength * mPushLength && !inZeroRun(mCurState)) {
mNzeroSteps = 0;
mZeroLinearSeen = false;
mZeroRunSize = 0;
++mPushRestarts;
++mPushRestartCount;
}
mPushPending = false;
// 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) {
// The volumes the run alternates between, recorded only when it gets near the threshold.
if (++mNzeroSteps >= kRecordZeroRun && mZeroRunSize < 2 && !inZeroRun(mCurState)) {
mZeroRun[mZeroRunSize++] = mCurState;
}
if (mNzeroSteps >= kActionThresholdNoZeroSteps) {
// 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.
Expand All @@ -205,7 +229,11 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
// 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);
const double push = std::min(100. * kCarTolerance * std::pow(4., tries), kMaxStuckPush);
step += push;
mPushPending = true;
mPushOrigin = globalPoint;
mPushLength = push;
}
if (mNzeroSteps >= kAbandonThresholdNoZeroSteps) {
++mAbandonCount;
Expand Down Expand Up @@ -342,7 +370,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
// 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 double push = (onBoundary && direction != nullptr) ? (mLeanLocate ? kLeanPush : boundaryPush(p, dir)) : 0.;
const V3 q = p + push * dir;

fWasLimitedByGeometry = false;
Expand Down Expand Up @@ -375,6 +403,18 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
mReloScratch = mCurState;
if (mCurState.GetCurrentLevel() <= 1) {
mCurState.Clear(); // left the world
} else if (mLeanLocate) {
// The volume left is popped whatever the point; ten nanometres past the face, containment pops
// every flush mother too. The next step refuses the volume left by the BVH navigator's rule.
vecgeom::Transformation3D m;
mCurState.TopMatrix(m);
vecgeom::GlobalLocator::RelocatePointFromPathForceDifferent(m.Transform(q), mCurState);
if (topOf(mCurState) == nullptr) {
mCurState.Clear();
}
if (mParallelWorld != nullptr) {
setExited(mReloScratch, false);
}
} else {
vecgeom::Transformation3D m;
mCurState.TopMatrix(m);
Expand Down Expand Up @@ -405,7 +445,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
}
mLocatedOnBoundary = onBoundary;
}
if (crossing) {
if (crossing && !mLeanLocate) {
leaveFlushVolumes(q, dir, entering ? static_cast<int>(mNextState.GetCurrentLevel()) : 0,
entering || topOf(mReloScratch) == nullptr ? nullptr : &mReloScratch);
}
Expand All @@ -417,7 +457,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
}
mWouldEnter = false;
mWouldExit = false;
return updateG4History();
return updateHistory(mCurState);
}

/// TGeo's rule: the node added first among those holding the point is where the track is. The
Expand Down Expand Up @@ -503,12 +543,11 @@ G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVect
}
if (kept) {
// The touchable's history is the path of the state: keep it rather than rebuild it.
mHistoryState = mCurState;
mHistoryValid = true;
keepTouchableHistory(mCurState);
return fHistory.GetTopVolume();
}
mHistoryValid = false;
return updateG4History();
invalidateHistory();
return updateHistory(mCurState);
}

void VecGeomG4Navigator::LocateGlobalPointWithinVolume(const G4ThreeVector& position)
Expand Down
56 changes: 40 additions & 16 deletions Detectors/gconfig/src/VecGeomG4Navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class VecGeomParallelWorld;
/// The point is first pushed across the face by a small depth, and afterwards leaves every volume
/// it is flush with and heading out of. As in Geant4, the volume left is blocked only in the first
/// ComputeStep after the exit, and only while the direction points away from it.
/// - With the lean locate (G4.vecgeomLeanLocate) the point is pushed 10 nm along the step instead,
/// which takes it past every coincident face, so no flush-volume loop is needed. Every mother with
/// daughters is then stepped with the BVH navigator, whose daughter query refuses at zero distance a
/// daughter the ray is leaving; that is the block of the volume just left, without a normal.
/// - Safety is zero only at the boundary point itself. A bounded query is answered from the last safety
/// computed in the same volume when that covers the bound.
///
Expand All @@ -41,8 +45,10 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
/// \param pushDepth how far past a face (cm, measured across it) a boundary point is pushed before
/// it is located. \param zeroSafety answers zero to every safety query.
/// \param parallelWorld the prioritised placements, or null if there are none.
/// \param leanLocate locates a crossing as VecGeom relocates: 10 nm along the step, no face normal, no
/// flush-volume loop; the volume left is refused by the BVH navigator's rule for a daughter the ray leaves.
VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety,
VecGeomParallelWorld const* parallelWorld = nullptr);
VecGeomParallelWorld const* parallelWorld = nullptr, bool leanLocate = false);
~VecGeomG4Navigator() override;

G4double ComputeStep(const G4ThreeVector& globalPoint, const G4ThreeVector& direction,
Expand All @@ -65,8 +71,6 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
G4ThreeVector GetGlobalExitNormal(const G4ThreeVector& point, G4bool* valid) override;

private:
/// Rewrites the Geant4 history from mCurState, unless it already stands for it.
G4VPhysicalVolume* updateG4History();
void locateFromWorld(const V3& point);
/// Go up past every volume the point is on the surface of and heading out of, then down again.
void leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::NavigationState const* avoid);
Expand Down Expand Up @@ -98,23 +102,24 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase

double mPushDepth = 1.e-9; ///< cm
bool mZeroSafety = false;
bool mLeanLocate = false;
/// The push of the lean locate, VecGeom's own after a boundary (VNavigator::MovePointAfterBoundary), cm.
static constexpr double kLeanPush = 1.e-6;
VecGeomParallelWorld const* mParallelWorld = nullptr;
bool mInParallelNode = false; ///< the track is in a parallel world node; changed by the locates only

vecgeom::NavigationState mCurState; ///< the volume the track is in; changed by the locates only
vecgeom::NavigationState mNextState; ///< the state entering the daughter the last ComputeStep hit
vecgeom::NavigationState mStepState; ///< scratch: a trial step
vecgeom::NavigationState mReloScratch; ///< scratch: the state before a crossing
vecgeom::NavigationState mPathScratch; ///< scratch: path comparisons
vecgeom::NavigationState mHistoryState; ///< the state fHistory was built from
vecgeom::NavigationState mEmptyState; ///< permanently empty; its last-exited entry clears others
vecgeom::NavigationState mNormalState; ///< the volume whose surface the last boundary step ended on
vecgeom::NavigationState mExitedState; ///< the volume the last crossing left
bool mExitBlockPending = false; ///< the next ComputeStep is the first after that exit
bool mExitNormalFromPush = false; ///< mPushNormal is the outward normal of the volume left
V3 mPushNormal; ///< global normal of the face the last push crossed
vecgeom::NavigationState mCurState; ///< the volume the track is in; changed by the locates only
vecgeom::NavigationState mNextState; ///< the state entering the daughter the last ComputeStep hit
vecgeom::NavigationState mStepState; ///< scratch: a trial step
vecgeom::NavigationState mReloScratch; ///< scratch: the state before a crossing
vecgeom::NavigationState mPathScratch; ///< scratch: path comparisons
vecgeom::NavigationState mEmptyState; ///< permanently empty; its last-exited entry clears others
vecgeom::NavigationState mNormalState; ///< the volume whose surface the last boundary step ended on
vecgeom::NavigationState mExitedState; ///< the volume the last crossing left
bool mExitBlockPending = false; ///< the next ComputeStep is the first after that exit
bool mExitNormalFromPush = false; ///< mPushNormal is the outward normal of the volume left
V3 mPushNormal; ///< global normal of the face the last push crossed
bool mPushNormalValid = false;
bool mHistoryValid = false;

bool mWouldEnter = false; ///< the last ComputeStep ends by entering a daughter
bool mWouldExit = false; ///< the last ComputeStep ends by leaving the current volume
Expand All @@ -134,10 +139,28 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
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
/// Restarts of a run of zero steps after a push, at most this many until the next finite step.
static constexpr int kMaxPushRestarts = 3;
int mPushRestarts = 0;
bool mPushPending = false; ///< the last zero step was lengthened
G4ThreeVector mPushOrigin; ///< where that step started
double mPushLength = 0.; ///< mm
static constexpr int kRecordZeroRun = kActionThresholdNoZeroSteps - 2;
/// The volumes the current run of zero steps alternates between, from its kRecordZeroRun-th step.
vecgeom::NavigationState mZeroRun[2];
int mZeroRunSize = 0;
bool inZeroRun(vecgeom::NavigationState const& st) const
{
return (mZeroRunSize > 0 && mZeroRun[0].HasSamePathAsOther(st)) ||
(mZeroRunSize > 1 && mZeroRun[1].HasSamePathAsOther(st));
}
void resetZeroSteps()
{
mNzeroSteps = 0;
mZeroLinearSeen = false;
mPushRestarts = 0;
mPushPending = false;
mZeroRunSize = 0;
}
G4ThreeVector mLastLocatedDir; ///< the direction the last locate was given

Expand All @@ -151,6 +174,7 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
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
};

} // namespace o2::simsetup
Expand Down
Loading