20#include <unordered_map>
25 bool numeric_value(
const std::string& value) {
29 static_cast<void>(std::stod(value, &parsed));
30 return parsed == value.size();
32 catch (
const std::exception&) {
return false; }
35 void set_resolved_cad_mesh(std::vector<std::string>& row,
const std::string& resolved) {
36 constexpr int PARAMETERS_INDEX = 2;
37 constexpr int DESCRIPTION_INDEX = 19;
41 (values.size() == 1 && (values[0] ==
"NULL" || numeric_value(values[0])))) {
42 row[DESCRIPTION_INDEX] = resolved;
46 const auto delimiter = row[PARAMETERS_INDEX].find(
',');
47 const auto suffix = delimiter == std::string::npos ?
"" : row[PARAMETERS_INDEX].substr(delimiter);
48 row[PARAMETERS_INDEX] = resolved + suffix;
52 bool geometry_column_exists(sqlite3* db,
const std::string& column_name) {
53 sqlite3_stmt* stmt =
nullptr;
54 if (sqlite3_prepare_v2(db,
"SELECT name FROM PRAGMA_TABLE_INFO('geometry')", -1, &stmt,
nullptr) != SQLITE_OK) {
58 while (sqlite3_step(stmt) == SQLITE_ROW) {
59 const unsigned char* colText = sqlite3_column_text(stmt, 0);
60 if (colText !=
nullptr && column_name ==
reinterpret_cast<const char*
>(colText)) {
65 sqlite3_finalize(stmt);
70void GSystemCADFactory::loadGeometry(
GSystem* s) {
81 unordered_map<string, string> meshByName;
83 meshByName[filesystem::path(cf).stem().string()] = cf;
91 vector<string> dirs = {
102 sqlite3* db =
nullptr;
103 if (sqlite3_open_v2(dbPath.value().c_str(), &db, SQLITE_OPEN_READONLY,
nullptr) != SQLITE_OK) {
108 log->info(1,
"CAD factory: reading definitions from sqlite database %1", dbPath.value());
110 const string placement_column = geometry_column_exists(db,
"g4placement_type")
113 "' AS g4placement_type";
114 const string sql_query =
115 "SELECT DISTINCT name, solid, parameters, material, mother, position, rotations, " +
117 ", mfield, visible, style, color, opacity, digitization, identifier, copyOf, solidsOpr, mirror, "
118 "exist, description FROM geometry WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
120 sqlite3_stmt* stmt =
nullptr;
121 if (sqlite3_prepare_v2(db, sql_query.c_str(), -1, &stmt,
nullptr) != SQLITE_OK) {
128 string system_name = s->
getName();
132 sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_TRANSIENT);
133 sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_TRANSIENT);
134 sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_TRANSIENT);
135 sqlite3_bind_int(stmt, 4, runno);
139 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
140 vector<string> gvolumePars;
141 const int colCount = sqlite3_column_count(stmt);
142 for (
int i = 0; i < colCount; i++) {
143 const unsigned char* colText = sqlite3_column_text(stmt, i);
144 gvolumePars.emplace_back(colText ?
reinterpret_cast<const char*
>(colText) :
"");
147 const string& volumeName = gvolumePars[0];
148 const string& solidType = gvolumePars[1];
153 auto it = meshByName.find(volumeName);
154 if (it == meshByName.end()) {
156 "CAD factory: volume <%1> is defined in the database but no mesh file was found in <%2>; "
157 "skipping.", volumeName, dirLocation->string());
160 set_resolved_cad_mesh(gvolumePars, (*dirLocation / it->second).string());
167 if (rc != SQLITE_DONE) {
173 sqlite3_finalize(stmt);
178 "CAD factory: loaded %1 volume(s) for system <%2>, variation <%3>, run %4 (%5 mesh file(s) present "
179 "in <%6>).", loaded, system_name, variation, runno, meshByName.size(), dirLocation->string());
std::shared_ptr< GLogger > log
std::vector< std::string > possibleLocationOfFiles
List of candidate directories used by file-based factories.
Represents a single detector system (e.g., calorimeter, tracker).
std::string getFilePath() const
Gets the full file path of the system.
std::string getExperiment() const
std::string getVariation() const
void addGVolume(std::vector< std::string > pars)
Build and add a volume from a serialized parameter list.
std::string getName() const
std::string get_dbhost() const
Conventions and shared constants for the detector-system module.
constexpr char GSYSTEMSQLITETDEFAULTFILE[]
Default sqlite DB filename used when the user does not specify one.
constexpr char DEFAULTG4PLACEMENTTYPE[]
constexpr char GSYSTEMCADTFACTORYLABEL[]
constexpr int ERR_GSQLITEERROR
constexpr char ROOTWORLDGVOLUMENAME[]
Canonical name for the ROOT/world gvolume entry.
constexpr int ERR_GDIRNOTFOUND
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
std::filesystem::path gemc_root()
vector< string > getListOfFilesInDirectory(const string &dirName, const vector< string > &extensions)
std::optional< std::string > searchForFileInLocations(const std::vector< std::string > &locations, std::string_view filename)
std::optional< std::filesystem::path > searchForDirInLocations(const string &dirName, const vector< string > &possibleLocations)