gsystem
Loading...
Searching...
No Matches
loadGeometry.cc
Go to the documentation of this file.
1
8// gsystem
10#include "gsystemConventions.h"
11
12void GSystemSQLiteFactory::loadGeometry(GSystem* system) {
13 // skip ROOT system
14 if (system->getName() == ROOTWORLDGVOLUMENAME) { return; }
15
16 // Initialize the DB if needed.
17 if (db == nullptr) { initialize_sqlite_db(system); }
18
19 // Check that db is valid.
20 if (db == nullptr) { log->error(ERR_GSQLITEERROR, "Database pointer is still null after initialization."); }
21
22
23 const char* sql_query =
24 "SELECT DISTINCT * FROM geometry WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
25 sqlite3_stmt* stmt = nullptr;
26 int rc = sqlite3_prepare_v2(db, sql_query, -1, &stmt, nullptr);
27 if (rc != SQLITE_OK) {
28 log->error(ERR_GSQLITEERROR, "Sqlite error preparing count query in loadGeometry: ",
29 sqlite3_errmsg(db), " (", rc, ") using query: ", sql_query);
30 }
31
32 // Bind parameters.
33 std::string experiment = system->getExperiment();
34 std::string system_name = system->getName();
35 std::string variation = system->getVariation();
36 int runno = system->getRunno();
37
38 rc = sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_STATIC);
39 if (rc != SQLITE_OK) {
40 log->error(ERR_GSQLITEERROR, "Error binding experiment: >", experiment, "<, ", sqlite3_errmsg(db));
41 }
42 rc = sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_STATIC);
43 if (rc != SQLITE_OK) {
44 log->error(ERR_GSQLITEERROR, "Error binding system name: >", system_name, "<, ", sqlite3_errmsg(db));
45 }
46 rc = sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_STATIC);
47 if (rc != SQLITE_OK) {
48 log->error(ERR_GSQLITEERROR, "Error binding variation: >", variation, "<, ", sqlite3_errmsg(db));
49 }
50 rc = sqlite3_bind_int(stmt, 4, runno);
51 if (rc != SQLITE_OK) {
52 log->error(ERR_GSQLITEERROR, "Error binding run number: >", runno, "<, ", sqlite3_errmsg(db));
53 }
54
55 // Log the expanded SQL for debugging (caller must free it).
56 if (auto sql = sqlite3_expanded_sql(stmt)) {
57 // returns char*
58 log->info(2, sql);
59 sqlite3_free(sql); // need to free the expanded SQL string
60 }
61
62 std::vector<std::string> gvolumePars;
63 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
64 int colCount = sqlite3_column_count(stmt);
65 for (int i = 0; i < colCount; i++) {
66 const char* colName = sqlite3_column_name(stmt, i);
67 const unsigned char* colText = sqlite3_column_text(stmt, i);
68
69 log->info(2, "<sqlite> column: ", (colName ? colName : "NULL"), " = ",
70 (colText ? reinterpret_cast<const char*>(colText) : "NULL"), " (column ", i, ")");
71
72 // The first columns are metadata; columns beyond index 4 are GVolume constructor parameters.
73 if (i > 4) {
74 colText = sqlite3_column_text(stmt, i);
75 gvolumePars.emplace_back(colText ? reinterpret_cast<const char*>(colText) : "");
76 }
77 }
78 system->addGVolume(gvolumePars);
79 gvolumePars.clear();
80 }
81
82 if (rc != SQLITE_DONE) {
83 log->error(ERR_GSQLITEERROR, "Sqlite database error in loadGeometry: ",
84 sqlite3_errmsg(db), " (", rc, ")");
85 }
86
87 sqlite3_finalize(stmt);
88}
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:113
std::string getExperiment() const
Definition gsystem.h:111
std::string getVariation() const
Definition gsystem.h:110
void addGVolume(std::vector< std::string > pars)
Build and add a volume from a serialized parameter list.
Definition gsystem.cc:77
std::string getName() const
Definition gsystem.h:108
Conventions and shared constants for the detector-system module.
#define ROOTWORLDGVOLUMENAME
Canonical name for the ROOT/world gvolume entry.
#define ERR_GSQLITEERROR