Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4RegionStore implementation 26 // G4RegionStore implementation 27 // 27 // 28 // 18.09.02, G.Cosmo - Initial version 28 // 18.09.02, G.Cosmo - Initial version 29 // ------------------------------------------- 29 // -------------------------------------------------------------------- 30 30 31 #include "G4Region.hh" 31 #include "G4Region.hh" 32 #include "G4RegionStore.hh" 32 #include "G4RegionStore.hh" 33 #include "G4GeometryManager.hh" 33 #include "G4GeometryManager.hh" 34 #include "G4VPhysicalVolume.hh" 34 #include "G4VPhysicalVolume.hh" 35 #include "G4PhysicalVolumeStore.hh" 35 #include "G4PhysicalVolumeStore.hh" 36 36 37 #include "G4ios.hh" 37 #include "G4ios.hh" 38 #include "G4AutoLock.hh" 38 #include "G4AutoLock.hh" 39 39 40 namespace 40 namespace 41 { 41 { 42 G4Mutex mapMutex = G4MUTEX_INITIALIZER; 42 G4Mutex mapMutex = G4MUTEX_INITIALIZER; 43 } 43 } 44 44 45 // ******************************************* 45 // *************************************************************************** 46 // Static class variables 46 // Static class variables 47 // ******************************************* 47 // *************************************************************************** 48 // 48 // 49 G4RegionStore* G4RegionStore::fgInstance = nul 49 G4RegionStore* G4RegionStore::fgInstance = nullptr; 50 G4ThreadLocal G4VStoreNotifier* G4RegionStore: 50 G4ThreadLocal G4VStoreNotifier* G4RegionStore::fgNotifier = nullptr; 51 G4ThreadLocal G4bool G4RegionStore::locked = f 51 G4ThreadLocal G4bool G4RegionStore::locked = false; 52 52 53 // ******************************************* 53 // *************************************************************************** 54 // Protected constructor: Construct underlying 54 // Protected constructor: Construct underlying container with 55 // initial size of 20 entries 55 // initial size of 20 entries 56 // ******************************************* 56 // *************************************************************************** 57 // 57 // 58 G4RegionStore::G4RegionStore() 58 G4RegionStore::G4RegionStore() 59 << 59 : std::vector<G4Region*>() 60 { 60 { 61 reserve(20); 61 reserve(20); 62 } 62 } 63 63 64 // ******************************************* 64 // *************************************************************************** 65 // Destructor 65 // Destructor 66 // ******************************************* 66 // *************************************************************************** 67 // 67 // 68 G4RegionStore::~G4RegionStore() 68 G4RegionStore::~G4RegionStore() 69 { 69 { 70 Clean(); // Delete all regions in the store 70 Clean(); // Delete all regions in the store 71 G4Region::Clean(); // Delete allocated sub- 71 G4Region::Clean(); // Delete allocated sub-instance data 72 } 72 } 73 73 74 // ******************************************* 74 // *************************************************************************** 75 // Delete all regions from the store except fo 75 // Delete all regions from the store except for the world region 76 // ******************************************* 76 // *************************************************************************** 77 // 77 // 78 void G4RegionStore::Clean() 78 void G4RegionStore::Clean() 79 { 79 { 80 // Do nothing if geometry is closed 80 // Do nothing if geometry is closed 81 // 81 // 82 if (G4GeometryManager::GetInstance()->IsGeom << 82 if (G4GeometryManager::IsGeometryClosed()) 83 { 83 { 84 G4cout << "WARNING - Attempt to delete the 84 G4cout << "WARNING - Attempt to delete the region store" 85 << " while geometry closed !" << G4 85 << " while geometry closed !" << G4endl; 86 return; 86 return; 87 } 87 } 88 88 89 // Locks store for deletion of regions. De-r 89 // Locks store for deletion of regions. De-registration will be 90 // performed at this stage. G4Regions will n 90 // performed at this stage. G4Regions will not de-register themselves. 91 // 91 // 92 locked = true; 92 locked = true; 93 93 94 G4RegionStore* store = GetInstance(); 94 G4RegionStore* store = GetInstance(); 95 95 96 for(auto pos=store->cbegin(); pos!=store->ce 96 for(auto pos=store->cbegin(); pos!=store->cend(); ++pos) 97 { 97 { 98 if (fgNotifier != nullptr) { fgNotifier->N 98 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); } 99 delete *pos; 99 delete *pos; 100 } 100 } 101 101 102 store->bmap.clear(); store->mvalid = false; 102 store->bmap.clear(); store->mvalid = false; 103 locked = false; 103 locked = false; 104 store->clear(); 104 store->clear(); 105 } 105 } 106 106 107 // ******************************************* 107 // *************************************************************************** 108 // Associate user notifier to the store 108 // Associate user notifier to the store 109 // ******************************************* 109 // *************************************************************************** 110 // 110 // 111 void G4RegionStore::SetNotifier(G4VStoreNotifi 111 void G4RegionStore::SetNotifier(G4VStoreNotifier* pNotifier) 112 { 112 { 113 GetInstance(); 113 GetInstance(); 114 fgNotifier = pNotifier; 114 fgNotifier = pNotifier; 115 } 115 } 116 116 117 // ******************************************* 117 // *************************************************************************** 118 // Bring contents of internal map up to date a 118 // Bring contents of internal map up to date and reset validity flag 119 // ******************************************* 119 // *************************************************************************** 120 // 120 // 121 void G4RegionStore::UpdateMap() 121 void G4RegionStore::UpdateMap() 122 { 122 { 123 G4AutoLock l(&mapMutex); // to avoid thread 123 G4AutoLock l(&mapMutex); // to avoid thread contention at initialisation 124 if (mvalid) return; 124 if (mvalid) return; 125 bmap.clear(); 125 bmap.clear(); 126 for(auto pos=GetInstance()->cbegin(); pos!=G 126 for(auto pos=GetInstance()->cbegin(); pos!=GetInstance()->cend(); ++pos) 127 { 127 { 128 const G4String& reg_name = (*pos)->GetName 128 const G4String& reg_name = (*pos)->GetName(); 129 auto it = bmap.find(reg_name); 129 auto it = bmap.find(reg_name); 130 if (it != bmap.cend()) 130 if (it != bmap.cend()) 131 { 131 { 132 it->second.push_back(*pos); 132 it->second.push_back(*pos); 133 } 133 } 134 else 134 else 135 { 135 { 136 std::vector<G4Region*> reg_vec { *pos }; 136 std::vector<G4Region*> reg_vec { *pos }; 137 bmap.insert(std::make_pair(reg_name, reg 137 bmap.insert(std::make_pair(reg_name, reg_vec)); 138 } 138 } 139 } 139 } 140 mvalid = true; 140 mvalid = true; 141 l.unlock(); 141 l.unlock(); 142 } 142 } 143 143 144 // ******************************************* 144 // *************************************************************************** 145 // Add Region to container 145 // Add Region to container 146 // ******************************************* 146 // *************************************************************************** 147 // 147 // 148 void G4RegionStore::Register(G4Region* pRegion 148 void G4RegionStore::Register(G4Region* pRegion) 149 { 149 { 150 G4RegionStore* store = GetInstance(); 150 G4RegionStore* store = GetInstance(); 151 store->push_back(pRegion); 151 store->push_back(pRegion); 152 const G4String& reg_name = pRegion->GetName( 152 const G4String& reg_name = pRegion->GetName(); 153 auto it = store->bmap.find(reg_name); 153 auto it = store->bmap.find(reg_name); 154 if (it != store->bmap.cend()) 154 if (it != store->bmap.cend()) 155 { 155 { 156 it->second.push_back(pRegion); 156 it->second.push_back(pRegion); 157 } 157 } 158 else 158 else 159 { 159 { 160 std::vector<G4Region*> reg_vec { pRegion } 160 std::vector<G4Region*> reg_vec { pRegion }; 161 store->bmap.insert(std::make_pair(reg_name 161 store->bmap.insert(std::make_pair(reg_name, reg_vec)); 162 } 162 } 163 if (fgNotifier != nullptr) { fgNotifier->Not << 163 if (fgNotifier) { fgNotifier->NotifyRegistration(); } 164 store->mvalid = true; 164 store->mvalid = true; 165 } 165 } 166 166 167 // ******************************************* 167 // *************************************************************************** 168 // Remove Region from container 168 // Remove Region from container 169 // ******************************************* 169 // *************************************************************************** 170 // 170 // 171 void G4RegionStore::DeRegister(G4Region* pRegi 171 void G4RegionStore::DeRegister(G4Region* pRegion) 172 { 172 { 173 G4RegionStore* store = GetInstance(); 173 G4RegionStore* store = GetInstance(); 174 if (!locked) // Do not de-register if loc 174 if (!locked) // Do not de-register if locked ! 175 { 175 { 176 if (fgNotifier != nullptr) { fgNotifier-> 176 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); } 177 for (auto i=store->cbegin(); i!=store->cen 177 for (auto i=store->cbegin(); i!=store->cend(); ++i) 178 { 178 { 179 if (**i==*pRegion) 179 if (**i==*pRegion) 180 { 180 { 181 store->erase(i); 181 store->erase(i); 182 break; 182 break; 183 } 183 } 184 } 184 } 185 const G4String& reg_name = pRegion->GetNam 185 const G4String& reg_name = pRegion->GetName(); 186 auto it = store->bmap.find(reg_name); 186 auto it = store->bmap.find(reg_name); 187 if (it != store->bmap.cend()) 187 if (it != store->bmap.cend()) 188 { 188 { 189 if (it->second.size() > 1) 189 if (it->second.size() > 1) 190 { 190 { 191 for (auto i=it->second.cbegin(); i!=it 191 for (auto i=it->second.cbegin(); i!=it->second.cend(); ++i) 192 { 192 { 193 if (**i==*pRegion) 193 if (**i==*pRegion) 194 { 194 { 195 it->second.erase(i); 195 it->second.erase(i); 196 break; 196 break; 197 } 197 } 198 } 198 } 199 } 199 } 200 else 200 else 201 { 201 { 202 store->bmap.erase(it); 202 store->bmap.erase(it); 203 } 203 } 204 } 204 } 205 } 205 } 206 } 206 } 207 207 208 // ******************************************* 208 // *************************************************************************** 209 // Return ptr to Store, setting if necessary 209 // Return ptr to Store, setting if necessary 210 // ******************************************* 210 // *************************************************************************** 211 // 211 // 212 G4RegionStore* G4RegionStore::GetInstance() 212 G4RegionStore* G4RegionStore::GetInstance() 213 { 213 { 214 static G4RegionStore worldStore; 214 static G4RegionStore worldStore; 215 if (fgInstance == nullptr) 215 if (fgInstance == nullptr) 216 { 216 { 217 fgInstance = &worldStore; 217 fgInstance = &worldStore; 218 } 218 } 219 return fgInstance; 219 return fgInstance; 220 } 220 } 221 221 222 // ******************************************* 222 // *************************************************************************** 223 // Loops through all regions to verify if a re 223 // Loops through all regions to verify if a region has been modified. 224 // It returns TRUE if just one region is modif 224 // It returns TRUE if just one region is modified. 225 // ******************************************* 225 // *************************************************************************** 226 // 226 // 227 G4bool G4RegionStore::IsModified() const 227 G4bool G4RegionStore::IsModified() const 228 { 228 { 229 for (auto i=GetInstance()->cbegin(); i!=GetI 229 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 230 { 230 { 231 if ((*i)->IsModified()) { return true; } 231 if ((*i)->IsModified()) { return true; } 232 } 232 } 233 return false; 233 return false; 234 } 234 } 235 235 236 // ******************************************* 236 // *************************************************************************** 237 // Loops through all regions to reset flag for 237 // Loops through all regions to reset flag for modification to FALSE. 238 // Used by the run manager to notify that the 238 // Used by the run manager to notify that the physics table has been updated. 239 // ******************************************* 239 // *************************************************************************** 240 // 240 // 241 void G4RegionStore::ResetRegionModified() 241 void G4RegionStore::ResetRegionModified() 242 { 242 { 243 for (auto i=GetInstance()->cbegin(); i!=GetI 243 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 244 { 244 { 245 (*i)->RegionModified(false); 245 (*i)->RegionModified(false); 246 } 246 } 247 } 247 } 248 248 249 // ******************************************* 249 // *************************************************************************** 250 // Forces recomputation of material lists in a 250 // Forces recomputation of material lists in all regions in the store. 251 // ******************************************* 251 // *************************************************************************** 252 // 252 // 253 void G4RegionStore::UpdateMaterialList(G4VPhys 253 void G4RegionStore::UpdateMaterialList(G4VPhysicalVolume* currentWorld) 254 { 254 { 255 for (auto i=GetInstance()->cbegin(); i!=GetI 255 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 256 { 256 { 257 if((*i)->IsInMassGeometry() || (*i)->IsInP 257 if((*i)->IsInMassGeometry() || (*i)->IsInParallelGeometry() 258 || (currentWor 258 || (currentWorld != nullptr)) 259 { (*i)->UpdateMaterialList(); } 259 { (*i)->UpdateMaterialList(); } 260 } 260 } 261 } 261 } 262 262 263 // ******************************************* 263 // *************************************************************************** 264 // Returns a region through its name specifica 264 // Returns a region through its name specification. 265 // ******************************************* 265 // *************************************************************************** 266 // 266 // 267 G4Region* G4RegionStore::GetRegion(const G4Str 267 G4Region* G4RegionStore::GetRegion(const G4String& name, G4bool verbose) const 268 { 268 { 269 G4RegionStore* store = GetInstance(); 269 G4RegionStore* store = GetInstance(); 270 if (!store->mvalid) { store->UpdateMap(); } 270 if (!store->mvalid) { store->UpdateMap(); } 271 auto pos = store->bmap.find(name); 271 auto pos = store->bmap.find(name); 272 if(pos != store->bmap.cend()) 272 if(pos != store->bmap.cend()) 273 { 273 { 274 if ((verbose) && (pos->second.size()>1)) 274 if ((verbose) && (pos->second.size()>1)) 275 { 275 { 276 std::ostringstream message; 276 std::ostringstream message; 277 message << "There exists more than ONE r 277 message << "There exists more than ONE region in store named: " 278 << name << "!" << G4endl 278 << name << "!" << G4endl 279 << "Returning the first found."; 279 << "Returning the first found."; 280 G4Exception("G4RegionStore::GetSolid()", 280 G4Exception("G4RegionStore::GetSolid()", 281 "GeomMgt1001", JustWarning, 281 "GeomMgt1001", JustWarning, message); 282 } 282 } 283 return pos->second[0]; 283 return pos->second[0]; 284 } 284 } 285 if (verbose) 285 if (verbose) 286 { 286 { 287 std::ostringstream message; 287 std::ostringstream message; 288 message << "Region NOT found in store !" < 288 message << "Region NOT found in store !" << G4endl 289 << " Region " << name << " 289 << " Region " << name << " NOT found in store !" << G4endl 290 << " Returning NULL pointer 290 << " Returning NULL pointer."; 291 G4Exception("G4RegionStore::GetRegion()", 291 G4Exception("G4RegionStore::GetRegion()", 292 "GeomMgt1001", JustWarning, me 292 "GeomMgt1001", JustWarning, message); 293 } 293 } 294 return nullptr; 294 return nullptr; 295 } 295 } 296 296 297 // ******************************************* 297 // *************************************************************************** 298 // Returns a region through its name specifica 298 // Returns a region through its name specification, if it exists. 299 // If it does not exist it will allocate a new 299 // If it does not exist it will allocate a new region with the given 300 // name, delegating the ownership to the calle 300 // name, delegating the ownership to the caller client. 301 // ******************************************* 301 // *************************************************************************** 302 // 302 // 303 G4Region* G4RegionStore::FindOrCreateRegion(co 303 G4Region* G4RegionStore::FindOrCreateRegion(const G4String& name) 304 { 304 { 305 G4Region* target = GetRegion(name,false); 305 G4Region* target = GetRegion(name,false); 306 if (target == nullptr) 306 if (target == nullptr) 307 { 307 { 308 target = new G4Region(name); 308 target = new G4Region(name); 309 } 309 } 310 return target; 310 return target; 311 } 311 } 312 312 313 // ******************************************* 313 // ************************************************************************** 314 // Set a world physical volume pointer to a re 314 // Set a world physical volume pointer to a region that belongs to it. 315 // Scan over all world volumes. 315 // Scan over all world volumes. 316 // ******************************************* 316 // ************************************************************************** 317 // 317 // 318 void G4RegionStore::SetWorldVolume() 318 void G4RegionStore::SetWorldVolume() 319 { 319 { 320 // Reset all pointers first 320 // Reset all pointers first 321 // 321 // 322 for (auto i=GetInstance()->cbegin(); i!=GetI 322 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 323 { (*i)->SetWorld(nullptr); } 323 { (*i)->SetWorld(nullptr); } 324 324 325 // Find world volumes 325 // Find world volumes 326 // 326 // 327 G4PhysicalVolumeStore* fPhysicalVolumeStore 327 G4PhysicalVolumeStore* fPhysicalVolumeStore 328 = G4PhysicalVolumeStore::GetInstance(); 328 = G4PhysicalVolumeStore::GetInstance(); 329 std::size_t nPhys = fPhysicalVolumeStore->si 329 std::size_t nPhys = fPhysicalVolumeStore->size(); 330 for(std::size_t iPhys=0; iPhys<nPhys; ++iPhy 330 for(std::size_t iPhys=0; iPhys<nPhys; ++iPhys) 331 { 331 { 332 G4VPhysicalVolume* fPhys = (*fPhysicalVolu 332 G4VPhysicalVolume* fPhys = (*fPhysicalVolumeStore)[iPhys]; 333 if(fPhys->GetMotherLogical() != nullptr) { 333 if(fPhys->GetMotherLogical() != nullptr) { continue; } // not a world volume 334 334 335 // Now 'fPhys' is a world volume, set it t 335 // Now 'fPhys' is a world volume, set it to regions that belong to it. 336 // 336 // 337 for (auto i=GetInstance()->cbegin(); i!=Ge 337 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 338 { (*i)->SetWorld(fPhys); } 338 { (*i)->SetWorld(fPhys); } 339 } 339 } 340 } 340 } 341 341 342 342