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