Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4tgrVolumeMgr.cc
Go to the documentation of this file.
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// G4tgrVolumeMgr implementation
27//
28// Author: P.Arce, CIEMAT (November 2007)
29// --------------------------------------------------------------------
30
31#include "G4tgrVolumeMgr.hh"
32#include "G4tgrUtils.hh"
35#include "G4tgrFileReader.hh"
36#include "G4tgrMessenger.hh"
37#include "G4tgrSolid.hh"
38#include "G4tgrSolidBoolean.hh"
39
40G4ThreadLocal G4tgrVolumeMgr* G4tgrVolumeMgr::theInstance = nullptr;
41
42// --------------------------------------------------------------------
43G4tgrVolumeMgr::G4tgrVolumeMgr()
44{
45}
46
47// --------------------------------------------------------------------
48G4tgrVolumeMgr::~G4tgrVolumeMgr()
49{
50 delete theInstance;
51}
52
53// --------------------------------------------------------------------
55{
56 if(theInstance == nullptr)
57 {
58 theInstance = new G4tgrVolumeMgr;
59 }
60 return theInstance;
61}
62
63// --------------------------------------------------------------------
64G4tgrSolid* G4tgrVolumeMgr::CreateSolid(const std::vector<G4String>& wl,
65 G4bool bVOLUtag)
66{
67 G4tgrSolid* sol = FindSolid(wl[1]);
68 if(sol != nullptr)
69 {
70 G4String ErrMessage = "Solid already exists... " + wl[1];
71 G4Exception("G4tgrVolumeMgr::CreateSolid()", "InvalidSetup", FatalException,
72 ErrMessage);
73 }
74
75 std::vector<G4String> wlc = wl;
76 if(bVOLUtag)
77 {
78 wlc.pop_back();
79 }
80
81 G4String wl2 = wlc[2];
82 for(std::size_t ii = 0; ii < wl2.length(); ++ii)
83 {
84 wl2[ii] = toupper(wl2[ii]);
85 }
86 if((wl2 == "UNION") || (wl2 == "SUBTRACTION") || (wl2 == "INTERSECTION"))
87 {
88 //---------- Boolean solid
89 //---------- Create G4tgrSolidBoolean and fill the solid params
90 sol = new G4tgrSolidBoolean(wlc);
91 }
92 else
93 {
94 //---------- Create G4tgrSolidSimple and fill the solid params
95 sol = new G4tgrSolid(wlc);
96 }
97
98 return sol;
99}
100
101// --------------------------------------------------------------------
103{
104 if(theG4tgrSolidMap.find(sol->GetName()) != theG4tgrSolidMap.cend())
105 {
106 G4String ErrMessage =
107 "Cannot be two solids with the same name... " + sol->GetName();
108 G4Exception("G4tgrVolumeMgr::RegisterMe()", "InvalidSetup", FatalException,
109 ErrMessage);
110 }
111 theG4tgrSolidMap.insert(G4mapssol::value_type(sol->GetName(), sol));
112}
113
114// --------------------------------------------------------------------
116{
117 if(theG4tgrSolidMap.find(sol->GetName()) != theG4tgrSolidMap.cend())
118 {
119 G4String ErrMessage =
120 "Cannot unregister a solid that is not registered... " + sol->GetName();
121 G4Exception("G4tgrSolidMgr::unRegisterMe()", "InvalidSetup", FatalException,
122 ErrMessage);
123 }
124 else
125 {
126 theG4tgrSolidMap.erase(theG4tgrSolidMap.find(sol->GetName()));
127 }
128}
129
130// --------------------------------------------------------------------
132{
133 theG4tgrVolumeList.push_back(vol);
134 if(theG4tgrVolumeMap.find(vol->GetName()) != theG4tgrVolumeMap.cend())
135 {
136 G4String ErrMessage =
137 "Cannot be two volumes with the same name... " + vol->GetName();
138 G4Exception("G4tgrVolumeMgr::RegisterMe()", "InvalidSetup", FatalException,
139 ErrMessage);
140 }
141 theG4tgrVolumeMap.insert(G4mapsvol::value_type(vol->GetName(), vol));
142}
143
144// --------------------------------------------------------------------
146{
147 std::vector<G4tgrVolume*>::const_iterator ite;
148 for(ite = theG4tgrVolumeList.cbegin();
149 ite != theG4tgrVolumeList.cend(); ++ite)
150 {
151 if((*ite) == vol)
152 {
153 break;
154 }
155 }
156 if(ite == theG4tgrVolumeList.cend())
157 {
158 G4String ErrMessage =
159 "Cannot unregister a volume not registered... " + vol->GetName();
160 G4Exception("G4tgrVolumeMgr::unRegisterMe()", "InvalidSetup",
161 FatalException, ErrMessage);
162 }
163 else
164 {
165 theG4tgrVolumeList.erase(ite);
166 }
167 theG4tgrVolumeMap.erase(theG4tgrVolumeMap.find(vol->GetName()));
168}
169
170// --------------------------------------------------------------------
172 const G4tgrPlace* pl)
173{
174 theG4tgrVolumeTree.insert(G4mmapspl::value_type(parentName, pl));
175}
176
177// --------------------------------------------------------------------
179{
180 G4tgrSolid* vol = nullptr;
181
182 G4mapssol::const_iterator svite = theG4tgrSolidMap.find(volname);
183 if(svite == theG4tgrSolidMap.cend())
184 {
185 if(exists)
186 {
187 for(svite = theG4tgrSolidMap.cbegin();
188 svite != theG4tgrSolidMap.cend(); ++svite)
189 {
190 G4cerr << " VOL:" << (*svite).first << G4endl;
191 }
192 G4String ErrMessage = "Solid not found... " + volname;
193 G4Exception("G4tgrVolumeMgr::FindSolid()", "InvalidSetup", FatalException,
194 ErrMessage);
195 }
196 }
197 else
198 {
199 vol = const_cast<G4tgrSolid*>((*svite).second);
200 }
201
202 return vol;
203}
204
205// --------------------------------------------------------------------
207{
208 G4tgrVolume* vol = nullptr;
209
210 G4mapsvol::const_iterator svite = theG4tgrVolumeMap.find(volname);
211 if(svite == theG4tgrVolumeMap.cend())
212 {
213 if(exists)
214 {
215 for(svite = theG4tgrVolumeMap.cbegin();
216 svite != theG4tgrVolumeMap.cend(); ++svite)
217 {
218 G4cerr << " VOL:" << (*svite).first << G4endl;
219 }
220 G4String ErrMessage = "Volume not found... " + volname;
221 G4Exception("G4tgrVolumeMgr::FindVolume()", "InvalidSetup",
222 FatalException, ErrMessage);
223 }
224 else
225 {
226 G4String WarMessage = "Volume does not exists... " + volname;
227 G4Exception("G4tgrVolumeMgr::FindVolume()", "SearchFailed", JustWarning,
228 WarMessage);
229 }
230 }
231 else
232 {
233 vol = const_cast<G4tgrVolume*>((*svite).second);
234 }
235
236 return vol;
237}
238
239// --------------------------------------------------------------------
240std::vector<G4tgrVolume*> G4tgrVolumeMgr::FindVolumes(const G4String& volname,
241 G4bool exists)
242{
243 std::vector<G4tgrVolume*> vols;
244
245 G4mapsvol::const_iterator svite;
246 for(svite = theG4tgrVolumeMap.cbegin();
247 svite != theG4tgrVolumeMap.cend(); ++svite)
248 {
249 if(G4tgrUtils::AreWordsEquivalent(volname, (*svite).second->GetName()))
250 {
251 vols.push_back(const_cast<G4tgrVolume*>((*svite).second));
252 }
253 }
254
255 if(vols.size() == 0)
256 {
257 if(exists)
258 {
259 for(svite = theG4tgrVolumeMap.cbegin();
260 svite != theG4tgrVolumeMap.cend(); ++svite)
261 {
262 G4cerr << " VOL:" << (*svite).first << G4endl;
263 }
264 G4String ErrMessage = "Volume not found... " + volname;
265 G4Exception("G4tgrVolumeMgr::FindVolumes()", "InvalidSetup",
266 FatalException, ErrMessage);
267 }
268 else
269 {
270 G4String WarMessage = "Volume does not exists... " + volname;
271 G4Exception("G4tgrVolumeMgr::FindVolumes()", "SearchFailed", JustWarning,
272 WarMessage);
273 }
274 }
275
276 return vols;
277}
278
279// --------------------------------------------------------------------
281{
282 //--- Start from any G4tgrVolume and go upwards until you get to the top.
283 // Check that indeed all volumes drive to the same top volume
284
285 const G4tgrVolume* topVol = nullptr;
286 for(auto itetv = theG4tgrVolumeMap.cbegin();
287 itetv != theG4tgrVolumeMap.cend(); ++itetv)
288 {
289 const G4tgrVolume* vol = (*itetv).second;
290#ifdef G4VERBOSE
292 {
293 G4cout << " G4tgrVolumeMgr::GetTopVolume() - Vol: " << vol->GetName()
294 << " no place = " << vol->GetPlacements().size() << G4endl;
295 }
296#endif
297
298 while(vol->GetPlacements().size() != 0)
299 {
300 vol = FindVolume((*(vol->GetPlacements()).cbegin())->GetParentName(), 1);
301#ifdef G4VERBOSE
303 {
304 G4cout << " G4tgrVolumeMgr::GetTopVolume() - Vol: " << vol->GetName()
305 << " N place = " << vol->GetPlacements().size() << G4endl;
306 }
307#endif
308 }
309 if((topVol != nullptr) && (topVol != vol) &&
310 (topVol->GetType() != "VOLDivision") &&
311 (vol->GetType() != "VOLDivision"))
312 {
313 G4Exception("G4tgrVolumeMgr::GetTopVolume()",
314 "Two world volumes found, second will be taken", JustWarning,
315 (G4String("Both volumes are at the top of a hierarchy: ") +
316 topVol->GetName() + " & " + vol->GetName())
317 .c_str());
318 }
319 topVol = vol;
320 }
321
322 return topVol;
323}
324
325// --------------------------------------------------------------------
326std::pair<G4mmapspl::iterator, G4mmapspl::iterator>
328{
329 std::pair<G4mmapspl::iterator, G4mmapspl::iterator> dite;
330 dite = theG4tgrVolumeTree.equal_range(name);
331 return dite;
332}
333
334// --------------------------------------------------------------------
336{
337 G4cout << " @@@@@@@@@@@@@@@@ DUMPING G4tgrVolume's Tree " << G4endl;
338
339 const G4tgrVolume* vol = GetTopVolume();
340
341 DumpVolumeLeaf(vol, 0, 0);
342}
343
344// --------------------------------------------------------------------
345void G4tgrVolumeMgr::DumpVolumeLeaf(const G4tgrVolume* vol, unsigned int copyNo,
346 unsigned int leafDepth)
347{
348 for(std::size_t ii = 0; ii < leafDepth; ++ii)
349 {
350 G4cout << " ";
351 }
352 G4cout << " VOL:(" << leafDepth << ")" << vol->GetName() << " copy No "
353 << copyNo << G4endl;
354
355 //---------- construct the children of this VOL
356 std::pair<G4mmapspl::iterator, G4mmapspl::iterator> children =
357 GetChildren(vol->GetName());
358 G4mmapspl::const_iterator cite;
359
360 ++leafDepth;
361 for(cite = children.first; cite != children.second; ++cite)
362 {
363 //---- find G4tgrVolume pointed by G4tgrPlace
364 const G4tgrPlace* pla = (*cite).second;
365 const G4tgrVolume* volchild = pla->GetVolume();
366 //--- find copyNo
367 unsigned int cn = pla->GetCopyNo();
368 DumpVolumeLeaf(volchild, cn, leafDepth);
369 }
370}
371
372// --------------------------------------------------------------------
374{
375 //---------- Dump number of objects of each class
376 G4cout << " @@@@@@@@@@@@@@@@@@ Dumping Detector Summary " << G4endl;
377 G4cout << " @@@ Geometry built inside world volume: "
378 << GetTopVolume()->GetName() << G4endl;
379 G4cout << " Number of G4tgrVolume's: " << theG4tgrVolumeMap.size() << G4endl;
380 unsigned int nPlace = 0;
381 for(auto cite = theG4tgrVolumeMap.cbegin();
382 cite != theG4tgrVolumeMap.cend(); ++cite)
383 {
384 nPlace += ((*cite).second)->GetPlacements().size();
385 }
386 G4cout << " Number of G4tgrPlace's: " << nPlace << G4endl;
387
389 G4cout << " Number of G4tgrIsotope's: " << matef->GetIsotopeList().size()
390 << G4endl;
391 G4cout << " Number of G4tgrElement's: " << matef->GetElementList().size()
392 << G4endl;
393 G4cout << " Number of G4tgrMaterial's: " << matef->GetMaterialList().size()
394 << G4endl;
395
397 G4cout << " Number of G4tgrRotationMatrix's: "
398 << rotmf->GetRotMatList().size() << G4endl;
399
400 //---------- Dump detail list of objects of each class
402
403 matef->DumpIsotopeList();
404 matef->DumpElementList();
405 matef->DumpMaterialList();
406 rotmf->DumpRotmList();
407}
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
bool G4bool
Definition: G4Types.hh:86
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
const G4mstgrelem & GetElementList() const
const G4mstgrmate & GetMaterialList() const
static G4tgrMaterialFactory * GetInstance()
const G4mstgrisot & GetIsotopeList() const
static G4int GetVerboseLevel()
unsigned int GetCopyNo() const
Definition: G4tgrPlace.hh:54
G4tgrVolume * GetVolume() const
Definition: G4tgrPlace.hh:53
std::vector< G4tgrRotationMatrix * > GetRotMatList() const
static G4tgrRotationMatrixFactory * GetInstance()
const G4String & GetName() const
Definition: G4tgrSolid.hh:54
static G4bool AreWordsEquivalent(const G4String &word1, const G4String &word2)
Definition: G4tgrUtils.cc:653
std::vector< G4tgrVolume * > FindVolumes(const G4String &volname, G4bool exists)
G4tgrVolume * FindVolume(const G4String &volname, G4bool exists=false)
G4tgrSolid * FindSolid(const G4String &name, G4bool exists=false)
void RegisterParentChild(const G4String &parentName, const G4tgrPlace *pl)
void DumpVolumeLeaf(const G4tgrVolume *vol, unsigned int copyNo, unsigned int leafDepth)
void RegisterMe(G4tgrSolid *vol)
const G4tgrVolume * GetTopVolume()
void UnRegisterMe(G4tgrSolid *vol)
G4tgrSolid * CreateSolid(const std::vector< G4String > &wl, G4bool bVOLUtag)
static G4tgrVolumeMgr * GetInstance()
std::pair< G4mmapspl::iterator, G4mmapspl::iterator > GetChildren(const G4String &name)
const G4String & GetName() const
Definition: G4tgrVolume.hh:83
const G4String & GetType() const
Definition: G4tgrVolume.hh:85
const std::vector< G4tgrPlace * > GetPlacements() const
Definition: G4tgrVolume.hh:89
#define G4ThreadLocal
Definition: tls.hh:77