Geant4  10.01.p03
G4TransportationManager.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 //
27 // $Id: G4TransportationManager.cc 83466 2014-08-25 10:31:39Z gcosmo $
28 //
29 //
30 // G4TransportationManager
31 //
32 // Created : J.Apostolakis, 1997
33 // Reviewed: G.Cosmo, 2006
34 // 10.04.07 V.Ivanchenko Use unique G4SafetyHelper
35 //
36 // --------------------------------------------------------------------
37 
39 
40 #include <algorithm>
41 
42 #include "G4GeometryMessenger.hh"
43 #include "G4PropagatorInField.hh"
44 #include "G4FieldManager.hh"
45 #include "G4LogicalVolume.hh"
46 #include "G4PVPlacement.hh"
47 
48 // Initialise the static instance of the singleton
49 //
52 
53 // ----------------------------------------------------------------------------
54 // Constructor
55 //
57 {
59  {
60  G4Exception("G4TransportationManager::G4TransportationManager()",
61  "GeomNav0002", FatalException,
62  "Only ONE instance of G4TransportationManager is allowed!");
63  }
64 
65  // Create the navigator for tracking and activate it; add to collections
66  //
67  G4Navigator* trackingNavigator = new G4Navigator();
68  trackingNavigator->Activate(true);
69  fNavigators.push_back(trackingNavigator);
70  fActiveNavigators.push_back(trackingNavigator);
71  fWorlds.push_back(trackingNavigator->GetWorldVolume()); // NULL registered
72 
77 }
78 
79 // ----------------------------------------------------------------------------
80 // Destructor
81 //
83 {
84  delete fFieldManager;
85  delete fPropagatorInField;
86  ClearNavigators();
87  delete fGeomMessenger;
88  delete fSafetyHelper;
90  {
92  }
93 }
94 
95 // ----------------------------------------------------------------------------
96 // GetTransportationManager()
97 //
98 // Retrieve the static instance of the singleton
99 //
101 {
103  {
105  }
106  return fTransportationManager;
107 }
108 
109 // ----------------------------------------------------------------------------
110 // SetFieldManager()
111 //
112 // Set the associated field manager.
113 //
115 {
116  fFieldManager = newFieldManager;
117 
118  // Message the PropagatorInField,
119  // which also maintains this information (to be reviewed)
120  //
121  if( fPropagatorInField )
122  {
123  fPropagatorInField -> SetDetectorFieldManager( newFieldManager );
124  }
125 }
126 
127 // ----------------------------------------------------------------------------
128 // SetNavigatorForTracking()
129 //
130 // Set the active navigator for tracking, always
131 // the first in the collection of registered navigators.
132 //
134 {
135  fNavigators[0] = newNavigator;
136  fActiveNavigators[0] = newNavigator;
138 }
139 
140 // ----------------------------------------------------------------------------
141 // ClearNavigators()
142 //
143 // Clear collection of navigators and delete allocated objects.
144 // Called only by the class destructor.
145 //
147 {
148  std::vector<G4Navigator*>::iterator pNav;
149  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
150  {
151  delete *pNav;
152  }
153  fNavigators.clear();
154  fActiveNavigators.clear();
155  fWorlds.clear();
156 }
157 
158 // ----------------------------------------------------------------------------
159 // GetParallelWorld()
160 //
161 // Provided the name of a world volume, returns the associated world pointer.
162 // If not existing, create (allocate) and register it in the collection.
163 //
166 {
167  G4VPhysicalVolume* wPV = IsWorldExisting(worldName);
168  if (!wPV)
169  {
171  G4LogicalVolume* wLV = wPV->GetLogicalVolume();
172  wLV = new G4LogicalVolume(wLV->GetSolid(), 0,
173  worldName);
174  wPV = new G4PVPlacement (wPV->GetRotation(),
175  wPV->GetTranslation(),
176  wLV, worldName, 0, false, 0);
177  RegisterWorld(wPV);
178  }
179  return wPV;
180 }
181 
182 // ----------------------------------------------------------------------------
183 // GetNavigator()
184 //
185 // Provided the name of a world volume, returns the associated navigator.
186 // If not existing, create it and register it in the collection, throw an
187 // exception if the associated parallel world does not exist.
188 //
190 {
191  // If already existing, return the stored pointer to the navigator
192  //
193  std::vector<G4Navigator*>::iterator pNav;
194  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
195  {
196  if ((*pNav)->GetWorldVolume()->GetName() == worldName) { return *pNav; }
197  }
198 
199  // Check if world of that name already exists,
200  // create a navigator and register it
201  //
202  G4Navigator* aNavigator = 0;
203  G4VPhysicalVolume* aWorld = IsWorldExisting(worldName);
204  if(aWorld)
205  {
206  aNavigator = new G4Navigator();
207  aNavigator->SetWorldVolume(aWorld);
208  fNavigators.push_back(aNavigator);
209  }
210  else
211  {
212  G4String message
213  = "World volume with name -" + worldName
214  + "- does not exist. Create it first by GetParallelWorld() method!";
215  G4Exception("G4TransportationManager::GetNavigator(name)",
216  "GeomNav0002", FatalException, message);
217  }
218 
219  return aNavigator;
220 }
221 
222 // ----------------------------------------------------------------------------
223 // GetNavigator()
224 //
225 // Provided a pointer to a world volume, returns the associated navigator.
226 // Create it in case not existing and add it to the collection.
227 // If world volume not existing, issue an exception.
228 //
230 {
231  std::vector<G4Navigator*>::iterator pNav;
232  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
233  {
234  if ((*pNav)->GetWorldVolume() == aWorld) { return *pNav; }
235  }
236  G4Navigator* aNavigator = 0;
237  std::vector<G4VPhysicalVolume*>::iterator pWorld =
238  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
239  if (pWorld != fWorlds.end())
240  {
241  aNavigator = new G4Navigator();
242  aNavigator->SetWorldVolume(aWorld);
243  fNavigators.push_back(aNavigator);
244  }
245  else
246  {
247  G4String message
248  = "World volume with name -" + aWorld->GetName()
249  + "- does not exist. Create it first by GetParallelWorld() method!";
250  G4Exception("G4TransportationManager::GetNavigator(pointer)",
251  "GeomNav0002", FatalException, message);
252  }
253 
254  return aNavigator;
255 }
256 
257 // ----------------------------------------------------------------------------
258 // DeRegisterNavigator()
259 //
260 // Provided a pointer to an already allocated navigator object, removes the
261 // associated entry in the navigators collection (remove pair) but does not
262 // delete the actual pointed object, which is still owned by the caller.
263 // The navigator for tracking -cannot- be deregistered.
264 //
266 {
267  if (aNavigator == fNavigators[0])
268  {
269  G4Exception("G4TransportationManager::DeRegisterNavigator()",
270  "GeomNav0003", FatalException,
271  "The navigator for tracking CANNOT be deregistered!");
272  }
273  std::vector<G4Navigator*>::iterator pNav =
274  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
275  if (pNav != fNavigators.end())
276  {
277  // Deregister associated world volume
278  //
279  DeRegisterWorld((*pNav)->GetWorldVolume());
280 
281  // Deregister the navigator
282  //
283  fNavigators.erase(pNav);
284  }
285  else
286  {
287  G4String message
288  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
289  + "- not found in memory!";
290  G4Exception("G4TransportationManager::DeRegisterNavigator()",
291  "GeomNav1002", JustWarning, message);
292  }
293 }
294 
295 // ----------------------------------------------------------------------------
296 // ActivateNavigator()
297 //
298 // Provided a pointer to an already allocated navigator object, set to 'true'
299 // the associated activation flag for the navigator in the collection.
300 // If the provided navigator is not already registered, issue a warning
301 // Return the index of the activated navigator. This index should be used for
302 // ComputeStep() method of G4PathFinder.
303 //
305 {
306  std::vector<G4Navigator*>::iterator pNav =
307  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
308  if (pNav == fNavigators.end())
309  {
310  G4String message
311  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
312  + "- not found in memory!";
313  G4Exception("G4TransportationManager::ActivateNavigator()",
314  "GeomNav1002", FatalException, message);
315  return -1;
316  }
317 
318  aNavigator->Activate(true);
319  G4int id = 0;
320  std::vector<G4Navigator*>::iterator pActiveNav;
321  for(pActiveNav=fActiveNavigators.begin();
322  pActiveNav!=fActiveNavigators.end(); pActiveNav++)
323  {
324  if (*pActiveNav == aNavigator) { return id; }
325  id++;
326  }
327 
328  fActiveNavigators.push_back(aNavigator);
329  return id;
330 }
331 
332 // ----------------------------------------------------------------------------
333 // DeActivateNavigator()
334 //
335 // Provided a pointer to an already allocated navigator object, set to 'false'
336 // the associated activation flag in the navigators collection.
337 // If the provided navigator is not already registered, issue a warning.
338 //
340 {
341  std::vector<G4Navigator*>::iterator pNav =
342  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
343  if (pNav != fNavigators.end())
344  {
345  (*pNav)->Activate(false);
346  }
347  else
348  {
349  G4String message
350  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
351  + "- not found in memory!";
352  G4Exception("G4TransportationManager::DeActivateNavigator()",
353  "GeomNav1002", JustWarning, message);
354  }
355 
356  std::vector<G4Navigator*>::iterator pActiveNav =
357  std::find(fActiveNavigators.begin(), fActiveNavigators.end(), aNavigator);
358  if (pActiveNav != fActiveNavigators.end())
359  {
360  fActiveNavigators.erase(pActiveNav);
361  }
362 }
363 
364 // ----------------------------------------------------------------------------
365 // InactivateAll()
366 //
367 // Inactivate all the navigators except for the tracking one, and clear the
368 // store of active navigators.
369 //
371 {
372  std::vector<G4Navigator*>::iterator pNav;
373  for (pNav=fActiveNavigators.begin(); pNav!=fActiveNavigators.end(); pNav++)
374  {
375  (*pNav)->Activate(false);
376  }
377  fActiveNavigators.clear();
378 
379  // Restore status for the navigator for tracking
380  //
381  fNavigators[0]->Activate(true);
382  fActiveNavigators.push_back(fNavigators[0]);
383 }
384 
385 // ----------------------------------------------------------------------------
386 // IsWorldExisting()
387 //
388 // Verify existance or not of an istance of the world volume with
389 // same name in the collection. Return the world pointer if existing.
390 //
393 {
394  std::vector<G4VPhysicalVolume*>::iterator pWorld = fWorlds.begin();
395  if (*pWorld==0) { *pWorld=fNavigators[0]->GetWorldVolume(); }
396 
397  for (pWorld=fWorlds.begin(); pWorld!=fWorlds.end(); pWorld++)
398  {
399  if ((*pWorld)->GetName() == name ) { return *pWorld; }
400  }
401  return 0;
402 }
403 
404 // ----------------------------------------------------------------------------
405 // RegisterWorld()
406 //
407 // Provided a pointer to an already allocated world object, check and add the
408 // associated entry in the worlds collection. Return 'true' if registration
409 // succeeds and the new entry is created.
410 //
412 {
413  G4bool done = false;
414 
415  std::vector<G4VPhysicalVolume*>::iterator pWorld =
416  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
417  if (pWorld == fWorlds.end())
418  {
419  fWorlds.push_back(aWorld);
420  done = true;
421  }
422  return done;
423 }
424 
425 // ----------------------------------------------------------------------------
426 // DeRegisterWorld()
427 //
428 // Provided a pointer to an already allocated world object, removes the
429 // associated entry in the worlds collection but does not delete the actual
430 // pointed object, which is still owned by the caller.
431 //
433 {
434  std::vector<G4VPhysicalVolume*>::iterator pWorld =
435  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
436  if (pWorld != fWorlds.end())
437  {
438  fWorlds.erase(pWorld);
439  }
440  else
441  {
442  G4String message
443  = "World volume -" + aWorld->GetName() + "- not found in memory!";
444  G4Exception("G4TransportationManager::DeRegisterWorld()",
445  "GeomNav1002", JustWarning, message);
446  }
447 }
448 
449 // ----------------------------------------------------------------------------
450 // ClearParallelWorlds()
451 //
452 // Clear collection of navigators and delete allocated objects associated with
453 // parallel worlds.
454 // Called only by the RunManager when the entire geometry is rebuilt from
455 // scratch.
456 //
458 {
459  std::vector<G4Navigator*>::iterator pNav = fNavigators.begin();
460  G4Navigator* trackingNavigator = *pNav;
461  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
462  {
463  if (*pNav != trackingNavigator) { delete *pNav; }
464  }
465  fNavigators.clear();
466  fActiveNavigators.clear();
467  fWorlds.clear();
468 
469  // trackingNavigator->SetWorldVolume(0);
470  fNavigators.push_back(trackingNavigator);
471  fActiveNavigators.push_back(trackingNavigator);
472  // fWorlds.push_back(trackingNavigator->GetWorldVolume()); // NULL registered
473  fWorlds.push_back(0); // NULL registered
474 }
475 
G4VPhysicalVolume * IsWorldExisting(const G4String &worldName)
void SetNavigatorForPropagating(G4Navigator *SimpleOrMultiNavigator)
G4String name
Definition: TRTMaterials.hh:40
G4Navigator * GetNavigatorForTracking() const
void SetFieldManager(G4FieldManager *newFieldManager)
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
std::vector< G4VPhysicalVolume * > fWorlds
G4PropagatorInField * fPropagatorInField
std::vector< G4Navigator * > fActiveNavigators
const G4String & GetName() const
bool G4bool
Definition: G4Types.hh:79
void DeActivateNavigator(G4Navigator *aNavigator)
void Activate(G4bool flag)
void DeRegisterNavigator(G4Navigator *aNavigator)
G4GeometryMessenger * fGeomMessenger
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4TransportationManager * GetTransportationManager()
G4VPhysicalVolume * GetParallelWorld(const G4String &worldName)
G4int ActivateNavigator(G4Navigator *aNavigator)
G4LogicalVolume * GetLogicalVolume() const
G4Navigator * GetNavigator(const G4String &worldName)
void SetWorldVolume(G4VPhysicalVolume *pWorld)
static G4ThreadLocal G4TransportationManager * fTransportationManager
void DeRegisterWorld(G4VPhysicalVolume *aWorld)
std::vector< G4Navigator * > fNavigators
G4bool RegisterWorld(G4VPhysicalVolume *aWorld)
void SetNavigatorForTracking(G4Navigator *newNavigator)
G4VPhysicalVolume * GetWorldVolume() const
G4VSolid * GetSolid() const