Skip to content

Commit d50502e

Browse files
sawenzelclaude
andcommitted
Navigate the ITS parallel world with VecGeom
This commit lets G4.navmode=kVecGeom run with GeometryManagerParam.useParallelWorld and adds a unit test. - VecGeomParallelWorld holds the prioritised placements of the TGeo parallel world with a VecGeom IndexedBVH over their boxes. - The relocating navigator gives a node that holds the point priority in every locate, and limits step and safety by the nodes while the track is outside them. - The rules are those of TGeoParallelWorld: the node added first wins, and inside a node the others are not seen. - The propagating navigator still refuses the parallel world. - The walk from a Geant4 path to a VecGeom state moves into VecGeomG4Map, where the parallel world reuses it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent bf28154 commit d50502e

9 files changed

Lines changed: 925 additions & 60 deletions

‎Detectors/gconfig/CMakeLists.txt‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ set(G4SETUP_SOURCES src/G4Config.cxx src/G4RunConfiguration.cxx src/G4LocalField
3131
src/VecGeomNavigation.cxx src/O2MonopolePhysics.cxx)
3232
if(G4SETUP_WITH_VECGEOM)
3333
list(APPEND G4SETUP_SOURCES src/VecGeomG4Map.cxx src/VecGeomChecks.cxx src/VecGeomG4NavigatorBase.cxx
34-
src/VecGeomG4Navigator.cxx
34+
src/VecGeomG4Navigator.cxx src/VecGeomParallelWorld.cxx
3535
src/VecGeomG4PropagatingNavigator.cxx)
3636
endif()
3737

@@ -90,6 +90,15 @@ if(G4SETUP_WITH_VECGEOM)
9090
if(targetNavigatorTest)
9191
target_compile_definitions(${targetNavigatorTest} PRIVATE O2_WITH_VECGEOM)
9292
endif()
93+
o2_add_test(VecGeomParallelWorld
94+
SOURCES test/testVecGeomParallelWorld.cxx
95+
COMPONENT_NAME SimSetup
96+
TARGETVARNAME targetParallelWorldTest
97+
PUBLIC_LINK_LIBRARIES O2::G4Setup TGeo2VecGeom::TGeo2VecGeom
98+
LABELS simsetup)
99+
if(targetParallelWorldTest)
100+
target_compile_definitions(${targetParallelWorldTest} PRIVATE O2_WITH_VECGEOM)
101+
endif()
93102
endif()
94103

95104
o2_add_test_root_macro(UserDecay.C

‎Detectors/gconfig/src/VecGeomG4Map.h‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
#ifndef O2_SIMSETUP_VECGEOMG4MAP_H_
1313
#define O2_SIMSETUP_VECGEOMG4MAP_H_
1414

15+
#include "G4VPhysicalVolume.hh"
16+
17+
#include <VecGeom/navigation/NavigationState.h>
1518
#include <VecGeom/volumes/PlacedVolume.h>
1619

1720
#include <cstddef>
1821
#include <cstdint>
1922
#include <unordered_map>
2023
#include <vector>
2124

22-
class G4VPhysicalVolume;
2325
class TG4RootDetectorConstruction;
2426
class TGeoNode;
2527

@@ -91,6 +93,12 @@ class VecGeomG4Map
9193
return true;
9294
}
9395

96+
/// Builds \a state from a Geant4 path, extending past the levels assembly flattening dissolved.
97+
/// \a volumeAt(level) gives the Geant4 volume at a level, 0 being the world, \a depth the deepest
98+
/// level. False if a level matches no VecGeom placement; \a state is then empty.
99+
template <typename VolumeAt>
100+
bool stateFromPath(VolumeAt&& volumeAt, std::size_t depth, vecgeom::NavigationState& state) const;
101+
94102
/// How many Geant4 levels a VecGeom placement accounts for.
95103
unsigned chainSize(int vgId) const
96104
{
@@ -119,6 +127,56 @@ class VecGeomG4Map
119127
std::size_t mMispaired = 0;
120128
};
121129

130+
template <typename VolumeAt>
131+
bool VecGeomG4Map::stateFromPath(VolumeAt&& volumeAt, std::size_t depth, vecgeom::NavigationState& state) const
132+
{
133+
// A run of levels that ends in a Geant4 volume several placements share is settled by comparing
134+
// the run against the placements' recorded chains, which is exact.
135+
constexpr std::size_t kMaxChain = 16; // longest run of Geant4 levels one flattened placement stands for
136+
state.Clear();
137+
if (mWorld == nullptr || volumeAt(0) == nullptr) {
138+
return false;
139+
}
140+
state.Push(mWorld);
141+
std::size_t l = chainSize(mWorld->id());
142+
while (l <= depth) {
143+
G4VPhysicalVolume* run[kMaxChain];
144+
vecgeom::VPlacedVolume const* found = nullptr;
145+
std::size_t n = 0;
146+
for (std::size_t e = l; e <= depth && n < kMaxChain; ++e, ++n) {
147+
run[n] = volumeAt(e);
148+
if (run[n] == nullptr) {
149+
state.Clear();
150+
return false;
151+
}
152+
auto const* candidate = toVecGeom(run[n]->GetInstanceID());
153+
if (candidate == nullptr) {
154+
continue; // an assembly level; the chain reaches further down
155+
}
156+
if (candidate == ambiguous()) {
157+
for (auto const* c : candidates(run[n]->GetInstanceID())) {
158+
if (chainMatches(c, run, n + 1)) {
159+
found = c;
160+
break;
161+
}
162+
}
163+
} else if (chainMatches(candidate, run, n + 1)) {
164+
found = candidate;
165+
}
166+
if (found != nullptr) {
167+
break;
168+
}
169+
}
170+
if (found == nullptr) {
171+
state.Clear();
172+
return false;
173+
}
174+
state.Push(found);
175+
l += chainSize(found->id());
176+
}
177+
return true;
178+
}
179+
122180
} // namespace o2::simsetup
123181

124182
#endif

‎Detectors/gconfig/src/VecGeomG4Navigator.cxx‎

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "VecGeomG4Navigator.h"
1313

14+
#include "VecGeomParallelWorld.h"
15+
1416
#include "G4Exception.hh"
1517
#include "G4TouchableHistory.hh"
1618
#include "G4VPhysicalVolume.hh"
@@ -45,8 +47,9 @@ vecgeom::VPlacedVolume const* topOf(vecgeom::NavigationState const& st)
4547
namespace o2::simsetup
4648
{
4749

48-
VecGeomG4Navigator::VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety)
49-
: VecGeomG4NavigatorBase(map), mPushDepth(pushDepth), mZeroSafety(zeroSafety)
50+
VecGeomG4Navigator::VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety,
51+
VecGeomParallelWorld const* parallelWorld)
52+
: VecGeomG4NavigatorBase(map), mPushDepth(pushDepth), mZeroSafety(zeroSafety), mParallelWorld(parallelWorld)
5053
{
5154
mEmptyState.Clear();
5255
}
@@ -57,6 +60,10 @@ VecGeomG4Navigator::~VecGeomG4Navigator()
5760
<< ", abandoned " << mAbandonCount << ", negative safeties " << mNegativeSafetyCount
5861
<< ", unmappable touchables " << mUnmappableHistoryCount << ", relocated resumes "
5962
<< mRelocatedResumeCount << ", missing exit normals " << mNoNormalCount;
63+
if (mParallelWorld != nullptr) {
64+
LOG(info) << "VecGeom parallel world: " << mParallelEntries << " steps ended on a node, " << mParallelRelocated
65+
<< " locates overridden";
66+
}
6067
}
6168

6269
G4VPhysicalVolume* VecGeomG4Navigator::updateG4History()
@@ -133,19 +140,40 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
133140
// along the curve. The volume the last crossing left is blocked in the first call only, and only
134141
// while the direction points away from it; a track turning back into it must see its boundary.
135142
mStepState = mCurState;
143+
bool blockExited = false;
136144
if (mExitBlockPending) {
137145
const bool block = samePoint(globalPoint, mLastLocatedPoint) &&
138146
(mExitNormalFromPush ? directionLeaves(mPushNormal, toDir(direction))
139147
: directionLeaves(mExitedState, toVG(globalPoint), toDir(direction)));
140148
if (!block) {
141149
mStepState.SetLastExited(mEmptyState.GetLastExitedState());
142150
}
151+
blockExited = block;
143152
clearLastExited();
144153
}
145154
const double limit = std::min(proposedStepLength * kG4ToVG, static_cast<double>(vecgeom::kInfLength));
146155
double safety = 0.;
147156
double vgStep = navigator->ComputeStepAndSafety(toVG(globalPoint), toDir(direction), limit, mStepState, calcSafety,
148157
safety, true);
158+
// Outside the parallel world its nodes limit the step and the safety like daughters of every volume;
159+
// inside a node they are not seen, as in TGeo. A node at the distance of the main boundary is left to
160+
// the main hierarchy, which is safe because the locate descends from what was entered and then asks the
161+
// parallel world again. The nodes just left are blocked by the rule of the volume just left.
162+
int parallelHit = -1;
163+
if (mParallelWorld != nullptr && !mInParallelNode) {
164+
const V3 p = toVG(globalPoint);
165+
double pwStep = std::min(std::max(vgStep, 0.), limit);
166+
parallelHit = mParallelWorld->distanceToIn(p, toDir(direction), pwStep, [&](int node) {
167+
return blockExited && mParallelWorld->isInNode(node, mExitedState);
168+
});
169+
if (parallelHit >= 0) {
170+
vgStep = pwStep;
171+
++mParallelEntries;
172+
}
173+
if (calcSafety && safety > 0.) {
174+
safety = mParallelWorld->safetyToIn(p, safety);
175+
}
176+
}
149177
if (calcSafety) {
150178
if (safety < 0.) {
151179
++mNegativeSafetyCount;
@@ -158,7 +186,7 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
158186
}
159187
const bool boundaryLimited = vgStep < limit;
160188
G4double step = std::max(vgStep, 0.) * kVGToG4;
161-
const bool entering = mStepState.GetCurrentLevel() > mCurState.GetCurrentLevel();
189+
const bool entering = parallelHit >= 0 || mStepState.GetCurrentLevel() > mCurState.GetCurrentLevel();
162190

163191
// A track that is not moving, with G4Navigator's thresholds: after ten zero steps the step is
164192
// lengthened, after twenty-five the event is aborted.
@@ -191,7 +219,8 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
191219
mWouldExit = !entering;
192220
mNextPoint = globalPoint + step * direction;
193221
if (entering) {
194-
mNextState = mStepState;
222+
// A parallel world node is entered from anywhere in the hierarchy, not only from its mother.
223+
mNextState = parallelHit >= 0 ? mParallelWorld->state(parallelHit) : mStepState;
195224
}
196225
// The surface this step ends on, for the exit normal: the entered daughter's or our own.
197226
mNormalState = entering ? mNextState : mCurState;
@@ -325,12 +354,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
325354
// mNextState is a copy of the step state and can still carry the block of the volume the previous
326355
// crossing left, which was meant for the first step after that exit only.
327356
mCurState.SetLastExited(mEmptyState.GetLastExitedState());
328-
auto const* daughter = mCurState.Top();
329-
mCurState.Pop();
330-
vecgeom::Transformation3D m;
331-
mCurState.TopMatrix(m);
332-
vecgeom::GlobalLocator::LocateGlobalPoint(daughter, daughter->GetTransformation()->Transform(m.Transform(q)),
333-
mCurState, false);
357+
descendInto(mCurState, q);
334358
mLocatedOnBoundary = true;
335359
} else if (crossing) {
336360
// Out of the current volume: up until the point is contained, then down, never back into the
@@ -372,13 +396,57 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
372396
if (crossing) {
373397
leaveFlushVolumes(q, dir, entering ? static_cast<int>(mNextState.GetCurrentLevel()) : 0,
374398
entering || topOf(mReloScratch) == nullptr ? nullptr : &mReloScratch);
399+
}
400+
if (mParallelWorld != nullptr) {
401+
applyParallelWorld(q, crossing && !entering);
402+
}
403+
if (crossing) {
375404
updateCrossingFlags(entering);
376405
}
377406
mWouldEnter = false;
378407
mWouldExit = false;
379408
return updateG4History();
380409
}
381410

411+
/// TGeo's rule: the node added first among those holding the point is where the track is. The
412+
/// locate then goes on into the node's daughters, which TGeo leaves out; with sensors and metal
413+
/// layers added before their chips, as ITS does, the result is the same.
414+
bool VecGeomG4Navigator::applyParallelWorld(const V3& point, bool exiting)
415+
{
416+
mInParallelNode = false;
417+
if (topOf(mCurState) == nullptr || !mParallelWorld->nearNodes(point)) {
418+
return false;
419+
}
420+
// The pushed point can still be within the tolerance of a node the step just left.
421+
const int node = mParallelWorld->findNode(point, [&](int k) {
422+
return exiting && mParallelWorld->isInNode(k, mReloScratch) && !mParallelWorld->isInNode(k, mCurState);
423+
});
424+
if (node < 0) {
425+
// No node holds the point, but the main hierarchy may have put the track in one within its tolerance.
426+
mInParallelNode = mParallelWorld->isInAnyNode(mCurState);
427+
return false;
428+
}
429+
mInParallelNode = true;
430+
if (mParallelWorld->isInNode(node, mCurState)) {
431+
return false;
432+
}
433+
mCurState = mParallelWorld->state(node);
434+
descendInto(mCurState, point);
435+
clearLastExited(); // the volume left is no daughter of this branch
436+
++mParallelRelocated;
437+
return true;
438+
}
439+
440+
void VecGeomG4Navigator::descendInto(vecgeom::NavigationState& state, const V3& point)
441+
{
442+
auto const* placed = state.Top();
443+
state.Pop();
444+
vecgeom::Transformation3D m;
445+
state.TopMatrix(m);
446+
vecgeom::GlobalLocator::LocateGlobalPoint(placed, placed->GetTransformation()->Transform(m.Transform(point)), state,
447+
false);
448+
}
449+
382450
G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVector& point, const G4ThreeVector&,
383451
const G4TouchableHistory& history)
384452
{
@@ -415,6 +483,10 @@ G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVect
415483
locateFromWorld(p);
416484
}
417485
clearLastExited();
486+
if (mParallelWorld != nullptr) {
487+
// A node holding the point has priority over the touchable too, as in TGeo's search on a resume.
488+
kept = !applyParallelWorld(p, false) && kept;
489+
}
418490
if (kept) {
419491
// The touchable's history is the path of the state: keep it rather than rebuild it.
420492
mHistoryState = mCurState;
@@ -463,6 +535,10 @@ G4double VecGeomG4Navigator::ComputeSafety(const G4ThreeVector& globalPoint, con
463535
return rest;
464536
}
465537
double safety = boundedSafety(mCurState, globalPoint, proposedMaxLength);
538+
if (mParallelWorld != nullptr && !mInParallelNode && safety > 0.) {
539+
// Up to the caller's bound only; the bound returned for a farther node is a valid lower bound.
540+
safety = mParallelWorld->safetyToIn(toVG(globalPoint), std::min(safety, proposedMaxLength) * kG4ToVG) * kVGToG4;
541+
}
466542
if (safety < 0.) {
467543
++mNegativeSafetyCount;
468544
safety = 0.;

‎Detectors/gconfig/src/VecGeomG4Navigator.h‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
namespace o2::simsetup
1818
{
1919

20+
class VecGeomParallelWorld;
21+
2022
/// A Geant4 tracking navigator that answers every navigation query from VecGeom while keeping the
2123
/// Geant4 navigation history, the touchable the scoring code reads, in step with the VecGeom state.
2224
/// Selected with G4.vecgeomNavigator=kRelocating, the default. It works as G4VecGeomNav's
@@ -38,7 +40,9 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
3840
public:
3941
/// \param pushDepth how far past a face (cm, measured across it) a boundary point is pushed before
4042
/// it is located. \param zeroSafety answers zero to every safety query.
41-
VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety);
43+
/// \param parallelWorld the prioritised placements, or null if there are none.
44+
VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety,
45+
VecGeomParallelWorld const* parallelWorld = nullptr);
4246
~VecGeomG4Navigator() override;
4347

4448
G4double ComputeStep(const G4ThreeVector& globalPoint, const G4ThreeVector& direction,
@@ -66,6 +70,12 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
6670
void locateFromWorld(const V3& point);
6771
/// Go up past every volume the point is on the surface of and heading out of, then down again.
6872
void leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::NavigationState const* avoid);
73+
/// Gives a parallel world node holding the point priority over the state the main hierarchy located.
74+
/// \param exiting the locate follows a step out of a volume, whose path is in mReloScratch.
75+
/// \return whether the state was moved into a node.
76+
bool applyParallelWorld(const V3& point, bool exiting);
77+
/// Locates \a point in the daughters of the top volume of \a state, which holds it.
78+
static void descendInto(vecgeom::NavigationState& state, const V3& point);
6979
/// Sets fEnteredDaughter and fExitedMother from the paths before (mReloScratch) and after a crossing.
7080
void updateCrossingFlags(bool entering);
7181
void clearLastExited()
@@ -88,6 +98,8 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
8898

8999
double mPushDepth = 1.e-9; ///< cm
90100
bool mZeroSafety = false;
101+
VecGeomParallelWorld const* mParallelWorld = nullptr;
102+
bool mInParallelNode = false; ///< the track is in a parallel world node; changed by the locates only
91103

92104
vecgeom::NavigationState mCurState; ///< the volume the track is in; changed by the locates only
93105
vecgeom::NavigationState mNextState; ///< the state entering the daughter the last ComputeStep hit
@@ -129,6 +141,8 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
129141
long mUnmappableHistoryCount = 0;
130142
long mRelocatedResumeCount = 0;
131143
long mNoNormalCount = 0;
144+
long mParallelEntries = 0; ///< steps that ended on a parallel world node
145+
long mParallelRelocated = 0; ///< locates the parallel world overrode
132146
};
133147

134148
} // namespace o2::simsetup

0 commit comments

Comments
 (0)