Skip to content

Commit

Permalink
confinement: use enum for solid type
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu committed Apr 23, 2024
1 parent db8c6ef commit 1a26541
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
32 changes: 20 additions & 12 deletions include/RMGVertexConfinement.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define _RMG_VERTEX_CONFINEMENT_HH_

#include <chrono>
#include <optional>
#include <regex>
#include <vector>

Expand All @@ -34,8 +35,14 @@ class RMGVertexConfinement : public RMGVVertexGenerator {

public:

enum GeometricalSolidType {
kSphere,
kCylinder,
kBox,
};

struct GenericGeometricalSolidData {
std::string g4_name;
GeometricalSolidType solid_type;
G4ThreeVector volume_center = G4ThreeVector(0, 0, 0);
double sphere_inner_radius = 0;
double sphere_outer_radius = -1;
Expand Down Expand Up @@ -143,7 +150,8 @@ class RMGVertexConfinement : public RMGVVertexGenerator {
std::vector<std::unique_ptr<G4GenericMessenger>> fMessengers;
void SetSamplingModeString(std::string mode);
void AddGeometricalVolumeString(std::string solid);
GenericGeometricalSolidData& SafeBack(std::string solid_type = "");
GenericGeometricalSolidData& SafeBack(
std::optional<GeometricalSolidType> solid_type = std::nullopt);

// FIXME: there is no easy way to set the position vector all at once with
// G4GenericMessenger. Only ::DeclarePropertyWithUnit() accepts vectors and
Expand All @@ -155,29 +163,29 @@ class RMGVertexConfinement : public RMGVVertexGenerator {
inline void SetGeomVolumeCenterZ(double z) { this->SafeBack().volume_center.setZ(z); }

inline void SetGeomSphereInnerRadius(double r) {
this->SafeBack("Sphere").sphere_inner_radius = r;
this->SafeBack(kSphere).sphere_inner_radius = r;
}
inline void SetGeomSphereOuterRadius(double r) {
this->SafeBack("Sphere").sphere_outer_radius = r;
this->SafeBack(kSphere).sphere_outer_radius = r;
}

inline void SetGeomCylinderInnerRadius(double r) {
this->SafeBack("Cylinder").cylinder_inner_radius = r;
this->SafeBack(kCylinder).cylinder_inner_radius = r;
}
inline void SetGeomCylinderOuterRadius(double r) {
this->SafeBack("Cylinder").cylinder_outer_radius = r;
this->SafeBack(kCylinder).cylinder_outer_radius = r;
}
inline void SetGeomCylinderHeight(double h) { this->SafeBack("Cylinder").cylinder_height = h; }
inline void SetGeomCylinderHeight(double h) { this->SafeBack(kCylinder).cylinder_height = h; }
inline void SetGeomCylinderStartingAngle(double a) {
this->SafeBack("Cylinder").cylinder_starting_angle = a;
this->SafeBack(kCylinder).cylinder_starting_angle = a;
}
inline void SetGeomCylinderSpanningAngle(double a) {
this->SafeBack("Cylinder").cylinder_spanning_angle = a;
this->SafeBack(kCylinder).cylinder_spanning_angle = a;
}

inline void SetGeomBoxXLength(double x) { this->SafeBack("Box").box_x_length = x; }
inline void SetGeomBoxYLength(double y) { this->SafeBack("Box").box_y_length = y; }
inline void SetGeomBoxZLength(double z) { this->SafeBack("Box").box_z_length = z; }
inline void SetGeomBoxXLength(double x) { this->SafeBack(kBox).box_x_length = x; }
inline void SetGeomBoxYLength(double y) { this->SafeBack(kBox).box_y_length = y; }
inline void SetGeomBoxZLength(double z) { this->SafeBack(kBox).box_z_length = z; }

void DefineCommands();
};
Expand Down
26 changes: 13 additions & 13 deletions src/RMGVertexConfinement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "RMGVertexConfinement.hh"

#include <chrono>
#include <optional>

#include "G4Box.hh"
#include "G4GenericMessenger.hh"
Expand Down Expand Up @@ -306,18 +307,18 @@ void RMGVertexConfinement::InitializeGeometricalVolumes() {

// no physical volume is specified nor at initialization or later
for (const auto& d : fGeomVolumeData) {
if (d.g4_name == "Sphere") {
if (d.solid_type == kSphere) {
fGeomVolumeSolids.emplace_back(nullptr, G4RotationMatrix(), d.volume_center,
new G4Sphere("RMGVertexConfinement::fGeomSamplingShape::Sphere/" +
std::to_string(fGeomVolumeSolids.size() + 1),
d.sphere_inner_radius, d.sphere_outer_radius, 0, CLHEP::twopi, 0, CLHEP::pi));
} else if (d.g4_name == "Cylinder") {
} else if (d.solid_type == kCylinder) {
fGeomVolumeSolids.emplace_back(nullptr, G4RotationMatrix(), d.volume_center,
new G4Tubs("RMGVertexConfinement::fGeomSamplingShape::Cylinder/" +
std::to_string(fGeomVolumeSolids.size() + 1),
d.cylinder_inner_radius, d.cylinder_outer_radius, 0.5 * d.cylinder_height,
d.cylinder_starting_angle, d.cylinder_spanning_angle));
} else if (d.g4_name == "Box") {
} else if (d.solid_type == kBox) {
fGeomVolumeSolids.emplace_back(nullptr, G4RotationMatrix(), d.volume_center,
new G4Box("RMGVertexConfinement::fGeomSamplingShape::Box/" +
std::to_string(fGeomVolumeSolids.size() + 1),
Expand All @@ -326,7 +327,7 @@ void RMGVertexConfinement::InitializeGeometricalVolumes() {
// else if (...)
else {
RMGLog::OutFormat(RMGLog::error, "Geometrical solid '{}' not known! (Implement me?)",
d.g4_name);
d.solid_type);
}

fGeomVolumeSolids.back().containment_check = false;
Expand Down Expand Up @@ -370,7 +371,7 @@ bool RMGVertexConfinement::ActualGenerateVertex(G4ThreeVector& vertex) {
this->InitializeGeometricalVolumes();

RMGLog::OutDev(RMGLog::debug,
"Sampling mode: ", magic_enum::enum_name<RMGVertexConfinement::SamplingMode>(fSamplingMode));
"Sampling mode: ", magic_enum::enum_name<SamplingMode>(fSamplingMode));

int calls = 0;

Expand Down Expand Up @@ -522,8 +523,7 @@ bool RMGVertexConfinement::ActualGenerateVertex(G4ThreeVector& vertex) {

void RMGVertexConfinement::SetSamplingModeString(std::string mode) {
try {
this->SetSamplingMode(
RMGTools::ToEnum<RMGVertexConfinement::SamplingMode>(mode, "sampling mode"));
this->SetSamplingMode(RMGTools::ToEnum<SamplingMode>(mode, "sampling mode"));
} catch (const std::bad_cast&) { return; }
}

Expand All @@ -539,12 +539,12 @@ void RMGVertexConfinement::AddPhysicalVolumeNameRegex(std::string name, std::str

void RMGVertexConfinement::AddGeometricalVolumeString(std::string solid) {
GenericGeometricalSolidData data;
data.g4_name = solid;
data.solid_type = RMGTools::ToEnum<GeometricalSolidType>(solid, "solid type");
fGeomVolumeData.push_back(data);
}

RMGVertexConfinement::GenericGeometricalSolidData& RMGVertexConfinement::SafeBack(
std::string solid_type) {
std::optional<GeometricalSolidType> solid_type) {
if (fGeomVolumeData.empty()) {
RMGLog::Out(RMGLog::fatal, "Must call /RMG/Generator/Confinement/Geometrical/AddSolid",
"' before setting any geometrical parameter value");
Expand All @@ -553,8 +553,8 @@ RMGVertexConfinement::GenericGeometricalSolidData& RMGVertexConfinement::SafeBac
RMGLog::Out(RMGLog::fatal,
"Solids for vertex confinement have already been initialized, no change possible!");
}
if (!solid_type.empty() && fGeomVolumeData.back().g4_name != solid_type) {
RMGLog::Out(RMGLog::fatal, "Trying to modify non-{} as {}", solid_type, solid_type);
if (!solid_type.has_value() && fGeomVolumeData.back().solid_type != solid_type) {
RMGLog::OutFormat(RMGLog::fatal, "Trying to modify non-{0} as {0}", solid_type.value());
}
return fGeomVolumeData.back();
}
Expand Down Expand Up @@ -596,7 +596,7 @@ void RMGVertexConfinement::DefineCommands() {
->DeclareMethod("SamplingMode", &RMGVertexConfinement::SetSamplingModeString)
.SetGuidance("Select sampling mode for volume confinement")
.SetParameterName("mode", false)
.SetCandidates(RMGTools::GetCandidates<RMGVertexConfinement::SamplingMode>())
.SetCandidates(RMGTools::GetCandidates<SamplingMode>())
.SetStates(G4State_PreInit, G4State_Idle)
.SetToBeBroadcasted(true);

Expand Down Expand Up @@ -628,7 +628,7 @@ void RMGVertexConfinement::DefineCommands() {
->DeclareMethod("AddSolid", &RMGVertexConfinement::AddGeometricalVolumeString)
.SetGuidance("Add geometrical solid to sample primaries from")
.SetParameterName("solid", false)
.SetCandidates("Sphere Box Cylinder")
.SetCandidates(RMGTools::GetCandidates<GeometricalSolidType>())
.SetStates(G4State_PreInit, G4State_Idle)
.SetToBeBroadcasted(true);

Expand Down

0 comments on commit 1a26541

Please sign in to comment.