gsystem
Loading...
Searching...
No Matches
loadGeometry.cc
Go to the documentation of this file.
1
8// gsystem
10#include "gsystemConventions.h"
11
12namespace {
13 bool geometry_column_exists(sqlite3* db, const std::string& column_name) {
14 sqlite3_stmt* stmt = nullptr;
15 int rc = sqlite3_prepare_v2(db, "SELECT name FROM PRAGMA_TABLE_INFO('geometry')", -1, &stmt, nullptr);
16 if (rc != SQLITE_OK) { return false; }
17
18 bool exists = false;
19 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
20 const unsigned char* colText = sqlite3_column_text(stmt, 0);
21 if (colText != nullptr && column_name == reinterpret_cast<const char*>(colText)) {
22 exists = true;
23 break;
24 }
25 }
26
27 sqlite3_finalize(stmt);
28 return exists;
29 }
30}
31
32void GSystemSQLiteFactory::loadGeometry(GSystem* system) {
33 // skip ROOT system
34 if (system->getName() == ROOTWORLDGVOLUMENAME) { return; }
35
36 // Initialize the DB if needed.
37 if (db == nullptr) { initialize_sqlite_db(system); }
38
39 // Check that db is valid.
40 if (db == nullptr) { log->error(ERR_GSQLITEERROR, "Database pointer is still null after initialization."); }
41
42
43 const std::string placement_column = geometry_column_exists(db, "g4placement_type")
44 ? "g4placement_type"
45 : "'" DEFAULTG4PLACEMENTTYPE "' AS g4placement_type";
46 const std::string sql_query =
47 "SELECT DISTINCT name, solid, parameters, material, mother, position, rotations, " +
48 placement_column +
49 ", mfield, visible, style, color, opacity, digitization, identifier, copyOf, solidsOpr, mirror, "
50 "exist, description FROM geometry WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
51 sqlite3_stmt* stmt = nullptr;
52 int rc = sqlite3_prepare_v2(db, sql_query.c_str(), -1, &stmt, nullptr);
53 if (rc != SQLITE_OK) {
54 log->error(ERR_GSQLITEERROR, "Sqlite error preparing count query in loadGeometry: ",
55 sqlite3_errmsg(db), " (", rc, ") using query: ", sql_query);
56 }
57
58 // Bind parameters.
59 std::string experiment = system->getExperiment();
60 std::string system_name = system->getName();
61 std::string variation = system->getVariation();
62 int runno = system->getRunno();
63
64 rc = sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_STATIC);
65 if (rc != SQLITE_OK) {
66 log->error(ERR_GSQLITEERROR, "Error binding experiment: >", experiment, "<, ", sqlite3_errmsg(db));
67 }
68 rc = sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_STATIC);
69 if (rc != SQLITE_OK) {
70 log->error(ERR_GSQLITEERROR, "Error binding system name: >", system_name, "<, ", sqlite3_errmsg(db));
71 }
72 rc = sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_STATIC);
73 if (rc != SQLITE_OK) {
74 log->error(ERR_GSQLITEERROR, "Error binding variation: >", variation, "<, ", sqlite3_errmsg(db));
75 }
76 rc = sqlite3_bind_int(stmt, 4, runno);
77 if (rc != SQLITE_OK) {
78 log->error(ERR_GSQLITEERROR, "Error binding run number: >", runno, "<, ", sqlite3_errmsg(db));
79 }
80
81 // Log the expanded SQL for debugging (caller must free it).
82 if (auto sql = sqlite3_expanded_sql(stmt)) {
83 // returns char*
84 log->info(2, sql);
85 sqlite3_free(sql); // need to free the expanded SQL string
86 }
87
88 std::vector<std::string> gvolumePars;
89 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
90 int colCount = sqlite3_column_count(stmt);
91 for (int i = 0; i < colCount; i++) {
92 const char* colName = sqlite3_column_name(stmt, i);
93 const unsigned char* colText = sqlite3_column_text(stmt, i);
94
95 log->info(2, "<sqlite> column: ", (colName ? colName : "NULL"), " = ",
96 (colText ? reinterpret_cast<const char*>(colText) : "NULL"), " (column ", i, ")");
97
98 gvolumePars.emplace_back(colText ? reinterpret_cast<const char*>(colText) : "");
99 }
100 system->addGVolume(gvolumePars);
101 gvolumePars.clear();
102 }
103
104 if (rc != SQLITE_DONE) {
105 log->error(ERR_GSQLITEERROR, "Sqlite database error in loadGeometry: ",
106 sqlite3_errmsg(db), " (", rc, ")");
107 }
108
109 sqlite3_finalize(stmt);
110}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:32
int getRunno() const
Definition gsystem.h:125
std::string getExperiment() const
Definition gsystem.h:123
std::string getVariation() const
Definition gsystem.h:122
void addGVolume(std::vector< std::string > pars)
Build and add a volume from a serialized parameter list.
Definition gsystem.cc:83
std::string getName() const
Definition gsystem.h:120
Conventions and shared constants for the detector-system module.
#define DEFAULTG4PLACEMENTTYPE
#define ROOTWORLDGVOLUMENAME
Canonical name for the ROOT/world gvolume entry.
#define ERR_GSQLITEERROR