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