20#include <unordered_map>
25 bool numeric_value(
const std::string& value) {
28 std::stod(value, &parsed);
29 return parsed == value.size();
31 catch (
const std::exception&) {
return false; }
34 void set_resolved_cad_mesh(std::vector<std::string>& row,
const std::string& resolved) {
35 constexpr int PARAMETERS_INDEX = 2;
36 constexpr int DESCRIPTION_INDEX = 19;
40 (values.size() == 1 && (values[0] ==
"NULL" || numeric_value(values[0])))) {
41 row[DESCRIPTION_INDEX] = resolved;
45 const auto delimiter = row[PARAMETERS_INDEX].find(
',');
46 const auto suffix = delimiter == std::string::npos ?
"" : row[PARAMETERS_INDEX].substr(delimiter);
47 row[PARAMETERS_INDEX] = resolved + suffix;
51 bool geometry_column_exists(sqlite3* db,
const std::string& column_name) {
52 sqlite3_stmt* stmt =
nullptr;
53 if (sqlite3_prepare_v2(db,
"SELECT name FROM PRAGMA_TABLE_INFO('geometry')", -1, &stmt,
nullptr) != SQLITE_OK) {
57 while (sqlite3_step(stmt) == SQLITE_ROW) {
58 const unsigned char* colText = sqlite3_column_text(stmt, 0);
59 if (colText !=
nullptr && column_name ==
reinterpret_cast<const char*
>(colText)) {
64 sqlite3_finalize(stmt);
69void GSystemCADFactory::loadGeometry(
GSystem* s) {
80 unordered_map<string, string> meshByName;
82 meshByName[filesystem::path(cf).stem().string()] = cf;
90 vector<string> dirs = {
101 sqlite3* db =
nullptr;
102 if (sqlite3_open_v2(dbPath.value().c_str(), &db, SQLITE_OPEN_READONLY,
nullptr) != SQLITE_OK) {
107 log->info(1,
"CAD factory: reading definitions from sqlite database ", dbPath.value());
109 const string placement_column = geometry_column_exists(db,
"g4placement_type")
112 "' AS g4placement_type";
113 const string sql_query =
114 "SELECT DISTINCT name, solid, parameters, material, mother, position, rotations, " +
116 ", mfield, visible, style, color, opacity, digitization, identifier, copyOf, solidsOpr, mirror, "
117 "exist, description FROM geometry WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
119 sqlite3_stmt* stmt =
nullptr;
120 if (sqlite3_prepare_v2(db, sql_query.c_str(), -1, &stmt,
nullptr) != SQLITE_OK) {
127 string system_name = s->
getName();
131 sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_TRANSIENT);
132 sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_TRANSIENT);
133 sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_TRANSIENT);
134 sqlite3_bind_int(stmt, 4, runno);
138 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
139 vector<string> gvolumePars;
140 const int colCount = sqlite3_column_count(stmt);
141 for (
int i = 0; i < colCount; i++) {
142 const unsigned char* colText = sqlite3_column_text(stmt, i);
143 gvolumePars.emplace_back(colText ?
reinterpret_cast<const char*
>(colText) :
"");
146 const string& volumeName = gvolumePars[0];
147 const string& solidType = gvolumePars[1];
152 auto it = meshByName.find(volumeName);
153 if (it == meshByName.end()) {
154 log->warning(
"CAD factory: volume <", volumeName,
155 "> is defined in the database but no mesh file was found in <",
156 dirLocation->string(),
">; skipping.");
159 set_resolved_cad_mesh(gvolumePars, (*dirLocation / it->second).string());
166 if (rc != SQLITE_DONE) {
168 "CAD factory: sqlite error while reading geometry: ", sqlite3_errmsg(db));
171 sqlite3_finalize(stmt);
174 log->info(0,
"CAD factory: loaded ", loaded,
" volume(s) for system <", system_name,
175 ">, variation <", variation,
">, run ", runno,
176 " (", meshByName.size(),
" mesh file(s) present in <", 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)