14#include "G4Exception.hh"
15#include "G4TouchableHistory.hh"
16#include "G4VPhysicalVolume.hh"
18#include <VecGeom/base/Transformation3D.h>
19#include <VecGeom/management/GeoManager.h>
20#include <VecGeom/navigation/GlobalLocator.h>
21#include <VecGeom/navigation/VNavigator.h>
22#include <VecGeom/navigation/VSafetyEstimator.h>
23#include <VecGeom/volumes/LogicalVolume.h>
24#include <VecGeom/volumes/PlacedVolume.h>
26#include <fairlogger/Logger.h>
35const G4ThreeVector kNoPoint(-1e8, -1e8, -1e8);
37bool samePoint(
const G4ThreeVector&
a,
const G4ThreeVector&
b) {
return a.diff2(
b) < 1
e-20; }
39vecgeom::VPlacedVolume
const* topOf(vecgeom::NavigationState
const&
st)
41 return st.IsOutside() ? nullptr :
st.Top();
56 LOG(info) <<
"VecGeom navigation: zero steps " << mZeroStepCount <<
", stuck pushes " << mStuckPushCount
57 <<
", abandoned " << mAbandonCount <<
", negative safeties " << mNegativeSafetyCount
58 <<
", unmappable touchables " << mUnmappableHistoryCount <<
", relocated resumes "
59 << mRelocatedResumeCount <<
", missing exit normals " << mNoNormalCount;
62G4VPhysicalVolume* VecGeomG4Navigator::updateG4History()
65 if (mHistoryValid && mCurState.HasSamePathAsOther(mHistoryState)) {
66 return fHistory.GetTopVolume();
68 mHistoryState = mCurState;
73void VecGeomG4Navigator::locateFromWorld(
const V3& point)
76 vecgeom::GlobalLocator::LocateGlobalPoint(vecgeom::GeoManager::Instance().GetWorld(), point, mCurState,
true);
83double VecGeomG4Navigator::boundaryPush(
const V3& point,
const V3& dir)
85 constexpr double kMaxPush = 1.e-4;
86 const double big = std::max({std::abs(point.x()), std::abs(point.y()), std::abs(point.z())});
87 const double rounding = 1.e3 * big * std::numeric_limits<double>::epsilon();
90 vecgeom::NavigationState
const&
st = mWouldEnter ? mNextState : mCurState;
92 mPushNormalValid =
false;
93 if (!
st.IsOutside() &&
st.Top() !=
nullptr) {
94 vecgeom::Transformation3D
m;
97 st.Top()->GetUnplacedVolume()->Normal(
m.Transform(point),
n);
98 const double c = std::abs(
n.Dot(
m.TransformDirection(dir)));
99 if (
c > 0. &&
n.Mag2() > 0.5) {
102 if (
n.Mag2() > 0.5) {
103 mPushNormal =
m.InverseTransformDirection(
n);
104 mPushNormalValid =
true;
107 return std::max(rounding, std::min(mPushDepth / cosn, kMaxPush));
111 const G4double proposedStepLength, G4double& newSafety)
117 auto const*
top = topOf(mCurState);
118 if (
top ==
nullptr) {
121 auto const* navigator =
top->GetLogicalVolume()->GetNavigator();
125 bool calcSafety = !mZeroSafety && !(mLocatedOnBoundary && samePoint(globalPoint, mLastLocatedPoint));
126 if (calcSafety && samePoint(globalPoint, mSafetyOrig) && mLastSafety < mLastSafetyLimit) {
128 newSafety = mLastSafety;
135 mStepState = mCurState;
136 if (mExitBlockPending) {
137 const bool block = samePoint(globalPoint, mLastLocatedPoint) &&
141 mStepState.SetLastExited(mEmptyState.GetLastExitedState());
145 const double limit = std::min(proposedStepLength *
kG4ToVG,
static_cast<double>(vecgeom::kInfLength));
147 double vgStep = navigator->ComputeStepAndSafety(
toVG(globalPoint),
toDir(direction), limit, mStepState, calcSafety,
151 ++mNegativeSafetyCount;
155 mSafetyOrig = globalPoint;
156 mLastSafety = newSafety;
157 mLastSafetyLimit = kInfinity;
159 const bool boundaryLimited = vgStep < limit;
160 G4double step = std::max(vgStep, 0.) *
kVGToG4;
161 const bool entering = mStepState.GetCurrentLevel() > mCurState.GetCurrentLevel();
165 if (step < 0.05 * kCarTolerance) {
167 if (++mNzeroSteps >= kActionThresholdNoZeroSteps) {
171 const int tries = mNzeroSteps - kActionThresholdNoZeroSteps;
172 constexpr double kMicron = 1.e-3 * CLHEP::mm;
173 step += tries < 10 ? std::min(100. * kCarTolerance * std::pow(4., tries), kMicron)
174 : kMicron *
static_cast<double>(2 << (tries - 10));
175 if (mNzeroSteps >= kAbandonThresholdNoZeroSteps) {
177 std::ostringstream
msg;
178 msg <<
"Track stuck or not moving: " << mNzeroSteps <<
" zero steps in " <<
top->GetLabel() <<
" at ("
179 << globalPoint.x() <<
", " << globalPoint.y() <<
", " << globalPoint.z() <<
") mm. Event aborted, as "
180 <<
"G4Navigator does.";
182 G4Exception(
"VecGeomG4Navigator::ComputeStep()",
"GeomNav0003", EventMustBeAborted,
msg.str().c_str());
189 if (boundaryLimited) {
190 mWouldEnter = entering;
191 mWouldExit = !entering;
192 mNextPoint = globalPoint + step * direction;
194 mNextState = mStepState;
197 mNormalState = entering ? mNextState : mCurState;
198 mNormalEnter = entering;
199 mNormalPoint = mNextPoint;
203 mNormalValid =
false;
215void VecGeomG4Navigator::leaveFlushVolumes(
const V3& point,
const V3& dir,
int minLevel,
216 vecgeom::VPlacedVolume
const* avoid)
218 for (
int round = 0; round < 4; ++round) {
219 vecgeom::VPlacedVolume
const*
left =
nullptr;
220 while (topOf(mCurState) !=
nullptr &&
static_cast<int>(mCurState.GetCurrentLevel()) > minLevel) {
221 vecgeom::Transformation3D
m;
222 mCurState.TopMatrix(
m);
223 if (mCurState.Top()->GetUnplacedVolume()->DistanceToOut(
m.Transform(point),
m.TransformDirection(dir)) > 0.) {
226 left = mCurState.Top();
227 mCurState.SetLastExited();
228 setExited(mCurState,
false);
229 if (mCurState.GetCurrentLevel() <= 1) {
235 if (
left ==
nullptr) {
238 auto const* mother = mCurState.Top();
239 vecgeom::Transformation3D
m;
240 mCurState.TopMatrix(
m);
241 const auto level = mCurState.GetCurrentLevel();
242 const auto blocked = mCurState.GetLastExitedState();
244 vecgeom::GlobalLocator::LocateGlobalPointExclVolume(mother,
left,
m.Transform(point), mCurState,
false);
245 mCurState.SetLastExited(blocked);
246 if (mCurState.GetCurrentLevel() ==
level) {
249 if (avoid !=
nullptr && mCurState.GetCurrentLevel() >
level) {
251 mPathScratch = mCurState;
252 while (mPathScratch.GetCurrentLevel() >
level && mPathScratch.Top() != avoid) {
255 if (mPathScratch.GetCurrentLevel() >
level) {
256 while (mCurState.GetCurrentLevel() >
level) {
268void VecGeomG4Navigator::updateCrossingFlags(
bool entering)
270 const int preLevel = mReloScratch.IsOutside() ? 0 :
static_cast<int>(mReloScratch.GetCurrentLevel());
271 const int curLevel = mCurState.IsOutside() ? 0 :
static_cast<int>(mCurState.GetCurrentLevel());
272 mPathScratch = mReloScratch;
273 mStepState = mCurState;
274 int la = preLevel, lb = curLevel;
283 while (la > 0 && !mPathScratch.HasSamePathAsOther(mStepState)) {
288 fExitedMother = !entering || la < preLevel;
289 fEnteredDaughter = entering || la < curLevel;
293 const G4ThreeVector* direction,
294 const G4bool relativeSearch,
const G4bool)
298 const bool onBoundary = relativeSearch && (fWasLimitedByGeometry || samePoint(point, mNextPoint));
299 const bool crossing = onBoundary && (mWouldEnter || mWouldExit) && direction !=
nullptr;
300 const bool entering = crossing && mWouldEnter;
302 const V3 dir = direction !=
nullptr ?
toDir(*direction) :
V3(0., 0., 0.);
303 const double push = (onBoundary && direction !=
nullptr) ? boundaryPush(p, dir) : 0.;
304 const V3 q = p + push * dir;
306 fWasLimitedByGeometry =
false;
307 fEnteredDaughter =
false;
308 fExitedMother =
false;
309 mLocatedOnBoundary =
false;
310 mLastLocatedPoint = point;
311 mSafetyOrig = kNoPoint;
314 if (!relativeSearch) {
317 }
else if (topOf(mCurState) ==
nullptr) {
319 }
else if (entering) {
321 mReloScratch = mCurState;
322 mCurState = mNextState;
325 mCurState.SetLastExited(mEmptyState.GetLastExitedState());
326 auto const* daughter = mCurState.Top();
328 vecgeom::Transformation3D
m;
329 mCurState.TopMatrix(
m);
330 vecgeom::GlobalLocator::LocateGlobalPoint(daughter, daughter->GetTransformation()->Transform(
m.Transform(q)),
332 mLocatedOnBoundary =
true;
333 }
else if (crossing) {
337 mReloScratch = mCurState;
338 if (mCurState.GetCurrentLevel() <= 1) {
341 vecgeom::Transformation3D
m;
342 mCurState.TopMatrix(
m);
343 vecgeom::GlobalLocator::RelocatePointFromPathForceDifferent(
m.Transform(q), mCurState);
346 while (!mCurState.IsOutside() &&
shared + 1 <
static_cast<int>(mCurState.GetCurrentLevel()) &&
347 shared + 1 <
static_cast<int>(mReloScratch.GetCurrentLevel()) &&
348 mCurState.At(
shared + 1) == mReloScratch.At(
shared + 1)) {
351 mPathScratch = mReloScratch;
352 while (
static_cast<int>(mPathScratch.GetCurrentLevel()) >
shared + 2) {
355 mPathScratch.SetLastExited();
356 mCurState.SetLastExited(mPathScratch.GetLastExitedState());
357 setExited(mReloScratch,
true);
359 mLocatedOnBoundary =
true;
362 vecgeom::Transformation3D
m;
363 mCurState.TopMatrix(
m);
364 vecgeom::GlobalLocator::RelocatePointFromPath(
m.Transform(q), mCurState);
365 if (topOf(mCurState) ==
nullptr) {
368 mLocatedOnBoundary = onBoundary;
371 leaveFlushVolumes(q, dir, entering ?
static_cast<int>(mNextState.GetCurrentLevel()) : 0,
372 entering ? nullptr : topOf(mReloScratch));
373 updateCrossingFlags(entering);
377 return updateG4History();
381 const G4TouchableHistory& history)
385 fWasLimitedByGeometry =
false;
386 fEnteredDaughter =
false;
387 fExitedMother =
false;
390 mLocatedOnBoundary =
false;
391 mLastLocatedPoint = point;
392 mSafetyOrig = kNoPoint;
394 fHistory = *history.GetHistory();
398 vecgeom::Transformation3D
m;
399 mCurState.TopMatrix(
m);
400 if (mCurState.Top()->GetUnplacedVolume()->Contains(
m.Transform(p))) {
403 ++mRelocatedResumeCount;
404 vecgeom::GlobalLocator::RelocatePointFromPath(
m.Transform(p), mCurState);
405 if (topOf(mCurState) ==
nullptr) {
410 if (++mUnmappableHistoryCount <= 10) {
411 LOG(warning) <<
"VecGeom navigation: a touchable matches no VecGeom path; locating from the world";
418 mHistoryState = mCurState;
419 mHistoryValid =
true;
420 return fHistory.GetTopVolume();
422 mHistoryValid =
false;
423 return updateG4History();
431 mLocatedOnBoundary =
false;
434 fEnteredDaughter =
false;
435 fExitedMother =
false;
445 if (mLocatedOnBoundary && samePoint(globalPoint, mLastLocatedPoint)) {
448 if ((mWouldEnter || mWouldExit) && samePoint(globalPoint, mNextPoint)) {
451 if (samePoint(globalPoint, mSafetyOrig) && (mLastSafety < mLastSafetyLimit || mLastSafety >= proposedMaxLength)) {
454 if (topOf(mCurState) ==
nullptr) {
459 const double rest = mLastSafety - std::sqrt(globalPoint.diff2(mSafetyOrig));
460 if (rest >= proposedMaxLength) {
463 double safety =
boundedSafety(mCurState, globalPoint, proposedMaxLength);
465 ++mNegativeSafetyCount;
468 mSafetyOrig = globalPoint;
469 mLastSafety = safety;
470 mLastSafetyLimit = proposedMaxLength;
480bool VecGeomG4Navigator::computeExitNormal(
const G4ThreeVector& point,
V3& globalNormal)
const
482 if (!mNormalValid || topOf(mNormalState) ==
nullptr) {
485 vecgeom::Transformation3D
m;
486 mNormalState.TopMatrix(
m);
487 V3 localNormal(0., 0., 0.);
488 mNormalState.Top()->GetUnplacedVolume()->Normal(
m.Transform(
toVG(point)), localNormal);
489 globalNormal =
m.InverseTransformDirection(localNormal);
490 const double mag = globalNormal.Mag();
496 globalNormal = -globalNormal;
504 if (!computeExitNormal(mNormalPoint,
n) || topOf(mCurState) ==
nullptr) {
506 if (
valid !=
nullptr) {
509 return G4ThreeVector();
511 vecgeom::Transformation3D
m;
512 mCurState.TopMatrix(
m);
513 const auto local =
m.TransformDirection(
n);
514 if (
valid !=
nullptr) {
517 return G4ThreeVector(local[0], local[1], local[2]);
523 if (!computeExitNormal(point,
n)) {
525 if (
valid !=
nullptr) {
528 return G4ThreeVector();
530 if (
valid !=
nullptr) {
533 return G4ThreeVector(
n[0],
n[1],
n[2]);
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual Show related Highlight grid Show only individual collision Color clusters belonging to track objects Set line Enable drawing TPC data Enable drawing TOF data Enable drawing magnetic field Set field line step count Invert colors configStandalone Heavy OpenGL display display_heavy Show tracks with ITS contribution Show tracks with TOF contribution Enable propagation of loopers Show clusters at nominal x of pad row for early transformed data Split CE tracks when they cross the central Mark clusters Mark adjacent clusters Hide rejected tracks Apply filter on tracks to be displayed Separate increase X h configStandalone Camera window renderer OpenGL display display_camera Orient the camera such that the y axis is always upwards Full Screen MultiSample Anti Downsampling Anti Enable Vertical Sync Target frame or if the backend cannot limit to the refresh Use OpenGL indirect draws to reduce number of draw calls Display h configStandalone Vulkan display display_vulkan Have uniform buffers in host accessible device memory h configStandalone Internal display display_internal Number of updates the Qt GUI may send ahead of the last one the display has before it waits for the display Opacity of the console background Number of console lines kept
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if shared
vecgeom::Vector3D< double > V3
static bool directionLeaves(const V3 &n, const V3 &dir)
static V3 toDir(const G4ThreeVector &d)
G4VPhysicalVolume * historyFromState(vecgeom::NavigationState const &state)
static V3 toVG(const G4ThreeVector &p)
bool stateFromHistory(vecgeom::NavigationState &state) const
static double boundedSafety(vecgeom::NavigationState const &state, const G4ThreeVector &point, double limit)
static constexpr double kVGToG4
static constexpr double kG4ToVG
vecgeom::Vector3D< double > V3
G4ThreeVector GetGlobalExitNormal(const G4ThreeVector &point, G4bool *valid) override
G4double ComputeSafety(const G4ThreeVector &globalPoint, const G4double proposedMaxLength=DBL_MAX, const G4bool keepState=true) override
G4double ComputeStep(const G4ThreeVector &globalPoint, const G4ThreeVector &direction, const G4double proposedStepLength, G4double &newSafety) override
~VecGeomG4Navigator() override
VecGeomG4Navigator(VecGeomG4Map const &map, double pushDepth, bool zeroSafety)
G4VPhysicalVolume * ResetHierarchyAndLocate(const G4ThreeVector &point, const G4ThreeVector &direction, const G4TouchableHistory &history) override
G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=nullptr, const G4bool relativeSearch=true, const G4bool ignoreDirection=true) override
void LocateGlobalPointWithinVolume(const G4ThreeVector &position) override
G4ThreeVector GetLocalExitNormal(G4bool *valid) override
GLdouble GLdouble GLdouble GLdouble top
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
uint64_t const void const *restrict const msg