gsystem
Loading...
Searching...
No Matches
loadMaterials.cc
Go to the documentation of this file.
1
7
8// gsystem
10#include "gsystemConventions.h"
11
12// c++
13using namespace std;
14
15
16void GSystemSQLiteFactory::loadMaterials(GSystem* system) {
17 // skip ROOT system
18 if (system->getName() == gsystem::ROOTWORLDGVOLUMENAME) { return; }
19
20 // Initialize the DB if needed.
21 if (db == nullptr) { initialize_sqlite_db(system); }
22
23 // Check that db is valid.
24 if (db == nullptr) {
25 log->error(gsystem::ERR_GSQLITEERROR, "Database pointer is still null after initialization.");
26 }
27
28 // Check if the materials table has any rows.
29 // An empty materials table can be valid if materials come from the Geant4 database.
30 int count = 0;
31 const char* count_query = "SELECT COUNT(*) FROM materials";
32 sqlite3_stmt* count_stmt = nullptr;
33 int rc = sqlite3_prepare_v2(db, count_query, -1, &count_stmt, nullptr);
34 if (rc != SQLITE_OK) {
35 log->error(
37 "Sqlite error preparing count query in loadMaterials: %1 (%2) using query: %3", sqlite3_errmsg(db),
38 rc, count_query);
39 }
40 if (sqlite3_step(count_stmt) == SQLITE_ROW) { count = sqlite3_column_int(count_stmt, 0); }
41 sqlite3_finalize(count_stmt);
42
43 if (count == 0) {
44 log->info(
45 2,
46 "Table 'materials' is empty for system <%1>, variation <%2>, run %3. This may be ok if the "
47 "materials are from the Geant4 database.", system_name, variation, runno);
48 return;
49 }
50
51
52 // Prepare the SQL query.
53 const char* sql_query = "SELECT DISTINCT * FROM materials WHERE system = ? AND variation = ? AND run = ?";
54 sqlite3_stmt* stmt = nullptr;
55 rc = sqlite3_prepare_v2(db, sql_query, -1, &stmt, nullptr);
56 if (rc != SQLITE_OK) {
57 log->error(
58 gsystem::ERR_GSQLITEERROR, "Sqlite database error in loadMaterials: %1 (%2) using query: %3",
59 sqlite3_errmsg(db), rc, sql_query);
60 }
61
62 // Bind parameters.
63 string system_name = system->getName();
64 string variation = system->getVariation();
65 int runno = system->getRunno();
66
67 sqlite3_bind_text(stmt, 1, system_name.c_str(), -1, SQLITE_STATIC);
68 sqlite3_bind_text(stmt, 2, variation.c_str(), -1, SQLITE_STATIC);
69 sqlite3_bind_int(stmt, 3, runno);
70
71
72 vector<string> gmaterialPars;
73 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
74 const int ncols = sqlite3_column_count(stmt);
75 for (int i = 0; i < ncols; ++i) {
76 const char* cname = sqlite3_column_name(stmt, i);
77
78 // Safely get text (use a literal "NULL" for SQLITE_NULL)
79 const char* ctext =
80 (sqlite3_column_type(stmt, i) == SQLITE_NULL)
81 ? "NULL"
82 : reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
83
84 log->info(2, "<sqlite> column: %1 = %2", cname, ctext);
85
86 // The first columns are metadata; columns beyond index 4 are GMaterial constructor parameters.
87 if (i > 4) {
88 // Push a string even for NULLs to avoid nullptr UB.
89 gmaterialPars.emplace_back(ctext); // will be "NULL" string for NULL columns
90 }
91 }
92 system->addGMaterial(gmaterialPars);
93 gmaterialPars.clear();
94 }
95
96
97 if (rc != SQLITE_DONE) {
98 log->error(
99 gsystem::ERR_GSQLITEERROR, "Sqlite database error in loadMaterials: %1 (%2)", sqlite3_errmsg(db),
100 rc);
101 }
102
103
104 sqlite3_finalize(stmt);
105}
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 getVariation() const
Definition gsystem.h:125
void addGMaterial(std::vector< std::string > pars)
Build and add a material from a serialized parameter list.
Definition gsystem.cc:175
std::string getName() const
Definition gsystem.h:123
Conventions and shared constants for the detector-system module.
constexpr int ERR_GSQLITEERROR
constexpr char ROOTWORLDGVOLUMENAME[]
Canonical name for the ROOT/world gvolume entry.