16#ifndef _RMG_VERTEX_CONFINEMENT_HH_
17#define _RMG_VERTEX_CONFINEMENT_HH_
27#include "G4AutoLock.hh"
28#include "G4GenericMessenger.hh"
29#include "G4RotationMatrix.hh"
30#include "G4ThreeVector.hh"
32#include "RMGVVertexGenerator.hh"
39class RMGVertexConfinement :
public RMGVVertexGenerator {
53 G4ThreeVector volume_center = G4ThreeVector(0, 0, 0);
54 double sphere_inner_radius = 0;
55 double sphere_outer_radius = -1;
56 double cylinder_inner_radius = 0;
57 double cylinder_outer_radius = -1;
58 double cylinder_height = -1;
59 double cylinder_starting_angle = 0;
60 double cylinder_spanning_angle = CLHEP::twopi;
61 double box_x_length = -1;
62 double box_y_length = -1;
63 double box_z_length = -1;
77 kIntersectPhysicalWithGeometrical,
172 [[nodiscard]]
double Sample()
const;
185 RMGVertexConfinement();
187 void BeginOfRunAction(
const G4Run* run)
override;
188 void EndOfRunAction(
const G4Run* run)
override;
206 fGeomVolumeData.emplace_back(data);
210 void SetSamplingMode(
SamplingMode mode) { fSamplingMode = mode; }
211 void SetFirstSamplingVolumeType(
VolumeType type) { fFirstSamplingVolumeType = type; }
212 void SetWeightByMass(
bool mode) { fWeightByMass = mode; }
213 void SetWeightByMassIsotope(
int z,
int n) {
214 fWeightByMass =
true;
215 fWeightByMassIsotopeZ = z;
216 fWeightByMassIsotopeN = n;
219 std::vector<GenericGeometricalSolidData>& GetGeometricalSolidDataList() {
220 return fGeomVolumeData;
233 struct SampleableObject {
235 SampleableObject() =
default;
236 SampleableObject(
const SampleableObject&) =
default;
251 G4RotationMatrix rot,
254 bool is_native_sampleable =
false,
255 bool on_surface =
false
259 ~SampleableObject() =
default;
266 [[nodiscard]]
bool IsInside(
const G4ThreeVector& vertex)
const;
285 [[nodiscard]]
bool Sample(
286 G4ThreeVector& vertex,
288 bool force_containment_check,
306 G4ThreeVector& vertex,
308 size_t max_intersections
341 void GetDirection(G4ThreeVector& dir, G4ThreeVector& pos)
const;
356 void RecalcMass(
int z,
int n);
360 G4RotationMatrix rotation;
361 G4ThreeVector translation;
367 bool surface_sample =
false;
368 bool native_sample =
false;
369 size_t max_num_intersections = 0;
382 struct SampleableObjectCollection {
384 SampleableObjectCollection() =
default;
385 ~SampleableObjectCollection() { data.clear(); }
397 [[nodiscard]]
bool IsInside(
const G4ThreeVector& vertex)
const;
400 [[nodiscard]]
size_t size()
const {
return data.size(); }
402 template<
typename... Args>
void emplace_back(Args&&... args);
403 [[nodiscard]]
bool empty()
const {
return data.empty(); }
411 void insert(SampleableObjectCollection& other) {
412 data.reserve(this->size() + other.size());
413 for (
size_t i = 0; i < other.size(); ++i) this->emplace_back(other.at(i));
414 this->total_volume += other.total_volume;
415 this->total_mass += other.total_mass;
416 this->total_surface += other.total_surface;
419 void recalc_total(
bool weigh_by_mass,
int mass_isotope_z,
int mass_istotope_n);
421 std::vector<SampleableObject> data;
422 double total_volume = 0;
423 double total_mass = 0;
424 double total_surface = 0;
429 void InitializePhysicalVolumes();
430 void InitializeGeometricalVolumes(
bool use_excluded_volumes);
431 bool ActualGenerateVertex(G4ThreeVector& v);
433 std::vector<std::string> fPhysicalVolumeNameRegexes;
434 std::vector<std::string> fPhysicalVolumeCopyNrRegexes;
436 std::vector<GenericGeometricalSolidData> fGeomVolumeData;
437 std::vector<GenericGeometricalSolidData> fExcludedGeomVolumeData;
439 static G4Mutex fGeometryMutex;
452 static std::vector<G4VSolid*> fOwnedSolids;
454 static std::atomic<bool> fVolumesInitialized;
457 VolumeType fFirstSamplingVolumeType = VolumeType::kUnset;
458 bool fWeightByMass =
false;
459 int fWeightByMassIsotopeZ = 0;
460 int fWeightByMassIsotopeN = 0;
462 bool fOnSurface =
false;
463 bool fForceContainmentCheck =
false;
464 bool fLastSolidExcluded =
false;
465 size_t fSurfaceSampleMaxIntersections = 0;
472 std::chrono::nanoseconds fVertexGenerationTime{};
474 std::vector<std::unique_ptr<G4GenericMessenger>> fMessengers;
475 void SetSamplingModeString(std::string mode);
476 void SetFirstSamplingVolumeTypeString(std::string type);
478 void AddGeometricalVolumeString(std::string solid);
479 void AddExcludedGeometricalVolumeString(std::string solid);
482 std::optional<GeometricalSolidType> solid_type = std::nullopt
489 void SetGeomVolumeCenter(
const G4ThreeVector& v) { this->SafeBack().volume_center = v; }
490 void SetGeomVolumeCenterX(
double x) { this->SafeBack().volume_center.setX(x); }
491 void SetGeomVolumeCenterY(
double y) { this->SafeBack().volume_center.setY(y); }
492 void SetGeomVolumeCenterZ(
double z) { this->SafeBack().volume_center.setZ(z); }
494 void SetGeomSphereInnerRadius(
double r) {
495 this->SafeBack(GeometricalSolidType::kSphere).sphere_inner_radius = r;
497 void SetGeomSphereOuterRadius(
double r) {
498 this->SafeBack(GeometricalSolidType::kSphere).sphere_outer_radius = r;
501 void SetGeomCylinderInnerRadius(
double r) {
502 this->SafeBack(GeometricalSolidType::kCylinder).cylinder_inner_radius = r;
504 void SetGeomCylinderOuterRadius(
double r) {
505 this->SafeBack(GeometricalSolidType::kCylinder).cylinder_outer_radius = r;
507 void SetGeomCylinderHeight(
double h) {
508 this->SafeBack(GeometricalSolidType::kCylinder).cylinder_height = h;
510 void SetGeomCylinderStartingAngle(
double a) {
511 this->SafeBack(GeometricalSolidType::kCylinder).cylinder_starting_angle = a;
513 void SetGeomCylinderSpanningAngle(
double a) {
514 this->SafeBack(GeometricalSolidType::kCylinder).cylinder_spanning_angle = a;
517 void SetGeomBoxXLength(
double x) {
518 this->SafeBack(GeometricalSolidType::kBox).box_x_length = x;
520 void SetGeomBoxYLength(
double y) {
521 this->SafeBack(GeometricalSolidType::kBox).box_y_length = y;
523 void SetGeomBoxZLength(
double z) {
524 this->SafeBack(GeometricalSolidType::kBox).box_z_length = z;
527 void DefineCommands();
529 void SetDepthProfileTypeString(std::string type);
530 void SetDepthProfileMean(
double mean) { fDepthProfile.mean = mean; }
531 void SetDepthProfileSigma(
double sigma) { fDepthProfile.sigma = sigma; }
532 void SetDepthProfileRangeLo(
double lo) { fDepthProfile.range_lo = lo; }
533 void SetDepthProfileRangeHi(
double hi) { fDepthProfile.range_hi = hi; }
VolumeType
Types of volume to sample, either physical (a volume in the geometry), geometrical (defined by the us...
Definition RMGVertexConfinement.hh:84
GeometricalSolidType
Different types of geometrical (user) defined solids.
Definition RMGVertexConfinement.hh:44
void AddPhysicalVolumeNameRegex(std::string name, std::string copy_nr=".*")
Definition RMGVertexConfinement.cc:1190
SamplingMode
Strategy for sampling physical and geometrical volumes.
Definition RMGVertexConfinement.hh:76
bool GenerateVertex(G4ThreeVector &v) override
Generate the actual vertex, according to the sampling mode (see RMGVertexConfinement::SamplingMode).
Definition RMGVertexConfinement.cc:915
Depth profile for displacing surface-sampled vertices into the material.
Definition RMGVertexConfinement.hh:114
double Sample() const
Sample a depth value from the configured distribution.
Definition RMGVertexConfinement.cc:110
void Validate() const
Validate the configured parameters, aborting with a fatal error on bad input.
Definition RMGVertexConfinement.cc:152
Type type
Distribution type. Defaults to kNone (no displacement).
Definition RMGVertexConfinement.hh:128
double sigma
Standard deviation for the truncated-Gaussian distribution.
Definition RMGVertexConfinement.hh:143
Type
Distribution type for the depth profile.
Definition RMGVertexConfinement.hh:120
@ kNone
No depth profile: vertex remains on the surface.
Definition RMGVertexConfinement.hh:121
@ kUniform
Uniform distribution over [range_lo, range_hi].
Definition RMGVertexConfinement.hh:124
@ kExponential
Exponential distribution parametrised by mean.
Definition RMGVertexConfinement.hh:122
@ kTruncatedGaussian
Gaussian distribution truncated to [range_lo, range_hi].
Definition RMGVertexConfinement.hh:123
double range_hi
Upper bound of the depth range for uniform and truncated-Gaussian distributions.
Definition RMGVertexConfinement.hh:157
double mean
Mean depth for the exponential and truncated-Gaussian distributions.
Definition RMGVertexConfinement.hh:136
double range_lo
Lower bound of the depth range for uniform and truncated-Gaussian distributions.
Definition RMGVertexConfinement.hh:150
Information about the geometrical (user) defined solids.
Definition RMGVertexConfinement.hh:51
A collection of SampleableObject objects. It can be used to sample from by selecting a volume weighte...
Definition RMGVertexConfinement.hh:382
const SampleableObject & SurfaceWeightedRand() const
Select a SampleableObject from the collection, weighted by surface area.
Definition RMGVertexConfinement.cc:195
const SampleableObject & VolumeWeightedRand(bool weight_by_mass) const
Select a SampleableObject from the collection, weighted by volume.
Definition RMGVertexConfinement.cc:219
Definition RMGVertexConfinement.hh:233
bool Sample(G4ThreeVector &vertex, size_t max_attempts, bool force_containment_check, size_t &n_trials) const
Generate a sample from the solid.
Definition RMGVertexConfinement.cc:413
void GetDirection(G4ThreeVector &dir, G4ThreeVector &pos) const
Get a position and direction for the generic surface sampling algorithm.
Definition RMGVertexConfinement.cc:317
bool GenerateSurfacePoint(G4ThreeVector &vertex, size_t max_attempts, size_t max_intersections) const
Generate a point on the surface of the solid.
Definition RMGVertexConfinement.cc:356
std::vector< G4ThreeVector > GetIntersections(G4ThreeVector start, G4ThreeVector dir) const
Get the number of intersections between the solid and the line starting at start with direction dir.
Definition RMGVertexConfinement.cc:263
DepthProfile depth_profile
Depth profile applied when displacing vertices inward from the surface.
Definition RMGVertexConfinement.hh:376
void ApplyDepthProfile(G4ThreeVector &local_vertex, const G4VSolid *solid) const
Displace a surface point inward according to depth_profile.
Definition RMGVertexConfinement.cc:178
bool IsInside(const G4ThreeVector &vertex) const
Check if the vertex is inside the solid.
Definition RMGVertexConfinement.cc:249