Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // Class G4TransportationManager implementatio << 27 // 23 // 28 // Created : J.Apostolakis, 1997 << 24 // $Id: G4TransportationManager.cc,v 1.4 2003/12/05 17:08:27 gcosmo Exp $ 29 // Reviewed: G.Cosmo, 2006 << 25 // GEANT4 tag $Name: geant4-06-00-patch-01 $ >> 26 // >> 27 // >> 28 // G4TransportationManager >> 29 // >> 30 // Author: J.Apostolakis (John.Apostolakis@cern.ch), 1997 >> 31 // 30 // ------------------------------------------- 32 // -------------------------------------------------------------------- 31 33 32 #include "G4TransportationManager.hh" 34 #include "G4TransportationManager.hh" 33 35 34 #include <algorithm> << 35 << 36 #include "G4GeometryMessenger.hh" 36 #include "G4GeometryMessenger.hh" 37 #include "G4PropagatorInField.hh" 37 #include "G4PropagatorInField.hh" 38 #include "G4FieldManager.hh" 38 #include "G4FieldManager.hh" 39 #include "G4LogicalVolume.hh" << 40 #include "G4PVPlacement.hh" << 41 << 42 // Initialise the static instance of the singl << 43 // << 44 G4ThreadLocal G4TransportationManager* << 45 G4TransportationManager::fTransportationManage << 46 39 47 // The first registered navigator -- expect th << 40 G4TransportationManager* G4TransportationManager::fTransportationManager=0; 48 // If it has an external sub-navigator, it wi << 49 G4Navigator* G4TransportationManager::fFirstTr << 50 41 51 // ------------------------------------------- << 52 // Constructor << 53 // << 54 G4TransportationManager::G4TransportationManag 42 G4TransportationManager::G4TransportationManager() 55 { 43 { 56 if (fTransportationManager != nullptr) << 44 if (!fTransportationManager) 57 { 45 { 58 G4Exception("G4TransportationManager::G4Tr << 46 fGeomMessenger= new G4GeometryMessenger(this); 59 "GeomNav0002", FatalException, << 47 fNavigatorForTracking= new G4Navigator() ; 60 "Only ONE instance of G4Transp << 48 fFieldManager= new G4FieldManager() ; 61 } << 49 fPropagatorInField= new G4PropagatorInField( fNavigatorForTracking, 62 << 50 fFieldManager); 63 // Create the navigator for tracking and act << 64 // << 65 G4Navigator* trackingNavigator= nullptr; << 66 if( (fFirstTrackingNavigator != nullptr) && << 67 { << 68 trackingNavigator = fFirstTrackingNavigat << 69 } 51 } 70 else 52 else 71 { 53 { 72 trackingNavigator = new G4Navigator(); << 54 G4cerr << "Only ONE instance of G4TransportationManager is allowed!" 73 if( fFirstTrackingNavigator == nullptr ) << 55 << G4endl; 74 { << 56 G4Exception("G4TransportationManager::G4TransportationManager()", 75 fFirstTrackingNavigator = trackingNavi << 57 "InvalidSetup", FatalException, 76 } << 58 "Only ONE instance of G4TransportationManager is allowed!"); 77 } 59 } 78 trackingNavigator->Activate(true); << 79 fNavigators.push_back(trackingNavigator); << 80 fActiveNavigators.push_back(trackingNavigato << 81 fWorlds.push_back(trackingNavigator->GetWorl << 82 << 83 fGeomMessenger = new G4GeometryMessenger( << 84 fFieldManager = new G4FieldManager(); // << 85 fPropagatorInField= new G4PropagatorInField( << 86 fSafetyHelper = new G4SafetyHelper(); << 87 << 88 G4FieldManager::SetGlobalFieldManager(fField << 89 } 60 } 90 61 91 // ------------------------------------------- << 92 // Destructor << 93 // << 94 G4TransportationManager::~G4TransportationMana 62 G4TransportationManager::~G4TransportationManager() 95 { 63 { 96 delete fSafetyHelper; << 97 delete fPropagatorInField; << 98 delete fGeomMessenger; 64 delete fGeomMessenger; 99 ClearNavigators(); << 65 delete fNavigatorForTracking; 100 fTransportationManager = nullptr; << 66 delete fPropagatorInField; 101 } << 67 delete fFieldManager; 102 << 103 // ------------------------------------------- << 104 // GetTransportationManager() << 105 // << 106 // Retrieve the static instance of the singlet << 107 // << 108 G4TransportationManager* G4TransportationManag << 109 { << 110 if (fTransportationManager == nullptr) << 111 { << 112 fTransportationManager = new G4Transporta << 113 } << 114 return fTransportationManager; << 115 } 68 } 116 69 117 // ------------------------------------------- << 70 G4TransportationManager* G4TransportationManager::GetTransportationManager() 118 // GetInstanceIfExist() << 119 // << 120 // Retrieve the static instance pointer of the << 121 // << 122 G4TransportationManager* G4TransportationManag << 123 { 71 { >> 72 static G4TransportationManager theInstance; >> 73 if (!fTransportationManager) >> 74 fTransportationManager = &theInstance; >> 75 124 return fTransportationManager; 76 return fTransportationManager; 125 } 77 } 126 78 127 // ------------------------------------------- << 128 // SetFieldManager() << 129 // << 130 // Set the associated field manager. << 131 // << 132 void G4TransportationManager::SetFieldManager( 79 void G4TransportationManager::SetFieldManager(G4FieldManager* newFieldManager) 133 { 80 { 134 fFieldManager = newFieldManager; 81 fFieldManager = newFieldManager; 135 G4FieldManager::SetGlobalFieldManager(fFiel << 136 82 137 // Message the PropagatorInField, 83 // Message the PropagatorInField, 138 // which also maintains this information (t << 84 // which also maintains this information (to be reviewed) 139 // << 85 if( fPropagatorInField ) 140 if( fPropagatorInField != nullptr ) << 141 { << 142 fPropagatorInField -> SetDetectorFieldMa 86 fPropagatorInField -> SetDetectorFieldManager( newFieldManager ); 143 } << 144 } << 145 << 146 // ------------------------------------------- << 147 // SetNavigatorForTracking() << 148 // << 149 // Set the active navigator for tracking, alwa << 150 // the first in the collection of registered n << 151 // << 152 void G4TransportationManager::SetNavigatorForT << 153 { << 154 fNavigators[0] = newNavigator; << 155 fActiveNavigators[0] = newNavigator; << 156 fPropagatorInField->SetNavigatorForPropagat << 157 } << 158 << 159 // ------------------------------------------- << 160 // ClearNavigators() << 161 // << 162 // Clear collection of navigators and delete a << 163 // Called only by the class destructor. << 164 // << 165 void G4TransportationManager::ClearNavigators( << 166 { << 167 for (const auto & fNavigator : fNavigators) << 168 { << 169 delete fNavigator; << 170 } << 171 fNavigators.clear(); << 172 fActiveNavigators.clear(); << 173 fWorlds.clear(); << 174 } << 175 << 176 // ------------------------------------------- << 177 // GetParallelWorld() << 178 // << 179 // Provided the name of a world volume, return << 180 // If not existing, create (allocate) and regi << 181 // << 182 G4VPhysicalVolume* << 183 G4TransportationManager::GetParallelWorld( con << 184 { << 185 G4VPhysicalVolume* wPV = IsWorldExisting(wo << 186 if (wPV == nullptr) << 187 { << 188 wPV = GetNavigatorForTracking()->GetWorld << 189 G4LogicalVolume* wLV = wPV->GetLogicalVol << 190 wLV = new G4LogicalVolume(wLV->GetSolid() << 191 worldName); << 192 wPV = new G4PVPlacement (wPV->GetRotation << 193 wPV->GetTranslat << 194 wLV, worldName, << 195 RegisterWorld(wPV); << 196 } << 197 return wPV; << 198 } << 199 << 200 // ------------------------------------------- << 201 // GetNavigator() << 202 // << 203 // Provided the name of a world volume, return << 204 // If not existing, create it and register it << 205 // exception if the associated parallel world << 206 // << 207 G4Navigator* G4TransportationManager::GetNavig << 208 { << 209 // If already existing, return the stored p << 210 // << 211 for (const auto & fNavigator : fNavigators) << 212 { << 213 if (fNavigator->GetWorldVolume()->GetNam << 214 { << 215 return fNavigator; << 216 } << 217 } << 218 << 219 // Check if world of that name already exis << 220 // create a navigator and register it << 221 // << 222 G4Navigator* aNavigator = nullptr; << 223 G4VPhysicalVolume* aWorld = IsWorldExisting << 224 if(aWorld != nullptr) << 225 { << 226 aNavigator = new G4Navigator(); << 227 aNavigator->SetWorldVolume(aWorld); << 228 fNavigators.push_back(aNavigator); << 229 } << 230 else << 231 { << 232 G4String message << 233 = "World volume with name -" + worldN << 234 + "- does not exist. Create it first << 235 G4Exception("G4TransportationManager::Ge << 236 "GeomNav0002", FatalExceptio << 237 } << 238 << 239 return aNavigator; << 240 } << 241 << 242 // ------------------------------------------- << 243 // GetNavigator() << 244 // << 245 // Provided a pointer to a world volume, retur << 246 // Create it in case not existing and add it t << 247 // If world volume not existing, issue an exce << 248 // << 249 G4Navigator* G4TransportationManager::GetNavig << 250 { << 251 for (const auto & fNavigator : fNavigators) << 252 { << 253 if (fNavigator->GetWorldVolume() == aWorl << 254 } << 255 G4Navigator* aNavigator = nullptr; << 256 auto pWorld = std::find(fWorlds.cbegin(), f << 257 if (pWorld != fWorlds.cend()) << 258 { << 259 aNavigator = new G4Navigator(); << 260 aNavigator->SetWorldVolume(aWorld); << 261 fNavigators.push_back(aNavigator); << 262 } << 263 else << 264 { << 265 G4String message << 266 = "World volume with name -" + aWorld << 267 + "- does not exist. Create it first << 268 G4Exception("G4TransportationManager::Ge << 269 "GeomNav0002", FatalExceptio << 270 } << 271 << 272 return aNavigator; << 273 } << 274 << 275 // ------------------------------------------- << 276 // DeRegisterNavigator() << 277 // << 278 // Provided a pointer to an already allocated << 279 // associated entry in the navigators collecti << 280 // delete the actual pointed object, which is << 281 // The navigator for tracking -cannot- be dere << 282 // << 283 void G4TransportationManager::DeRegisterNaviga << 284 { << 285 if (aNavigator == fNavigators[0]) << 286 { << 287 G4Exception("G4TransportationManager::De << 288 "GeomNav0003", FatalExceptio << 289 "The navigator for tracking << 290 } << 291 auto pNav = std::find(fNavigators.cbegin(), << 292 if (pNav != fNavigators.cend()) << 293 { << 294 // Deregister associated world volume << 295 // << 296 DeRegisterWorld((*pNav)->GetWorldVolume( << 297 << 298 // Deregister the navigator << 299 // << 300 fNavigators.erase(pNav); << 301 } << 302 else << 303 { << 304 G4String message << 305 = "Navigator for volume -" + aNavigat << 306 + "- not found in memory!"; << 307 G4Exception("G4TransportationManager::De << 308 "GeomNav1002", JustWarning, << 309 } << 310 } << 311 << 312 // ------------------------------------------- << 313 // ActivateNavigator() << 314 // << 315 // Provided a pointer to an already allocated << 316 // the associated activation flag for the navi << 317 // If the provided navigator is not already re << 318 // Return the index of the activated navigator << 319 // ComputeStep() method of G4PathFinder. << 320 // << 321 G4int G4TransportationManager::ActivateNavigat << 322 { << 323 auto pNav = std::find(fNavigators.cbegin(), << 324 if (pNav == fNavigators.cend()) << 325 { << 326 G4String message << 327 = "Navigator for volume -" + aNavigat << 328 + "- not found in memory!"; << 329 G4Exception("G4TransportationManager::Ac << 330 "GeomNav1002", FatalExceptio << 331 return -1; << 332 } << 333 << 334 aNavigator->Activate(true); << 335 G4int id = 0; << 336 for(const auto & fActiveNavigator : fActive << 337 { << 338 if (fActiveNavigator == aNavigator) { r << 339 ++id; << 340 } << 341 << 342 fActiveNavigators.push_back(aNavigator); << 343 return id; << 344 } << 345 << 346 // ------------------------------------------- << 347 // DeActivateNavigator() << 348 // << 349 // Provided a pointer to an already allocated << 350 // the associated activation flag in the navig << 351 // If the provided navigator is not already re << 352 // << 353 void G4TransportationManager::DeActivateNaviga << 354 { << 355 auto pNav = std::find(fNavigators.cbegin(), << 356 if (pNav != fNavigators.cend()) << 357 { << 358 (*pNav)->Activate(false); << 359 } << 360 else << 361 { << 362 G4String message << 363 = "Navigator for volume -" + aNavigat << 364 + "- not found in memory!"; << 365 G4Exception("G4TransportationManager::De << 366 "GeomNav1002", JustWarning, << 367 } << 368 << 369 auto pActiveNav = std::find(fActiveNavigato << 370 fActiveNavigato << 371 if (pActiveNav != fActiveNavigators.cend()) << 372 { << 373 fActiveNavigators.erase(pActiveNav); << 374 } << 375 } << 376 << 377 // ------------------------------------------- << 378 // InactivateAll() << 379 // << 380 // Inactivate all the navigators except for th << 381 // store of active navigators. << 382 // << 383 void G4TransportationManager::InactivateAll( ) << 384 { << 385 for (const auto & fActiveNavigator : fActiv << 386 { << 387 fActiveNavigator->Activate(false); << 388 } << 389 fActiveNavigators.clear(); << 390 << 391 // Restore status for the navigator for tra << 392 // << 393 fNavigators[0]->Activate(true); << 394 fActiveNavigators.push_back(fNavigators[0]) << 395 } << 396 << 397 // ------------------------------------------- << 398 // IsWorldExisting() << 399 // << 400 // Verify existance or not of an istance of th << 401 // same name in the collection. Return the wor << 402 // << 403 G4VPhysicalVolume* << 404 G4TransportationManager::IsWorldExisting ( con << 405 { << 406 auto pWorld = fWorlds.begin(); << 407 if ( *pWorld==nullptr ) { *pWorld=fNavigat << 408 << 409 for (const auto & fWorld : fWorlds) << 410 { << 411 if (fWorld->GetName() == name ) { return << 412 } << 413 return nullptr; << 414 } << 415 << 416 // ------------------------------------------- << 417 // RegisterWorld() << 418 // << 419 // Provided a pointer to an already allocated << 420 // associated entry in the worlds collection. << 421 // succeeds and the new entry is created. << 422 // << 423 G4bool G4TransportationManager::RegisterWorld( << 424 { << 425 G4bool done = false; << 426 << 427 auto pWorld = std::find(fWorlds.cbegin(), f << 428 if (pWorld == fWorlds.cend()) << 429 { << 430 fWorlds.push_back(aWorld); << 431 done = true; << 432 } << 433 return done; << 434 } << 435 << 436 // ------------------------------------------- << 437 // DeRegisterWorld() << 438 // << 439 // Provided a pointer to an already allocated << 440 // associated entry in the worlds collection b << 441 // pointed object, which is still owned by the << 442 // << 443 void G4TransportationManager::DeRegisterWorld( << 444 { << 445 auto pWorld = std::find(fWorlds.cbegin(), f << 446 if (pWorld != fWorlds.cend()) << 447 { << 448 fWorlds.erase(pWorld); << 449 } << 450 else << 451 { << 452 G4String message << 453 = "World volume -" + aWorld->GetName() << 454 G4Exception("G4TransportationManager::DeR << 455 "GeomNav1002", JustWarning, m << 456 } << 457 } << 458 << 459 // ------------------------------------------- << 460 // ClearParallelWorlds() << 461 // << 462 // Clear collection of navigators and delete a << 463 // parallel worlds. << 464 // Called only by the RunManager when the enti << 465 // scratch. << 466 // << 467 void G4TransportationManager::ClearParallelWor << 468 { << 469 auto pNav = fNavigators.cbegin(); << 470 G4Navigator* trackingNavigator = *pNav; << 471 for (pNav=fNavigators.cbegin(); pNav!=fNavi << 472 { << 473 if (*pNav != trackingNavigator) { delete << 474 } << 475 fNavigators.clear(); << 476 fActiveNavigators.clear(); << 477 fWorlds.clear(); << 478 << 479 fNavigators.push_back(trackingNavigator); << 480 fActiveNavigators.push_back(trackingNavigat << 481 fWorlds.push_back(nullptr); // NULL registe << 482 } << 483 << 484 // ------------------------------------------- << 485 // GetFirstTrackingNavigator() << 486 // << 487 // Get pointer to the first tracking Navigator << 488 // << 489 G4Navigator* G4TransportationManager::GetFirst << 490 { << 491 return fFirstTrackingNavigator; << 492 } << 493 << 494 // ------------------------------------------- << 495 // GetFirstTrackingNavigator() << 496 // << 497 // Get pointer to the first tracking Navigator << 498 << 499 void G4TransportationManager::SetFirstTracking << 500 { << 501 fFirstTrackingNavigator= nav; << 502 } 87 } 503 88