Skip to content

Commit 9911de9

Browse files
committed
gbase to gsfactory and g4sfactory
1 parent 0534015 commit 9911de9

51 files changed

Lines changed: 280 additions & 261 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dbselect/dbselectView.cc

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@ DBSelectView::DBSelectView(const std::shared_ptr<GOptions>& gopts, GDetectorCons
2323
dbhost = gopts->getScalarString("sql");
2424
experiment = gopts->getScalarString("experiment");
2525

26-
if (sqlite3_open(dbhost.c_str(), &db) != SQLITE_OK || !isGeometryTableValid()) {
27-
std::cerr << "Database Warning: Failed to open or validate database: " << dbhost << std::endl;
28-
sqlite3_close(db);
29-
db = nullptr;
26+
// try local dir, gemc installation, then example dir
27+
std::vector<std::string> dirs = {
28+
".",
29+
gutilities::gemc_root().string(),
30+
(gutilities::gemc_root() / "examples").string()
31+
};
3032

31-
std::filesystem::path gemcRoot = gutilities::gemc_root();
32-
dbhost = gemcRoot.string() + "/examples/gemc.db";
3333

34-
if (sqlite3_open(dbhost.c_str(), &db) != SQLITE_OK || !isGeometryTableValid()) {
35-
std::cerr << "Database Error: Failed to open or validate backup database: " << dbhost << std::endl;
36-
sqlite3_close(db);
37-
db = nullptr;
38-
std::exit(1);
39-
}
40-
else { std::cerr << "Database Warning: Using backup database: " << dbhost << std::endl; }
34+
auto dbPath = gutilities::searchForFileInLocations(dirs, dbhost);
35+
if (!dbPath) { log->error(ERR_GSQLITEERROR, "Failed to find database file. Exiting."); }
36+
37+
if (sqlite3_open_v2(dbPath.value().c_str(), &db, SQLITE_OPEN_READONLY, nullptr) != SQLITE_OK || !isGeometryTableValid()) {
38+
sqlite3_close(db);
39+
db = nullptr;
40+
log->error(ERR_GSQLITEERROR, " Failed to open or validate database", dbhost);
4141
}
42+
log->info(1, "Opened database: " + dbhost, " found at ", dbPath.value());
4243

4344
setupUI();
4445

@@ -56,10 +57,7 @@ DBSelectView::DBSelectView(const std::shared_ptr<GOptions>& gopts, GDetectorCons
5657
break;
5758
}
5859
}
59-
if (!expFound) {
60-
std::cerr << "Error: Experiment \"" << experiment << "\" not found in database. Exiting." << std::endl;
61-
std::exit(1);
62-
}
60+
if (!expFound) { log->error(ERR_EXPERIMENTNOTFOUND, experiment, " not found in database.", dbhost); }
6361

6462
// Apply selections from GSystem objects.
6563
applyGSystemSelections();
@@ -88,17 +86,15 @@ bool DBSelectView::isGeometryTableValid() const {
8886
sqlite3_stmt* stmt = nullptr;
8987
const char* sql_query = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='geometry'";
9088
if (sqlite3_prepare_v2(db, sql_query, -1, &stmt, nullptr) != SQLITE_OK) {
91-
std::cerr << "SQL Error: Failed to check geometry table existence: " << sqlite3_errmsg(db) << std::endl;
92-
return false;
89+
log->error(ERR_GSQLITEERROR, "SQL Error: Failed to check geometry table existence:", sqlite3_errmsg(db));
9390
}
9491
bool tableExists = false;
9592
if (sqlite3_step(stmt) == SQLITE_ROW) { tableExists = sqlite3_column_int(stmt, 0) > 0; }
9693
sqlite3_finalize(stmt);
9794
if (!tableExists) return false;
9895
sql_query = "SELECT COUNT(*) FROM geometry";
9996
if (sqlite3_prepare_v2(db, sql_query, -1, &stmt, nullptr) != SQLITE_OK) {
100-
std::cerr << "SQL Error: Failed to count rows in geometry table: " << sqlite3_errmsg(db) << std::endl;
101-
return false;
97+
log->error(ERR_GSQLITEERROR, "SQL Error: Failed to count rows in geometry table:", sqlite3_errmsg(db));
10298
}
10399
bool hasData = false;
104100
if (sqlite3_step(stmt) == SQLITE_ROW) { hasData = sqlite3_column_int(stmt, 0) > 0; }
@@ -221,10 +217,7 @@ void DBSelectView::loadExperiments() {
221217
sqlite3_stmt* stmt = nullptr;
222218
const char* sql_query = "SELECT DISTINCT experiment FROM geometry";
223219
int rc = sqlite3_prepare_v2(db, sql_query, -1, &stmt, nullptr);
224-
if (rc != SQLITE_OK) {
225-
std::cerr << "Failed to prepare experiment query: " << sqlite3_errmsg(db) << std::endl;
226-
return;
227-
}
220+
if (rc != SQLITE_OK) { log->error(ERR_GSQLITEERROR, "Failed to prepare experiment query:", sqlite3_errmsg(db)); }
228221
while (sqlite3_step(stmt) == SQLITE_ROW) {
229222
const char* expText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
230223
if (expText) {
@@ -294,7 +287,7 @@ int DBSelectView::getGeometryCount(const std::string& system, const std::string&
294287
sqlite3_bind_int(stmt, 4, run);
295288
if (sqlite3_step(stmt) == SQLITE_ROW) { count = sqlite3_column_int(stmt, 0); }
296289
}
297-
else { std::cerr << "getGeometryCount: prepare failed: " << sqlite3_errmsg(db) << std::endl; }
290+
else { log->error(ERR_GSQLITEERROR, "SQL Error: Failed togetGeometryCounte:", sqlite3_errmsg(db)); }
298291
sqlite3_finalize(stmt);
299292
return count;
300293
}
@@ -333,8 +326,7 @@ bool DBSelectView::systemAvailable(const std::string& system, const std::string&
333326
std::string query = "SELECT COUNT(*) FROM geometry WHERE system = ? AND variation = ? AND run = ?";
334327
sqlite3_stmt* stmt = nullptr;
335328
if (sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
336-
std::cerr << "systemAvailable: prepare failed: " << sqlite3_errmsg(db) << std::endl;
337-
return false;
329+
log->error(ERR_GSQLITEERROR, "SQL Error:systemAvailable: prepare failed:e:", sqlite3_errmsg(db));
338330
}
339331
sqlite3_bind_text(stmt, 1, system.c_str(), -1, SQLITE_TRANSIENT);
340332
sqlite3_bind_text(stmt, 2, variation.c_str(), -1, SQLITE_TRANSIENT);
@@ -503,9 +495,7 @@ void DBSelectView::reload_geometry() {
503495
log->info(0, SFUNCTION_NAME, ": Reloading geometry...");
504496

505497
auto reloaded_system = get_gsystems();
506-
for (auto& gsys : reloaded_system) {
507-
log->info(2, SFUNCTION_NAME, ": reloaded system: ", gsys->getName());
508-
}
498+
for (auto& gsys : reloaded_system) { log->info(2, SFUNCTION_NAME, ": reloaded system: ", gsys->getName()); }
509499

510500
// Reload the geometry using the updated GSystem objects.
511501
gDetectorConstruction->reload_geometry(reloaded_system);

examples/meson.build

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
examples_dir = get_option('prefix') + '/examples/'
2+
sql_db = get_option('prefix') + '/examples/gemc.db'
3+
24
python_exe = import('python').find_installation('python3')
35

46
install_subdir(meson.current_source_dir(), install_dir : '.')
@@ -32,18 +34,18 @@ foreach branch : examples_map.keys()
3234
root_output = '-gstreamer="[{format: root, filename: out' + branch + variation + '}]"'
3335
test(geo_test_name,
3436
python_exe,
35-
args : [example_dir + example + '.py', '-f', format, '-sql', '../../gemc.db', '-v', variation], # the sql flag is ignored for ascii format
37+
args : [example_dir + example + '.py', '-f', format, '-sql', sql_db, '-v', variation], # the sql flag is ignored for ascii format
3638
workdir : example_dir,
3739
env : { 'PYTHONDONTWRITEBYTECODE' : '1' },
3840
priority : 10)
3941
test(run_test_name_asci,
4042
gemc,
41-
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', variation: ' + variation + '}]"', ascii_output, '-sql=../../gemc.db'],
43+
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', variation: ' + variation + '}]"', ascii_output, '-sql=' + sql_db],
4244
workdir : example_dir,
4345
priority : -10)
4446
test(run_test_name_root,
4547
gemc,
46-
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', variation: ' + variation + '}]"', root_output, '-sql=../../gemc.db'],
48+
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', variation: ' + variation + '}]"', root_output, '-sql=' + sql_db],
4749
workdir : example_dir,
4850
priority : -15)
4951
endforeach
@@ -57,20 +59,20 @@ foreach branch : examples_map.keys()
5759
root_output = '-gstreamer="[{format: root, filename: out' + branch + run_value + '}]"'
5860
test(geo_test_name,
5961
python_exe,
60-
args : [example_dir + example + '.py', '-f', format, '-sql', '../../gemc.db', '-r', run_value], # the sql flag is ignored for ascii format
62+
args : [example_dir + example + '.py', '-f', format, '-sql', sql_db, '-r', run_value], # the sql flag is ignored for ascii format
6163
workdir : example_dir,
6264
env : { 'PYTHONDONTWRITEBYTECODE' : '1' },
6365
priority : 5)
6466
test(run_test_name_asci,
6567
gemc,
66-
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', runno:' + run_value + '}]"', ascii_output, '-sql=../../gemc.db'],
68+
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', runno:' + run_value + '}]"', ascii_output, '-sql=' + sql_db],
6769
workdir : example_dir,
68-
priority : -20)
70+
priority : -22)
6971
test(run_test_name_root,
7072
gemc,
71-
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', runno:' + run_value + '}]"', root_output, '-sql=../../gemc.db'],
73+
args : [yaml_file, '-gsystem="[{name: ' + example + ', factory: ' + format + ', runno:' + run_value + '}]"', root_output, '-sql=' + sql_db],
7274
workdir : example_dir,
73-
priority : -25)
75+
priority : -27)
7476
endforeach
7577

7678
endforeach

g4system/g4objectsFactories/cad/cadSystemFactory.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include <unordered_map>
1515
#include <string>
16-
#include <vector>
1716

1817
#include "g4objectsFactory.h"
1918

@@ -27,9 +26,8 @@
2726
class G4CadSystemFactory final : public G4ObjectsFactory
2827
{
2928
public:
30-
/** Default constructor / destructor. */
31-
G4CadSystemFactory() = default;
32-
~G4CadSystemFactory() override = default;
29+
// inherit the base (const std::shared_ptr<GOptions>&) ctor
30+
using G4ObjectsFactory::G4ObjectsFactory;
3331

3432
/**
3533
* @copydoc G4ObjectsFactory::className

g4system/g4objectsFactories/g4native/buildSolid.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
* @brief Implementation of G4NativeSystemFactory::buildSolid()
55
*/
66

7-
#include "g4NativeObjectsFactory.h"
87

98
// g4system
10-
#include "g4NativeObjectsFactory.h" // self
11-
#include "g4system/g4systemConventions.h"
9+
#include "g4NativeObjectsFactory.h"
10+
#include "g4systemConventions.h"
1211

1312
// geant4
1413
#include "G4Box.hh"

g4system/g4objectsFactories/g4native/checkAndReturnParameters.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
#include "g4system/g4systemConventions.h"
1111

1212

13-
14-
// ────────────────────────────────────────────────────────────────
1513
// checkAndReturnParameters – validates & parses constructor inputs
16-
// ────────────────────────────────────────────────────────────────
17-
1814
std::vector<double> G4NativeSystemFactory::checkAndReturnParameters(const GVolume* s)
1915
{
2016
// PRAGMA TODO: check non‑zero pars for various constructor

g4system/g4objectsFactories/g4native/g4NativeObjectsFactory.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <unordered_map> // std::unordered_map
1515

1616
// g4system
17-
#include "g4NativeObjectsFactory.h" // self include‑guard pattern
17+
#include "g4NativeObjectsFactory.h"
1818

1919
// gemc utility helpers
2020
#include "gutilities.h"
@@ -28,9 +28,8 @@
2828
*/
2929
class G4NativeSystemFactory final : public G4ObjectsFactory {
3030
public:
31-
/** Default constructor/destructor. */
32-
G4NativeSystemFactory() = default;
33-
~G4NativeSystemFactory() override = default;
31+
// inherit the base (const std::shared_ptr<GOptions>&) ctor
32+
using G4ObjectsFactory::G4ObjectsFactory;
3433

3534
/**
3635
* @brief Human‑readable name used by the base class for logging.

g4system/g4objectsFactories/g4objectsFactory.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
// c++
2020
#include <string_view>
2121

22-
void G4ObjectsFactory::initialize_context(const std::shared_ptr<GLogger>& logger,
23-
int check_overlaps,
24-
const std::string& backup_mat) {
25-
log = logger;
26-
checkOverlaps = check_overlaps;
27-
backupMaterial = backup_mat;
28-
log_is_assigned = true;
22+
void G4ObjectsFactory::initialize_context(int check_overlaps,
23+
const std::string& backup_mat) {
24+
checkOverlaps = check_overlaps;
25+
backupMaterial = backup_mat;
2926
}
3027

3128

g4system/g4objectsFactories/g4objectsFactory.h

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
* `buildPhysical()` if the defaults do not fit your detector geometry.
1010
*/
1111

12+
// c++
1213
#include <memory>
1314
#include <string>
1415
#include <string_view>
1516
#include <unordered_map>
1617

17-
#include "glogger.h"
18-
#include "g4system/g4volume.h"
19-
#include "gsystem/gvolume.h"
18+
// gemc
19+
#include "gbase.h"
20+
#include "gvolume.h"
2021

22+
// g4system
23+
#include "g4volume.h"
24+
#include "g4system_options.h"
25+
26+
// geant4
2127
#include "G4LogicalVolume.hh"
2228
#include "G4PVPlacement.hh"
2329
#include "G4RotationMatrix.hh"
@@ -31,17 +37,20 @@
3137
* *solid*, *logical*, and *physical* Geant4 representation.
3238
*
3339
* Call sequence inside `loadG4System()`
34-
* 1. `buildSolid()` ► *pure virtual* – derived class must implement.
40+
* 1. `buildSolid()`► *pure virtual* –derived class must implement.
3541
* 2. `buildLogical()` – default uses material lookup, may be overridden.
3642
* 3. `buildPhysical()` – default places volume, may be overridden.
3743
*
3844
* The factory short circuits if dependencies are missing, so systems can be
3945
* built in any order.
4046
*/
41-
class G4ObjectsFactory {
47+
class G4ObjectsFactory : public GBase<G4ObjectsFactory> {
4248
public:
43-
/** Virtual destructor. */
44-
virtual ~G4ObjectsFactory() = default;
49+
~G4ObjectsFactory() override = default;
50+
51+
explicit G4ObjectsFactory(const std::shared_ptr<GOptions>& g) : GBase(g, G4SFACTORY_LOGGER) {
52+
}
53+
4554

4655
/**
4756
* @brief Provide the runtime logger, overlap‑check flag, and backup material.
@@ -52,11 +61,8 @@ class G4ObjectsFactory {
5261
*
5362
* Must be invoked before the first call to `loadG4System()`.
5463
*/
55-
void initialize_context(const std::shared_ptr<GLogger>& logger,
56-
int checkOverlaps,
57-
const std::string& backupMaterial);
58-
59-
bool has_log() const noexcept { return log_is_assigned; } ///< Check if logger is assigned
64+
void initialize_context(int checkOverlaps,
65+
const std::string& backupMaterial);
6066

6167
/**
6268
* @brief Build—or retrieve—solid, logical, and physical volumes for @p s.
@@ -99,20 +105,17 @@ class G4ObjectsFactory {
99105
std::unordered_map<std::string, G4Volume*>* g4s);
100106

101107

102-
// ──────── static convenience helpers ───────────────────────────
103-
/// Lookup solid in `g4s` map.
104-
static G4VSolid* getSolidFromMap(const std::string& volume_name,
105-
std::unordered_map<std::string, G4Volume*>* g4s);
106-
/// Lookup logical volume in `g4s` map.
107-
static G4LogicalVolume* getLogicalFromMap(const std::string& volume_name,
108-
std::unordered_map<std::string, G4Volume*>* g4s);
109-
/// Lookup physical volume in `g4s` map.
110-
static G4VPhysicalVolume* getPhysicalFromMap(const std::string& volume_name,
111-
std::unordered_map<std::string, G4Volume*>* g4s);
112-
113-
// get or create new GVolume in the map
114-
G4Volume* getOrCreateG4Volume(const std::string& volume_name,
115-
std::unordered_map<std::string, G4Volume*>* g4s) {
108+
/// Lookup solid in the `g4s` map.
109+
static G4VSolid* getSolidFromMap(const std::string& volume_name, std::unordered_map<std::string, G4Volume*>* g4s);
110+
111+
/// Lookup logical volume in the `g4s` map.
112+
static G4LogicalVolume* getLogicalFromMap(const std::string& volume_name, std::unordered_map<std::string, G4Volume*>* g4s);
113+
114+
/// Lookup physical volume in the `g4s` map.
115+
static G4VPhysicalVolume* getPhysicalFromMap(const std::string& volume_name, std::unordered_map<std::string, G4Volume*>* g4s);
116+
117+
// get or create a new GVolume in the map
118+
G4Volume* getOrCreateG4Volume(const std::string& volume_name, std::unordered_map<std::string, G4Volume*>* g4s) {
116119
if (auto it = g4s->find(volume_name); it != g4s->end()) { return it->second; }
117120
else {
118121
(*g4s)[volume_name] = new G4Volume();
@@ -163,13 +166,6 @@ class G4ObjectsFactory {
163166
static G4RotationMatrix* getRotation(const GVolume* s); ///< Parse rotation string.
164167
static G4ThreeVector getPosition(const GVolume* s); ///< Parse position + shift.
165168

166-
// ──────── data members ─────────────────────────────────────────
167-
/**
168-
* @brief Logger used for info/debug/error output.
169-
*
170-
* May remain `nullptr` until `assign_logger()` is called.
171-
*/
172-
std::shared_ptr<GLogger> log;
173169

174170
/** Forwarded to `G4PVPlacement` overlap‑check flag. */
175171
int checkOverlaps{0};
@@ -181,7 +177,4 @@ class G4ObjectsFactory {
181177
*/
182178
std::string backupMaterial;
183179

184-
private:
185-
bool log_is_assigned{false}; ///< Flag to check if logger is assigned
186-
187180
};

g4system/g4system_options.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ namespace g4system {
88

99
// returns array of options definitions
1010
GOptions defineOptions() {
11+
1112
GOptions goptions(G4SYSTEM_LOGGER);
13+
goptions += GOptions(G4SFACTORY_LOGGER);
14+
1215
goptions += gsystem::defineOptions();
1316

1417
std::string help =

g4system/g4system_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "goptions.h"
55

66
constexpr const char* G4SYSTEM_LOGGER = "g4system";
7+
constexpr const char* G4SFACTORY_LOGGER = "g4sfactory";
78

89
namespace g4system {
910

0 commit comments

Comments
 (0)