gsystem
Loading...
Searching...
No Matches
gmaterial.cc
Go to the documentation of this file.
1
8#include "gutilities.h"
9//using namespace gutilities;
10
11
12// gsystem
13#include "gmaterial.h"
14#include "gsystemConventions.h"
15
16// c++
17#include <iostream>
18#include <sstream>
19
20
21GMaterial::GMaterial(const std::string& s, std::vector<std::string> pars, const std::shared_ptr<GLogger>& logger) :
22 GBase(logger),
23 system(s) {
24 if (pars.size() != GMATERIALNUMBEROFPARS) {
26 "Incorrect number of material parameters for ", pars[0], ". Expected ",
27 GMATERIALNUMBEROFPARS, " but we got ", pars.size());
28 }
29 else {
30 // The parameter vector is a serialized DB/ASCII row. Parsing is positional.
31 size_t i = 0;
32
34 density = stod(gutilities::removeAllSpacesFromString(pars[i++]));
35
36 // The "composition" field is tokenized into (component, amount) pairs.
37 setComponentsFromString(pars[i++]);
38
39 // Human-readable description (kept verbatim).
40 description = pars[i++];
41
42 // Optical properties: each field may be UNINITIALIZEDSTRINGQUANTITY, in which case it is skipped.
43 getMaterialPropertyFromString(pars[i++], "photonEnergy");
44 getMaterialPropertyFromString(pars[i++], "indexOfRefraction");
45 getMaterialPropertyFromString(pars[i++], "absorptionLength");
46 getMaterialPropertyFromString(pars[i++], "reflectivity");
47 getMaterialPropertyFromString(pars[i++], "efficiency");
48
49 // Scintillation properties (spectra first, then scalars).
50 getMaterialPropertyFromString(pars[i++], "fastcomponent");
51 getMaterialPropertyFromString(pars[i++], "slowcomponent");
52
53 assign_if_set(pars, i, scintillationyield);
54 assign_if_set(pars, i, resolutionscale);
55 assign_if_set(pars, i, fasttimeconstant);
56 assign_if_set(pars, i, slowtimeconstant);
57 assign_if_set(pars, i, yieldratio);
58 assign_if_set(pars, i, birksConstant);
59
60 // Other optical processes: the final vector load triggers cross-vector size validation.
61 getMaterialPropertyFromString(pars[i++], "rayleigh");
62 }
63}
64
65
66std::ostream& operator<<(std::ostream& stream, const GMaterial& gMat) {
67 stream << std::endl;
68 stream << " - Material: " << gMat.name << " in system " << gMat.system << ": " << std::endl;
69 stream << " Density: " << gMat.density << std::endl;
70 if (!gMat.components.empty()) {
71 stream << " Composition: " << std::endl;
72 for (unsigned m = 0; m < gMat.components.size(); m++) {
73 std::string quantity = gMat.amounts[m] > 1 ? " atoms " : " fractional mass";
74 stream << " ・ " << gMat.components[m] << quantity << " " << gMat.amounts[m] << std::endl;
75 }
76 }
77 stream << " Description: " << gMat.description << std::endl;
78 stream << std::endl;
79
80 return stream;
81}
82
83
84// sets components and amounts
85void GMaterial::setComponentsFromString(const std::string& composition) {
86 std::vector<std::string> allComponents = gutilities::getStringVectorFromString(composition);
87
88 // Interpret alternating tokens as (component, amount) pairs.
89 for (unsigned e = 0; e < allComponents.size() / 2; e++) {
90 components.push_back(allComponents[e * 2]);
91 amounts.push_back(stod(allComponents[e * 2 + 1]));
92 }
93}
94
95
96// load property from DB entry based on its name
97void GMaterial::getMaterialPropertyFromString(const std::string& parameter, const std::string& propertyName) {
98 // Nothing to do if the parameter is not assigned.
99 if (gutilities::removeLeadingAndTrailingSpacesFromString(parameter) == UNINITIALIZEDSTRINGQUANTITY) { return; }
100
101 // Tokenize the string and parse each component to a numeric value with units.
102 std::stringstream parameterComponents(parameter);
103
104 while (!parameterComponents.eof()) {
105 std::string component;
106 parameterComponents >> component;
107
108 // Removing whitespaces.
109 std::string trimmedComponent = gutilities::removeLeadingAndTrailingSpacesFromString(component);
110
111 // Vector-valued properties append one value per token.
112 if (propertyName == "photonEnergy") { photonEnergy.push_back(gutilities::getG4Number(trimmedComponent)); }
113 else if (propertyName == "indexOfRefraction") {
114 indexOfRefraction.push_back(gutilities::getG4Number(trimmedComponent));
115 }
116 else if (propertyName == "absorptionLength") {
117 absorptionLength.push_back(gutilities::getG4Number(trimmedComponent));
118 }
119 else if (propertyName == "reflectivity") { reflectivity.push_back(gutilities::getG4Number(trimmedComponent)); }
120 else if (propertyName == "efficiency") { efficiency.push_back(gutilities::getG4Number(trimmedComponent)); }
121 else if (propertyName == "fastcomponent") {
122 fastcomponent.push_back(gutilities::getG4Number(trimmedComponent));
123 }
124 else if (propertyName == "slowcomponent") {
125 slowcomponent.push_back(gutilities::getG4Number(trimmedComponent));
126 }
127 // Scalar-valued properties overwrite with the last parsed token.
128 else if (propertyName == "scintillationyield") {
129 scintillationyield = gutilities::getG4Number(trimmedComponent);
130 }
131 else if (propertyName == "resolutionscale") { resolutionscale = gutilities::getG4Number(trimmedComponent); }
132 else if (propertyName == "fasttimeconstant") { fasttimeconstant = gutilities::getG4Number(trimmedComponent); }
133 else if (propertyName == "slowtimeconstant") { slowtimeconstant = gutilities::getG4Number(trimmedComponent); }
134 else if (propertyName == "yieldratio") { yieldratio = gutilities::getG4Number(trimmedComponent); }
135 else if (propertyName == "birkConstant") { birksConstant = gutilities::getG4Number(trimmedComponent); }
136 else if (propertyName == "rayleigh") { rayleigh.push_back(gutilities::getG4Number(trimmedComponent)); }
137
138
139 if (propertyName == "rayleigh") {
140 // Rayleigh is loaded last: at this point we can validate that all other optical vectors
141 // either are empty (not specified) or match the photonEnergy vector length.
142 //
143 // If they do not match, behavior is undefined because properties would be evaluated
144 // at inconsistent energy grids.
145 unsigned long photonEnergyVectorSize = photonEnergy.size();
146
147 if (!indexOfRefraction.empty() && indexOfRefraction.size() != photonEnergyVectorSize) {
149 "indexOfRefraction size ", indexOfRefraction.size(), " mismatch: photonEnergy has size ",
150 photonEnergyVectorSize);
151 }
152 if (!absorptionLength.empty() && absorptionLength.size() != photonEnergyVectorSize) {
154 "absorptionLength size ", absorptionLength.size(), " mismatch: photonEnergy has size ",
155 photonEnergyVectorSize);
156 }
157 if (!reflectivity.empty() && reflectivity.size() != photonEnergyVectorSize) {
159 "reflectivity size ", reflectivity.size(), " mismatch: photonEnergy has size ",
160 photonEnergyVectorSize);
161 }
162 if (!efficiency.empty() && efficiency.size() != photonEnergyVectorSize) {
164 "efficiency size ", efficiency.size(), " mismatch: photonEnergy has size ",
165 photonEnergyVectorSize);
166 }
167 if (!fastcomponent.empty() && fastcomponent.size() != photonEnergyVectorSize) {
169 "fastcomponent size ", fastcomponent.size(), " mismatch: photonEnergy has size ",
170 photonEnergyVectorSize);
171 }
172 if (!slowcomponent.empty() && slowcomponent.size() != photonEnergyVectorSize) {
174 "slowcomponent size ", slowcomponent.size(), " mismatch: photonEnergy has size ",
175 photonEnergyVectorSize);
176 }
177 if (!rayleigh.empty() && rayleigh.size() != photonEnergyVectorSize) {
179 "rayleigh size ", rayleigh.size(), " mismatch: photonEnergy has size ",
180 photonEnergyVectorSize);
181 }
182 }
183 }
184}
185
186
187bool GMaterial::assign_if_set(const std::vector<std::string>& pars, size_t& i, double& out) {
188 if (i >= pars.size()) return false;
189
190 const std::string trimmed =
192
193 std::string_view sv(trimmed);
194 if (gutilities::is_unset(sv)) return false;
195
196 try {
197 out = std::stod(trimmed);
198 return true;
199 }
200 catch (const std::exception&) {
201 // Do not error here: malformed optional scalars are treated as "not set".
202 return false;
203 }
204}
std::shared_ptr< GLogger > log
void error(int exit_code, Args &&... args) const
Material definition belonging to a detector system.
Definition gmaterial.h:29
GMaterial(const std::string &system, std::vector< std::string > pars, const std::shared_ptr< GLogger > &logger)
Construct a material from a serialized parameter list.
Definition gmaterial.cc:21
std::ostream & operator<<(std::ostream &stream, const GMaterial &gMat)
Definition gmaterial.cc:66
Conventions and shared constants for the detector-system module.
#define GMATERIALNUMBEROFPARS
Number of database parameters defining a gmaterial entry.
#define ERR_GMATERIALOPTICALPROPERTYMISMATCH
#define ERR_GWRONGNUMBEROFPARS
double getG4Number(const string &v, bool warnIfNotUnit=false)
string removeAllSpacesFromString(const std::string &str)
bool is_unset(std::string_view s)
string removeLeadingAndTrailingSpacesFromString(const std::string &input)
vector< std::string > getStringVectorFromString(const std::string &input)