Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4TransportationManager.cc
이 파일의 문서화 페이지로 가기
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 103236 2017-03-22 15:54:40Z 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 
74  fFieldManager = new G4FieldManager(); // deleted by G4FieldManagerStore
77 }
78 
79 // ----------------------------------------------------------------------------
80 // Destructor
81 //
83 {
84  delete fSafetyHelper;
85  delete fPropagatorInField;
86  delete fGeomMessenger;
89 }
90 
91 // ----------------------------------------------------------------------------
92 // GetTransportationManager()
93 //
94 // Retrieve the static instance of the singleton and create it if not existing
95 //
97 {
99  {
101  }
102  return fTransportationManager;
103 }
104 
105 // ----------------------------------------------------------------------------
106 // GetInstanceIfExist()
107 //
108 // Retrieve the static instance pointer of the singleton
109 //
111 {
112  return fTransportationManager;
113 }
114 
115 // ----------------------------------------------------------------------------
116 // SetFieldManager()
117 //
118 // Set the associated field manager.
119 //
121 {
122  fFieldManager = newFieldManager;
123 
124  // Message the PropagatorInField,
125  // which also maintains this information (to be reviewed)
126  //
127  if( fPropagatorInField )
128  {
129  fPropagatorInField -> SetDetectorFieldManager( newFieldManager );
130  }
131 }
132 
133 // ----------------------------------------------------------------------------
134 // SetNavigatorForTracking()
135 //
136 // Set the active navigator for tracking, always
137 // the first in the collection of registered navigators.
138 //
140 {
141  fNavigators[0] = newNavigator;
142  fActiveNavigators[0] = newNavigator;
144 }
145 
146 // ----------------------------------------------------------------------------
147 // ClearNavigators()
148 //
149 // Clear collection of navigators and delete allocated objects.
150 // Called only by the class destructor.
151 //
153 {
154  std::vector<G4Navigator*>::iterator pNav;
155  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
156  {
157  delete *pNav;
158  }
159  fNavigators.clear();
160  fActiveNavigators.clear();
161  fWorlds.clear();
162 }
163 
164 // ----------------------------------------------------------------------------
165 // GetParallelWorld()
166 //
167 // Provided the name of a world volume, returns the associated world pointer.
168 // If not existing, create (allocate) and register it in the collection.
169 //
172 {
173  G4VPhysicalVolume* wPV = IsWorldExisting(worldName);
174  if (!wPV)
175  {
177  G4LogicalVolume* wLV = wPV->GetLogicalVolume();
178  wLV = new G4LogicalVolume(wLV->GetSolid(), 0,
179  worldName);
180  wPV = new G4PVPlacement (wPV->GetRotation(),
181  wPV->GetTranslation(),
182  wLV, worldName, 0, false, 0);
183  RegisterWorld(wPV);
184  }
185  return wPV;
186 }
187 
188 // ----------------------------------------------------------------------------
189 // GetNavigator()
190 //
191 // Provided the name of a world volume, returns the associated navigator.
192 // If not existing, create it and register it in the collection, throw an
193 // exception if the associated parallel world does not exist.
194 //
196 {
197  // If already existing, return the stored pointer to the navigator
198  //
199  std::vector<G4Navigator*>::iterator pNav;
200  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
201  {
202  if ((*pNav)->GetWorldVolume()->GetName() == worldName) { return *pNav; }
203  }
204 
205  // Check if world of that name already exists,
206  // create a navigator and register it
207  //
208  G4Navigator* aNavigator = 0;
209  G4VPhysicalVolume* aWorld = IsWorldExisting(worldName);
210  if(aWorld)
211  {
212  aNavigator = new G4Navigator();
213  aNavigator->SetWorldVolume(aWorld);
214  fNavigators.push_back(aNavigator);
215  }
216  else
217  {
219  = "World volume with name -" + worldName
220  + "- does not exist. Create it first by GetParallelWorld() method!";
221  G4Exception("G4TransportationManager::GetNavigator(name)",
222  "GeomNav0002", FatalException, message);
223  }
224 
225  return aNavigator;
226 }
227 
228 // ----------------------------------------------------------------------------
229 // GetNavigator()
230 //
231 // Provided a pointer to a world volume, returns the associated navigator.
232 // Create it in case not existing and add it to the collection.
233 // If world volume not existing, issue an exception.
234 //
236 {
237  std::vector<G4Navigator*>::iterator pNav;
238  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
239  {
240  if ((*pNav)->GetWorldVolume() == aWorld) { return *pNav; }
241  }
242  G4Navigator* aNavigator = 0;
243  std::vector<G4VPhysicalVolume*>::iterator pWorld =
244  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
245  if (pWorld != fWorlds.end())
246  {
247  aNavigator = new G4Navigator();
248  aNavigator->SetWorldVolume(aWorld);
249  fNavigators.push_back(aNavigator);
250  }
251  else
252  {
254  = "World volume with name -" + aWorld->GetName()
255  + "- does not exist. Create it first by GetParallelWorld() method!";
256  G4Exception("G4TransportationManager::GetNavigator(pointer)",
257  "GeomNav0002", FatalException, message);
258  }
259 
260  return aNavigator;
261 }
262 
263 // ----------------------------------------------------------------------------
264 // DeRegisterNavigator()
265 //
266 // Provided a pointer to an already allocated navigator object, removes the
267 // associated entry in the navigators collection (remove pair) but does not
268 // delete the actual pointed object, which is still owned by the caller.
269 // The navigator for tracking -cannot- be deregistered.
270 //
272 {
273  if (aNavigator == fNavigators[0])
274  {
275  G4Exception("G4TransportationManager::DeRegisterNavigator()",
276  "GeomNav0003", FatalException,
277  "The navigator for tracking CANNOT be deregistered!");
278  }
279  std::vector<G4Navigator*>::iterator pNav =
280  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
281  if (pNav != fNavigators.end())
282  {
283  // Deregister associated world volume
284  //
285  DeRegisterWorld((*pNav)->GetWorldVolume());
286 
287  // Deregister the navigator
288  //
289  fNavigators.erase(pNav);
290  }
291  else
292  {
294  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
295  + "- not found in memory!";
296  G4Exception("G4TransportationManager::DeRegisterNavigator()",
297  "GeomNav1002", JustWarning, message);
298  }
299 }
300 
301 // ----------------------------------------------------------------------------
302 // ActivateNavigator()
303 //
304 // Provided a pointer to an already allocated navigator object, set to 'true'
305 // the associated activation flag for the navigator in the collection.
306 // If the provided navigator is not already registered, issue a warning
307 // Return the index of the activated navigator. This index should be used for
308 // ComputeStep() method of G4PathFinder.
309 //
311 {
312  std::vector<G4Navigator*>::iterator pNav =
313  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
314  if (pNav == fNavigators.end())
315  {
317  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
318  + "- not found in memory!";
319  G4Exception("G4TransportationManager::ActivateNavigator()",
320  "GeomNav1002", FatalException, message);
321  return -1;
322  }
323 
324  aNavigator->Activate(true);
325  G4int id = 0;
326  std::vector<G4Navigator*>::iterator pActiveNav;
327  for(pActiveNav=fActiveNavigators.begin();
328  pActiveNav!=fActiveNavigators.end(); pActiveNav++)
329  {
330  if (*pActiveNav == aNavigator) { return id; }
331  id++;
332  }
333 
334  fActiveNavigators.push_back(aNavigator);
335  return id;
336 }
337 
338 // ----------------------------------------------------------------------------
339 // DeActivateNavigator()
340 //
341 // Provided a pointer to an already allocated navigator object, set to 'false'
342 // the associated activation flag in the navigators collection.
343 // If the provided navigator is not already registered, issue a warning.
344 //
346 {
347  std::vector<G4Navigator*>::iterator pNav =
348  std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
349  if (pNav != fNavigators.end())
350  {
351  (*pNav)->Activate(false);
352  }
353  else
354  {
356  = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
357  + "- not found in memory!";
358  G4Exception("G4TransportationManager::DeActivateNavigator()",
359  "GeomNav1002", JustWarning, message);
360  }
361 
362  std::vector<G4Navigator*>::iterator pActiveNav =
363  std::find(fActiveNavigators.begin(), fActiveNavigators.end(), aNavigator);
364  if (pActiveNav != fActiveNavigators.end())
365  {
366  fActiveNavigators.erase(pActiveNav);
367  }
368 }
369 
370 // ----------------------------------------------------------------------------
371 // InactivateAll()
372 //
373 // Inactivate all the navigators except for the tracking one, and clear the
374 // store of active navigators.
375 //
377 {
378  std::vector<G4Navigator*>::iterator pNav;
379  for (pNav=fActiveNavigators.begin(); pNav!=fActiveNavigators.end(); pNav++)
380  {
381  (*pNav)->Activate(false);
382  }
383  fActiveNavigators.clear();
384 
385  // Restore status for the navigator for tracking
386  //
387  fNavigators[0]->Activate(true);
388  fActiveNavigators.push_back(fNavigators[0]);
389 }
390 
391 // ----------------------------------------------------------------------------
392 // IsWorldExisting()
393 //
394 // Verify existance or not of an istance of the world volume with
395 // same name in the collection. Return the world pointer if existing.
396 //
399 {
400  std::vector<G4VPhysicalVolume*>::iterator pWorld = fWorlds.begin();
401  if (*pWorld==0) { *pWorld=fNavigators[0]->GetWorldVolume(); }
402 
403  for (pWorld=fWorlds.begin(); pWorld!=fWorlds.end(); pWorld++)
404  {
405  if ((*pWorld)->GetName() == name ) { return *pWorld; }
406  }
407  return 0;
408 }
409 
410 // ----------------------------------------------------------------------------
411 // RegisterWorld()
412 //
413 // Provided a pointer to an already allocated world object, check and add the
414 // associated entry in the worlds collection. Return 'true' if registration
415 // succeeds and the new entry is created.
416 //
418 {
419  G4bool done = false;
420 
421  std::vector<G4VPhysicalVolume*>::iterator pWorld =
422  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
423  if (pWorld == fWorlds.end())
424  {
425  fWorlds.push_back(aWorld);
426  done = true;
427  }
428  return done;
429 }
430 
431 // ----------------------------------------------------------------------------
432 // DeRegisterWorld()
433 //
434 // Provided a pointer to an already allocated world object, removes the
435 // associated entry in the worlds collection but does not delete the actual
436 // pointed object, which is still owned by the caller.
437 //
439 {
440  std::vector<G4VPhysicalVolume*>::iterator pWorld =
441  std::find(fWorlds.begin(), fWorlds.end(), aWorld);
442  if (pWorld != fWorlds.end())
443  {
444  fWorlds.erase(pWorld);
445  }
446  else
447  {
449  = "World volume -" + aWorld->GetName() + "- not found in memory!";
450  G4Exception("G4TransportationManager::DeRegisterWorld()",
451  "GeomNav1002", JustWarning, message);
452  }
453 }
454 
455 // ----------------------------------------------------------------------------
456 // ClearParallelWorlds()
457 //
458 // Clear collection of navigators and delete allocated objects associated with
459 // parallel worlds.
460 // Called only by the RunManager when the entire geometry is rebuilt from
461 // scratch.
462 //
464 {
465  std::vector<G4Navigator*>::iterator pNav = fNavigators.begin();
466  G4Navigator* trackingNavigator = *pNav;
467  for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
468  {
469  if (*pNav != trackingNavigator) { delete *pNav; }
470  }
471  fNavigators.clear();
472  fActiveNavigators.clear();
473  fWorlds.clear();
474 
475  // trackingNavigator->SetWorldVolume(0);
476  fNavigators.push_back(trackingNavigator);
477  fActiveNavigators.push_back(trackingNavigator);
478  // fWorlds.push_back(trackingNavigator->GetWorldVolume()); // NULL registered
479  fWorlds.push_back(0); // NULL registered
480 }
481 
const XML_Char * name
Definition: expat.h:151
G4LogicalVolume * GetLogicalVolume() const
static G4TransportationManager * GetInstanceIfExist()
void message(RunManager *runmanager)
Definition: ts_scorers.cc:72
G4Navigator * GetNavigatorForTracking() const
G4VPhysicalVolume * GetParallelWorld(const G4String &worldName)
#define G4ThreadLocal
Definition: tls.hh:69
G4GeometryMessenger * fGeomMessenger
void SetFieldManager(G4FieldManager *newFieldManager)
void DeRegisterWorld(G4VPhysicalVolume *aWorld)
G4VPhysicalVolume * IsWorldExisting(const G4String &worldName)
void SetNavigatorForPropagating(G4Navigator *SimpleOrMultiNavigator)
bool G4bool
Definition: G4Types.hh:79
G4bool RegisterWorld(G4VPhysicalVolume *aWorld)
void SetWorldVolume(G4VPhysicalVolume *pWorld)
std::vector< G4Navigator * > fActiveNavigators
static G4TransportationManager * GetTransportationManager()
G4Navigator * GetNavigator(const G4String &worldName)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
int G4int
Definition: G4Types.hh:78
G4int ActivateNavigator(G4Navigator *aNavigator)
std::vector< G4VPhysicalVolume * > fWorlds
G4VSolid * GetSolid() const
G4PropagatorInField * fPropagatorInField
void SetNavigatorForTracking(G4Navigator *newNavigator)
void Activate(G4bool flag)
void DeRegisterNavigator(G4Navigator *aNavigator)
G4VPhysicalVolume * GetWorldVolume() const
void DeActivateNavigator(G4Navigator *aNavigator)
static G4ThreadLocal G4TransportationManager * fTransportationManager
const G4String & GetName() const
std::vector< G4Navigator * > fNavigators