G4TransportationManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 //
00030 // G4TransportationManager 
00031 //
00032 // Created : J.Apostolakis, 1997
00033 // Reviewed: G.Cosmo, 2006
00034 //  10.04.07 V.Ivanchenko  Use unique G4SafetyHelper
00035 //
00036 // --------------------------------------------------------------------
00037 
00038 #include "G4TransportationManager.hh"
00039 
00040 #include <algorithm>
00041 
00042 #include "G4GeometryMessenger.hh"
00043 #include "G4PropagatorInField.hh"
00044 #include "G4FieldManager.hh"
00045 #include "G4LogicalVolume.hh"
00046 #include "G4PVPlacement.hh"
00047 
00048 // Initialise the static instance of the singleton
00049 //
00050 G4TransportationManager* G4TransportationManager::fTransportationManager=0;
00051 
00052 // ----------------------------------------------------------------------------
00053 // Constructor
00054 //
00055 G4TransportationManager::G4TransportationManager() 
00056 { 
00057   if (fTransportationManager)
00058   {
00059     G4Exception("G4TransportationManager::G4TransportationManager()",
00060                 "GeomNav0002", FatalException,
00061                 "Only ONE instance of G4TransportationManager is allowed!");
00062   }
00063 
00064   // Create the navigator for tracking and activate it; add to collections
00065   //
00066   G4Navigator* trackingNavigator = new G4Navigator();
00067   trackingNavigator->Activate(true);
00068   fNavigators.push_back(trackingNavigator);
00069   fActiveNavigators.push_back(trackingNavigator);
00070   fWorlds.push_back(trackingNavigator->GetWorldVolume()); // NULL registered
00071 
00072   fGeomMessenger    = new G4GeometryMessenger(this);
00073   fFieldManager     = new G4FieldManager();
00074   fPropagatorInField= new G4PropagatorInField(trackingNavigator,fFieldManager);
00075   fSafetyHelper     = new G4SafetyHelper();
00076 } 
00077 
00078 // ----------------------------------------------------------------------------
00079 // Destructor
00080 //
00081 G4TransportationManager::~G4TransportationManager()
00082 {
00083   delete fFieldManager; 
00084   delete fPropagatorInField;
00085   ClearNavigators(); 
00086   delete fGeomMessenger;
00087   delete fSafetyHelper;
00088 }
00089 
00090 // ----------------------------------------------------------------------------
00091 // GetTransportationManager()
00092 //
00093 // Retrieve the static instance of the singleton
00094 //
00095 G4TransportationManager* G4TransportationManager::GetTransportationManager()
00096 {
00097    static G4TransportationManager theInstance;
00098    if (!fTransportationManager)
00099      fTransportationManager = &theInstance;
00100    
00101    return fTransportationManager;
00102 }
00103 
00104 // ----------------------------------------------------------------------------
00105 // SetFieldManager()
00106 //
00107 // Set the associated field manager.
00108 //
00109 void G4TransportationManager::SetFieldManager(G4FieldManager* newFieldManager)
00110 {
00111    fFieldManager = newFieldManager; 
00112 
00113    // Message the PropagatorInField, 
00114    // which also maintains this information (to be reviewed)
00115    //
00116    if( fPropagatorInField )
00117    {
00118       fPropagatorInField -> SetDetectorFieldManager( newFieldManager );
00119    }
00120 }
00121 
00122 // ----------------------------------------------------------------------------
00123 // ClearNavigators()
00124 //
00125 // Clear collection of navigators and delete allocated objects.
00126 // Called only by the class destructor.
00127 //
00128 void G4TransportationManager::ClearNavigators()
00129 {
00130    std::vector<G4Navigator*>::iterator pNav;
00131    for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
00132    {
00133      delete *pNav;
00134    }
00135    fNavigators.clear();
00136    fActiveNavigators.clear();
00137    fWorlds.clear();
00138 }
00139 
00140 // ----------------------------------------------------------------------------
00141 // GetParallelWorld()
00142 //
00143 // Provided the name of a world volume, returns the associated world pointer.
00144 // If not existing, create (allocate) and register it in the collection.
00145 //
00146 G4VPhysicalVolume*
00147 G4TransportationManager::GetParallelWorld( const G4String& worldName )
00148 {
00149    G4VPhysicalVolume* wPV = IsWorldExisting(worldName);
00150    if (!wPV)
00151    {
00152      wPV = GetNavigatorForTracking()->GetWorldVolume();
00153      G4LogicalVolume* wLV = wPV->GetLogicalVolume();
00154      wLV = new G4LogicalVolume(wLV->GetSolid(), 0,
00155                                worldName);
00156      wPV = new G4PVPlacement (wPV->GetRotation(),
00157                               wPV->GetTranslation(),
00158                               wLV, worldName, 0, false, 0);
00159      RegisterWorld(wPV);
00160    }
00161    return wPV;
00162 }
00163 
00164 // ----------------------------------------------------------------------------
00165 // GetNavigator()
00166 //
00167 // Provided the name of a world volume, returns the associated navigator.
00168 // If not existing, create it and register it in the collection, throw an
00169 // exception if the associated parallel world does not exist.
00170 //
00171 G4Navigator* G4TransportationManager::GetNavigator( const G4String& worldName )
00172 {
00173    // If already existing, return the stored pointer to the navigator
00174    //
00175    std::vector<G4Navigator*>::iterator pNav;
00176    for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
00177    {
00178       if ((*pNav)->GetWorldVolume()->GetName() == worldName) { return *pNav; }
00179    }
00180 
00181    // Check if world of that name already exists,
00182    // create a navigator and register it
00183    //
00184    G4Navigator* aNavigator = 0;
00185    G4VPhysicalVolume* aWorld = IsWorldExisting(worldName);
00186    if(aWorld)
00187    {
00188       aNavigator = new G4Navigator();
00189       aNavigator->SetWorldVolume(aWorld);
00190       fNavigators.push_back(aNavigator);
00191    }
00192    else
00193    {
00194       G4String message
00195          = "World volume with name -" + worldName
00196          + "- does not exist. Create it first by GetParallelWorld() method!";      
00197       G4Exception("G4TransportationManager::GetNavigator(name)",
00198                   "GeomNav0002", FatalException, message);
00199    }
00200 
00201    return aNavigator;
00202 }
00203 
00204 // ----------------------------------------------------------------------------
00205 // GetNavigator()
00206 //
00207 // Provided a pointer to a world volume, returns the associated navigator.
00208 // Create it in case not existing and add it to the collection.
00209 // If world volume not existing, issue an exception.
00210 //
00211 G4Navigator* G4TransportationManager::GetNavigator( G4VPhysicalVolume* aWorld )
00212 {
00213    std::vector<G4Navigator*>::iterator pNav;
00214    for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
00215    {
00216      if ((*pNav)->GetWorldVolume() == aWorld) { return *pNav; }
00217    }
00218    G4Navigator* aNavigator = 0;
00219    std::vector<G4VPhysicalVolume*>::iterator pWorld =
00220      std::find(fWorlds.begin(), fWorlds.end(), aWorld);
00221    if (pWorld != fWorlds.end())
00222    {
00223       aNavigator = new G4Navigator();
00224       aNavigator->SetWorldVolume(aWorld);
00225       fNavigators.push_back(aNavigator);
00226    }
00227    else
00228    {
00229       G4String message
00230          = "World volume with name -" + aWorld->GetName()
00231          + "- does not exist. Create it first by GetParallelWorld() method!";
00232       G4Exception("G4TransportationManager::GetNavigator(pointer)",
00233                   "GeomNav0002", FatalException, message);
00234    }
00235 
00236    return aNavigator;
00237 }
00238 
00239 // ----------------------------------------------------------------------------
00240 // DeRegisterNavigator()
00241 //
00242 // Provided a pointer to an already allocated navigator object, removes the
00243 // associated entry in the navigators collection (remove pair) but does not
00244 // delete the actual pointed object, which is still owned by the caller.
00245 // The navigator for tracking -cannot- be deregistered.
00246 //
00247 void G4TransportationManager::DeRegisterNavigator( G4Navigator* aNavigator )
00248 {
00249    if (aNavigator == fNavigators[0])
00250    {
00251       G4Exception("G4TransportationManager::DeRegisterNavigator()",
00252                   "GeomNav0003", FatalException,
00253                   "The navigator for tracking CANNOT be deregistered!");
00254    }
00255    std::vector<G4Navigator*>::iterator pNav =
00256      std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
00257    if (pNav != fNavigators.end())
00258    {
00259       // Deregister associated world volume
00260       //
00261       DeRegisterWorld((*pNav)->GetWorldVolume());
00262 
00263       // Deregister the navigator
00264       //
00265       fNavigators.erase(pNav);
00266    }
00267    else
00268    {
00269       G4String message
00270          = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
00271          + "- not found in memory!";      
00272       G4Exception("G4TransportationManager::DeRegisterNavigator()",
00273                   "GeomNav1002", JustWarning, message);
00274    }
00275 }
00276 
00277 // ----------------------------------------------------------------------------
00278 // ActivateNavigator()
00279 //
00280 // Provided a pointer to an already allocated navigator object, set to 'true'
00281 // the associated activation flag for the navigator in the collection.
00282 // If the provided navigator is not already registered, issue a warning
00283 // Return the index of the activated navigator. This index should be used for
00284 // ComputeStep() method of G4PathFinder.
00285 //
00286 G4int G4TransportationManager::ActivateNavigator( G4Navigator* aNavigator )
00287 {
00288    std::vector<G4Navigator*>::iterator pNav =
00289      std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
00290    if (pNav == fNavigators.end())
00291    {
00292       G4String message
00293          = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
00294          + "- not found in memory!";      
00295       G4Exception("G4TransportationManager::ActivateNavigator()",
00296                   "GeomNav1002", JustWarning, message);
00297       return -1;
00298    }
00299 
00300    aNavigator->Activate(true);
00301    G4int id = 0;
00302    std::vector<G4Navigator*>::iterator pActiveNav;
00303    for(pActiveNav=fActiveNavigators.begin();
00304        pActiveNav!=fActiveNavigators.end(); pActiveNav++)
00305    {
00306       if (*pActiveNav == aNavigator)  { return id; }
00307       id++;
00308    }
00309    
00310    fActiveNavigators.push_back(aNavigator);
00311    return id;
00312 }
00313 
00314 // ----------------------------------------------------------------------------
00315 // DeActivateNavigator()
00316 //
00317 // Provided a pointer to an already allocated navigator object, set to 'false'
00318 // the associated activation flag in the navigators collection.
00319 // If the provided navigator is not already registered, issue a warning.
00320 //
00321 void G4TransportationManager::DeActivateNavigator( G4Navigator* aNavigator )
00322 {
00323    std::vector<G4Navigator*>::iterator pNav =
00324      std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
00325    if (pNav != fNavigators.end())
00326    {
00327       (*pNav)->Activate(false);
00328    }
00329    else
00330    {
00331       G4String message
00332          = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
00333          + "- not found in memory!";
00334       G4Exception("G4TransportationManager::DeActivateNavigator()",
00335                   "GeomNav1002", JustWarning, message);
00336    }
00337 
00338    std::vector<G4Navigator*>::iterator pActiveNav =
00339      std::find(fActiveNavigators.begin(), fActiveNavigators.end(), aNavigator);
00340    if (pActiveNav != fActiveNavigators.end())
00341    {
00342       fActiveNavigators.erase(pActiveNav);
00343    }
00344 }
00345 
00346 // ----------------------------------------------------------------------------
00347 // InactivateAll()
00348 //
00349 // Inactivate all the navigators except for the tracking one, and clear the
00350 // store of active navigators.
00351 //
00352 void G4TransportationManager::InactivateAll( )
00353 {
00354    std::vector<G4Navigator*>::iterator pNav;
00355    for (pNav=fActiveNavigators.begin(); pNav!=fActiveNavigators.end(); pNav++)
00356    {
00357       (*pNav)->Activate(false);
00358    }
00359    fActiveNavigators.clear();
00360 
00361    // Restore status for the navigator for tracking
00362    //
00363    fNavigators[0]->Activate(true);
00364    fActiveNavigators.push_back(fNavigators[0]);
00365 }
00366 
00367 // ----------------------------------------------------------------------------
00368 // IsWorldExisting()
00369 //
00370 // Verify existance or not of an istance of the world volume with
00371 // same name in the collection. Return the world pointer if existing.
00372 //
00373 G4VPhysicalVolume*
00374 G4TransportationManager::IsWorldExisting ( const G4String& name )
00375 {
00376    std::vector<G4VPhysicalVolume*>::iterator pWorld = fWorlds.begin();
00377    if (*pWorld==0)  { *pWorld=fNavigators[0]->GetWorldVolume(); }
00378 
00379    for (pWorld=fWorlds.begin(); pWorld!=fWorlds.end(); pWorld++)
00380    {
00381       if ((*pWorld)->GetName() == name ) { return *pWorld; }
00382    }
00383    return 0;
00384 }
00385 
00386 // ----------------------------------------------------------------------------
00387 // RegisterWorld()
00388 //
00389 // Provided a pointer to an already allocated world object, check and add the
00390 // associated entry in the worlds collection. Return 'true' if registration
00391 // succeeds and the new entry is created.
00392 //
00393 G4bool G4TransportationManager::RegisterWorld( G4VPhysicalVolume* aWorld )
00394 {
00395    G4bool done = false;
00396 
00397    std::vector<G4VPhysicalVolume*>::iterator pWorld =
00398      std::find(fWorlds.begin(), fWorlds.end(), aWorld);
00399    if (pWorld == fWorlds.end())
00400    {
00401      fWorlds.push_back(aWorld);
00402      done = true;
00403    }
00404    return done;
00405 }
00406 
00407 // ----------------------------------------------------------------------------
00408 // DeRegisterWorld()
00409 //
00410 // Provided a pointer to an already allocated world object, removes the
00411 // associated entry in the worlds collection but does not delete the actual
00412 // pointed object, which is still owned by the caller.
00413 //
00414 void G4TransportationManager::DeRegisterWorld( G4VPhysicalVolume* aWorld )
00415 {
00416    std::vector<G4VPhysicalVolume*>::iterator pWorld =
00417      std::find(fWorlds.begin(), fWorlds.end(), aWorld);
00418    if (pWorld != fWorlds.end())
00419    {
00420       fWorlds.erase(pWorld);
00421    }
00422    else
00423    {
00424      G4String message
00425        = "World volume -" + aWorld->GetName() + "- not found in memory!";      
00426      G4Exception("G4TransportationManager::DeRegisterWorld()",
00427                  "GeomNav1002", JustWarning, message);
00428    }
00429 }

Generated on Mon May 27 17:50:02 2013 for Geant4 by  doxygen 1.4.7