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 ] [ 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 ]

Diff markup

Differences between /geometry/management/src/G4LogicalVolumeStore.cc (Version 11.3.0) and /geometry/management/src/G4LogicalVolumeStore.cc (Version 9.5.p2)


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