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

Last change on this file since 846 was 831, checked in by garnier, 16 years ago

import all except CVS

File size: 9.3 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.13 2007/04/10 10:13:50 gcosmo Exp $
28// GEANT4 tag $Name:  $
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  // Do NOT delete world region !
102  //
103  for(iterator pos=store->begin(); pos!=store->end(); ++pos)
104  {
105    if (fgNotifier) { fgNotifier->NotifyDeRegistration(); }
106    if (*pos) { delete *pos; }
107    i++;
108  }
109
110#ifdef G4GEOMETRY_VOXELDEBUG
111  if (store->size() < i-1)
112    { G4cout << "No regions deleted. Already deleted by user ?" << G4endl; }
113  else
114    { G4cout << i-1 << " regions deleted !" << G4endl; }
115#endif
116
117  locked = false;
118  store->clear();
119}
120
121// ***************************************************************************
122// Associate user notifier to the store
123// ***************************************************************************
124//
125void G4RegionStore::SetNotifier(G4VStoreNotifier* pNotifier)
126{
127  GetInstance();
128  fgNotifier = pNotifier;
129}
130
131// ***************************************************************************
132// Add Region to container
133// ***************************************************************************
134//
135void G4RegionStore::Register(G4Region* pRegion)
136{
137  GetInstance()->push_back(pRegion);
138  if (fgNotifier)  { fgNotifier->NotifyRegistration(); }
139}
140
141// ***************************************************************************
142// Remove Region from container
143// ***************************************************************************
144//
145void G4RegionStore::DeRegister(G4Region* pRegion)
146{
147  if (!locked)    // Do not de-register if locked !
148  {
149    if (fgNotifier)  { fgNotifier->NotifyDeRegistration(); }
150    for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
151    {
152      if (**i==*pRegion)
153      {
154        GetInstance()->erase(i);
155        break;
156      }
157    }
158  }
159}
160
161// ***************************************************************************
162// Return ptr to Store, setting if necessary
163// ***************************************************************************
164//
165G4RegionStore* G4RegionStore::GetInstance()
166{
167  static G4RegionStore worldStore;
168  if (!fgInstance)
169  {
170    fgInstance = &worldStore;
171  }
172  return fgInstance;
173}
174
175// ***************************************************************************
176// Loops through all regions to verify if a region has been modified.
177// It returns TRUE if just one region is modified.
178// ***************************************************************************
179//
180G4bool G4RegionStore::IsModified() const
181{
182  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
183  {
184    if ((*i)->IsModified()) { return true; }
185  }
186  return false;
187}
188
189// ***************************************************************************
190// Loops through all regions to reset flag for modification to FALSE.
191// Used by the run manager to notify that the physics table has been updated.
192// ***************************************************************************
193//
194void G4RegionStore::ResetRegionModified()
195{
196  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
197  {
198    (*i)->RegionModified(false);
199  }
200}
201
202// ***************************************************************************
203// Forces recomputation of material lists in all regions in the store.
204// ***************************************************************************
205//
206void G4RegionStore::UpdateMaterialList(G4VPhysicalVolume* currentWorld)
207{
208  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
209  {
210    if((*i)->GetWorldPhysical()==currentWorld) { (*i)->UpdateMaterialList(); }
211  }
212}
213
214// ***************************************************************************
215// Returns a region through its name specification.
216// ***************************************************************************
217//
218G4Region* G4RegionStore::GetRegion(const G4String& name, G4bool verbose) const
219{
220  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
221  {
222    if ((*i)->GetName() == name) { return *i; }
223  }
224  if (verbose)
225  {
226    G4cerr << "ERROR - G4RegionStore::GetRegion()" << G4endl
227           << "        Region " << name << " NOT found in store !" << G4endl
228           << "        Returning NULL pointer." << G4endl;
229    G4Exception("G4RegionStore::GetRegion()", "InvalidQuery",
230                JustWarning, "Region NOT found in store !");
231  }
232  return 0;
233}
234
235// ***************************************************************************
236// Returns a region through its name specification, if it exists.
237// If it does not exist it will allocate a new region with the given
238// name, delegating the ownership to the caller client.
239// ***************************************************************************
240//
241G4Region* G4RegionStore::FindOrCreateRegion(const G4String& name)
242{
243  G4Region* target = GetRegion(name,false);
244  if (!target)
245  {
246    target = new G4Region(name);
247  }
248  return target;
249}
250
251// **************************************************************************
252// Set a world physical volume pointer to a region that belongs to it.
253// Scan over all world volumes.
254// **************************************************************************
255//
256void G4RegionStore::SetWorldVolume()
257{
258  // Reset all pointers first
259  //
260  for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
261  { (*i)->SetWorld(0); }
262
263  // Find world volumes
264  //
265  G4PhysicalVolumeStore* fPhysicalVolumeStore
266    = G4PhysicalVolumeStore::GetInstance();
267  size_t nPhys = fPhysicalVolumeStore->size();
268  for(size_t iPhys=0; iPhys<nPhys; iPhys++)
269  {
270    G4VPhysicalVolume* fPhys = (*fPhysicalVolumeStore)[iPhys];
271    if(fPhys->GetMotherLogical()) { continue; } // not a world volume
272
273    // Now 'fPhys' is a world volume, set it to regions that belong to it.
274    //
275    for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
276    { (*i)->SetWorld(fPhys); }
277  }
278}
279
Note: See TracBrowser for help on using the repository browser.