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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 // G4tgbVolumeMgr implementation
 27 //
 28 // Author: P.Arce, CIEMAT (November 2007)
 29 // --------------------------------------------------------------------
 30 
 31 #include "G4tgbVolumeMgr.hh"
 32 
 33 #include "G4SystemOfUnits.hh"
 34 #include "G4tgbVolume.hh"
 35 #include "G4tgbMaterialMgr.hh"
 36 #include "G4tgbRotationMatrixMgr.hh"
 37 
 38 #include "G4tgrVolumeMgr.hh"
 39 #include "G4tgrFileReader.hh"
 40 #include "G4tgrUtils.hh"
 41 
 42 #include "G4VSolid.hh"
 43 #include "G4PhysicalVolumeStore.hh"
 44 #include "G4ReflectionFactory.hh"
 45 #include "G4tgrMessenger.hh"
 46 #include "G4tgbDetectorBuilder.hh"
 47 
 48 G4ThreadLocal G4tgbVolumeMgr* G4tgbVolumeMgr::theInstance = nullptr;
 49 
 50 // --------------------------------------------------------------------
 51 G4tgbVolumeMgr::G4tgbVolumeMgr()
 52 {
 53   G4ReflectionFactory::Instance()->SetScalePrecision(1.E-6 * mm);
 54   // NOTE: problems building matrices with not enough figures,
 55   // like  :ROTM RR30 0.866025 0.5 0. -0.5 0.866025 0. 0. 0 -1
 56   theDetectorBuilder = new G4tgbDetectorBuilder();
 57 }
 58 
 59 // --------------------------------------------------------------------
 60 G4tgbVolumeMgr::~G4tgbVolumeMgr()
 61 {
 62   delete theDetectorBuilder;
 63   delete theInstance;
 64 }
 65 
 66 // --------------------------------------------------------------------
 67 G4tgbVolumeMgr* G4tgbVolumeMgr::GetInstance()
 68 {
 69   if(theInstance == nullptr)
 70   {
 71     theInstance = new G4tgbVolumeMgr();
 72   }
 73   return theInstance;
 74 }
 75 
 76 // --------------------------------------------------------------------
 77 void G4tgbVolumeMgr::AddTextFile(const G4String& fname)
 78 {
 79   G4tgrFileReader::GetInstance()->AddTextFile(fname);
 80 }
 81 
 82 // --------------------------------------------------------------------
 83 G4VPhysicalVolume* G4tgbVolumeMgr::ReadAndConstructDetector()
 84 {
 85   const G4tgrVolume* tgrVoltop = theDetectorBuilder->ReadDetector();
 86   return theDetectorBuilder->ConstructDetector(tgrVoltop);
 87 }
 88 
 89 // --------------------------------------------------------------------
 90 void G4tgbVolumeMgr::RegisterMe(const G4tgbVolume* vol)
 91 {
 92   theVolumeList.insert(
 93     G4mssvol::value_type(vol->GetName(), const_cast<G4tgbVolume*>(vol)));
 94 }
 95 
 96 // --------------------------------------------------------------------
 97 void G4tgbVolumeMgr::RegisterMe(const G4VSolid* solid)
 98 {
 99   theSolids.insert(
100     G4mmssol::value_type(solid->GetName(), const_cast<G4VSolid*>(solid)));
101 }
102 
103 // --------------------------------------------------------------------
104 void G4tgbVolumeMgr::RegisterMe(const G4LogicalVolume* lv)
105 {
106   theLVs.insert(
107     G4mmslv::value_type(lv->GetName(), const_cast<G4LogicalVolume*>(lv)));
108 
109 #ifdef G4VERBOSE
110   if(G4tgrMessenger::GetVerboseLevel() >= 2)
111   {
112     G4cout << " G4tgbVolumeMgr::RegisterMe() - Logical volume registered: "
113            << lv->GetName() << G4endl;
114   }
115 #endif
116 }
117 
118 // --------------------------------------------------------------------
119 void G4tgbVolumeMgr::RegisterMe(const G4VPhysicalVolume* pv)
120 {
121   thePVs.insert(
122     G4mmspv::value_type(pv->GetName(), const_cast<G4VPhysicalVolume*>(pv)));
123 }
124 
125 // --------------------------------------------------------------------
126 void G4tgbVolumeMgr::RegisterChildParentLVs(const G4LogicalVolume* logvol,
127                                             const G4LogicalVolume* parentLV)
128 {
129   theLVInvTree[const_cast<G4LogicalVolume*>(logvol)] =
130     const_cast<G4LogicalVolume*>(parentLV);
131   theLVTree[const_cast<G4LogicalVolume*>(parentLV)] =
132     const_cast<G4LogicalVolume*>(logvol);
133 }
134 
135 // --------------------------------------------------------------------
136 void G4tgbVolumeMgr::CopyVolumes()
137 {
138   //--------- Loop G4tgbVolume's and create a G4tgbVolume for each DetUnit
139   G4mapsvol vollist = G4tgrVolumeMgr::GetInstance()->GetVolumeMap();
140   for(auto cite = vollist.cbegin(); cite != vollist.cend(); ++cite)
141   {
142     G4tgrVolume* tgrvol = const_cast<G4tgrVolume*>((*cite).second);
143     G4tgbVolume* svol   = new G4tgbVolume(tgrvol);
144     RegisterMe(svol);
145   }
146 }
147 
148 // --------------------------------------------------------------------
149 G4tgbVolume* G4tgbVolumeMgr::FindVolume(const G4String& volname)
150 {
151   G4mssvol::const_iterator cite = theVolumeList.find(volname);
152   if(cite == theVolumeList.cend())
153   {
154     G4String ErrMessage = "G4tgbVolume not found: " + volname + " !";
155     G4Exception("G4tgbVolumeMgr::FindVolume()", "InvalidSetup", FatalException,
156                 ErrMessage);
157     return nullptr;
158   }
159   else
160   {
161     return (*cite).second;
162   }
163 }
164 
165 // --------------------------------------------------------------------
166 G4VSolid* G4tgbVolumeMgr::FindG4Solid(const G4String& name)
167 {
168 #ifdef G4VERBOSE
169   if(G4tgrMessenger::GetVerboseLevel() >= 2)
170   {
171     G4cout << " G4tgbVolumeMgr::FindG4Solid() - " << name << G4endl;
172   }
173 #endif
174 
175   G4VSolid* oldSolid = nullptr;
176   std::pair<G4mmssol::iterator, G4mmssol::iterator> mmssdi;
177   mmssdi = theSolids.equal_range(name);
178 
179   if(mmssdi.first != mmssdi.second)
180   {  // check there is a solid found
181     G4mmssol::const_iterator mmsscite = mmssdi.first;
182 
183 #ifdef G4VERBOSE
184     if(G4tgrMessenger::GetVerboseLevel() >= 2)
185     {
186       G4cout << " G4tgbVolumeMgr::FindG4Solid() - Solid finding " << name
187              << G4endl;
188     }
189 #endif
190     /*
191        G4VSolid overwrites the operator== comparing the addresses
192        => this cannot be used !!
193        Then just compare solids by name =>> POSP tag cannot be used
194        for the moment ...
195          if( solid == *( (*mmsscite).second ) )
196          {
197            oldSolid = (*mmsscite).second;
198            break;
199          }
200        until we write operator== for each solid type, we take a solid
201        with the same name (therefore we will not allow two solids with
202        equal name and different parameters (POSP) )
203     */
204     oldSolid = (*mmsscite).second;
205 #ifdef G4VERBOSE
206     if(G4tgrMessenger::GetVerboseLevel() >= 1)
207     {
208       G4cout << " G4tgbVolumeMgr::FindG4Solid() - Solid already found " << name
209              << G4endl;
210     }
211 #endif
212   }
213 
214 #ifdef G4VERBOSE
215   if(G4tgrMessenger::GetVerboseLevel() >= 2)
216   {
217     G4cout << " G4tgbVolumeMgr::FindG4Solid() - Old solid: " << oldSolid
218            << G4endl;
219   }
220 #endif
221 
222   return oldSolid;
223 }
224 
225 // --------------------------------------------------------------------
226 G4LogicalVolume* G4tgbVolumeMgr::FindG4LogVol(const G4String& name,
227                                               const G4bool exists)
228 {
229   G4mmslv::const_iterator mscite = theLVs.find(name);
230   if(mscite == theLVs.cend())
231   {
232     if(exists)
233     {
234       G4String ErrMessage = "Logical volume name " + name + " not found !";
235       G4Exception("G4tgbVolumeMgr::FindG4LogVol()", "InvalidSetup",
236                   FatalException, ErrMessage);
237     }
238     return nullptr;
239   }
240   else
241   {
242     return (*mscite).second;
243   }
244 }
245 
246 // --------------------------------------------------------------------
247 G4VPhysicalVolume* G4tgbVolumeMgr::FindG4PhysVol(const G4String& name,
248                                                  const G4bool exists)
249 {
250   G4mmspv::const_iterator mscite = thePVs.find(name);
251   if(mscite == thePVs.cend())
252   {
253     if(exists)
254     {
255       G4String ErrMessage = "Physical volume name " + name + " not found !";
256       G4Exception("G4tgbVolumeMgr::FindG4PhysVol()", "InvalidSetup",
257                   FatalException, ErrMessage);
258     }
259     return nullptr;
260   }
261   else
262   {
263     return (*mscite).second;
264   }
265 }
266 
267 // --------------------------------------------------------------------
268 G4VPhysicalVolume* G4tgbVolumeMgr::GetTopPhysVol()
269 {
270   G4LogicalVolume* lv   = GetTopLogVol();
271   G4VPhysicalVolume* pv = (*(thePVs.find(lv->GetName()))).second;
272 
273 #ifdef G4VERBOSE
274   if(G4tgrMessenger::GetVerboseLevel() >= 2)
275   {
276     G4cout << " G4tgbVolumeMgr::GetTopPhysVol() - pv: " << pv->GetName()
277            << G4endl;
278   }
279 #endif
280 
281   return pv;
282 }
283 
284 // --------------------------------------------------------------------
285 G4LogicalVolume* G4tgbVolumeMgr::GetTopLogVol()
286 {
287   //----------- Start from any G4LogicalVolume, because if you go upwards
288   //            you will always end at the top
289 #ifdef G4VERBOSE
290   if(G4tgrMessenger::GetVerboseLevel() >= 2)
291   {
292     G4cout << " G4tgbVolumeMgr::GetTopLogVol theLVInvTresize "
293            << theLVInvTree.size() << G4endl;
294   }
295 #endif
296   if(theLVInvTree.size() == 0)
297   {
298     G4Exception("G4tgbVolumeMgr::GetTopLogVol()", "InvalidSetup",
299                 FatalException, "theLVInvTree has no elements.");
300   }
301   G4LogicalVolume* lv = (*(theLVInvTree.begin())).second;
302 
303   //------- if first element is the top LV, its parent is 0
304   if(lv == nullptr)
305   {
306     lv = (*(theLVInvTree.begin())).first;
307   }
308   else
309   {
310     while((*(theLVInvTree.find(lv))).second != nullptr)
311     {
312       //---------- get parent of first position
313       lv = (*(theLVInvTree.find(lv))).second;
314 #ifdef G4VERBOSE
315       if(G4tgrMessenger::GetVerboseLevel() >= 2)
316       {
317         G4cout << " G4tgbVolumeMgr::GetTopPhysVol: lv " << lv->GetName()
318                << G4endl;
319       }
320 #endif
321     }
322   }
323 
324   return lv;
325 }
326 
327 // --------------------------------------------------------------------
328 void G4tgbVolumeMgr::BuildPhysVolTree()
329 {
330   /*
331     G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
332     for( auto cite = pvs->cbegin(); cite != pvs->cend(); ++cite )
333     {
334       thePVTree[ *cite ] = (*cite)->GetMother();
335       thePVInvTree[ (*cite)->GetMother() ] = *cite;
336     }
337   */
338 }
339 
340 // --------------------------------------------------------------------
341 void G4tgbVolumeMgr::DumpSummary()
342 {
343   //---------- Dump number of objects of each class
344   G4cout << " @@@@@@@@@@@@@ Dumping Geant4 geometry objects Summary " << G4endl;
345   G4cout << " @@@ Geometry built inside world volume: "
346          << GetTopPhysVol()->GetName() << G4endl;
347   G4cout << " Number of G4VSolid's: " << theSolids.size() << G4endl;
348   G4cout << " Number of G4LogicalVolume's: " << theLVs.size() << G4endl;
349   G4cout << " Number of G4VPhysicalVolume's: " << thePVs.size() << G4endl;
350 
351   G4tgbMaterialMgr* mateMgr = G4tgbMaterialMgr::GetInstance();
352   G4cout << " Number of G4Isotope's: " << mateMgr->GetG4IsotopeList().size()
353          << G4endl;
354   G4cout << " Number of G4Element's: " << mateMgr->GetG4ElementList().size()
355          << G4endl;
356   G4cout << " Number of G4Material's: " << mateMgr->GetG4MaterialList().size()
357          << G4endl;
358 
359   G4tgbRotationMatrixMgr* rotmMgr = G4tgbRotationMatrixMgr::GetInstance();
360   G4cout << " Number of G4RotationMatrix's: "
361          << rotmMgr->GetG4RotMatList().size() << G4endl;
362 
363   //---------- Dump list of objects of each class
364   DumpG4SolidList();
365   DumpG4LogVolTree();
366   DumpG4PhysVolTree();
367 }
368 
369 // --------------------------------------------------------------------
370 void G4tgbVolumeMgr::DumpG4SolidList()
371 {
372   for(auto cite = theSolids.cbegin(); cite != theSolids.cend(); ++cite)
373   {
374     G4cout << "G4SOLID: " << (*cite).second->GetName() << " of type "
375            << (*cite).second->GetEntityType() << G4endl;
376   }
377 }
378 
379 //---------------------------------------------------------------------
380 void G4tgbVolumeMgr::DumpG4LogVolTree()
381 {
382   G4cout << " @@@@@@@@@@@@@ DUMPING G4LogicalVolume's Tree  " << G4endl;
383 
384   G4LogicalVolume* lv = GetTopLogVol();
385 
386   DumpG4LogVolLeaf(lv, 0);
387 }
388 
389 //---------------------------------------------------------------------
390 void G4tgbVolumeMgr::DumpG4LogVolLeaf(const G4LogicalVolume* lv,
391                                       unsigned int leafDepth)
392 {
393   for(std::size_t ii = 0; ii < leafDepth; ++ii)
394   {
395     G4cout << "  ";
396   }
397   G4cout << " LV:(" << leafDepth << ")" << lv->GetName() << G4endl;
398 
399   //---------- construct the children of this volume
400   // G4LogicalVolume* lvnc = const_cast<G4LogicalVolume*>(lv);
401   // std::pair<G4mlvlv::iterator, G4mlvlv::iterator> children
402   //   = theLVTree.equal_range( lvnc );
403   //
404   // G4mlvlv::iterator cite;
405 
406   ++leafDepth;
407   // for( cite = children.first; cite != children.second; ++cite )
408   // {
409   //  DumpG4LVLeaf( (*cite)->second, leafDepth );
410   // }
411 }
412 
413 // --------------------------------------------------------------------
414 void G4tgbVolumeMgr::DumpG4PhysVolTree()
415 {
416   G4cout << " @@@@@@@@@@@@@ DUMPING G4PhysicalVolume's Tree  " << G4endl;
417 
418   G4VPhysicalVolume* pv = GetTopPhysVol();
419 
420   DumpG4PhysVolLeaf(pv, 0);
421 }
422 
423 // --------------------------------------------------------------------
424 void G4tgbVolumeMgr::DumpG4PhysVolLeaf(const G4VPhysicalVolume* pv,
425                                        unsigned int leafDepth)
426 {
427   for(std::size_t ii = 0; ii < leafDepth; ++ii)
428   {
429     G4cout << "  ";
430   }
431   G4cout << " PV:(" << leafDepth << ")" << pv->GetName() << G4endl;
432 
433   //---------- construct the children of this PV
434   // G4VPhysicalVolume* pvnc = const_cast<G4VPhysicalVolume*>(pv);
435   // std::pair<G4mpvpv::iterator, G4mpvpv::iterator> children
436   //  = thePVTree.equal_range( pvnc );
437   //
438   // G4mpvpv::iterator cite;
439 
440   ++leafDepth;
441   // for( cite = children.first; cite != children.second; ++cite )
442   // {
443   //  DumpG4PVLeaf( (*cite)->second, leafDepth );
444   // }
445 }
446