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5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 // G4PhysicalVolumeStore implementation
 27 //
 28 // 25.07.95, P.Kent, G.Cosmo
 29 // --------------------------------------------------------------------
 30 
 31 #include "G4Types.hh"
 32 #include "G4PhysicalVolumeStore.hh"
 33 #include "G4GeometryManager.hh"
 34 #include "G4LogicalVolume.hh"
 35 
 36 #include "G4AutoLock.hh"
 37 
 38 namespace
 39 {
 40   G4Mutex mapMutex = G4MUTEX_INITIALIZER;
 41 }
 42 
 43 // ***************************************************************************
 44 // Static class variables
 45 // ***************************************************************************
 46 //
 47 G4PhysicalVolumeStore* G4PhysicalVolumeStore::fgInstance = nullptr;
 48 G4ThreadLocal G4VStoreNotifier* G4PhysicalVolumeStore::fgNotifier = nullptr;
 49 G4ThreadLocal G4bool G4PhysicalVolumeStore::locked = false;
 50 
 51 // ***************************************************************************
 52 // Protected constructor: Construct underlying container with
 53 // initial size of 100 entries
 54 // ***************************************************************************
 55 //
 56 G4PhysicalVolumeStore::G4PhysicalVolumeStore()
 57    
 58 {
 59   reserve(100);
 60 }
 61 
 62 // ***************************************************************************
 63 // Destructor
 64 // ***************************************************************************
 65 //
 66 G4PhysicalVolumeStore::~G4PhysicalVolumeStore()
 67 {
 68   Clean();  // Delete all volumes in the store
 69   G4VPhysicalVolume::Clean();  // Delete allocated sub-instance data
 70 }
 71 
 72 // ***************************************************************************
 73 // Delete all elements from the store
 74 // ***************************************************************************
 75 //
 76 void G4PhysicalVolumeStore::Clean()
 77 {
 78   // Do nothing if geometry is closed
 79   //
 80   if (G4GeometryManager::GetInstance()->IsGeometryClosed())
 81   {
 82     G4cout << "WARNING - Attempt to delete the physical volume store"
 83            << " while geometry closed !" << G4endl;
 84     return;
 85   }
 86 
 87   // Locks store for deletion of volumes. De-registration will be
 88   // performed at this stage. G4VPhysicalVolumes will not de-register
 89   // themselves.
 90   //
 91   locked = true;
 92 
 93   G4PhysicalVolumeStore* store = GetInstance();
 94 
 95   for(auto pos=store->cbegin(); pos!=store->cend(); ++pos)
 96   {
 97     if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
 98     delete *pos;
 99   }
100 
101   store->bmap.clear(); store->mvalid = false;
102   locked = false;
103   store->clear();
104 }
105 
106 // ***************************************************************************
107 // Associate user notifier to the store
108 // ***************************************************************************
109 //
110 void G4PhysicalVolumeStore::SetNotifier(G4VStoreNotifier* pNotifier)
111 {
112   GetInstance();
113   fgNotifier = pNotifier;
114 }
115 
116 // ***************************************************************************
117 // Bring contents of internal map up to date and reset validity flag
118 // ***************************************************************************
119 //
120 void G4PhysicalVolumeStore::UpdateMap()
121 {
122   G4AutoLock l(&mapMutex);  // to avoid thread contention at initialisation
123   if (mvalid) return;
124   bmap.clear();
125   for(auto pos=GetInstance()->cbegin(); pos!=GetInstance()->cend(); ++pos)
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 { *pos };
136       bmap.insert(std::make_pair(vol_name, vol_vec));
137     }
138   }
139   mvalid = true;
140   l.unlock();
141 }
142 
143 // ***************************************************************************
144 // Add Volume to container
145 // ***************************************************************************
146 //
147 void G4PhysicalVolumeStore::Register(G4VPhysicalVolume* pVolume)
148 {
149   G4PhysicalVolumeStore* store = GetInstance();
150   store->push_back(pVolume);
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 { pVolume };
160     store->bmap.insert(std::make_pair(vol_name, vol_vec));
161   }
162   if (fgNotifier != nullptr) { fgNotifier->NotifyRegistration(); }
163   store->mvalid = true;
164 }
165 
166 // ***************************************************************************
167 // Remove Volume from container and update the list of daughters
168 // of the mother's logical volume
169 // ***************************************************************************
170 //
171 void G4PhysicalVolumeStore::DeRegister(G4VPhysicalVolume* pVolume)
172 {
173   G4PhysicalVolumeStore* store = GetInstance();
174   if (!locked)    // Do not de-register if locked !
175   {
176     if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
177     G4LogicalVolume* motherLogical = pVolume->GetMotherLogical();
178     if (motherLogical != nullptr) { motherLogical->RemoveDaughter(pVolume); }
179     for (auto i=store->cbegin(); i!=store->cend(); ++i)
180     {
181       if (**i==*pVolume)
182       {
183         store->erase(i);
184         break;
185       }
186     }
187     const G4String& vol_name = pVolume->GetName();
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->second.cend(); ++i)
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   }
208 }
209 
210 // ***************************************************************************
211 // Retrieve the first or last volume pointer in the container having that name
212 // ***************************************************************************
213 //
214 G4VPhysicalVolume*
215 G4PhysicalVolumeStore::GetVolume(const G4String& name, G4bool verbose,
216                                  G4bool reverseSearch) const
217 {
218   G4PhysicalVolumeStore* store = GetInstance();
219   if (!store->mvalid)  { store->UpdateMap(); }
220   auto pos = store->bmap.find(name);
221   if(pos != store->bmap.cend())
222   {
223     if ((verbose) && (pos->second.size()>1))
224     {
225       std::ostringstream message;
226       message << "There exists more than ONE physical volume in store named: "
227               << name << "!" << G4endl
228               << "Returning the first found.";
229       G4Exception("G4PhysicalVolumeStore::GetVolume()",
230                   "GeomMgt1001", JustWarning, message);
231     }
232     if(reverseSearch)
233     {
234       return pos->second[pos->second.size()-1];
235     }
236     else
237     {
238       return pos->second[0];
239     }
240   }
241   if (verbose)
242   {
243      std::ostringstream message;
244      message << "Volume NOT found in store !" << G4endl
245             << "        Volume " << name << " NOT found in store !" << G4endl
246             << "        Returning NULL pointer.";
247      G4Exception("G4PhysicalVolumeStore::GetVolume()",
248                  "GeomMgt1001", JustWarning, message);
249   }
250   return nullptr;
251 }
252 
253 // ***************************************************************************
254 // Return ptr to Store, setting if necessary
255 // ***************************************************************************
256 //
257 G4PhysicalVolumeStore* G4PhysicalVolumeStore::GetInstance()
258 {
259   static G4PhysicalVolumeStore worldStore;
260   if (fgInstance == nullptr)
261   {
262     fgInstance = &worldStore;
263   }
264   return fgInstance;
265 }
266