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(gsystem::ERR_GSQLITEERROR, "Sqlite error preparing count query in loadMaterials: ",
36 sqlite3_errmsg(db), " (", rc, ") using query: ", count_query);
37 }
38 if (sqlite3_step(count_stmt) == SQLITE_ROW) { count = sqlite3_column_int(count_stmt, 0); }
39 sqlite3_finalize(count_stmt);
40
41 if (count == 0) {
42 log->info(2, "Table 'materials' is empty for system <", system_name, ">, variation <", variation, ">, "
43 "run ", runno, ". This may be ok if the materials are from the Geant4 database.");
44 return;
45 }
46
47
48 // Prepare the SQL query.
49 const char* sql_query = "SELECT DISTINCT * FROM materials WHERE system = ? AND variation = ? AND run = ?";
50 sqlite3_stmt* stmt = nullptr;
51 rc = sqlite3_prepare_v2(db, sql_query, -1, &stmt, nullptr);
52 if (rc != SQLITE_OK) {
53 log->error(gsystem::ERR_GSQLITEERROR, "Sqlite database error in loadMaterials: ",
54 sqlite3_errmsg(db), " (", rc, ") using query: ", sql_query);
55 }
56
57 // Bind parameters.
58 string system_name = system->getName();
59 string variation = system->getVariation();
60 int runno = system->getRunno();
61
62 sqlite3_bind_text(stmt, 1, system_name.c_str(), -1, SQLITE_STATIC);
63 sqlite3_bind_text(stmt, 2, variation.c_str(), -1, SQLITE_STATIC);
64 sqlite3_bind_int(stmt, 3, runno);
65
66
67 vector<string> gmaterialPars;
68 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
69 const int ncols = sqlite3_column_count(stmt);
70 for (int i = 0; i < ncols; ++i) {
71 const char* cname = sqlite3_column_name(stmt, i);
72
73 // Safely get text (use a literal "NULL" for SQLITE_NULL)
74 const char* ctext =
75 (sqlite3_column_type(stmt, i) == SQLITE_NULL)
76 ? "NULL"
77 : reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
78
79 log->info(2, "<sqlite> column: ", cname, " = ", ctext);
80
81 // The first columns are metadata; columns beyond index 4 are GMaterial constructor parameters.
82 if (i > 4) {
83 // Push a string even for NULLs to avoid nullptr UB.
84 gmaterialPars.emplace_back(ctext); // will be "NULL" string for NULL columns
85 }
86 }
87 system->addGMaterial(gmaterialPars);
88 gmaterialPars.clear();
89 }
90
91
92 if (rc != SQLITE_DONE) {
93 log->error(gsystem::ERR_GSQLITEERROR, "Sqlite database error in loadMaterials: ",
94 sqlite3_errmsg(db), " (", rc, ")");
95 }
96
97
98 sqlite3_finalize(stmt);
99}
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:176
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.