Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ITTransportationManager.cc
Go to the documentation of this file.
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 // $Id: G4ITTransportationManager.cc 87061 2014-11-24 11:43:34Z gcosmo $
27 //
31 //
32 // History:
33 // -----------
34 //
35 // -------------------------------------------------------------------
36 
39 #include "G4ITNavigator.hh"
40 #include "G4ITSafetyHelper.hh"
41 #include "G4PVPlacement.hh"
42 
44 G4ITTransportationManager::fpInstance(0);
45 
46 G4ITTransportationManager::G4ITTransportationManager()
47 {
48  Initialize();
49 }
50 
51 G4ITTransportationManager::~G4ITTransportationManager()
52 {
53  ClearNavigators();
54  if (fpSafetyHelper) delete fpSafetyHelper;
55 }
56 
58 {
59  if (fpInstance)
60  {
61  delete fpInstance;
62  fpInstance = 0;
63  }
64 }
65 
66 // ----------------------------------------------------------------------------
67 // ClearNavigators()
68 //
69 // Clear collection of navigators and delete allocated objects.
70 // Called only by the class destructor.
71 //
72 void G4ITTransportationManager::ClearNavigators()
73 {
74  std::vector<G4ITNavigator*>::iterator pNav;
75  for (pNav = fNavigators.begin(); pNav != fNavigators.end(); pNav++)
76  {
77  delete *pNav;
78  }
79  fNavigators.clear();
80  fActiveNavigators.clear();
81  fWorlds.clear();
82 }
83 
84 void G4ITTransportationManager::Initialize()
85 {
86  // Create the navigator for tracking and activate it; add to collections
87  //
88  G4ITNavigator* trackingNavigator = new G4ITNavigator();
89  trackingNavigator->Activate(true);
90  G4Navigator* navForTracking =
93  G4VPhysicalVolume* world = navForTracking->GetWorldVolume();
94  trackingNavigator->SetWorldVolume(world);
95  fNavigators.push_back(trackingNavigator);
96  fActiveNavigators.push_back(trackingNavigator);
97  //fWorlds.push_back(world); // NULL registered
98 
100  ->GetNoWorlds();
101  std::vector<G4VPhysicalVolume*>::iterator it =
103 
104  for (size_t i = 0; i < n_worlds; i++, it++)
105  {
106  fWorlds.push_back(*it);
107  }
108  fpSafetyHelper = new G4ITSafetyHelper();
109 }
110 
112 {
113  if (fpInstance == 0) fpInstance = new G4ITTransportationManager;
114  return fpInstance;
115 }
116 
117 // ----------------------------------------------------------------------------
118 // GetParallelWorld()
119 //
120 // Provided the name of a world volume, returns the associated world pointer.
121 // If not existing, create (allocate) and register it in the collection.
122 //
125 {
126  G4VPhysicalVolume* wPV = IsWorldExisting(worldName);
127  if (!wPV)
128  {
129  wPV = GetNavigatorForTracking()->GetWorldVolume();
130  G4LogicalVolume* wLV = wPV->GetLogicalVolume();
131  wLV = new G4LogicalVolume(wLV->GetSolid(), 0, worldName);
132  wPV = new G4PVPlacement(wPV->GetRotation(), wPV->GetTranslation(), wLV,
133  worldName, 0, false, 0);
134  RegisterWorld(wPV);
135  }
136  return wPV;
137 }
138 
139 // ----------------------------------------------------------------------------
140 // GetNavigator()
141 //
142 // Provided the name of a world volume, returns the associated navigator.
143 // If not existing, create it and register it in the collection, throw an
144 // exception if the associated parallel world does not exist.
145 //
146 G4ITNavigator* G4ITTransportationManager::GetNavigator(const G4String& worldName)
147 {
148  // If already existing, return the stored pointer to the navigator
149  //
150  std::vector<G4ITNavigator*>::iterator pNav;
151  for (pNav = fNavigators.begin(); pNav != fNavigators.end(); pNav++)
152  {
153  if ((*pNav)->GetWorldVolume()->GetName() == worldName)
154  {
155  return *pNav;
156  }
157  }
158 
159  // Check if world of that name already exists,
160  // create a navigator and register it
161  //
162  G4ITNavigator* aNavigator = 0;
163  G4VPhysicalVolume* aWorld = IsWorldExisting(worldName);
164  if (aWorld)
165  {
166  aNavigator = new G4ITNavigator();
167  aNavigator->SetWorldVolume(aWorld);
168  fNavigators.push_back(aNavigator);
169  }
170  else
171  {
172  G4String message = "World volume with name -"
173  + worldName
174  + "- does not exist. Create it first by GetParallelWorld() method!";
175  G4Exception("G4ITTransportationManager::GetNavigator(name)", "GeomNav0002",
176  FatalException, message);
177  }
178 
179  return aNavigator;
180 }
181 
182 // ----------------------------------------------------------------------------
183 // GetNavigator()
184 //
185 // Provided a pointer to a world volume, returns the associated navigator.
186 // Create it in case not existing and add it to the collection.
187 // If world volume not existing, issue an exception.
188 //
190 {
191  std::vector<G4ITNavigator*>::iterator pNav;
192  for (pNav = fNavigators.begin(); pNav != fNavigators.end(); pNav++)
193  {
194  if ((*pNav)->GetWorldVolume() == aWorld)
195  {
196  return *pNav;
197  }
198  }
199  G4ITNavigator* aNavigator = 0;
200  std::vector<G4VPhysicalVolume*>::iterator pWorld = std::find(fWorlds.begin(),
201  fWorlds.end(),
202  aWorld);
203  if (pWorld != fWorlds.end())
204  {
205  aNavigator = new G4ITNavigator();
206  aNavigator->SetWorldVolume(aWorld);
207  fNavigators.push_back(aNavigator);
208  }
209  else
210  {
211  G4String message = "World volume with name -"
212  + aWorld->GetName()
213  + "- does not exist. Create it first by GetParallelWorld() method!";
214  G4Exception("G4ITTransportationManager::GetNavigator(pointer)",
215  "GeomNav0002", FatalException, message);
216  }
217 
218  return aNavigator;
219 }
220 
221 // ----------------------------------------------------------------------------
222 // DeRegisterNavigator()
223 //
224 // Provided a pointer to an already allocated navigator object, removes the
225 // associated entry in the navigators collection (remove pair) but does not
226 // delete the actual pointed object, which is still owned by the caller.
227 // The navigator for tracking -cannot- be deregistered.
228 //
229 void G4ITTransportationManager::DeRegisterNavigator(G4ITNavigator* aNavigator)
230 {
231  if (aNavigator == fNavigators[0])
232  {
233  G4Exception("G4ITTransportationManager::DeRegisterNavigator()",
234  "GeomNav0003", FatalException,
235  "The navigator for tracking CANNOT be deregistered!");
236  }
237  std::vector<G4ITNavigator*>::iterator pNav = std::find(fNavigators.begin(),
238  fNavigators.end(),
239  aNavigator);
240  if (pNav != fNavigators.end())
241  {
242  // Deregister associated world volume
243  //
244  DeRegisterWorld((*pNav)->GetWorldVolume());
245 
246  // Deregister the navigator
247  //
248  fNavigators.erase(pNav);
249  }
250  else
251  {
252  G4String message = "Navigator for volume -"
253  + aNavigator->GetWorldVolume()->GetName() + "- not found in memory!";
254  G4Exception("G4ITTransportationManager::DeRegisterNavigator()",
255  "GeomNav1002", JustWarning, message);
256  }
257 }
258 
259 // ----------------------------------------------------------------------------
260 // ActivateNavigator()
261 //
262 // Provided a pointer to an already allocated navigator object, set to 'true'
263 // the associated activation flag for the navigator in the collection.
264 // If the provided navigator is not already registered, issue a warning
265 // Return the index of the activated navigator. This index should be used for
266 // ComputeStep() method of G4PathFinder.
267 //
269 {
270  std::vector<G4ITNavigator*>::iterator pNav = std::find(fNavigators.begin(),
271  fNavigators.end(),
272  aNavigator);
273  if (pNav == fNavigators.end())
274  {
275  G4String message = "Navigator for volume -"
276  + aNavigator->GetWorldVolume()->GetName() + "- not found in memory!";
277  G4Exception("G4ITTransportationManager::ActivateNavigator()", "GeomNav1002",
278  JustWarning, message);
279  return -1;
280  }
281 
282  aNavigator->Activate(true);
283  G4int id = 0;
284  std::vector<G4ITNavigator*>::iterator pActiveNav;
285  for (pActiveNav = fActiveNavigators.begin();
286  pActiveNav != fActiveNavigators.end(); pActiveNav++)
287  {
288  if (*pActiveNav == aNavigator)
289  {
290  return id;
291  }
292  id++;
293  }
294 
295  fActiveNavigators.push_back(aNavigator);
296  return id;
297 }
298 
299 // ----------------------------------------------------------------------------
300 // DeActivateNavigator()
301 //
302 // Provided a pointer to an already allocated navigator object, set to 'false'
303 // the associated activation flag in the navigators collection.
304 // If the provided navigator is not already registered, issue a warning.
305 //
306 void G4ITTransportationManager::DeActivateNavigator(G4ITNavigator* aNavigator)
307 {
308  std::vector<G4ITNavigator*>::iterator pNav = std::find(fNavigators.begin(),
309  fNavigators.end(),
310  aNavigator);
311  if (pNav != fNavigators.end())
312  {
313  (*pNav)->Activate(false);
314  }
315  else
316  {
317  G4String message = "Navigator for volume -"
318  + aNavigator->GetWorldVolume()->GetName() + "- not found in memory!";
319  G4Exception("G4ITTransportationManager::DeActivateNavigator()",
320  "GeomNav1002", JustWarning, message);
321  }
322 
323  std::vector<G4ITNavigator*>::iterator pActiveNav = std::find(
324  fActiveNavigators.begin(), fActiveNavigators.end(), aNavigator);
325  if (pActiveNav != fActiveNavigators.end())
326  {
327  fActiveNavigators.erase(pActiveNav);
328  }
329 }
330 
331 // ----------------------------------------------------------------------------
332 // InactivateAll()
333 //
334 // Inactivate all the navigators except for the tracking one, and clear the
335 // store of active navigators.
336 //
338 {
339  std::vector<G4ITNavigator*>::iterator pNav;
340  for (pNav = fActiveNavigators.begin(); pNav != fActiveNavigators.end();
341  pNav++)
342  {
343  (*pNav)->Activate(false);
344  }
345  fActiveNavigators.clear();
346 
347  // Restore status for the navigator for tracking
348  //
349  fNavigators[0]->Activate(true);
350  fActiveNavigators.push_back(fNavigators[0]);
351 }
352 
353 // ----------------------------------------------------------------------------
354 // IsWorldExisting()
355 //
356 // Verify existance or not of an istance of the world volume with
357 // same name in the collection. Return the world pointer if existing.
358 //
361 {
362  std::vector<G4VPhysicalVolume*>::iterator pWorld = fWorlds.begin();
363  if (*pWorld == 0)
364  {
365  *pWorld = fNavigators[0]->GetWorldVolume();
366  }
367 
368  for (pWorld = fWorlds.begin(); pWorld != fWorlds.end(); pWorld++)
369  {
370  if ((*pWorld)->GetName() == name)
371  {
372  return *pWorld;
373  }
374  }
375  return 0;
376 }
377 
378 // ----------------------------------------------------------------------------
379 // RegisterWorld()
380 //
381 // Provided a pointer to an already allocated world object, check and add the
382 // associated entry in the worlds collection. Return 'true' if registration
383 // succeeds and the new entry is created.
384 //
386 {
387  G4bool done = false;
388 
389  std::vector<G4VPhysicalVolume*>::iterator pWorld = std::find(fWorlds.begin(),
390  fWorlds.end(),
391  aWorld);
392  if (pWorld == fWorlds.end())
393  {
394  fWorlds.push_back(aWorld);
395  done = true;
396  }
397  return done;
398 }
399 
400 // ----------------------------------------------------------------------------
401 // DeRegisterWorld()
402 //
403 // Provided a pointer to an already allocated world object, removes the
404 // associated entry in the worlds collection but does not delete the actual
405 // pointed object, which is still owned by the caller.
406 //
407 void G4ITTransportationManager::DeRegisterWorld(G4VPhysicalVolume* aWorld)
408 {
409  std::vector<G4VPhysicalVolume*>::iterator pWorld = std::find(fWorlds.begin(),
410  fWorlds.end(),
411  aWorld);
412  if (pWorld != fWorlds.end())
413  {
414  fWorlds.erase(pWorld);
415  }
416  else
417  {
418  G4String message = "World volume -" + aWorld->GetName()
419  + "- not found in memory!";
420  G4Exception("G4ITTransportationManager::DeRegisterWorld()", "GeomNav1002",
421  JustWarning, message);
422  }
423 }
const XML_Char * name
Definition: expat.h:151
std::vector< G4VPhysicalVolume * >::iterator GetWorldsIterator()
G4Navigator * GetNavigatorForTracking() const
G4VSolid * GetSolid() const
#define G4ThreadLocal
Definition: tls.hh:89
G4bool RegisterWorld(G4VPhysicalVolume *aWorld)
int G4int
Definition: G4Types.hh:78
static G4ITTransportationManager * GetTransportationManager()
G4VPhysicalVolume * IsWorldExisting(const G4String &worldName)
const G4String & GetName() const
bool G4bool
Definition: G4Types.hh:79
void DeRegisterNavigator(G4ITNavigator *aNavigator)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4TransportationManager * GetTransportationManager()
virtual void Initialize(G4HCofThisEvent *)
G4ITNavigator * GetNavigator(const G4String &worldName)
G4int ActivateNavigator(G4ITNavigator *aNavigator)
G4LogicalVolume * GetLogicalVolume() const
G4ITNavigator * GetNavigatorForTracking() const
size_t GetNoWorlds() const
G4VPhysicalVolume * GetParallelWorld(const G4String &worldName)
G4VPhysicalVolume * GetWorldVolume() const
void DeActivateNavigator(G4ITNavigator *aNavigator)