gsystem
Loading...
Searching...
No Matches
gvolume.cc
Go to the documentation of this file.
1
7
8// gsystem
9#include "gvolume.h"
10
11#include <algorithm>
12#include <cctype>
13#include <optional>
14#include <stdexcept>
15#include <utility>
16#include "gsystemConventions.h"
17
18using namespace gutilities;
19
20namespace {
21std::optional<std::string> normalize_optional_geometry_field(std::optional<std::string> value,
22 bool remove_all_spaces) {
23 if (!value) return std::nullopt;
24
25 std::string trimmed = removeLeadingAndTrailingSpacesFromString(*value);
26 if (is_unset(trimmed)) return std::nullopt;
27
28 std::string lowercase = trimmed;
29 std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(),
30 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
31 if (lowercase == "no" || lowercase == "none" || lowercase == "not provided") return std::nullopt;
32
33 if (remove_all_spaces) return removeAllSpacesFromString(trimmed);
34 return trimmed;
35}
36} // namespace
37
38const std::string& GVolume::getG4Name() const {
39 if (!g4name) {
40 throw std::logic_error("Geant4 name requested before GVolume <" + system + "/" + name + "> was resolved");
41 }
42 return *g4name;
43}
44
45const std::string& GVolume::getG4MotherName() const {
46 if (!g4motherName) {
47 throw std::logic_error(
48 "Geant4 mother name requested before GVolume <" + system + "/" + name + "> was resolved");
49 }
50 return *g4motherName;
51}
52
53// need to set pCopyNo with unique identifier
54// see c++ thread safe ID generation function
55GVolume::GVolume(const std::shared_ptr<GLogger>& logger,
56 const std::string& s,
57 std::vector<std::string> pars,
58 std::optional<std::string> importPath) :
59 GBase(logger),
60 system(s),
61 importFilename(normalize_optional_geometry_field(std::move(importPath), false)) {
62 if (pars.size() == gsystem::GVOLUMELEGACYNUMBEROFPARS) {
63 pars.insert(pars.begin() + 7, gsystem::DEFAULTG4PLACEMENTTYPE);
64 }
65
66 if (pars.size() != gsystem::GVOLUMENUMBEROFPARS) {
67 // Dump received parameters to help diagnose mismatched schema or input corruption.
68 for (auto& parameter : pars) { log->warning(" - parameter ", parameter); }
69
71 "Incorrect number of system parameters for GVolume: ", pars.size(), ", it should be ",
73 }
74 else {
75 // The parameter vector is a serialized DB/ASCII row. Parsing is positional.
76 int i = 0;
77
78 name = removeAllSpacesFromString(pars[i++]);
79
80 // checking that name does not contain gsystem::GSYSTEM_DELIMITER
81 // because gsystem::GSYSTEM_DELIMITER is used later to build fully-qualified Geant4 names.
82 if (name.find(gsystem::GSYSTEM_DELIMITER) != string::npos) {
84 "the gVolume name <", name, "> contains the invalid character: <", gsystem::GSYSTEM_DELIMITER,
85 ">. Exiting.");
86 }
87
88 type = removeAllSpacesFromString(pars[i++]);
89 parameters = normalize_optional_geometry_field(pars[i++], false);
90 material = removeAllSpacesFromString(pars[i++]);
91 motherName = removeAllSpacesFromString(pars[i++]);
94 g4placementType = removeAllSpacesFromString(pars[i++]);
95 if (g4placementType.empty() || gutilities::is_unset(g4placementType)) {
96 g4placementType = gsystem::DEFAULTG4PLACEMENTTYPE;
97 }
98 emfield = normalize_optional_geometry_field(pars[i++], true);
99 string pvis = removeAllSpacesFromString(pars[i++]);
100 visible = (pvis == "1") ? true : false;
101 style = stoi(removeAllSpacesFromString(pars[i++]));
102 color = removeAllSpacesFromString(pars[i++]);
103 opacity = stod(removeAllSpacesFromString(pars[i++]));
104 digitization = normalize_optional_geometry_field(pars[i++], true);
105 gidentity = normalize_optional_geometry_field(pars[i++], false);
106 copyOf = normalize_optional_geometry_field(pars[i++], false);
107 solidsOpr = normalize_optional_geometry_field(pars[i++], false);
108 mirror = normalize_optional_geometry_field(pars[i++], false);
109 string pexists = removeAllSpacesFromString(pars[i++]);
110 exist = (pexists == "1") ? true : false;
111
112 description = removeLeadingAndTrailingSpacesFromString(pars[i++]);
113 variation = removeLeadingAndTrailingSpacesFromString(pars[i++]);
114 runno = stoi(removeAllSpacesFromString(pars[i++]));
115
116 }
117}
118
119
120std::ostream& operator<<(std::ostream& stream, const GVolume& gVol) {
121 string style = "unknown";
122 if (gVol.style == 0) { style = "wireframe"; }
123 else if (gVol.style == 1) { style = "solid"; }
124 else if (gVol.style == 2) { style = "cloud"; }
125 string visibility = "yes";
126 if (!gVol.visible) { visibility = "no"; }
127
128 stream << std::endl;
129 stream << " - Name: " << gVol.name << " - " << gVol.description << std::endl;
130 stream << " - System: " << gVol.system << std::endl;
131 stream << " - Variation: " << gVol.variation << std::endl;
132 stream << " - Run Number: " << gVol.runno << std::endl;
133 if (gVol.copyOf) stream << " - copyOf: " << *gVol.copyOf << std::endl;
134 if (gVol.solidsOpr) stream << " - solidsOpr: " << *gVol.solidsOpr << std::endl;
135 if (!gVol.type.empty() && !gutilities::is_unset(gVol.type))
136 stream << " - Type: " << gVol.type <<
137 std::endl;
138 if (gVol.parameters) stream << " - Parameters: " << *gVol.parameters << std::endl;
139 stream << " - Material: " << gVol.material << std::endl;
140 stream << " - Mother: " << gVol.motherName << std::endl;
141 stream << " - Positions: " << gVol.pos << std::endl;
142 stream << " - Rotation(s): " << gVol.rot << std::endl;
143 stream << " - G4 Placement: " << gVol.g4placementType << std::endl;
144 if (gVol.emfield) stream << " - E.M. field: " << *gVol.emfield << std::endl;
145 if (gVol.digitization) stream << " - Digitization: " << *gVol.digitization << std::endl;
146 if (gVol.gidentity) stream << " - GIdentity: " << *gVol.gidentity << std::endl;
147 stream << " - Col, Vis, Style: " << gVol.color << ", " << visibility << ", " << style << std::endl;
148 stream << std::endl;
149
150 return stream;
151}
152
153
154GVolume::GVolume(const std::string& rootVolumeDefinition,
155 const std::shared_ptr<GLogger>& logger) :
156 GBase(logger) {
157 // The ROOT/world definition is tokenized by spaces:
158 // <solidType> <dim1> <dim2> ... <material>
159 vector<string> rootDefinitions = getStringVectorFromStringWithDelimiter(rootVolumeDefinition, " ");
160 string volumeParameters;
161
162 // Build the parameter string (skip the type and the final material token).
163 for (size_t i = 1; i < rootDefinitions.size() - 1; i++) { volumeParameters += ", " + rootDefinitions[i]; }
164
167 variation = "default";
168 type = rootDefinitions[0];
169 parameters = volumeParameters;
170 material = rootDefinitions.back();
171 motherName = gsystem::MOTHEROFUSALL;
174 g4placementType = gsystem::DEFAULTG4PLACEMENTTYPE;
175 visible = false;
176 style = 0; // wireframe
177 color = "ccffff";
178 exist = true;
179
180 description = "root volume";
181
182}
183
184void GVolume::setDigitization(std::optional<std::string> value) {
185 digitization = normalize_optional_geometry_field(std::move(value), true);
186}
187
188void GVolume::setGIdentity(std::optional<std::string> value) {
189 gidentity = normalize_optional_geometry_field(std::move(value), false);
190}
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
void setGIdentity(std::optional< std::string > g)
Override the identity string after loading.
Definition gvolume.cc:188
void setDigitization(std::optional< std::string > d)
Override the digitization label after loading.
Definition gvolume.cc:184
const std::string & getG4Name() const
Definition gvolume.cc:38
GVolume(const std::shared_ptr< GLogger > &log, const std::string &system, std::vector< std::string > pars, std::optional< std::string > importPath=std::nullopt)
Construct a volume from a serialized parameter vector.
Definition gvolume.cc:55
const std::string & getG4MotherName() const
Definition gvolume.cc:45
Conventions and shared constants for the detector-system module.
std::ostream & operator<<(std::ostream &stream, const GVolume &gVol)
Definition gvolume.cc:120
constexpr int GVOLUMELEGACYNUMBEROFPARS
Number of database parameters defining a legacy gvolume entry without g4placement_type.
constexpr int ERR_GWRONGNUMBEROFPARS
constexpr char DEFAULTG4PLACEMENTTYPE[]
constexpr char GSYSTEM_DELIMITER[]
Delimiter used to build fully-qualified names (system/name).
constexpr char DEFAULTROTATION[]
constexpr int ERR_GVOLUMENAMECONTAINSINVALID
constexpr int GVOLUMENUMBEROFPARS
constexpr char MOTHEROFUSALL[]
Special mother-name marker for the top-level world root.
constexpr char DEFAULTPOSITION[]
constexpr char ROOTWORLDGVOLUMENAME[]
Canonical name for the ROOT/world gvolume entry.
string removeAllSpacesFromString(const std::string &str)
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
bool is_unset(std::string_view s)
string removeLeadingAndTrailingSpacesFromString(const std::string &input)