ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ITNavigator2.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4ITNavigator2.hh
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // Original author: Paul Kent, July 95/96
28 //
40 //
41 // Contact : Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
42 //
43 // WARNING : This class is released as a prototype.
44 // It might strongly evolve or even disappear in the next releases.
45 //
46 // We would be very happy hearing from you, send us your feedback! :)
47 //
48 // History:
49 // - Created. Paul Kent, Jul 95/96
50 // - Zero step protections J.A. / G.C., Nov 2004
51 // - Added check mode G. Cosmo, Mar 2004
52 // - Made Navigator Abstract G. Cosmo, Nov 2003
53 // - G4ITNavigator created M.K., Nov 2012
54 // *********************************************************************
55 
56 #ifndef G4ITNAVIGATOR_HH
57 #define G4ITNAVIGATOR_HH
58 
59 #include "geomdefs.hh"
60 
61 #include "G4ThreeVector.hh"
62 #include "G4AffineTransform.hh"
63 #include "G4RotationMatrix.hh"
64 
65 #include "G4LogicalVolume.hh" // Used in inline methods
66 #include "G4GRSVolume.hh" // " "
67 #include "G4GRSSolid.hh" // " "
68 #include "G4TouchableHandle.hh" // " "
70 
71 #include "G4NavigationHistory.hh"
72 #include "G4NormalNavigation.hh"
73 #include "G4VoxelNavigation.hh"
75 #include "G4ReplicaNavigation.hh"
76 #include "G4RegularNavigation.hh"
77 
78 #include <iostream>
79 #include "G4TrackState.hh"
80 #include <memory>
81 
82 class G4VPhysicalVolume;
83 
85 {
87  {
88  ;
89  }
90 protected:
92  {
93  ;
94  }
96  {
97  ;
98  }
99 };
100 
102 {
103 public:
104  static const G4int fMaxNav = 8; // rename to kMaxNoNav ??
105 
106 public:
107  // with description
108 
109  friend std::ostream& operator <<(std::ostream &os, const G4ITNavigator2 &n);
110 
111  G4ITNavigator2();
112  // Constructor - initialisers and setup.
113 
114  virtual ~G4ITNavigator2();
115  // Destructor. No actions.
116 
117  // !>
120  void NewNavigatorState();
122  void ResetNavigatorState();
124  const G4ThreeVector &direction);
125  void CheckNavigatorState() const;
126 
127  inline std::shared_ptr<G4ITNavigatorState_Lock2> GetSnapshotOfState();
128  inline void ResetFromSnapshot(std::shared_ptr<G4ITNavigatorState_Lock2>);
129  // <!
130 
131  virtual G4double ComputeStep(const G4ThreeVector &pGlobalPoint,
132  const G4ThreeVector &pDirection,
133  const G4double pCurrentProposedStepLength,
134  G4double &pNewSafety);
135  // Calculate the distance to the next boundary intersected
136  // along the specified NORMALISED vector direction and
137  // from the specified point in the global coordinate
138  // system. LocateGlobalPointAndSetup or LocateGlobalPointWithinVolume
139  // must have been called with the same global point prior to this call.
140  // The isotropic distance to the nearest boundary is also
141  // calculated (usually an underestimate). The current
142  // proposed Step length is used to avoid intersection
143  // calculations: if it can be determined that the nearest
144  // boundary is >pCurrentProposedStepLength away, kInfinity
145  // is returned together with the computed isotropic safety
146  // distance. Geometry must be closed.
147 
148  G4double CheckNextStep(const G4ThreeVector &pGlobalPoint,
149  const G4ThreeVector &pDirection,
150  const G4double pCurrentProposedStepLength,
151  G4double &pNewSafety);
152  // Same as above, but do not disturb the state of the Navigator.
153 
155  const G4ThreeVector &direction,
156  const G4TouchableHistory &h);
157 
158  // Resets the geometrical hierarchy and search for the volumes deepest
159  // in the hierarchy containing the point in the global coordinate space.
160  // The direction is used to check if a volume is entered.
161  // The search begin is the geometrical hierarchy at the location of the
162  // last located point, or the endpoint of the previous Step if
163  // SetGeometricallyLimitedStep() has been called immediately before.
164  //
165  // Important Note: In order to call this the geometry MUST be closed.
166 
167  virtual
169  const G4ThreeVector* direction=0,
170  const G4bool pRelativeSearch=true,
171  const G4bool ignoreDirection=true);
172  // Search the geometrical hierarchy for the volumes deepest in the hierarchy
173  // containing the point in the global coordinate space. Two main cases are:
174  // i) If pRelativeSearch=false it makes use of no previous/state
175  // information. Returns the physical volume containing the point,
176  // with all previous mothers correctly set up.
177  // ii) If pRelativeSearch is set to true, the search begin is the
178  // geometrical hierarchy at the location of the last located point,
179  // or the endpoint of the previous Step if SetGeometricallyLimitedStep()
180  // has been called immediately before.
181  // The direction is used (to check if a volume is entered) if either
182  // - the argument ignoreDirection is false, or
183  // - the Navigator has determined that it is on an edge shared by two or
184  // more volumes. (This is state information.)
185  //
186  // Important Note: In order to call this the geometry MUST be closed.
187 
188  //----------------------------------------------------------------------------
189  EInside InsideCurrentVolume(const G4ThreeVector& globalPoint) const;
190 
191  void GetRandomInCurrentVolume(G4ThreeVector& rndmPoint) const;
192  //----------------------------------------------------------------------------
193 
194  virtual
196  // Notify the Navigator that a track has moved to the new Global point
197  // 'position', that is known to be within the current safety.
198  // No check is performed to ensure that it is within the volume.
199  // This method can be called instead of LocateGlobalPointAndSetup ONLY if
200  // the caller is certain that the new global point (position) is inside the
201  // same volume as the previous position. Usually this can be guaranteed
202  // only if the point is within safety.
203 
205  const G4ThreeVector& position,
206  const G4ThreeVector& direction,
207  G4TouchableHandle& oldTouchableToUpdate,
208  const G4bool RelativeSearch = true);
209  // First, search the geometrical hierarchy like the above method
210  // LocateGlobalPointAndSetup(). Then use the volume found and its
211  // navigation history to update the touchable.
212 
214  const G4ThreeVector& position,
215  const G4ThreeVector& direction,
216  G4VTouchable* touchableToUpdate,
217  const G4bool RelativeSearch = true);
218  // First, search the geometrical hierarchy like the above method
219  // LocateGlobalPointAndSetup(). Then use the volume found and its
220  // navigation history to update the touchable.
221 
223  const G4ThreeVector& position,
224  G4VTouchable* touchableToUpdate,
225  const G4bool RelativeSearch = true);
226  // Same as the method above but missing direction.
227 
228  inline void SetGeometricallyLimitedStep();
229  // Inform the navigator that the previous Step calculated
230  // by the geometry was taken in its entirety.
231 
232  virtual G4double ComputeSafety(const G4ThreeVector &globalpoint,
233  const G4double pProposedMaxLength = DBL_MAX,
234  const G4bool keepState = true);
235  // Calculate the isotropic distance to the nearest boundary from the
236  // specified point in the global coordinate system.
237  // The globalpoint utilised must be within the current volume.
238  // The value returned is usually an underestimate.
239  // The proposed maximum length is used to avoid volume safety
240  // calculations. The geometry must be closed.
241  // To ensure minimum side effects from the call, keepState
242  // must be true.
243 
245  const G4ThreeVector &pGlobalPoint,
246  const G4ThreeVector &pDirection,
247  const G4double CurrentProposedStepLength,
248  G4double *prDistance,
249  G4double *prNewSafety=0) const;
250  // Trial method for checking potential displacement for MS
251  // Check new Globalpoint, to see whether it is in current volume
252  // (mother) and not in potential entering daughter.
253  // If in mother, check distance to boundary along pDirection.
254  // If in entering daughter, check distance back to boundary.
255  // NOTE:
256  // Can be called only after ComputeStep is called - before ReLocation
257  // Deals only with current volume (and potentially entered)
258 
259  inline G4VPhysicalVolume* GetWorldVolume() const;
260  // Return the current world (`topmost') volume.
261 
262  inline void SetWorldVolume(G4VPhysicalVolume* pWorld);
263  // Set the world (`topmost') volume. This must be positioned at
264  // origin (0,0,0) and unrotated.
265 
266  inline G4GRSVolume* CreateGRSVolume() const;
267  inline G4GRSSolid* CreateGRSSolid() const;
270  // `Touchable' creation methods: caller has deletion responsibility.
271 
273  // Returns a reference counted handle to a touchable history.
274 
277  G4bool* valid);
279  G4bool* valid);
280  // Return Exit Surface Normal and validity too.
281  // Can only be called if the Navigator's last Step has crossed a
282  // volume geometrical boundary.
283  // It returns the Normal to the surface pointing out of the volume that
284  // was left behind and/or into the volume that was entered.
285  // Convention:
286  // The *local* normal is in the coordinate system of the *final* volume.
287  // Restriction:
288  // Normals are not available for replica volumes (returns valid= false)
289  // These methods takes full care about how to calculate this normal,
290  // but if the surfaces are not convex it will return valid=false.
291 
292  inline G4int GetVerboseLevel() const;
293  inline void SetVerboseLevel(G4int level);
294  // Get/Set Verbose(ness) level.
295  // [if level>0 && G4VERBOSE, printout can occur]
296 
297  inline G4bool IsActive() const;
298  // Verify if the navigator is active.
299  inline void Activate(G4bool flag);
300  // Activate/inactivate the navigator.
301 
302  inline G4bool EnteredDaughterVolume() const;
303  // The purpose of this function is to inform the caller if the track is
304  // entering a daughter volume while exiting from the current volume.
305  // This method returns
306  // - True only in case 1) above, that is when the Step has caused
307  // the track to arrive at a boundary of a daughter.
308  // - False in cases 2), 3) and 4), i.e. in all other cases.
309  // This function is not guaranteed to work if SetGeometricallyLimitedStep()
310  // was not called when it should have been called.
311  inline G4bool ExitedMotherVolume() const;
312  // Verify if the step has exited the mother volume.
313 
314  inline void CheckMode(G4bool mode);
315  // Run navigation in "check-mode", therefore using additional
316  // verifications and more strict correctness conditions.
317  // Is effective only with G4VERBOSE set.
318  inline G4bool IsCheckModeActive() const;
319  inline void SetPushVerbosity(G4bool mode);
320  // Set/unset verbosity for pushed tracks (default is true).
321 
322  void PrintState() const;
323  // Print the internal state of the Navigator (for debugging).
324  // The level of detail is according to the verbosity.
325 
326  inline const G4AffineTransform& GetGlobalToLocalTransform() const;
327  inline const G4AffineTransform GetLocalToGlobalTransform() const;
328  // Obtain the transformations Global/Local (and inverse).
329  // Clients of these methods must copy the data if they need to keep it.
330 
332  G4int dReplicaNo,
333  EVolume dVolumeType );
334  // Obtain mother to daughter transformation
335 
336  inline void ResetStackAndState();
337  // Reset stack and minimum or navigator state machine necessary for reset
338  // as needed by LocalGlobalPointAndSetup.
339  // [Does not perform clears, resizes, or reset fLastLocatedPointLocal]
340 
341  inline G4int SeverityOfZeroStepping( G4int* noZeroSteps ) const;
342  // Report on severity of error and number of zero steps,
343  // in case Navigator is stuck and is returning zero steps.
344  // Values: 1 (small problem), 5 (correcting),
345  // 9 (ready to abandon), 10 (abandoned)
346 
347  /*
348  void SetSavedState();
349  // ( fValidExitNormal, fExitNormal, fExiting, fEntering,
350  // fBlockedPhysicalVolume, fBlockedReplicaNo, fLastStepWasZero);
351  void RestoreSavedState();
352  // Copy aspects of the state, to enable a non-state changing
353  // call to ComputeStep
354  */
356  // Return the local coordinate of the point in the reference system
357  // of its containing volume that was found by LocalGlobalPointAndSetup.
358  // The local coordinate of the last located track.
359 
360  inline G4ThreeVector NetTranslation() const;
361  inline G4RotationMatrix NetRotation() const;
362  // Compute+return the local->global translation/rotation of current volume.
363 
364  inline void EnableBestSafety( G4bool value= false );
365  // Enable best-possible evaluation of isotropic safety
366 
367 protected:// with description
368 
369  inline G4ThreeVector ComputeLocalPoint(const G4ThreeVector& rGlobPoint) const;
370  // Return position vector in local coordinate system, given a position
371  // vector in world coordinate system.
372 
373  inline G4ThreeVector ComputeLocalAxis(const G4ThreeVector& pVec) const;
374  // Return the local direction of the specified vector in the reference
375  // system of the volume that was found by LocalGlobalPointAndSetup.
376  // The Local Coordinates of point in world coordinate system.
377 
378  virtual void ResetState();
379  // Utility method to reset the navigator state machine.
380 
381  inline EVolume VolumeType(const G4VPhysicalVolume *pVol) const;
382  // Characterise `type' of volume - normal/replicated/parameterised.
383 
384  inline EVolume CharacteriseDaughters(const G4LogicalVolume *pLog) const;
385  // Characterise daughter of logical volume.
386 
387  inline G4int GetDaughtersRegularStructureId(const G4LogicalVolume *pLog) const;
388  // Get regular structure ID of first daughter
389 
390  virtual void SetupHierarchy();
391  // Renavigate & reset hierarchy described by current history
392  // o Reset volumes
393  // o Recompute transforms and/or solids of replicated/parameterised
394  // volumes.
395 
396 private:
397 
400  // Private copy-constructor and assignment operator.
401 
402  void ComputeStepLog(const G4ThreeVector& pGlobalpoint,
403  G4double moveLenSq) const;
404  // Log and checks for steps larger than the tolerance
405 
406 protected:// without description
407 
409  // Geometrical tolerance for surface thickness of shapes.
410 
412  // Verbose(ness) level [if > 0, printout can occur].
413 
414 private:
415 
417  // States if the navigator is activated or not.
418 
420  // After this many failed/zero steps, act (push etc)
422  // After this many failed/zero steps, abandon track
423 
424 protected:
425 public:
426  struct G4SaveNavigatorState;
427 
428  //
429  // BEGIN State information
430  //
432  {
436  { ;}
437 
440 
441  void ResetState();
442  void ResetStack();
443  void ResetStackAndState();
444 
446  // Transformation and history of the current path
447  // through the geometrical hierarchy.
448 
450  // A memory of whether in this Step a daughter volume is entered
451  // (set in Compute & Locate).
452  // After Compute: it expects to enter a daughter
453  // After Locate: it has entered a daughter
454 
456  // A similar memory whether the Step exited current "mother" volume
457  // completely, not entering daughter.
458 
460  // Set true if last Step was limited by geometry.
461 
463  // Endpoint of last ComputeStep
464  // can be used for optimisation (e.g. when computing safety).
466  // Position of the end-point of the last call to ComputeStep
467  // in last Local coordinates.
468 
470  // Push flags [if true, means a stuck particle has been pushed].
471 
472  private:
473  friend class G4ITNavigator2;
474  // The friend class would allow G4Navigator to access the private members
475  // of G4NavigatorState but not the classes inheriting from G4Navigator
476 
477  friend struct G4SaveNavigatorState;
478  friend std::ostream& operator << (std::ostream &os,const G4ITNavigator2 &n);
479 
481  // Whether ComputeStep was called since the last call to a Locate method
482  // Uses: - distinguish parts of state which differ before/after calls
483  // to ComputeStep or one of the Locate methods;
484  // - avoid two consecutive calls to compute-step (illegal).
485 
487  // Entering/Exiting volumes blocking/setup
488  // o If exiting
489  // volume ptr & replica number (set & used by Locate..())
490  // used for blocking on redescent of geometry
491  // o If entering
492  // volume ptr & replica number (set by ComputeStep(),used by
493  // Locate..()) of volume for `automatic' entry
494 
497 
499  // Position of the last located point relative to its containing volume.
501  // Whether the last call to Locate methods left the world
502 
503  G4bool fValidExitNormal;// Set true if have leaving volume normal
504  G4ThreeVector fExitNormal;// Leaving volume normal, in the
505  // volume containing the exited
506  // volume's coordinate system
507  G4ThreeVector fGrandMotherExitNormal;// Leaving volume normal, in its
508  // own coordinate system
509  G4bool fChangedGrandMotherRefFrame;// Whether frame is changed
510 
511  G4ThreeVector fExitNormalGlobalFrame;// Leaving volume normal, in the
512  // global coordinate system
513  G4bool fCalculatedExitNormal;// Has it been computed since
514  // the last call to ComputeStep
515  // Covers both Global and GrandMother
516 
517  // Count zero steps - as one or two can occur due to changing momentum at
518  // a boundary or at an edge common between volumes
519  // - several are likely a problem in the geometry
520  // description or in the navigation
521  //
523  // Whether the last ComputeStep moved Zero. Used to check for edges.
524 
526  // Whether the Navigator has detected an edge
528  // Number of preceding moves that were Zero. Reset to 0 after finite step
529 
532  // Memory of last safety origin & value. Used in ComputeStep to ensure
533  // that origin of current Step is in the same volume as the point of the
534  // last relocation
535 
536  //
537  // END State information
538  //
539  };
540 
542 
543  // Save key state information (NOT the navigation history stack)
544  //
546  {
550  { ;}
557 
558  // !>
559 // G4bool sLocatedOnEdge;
560 // G4bool sPushed;
561 // G4int sNumberZeroSteps;
562  // <!
563 
564  // Potentially relevant
565  //
570 
573  };
574 
575  // G4SaveNavigatorState* fpSaveState;
576 
577  // Tracking Invariants
578  //
580  // A link to the topmost physical volume in the detector.
581  // Must be positioned at the origin and unrotated.
582 
583  // Utility information
584  //
586  // Check-mode flag [if true, more strict checks are performed].
587 
589  // Push flag [for verbose].
590 
591  // Helpers/Utility classes
592  //
599 };
600 
601 RegisterTrackState(G4ITNavigator2, G4NavigatorState)
602 
603 #define CheckNavigatorStateIsValid() \
604 if(fpNavigatorState == 0) \
605 { \
606  G4ExceptionDescription exceptionDescription; \
607  exceptionDescription << "The navigator state is NULL. "; \
608  exceptionDescription << "Either NewNavigatorStateAndLocate was not called "; \
609  exceptionDescription << "or the provided navigator state was already NULL."; \
610  G4Exception((G4String("G4Navigator")+G4String(__FUNCTION__)).c_str(),\
611  "NavigatorStateNotValid",FatalException,exceptionDescription); \
612 }
613 
614 #include "G4ITNavigator2.icc"
615 
616 #endif
617 
618 // NOTES:
619 //
620 // The following methods provide detailed information when a Step has
621 // arrived at a geometrical boundary. They distinguish between the different
622 // causes that can result in the track leaving its current volume.
623 //
624 // Four cases are possible:
625 //
626 // 1) The particle has reached a boundary of a daughter of the current volume:
627 // (this could cause the relocation to enter the daughter itself
628 // or a potential granddaughter or further descendant)
629 //
630 // 2) The particle has reached a boundary of the current
631 // volume, exiting into a mother (regardless the level
632 // at which it is located in the tree):
633 //
634 // 3) The particle has reached a boundary of the current
635 // volume, exiting into a volume which is not in its
636 // parental hierarchy:
637 //
638 // 4) The particle is not on a boundary between volumes:
639 // the function returns an exception, and the caller is
640 // reccomended to compare the G4touchables associated
641 // to the preStepPoint and postStepPoint to handle this case.
642 //
643 // G4bool EnteredDaughterVolume()
644 // G4bool IsExitNormalValid()
645 // G4ThreeVector GetLocalExitNormal()
646 //
647 // The expected usefulness of these methods is to allow the caller to
648 // determine how to compute the surface normal at the volume boundary. The two
649 // possibilities are to obtain the normal from:
650 //
651 // i) the solid associated with the volume of the initial point of the Step.
652 // This is valid for cases 2 and 3.
653 // (Note that the initial point is generally the PreStepPoint of a Step).
654 // or
655 //
656 // ii) the solid of the final point, ie of the volume after the relocation.
657 // This is valid for case 1.
658 // (Note that the final point is generally the PreStepPoint of a Step).
659 //
660 // This way the caller can always get a valid normal, pointing outside
661 // the solid for which it is computed, that can be used at his own
662 // discretion.