[831] | 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 | // |
---|
[921] | 26 | // $Id: G4SafetyHelper.cc,v 1.16 2008/10/24 14:00:03 gcosmo Exp $ |
---|
[831] | 27 | // GEANT4 tag $ Name: $ |
---|
| 28 | // |
---|
| 29 | // class G4SafetyHelper Implementation |
---|
| 30 | // |
---|
| 31 | // Original author: John Apostolakis, 2006 |
---|
| 32 | // |
---|
| 33 | // -------------------------------------------------------------------- |
---|
| 34 | |
---|
| 35 | #include "G4SafetyHelper.hh" |
---|
| 36 | #include "G4PathFinder.hh" |
---|
| 37 | #include "G4TransportationManager.hh" |
---|
| 38 | #include "G4Navigator.hh" |
---|
| 39 | |
---|
| 40 | #include "globals.hh" |
---|
| 41 | |
---|
| 42 | G4SafetyHelper::G4SafetyHelper() |
---|
| 43 | : fUseParallelGeometries(false), // By default, one geometry only |
---|
| 44 | fFirstCall(true), |
---|
| 45 | fLastSafetyPosition(0.0,0.0,0.0), |
---|
| 46 | fLastSafety(0.0), |
---|
| 47 | fRecomputeFactor(0.0) |
---|
| 48 | { |
---|
| 49 | fpPathFinder= 0; // Cannot initialise this yet - a loop results |
---|
| 50 | |
---|
| 51 | // Initialization of the Navigator pointer is postponed, and must |
---|
| 52 | // be undertaken by another class calling InitialiseHelper() |
---|
| 53 | // |
---|
| 54 | fpMassNavigator= 0; |
---|
| 55 | fMassNavigatorId= -1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void G4SafetyHelper::InitialiseNavigator() |
---|
| 59 | { |
---|
| 60 | fpPathFinder= G4PathFinder::GetInstance(); |
---|
| 61 | |
---|
| 62 | G4TransportationManager* pTransportMgr= |
---|
| 63 | G4TransportationManager::GetTransportationManager(); |
---|
| 64 | |
---|
| 65 | fpMassNavigator = pTransportMgr->GetNavigatorForTracking(); |
---|
| 66 | |
---|
| 67 | // Check |
---|
| 68 | // |
---|
| 69 | G4VPhysicalVolume* worldPV = fpMassNavigator->GetWorldVolume(); |
---|
| 70 | if( worldPV == 0 ) |
---|
| 71 | { |
---|
| 72 | G4Exception("G4SafetyHelper::InitialiseNavigator", |
---|
| 73 | "InvalidNavigatorWorld", FatalException, |
---|
| 74 | "Found that existing tracking Navigator has NULL world"); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | fMassNavigatorId = pTransportMgr->ActivateNavigator( fpMassNavigator ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void G4SafetyHelper::InitialiseHelper() |
---|
| 81 | { |
---|
| 82 | fLastSafetyPosition = G4ThreeVector(0.0,0.0,0.0); |
---|
| 83 | fLastSafety = 0.0; |
---|
| 84 | if (fFirstCall) { InitialiseNavigator(); } |
---|
| 85 | fFirstCall = false; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | G4SafetyHelper::~G4SafetyHelper() |
---|
| 89 | { |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | G4double |
---|
| 93 | G4SafetyHelper::CheckNextStep(const G4ThreeVector &position, |
---|
| 94 | const G4ThreeVector &direction, |
---|
| 95 | const G4double currentMaxStep, |
---|
| 96 | G4double& newSafety ) |
---|
| 97 | { |
---|
| 98 | // Distance in the Mass geometry |
---|
| 99 | // |
---|
| 100 | G4double linstep = fpMassNavigator->CheckNextStep( position, |
---|
| 101 | direction, |
---|
| 102 | currentMaxStep, |
---|
| 103 | newSafety); |
---|
| 104 | fLastSafetyPosition = position; |
---|
| 105 | fLastSafety = newSafety; |
---|
| 106 | |
---|
| 107 | // TO-DO: Can replace this with a call to PathFinder |
---|
| 108 | // giving id of Mass Geometry --> this avoid doing the work twice |
---|
| 109 | |
---|
| 110 | return linstep; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | G4double G4SafetyHelper::ComputeSafety( const G4ThreeVector& position ) |
---|
| 114 | { |
---|
| 115 | G4double newSafety; |
---|
| 116 | |
---|
| 117 | // Only recompute (calling Navigator/PathFinder) if 'position' |
---|
| 118 | // is *not* the safety location and has moved 'significantly' |
---|
| 119 | // |
---|
| 120 | G4double moveLengthSq = (position-fLastSafetyPosition).mag2(); |
---|
| 121 | G4double safeDistance = fRecomputeFactor*fLastSafety; |
---|
| 122 | if( (moveLengthSq > 0.0 ) |
---|
| 123 | && (moveLengthSq >= safeDistance*safeDistance)) |
---|
| 124 | { |
---|
| 125 | fLastSafetyPosition = position; |
---|
| 126 | |
---|
| 127 | if( !fUseParallelGeometries ) |
---|
| 128 | { |
---|
| 129 | // Safety for mass geometry |
---|
[921] | 130 | fLastSafety = fpMassNavigator->ComputeSafety(position,true); |
---|
[831] | 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | // Safety for all geometries |
---|
[921] | 135 | fLastSafety = fpPathFinder->ComputeSafety(position); |
---|
[831] | 136 | } |
---|
| 137 | newSafety = fLastSafety; |
---|
| 138 | } |
---|
| 139 | else |
---|
| 140 | { |
---|
| 141 | // return last value if position is not significantly changed |
---|
| 142 | // |
---|
| 143 | G4double moveLength = 0; |
---|
| 144 | if( moveLengthSq > 0.0 ) |
---|
| 145 | { |
---|
| 146 | moveLength= std::sqrt(moveLengthSq); |
---|
| 147 | } |
---|
| 148 | newSafety = fLastSafety-moveLength; |
---|
| 149 | } |
---|
| 150 | return newSafety; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void G4SafetyHelper::ReLocateWithinVolume( const G4ThreeVector &newPosition ) |
---|
| 154 | { |
---|
| 155 | if( !fUseParallelGeometries ) |
---|
| 156 | { |
---|
| 157 | fpMassNavigator->LocateGlobalPointWithinVolume( newPosition ); |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | fpPathFinder->ReLocate( newPosition ); |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void G4SafetyHelper::Locate( const G4ThreeVector& newPosition, |
---|
| 166 | const G4ThreeVector& newDirection) |
---|
| 167 | { |
---|
| 168 | if( !fUseParallelGeometries) |
---|
| 169 | { |
---|
| 170 | fpMassNavigator->LocateGlobalPointAndSetup(newPosition, &newDirection, |
---|
| 171 | true, false); |
---|
| 172 | } |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | fpPathFinder->Locate( newPosition, newDirection ); |
---|
| 176 | } |
---|
| 177 | } |
---|