source: trunk/source/geometry/management/src/G4RegionStore.cc @ 1294

Last change on this file since 1294 was 1228, checked in by garnier, 14 years ago

update geant4.9.3 tag

File size: 9.2 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: G4RegionStore.cc,v 1.14 2008/07/10 09:46:01 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-03 $
29//
30// G4RegionStore
31//
32// Implementation for singleton container
33//
34// History:
35// 18.09.02 G.Cosmo Initial version
36// --------------------------------------------------------------------
37
38#include "G4Region.hh"
39#include "G4RegionStore.hh"
40#include "G4GeometryManager.hh"
41#include "G4VPhysicalVolume.hh"
42#include "G4PhysicalVolumeStore.hh"
43
44#include "G4ios.hh"
45
46// ***************************************************************************
47// Static class variables
48// ***************************************************************************
49//
50G4RegionStore* G4RegionStore::fgInstance = 0;
51G4VStoreNotifier* G4RegionStore::fgNotifier = 0;
52G4bool G4RegionStore::locked = false;
53
54// ***************************************************************************
55// Protected constructor: Construct underlying container with
56// initial size of 20 entries
57// ***************************************************************************
58//
59G4RegionStore::G4RegionStore()
60  : std::vector<G4Region*>()
61{
62  reserve(20);
63}
64
65// ***************************************************************************
66// Destructor
67// ***************************************************************************
68//
69G4RegionStore::~G4RegionStore() 
70{
71  Clean();
72}
73
74// ***************************************************************************
75// Delete all regions from the store except for the world region
76// ***************************************************************************
77//
78void G4RegionStore::Clean()
79{
80  // Do nothing if geometry is closed
81  //
82  if (G4GeometryManager::GetInstance()->IsGeometryClosed())
83  {
84    G4cout << "WARNING - Attempt to delete the region store"
85           << " while geometry closed !" << G4endl;
86    return;
87  }
88
89  // Locks store for deletion of regions. De-registration will be
90  // performed at this stage. G4Regions will not de-register themselves.
91  //
92  locked = true; 
93
94  size_t i=0;
95  G4RegionStore* store = GetInstance();
96
97#ifdef G4GEOMETRY_VOXELDEBUG
98  G4cout << "Deleting Regions ... ";
99#endif
100
101  for(iterator pos=store->begin(); pos!=store->end(); ++pos)
102  {
103    if (fgNotifier) { fgNotifier->NotifyDeRegistration(); }
104    if (*pos) { delete *pos; }
105    i++;
106  }
107
108#ifdef G4GEOMETRY_VOXELDEBUG
109  if (store->size() < i-1)
110    { G4cout << "No regions deleted. Already deleted by user ?" << G4endl; }
111  else
112    { G4cout << i-1 << " regions deleted !" << G4endl; }
113#endif
114
115  locked = false;
116  store->clear();
117}
118
119// ***************************************************************************
120// Associate user notifier to the store
121// ***************************************************************************
122//
123void G4RegionStore::SetNotifier(G4VStoreNotifier* pNotifier)
124{
125  GetInstance();
126  fgNotifier = pNotifier;
127}
128
129// ***************************************************************************
130// Add Region to container
131// ***************************************************************************
132//
133void G4RegionStore::Register(G4Region* pRegion)
134{
135  GetInstance()->push_back(pRegion);
136  if (fgNotifier)  { fgNotifier->NotifyRegistration(); }
137}
138
139// ***************************************************************************
140// Remove Region from container
141// ***************************************************************************
142//
143void G4RegionStore::DeRegister(G4Region* pRegion)
144{
145  if (!locked)    // Do not de-register if locked !
146  {
147    if (fgNotifier)  { fgNotifier->NotifyDeRegistration(); }
148    for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
149    {
150      if (**i==*pRegion)
151      {
152        GetInstance()->erase(i);
153        break;
154      }
155    }
156  }
157}
158
159// ***************************************************************************
160// Return ptr to Store, setting if necessary
161// ***************************************************************************
162//
163G4RegionStore* G4RegionStore::GetInstance()
164{
165  static G4RegionStore worldStore;
166  if (!fgInstance)
167  {
168    fgInstance = &worldStore;
169  }
170  return fgInstance;
171}
172
173// ***************************************************************************
174// Loops through all regions to verify if a region has been modified.
175// It returns TRUE if just one region is modified.
176// ***************************************************************************
177//
178G4bool G4RegionStore::IsModified() const
179{
180  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
181  {
182    if ((*i)->IsModified()) { return true; }
183  }
184  return false;
185}
186
187// ***************************************************************************
188// Loops through all regions to reset flag for modification to FALSE.
189// Used by the run manager to notify that the physics table has been updated.
190// ***************************************************************************
191//
192void G4RegionStore::ResetRegionModified()
193{
194  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
195  {
196    (*i)->RegionModified(false);
197  }
198}
199
200// ***************************************************************************
201// Forces recomputation of material lists in all regions in the store.
202// ***************************************************************************
203//
204void G4RegionStore::UpdateMaterialList(G4VPhysicalVolume* currentWorld)
205{
206  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
207  {
208    if((*i)->GetWorldPhysical()==currentWorld) { (*i)->UpdateMaterialList(); }
209  }
210}
211
212// ***************************************************************************
213// Returns a region through its name specification.
214// ***************************************************************************
215//
216G4Region* G4RegionStore::GetRegion(const G4String& name, G4bool verbose) const
217{
218  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
219  {
220    if ((*i)->GetName() == name) { return *i; }
221  }
222  if (verbose)
223  {
224    G4cerr << "ERROR - G4RegionStore::GetRegion()" << G4endl
225           << "        Region " << name << " NOT found in store !" << G4endl
226           << "        Returning NULL pointer." << G4endl;
227    G4Exception("G4RegionStore::GetRegion()", "InvalidQuery",
228                JustWarning, "Region NOT found in store !");
229  }
230  return 0;
231}
232
233// ***************************************************************************
234// Returns a region through its name specification, if it exists.
235// If it does not exist it will allocate a new region with the given
236// name, delegating the ownership to the caller client.
237// ***************************************************************************
238//
239G4Region* G4RegionStore::FindOrCreateRegion(const G4String& name)
240{
241  G4Region* target = GetRegion(name,false);
242  if (!target)
243  {
244    target = new G4Region(name);
245  }
246  return target;
247}
248
249// **************************************************************************
250// Set a world physical volume pointer to a region that belongs to it.
251// Scan over all world volumes.
252// **************************************************************************
253//
254void G4RegionStore::SetWorldVolume()
255{
256  // Reset all pointers first
257  //
258  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
259  { (*i)->SetWorld(0); }
260
261  // Find world volumes
262  //
263  G4PhysicalVolumeStore* fPhysicalVolumeStore
264    = G4PhysicalVolumeStore::GetInstance();
265  size_t nPhys = fPhysicalVolumeStore->size();
266  for(size_t iPhys=0; iPhys<nPhys; iPhys++)
267  {
268    G4VPhysicalVolume* fPhys = (*fPhysicalVolumeStore)[iPhys];
269    if(fPhys->GetMotherLogical()) { continue; } // not a world volume
270
271    // Now 'fPhys' is a world volume, set it to regions that belong to it.
272    //
273    for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
274    { (*i)->SetWorld(fPhys); }
275  }
276}
277
Note: See TracBrowser for help on using the repository browser.