Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4PhysicalVolumeStore Class Reference

G4PhysicalVolumeStore is a singleton class, acting as container for all physical volumes, with functionality derived from std::vector<T>. All physical volumes should be registered with G4PhysicalVolumeStore, and removed on their destruction. The underlying container initially has a capacity of 100. A map indexed by volume names is also recorded for fast search; pointers to volumes with same name are stored in buckets. More...

#include <G4PhysicalVolumeStore.hh>

Inheritance diagram for G4PhysicalVolumeStore:

Public Member Functions

virtual ~G4PhysicalVolumeStore ()
 G4PhysicalVolumeStore (const G4PhysicalVolumeStore &)=delete
G4PhysicalVolumeStoreoperator= (const G4PhysicalVolumeStore &)=delete
G4VPhysicalVolumeGetVolume (const G4String &name, G4bool verbose=true, G4bool reverseSearch=false) const
G4bool IsMapValid () const
void SetMapValid (G4bool val)
const std::map< G4String, std::vector< G4VPhysicalVolume * > > & GetMap () const
void UpdateMap ()

Static Public Member Functions

static void Register (G4VPhysicalVolume *pVolume)
static void DeRegister (G4VPhysicalVolume *pVolume)
static G4PhysicalVolumeStoreGetInstance ()
static void SetNotifier (G4VStoreNotifier *pNotifier)
static void Clean ()

Protected Member Functions

 G4PhysicalVolumeStore ()

Detailed Description

G4PhysicalVolumeStore is a singleton class, acting as container for all physical volumes, with functionality derived from std::vector<T>. All physical volumes should be registered with G4PhysicalVolumeStore, and removed on their destruction. The underlying container initially has a capacity of 100. A map indexed by volume names is also recorded for fast search; pointers to volumes with same name are stored in buckets.

Definition at line 63 of file G4PhysicalVolumeStore.hh.

Constructor & Destructor Documentation

◆ ~G4PhysicalVolumeStore()

G4PhysicalVolumeStore::~G4PhysicalVolumeStore ( )
virtual

Destructor: takes care to delete allocated physical volumes.

Definition at line 66 of file G4PhysicalVolumeStore.cc.

67{
68 Clean(); // Delete all volumes in the store
69 G4VPhysicalVolume::Clean(); // Delete allocated sub-instance data
70}

◆ G4PhysicalVolumeStore() [1/2]

G4PhysicalVolumeStore::G4PhysicalVolumeStore ( const G4PhysicalVolumeStore & )
delete

Copy constructor and assignment operator not allowed.

Referenced by Clean(), DeRegister(), G4PhysicalVolumeStore(), GetInstance(), GetVolume(), operator=(), and Register().

◆ G4PhysicalVolumeStore() [2/2]

G4PhysicalVolumeStore::G4PhysicalVolumeStore ( )
protected

Protected constructor.

Definition at line 56 of file G4PhysicalVolumeStore.cc.

58{
59 reserve(100);
60}

Member Function Documentation

◆ Clean()

void G4PhysicalVolumeStore::Clean ( )
static

Deletes all physical volumes from the store. Mother logical volumes are automatically notified and have their daughters de-registered.

Definition at line 76 of file G4PhysicalVolumeStore.cc.

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
94
95 for(const auto & pos : *store)
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}
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
static G4GeometryManager * GetInstance()
static G4PhysicalVolumeStore * GetInstance()
G4PhysicalVolumeStore(const G4PhysicalVolumeStore &)=delete

Referenced by G4RunManager::ReinitializeGeometry(), and ~G4PhysicalVolumeStore().

◆ DeRegister()

void G4PhysicalVolumeStore::DeRegister ( G4VPhysicalVolume * pVolume)
static

Removes the logical volume 'pVolume' from the collection.

Definition at line 171 of file G4PhysicalVolumeStore.cc.

172{
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}
void RemoveDaughter(const G4VPhysicalVolume *p)
G4LogicalVolume * GetMotherLogical() const
const G4String & GetName() const

Referenced by G4VPhysicalVolume::~G4VPhysicalVolume().

◆ GetInstance()

◆ GetMap()

const std::map< G4String, std::vector< G4VPhysicalVolume * > > & G4PhysicalVolumeStore::GetMap ( ) const
inline

Return the internal map.

Definition at line 127 of file G4PhysicalVolumeStore.hh.

127{ return bmap; }

◆ GetVolume()

G4VPhysicalVolume * G4PhysicalVolumeStore::GetVolume ( const G4String & name,
G4bool verbose = true,
G4bool reverseSearch = false ) const

Returns a pointer to the first or last volume in the collection having that 'name'. Uses the internal map for fast search and warns if a volume in the collection is not unique or not found.

Parameters
[in]nameThe name of the volume to search.
[in]verboseFlag for enabling verbosity (default true).
[in]reverseSearchFlag to enable inverse search (default false).

Definition at line 215 of file G4PhysicalVolumeStore.cc.

217{
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 return pos->second[0];
237 }
238 if (verbose)
239 {
240 std::ostringstream message;
241 message << "Volume NOT found in store !" << G4endl
242 << " Volume " << name << " NOT found in store !" << G4endl
243 << " Returning NULL pointer.";
244 G4Exception("G4PhysicalVolumeStore::GetVolume()",
245 "GeomMgt1001", JustWarning, message);
246 }
247 return nullptr;
248}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
const char * name(G4int ptype)

Referenced by G4AdjointCrossSurfChecker::AddanExtSurfaceOfAvolume(), G4AdjointCrossSurfChecker::AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume(), G4SPSPosDistribution::ConfineSourceToVolume(), and G4GDMLReadStructure::GetPhysvol().

◆ IsMapValid()

G4bool G4PhysicalVolumeStore::IsMapValid ( ) const
inline

Accessor and modifier to assess validity of the internal map.

Definition at line 120 of file G4PhysicalVolumeStore.hh.

120{ return mvalid; }

◆ operator=()

G4PhysicalVolumeStore & G4PhysicalVolumeStore::operator= ( const G4PhysicalVolumeStore & )
delete

◆ Register()

void G4PhysicalVolumeStore::Register ( G4VPhysicalVolume * pVolume)
static

Adds the logical volume 'pVolume' to the collection.

Definition at line 147 of file G4PhysicalVolumeStore.cc.

148{
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}

Referenced by G4VPhysicalVolume::G4VPhysicalVolume(), and G4VPhysicalVolume::G4VPhysicalVolume().

◆ SetMapValid()

void G4PhysicalVolumeStore::SetMapValid ( G4bool val)
inline

Definition at line 121 of file G4PhysicalVolumeStore.hh.

121{ mvalid = val; }

Referenced by G4VPhysicalVolume::SetName().

◆ SetNotifier()

void G4PhysicalVolumeStore::SetNotifier ( G4VStoreNotifier * pNotifier)
static

Assigns a notifier for allocation/deallocation of the physical volumes.

Definition at line 110 of file G4PhysicalVolumeStore.cc.

111{
112 GetInstance();
113 fgNotifier = pNotifier;
114}

◆ UpdateMap()

void G4PhysicalVolumeStore::UpdateMap ( )

Brings contents of the internal map up to date and resets validity flag.

Definition at line 120 of file G4PhysicalVolumeStore.cc.

121{
122 G4AutoLock l(&mapMutex); // to avoid thread contention at initialisation
123 if (mvalid) { return; }
124 bmap.clear();
125 for(const auto & pos : *GetInstance())
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}
G4TemplateAutoLock< G4Mutex > G4AutoLock

Referenced by GetVolume(), and G4GDMLRead::StripNames().


The documentation for this class was generated from the following files: