Geant4_10
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 68709 2013-04-05 09:03:07Z 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 //
51 G4TransportationManager::fTransportationManager=0;
52 
53 // ----------------------------------------------------------------------------
54 // Constructor
55 //
57 {
58  if (fTransportationManager)
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 
73  fGeomMessenger = new G4GeometryMessenger(this);
74  fFieldManager = new G4FieldManager();
75  fPropagatorInField= new G4PropagatorInField(trackingNavigator,fFieldManager);
76  fSafetyHelper = new G4SafetyHelper();
77 }
78 
79 // ----------------------------------------------------------------------------
80 // Destructor
81 //
83 {
84  delete fFieldManager;
85  delete fPropagatorInField;
86  ClearNavigators();
87  delete fGeomMessenger;
88  delete fSafetyHelper;
89  if (fTransportationManager)
90  {
91  delete fTransportationManager; fTransportationManager=0;
92  }
93 }
94 
95 // ----------------------------------------------------------------------------
96 // GetTransportationManager()
97 //
98 // Retrieve the static instance of the singleton
99 //
101 {
102  if (!fTransportationManager)
103  {
104  fTransportationManager = new G4TransportationManager;
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 // ClearNavigators()
129 //
130 // Clear collection of navigators and delete allocated objects.
131 // Called only by the class destructor.
132 //
133 void G4TransportationManager::ClearNavigators()
134 {
135  std::vector<G4Navigator*>::iterator pNav;
136  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
137  {
138  delete *pNav;
139  }
140  fNavigators.clear();
141  fActiveNavigators.clear();
142  fWorlds.clear();
143 }
144 
145 // ----------------------------------------------------------------------------
146 // GetParallelWorld()
147 //
148 // Provided the name of a world volume, returns the associated world pointer.
149 // If not existing, create (allocate) and register it in the collection.
150 //
153 {
154  G4VPhysicalVolume* wPV = IsWorldExisting(worldName);
155  if (!wPV)
156  {
158  G4LogicalVolume* wLV = wPV->GetLogicalVolume();
159  wLV = new G4LogicalVolume(wLV->GetSolid(), 0,
160  worldName);
161  wPV = new G4PVPlacement (wPV->GetRotation(),
162  wPV->GetTranslation(),
163  wLV, worldName, 0, false, 0);
164  RegisterWorld(wPV);
165  }
166  return wPV;
167 }
168 
169 // ----------------------------------------------------------------------------
170 // GetNavigator()
171 //
172 // Provided the name of a world volume, returns the associated navigator.
173 // If not existing, create it and register it in the collection, throw an
174 // exception if the associated parallel world does not exist.
175 //
177 {
178  // If already existing, return the stored pointer to the navigator
179  //
180  std::vector<G4Navigator*>::iterator pNav;
181  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
182  {
183  if ((*pNav)->GetWorldVolume()->GetName() == worldName) { return *pNav; }
184  }
185 
186  // Check if world of that name already exists,
187  // create a navigator and register it
188  //
189  G4Navigator* aNavigator = 0;
190  G4VPhysicalVolume* aWorld = IsWorldExisting(worldName);
191  if(aWorld)
192  {
193  aNavigator = new G4Navigator();
194  aNavigator->SetWorldVolume(aWorld);
195  fNavigators.push_back(aNavigator);
196  }
197  else
198  {
199  G4String message
200  = "World volume with name -" + worldName
201  + "- does not exist. Create it first by GetParallelWorld() method!";
202  G4Exception("G4TransportationManager::GetNavigator(name)",
203  "GeomNav0002", FatalException, message);
204  }
205 
206  return aNavigator;
207 }
208 
209 // ----------------------------------------------------------------------------
210 // GetNavigator()
211 //
212 // Provided a pointer to a world volume, returns the associated navigator.
213 // Create it in case not existing and add it to the collection.
214 // If world volume not existing, issue an exception.
215 //
217 {
218  std::vector<G4Navigator*>::iterator pNav;
219  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
220  {
221  if ((*pNav)->GetWorldVolume() == aWorld) { return *pNav; }
222  }
223  G4Navigator* aNavigator = 0;
224  std::vector<G4VPhysicalVolume*>::iterator pWorld =
225  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
226  if (pWorld != fWorlds.end())
227  {
228  aNavigator = new G4Navigator();
229  aNavigator->SetWorldVolume(aWorld);
230  fNavigators.push_back(aNavigator);
231  }
232  else
233  {
234  G4String message
235  = "World volume with name -" + aWorld->GetName()
236  + "- does not exist. Create it first by GetParallelWorld() method!";
237  G4Exception("G4TransportationManager::GetNavigator(pointer)",
238  "GeomNav0002", FatalException, message);
239  }
240 
241  return aNavigator;
242 }
243 
244 // ----------------------------------------------------------------------------
245 // DeRegisterNavigator()
246 //
247 // Provided a pointer to an already allocated navigator object, removes the
248 // associated entry in the navigators collection (remove pair) but does not
249 // delete the actual pointed object, which is still owned by the caller.
250 // The navigator for tracking -cannot- be deregistered.
251 //
253 {
254  if (aNavigator == fNavigators[0])
255  {
256  G4Exception("G4TransportationManager::DeRegisterNavigator()",
257  "GeomNav0003", FatalException,
258  "The navigator for tracking CANNOT be deregistered!");
259  }
260  std::vector<G4Navigator*>::iterator pNav =
261  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
262  if (pNav != fNavigators.end())
263  {
264  // Deregister associated world volume
265  //
266  DeRegisterWorld((*pNav)->GetWorldVolume());
267 
268  // Deregister the navigator
269  //
270  fNavigators.erase(pNav);
271  }
272  else
273  {
274  G4String message
275  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
276  + "- not found in memory!";
277  G4Exception("G4TransportationManager::DeRegisterNavigator()",
278  "GeomNav1002", JustWarning, message);
279  }
280 }
281 
282 // ----------------------------------------------------------------------------
283 // ActivateNavigator()
284 //
285 // Provided a pointer to an already allocated navigator object, set to 'true'
286 // the associated activation flag for the navigator in the collection.
287 // If the provided navigator is not already registered, issue a warning
288 // Return the index of the activated navigator. This index should be used for
289 // ComputeStep() method of G4PathFinder.
290 //
292 {
293  std::vector<G4Navigator*>::iterator pNav =
294  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
295  if (pNav == fNavigators.end())
296  {
297  G4String message
298  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
299  + "- not found in memory!";
300  G4Exception("G4TransportationManager::ActivateNavigator()",
301  "GeomNav1002", JustWarning, message);
302  return -1;
303  }
304 
305  aNavigator->Activate(true);
306  G4int id = 0;
307  std::vector<G4Navigator*>::iterator pActiveNav;
308  for(pActiveNav=fActiveNavigators.begin();
309  pActiveNav!=fActiveNavigators.end(); pActiveNav++)
310  {
311  if (*pActiveNav == aNavigator) { return id; }
312  id++;
313  }
314 
315  fActiveNavigators.push_back(aNavigator);
316  return id;
317 }
318 
319 // ----------------------------------------------------------------------------
320 // DeActivateNavigator()
321 //
322 // Provided a pointer to an already allocated navigator object, set to 'false'
323 // the associated activation flag in the navigators collection.
324 // If the provided navigator is not already registered, issue a warning.
325 //
327 {
328  std::vector<G4Navigator*>::iterator pNav =
329  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
330  if (pNav != fNavigators.end())
331  {
332  (*pNav)->Activate(false);
333  }
334  else
335  {
336  G4String message
337  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
338  + "- not found in memory!";
339  G4Exception("G4TransportationManager::DeActivateNavigator()",
340  "GeomNav1002", JustWarning, message);
341  }
342 
343  std::vector<G4Navigator*>::iterator pActiveNav =
344  std::find(fActiveNavigators.begin(), fActiveNavigators.end(), aNavigator);
345  if (pActiveNav != fActiveNavigators.end())
346  {
347  fActiveNavigators.erase(pActiveNav);
348  }
349 }
350 
351 // ----------------------------------------------------------------------------
352 // InactivateAll()
353 //
354 // Inactivate all the navigators except for the tracking one, and clear the
355 // store of active navigators.
356 //
358 {
359  std::vector<G4Navigator*>::iterator pNav;
360  for (pNav=fActiveNavigators.begin(); pNav!=fActiveNavigators.end(); pNav++)
361  {
362  (*pNav)->Activate(false);
363  }
364  fActiveNavigators.clear();
365 
366  // Restore status for the navigator for tracking
367  //
368  fNavigators[0]->Activate(true);
369  fActiveNavigators.push_back(fNavigators[0]);
370 }
371 
372 // ----------------------------------------------------------------------------
373 // IsWorldExisting()
374 //
375 // Verify existance or not of an istance of the world volume with
376 // same name in the collection. Return the world pointer if existing.
377 //
380 {
381  std::vector<G4VPhysicalVolume*>::iterator pWorld = fWorlds.begin();
382  if (*pWorld==0) { *pWorld=fNavigators[0]->GetWorldVolume(); }
383 
384  for (pWorld=fWorlds.begin(); pWorld!=fWorlds.end(); pWorld++)
385  {
386  if ((*pWorld)->GetName() == name ) { return *pWorld; }
387  }
388  return 0;
389 }
390 
391 // ----------------------------------------------------------------------------
392 // RegisterWorld()
393 //
394 // Provided a pointer to an already allocated world object, check and add the
395 // associated entry in the worlds collection. Return 'true' if registration
396 // succeeds and the new entry is created.
397 //
399 {
400  G4bool done = false;
401 
402  std::vector<G4VPhysicalVolume*>::iterator pWorld =
403  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
404  if (pWorld == fWorlds.end())
405  {
406  fWorlds.push_back(aWorld);
407  done = true;
408  }
409  return done;
410 }
411 
412 // ----------------------------------------------------------------------------
413 // DeRegisterWorld()
414 //
415 // Provided a pointer to an already allocated world object, removes the
416 // associated entry in the worlds collection but does not delete the actual
417 // pointed object, which is still owned by the caller.
418 //
419 void G4TransportationManager::DeRegisterWorld( G4VPhysicalVolume* aWorld )
420 {
421  std::vector<G4VPhysicalVolume*>::iterator pWorld =
422  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
423  if (pWorld != fWorlds.end())
424  {
425  fWorlds.erase(pWorld);
426  }
427  else
428  {
429  G4String message
430  = "World volume -" + aWorld->GetName() + "- not found in memory!";
431  G4Exception("G4TransportationManager::DeRegisterWorld()",
432  "GeomNav1002", JustWarning, message);
433  }
434 }
G4VPhysicalVolume * IsWorldExisting(const G4String &worldName)
G4Navigator * GetNavigatorForTracking() const
void SetFieldManager(G4FieldManager *newFieldManager)
const XML_Char * name
Definition: expat.h:151
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
const G4String & GetName() const
G4FieldManager(G4Field *detectorField=0, G4ChordFinder *pChordFinder=0, G4bool b=true)
bool G4bool
Definition: G4Types.hh:79
void DeActivateNavigator(G4Navigator *aNavigator)
void Activate(G4bool flag)
void DeRegisterNavigator(G4Navigator *aNavigator)
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)
G4bool RegisterWorld(G4VPhysicalVolume *aWorld)
G4VPhysicalVolume * GetWorldVolume() const
G4VSolid * GetSolid() const