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