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 // G4PhysicalVolumeStore implementation << 27 // 26 // 28 // 25.07.95, P.Kent, G.Cosmo << 27 // $Id: G4PhysicalVolumeStore.cc 66872 2013-01-15 01:25:57Z japost $ >> 28 // >> 29 // G4PhysicalVolumeStore >> 30 // >> 31 // Implementation for singleton container >> 32 // >> 33 // History: >> 34 // 25.07.95 P.Kent Initial version 29 // ------------------------------------------- 35 // -------------------------------------------------------------------- 30 36 31 #include "G4Types.hh" 37 #include "G4Types.hh" 32 #include "G4PhysicalVolumeStore.hh" 38 #include "G4PhysicalVolumeStore.hh" 33 #include "G4GeometryManager.hh" 39 #include "G4GeometryManager.hh" 34 #include "G4LogicalVolume.hh" 40 #include "G4LogicalVolume.hh" 35 41 36 #include "G4AutoLock.hh" << 37 << 38 namespace << 39 { << 40 G4Mutex mapMutex = G4MUTEX_INITIALIZER; << 41 } << 42 << 43 // ******************************************* 42 // *************************************************************************** 44 // Static class variables 43 // Static class variables 45 // ******************************************* 44 // *************************************************************************** 46 // 45 // 47 G4PhysicalVolumeStore* G4PhysicalVolumeStore:: << 46 G4PhysicalVolumeStore* G4PhysicalVolumeStore::fgInstance = 0; 48 G4ThreadLocal G4VStoreNotifier* G4PhysicalVolu << 47 G4ThreadLocal G4VStoreNotifier* G4PhysicalVolumeStore::fgNotifier = 0; 49 G4ThreadLocal G4bool G4PhysicalVolumeStore::lo 48 G4ThreadLocal G4bool G4PhysicalVolumeStore::locked = false; 50 49 51 // ******************************************* 50 // *************************************************************************** 52 // Protected constructor: Construct underlying 51 // Protected constructor: Construct underlying container with 53 // initial size of 100 entries 52 // initial size of 100 entries 54 // ******************************************* 53 // *************************************************************************** 55 // 54 // 56 G4PhysicalVolumeStore::G4PhysicalVolumeStore() 55 G4PhysicalVolumeStore::G4PhysicalVolumeStore() 57 << 56 : std::vector<G4VPhysicalVolume*>() 58 { 57 { 59 reserve(100); 58 reserve(100); 60 } 59 } 61 60 62 // ******************************************* 61 // *************************************************************************** 63 // Destructor 62 // Destructor 64 // ******************************************* 63 // *************************************************************************** 65 // 64 // 66 G4PhysicalVolumeStore::~G4PhysicalVolumeStore( 65 G4PhysicalVolumeStore::~G4PhysicalVolumeStore() 67 { 66 { 68 Clean(); // Delete all volumes in the store << 67 Clean(); 69 G4VPhysicalVolume::Clean(); // Delete alloc << 70 } 68 } 71 69 72 // ******************************************* 70 // *************************************************************************** 73 // Delete all elements from the store 71 // Delete all elements from the store 74 // ******************************************* 72 // *************************************************************************** 75 // 73 // 76 void G4PhysicalVolumeStore::Clean() 74 void G4PhysicalVolumeStore::Clean() 77 { 75 { 78 // Do nothing if geometry is closed 76 // Do nothing if geometry is closed 79 // 77 // 80 if (G4GeometryManager::GetInstance()->IsGeom 78 if (G4GeometryManager::GetInstance()->IsGeometryClosed()) 81 { 79 { 82 G4cout << "WARNING - Attempt to delete the 80 G4cout << "WARNING - Attempt to delete the physical volume store" 83 << " while geometry closed !" << G4 81 << " while geometry closed !" << G4endl; 84 return; 82 return; 85 } 83 } 86 84 87 // Locks store for deletion of volumes. De-r 85 // Locks store for deletion of volumes. De-registration will be 88 // performed at this stage. G4VPhysicalVolum 86 // performed at this stage. G4VPhysicalVolumes will not de-register 89 // themselves. 87 // themselves. 90 // 88 // 91 locked = true; 89 locked = true; 92 90 >> 91 size_t i=0; 93 G4PhysicalVolumeStore* store = GetInstance() 92 G4PhysicalVolumeStore* store = GetInstance(); >> 93 std::vector<G4VPhysicalVolume*>::iterator pos; >> 94 >> 95 #ifdef G4GEOMETRY_VOXELDEBUG >> 96 G4cout << "Deleting Physical Volumes ... "; >> 97 #endif 94 98 95 for(auto pos=store->cbegin(); pos!=store->ce << 99 for(pos=store->begin(); pos!=store->end(); pos++) 96 { 100 { 97 if (fgNotifier != nullptr) { fgNotifier->N << 101 if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } 98 delete *pos; << 102 if (*pos) { delete *pos; } >> 103 i++; 99 } 104 } 100 105 101 store->bmap.clear(); store->mvalid = false; << 106 #ifdef G4GEOMETRY_VOXELDEBUG >> 107 if (store->size() < i-1) >> 108 { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; } >> 109 else >> 110 { G4cout << i-1 << " volumes deleted !" << G4endl; } >> 111 #endif >> 112 102 locked = false; 113 locked = false; 103 store->clear(); 114 store->clear(); 104 } 115 } 105 116 106 // ******************************************* 117 // *************************************************************************** 107 // Associate user notifier to the store 118 // Associate user notifier to the store 108 // ******************************************* 119 // *************************************************************************** 109 // 120 // 110 void G4PhysicalVolumeStore::SetNotifier(G4VSto 121 void G4PhysicalVolumeStore::SetNotifier(G4VStoreNotifier* pNotifier) 111 { 122 { 112 GetInstance(); 123 GetInstance(); 113 fgNotifier = pNotifier; 124 fgNotifier = pNotifier; 114 } 125 } 115 126 116 // ******************************************* 127 // *************************************************************************** 117 // Bring contents of internal map up to date a << 118 // ******************************************* << 119 // << 120 void G4PhysicalVolumeStore::UpdateMap() << 121 { << 122 G4AutoLock l(&mapMutex); // to avoid thread << 123 if (mvalid) return; << 124 bmap.clear(); << 125 for(auto pos=GetInstance()->cbegin(); pos!=G << 126 { << 127 const G4String& vol_name = (*pos)->GetName << 128 auto it = bmap.find(vol_name); << 129 if (it != bmap.cend()) << 130 { << 131 it->second.push_back(*pos); << 132 } << 133 else << 134 { << 135 std::vector<G4VPhysicalVolume*> vol_vec << 136 bmap.insert(std::make_pair(vol_name, vol << 137 } << 138 } << 139 mvalid = true; << 140 l.unlock(); << 141 } << 142 << 143 // ******************************************* << 144 // Add Volume to container 128 // Add Volume to container 145 // ******************************************* 129 // *************************************************************************** 146 // 130 // 147 void G4PhysicalVolumeStore::Register(G4VPhysic 131 void G4PhysicalVolumeStore::Register(G4VPhysicalVolume* pVolume) 148 { 132 { 149 G4PhysicalVolumeStore* store = GetInstance() << 133 GetInstance()->push_back(pVolume); 150 store->push_back(pVolume); << 134 if (fgNotifier) { fgNotifier->NotifyRegistration(); } 151 const G4String& vol_name = pVolume->GetName( << 152 auto it = store->bmap.find(vol_name); << 153 if (it != store->bmap.cend()) << 154 { << 155 it->second.push_back(pVolume); << 156 } << 157 else << 158 { << 159 std::vector<G4VPhysicalVolume*> vol_vec { << 160 store->bmap.insert(std::make_pair(vol_name << 161 } << 162 if (fgNotifier != nullptr) { fgNotifier->Not << 163 store->mvalid = true; << 164 } 135 } 165 136 166 // ******************************************* 137 // *************************************************************************** 167 // Remove Volume from container and update the 138 // Remove Volume from container and update the list of daughters 168 // of the mother's logical volume 139 // of the mother's logical volume 169 // ******************************************* 140 // *************************************************************************** 170 // 141 // 171 void G4PhysicalVolumeStore::DeRegister(G4VPhys 142 void G4PhysicalVolumeStore::DeRegister(G4VPhysicalVolume* pVolume) 172 { 143 { 173 G4PhysicalVolumeStore* store = GetInstance() << 174 if (!locked) // Do not de-register if loc 144 if (!locked) // Do not de-register if locked ! 175 { 145 { 176 if (fgNotifier != nullptr) { fgNotifier->N << 146 if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } 177 G4LogicalVolume* motherLogical = pVolume-> 147 G4LogicalVolume* motherLogical = pVolume->GetMotherLogical(); 178 if (motherLogical != nullptr) { motherLogi << 148 if (motherLogical) { motherLogical->RemoveDaughter(pVolume); } 179 for (auto i=store->cbegin(); i!=store->cen << 149 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 180 { 150 { 181 if (**i==*pVolume) 151 if (**i==*pVolume) 182 { 152 { 183 store->erase(i); << 153 GetInstance()->erase(i); 184 break; 154 break; 185 } 155 } 186 } 156 } 187 const G4String& vol_name = pVolume->GetNam << 188 auto it = store->bmap.find(vol_name); << 189 if (it != store->bmap.cend()) << 190 { << 191 if (it->second.size() > 1) << 192 { << 193 for (auto i=it->second.cbegin(); i!=it << 194 { << 195 if (**i==*pVolume) << 196 { << 197 it->second.erase(i); << 198 break; << 199 } << 200 } << 201 } << 202 else << 203 { << 204 store->bmap.erase(it); << 205 } << 206 } << 207 } 157 } 208 } 158 } 209 159 210 // ******************************************* 160 // *************************************************************************** 211 // Retrieve the first or last volume pointer i << 161 // Retrieve the first volume pointer in the container having that name 212 // ******************************************* 162 // *************************************************************************** 213 // 163 // 214 G4VPhysicalVolume* 164 G4VPhysicalVolume* 215 G4PhysicalVolumeStore::GetVolume(const G4Strin << 165 G4PhysicalVolumeStore::GetVolume(const G4String& name, G4bool verbose) const 216 G4bool revers << 217 { 166 { 218 G4PhysicalVolumeStore* store = GetInstance() << 167 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 219 if (!store->mvalid) { store->UpdateMap(); } << 220 auto pos = store->bmap.find(name); << 221 if(pos != store->bmap.cend()) << 222 { 168 { 223 if ((verbose) && (pos->second.size()>1)) << 169 if ((*i)->GetName() == name) { return *i; } 224 { << 225 std::ostringstream message; << 226 message << "There exists more than ONE p << 227 << name << "!" << G4endl << 228 << "Returning the first found."; << 229 G4Exception("G4PhysicalVolumeStore::GetV << 230 "GeomMgt1001", JustWarning, << 231 } << 232 if(reverseSearch) << 233 { << 234 return pos->second[pos->second.size()-1] << 235 } << 236 else << 237 { << 238 return pos->second[0]; << 239 } << 240 } 170 } 241 if (verbose) 171 if (verbose) 242 { 172 { 243 std::ostringstream message; 173 std::ostringstream message; 244 message << "Volume NOT found in store !" 174 message << "Volume NOT found in store !" << G4endl 245 << " Volume " << name << " 175 << " Volume " << name << " NOT found in store !" << G4endl 246 << " Returning NULL pointer 176 << " Returning NULL pointer."; 247 G4Exception("G4PhysicalVolumeStore::GetVo 177 G4Exception("G4PhysicalVolumeStore::GetVolume()", 248 "GeomMgt1001", JustWarning, m 178 "GeomMgt1001", JustWarning, message); 249 } 179 } 250 return nullptr; << 180 return 0; 251 } 181 } 252 182 253 // ******************************************* 183 // *************************************************************************** 254 // Return ptr to Store, setting if necessary 184 // Return ptr to Store, setting if necessary 255 // ******************************************* 185 // *************************************************************************** 256 // 186 // 257 G4PhysicalVolumeStore* G4PhysicalVolumeStore:: 187 G4PhysicalVolumeStore* G4PhysicalVolumeStore::GetInstance() 258 { 188 { 259 static G4PhysicalVolumeStore worldStore; 189 static G4PhysicalVolumeStore worldStore; 260 if (fgInstance == nullptr) << 190 if (!fgInstance) 261 { 191 { 262 fgInstance = &worldStore; 192 fgInstance = &worldStore; 263 } 193 } 264 return fgInstance; 194 return fgInstance; 265 } 195 } 266 196