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