Skip to content
Merged
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
39 changes: 36 additions & 3 deletions Detectors/gconfig/src/VecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -516,6 +548,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVect
mLocatedOnBoundary = false;
mLastLocatedPoint = point;
mLastLocatedDir = direction;
mPushedFrom = kNoPoint;
mSafetyOrig = kNoPoint;
resetZeroSteps();
fHistory = *history.GetHistory();
Expand Down Expand Up @@ -592,8 +625,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;
Expand Down
12 changes: 9 additions & 3 deletions Detectors/gconfig/src/VecGeomG4Navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -130,6 +132,9 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
vecgeom::Vector3D<double> 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
Expand Down Expand Up @@ -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
Expand Down
148 changes: 147 additions & 1 deletion Detectors/gconfig/test/testVecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
/// 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, 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
Expand All @@ -26,6 +28,7 @@
#include "../src/VecGeomG4Map.h"
#include "../src/VecGeomG4Navigator.h"
#include "../src/VecGeomG4PropagatingNavigator.h"
#include "../src/VecGeomParallelWorld.h"

#include "TG4RootDetectorConstruction.h"

Expand All @@ -35,6 +38,9 @@
#include "G4VExceptionHandler.hh"
#include "G4VPhysicalVolume.hh"

#include <VecGeom/management/GeoManager.h>
#include <VecGeom/navigation/GlobalLocator.h>

#include <TGeoManager.h>
#include <TGeoMaterial.h>
#include <TGeoMatrix.h>
Expand All @@ -52,6 +58,7 @@
using o2::simsetup::VecGeomG4Map;
using o2::simsetup::VecGeomG4Navigator;
using o2::simsetup::VecGeomG4PropagatingNavigator;
using o2::simsetup::VecGeomParallelWorld;

namespace
{
Expand All @@ -61,6 +68,8 @@ struct Fixture {
std::unique_ptr<VecGeomG4Navigator> navigator;
std::unique_ptr<VecGeomG4Navigator> lean; // the same with G4.vecgeomLeanLocate
std::unique_ptr<VecGeomG4PropagatingNavigator> propagating;
VecGeomParallelWorld parallelWorld; // one node: the first lead tile of module 3
std::unique_ptr<VecGeomG4Navigator> leanParallel; // the lean navigator with that parallel world

Fixture()
{
Expand Down Expand Up @@ -99,6 +108,14 @@ struct Fixture {
lean->SetWorldVolume(world);
propagating = std::make_unique<VecGeomG4PropagatingNavigator>(map, false);
propagating->SetWorldVolume(world);
vecgeom::NavigationState node;
node.Clear();
vecgeom::GlobalLocator::LocateGlobalPoint(vecgeom::GeoManager::Instance().GetWorld(),
vecgeom::Vector3D<double>(35., 0., -8.75), node, true);
BOOST_REQUIRE_EQUAL(node.Top()->GetLogicalVolume()->GetName(), std::string("LEAD"));
parallelWorld.build({node});
leanParallel = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false, &parallelWorld, true);
leanParallel->SetWorldVolume(world);
}
};

Expand Down Expand Up @@ -529,3 +546,132 @@ 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<double> 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);
}

/// 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<G4ThreeVector> 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<Crossing> 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);
}
}
}