20#include <unordered_map>
26 bool geometry_column_exists(sqlite3* db,
const std::string& column_name) {
27 sqlite3_stmt* stmt =
nullptr;
28 if (sqlite3_prepare_v2(db,
"SELECT name FROM PRAGMA_TABLE_INFO('geometry')", -1, &stmt,
nullptr) != SQLITE_OK) {
32 while (sqlite3_step(stmt) == SQLITE_ROW) {
33 const unsigned char* colText = sqlite3_column_text(stmt, 0);
34 if (colText !=
nullptr && column_name ==
reinterpret_cast<const char*
>(colText)) {
39 sqlite3_finalize(stmt);
44void GSystemCADFactory::loadGeometry(
GSystem* s) {
50 if (!filesystem::exists(dirLocation)) {
56 unordered_map<string, string> meshByName;
58 meshByName[filesystem::path(cf).stem().string()] = cf;
66 vector<string> dirs = {
77 sqlite3* db =
nullptr;
78 if (sqlite3_open_v2(dbPath.value().c_str(), &db, SQLITE_OPEN_READONLY,
nullptr) != SQLITE_OK) {
83 log->
info(1,
"CAD factory: reading definitions from sqlite database ", dbPath.value());
85 const string placement_column = geometry_column_exists(db,
"g4placement_type")
88 const string sql_query =
89 "SELECT DISTINCT name, solid, parameters, material, mother, position, rotations, " +
91 ", mfield, visible, style, color, opacity, digitization, identifier, copyOf, solidsOpr, mirror, "
92 "exist, description FROM geometry WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
94 sqlite3_stmt* stmt =
nullptr;
95 if (sqlite3_prepare_v2(db, sql_query.c_str(), -1, &stmt,
nullptr) != SQLITE_OK) {
102 string system_name = s->
getName();
106 sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_TRANSIENT);
107 sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_TRANSIENT);
108 sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_TRANSIENT);
109 sqlite3_bind_int(stmt, 4, runno);
113 constexpr int DESCRIPTION_INDEX = 19;
117 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
118 vector<string> gvolumePars;
119 const int colCount = sqlite3_column_count(stmt);
120 for (
int i = 0; i < colCount; i++) {
121 const unsigned char* colText = sqlite3_column_text(stmt, i);
122 gvolumePars.emplace_back(colText ?
reinterpret_cast<const char*
>(colText) :
"");
125 const string& volumeName = gvolumePars[0];
126 const string& solidType = gvolumePars[1];
131 auto it = meshByName.find(volumeName);
132 if (it == meshByName.end()) {
133 log->
warning(
"CAD factory: volume <", volumeName,
134 "> is defined in the database but no mesh file was found in <",
135 dirLocation,
">; skipping.");
138 gvolumePars[DESCRIPTION_INDEX] = dirLocation +
"/" + it->second;
145 if (rc != SQLITE_DONE) {
149 sqlite3_finalize(stmt);
152 log->
info(0,
"CAD factory: loaded ", loaded,
" volume(s) for system <", system_name,
153 ">, variation <", variation,
">, run ", runno,
154 " (", meshByName.size(),
" mesh file(s) present in <", dirLocation,
">).");
std::shared_ptr< GLogger > log
void warning(Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
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.
#define GSYSTEMCADTFACTORYLABEL
#define DEFAULTG4PLACEMENTTYPE
#define GSYSTEMSQLITETDEFAULTFILE
Default sqlite DB filename used when the user does not specify one.
#define ROOTWORLDGVOLUMENAME
Canonical name for the ROOT/world gvolume entry.
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)
string searchForDirInLocations(const string &dirName, const vector< string > &possibleLocations)