19 bool numeric_value(
const std::string& value) {
23 static_cast<void>(std::stod(value, &parsed));
24 return parsed == value.size();
26 catch (
const std::exception&) {
return false; }
29 std::string cad_mesh_reference(
const std::string& parameters,
const std::string& description) {
32 (values.size() == 1 && (values[0] ==
"NULL" || numeric_value(values[0])))) {
38 void set_resolved_cad_mesh(std::vector<std::string>& row,
const std::string& resolved) {
39 constexpr int PARAMETERS_INDEX = 2;
40 constexpr int DESCRIPTION_INDEX = 19;
44 (values.size() == 1 && (values[0] ==
"NULL" || numeric_value(values[0])))) {
45 row[DESCRIPTION_INDEX] = resolved;
49 const auto delimiter = row[PARAMETERS_INDEX].find(
',');
50 const auto suffix = delimiter == std::string::npos ?
"" : row[PARAMETERS_INDEX].substr(delimiter);
51 row[PARAMETERS_INDEX] = resolved + suffix;
54 bool geometry_column_exists(sqlite3* db,
const std::string& column_name) {
55 sqlite3_stmt* stmt =
nullptr;
56 int rc = sqlite3_prepare_v2(db,
"SELECT name FROM PRAGMA_TABLE_INFO('geometry')", -1, &stmt,
nullptr);
57 if (rc != SQLITE_OK) {
return false; }
60 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
61 const unsigned char* colText = sqlite3_column_text(stmt, 0);
62 if (colText !=
nullptr && column_name ==
reinterpret_cast<const char*
>(colText)) {
68 sqlite3_finalize(stmt);
73void GSystemSQLiteFactory::loadGeometry(
GSystem* system) {
78 if (db ==
nullptr) { initialize_sqlite_db(system); }
85 const std::string placement_column = geometry_column_exists(db,
"g4placement_type")
88 "' AS g4placement_type";
89 const std::string sql_query =
90 "SELECT DISTINCT name, solid, parameters, material, mother, position, rotations, " +
92 ", mfield, visible, style, color, opacity, digitization, identifier, copyOf, solidsOpr, mirror, "
93 "exist, description FROM geometry WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
94 sqlite3_stmt* stmt =
nullptr;
95 int rc = sqlite3_prepare_v2(db, sql_query.c_str(), -1, &stmt,
nullptr);
96 if (rc != SQLITE_OK) {
99 "Sqlite error preparing count query in loadGeometry: %1 (%2) using query: %3", sqlite3_errmsg(db),
105 std::string system_name = system->
getName();
109 rc = sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_STATIC);
110 if (rc != SQLITE_OK) {
114 rc = sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_STATIC);
115 if (rc != SQLITE_OK) {
119 rc = sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_STATIC);
120 if (rc != SQLITE_OK) {
124 rc = sqlite3_bind_int(stmt, 4, runno);
125 if (rc != SQLITE_OK) {
130 if (
auto sql = sqlite3_expanded_sql(stmt)) {
132 log->info(2,
"%1", sql);
136 std::vector<std::string> gvolumePars;
137 std::vector<std::string> cadSearchDirectories;
140 if (!std::filesystem::is_directory(location, ec)) {
continue; }
142 cadSearchDirectories.push_back(location);
143 const auto systemLocation = std::filesystem::path(location) / system->
getName();
144 if (std::filesystem::is_directory(systemLocation, ec)) {
145 cadSearchDirectories.push_back(systemLocation.string());
148 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
149 int colCount = sqlite3_column_count(stmt);
150 for (
int i = 0; i < colCount; i++) {
151 const char* colName = sqlite3_column_name(stmt, i);
152 const unsigned char* colText = sqlite3_column_text(stmt, i);
155 2,
"<sqlite> column: %1 = %2 (column %3)", (colName ? colName :
"NULL"),
156 (colText ?
reinterpret_cast<const char*
>(colText) :
"NULL"), i);
158 gvolumePars.emplace_back(colText ?
reinterpret_cast<const char*
>(colText) :
"");
164 const auto meshReference = cad_mesh_reference(gvolumePars[2], gvolumePars[19]);
169 "SQLite factory: CAD volume <%1> references missing mesh <%2>; skipping.", gvolumePars[0],
174 set_resolved_cad_mesh(gvolumePars, meshPath.value());
181 if (rc != SQLITE_DONE) {
187 sqlite3_finalize(stmt);
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 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
Conventions and shared constants for the detector-system module.
constexpr char DEFAULTG4PLACEMENTTYPE[]
constexpr char GSYSTEMCADTFACTORYLABEL[]
constexpr int ERR_GSQLITEERROR
constexpr char ROOTWORLDGVOLUMENAME[]
Canonical name for the ROOT/world gvolume entry.
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
std::optional< std::string > searchForFileInLocations(const std::vector< std::string > &locations, std::string_view filename)