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)
4547namespace 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
6269G4VPhysicalVolume* 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.
@@ -199,7 +227,8 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
199227 mNextPoint = globalPoint + step * direction;
200228 mNextDir = toDir (direction);
201229 if (entering) {
202- mNextState = mStepState ;
230+ // A parallel world node is entered from anywhere in the hierarchy, not only from its mother.
231+ mNextState = parallelHit >= 0 ? mParallelWorld ->state (parallelHit) : mStepState ;
203232 }
204233 // The surface this step ends on, for the exit normal: the entered daughter's or our own.
205234 mNormalState = entering ? mNextState : mCurState ;
@@ -337,12 +366,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
337366 // mNextState is a copy of the step state and can still carry the block of the volume the previous
338367 // crossing left, which was meant for the first step after that exit only.
339368 mCurState .SetLastExited (mEmptyState .GetLastExitedState ());
340- auto const * daughter = mCurState .Top ();
341- mCurState .Pop ();
342- vecgeom::Transformation3D m;
343- mCurState .TopMatrix (m);
344- vecgeom::GlobalLocator::LocateGlobalPoint (daughter, daughter->GetTransformation ()->Transform (m.Transform (q)),
345- mCurState , false );
369+ descendInto (mCurState , q);
346370 mLocatedOnBoundary = true ;
347371 } else if (crossing) {
348372 // Out of the current volume: up until the point is contained, then down, never back into the
@@ -384,13 +408,57 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
384408 if (crossing) {
385409 leaveFlushVolumes (q, dir, entering ? static_cast <int >(mNextState .GetCurrentLevel ()) : 0 ,
386410 entering || topOf (mReloScratch ) == nullptr ? nullptr : &mReloScratch );
411+ }
412+ if (mParallelWorld != nullptr ) {
413+ applyParallelWorld (q, crossing && !entering);
414+ }
415+ if (crossing) {
387416 updateCrossingFlags (entering);
388417 }
389418 mWouldEnter = false ;
390419 mWouldExit = false ;
391420 return updateG4History ();
392421}
393422
423+ // / TGeo's rule: the node added first among those holding the point is where the track is. The
424+ // / locate then goes on into the node's daughters, which TGeo leaves out; with sensors and metal
425+ // / layers added before their chips, as ITS does, the result is the same.
426+ bool VecGeomG4Navigator::applyParallelWorld (const V3 & point, bool exiting)
427+ {
428+ mInParallelNode = false ;
429+ if (topOf (mCurState ) == nullptr || !mParallelWorld ->nearNodes (point)) {
430+ return false ;
431+ }
432+ // The pushed point can still be within the tolerance of a node the step just left.
433+ const int node = mParallelWorld ->findNode (point, [&](int k) {
434+ return exiting && mParallelWorld ->isInNode (k, mReloScratch ) && !mParallelWorld ->isInNode (k, mCurState );
435+ });
436+ if (node < 0 ) {
437+ // No node holds the point, but the main hierarchy may have put the track in one within its tolerance.
438+ mInParallelNode = mParallelWorld ->isInAnyNode (mCurState );
439+ return false ;
440+ }
441+ mInParallelNode = true ;
442+ if (mParallelWorld ->isInNode (node, mCurState )) {
443+ return false ;
444+ }
445+ mCurState = mParallelWorld ->state (node);
446+ descendInto (mCurState , point);
447+ clearLastExited (); // the volume left is no daughter of this branch
448+ ++mParallelRelocated ;
449+ return true ;
450+ }
451+
452+ void VecGeomG4Navigator::descendInto (vecgeom::NavigationState& state, const V3 & point)
453+ {
454+ auto const * placed = state.Top ();
455+ state.Pop ();
456+ vecgeom::Transformation3D m;
457+ state.TopMatrix (m);
458+ vecgeom::GlobalLocator::LocateGlobalPoint (placed, placed->GetTransformation ()->Transform (m.Transform (point)), state,
459+ false );
460+ }
461+
394462G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate (const G4ThreeVector& point,
395463 const G4ThreeVector& direction,
396464 const G4TouchableHistory& history)
@@ -429,6 +497,10 @@ G4VPhysicalVolume* VecGeomG4Navigator::ResetHierarchyAndLocate(const G4ThreeVect
429497 locateFromWorld (p);
430498 }
431499 clearLastExited ();
500+ if (mParallelWorld != nullptr ) {
501+ // A node holding the point has priority over the touchable too, as in TGeo's search on a resume.
502+ kept = !applyParallelWorld (p, false ) && kept;
503+ }
432504 if (kept) {
433505 // The touchable's history is the path of the state: keep it rather than rebuild it.
434506 mHistoryState = mCurState ;
@@ -477,6 +549,10 @@ G4double VecGeomG4Navigator::ComputeSafety(const G4ThreeVector& globalPoint, con
477549 return rest;
478550 }
479551 double safety = boundedSafety (mCurState , globalPoint, proposedMaxLength);
552+ if (mParallelWorld != nullptr && !mInParallelNode && safety > 0 .) {
553+ // Up to the caller's bound only; the bound returned for a farther node is a valid lower bound.
554+ safety = mParallelWorld ->safetyToIn (toVG (globalPoint), std::min (safety, proposedMaxLength) * kG4ToVG ) * kVGToG4 ;
555+ }
480556 if (safety < 0 .) {
481557 ++mNegativeSafetyCount ;
482558 safety = 0 .;
0 commit comments