17#include "TGeoManager.h"
22#include "G4SystemOfUnits.hh"
27#include "G4TrackingManager.hh"
30static const double gCm = 1. / cm;
40 fStepEntering(kFALSE),
46 fG4TrackingManager(nullptr),
47 fRestoreGeoStateFunction(nullptr)
58 fStepEntering(kFALSE),
64 fG4TrackingManager(nullptr),
65 fRestoreGeoStateFunction(nullptr)
86 G4Exception(
"TG4RootNavigator::SetDetectorConstruction",
"G4Root_F001",
88 "Cannot create TG4RootNavigator without closed ROOT geometry !");
98 const G4ThreeVector& pDirection,
const G4double pCurrentProposedStepLength,
116 static Long64_t istep = 0;
121 G4cout <<
"*** ComputeStep #" << istep <<
": ***"
122 << fHistory.GetTopVolume()->GetName()
123 <<
" entered: " << fEnteredDaughter <<
" exited: " << fExitedMother
127 Bool_t compute_safety = kTRUE;
128 Double_t pstep = pCurrentProposedStepLength;
129 if (pstep > TGeoShape::Big()) {
130 pstep = TGeoShape::Big();
131 compute_safety = kFALSE;
135 Bool_t frombdr = fEnteredDaughter | fExitedMother;
137 Bool_t oldpoint = kFALSE;
141 tol = TGeoShape::Tolerance();
142 npt[0] = pGlobalPoint.x() *
gCm + tol * pDirection.x();
143 npt[1] = pGlobalPoint.y() *
gCm + tol * pDirection.y();
144 npt[2] = pGlobalPoint.z() *
gCm + tol * pDirection.z();
145 fNavigator->SetCurrentPoint(npt[0], npt[1], npt[2]);
146 compute_safety = kFALSE;
151 pGlobalPoint.x() *
gCm, pGlobalPoint.y() *
gCm, pGlobalPoint.z() *
gCm);
157 compute_safety = kFALSE;
163 pDirection.x(), pDirection.y(), pDirection.z());
164 fNavigator->FindNextBoundary(-(pstep *
gCm - tol),
"", !compute_safety);
166 if (compute_safety) {
167 pNewSafety = (
fNavigator->GetSafeDistance() - tol) * cm;
168 if (pNewSafety < 0.) pNewSafety = 0.;
171 G4double step = (
fNavigator->GetStep() + tol) * cm;
173 if (step < 1.e3 * tol * cm) {
188 fNextPoint = pGlobalPoint + step * pDirection;
195 G4cout.precision(12);
196 G4cout <<
"ComputeStep: point=" << pGlobalPoint <<
" dir=" << pDirection
198 G4cout <<
" pstep=" << pCurrentProposedStepLength
199 <<
" snext=" << step << G4endl;
200 G4cout <<
" safe =" << pNewSafety <<
" frombdr=" << frombdr
204 G4cout <<
" current: "
206 <<
" next: " <<
fNavigator->GetNextNode()->GetName() << G4endl;
214 const G4ThreeVector& point,
const G4ThreeVector& direction,
215 const G4TouchableHistory& h)
230 G4cout.precision(12);
231 G4cout <<
"ResetHierarchyAndLocate: POINT: " << point <<
" DIR: " << direction
235 fEnteredDaughter = kFALSE;
236 fExitedMother = kFALSE;
239 fHistory = *h.GetHistory();
242 direction.x(), direction.y(), direction.z());
254 Int_t depth = fHistory.GetDepth();
255 Int_t nodeIndex, level;
256 G4VPhysicalVolume* pvol;
257 TGeoNode *pnode, *newnode = 0;
258 for (level = 1; level <= depth; level++) {
259 pvol = fHistory.GetVolume(level);
261 if (level <= geolevel) {
264 pnode =
fNavigator->GetMother(geolevel - level);
266 if (pnode == newnode)
continue;
268 while (geolevel >= level) {
274 nodeIndex =
fNavigator->GetCurrentVolume()->GetIndex(newnode);
276 G4cerr <<
"SynchronizeGeoManager did not work (1)!!!" << G4endl;
285 nodeIndex =
fNavigator->GetCurrentVolume()->GetIndex(newnode);
287 G4cerr <<
"SynchronizeGeoManager did not work (2)!!!" << G4endl;
304 Int_t depth = fHistory.GetDepth();
306 G4VPhysicalVolume *pvol, *pnewvol = 0;
309 for (level = 0; level <= geolevel; level++) {
310 pnode =
fNavigator->GetMother(geolevel - level);
312 if (level <= depth) {
313 pvol = fHistory.GetVolume(level);
316 if (pvol == pnewvol)
continue;
319 fHistory.BackLevel(depth - level + 1);
321 fHistory.NewLevel(pnewvol, kNormal, pnewvol->GetCopyNo());
325 fHistory.BackLevel(depth);
326 fHistory.SetFirstEntry(pnewvol);
332 fHistory.NewLevel(pnewvol, kNormal, pnewvol->GetCopyNo());
336 if (depth > level - 1) fHistory.BackLevel(depth - level + 1);
343 const G4ThreeVector& globalPoint,
const G4ThreeVector* pGlobalDirection,
344 const G4bool ,
const G4bool ignoreDirection)
358 static Long64_t ilocate = 0;
362 Bool_t isGeoStateRestored = kFALSE;
372 G4cout.precision(12);
373 G4cout <<
"LocateGlobalPointAndSetup #" << ilocate
374 <<
": point: " << globalPoint << G4endl;
377 globalPoint.x() *
gCm, globalPoint.y() *
gCm, globalPoint.z() *
gCm);
378 fEnteredDaughter = fExitedMother = kFALSE;
379 Bool_t onBoundary = kFALSE;
388 G4cout <<
" ON BOUNDARY "
392 G4cout <<
" IN VOLUME "
397 if ((!ignoreDirection || onBoundary) && pGlobalDirection) {
399 pGlobalDirection->x(), pGlobalDirection->y(), pGlobalDirection->z());
404 if (
fNavigator->IsOutside()) G4cout <<
" outside" << G4endl;
409 TGeoNode* skip =
fNavigator->GetCurrentNode();
418 else if (!isGeoStateRestored) {
425 G4cout <<
" POINT INSIDE: " << target->GetName()
426 <<
" entered=" << fEnteredDaughter <<
" exited=" << fExitedMother
434 const G4ThreeVector& pGlobalPoint)
447 G4cout.precision(12);
448 G4cout <<
"LocateGlobalPointWithinVolume " << pGlobalPoint << G4endl;
451 pGlobalPoint.x() *
gCm, pGlobalPoint.y() *
gCm, pGlobalPoint.z() *
gCm);
454 fEnteredDaughter = kFALSE;
455 fExitedMother = kFALSE;
460 const G4double pProposedMaxLength,
const G4bool )
463 G4double saf =
ComputeSafety(globalpoint, pProposedMaxLength);
469 const G4ThreeVector& globalpoint,
const G4double )
486 G4cout <<
"ComputeSafety: POINT on boundary: " << globalpoint
487 <<
" SKIPPED... oldsafe= 0.0" << G4endl;
494 G4cout <<
"ComputeSafety: POINT not changed: " << globalpoint
495 <<
" SKIPPED... oldsafe=" <<
fLastSafety << G4endl;
501 globalpoint.x() *
gCm, globalpoint.y() *
gCm, globalpoint.z() *
gCm);
507 G4cout.precision(12);
508 G4cout <<
"ComputeSafety: POINT: " << globalpoint <<
" safe = " << safety
518 return G4Navigator::CreateTouchableHistoryHandle();
532 Double_t *norm, lnorm[3];
535 G4ThreeVector normal(0., 0., 1.);
541 normal.setX(lnorm[0]);
542 normal.setY(lnorm[1]);
543 normal.setZ(lnorm[2]);
545 G4cout <<
"GetLocalExitNormal: " << normal << G4endl;
552 const G4ThreeVector& , G4bool* valid)
568 G4ThreeVector normal(0., 0., 1.);
573 normal.setX(norm[0]);
574 normal.setY(norm[1]);
575 normal.setZ(norm[2]);
577 G4cout <<
"GetGlobalExitNormal: " << normal << G4endl;
590 std::function<Bool_t(Int_t)> restoreGeoStateFunction)
Definition of the TG4RootDetectorConstruction and TVirtualUserPostDetConstruction classes.
static const double gCm
constant for conversion cm <-> mm
static const int gAbandonZeroSteps
static const double gZeroStepThr
Definition of the TG4RootNavigator class.
Builder creating a pseudo G4 geometry starting from a TGeo geometry.
G4VPhysicalVolume * GetG4VPhysicalVolume(const TGeoNode *node) const
TGeoManager * GetGeometryManager() const
Return TGeo geometry manager.
TGeoNode * GetNode(const G4VPhysicalVolume *g4vol) const
G4VPhysicalVolume * GetTopPV() const
Return the World G4 physical volume.
virtual ~TG4RootNavigator()
G4VPhysicalVolume * SynchronizeHistory()
G4double fLastSafety
Last computed safety.
void SetG4TrackingManager(G4TrackingManager *trackingManager)
Set current G4TrackingManager.
virtual G4double ComputeSafety(const G4ThreeVector &globalpoint, const G4double pProposedMaxLength)
TG4RootDetectorConstruction * fDetConstruction
virtual G4ThreeVector GetLocalExitNormal(G4bool *valid)
virtual G4TouchableHandle CreateTouchableHistoryHandle() const
virtual void LocateGlobalPointWithinVolume(const G4ThreeVector &position)
TGeoNode * SynchronizeGeoManager()
TGeoManager * fGeometry
TGeo geometry manager.
TGeoNavigator * fNavigator
TGeo navigator.
void SetGeometryRestoreFunction(std::function< Bool_t(Int_t)> restoreGeoStateFunction)
Int_t fNzeroSteps
Number of zero steps in ComputeStep.
virtual G4ThreeVector GetGlobalExitNormal(const G4ThreeVector &point, G4bool *valid)
virtual G4VPhysicalVolume * ResetHierarchyAndLocate(const G4ThreeVector &point, const G4ThreeVector &direction, const G4TouchableHistory &h)
G4ThreeVector fNextPoint
Crossing point with next boundary.
G4ThreeVector fSafetyOrig
Last computed safety origin.
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=0, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
G4TrackingManager * fG4TrackingManager
Store pointer to G4TrackingManager.
std::function< Bool_t(Int_t)> fRestoreGeoStateFunction
virtual G4double ComputeStep(const G4ThreeVector &pGlobalPoint, const G4ThreeVector &pDirection, const G4double pCurrentProposedStepLength, G4double &pNewSafety)
Bool_t fStepExiting
Next step is exiting current volume.
void SetDetectorConstruction(TG4RootDetectorConstruction *dc)
Bool_t fStepEntering
Next step is entering daughter.