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.
@@ -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+
382450G4VPhysicalVolume* 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 .;
0 commit comments