EDepSimDensityPositionGenerator.cc
Go to the documentation of this file.
1 #include <G4TransportationManager.hh>
2 #include <G4Navigator.hh>
3 #include <G4VPhysicalVolume.hh>
4 #include <G4LogicalVolume.hh>
5 #include <G4Material.hh>
6 #include <G4VisExtent.hh>
7 #include <Randomize.hh>
8 
9 #include "EDepSimLog.hh"
10 
12 
14  : EDepSim::VConstrainedPositionGenerator(name), fMaximumDensity(-1) {}
15 
17 
19  if (fMaximumDensity < 0) {
20  const G4MaterialTable* mTable = G4Material::GetMaterialTable();
21  for (G4MaterialTable::const_iterator m = mTable->begin();
22  m != mTable->end();
23  ++m) {
24  fMaximumDensity = std::max(fMaximumDensity, (*m)->GetDensity());
25  }
26  }
27  // Get the navigator.
28  G4Navigator* navigator
29  = G4TransportationManager::GetTransportationManager()
30  ->GetNavigatorForTracking();
31  for (int finiteLoop = 0; finiteLoop<100000; ++finiteLoop) {
32  G4LorentzVector vtx = TrialPosition();
33 
34  // Get the volume that contains the point.
35  G4VPhysicalVolume* volume = navigator->LocateGlobalPointAndSetup(vtx);
36 
37  // Skip this vertex if it's not in a legal volume.
38  if (!volume) continue;
39 
40  // Get the density.
41  double density
42  = volume->GetLogicalVolume()->GetMaterial()->GetDensity();
43 
44  // Skip this vertex if it misses the sampling.
45  if (density < fMaximumDensity*G4UniformRand()) continue;
46 
47  // The vertex meets the density sampling, so check if it is valid.
48  if (ValidPosition(vtx)) return vtx;
49  }
50  EDepSimError("EDepSim::DensityPositionGenerator::GetPosition:"
51  << " No valid position found");
52  throw;
53 }
54 
56  return true;
57 }
static QCString name
Definition: declinfo.cpp:673
virtual bool ValidPosition(const G4LorentzVector &vtx)
Return true if the vertex is valid. This is used in the derived class.
double fMaximumDensity
The maximum density in the detector.
static const std::string volume[nvol]
intermediate_table::const_iterator const_iterator
G4LorentzVector TrialPosition()
Generate a trial position uniformly in the sample box.
Construct a module from components.
Definition: TG4HitSegment.h:10
static int max(int a, int b)
#define EDepSimError(outStream)
Definition: EDepSimLog.hh:503
virtual G4LorentzVector GetPosition()
Return a candidate vertex.