gsystem
Loading...
Searching...
No Matches
loadMirrors.cc
Go to the documentation of this file.
1
7
8// gsystem
10#include "gsystemConventions.h"
11
12namespace {
13bool mirrors_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('mirrors')", -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::loadMirrors(GSystem* system) {
33 // skip ROOT system
34 if (system->getName() == gsystem::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) {
41 log->error(gsystem::ERR_GSQLITEERROR, "Database pointer is still null after initialization.");
42 }
43
44 // Databases created before mirror support may lack the table, and databases whose
45 // systems define no mirrors carry a bare table with only the id column: mirrors
46 // are optional in both cases.
47 if (!table_exists(db, "mirrors") || !mirrors_column_exists(db, "name")) {
48 log->info(2, "No usable 'mirrors' table in the database. This is ok if no system defines mirrors.");
49 return;
50 }
51
52 // Columns are selected explicitly by name (in the canonical GMirror parameter order)
53 // so the loader does not depend on the table column order.
54 const std::string transmittance_column = mirrors_column_exists(db, "transmittance")
55 ? "transmittance"
56 : "NULL AS transmittance";
57 const std::string sql_query =
58 "SELECT name, description, type, finish, model, border, matOptProps, photonEnergy, "
59 "indexOfRefraction, reflectivity, efficiency, specularlobe, specularspike, backscatter, "
60 + transmittance_column +
61 ", sigmaAlpha FROM mirrors WHERE experiment = ? AND system = ? AND variation = ? AND run = ?";
62 sqlite3_stmt* stmt = nullptr;
63 int rc = sqlite3_prepare_v2(db, sql_query.c_str(), -1, &stmt, nullptr);
64 if (rc != SQLITE_OK) {
65 log->error(gsystem::ERR_GSQLITEERROR, "Sqlite error preparing query in loadMirrors: ",
66 sqlite3_errmsg(db), " (", rc, ") using query: ", sql_query);
67 }
68
69 // Bind parameters.
70 std::string experiment = system->getExperiment();
71 std::string system_name = system->getName();
72 std::string variation = system->getVariation();
73 int runno = system->getRunno();
74
75 sqlite3_bind_text(stmt, 1, experiment.c_str(), -1, SQLITE_STATIC);
76 sqlite3_bind_text(stmt, 2, system_name.c_str(), -1, SQLITE_STATIC);
77 sqlite3_bind_text(stmt, 3, variation.c_str(), -1, SQLITE_STATIC);
78 sqlite3_bind_int(stmt, 4, runno);
79
80 // Log the expanded SQL for debugging (caller must free it).
81 if (auto sql = sqlite3_expanded_sql(stmt)) {
82 log->info(2, sql);
83 sqlite3_free(sql);
84 }
85
86 std::vector<std::string> gmirrorPars;
87 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
88 const int ncols = sqlite3_column_count(stmt);
89 for (int i = 0; i < ncols; ++i) {
90 const char* cname = sqlite3_column_name(stmt, i);
91
92 // Safely get text (use a literal "NULL" for SQLITE_NULL)
93 const char* ctext =
94 (sqlite3_column_type(stmt, i) == SQLITE_NULL)
95 ? "NULL"
96 : reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
97
98 log->info(2, "<sqlite> column: ", cname, " = ", ctext);
99
100 gmirrorPars.emplace_back(ctext);
101 }
102 system->addGMirror(gmirrorPars);
103 gmirrorPars.clear();
104 }
105
106 if (rc != SQLITE_DONE) {
107 log->error(gsystem::ERR_GSQLITEERROR, "Sqlite database error in loadMirrors: ",
108 sqlite3_errmsg(db), " (", rc, ")");
109 }
110
111 sqlite3_finalize(stmt);
112}
std::shared_ptr< GLogger > log
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:34
int getRunno() const
Definition gsystem.h:128
std::string getExperiment() const
Definition gsystem.h:126
std::string getVariation() const
Definition gsystem.h:125
std::string getName() const
Definition gsystem.h:123
void addGMirror(std::vector< std::string > pars)
Build and add a mirror (optical surface) from a serialized parameter list.
Definition gsystem.cc:194
Conventions and shared constants for the detector-system module.
constexpr int ERR_GSQLITEERROR
constexpr char ROOTWORLDGVOLUMENAME[]
Canonical name for the ROOT/world gvolume entry.