Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/geometry/management/src/G4LogicalVolume.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.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 ]

Diff markup

Differences between /geometry/management/src/G4LogicalVolume.cc (Version 11.3.0) and /geometry/management/src/G4LogicalVolume.cc (Version 10.6.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 // class G4LogicalVolume implementation            26 // class G4LogicalVolume implementation
 27 //                                                 27 //
 28 // 15.01.13 G.Cosmo, A.Dotti: Modified for thr     28 // 15.01.13 G.Cosmo, A.Dotti: Modified for thread-safety for MT
 29 // 01.03.05 G.Santin: Added flag for optional      29 // 01.03.05 G.Santin: Added flag for optional propagation of GetMass()
 30 // 17.05.02 G.Cosmo: Added flag for optional o     30 // 17.05.02 G.Cosmo: Added flag for optional optimisation
 31 // 12.02.99 S.Giani: Default initialization of     31 // 12.02.99 S.Giani: Default initialization of voxelization quality
 32 // 04.08.97 P.M.DeFreitas: Added methods for p     32 // 04.08.97 P.M.DeFreitas: Added methods for parameterised simulation 
 33 // 11.07.95 P.Kent: Initial version                33 // 11.07.95 P.Kent: Initial version
 34 // -------------------------------------------     34 // --------------------------------------------------------------------
 35                                                    35 
 36 #include "G4LogicalVolume.hh"                      36 #include "G4LogicalVolume.hh"
 37 #include "G4LogicalVolumeStore.hh"                 37 #include "G4LogicalVolumeStore.hh"
 38 #include "G4VSolid.hh"                             38 #include "G4VSolid.hh"
 39 #include "G4Material.hh"                           39 #include "G4Material.hh"
 40 #include "G4VPVParameterisation.hh"                40 #include "G4VPVParameterisation.hh"
 41 #include "G4VisAttributes.hh"                      41 #include "G4VisAttributes.hh"
 42                                                    42 
 43 #include "G4UnitsTable.hh"                         43 #include "G4UnitsTable.hh"
 44                                                    44 
 45 G4LVData::G4LVData() {;}                           45 G4LVData::G4LVData() {;}
 46                                                    46 
 47 // This new field helps to use the class G4LVM     47 // This new field helps to use the class G4LVManager
 48 //                                                 48 //
 49 G4LVManager G4LogicalVolume::subInstanceManage     49 G4LVManager G4LogicalVolume::subInstanceManager;
 50                                                    50 
 51 // These macros change the references to field     51 // These macros change the references to fields that are now encapsulated
 52 // in the class G4LVData.                          52 // in the class G4LVData.
 53 //                                                 53 //
 54 #define G4MT_solid     ((subInstanceManager.of     54 #define G4MT_solid     ((subInstanceManager.offset[instanceID]).fSolid)
 55 #define G4MT_sdetector ((subInstanceManager.of     55 #define G4MT_sdetector ((subInstanceManager.offset[instanceID]).fSensitiveDetector)
 56 #define G4MT_fmanager  ((subInstanceManager.of     56 #define G4MT_fmanager  ((subInstanceManager.offset[instanceID]).fFieldManager)
 57 #define G4MT_material  ((subInstanceManager.of     57 #define G4MT_material  ((subInstanceManager.offset[instanceID]).fMaterial)
 58 #define G4MT_mass      ((subInstanceManager.of     58 #define G4MT_mass      ((subInstanceManager.offset[instanceID]).fMass)
 59 #define G4MT_ccouple   ((subInstanceManager.of     59 #define G4MT_ccouple   ((subInstanceManager.offset[instanceID]).fCutsCouple)
 60 #define G4MT_instance  (subInstanceManager.off     60 #define G4MT_instance  (subInstanceManager.offset[instanceID])
 61                                                    61 
 62 // *******************************************     62 // ********************************************************************
 63 // Constructor - sets member data and adds to      63 // Constructor - sets member data and adds to logical Store,
 64 //               voxel pointer for optimisatio     64 //               voxel pointer for optimisation set to 0 by default.
 65 //               Initialises daughter vector t     65 //               Initialises daughter vector to 0 length.
 66 // *******************************************     66 // ********************************************************************
 67 //                                                 67 //
 68 G4LogicalVolume::G4LogicalVolume( G4VSolid* pS     68 G4LogicalVolume::G4LogicalVolume( G4VSolid* pSolid,
 69                                   G4Material*      69                                   G4Material* pMaterial,
 70                             const G4String& na     70                             const G4String& name,
 71                                   G4FieldManag     71                                   G4FieldManager* pFieldMgr,
 72                                   G4VSensitive     72                                   G4VSensitiveDetector* pSDetector,
 73                                   G4UserLimits     73                                   G4UserLimits* pULimits,
 74                                   G4bool optim     74                                   G4bool optimise )
 75  : fDaughters(0,(G4VPhysicalVolume*)nullptr),      75  : fDaughters(0,(G4VPhysicalVolume*)nullptr), fDaughtersVolumeType(kNormal),
 76    fOptimise(optimise)                             76    fOptimise(optimise)
 77 {                                                  77 {
 78   // Initialize 'Shadow'/master pointers - for     78   // Initialize 'Shadow'/master pointers - for use in copying to workers
 79   //                                               79   //
 80   fSolid = pSolid;                                 80   fSolid = pSolid;
 81   fSensitiveDetector = pSDetector;                 81   fSensitiveDetector = pSDetector;
 82   fFieldManager = pFieldMgr;                       82   fFieldManager = pFieldMgr;
 83                                                    83 
 84   instanceID = subInstanceManager.CreateSubIns     84   instanceID = subInstanceManager.CreateSubInstance();
 85   AssignFieldManager(pFieldMgr);                   85   AssignFieldManager(pFieldMgr);
 86                                                    86   
 87   G4MT_mass = 0.;                                  87   G4MT_mass = 0.;
 88   G4MT_ccouple = nullptr;                          88   G4MT_ccouple = nullptr;
 89                                                    89 
 90   SetSolid(pSolid);                                90   SetSolid(pSolid);
 91   SetMaterial(pMaterial);                          91   SetMaterial(pMaterial);
 92   SetName(name);                                   92   SetName(name);
 93   SetSensitiveDetector(pSDetector);                93   SetSensitiveDetector(pSDetector);
 94   SetUserLimits(pULimits);                         94   SetUserLimits(pULimits);    
 95                                                    95 
 96   // Initialize 'Shadow' data structure - for      96   // Initialize 'Shadow' data structure - for use by object persistency
 97   //                                               97   //
 98   lvdata = new G4LVData();                         98   lvdata = new G4LVData();
 99   lvdata->fSolid = pSolid;                         99   lvdata->fSolid = pSolid;
100   lvdata->fMaterial = pMaterial;                  100   lvdata->fMaterial = pMaterial;
101                                                   101 
102   //                                              102   //
103   // Add to store                                 103   // Add to store
104   //                                              104   //
105   G4LogicalVolumeStore::Register(this);           105   G4LogicalVolumeStore::Register(this);
106 }                                                 106 }
107                                                   107 
108 // *******************************************    108 // ********************************************************************
109 // Fake default constructor - sets only member    109 // Fake default constructor - sets only member data and allocates memory
110 //                            for usage restri    110 //                            for usage restricted to object persistency.
111 // *******************************************    111 // ********************************************************************
112 //                                                112 //
113 G4LogicalVolume::G4LogicalVolume( __void__& )     113 G4LogicalVolume::G4LogicalVolume( __void__& )
114  : fDaughters(0,(G4VPhysicalVolume*)nullptr),     114  : fDaughters(0,(G4VPhysicalVolume*)nullptr), fName("")
115 {                                                 115 {
116   instanceID = subInstanceManager.CreateSubIns    116   instanceID = subInstanceManager.CreateSubInstance();
117                                                   117   
118   SetSensitiveDetector(nullptr);    // G4MT_sd    118   SetSensitiveDetector(nullptr);    // G4MT_sdetector = nullptr;
119   SetFieldManager(nullptr, false);  // G4MT_fm    119   SetFieldManager(nullptr, false);  // G4MT_fmanager = nullptr;
120                                                   120 
121   G4MT_mass = 0.;                                 121   G4MT_mass = 0.;
122   G4MT_ccouple = nullptr;                         122   G4MT_ccouple = nullptr;
123                                                   123   
124   // Add to store                                 124   // Add to store
125   //                                              125   //
126   G4LogicalVolumeStore::Register(this);           126   G4LogicalVolumeStore::Register(this);
127 }                                                 127 }
128                                                   128 
129 // *******************************************    129 // ********************************************************************
130 // Destructor - Removes itself from solid Stor    130 // Destructor - Removes itself from solid Store
131 // NOTE: Not virtual                              131 // NOTE: Not virtual
132 // *******************************************    132 // ********************************************************************
133 //                                                133 //
134 G4LogicalVolume::~G4LogicalVolume()               134 G4LogicalVolume::~G4LogicalVolume()
135 {                                                 135 {
136   if (!fLock && fRootRegion)  // De-register r    136   if (!fLock && fRootRegion)  // De-register root region first if not locked
137   {                           // and flagged a    137   {                           // and flagged as root logical-volume
138     fRegion->RemoveRootLogicalVolume(this, tru    138     fRegion->RemoveRootLogicalVolume(this, true);
139   }                                               139   }
140   delete lvdata;                                  140   delete lvdata;
141   G4LogicalVolumeStore::DeRegister(this);         141   G4LogicalVolumeStore::DeRegister(this);
142 }                                                 142 }
143                                                   143 
144 // *******************************************    144 // ********************************************************************
145 // SetName - Set volume name and notify store  << 
146 // ******************************************* << 
147 //                                             << 
148 void G4LogicalVolume::SetName(const G4String&  << 
149 {                                              << 
150   fName = pName;                               << 
151   G4LogicalVolumeStore::GetInstance()->SetMapV << 
152 }                                              << 
153                                                << 
154 // ******************************************* << 
155 // InitialiseWorker                               145 // InitialiseWorker
156 //                                                146 //
157 // This method is similar to the constructor.     147 // This method is similar to the constructor. It is used by each worker
158 // thread to achieve the same effect as that o    148 // thread to achieve the same effect as that of the master thread exept
159 // to register the new created instance. This     149 // to register the new created instance. This method is invoked explicitly.
160 // It does not create a new G4LogicalVolume in    150 // It does not create a new G4LogicalVolume instance. It only assign the value
161 // for the fields encapsulated by the class G4    151 // for the fields encapsulated by the class G4LVData.
162 // *******************************************    152 // ********************************************************************
163 //                                                153 //
164 void G4LogicalVolume::                            154 void G4LogicalVolume::
165 InitialiseWorker( G4LogicalVolume* /*pMasterOb    155 InitialiseWorker( G4LogicalVolume* /*pMasterObject*/,
166                   G4VSolid* pSolid,               156                   G4VSolid* pSolid,
167                   G4VSensitiveDetector* pSDete    157                   G4VSensitiveDetector* pSDetector)
168 {                                                 158 {
169   subInstanceManager.SlaveCopySubInstanceArray    159   subInstanceManager.SlaveCopySubInstanceArray();
170                                                   160 
171   SetSolid(pSolid);                               161   SetSolid(pSolid);
172   SetSensitiveDetector(pSDetector); //  How th    162   SetSensitiveDetector(pSDetector); //  How this object is available now ?
173   AssignFieldManager(fFieldManager);              163   AssignFieldManager(fFieldManager);
174    // Should be set - but a per-thread copy is    164    // Should be set - but a per-thread copy is not available yet
175    // Must not call SetFieldManager(), which p    165    // Must not call SetFieldManager(), which propagates FieldMgr
176                                                   166 
177 #ifdef CLONE_FIELD_MGR                            167 #ifdef CLONE_FIELD_MGR
178   // Create a field FieldManager by cloning       168   // Create a field FieldManager by cloning
179   //                                              169   //
180   G4FieldManager workerFldMgr = fFieldManager-    170   G4FieldManager workerFldMgr = fFieldManager->GetWorkerClone(G4bool* created);
181   if( created || (GetFieldManager() != workerF    171   if( created || (GetFieldManager() != workerFldMgr) )
182   {                                               172   {
183     SetFieldManager(fFieldManager, false); //     173     SetFieldManager(fFieldManager, false); // which propagates FieldMgr
184   }                                               174   }
185   else                                            175   else
186   {                                               176   {
187     // Field manager existed and is equal to c    177     // Field manager existed and is equal to current one
188     //                                            178     //
189     AssignFieldManager(workerFldMgr);             179     AssignFieldManager(workerFldMgr);
190   }                                               180   }
191 #endif                                            181 #endif
192 }                                                 182 }
193                                                   183 
194 // *******************************************    184 // ********************************************************************
195 // Clean                                          185 // Clean
196 // *******************************************    186 // ********************************************************************
197 //                                                187 //
198 void G4LogicalVolume::Clean()                     188 void G4LogicalVolume::Clean()
199 {                                                 189 {
200   subInstanceManager.FreeSlave();                 190   subInstanceManager.FreeSlave();
201 }                                                 191 }
202                                                   192 
203 // *******************************************    193 // ********************************************************************
204 // TerminateWorker                                194 // TerminateWorker
205 //                                                195 //
206 // This method is similar to the destructor. I    196 // This method is similar to the destructor. It is used by each worker
207 // thread to achieve the partial effect as tha    197 // thread to achieve the partial effect as that of the master thread.
208 // For G4LogicalVolume instances, nothing more    198 // For G4LogicalVolume instances, nothing more to do here.
209 // *******************************************    199 // ********************************************************************
210 //                                                200 //
211 void G4LogicalVolume::                            201 void G4LogicalVolume::
212 TerminateWorker( G4LogicalVolume* /*pMasterObj    202 TerminateWorker( G4LogicalVolume* /*pMasterObject*/)
213 {                                                 203 {
214 }                                                 204 }
215                                                   205 
216 // *******************************************    206 // ********************************************************************
217 // GetSubInstanceManager                          207 // GetSubInstanceManager
218 //                                                208 //
219 // Returns the private data instance manager.     209 // Returns the private data instance manager.    
220 // *******************************************    210 // ********************************************************************
221 //                                                211 //
222 const G4LVManager& G4LogicalVolume::GetSubInst    212 const G4LVManager& G4LogicalVolume::GetSubInstanceManager()
223 {                                                 213 {
224   return subInstanceManager;                      214   return subInstanceManager;
225 }                                                 215 }
226                                                   216 
227 // *******************************************    217 // ********************************************************************
228 // GetFieldManager                                218 // GetFieldManager
229 // *******************************************    219 // ********************************************************************
230 //                                                220 //
231 G4FieldManager* G4LogicalVolume::GetFieldManag    221 G4FieldManager* G4LogicalVolume::GetFieldManager() const
232 {                                                 222 {
233   return G4MT_fmanager;                           223   return G4MT_fmanager;
234 }                                                 224 }
235                                                   225 
236 // *******************************************    226 // ********************************************************************
237 // AssignFieldManager                             227 // AssignFieldManager
238 // *******************************************    228 // ********************************************************************
239 //                                                229 //
240 void G4LogicalVolume::AssignFieldManager( G4Fi    230 void G4LogicalVolume::AssignFieldManager( G4FieldManager *fldMgr)
241 {                                                 231 {
242   G4MT_fmanager= fldMgr;                          232   G4MT_fmanager= fldMgr;
243   if(G4Threading::IsMasterThread())  { fFieldM    233   if(G4Threading::IsMasterThread())  { fFieldManager = fldMgr; }
244 }                                                 234 }
245                                                   235 
246 // *******************************************    236 // ********************************************************************
247 // IsExtended                                     237 // IsExtended
248 // *******************************************    238 // ********************************************************************
249 //                                                239 //
250 G4bool G4LogicalVolume::IsExtended() const        240 G4bool G4LogicalVolume::IsExtended() const
251 {                                                 241 {
252   return false;                                   242   return false;
253 }                                                 243 }
254                                                   244 
255 // *******************************************    245 // ********************************************************************
256 // SetFieldManager                                246 // SetFieldManager
257 // *******************************************    247 // ********************************************************************
258 //                                                248 //
259 void                                              249 void
260 G4LogicalVolume::SetFieldManager(G4FieldManage    250 G4LogicalVolume::SetFieldManager(G4FieldManager* pNewFieldMgr,
261                                  G4bool           251                                  G4bool          forceAllDaughters) 
262 {                                                 252 {
263   AssignFieldManager(pNewFieldMgr);               253   AssignFieldManager(pNewFieldMgr);
264                                                   254 
265   auto NoDaughters = GetNoDaughters();            255   auto NoDaughters = GetNoDaughters();
266   while ( (NoDaughters--)>0 )                     256   while ( (NoDaughters--)>0 )
267   {                                               257   {
268     G4LogicalVolume* DaughterLogVol;              258     G4LogicalVolume* DaughterLogVol; 
269     DaughterLogVol = GetDaughter(NoDaughters)-    259     DaughterLogVol = GetDaughter(NoDaughters)->GetLogicalVolume();
270     if ( forceAllDaughters || (DaughterLogVol-    260     if ( forceAllDaughters || (DaughterLogVol->GetFieldManager() == nullptr) )
271     {                                             261     {
272       DaughterLogVol->SetFieldManager(pNewFiel    262       DaughterLogVol->SetFieldManager(pNewFieldMgr, forceAllDaughters);
273     }                                             263     }
274   }                                               264   }
275 }                                                 265 }
276                                                   266 
277 // *******************************************    267 // ********************************************************************
278 // AddDaughter                                    268 // AddDaughter
279 // *******************************************    269 // ********************************************************************
280 //                                                270 //
281 void G4LogicalVolume::AddDaughter(G4VPhysicalV    271 void G4LogicalVolume::AddDaughter(G4VPhysicalVolume* pNewDaughter)
282 {                                                 272 {
283   EVolume daughterType = pNewDaughter->VolumeT    273   EVolume daughterType = pNewDaughter->VolumeType();
284                                                   274 
285   // The type of the navigation needed is dete    275   // The type of the navigation needed is determined by the first daughter
286   //                                              276   //
287   if( fDaughters.empty() )                        277   if( fDaughters.empty() )
288   {                                               278   {
289     fDaughtersVolumeType = daughterType;          279     fDaughtersVolumeType = daughterType;
290   }                                               280   }
291   else                                            281   else
292   {                                               282   {
293     // Check consistency of detector descripti    283     // Check consistency of detector description
294                                                   284 
295     // 1. A replica or parameterised volume ca    285     // 1. A replica or parameterised volume can have only one daughter
296     //                                            286     //
297     if( fDaughters[0]->IsReplicated() )           287     if( fDaughters[0]->IsReplicated() )
298     {                                             288     {
299       std::ostringstream message;                 289       std::ostringstream message;
300       message << "ERROR - Attempt to place a v    290       message << "ERROR - Attempt to place a volume in a mother volume"
301         << G4endl                                 291         << G4endl
302         << "        already containing a repli    292         << "        already containing a replicated volume." << G4endl
303         << "        A volume can either contai    293         << "        A volume can either contain several placements" << G4endl
304         << "        or a unique replica or par    294         << "        or a unique replica or parameterised volume !" << G4endl
305         << "           Mother logical volume:     295         << "           Mother logical volume: " << GetName() << G4endl
306         << "           Placing volume: " << pN    296         << "           Placing volume: " << pNewDaughter->GetName()
307         << G4endl;                                297         << G4endl;
308       G4Exception("G4LogicalVolume::AddDaughte    298       G4Exception("G4LogicalVolume::AddDaughter()", "GeomMgt0002",
309                   FatalException, message,        299                   FatalException, message,
310                   "Replica or parameterised vo    300                   "Replica or parameterised volume must be the only daughter!");
311     }                                             301     }
312     else                                          302     else
313     {                                             303     {
314       // 2. Ensure that Placement and External    304       // 2. Ensure that Placement and External physical volumes do not mix
315       //                                          305       //
316       if(  daughterType != fDaughtersVolumeTyp    306       if(  daughterType != fDaughtersVolumeType )
317       {                                           307       {
318         std::ostringstream message;               308         std::ostringstream message;
319         message << "ERROR - Attempt to place a    309         message << "ERROR - Attempt to place a volume in a mother volume"
320           << G4endl                               310           << G4endl
321           << "        already containing a dif    311           << "        already containing a different type of volume." << G4endl
322           << "        A volume can either cont    312           << "        A volume can either contain" << G4endl
323           << "        - one or more placements    313           << "        - one or more placements, OR" << G4endl
324           << "        - one or more 'external'    314           << "        - one or more 'external' type physical volumes." << G4endl
325           << "          Mother logical volume:    315           << "          Mother logical volume: " << GetName() << G4endl
326           << "          Volume being placed: "    316           << "          Volume being placed: " << pNewDaughter->GetName()
327           << G4endl;                              317           << G4endl;
328         G4Exception("G4LogicalVolume::AddDaugh    318         G4Exception("G4LogicalVolume::AddDaughter()", "GeomMgt0002",
329                     FatalException, message,      319                     FatalException, message,
330                     "Cannot mix placements and    320                     "Cannot mix placements and external physical volumes !");
331       }                                           321       }
332     }                                             322     }
333   }                                               323   }
334                                                   324 
335   // Invalidate previous calculation of mass -    325   // Invalidate previous calculation of mass - if any - for all threads
336   //                                              326   //
337   G4MT_mass = 0.;                                 327   G4MT_mass = 0.;
338   fDaughters.push_back(pNewDaughter);             328   fDaughters.push_back(pNewDaughter);
339                                                   329 
340   G4LogicalVolume* pDaughterLogical = pNewDaug    330   G4LogicalVolume* pDaughterLogical = pNewDaughter->GetLogicalVolume();
341                                                   331 
342   // Propagate the Field Manager, if the daugh    332   // Propagate the Field Manager, if the daughter has no field Manager
343   //                                              333   //
344   G4FieldManager* pDaughterFieldManager = pDau    334   G4FieldManager* pDaughterFieldManager = pDaughterLogical->GetFieldManager();
345                                                   335 
346   // Avoid propagating the fieldManager pointe    336   // Avoid propagating the fieldManager pointer if null
347   // and daughter's one is null as well...        337   // and daughter's one is null as well...
348   //                                              338   // 
349   if( (G4MT_fmanager != nullptr ) && (pDaughte    339   if( (G4MT_fmanager != nullptr ) && (pDaughterFieldManager == nullptr) )
350   {                                               340   {
351     pDaughterLogical->SetFieldManager(G4MT_fma    341     pDaughterLogical->SetFieldManager(G4MT_fmanager, false);
352   }                                               342   }
353   if (fRegion != nullptr)                      << 343   if (fRegion)
354   {                                               344   {
355     PropagateRegion();                            345     PropagateRegion();
356     fRegion->RegionModified(true);                346     fRegion->RegionModified(true);
357   }                                               347   }
358 }                                                 348 }
359                                                   349 
360 // *******************************************    350 // ********************************************************************
361 // RemoveDaughter                                 351 // RemoveDaughter
362 // *******************************************    352 // ********************************************************************
363 //                                                353 //
364 void G4LogicalVolume::RemoveDaughter(const G4V    354 void G4LogicalVolume::RemoveDaughter(const G4VPhysicalVolume* p)
365 {                                                 355 {
366   for (auto i=fDaughters.cbegin(); i!=fDaughte    356   for (auto i=fDaughters.cbegin(); i!=fDaughters.cend(); ++i )
367   {                                               357   {
368     if (**i==*p)                                  358     if (**i==*p)
369     {                                             359     {
370       fDaughters.erase(i);                        360       fDaughters.erase(i);
371       break;                                      361       break;
372     }                                             362     }
373   }                                               363   }
374   if (fRegion != nullptr)                      << 364   if (fRegion)
375   {                                               365   {
376     fRegion->RegionModified(true);                366     fRegion->RegionModified(true);
377   }                                               367   }
378   G4MT_mass = 0.;                                 368   G4MT_mass = 0.;
379 }                                                 369 }
380                                                   370 
381 // *******************************************    371 // ********************************************************************
382 // ClearDaughters                                 372 // ClearDaughters
383 // *******************************************    373 // ********************************************************************
384 //                                                374 //
385 void G4LogicalVolume::ClearDaughters()            375 void G4LogicalVolume::ClearDaughters()
386 {                                                 376 {
387   fDaughters.erase(fDaughters.cbegin(), fDaugh    377   fDaughters.erase(fDaughters.cbegin(), fDaughters.cend());
388   if (fRegion != nullptr)                         378   if (fRegion != nullptr)
389   {                                               379   {
390     fRegion->RegionModified(true);                380     fRegion->RegionModified(true);
391   }                                               381   }
392   G4MT_mass = 0.;                                 382   G4MT_mass = 0.;
393 }                                                 383 }
394                                                   384 
395 // *******************************************    385 // ********************************************************************
396 // ResetMass                                      386 // ResetMass
397 // *******************************************    387 // ********************************************************************
398 //                                                388 //
399 void G4LogicalVolume::ResetMass()                 389 void G4LogicalVolume::ResetMass()
400 {                                                 390 {
401   G4MT_mass= 0.0;                                 391   G4MT_mass= 0.0;
402 }                                                 392 }
403                                                   393 
404 // *******************************************    394 // ********************************************************************
405 // GetSolid                                       395 // GetSolid
406 // *******************************************    396 // ********************************************************************
407 //                                                397 //
408 G4VSolid* G4LogicalVolume::GetSolid(G4LVData &    398 G4VSolid* G4LogicalVolume::GetSolid(G4LVData &instLVdata) // const
409 {                                                 399 {
410   return instLVdata.fSolid;                       400   return instLVdata.fSolid;
411 }                                                 401 }
412                                                   402 
413 G4VSolid* G4LogicalVolume::GetSolid() const       403 G4VSolid* G4LogicalVolume::GetSolid() const
414 {                                                 404 {
415   return this->GetSolid( subInstanceManager.of    405   return this->GetSolid( subInstanceManager.offset[instanceID] ); 
416 }                                                 406 }
417                                                   407 
418 // *******************************************    408 // ********************************************************************
419 // SetSolid                                       409 // SetSolid
420 // *******************************************    410 // ********************************************************************
421 //                                                411 //
422 void G4LogicalVolume::SetSolid(G4VSolid *pSoli    412 void G4LogicalVolume::SetSolid(G4VSolid *pSolid)
423 {                                                 413 {
424                                                   414 
425   G4MT_solid = pSolid;                            415   G4MT_solid = pSolid;
426   this->ResetMass();                              416   this->ResetMass(); 
427 }                                                 417 }
428                                                   418 
429 void G4LogicalVolume::SetSolid(G4LVData& instL    419 void G4LogicalVolume::SetSolid(G4LVData& instLVdata, G4VSolid* pSolid)
430 {                                                 420 {  
431   instLVdata.fSolid = pSolid;                     421   instLVdata.fSolid = pSolid;
432   instLVdata.fMass = 0.0;                         422   instLVdata.fMass = 0.0;
433 }                                                 423 }
434                                                   424 
435 // *******************************************    425 // ********************************************************************
436 // GetMaterial                                    426 // GetMaterial
437 // *******************************************    427 // ********************************************************************
438 //                                                428 //
439 G4Material* G4LogicalVolume::GetMaterial() con    429 G4Material* G4LogicalVolume::GetMaterial() const
440 {                                                 430 {
441   return G4MT_material;                           431   return G4MT_material;
442 }                                                 432 }
443                                                   433 
444 // *******************************************    434 // ********************************************************************
445 // SetMaterial                                    435 // SetMaterial
446 // *******************************************    436 // ********************************************************************
447 //                                                437 //
448 void G4LogicalVolume::SetMaterial(G4Material*     438 void G4LogicalVolume::SetMaterial(G4Material* pMaterial)
449 {                                                 439 {
450   G4MT_material = pMaterial;                      440   G4MT_material = pMaterial;
451   G4MT_mass = 0.0;                                441   G4MT_mass = 0.0;
452 }                                                 442 }
453                                                   443 
454 // *******************************************    444 // ********************************************************************
455 // UpdateMaterial                                 445 // UpdateMaterial
456 // *******************************************    446 // ********************************************************************
457 //                                                447 //
458 void G4LogicalVolume::UpdateMaterial(G4Materia    448 void G4LogicalVolume::UpdateMaterial(G4Material* pMaterial)
459 {                                                 449 {
460   G4MT_material=pMaterial;                        450   G4MT_material=pMaterial;
461   if (fRegion != nullptr) { G4MT_ccouple = fRe    451   if (fRegion != nullptr) { G4MT_ccouple = fRegion->FindCouple(pMaterial); }
462   G4MT_mass = 0.0;                                452   G4MT_mass = 0.0;
463 }                                                 453 }
464                                                   454 
465 // *******************************************    455 // ********************************************************************
466 // GetSensitiveDetector                           456 // GetSensitiveDetector
467 // *******************************************    457 // ********************************************************************
468 //                                                458 //
469 G4VSensitiveDetector* G4LogicalVolume::GetSens    459 G4VSensitiveDetector* G4LogicalVolume::GetSensitiveDetector() const
470 {                                                 460 {
471   return G4MT_sdetector;                          461   return G4MT_sdetector;
472 }                                                 462 }
473                                                   463 
474 // *******************************************    464 // ********************************************************************
475 // SetSensitiveDetector                           465 // SetSensitiveDetector
476 // *******************************************    466 // ********************************************************************
477 //                                                467 //
478 void G4LogicalVolume::SetSensitiveDetector(G4V    468 void G4LogicalVolume::SetSensitiveDetector(G4VSensitiveDetector* pSDetector)
479 {                                                 469 {
480   G4MT_sdetector = pSDetector;                    470   G4MT_sdetector = pSDetector;
481   if (G4Threading::IsMasterThread())  { fSensi    471   if (G4Threading::IsMasterThread())  { fSensitiveDetector = pSDetector; }
482 }                                                 472 }
483                                                   473 
484 // *******************************************    474 // ********************************************************************
485 // GetMaterialCutsCouple                          475 // GetMaterialCutsCouple
486 // *******************************************    476 // ********************************************************************
487 //                                                477 //
488 const G4MaterialCutsCouple* G4LogicalVolume::G    478 const G4MaterialCutsCouple* G4LogicalVolume::GetMaterialCutsCouple() const
489 {                                                 479 {
490   return G4MT_ccouple;                            480   return G4MT_ccouple;
491 }                                                 481 }
492                                                   482 
493 // *******************************************    483 // ********************************************************************
494 // SetMaterialCutsCouple                          484 // SetMaterialCutsCouple
495 // *******************************************    485 // ********************************************************************
496 //                                                486 //
497 void G4LogicalVolume::SetMaterialCutsCouple(G4    487 void G4LogicalVolume::SetMaterialCutsCouple(G4MaterialCutsCouple* cuts)
498 {                                                 488 {
499   G4MT_ccouple = cuts;                            489   G4MT_ccouple = cuts;
500 }                                                 490 }
501                                                   491 
502 // *******************************************    492 // ********************************************************************
503 // IsAncestor                                     493 // IsAncestor
504 //                                                494 //
505 // Finds out if the current logical volume is     495 // Finds out if the current logical volume is an ancestor of a given 
506 // physical volume                                496 // physical volume
507 // *******************************************    497 // ********************************************************************
508 //                                                498 //
509 G4bool                                            499 G4bool
510 G4LogicalVolume::IsAncestor(const G4VPhysicalV    500 G4LogicalVolume::IsAncestor(const G4VPhysicalVolume* aVolume) const
511 {                                                 501 {
512   G4bool isDaughter = IsDaughter(aVolume);        502   G4bool isDaughter = IsDaughter(aVolume);
513   if (!isDaughter)                                503   if (!isDaughter)
514   {                                               504   {
515     for (auto itDau = fDaughters.cbegin(); itD    505     for (auto itDau = fDaughters.cbegin(); itDau != fDaughters.cend(); ++itDau)
516     {                                             506     {
517       isDaughter = (*itDau)->GetLogicalVolume(    507       isDaughter = (*itDau)->GetLogicalVolume()->IsAncestor(aVolume);
518       if (isDaughter)  break;                     508       if (isDaughter)  break;
519     }                                             509     }
520   }                                               510   }
521   return isDaughter;                              511   return isDaughter;
522 }                                                 512 }
523                                                   513 
524 // *******************************************    514 // ********************************************************************
525 // TotalVolumeEntities                            515 // TotalVolumeEntities
526 //                                                516 //
527 // Returns the total number of physical volume    517 // Returns the total number of physical volumes (replicated or placed)
528 // in the tree represented by the current logi    518 // in the tree represented by the current logical volume.
529 // *******************************************    519 // ********************************************************************
530 //                                                520 //
531 G4int G4LogicalVolume::TotalVolumeEntities() c    521 G4int G4LogicalVolume::TotalVolumeEntities() const
532 {                                                 522 {
533   G4int vols = 1;                                 523   G4int vols = 1;
534   for (auto itDau = fDaughters.cbegin(); itDau    524   for (auto itDau = fDaughters.cbegin(); itDau != fDaughters.cend(); ++itDau)
535   {                                               525   {
536     G4VPhysicalVolume* physDaughter = (*itDau)    526     G4VPhysicalVolume* physDaughter = (*itDau);
537     vols += physDaughter->GetMultiplicity()       527     vols += physDaughter->GetMultiplicity()
538            *physDaughter->GetLogicalVolume()->    528            *physDaughter->GetLogicalVolume()->TotalVolumeEntities();
539   }                                               529   }
540   return vols;                                    530   return vols;
541 }                                                 531 }
542                                                   532 
543 // *******************************************    533 // ********************************************************************
544 // GetMass                                        534 // GetMass
545 //                                                535 //
546 // Returns the mass of the logical volume tree    536 // Returns the mass of the logical volume tree computed from the
547 // estimated geometrical volume of each solid     537 // estimated geometrical volume of each solid and material associated
548 // to the logical volume and its daughters.       538 // to the logical volume and its daughters.
549 // NOTE: the computation may require considera    539 // NOTE: the computation may require considerable amount of time,
550 //       depending from the complexity of the     540 //       depending from the complexity of the geometry tree.
551 //       The returned value is cached and can     541 //       The returned value is cached and can be used for successive
552 //       calls (default), unless recomputation    542 //       calls (default), unless recomputation is forced by providing
553 //       'true' for the boolean argument in in    543 //       'true' for the boolean argument in input. Computation should
554 //       be forced if the geometry setup has c    544 //       be forced if the geometry setup has changed after the previous
555 //       call. By setting the 'propagate' bool    545 //       call. By setting the 'propagate' boolean flag to 'false' the 
556 //       method returns the mass of the presen    546 //       method returns the mass of the present logical volume only 
557 //       (subtracted for the volume occupied b    547 //       (subtracted for the volume occupied by the daughter volumes).
558 //       The extra argument 'parMaterial' is i    548 //       The extra argument 'parMaterial' is internally used to
559 //       consider cases of geometrical paramet    549 //       consider cases of geometrical parameterisations by material.
560 // *******************************************    550 // ********************************************************************
561 //                                                551 //
562 G4double G4LogicalVolume::GetMass(G4bool force    552 G4double G4LogicalVolume::GetMass(G4bool forced,
563                                   G4bool propa    553                                   G4bool propagate,
564                                   G4Material*     554                                   G4Material* parMaterial)
565 {                                                 555 {
566   // Return the cached non-zero value, if not     556   // Return the cached non-zero value, if not forced
567   //                                              557   //
568   if ( ((G4MT_mass) != 0.0) && (!forced) )  {  << 558   if ( (G4MT_mass) && (!forced) )  { return G4MT_mass; }
569                                                   559 
570   // Global density and computed mass associat    560   // Global density and computed mass associated to the logical
571   // volume without considering its daughters     561   // volume without considering its daughters
572   //                                              562   //
573   G4Material* logMaterial = parMaterial != nul << 563   G4Material* logMaterial = parMaterial ? parMaterial : GetMaterial();
574   if (logMaterial == nullptr)                     564   if (logMaterial == nullptr)
575   {                                               565   {
576     std::ostringstream message;                   566     std::ostringstream message;
577     message << "No material associated to the     567     message << "No material associated to the logical volume: "
578             << fName << " !" << G4endl            568             << fName << " !" << G4endl
579             << "Sorry, cannot compute the mass    569             << "Sorry, cannot compute the mass ...";
580     G4Exception("G4LogicalVolume::GetMass()",     570     G4Exception("G4LogicalVolume::GetMass()", "GeomMgt0002",
581                 FatalException, message);         571                 FatalException, message);
582     return 0.0;                                   572     return 0.0;
583   }                                               573   }
584   if ( GetSolid() == nullptr )                    574   if ( GetSolid() == nullptr )
585   {                                               575   {
586     std::ostringstream message;                   576     std::ostringstream message;
587     message << "No solid is associated to the     577     message << "No solid is associated to the logical volume: "
588             << fName << " !" << G4endl            578             << fName << " !" << G4endl
589             << "Sorry, cannot compute the mass    579             << "Sorry, cannot compute the mass ...";
590     G4Exception("G4LogicalVolume::GetMass()",     580     G4Exception("G4LogicalVolume::GetMass()", "GeomMgt0002",
591                 FatalException, message);         581                 FatalException, message);
592     return 0.0;                                   582     return 0.0;
593   }                                               583   }
594   G4double globalDensity = logMaterial->GetDen    584   G4double globalDensity = logMaterial->GetDensity();
595   G4double motherMass = GetSolid()->GetCubicVo    585   G4double motherMass = GetSolid()->GetCubicVolume() * globalDensity;
596   G4double massSum = motherMass;                  586   G4double massSum = motherMass;
597                                                   587   
598   // For each daughter in the tree, subtract t    588   // For each daughter in the tree, subtract the mass occupied
599   // and if required by the propagate flag, ad    589   // and if required by the propagate flag, add the real daughter's
600   // one computed recursively                     590   // one computed recursively
601                                                   591 
602   for (auto itDau = fDaughters.cbegin(); itDau    592   for (auto itDau = fDaughters.cbegin(); itDau != fDaughters.cend(); ++itDau)
603   {                                               593   {
604     G4VPhysicalVolume* physDaughter = (*itDau)    594     G4VPhysicalVolume* physDaughter = (*itDau);
605     G4LogicalVolume* logDaughter = physDaughte    595     G4LogicalVolume* logDaughter = physDaughter->GetLogicalVolume();
606     G4double subMass = 0.0;                       596     G4double subMass = 0.0;
607     G4VSolid* daughterSolid = nullptr;            597     G4VSolid* daughterSolid = nullptr;
608     G4Material* daughterMaterial = nullptr;       598     G4Material* daughterMaterial = nullptr;
609                                                   599 
610     // Compute the mass to subtract and to add    600     // Compute the mass to subtract and to add for each daughter
611     // considering its multiplicity (i.e. repl    601     // considering its multiplicity (i.e. replicated or not) and
612     // eventually its parameterisation (by sol    602     // eventually its parameterisation (by solid and/or by material)
613     //                                            603     //
614     for (auto i=0; i<physDaughter->GetMultipli    604     for (auto i=0; i<physDaughter->GetMultiplicity(); ++i)
615     {                                             605     {
616       G4VPVParameterisation* physParam = physD    606       G4VPVParameterisation* physParam = physDaughter->GetParameterisation();
617       if (physParam != nullptr)                << 607       if (physParam)
618       {                                           608       {
619         daughterSolid = physParam->ComputeSoli    609         daughterSolid = physParam->ComputeSolid(i, physDaughter);
620         daughterSolid->ComputeDimensions(physP    610         daughterSolid->ComputeDimensions(physParam, i, physDaughter);
621         daughterMaterial = physParam->ComputeM    611         daughterMaterial = physParam->ComputeMaterial(i, physDaughter);
622       }                                           612       }
623       else                                        613       else
624       {                                           614       {
625         daughterSolid = logDaughter->GetSolid(    615         daughterSolid = logDaughter->GetSolid();
626         daughterMaterial = logDaughter->GetMat    616         daughterMaterial = logDaughter->GetMaterial();
627       }                                           617       }
628       subMass = daughterSolid->GetCubicVolume(    618       subMass = daughterSolid->GetCubicVolume() * globalDensity;
629                                                   619 
630       // Subtract the daughter's portion for t    620       // Subtract the daughter's portion for the mass and, if required,
631       // add the real daughter's mass computed    621       // add the real daughter's mass computed recursively
632       //                                          622       //
633       massSum -= subMass;                         623       massSum -= subMass;
634       if (propagate)                              624       if (propagate)
635       {                                           625       {
636         massSum += logDaughter->GetMass(true,     626         massSum += logDaughter->GetMass(true, true, daughterMaterial);
637       }                                           627       }
638     }                                             628     }
639   }                                               629   }
640   G4MT_mass = massSum;                            630   G4MT_mass = massSum;
641   return massSum;                                 631   return massSum;
642 }                                                 632 }
643                                                   633 
                                                   >> 634 void G4LogicalVolume::SetVisAttributes (const G4VisAttributes& VA)
                                                   >> 635 {
                                                   >> 636   fVisAttributes = new G4VisAttributes(VA);
                                                   >> 637 }
                                                   >> 638 
644 // *******************************************    639 // ********************************************************************
645 // Change the daughters volume type -- checkin    640 // Change the daughters volume type -- checking proposed values
646 //                                                641 //
647 //  Undertakes primitive checking, to ensure t    642 //  Undertakes primitive checking, to ensure that only 'legal' changes
648 //  are made:                                     643 //  are made:
649 //    - any type to 'external'  ( user respons    644 //    - any type to 'external'  ( user responsibility )
650 //    - the type proposed is checked against t    645 //    - the type proposed is checked against the deduced type
651 //       (for potential switch back to 'intern    646 //       (for potential switch back to 'internal' type.)
652 //  Returns success (true) or failure (false)     647 //  Returns success (true) or failure (false)
653 //                                                648 //
654 G4bool G4LogicalVolume::ChangeDaughtersType(EV    649 G4bool G4LogicalVolume::ChangeDaughtersType(EVolume aType)
655 {                                                 650 {
656   G4bool works = false;                           651   G4bool works = false;
657   if( aType == kExternal )                        652   if( aType == kExternal )
658   {                                               653   {
659     // It is the responsibility of External Na    654     // It is the responsibility of External Navigator to handle types selected
660     //                                            655     //
661     fDaughtersVolumeType = aType;                 656     fDaughtersVolumeType = aType;
662     works = true;                                 657     works = true;
663   }                                               658   }
664   else                                            659   else
665   {                                               660   {
666     EVolume expectedVType = DeduceDaughtersTyp    661     EVolume expectedVType = DeduceDaughtersType();
667     works = (expectedVType == aType);             662     works = (expectedVType == aType);
668     if ( works )                                  663     if ( works )
669     {                                             664     {
670       fDaughtersVolumeType = aType;               665       fDaughtersVolumeType = aType;
671     }                                             666     }
672   }                                               667   }
673   return works;                                   668   return works;
674 }                                              << 
675                                                << 
676 // ******************************************* << 
677 // SetVisAttributes - copy version             << 
678 // ******************************************* << 
679 //                                             << 
680 void G4LogicalVolume::SetVisAttributes (const  << 
681 {                                              << 
682   if (G4Threading::IsWorkerThread()) return;   << 
683   fVisAttributes = std::make_shared<const G4Vi << 
684 }                                              << 
685                                                << 
686 // ******************************************* << 
687 // SetVisAttributes                            << 
688 // ******************************************* << 
689 //                                             << 
690 void G4LogicalVolume::SetVisAttributes (const  << 
691 {                                              << 
692   if (G4Threading::IsWorkerThread()) return;   << 
693   fVisAttributes = std::shared_ptr<const G4Vis << 
694 }                                                 669 }
695                                                   670