source: trunk/source/geometry/navigation/src/G4TransportationManager.cc@ 1226

Last change on this file since 1226 was 921, checked in by garnier, 17 years ago

en test de gl2ps. Problemes de libraries

File size: 14.6 KB
Line 
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,v 1.15 2007/04/12 11:51:48 vnivanch Exp $
28// GEANT4 tag $Name: geant4-09-02-cand-01 $
29//
30//
31// G4TransportationManager
32//
33// Created : J.Apostolakis, 1997
34// Reviewed: G.Cosmo, 2006
35// 10.04.07 V.Ivanchenko Use unique G4SafetyHelper
36//
37// --------------------------------------------------------------------
38
39#include "G4TransportationManager.hh"
40
41#include <algorithm>
42
43#include "G4GeometryMessenger.hh"
44#include "G4PropagatorInField.hh"
45#include "G4FieldManager.hh"
46#include "G4LogicalVolume.hh"
47#include "G4PVPlacement.hh"
48
49// Initialise the static instance of the singleton
50//
51G4TransportationManager* G4TransportationManager::fTransportationManager=0;
52
53// ----------------------------------------------------------------------------
54// Constructor
55//
56G4TransportationManager::G4TransportationManager()
57{
58 if (!fTransportationManager)
59 {
60 // Create the navigator for tracking and activate it; add to collections
61 //
62 G4Navigator* trackingNavigator = new G4Navigator();
63 trackingNavigator->Activate(true);
64 fNavigators.push_back(trackingNavigator);
65 fActiveNavigators.push_back(trackingNavigator);
66 fWorlds.push_back(trackingNavigator->GetWorldVolume()); // NULL registered
67
68 fGeomMessenger = new G4GeometryMessenger(this);
69 fFieldManager = new G4FieldManager();
70 fPropagatorInField = new G4PropagatorInField(trackingNavigator,
71 fFieldManager);
72 fSafetyHelper = new G4SafetyHelper();
73 }
74 else
75 {
76 G4cerr << "Only ONE instance of G4TransportationManager is allowed!"
77 << G4endl;
78 G4Exception("G4TransportationManager::G4TransportationManager()",
79 "InvalidSetup", FatalException,
80 "Only ONE instance of G4TransportationManager is allowed!");
81 }
82}
83
84// ----------------------------------------------------------------------------
85// Destructor
86//
87G4TransportationManager::~G4TransportationManager()
88{
89 delete fFieldManager;
90 delete fPropagatorInField;
91 ClearNavigators();
92 delete fGeomMessenger;
93 delete fSafetyHelper;
94}
95
96// ----------------------------------------------------------------------------
97// GetTransportationManager()
98//
99// Retrieve the static instance of the singleton
100//
101G4TransportationManager* G4TransportationManager::GetTransportationManager()
102{
103 static G4TransportationManager theInstance;
104 if (!fTransportationManager)
105 fTransportationManager = &theInstance;
106
107 return fTransportationManager;
108}
109
110// ----------------------------------------------------------------------------
111// SetFieldManager()
112//
113// Set the associated field manager.
114//
115void G4TransportationManager::SetFieldManager(G4FieldManager* newFieldManager)
116{
117 fFieldManager = newFieldManager;
118
119 // Message the PropagatorInField,
120 // which also maintains this information (to be reviewed)
121 //
122 if( fPropagatorInField )
123 {
124 fPropagatorInField -> SetDetectorFieldManager( newFieldManager );
125 }
126}
127
128// ----------------------------------------------------------------------------
129// ClearNavigators()
130//
131// Clear collection of navigators and delete allocated objects.
132// Called only by the class destructor.
133//
134void G4TransportationManager::ClearNavigators()
135{
136 std::vector<G4Navigator*>::iterator pNav;
137 for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
138 {
139 delete *pNav;
140 }
141 fNavigators.clear();
142 fActiveNavigators.clear();
143 fWorlds.clear();
144}
145
146// ----------------------------------------------------------------------------
147// GetParallelWorld()
148//
149// Provided the name of a world volume, returns the associated world pointer.
150// If not existing, create (allocate) and register it in the collection.
151//
152G4VPhysicalVolume*
153G4TransportationManager::GetParallelWorld( const G4String& worldName )
154{
155 G4VPhysicalVolume* wPV = IsWorldExisting(worldName);
156 if (!wPV)
157 {
158 wPV = GetNavigatorForTracking()->GetWorldVolume();
159 G4LogicalVolume* wLV = wPV->GetLogicalVolume();
160 wLV = new G4LogicalVolume(wLV->GetSolid(), 0,
161 worldName);
162 wPV = new G4PVPlacement (wPV->GetRotation(),
163 wPV->GetTranslation(),
164 wLV, worldName, 0, false, 0);
165 RegisterWorld(wPV);
166 }
167 return wPV;
168}
169
170// ----------------------------------------------------------------------------
171// GetNavigator()
172//
173// Provided the name of a world volume, returns the associated navigator.
174// If not existing, create it and register it in the collection, throw an
175// exception if the associated parallel world does not exist.
176//
177G4Navigator* G4TransportationManager::GetNavigator( const G4String& worldName )
178{
179 // If already existing, return the stored pointer to the navigator
180 //
181 std::vector<G4Navigator*>::iterator pNav;
182 for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
183 {
184 if ((*pNav)->GetWorldVolume()->GetName() == worldName) { return *pNav; }
185 }
186
187 // Check if world of that name already exists,
188 // create a navigator and register it
189 //
190 G4Navigator* aNavigator = 0;
191 G4VPhysicalVolume* aWorld = IsWorldExisting(worldName);
192 if(aWorld)
193 {
194 aNavigator = new G4Navigator();
195 aNavigator->SetWorldVolume(aWorld);
196 fNavigators.push_back(aNavigator);
197 }
198 else
199 {
200 G4String message
201 = "World volume with name -" + worldName
202 + "- does not exist. Create it first by GetParallelWorld() method!";
203 G4Exception("G4TransportationManager::GetNavigator(name)",
204 "InvalidSetup", FatalException, message);
205 }
206
207 return aNavigator;
208}
209
210// ----------------------------------------------------------------------------
211// GetNavigator()
212//
213// Provided a pointer to a world volume, returns the associated navigator.
214// Create it in case not existing and add it to the collection.
215// If world volume not existing, issue an exception.
216//
217G4Navigator* G4TransportationManager::GetNavigator( G4VPhysicalVolume* aWorld )
218{
219 std::vector<G4Navigator*>::iterator pNav;
220 for (pNav=fNavigators.begin(); pNav!=fNavigators.end(); pNav++)
221 {
222 if ((*pNav)->GetWorldVolume() == aWorld) { return *pNav; }
223 }
224 G4Navigator* aNavigator = 0;
225 std::vector<G4VPhysicalVolume*>::iterator pWorld =
226 std::find(fWorlds.begin(), fWorlds.end(), aWorld);
227 if (pWorld != fWorlds.end())
228 {
229 aNavigator = new G4Navigator();
230 aNavigator->SetWorldVolume(aWorld);
231 fNavigators.push_back(aNavigator);
232 }
233 else
234 {
235 G4String message
236 = "World volume with name -" + aWorld->GetName()
237 + "- does not exist. Create it first by GetParallelWorld() method!";
238 G4Exception("G4TransportationManager::GetNavigator(pointer)",
239 "InvalidSetup", FatalException, message);
240 }
241
242 return aNavigator;
243}
244
245// ----------------------------------------------------------------------------
246// DeRegisterNavigator()
247//
248// Provided a pointer to an already allocated navigator object, removes the
249// associated entry in the navigators collection (remove pair) but does not
250// delete the actual pointed object, which is still owned by the caller.
251// The navigator for tracking -cannot- be deregistered.
252//
253void G4TransportationManager::DeRegisterNavigator( G4Navigator* aNavigator )
254{
255 if (aNavigator == fNavigators[0])
256 {
257 G4Exception("G4TransportationManager::DeRegisterNavigator()",
258 "InvalidCall", FatalException,
259 "The navigator for tracking CANNOT be deregistered!");
260 }
261 std::vector<G4Navigator*>::iterator pNav =
262 std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
263 if (pNav != fNavigators.end())
264 {
265 // Deregister associated world volume
266 //
267 DeRegisterWorld((*pNav)->GetWorldVolume());
268
269 // Deregister the navigator
270 //
271 fNavigators.erase(pNav);
272 }
273 else
274 {
275 G4String message
276 = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
277 + "- not found in memory!";
278 G4Exception("G4TransportationManager::DeRegisterNavigator()",
279 "NoEffect", JustWarning, message);
280 }
281}
282
283// ----------------------------------------------------------------------------
284// ActivateNavigator()
285//
286// Provided a pointer to an already allocated navigator object, set to 'true'
287// the associated activation flag for the navigator in the collection.
288// If the provided navigator is not already registered, issue a warning
289// Return the index of the activated navigator. This index should be used for
290// ComputeStep() method of G4PathFinder.
291//
292G4int G4TransportationManager::ActivateNavigator( G4Navigator* aNavigator )
293{
294 std::vector<G4Navigator*>::iterator pNav =
295 std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
296 if (pNav == fNavigators.end())
297 {
298 G4String message
299 = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
300 + "- not found in memory!";
301 G4Exception("G4TransportationManager::ActivateNavigator()",
302 "NoEffect", JustWarning, message);
303 return -1;
304 }
305
306 aNavigator->Activate(true);
307 G4int id = 0;
308 std::vector<G4Navigator*>::iterator pActiveNav;
309 for(pActiveNav=fActiveNavigators.begin();
310 pActiveNav!=fActiveNavigators.end(); pActiveNav++)
311 {
312 if (*pActiveNav == aNavigator) { return id; }
313 id++;
314 }
315
316 fActiveNavigators.push_back(aNavigator);
317 return id;
318}
319
320// ----------------------------------------------------------------------------
321// DeActivateNavigator()
322//
323// Provided a pointer to an already allocated navigator object, set to 'false'
324// the associated activation flag in the navigators collection.
325// If the provided navigator is not already registered, issue a warning.
326//
327void G4TransportationManager::DeActivateNavigator( G4Navigator* aNavigator )
328{
329 std::vector<G4Navigator*>::iterator pNav =
330 std::find(fNavigators.begin(), fNavigators.end(), aNavigator);
331 if (pNav == fNavigators.end())
332 {
333 G4String message
334 = "Navigator for volume -" + aNavigator->GetWorldVolume()->GetName()
335 + "- not found in memory!";
336 G4Exception("G4TransportationManager::DeActivateNavigator()",
337 "NoEffect", JustWarning, message);
338 }
339 else
340 {
341 (*pNav)->Activate(false);
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//
357void G4TransportationManager::InactivateAll( )
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//
378G4VPhysicalVolume*
379G4TransportationManager::IsWorldExisting ( const G4String& name )
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//
398G4bool G4TransportationManager::RegisterWorld( G4VPhysicalVolume* aWorld )
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//
419void 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 "InvalidSetup", FatalException, message);
433 }
434}
Note: See TracBrowser for help on using the repository browser.