Skip to content

Commit 1baa737

Browse files
committed
making inputdata a global instance
1 parent 590e6a0 commit 1baa737

35 files changed

+152
-102
lines changed

common/framework/include/OMSim.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @brief Main simulation class for the Optical Module (OM) simulation.
44
* This file defines the OMSim class, which controls the entire simulation process in all studies and includes the general program options.
55
* @warning
6-
* There are a few material related arguments that are depracated as for example the glass and gel arguments. This were used to easily change materials during the OM development phase. Check @link InputDataManager::getMaterial @endlink and modify the respective OM class if you want to use these args.
6+
* There are a few material related arguments that are depracated as for example the glass and gel arguments. This were used to easily change materials during the OM development phase. Check @link OMSimInputData::getMaterial @endlink and modify the respective OM class if you want to use these args.
77
* @ingroup common
88
*/
99

common/framework/include/OMSimCommandArgsTable.hh

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@
3131
#include <boost/any.hpp>
3232
#include <sys/time.h>
3333

34+
35+
// Forward declaration of the class
36+
class OMSimCommandArgsTable;
37+
38+
// Declaration and definition of the inline global pointer
39+
inline OMSimCommandArgsTable* gCommandArgsTable = nullptr;
40+
41+
3442
/**
3543
* @class OMSimCommandArgsTable
36-
* @brief A singleton class used to hold OMSim command arguments.
44+
* @brief A class used to hold OMSim command arguments with global instance access.
3745
*
38-
* This class uses a map to hold key-value pairs of simulation command arguments. In principle it is just a wrapper around the map created by the boost library to avoid users changing the arg values after initialisation.
39-
* Provides a method to write the parameters to a JSON file.
46+
* This class uses a map to hold key-value pairs of simulation command arguments.
47+
* It provides a method to write the parameters to a JSON file.
4048
* @ingroup common
4149
*/
4250
class OMSimCommandArgsTable
@@ -50,14 +58,27 @@ public:
5058
using Key = std::string;
5159
using Value = boost::any; // Using boost::any to hold any type
5260

61+
static void init()
62+
{
63+
if (!gCommandArgsTable)
64+
gCommandArgsTable = new OMSimCommandArgsTable();
65+
}
66+
67+
static void shutdown()
68+
{
69+
delete gCommandArgsTable;
70+
gCommandArgsTable = nullptr;
71+
}
72+
5373
/**
54-
* @brief Retrieves the instance of the singleton.
55-
* @return The instance of OMSimCommandArgsTable.
74+
* @return A reference to the OMSimCommandArgsTable instance.
75+
* @throw std::runtime_error if accessed before initialization or after shutdown.
5676
*/
5777
static OMSimCommandArgsTable &getInstance()
5878
{
59-
static OMSimCommandArgsTable instance;
60-
return instance;
79+
if (!gCommandArgsTable)
80+
throw std::runtime_error("OMSimCommandArgsTable accessed before initialization or after shutdown!");
81+
return *gCommandArgsTable;
6182
}
6283

6384
/**

common/framework/include/OMSimInputData.hh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @file
2-
* @brief Definition of ParameterTable and InputDataManager.
2+
* @brief Definition of ParameterTable and OMSimInputData.
33
* @ingroup common
44
*/
55

@@ -20,7 +20,7 @@ namespace pt = boost::property_tree;
2020
* Interface for handling and querying data in the form of a property tree of the boost library. It facilitates the extraction of specific
2121
* parameters from loaded JSON files while also supporting units and scales.
2222
*
23-
* Its main use is as base class of InputDataManager, but you may use it as needed (see @ref ExampleUsageParameterTable).
23+
* Its main use is as base class of OMSimInputData, but you may use it as needed (see @ref ExampleUsageParameterTable).
2424
*
2525
* @ingroup common
2626
*
@@ -165,7 +165,7 @@ private:
165165
};
166166

167167
/**
168-
* @class InputDataManager
168+
* @class OMSimInputData
169169
* @brief Manages the input data, including parsing and storing material properties.
170170
*
171171
* @details
@@ -178,7 +178,7 @@ private:
178178
* Example usage (see also @ref ExampleUsageParameterTable):
179179
*
180180
* @code
181-
* InputDataManager lManager;
181+
* OMSimInputData lManager;
182182
* lManager.searchFolders(); // Search for all recognized data files in the predefined directories
183183
* G4Material* lWater = manager.getMaterial("CustomWater"); // Retrieve a Geant4 material by name
184184
* G4OpticalSurface* lSurface = manager.getOpticalSurface("SomeSurfaceName"); // Retrieve an optical surface by name
@@ -190,21 +190,28 @@ private:
190190
*
191191
* @ingroup common
192192
*/
193-
class InputDataManager : public ParameterTable
193+
class OMSimInputData : public ParameterTable
194194
{
195195
public:
196-
InputDataManager(){};
197-
~InputDataManager(){};
196+
static void init();
197+
static void shutdown();
198+
static OMSimInputData& getInstance();
198199
G4Material *getMaterial(G4String pName);
199200
G4OpticalSurface *getOpticalSurface(G4String pName);
200201
void searchFolders();
201202
std::map<G4String, G4OpticalSurface *> mOpticalSurfaceMap; ///< Map that links names with optical surfaces.
202203

203204
private:
205+
OMSimInputData() = default;
206+
~OMSimInputData() = default;
207+
OMSimInputData(const OMSimInputData&) = delete;
208+
OMSimInputData& operator=(const OMSimInputData&) = delete;
204209
void scannDataDirectory();
205210
void processFile(const std::string &filePath, const std::string &fileName);
206211
G4String mDataDirectory; ///< The current directory being scanned for data.
207212
};
208213

214+
inline OMSimInputData* gOMSimInputData = nullptr;
215+
209216
#endif
210217
//

common/framework/src/OMSim.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @brief Implementation of the OMSim class.
44
*
55
* @warning
6-
* There are a few material related arguments that are depracated as for example the glass and gel arguments. This were used to easily change materials during the OM development phase. Check @link InputDataManager::getMaterial @endlink and modify the respective OM class if you want to use these args.
6+
* There are a few material related arguments that are depracated as for example the glass and gel arguments. This were used to easily change materials during the OM development phase. Check @link OMSimInputData::getMaterial @endlink and modify the respective OM class if you want to use these args.
77
*
88
* @ingroup common
99
*/
@@ -23,6 +23,7 @@ mRunManager(nullptr),
2323
mVisManager(nullptr),
2424
mNavigator(nullptr)
2525
{
26+
OMSimCommandArgsTable::init();
2627
setGeneralOptions();
2728
initialLoggerConfiguration();
2829
}
@@ -145,9 +146,9 @@ int OMSim::determineNumberOfThreads()
145146
*/
146147
void OMSim::initialiseSimulation(OMSimDetectorConstruction* pDetectorConstruction)
147148
{
148-
configureLogger();
149149
OMSimHitManager::init();
150-
150+
configureLogger();
151+
151152
OMSimCommandArgsTable &lArgs = OMSimCommandArgsTable::getInstance();
152153
ensureOutputDirectoryExists(lArgs.get<std::string>("output_file"));
153154

@@ -280,6 +281,8 @@ OMSim::~OMSim()
280281
log_trace("Deleting OMSimHitManager");
281282
OMSimHitManager::shutdown();
282283

284+
log_trace("Deleting OMSimCommandArgsTable");
285+
OMSimCommandArgsTable::shutdown();
283286

284287
log_trace("OMSim destructor finished");
285288
std::chrono::high_resolution_clock::time_point lFinishtime = std::chrono::high_resolution_clock::now();

common/framework/src/OMSimInputData.cc

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ G4bool ParameterTable::checkIfKeyInTable(G4String pKey)
116116
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117117
*/
118118

119+
120+
void OMSimInputData::init()
121+
{
122+
if (!gOMSimInputData){
123+
gOMSimInputData = new OMSimInputData();
124+
gOMSimInputData->searchFolders();
125+
}
126+
127+
}
128+
129+
void OMSimInputData::shutdown()
130+
{
131+
delete gOMSimInputData;
132+
gOMSimInputData = nullptr;
133+
}
134+
135+
/**
136+
* @return A reference to the OMSimInputData instance.
137+
* @throw std::runtime_error if accessed before initialization or after shutdown.
138+
*/
139+
OMSimInputData& OMSimInputData::getInstance()
140+
{
141+
if (!gOMSimInputData)
142+
throw std::runtime_error("OMSimInputData accessed before initialization or after shutdown!");
143+
return *gOMSimInputData;
144+
}
145+
146+
119147
/**
120148
* Get a G4Material. In order to get custom built materials, method
121149
* searchFolders() should have already been called. Standard materials from
@@ -127,7 +155,7 @@ G4bool ParameterTable::checkIfKeyInTable(G4String pKey)
127155
* "argWorld" for argument materials
128156
* @return G4Material
129157
*/
130-
G4Material *InputDataManager::getMaterial(G4String pName)
158+
G4Material *OMSimInputData::getMaterial(G4String pName)
131159
{
132160

133161
// Check if requested material is an argument material
@@ -173,7 +201,7 @@ G4Material *InputDataManager::getMaterial(G4String pName)
173201
* "argReflector"
174202
* @return G4OpticalSurface
175203
*/
176-
G4OpticalSurface *InputDataManager::getOpticalSurface(G4String pName)
204+
G4OpticalSurface *OMSimInputData::getOpticalSurface(G4String pName)
177205
{
178206

179207
// Check if requested material is an argument surface
@@ -204,7 +232,7 @@ G4OpticalSurface *InputDataManager::getOpticalSurface(G4String pName)
204232
/**
205233
* @brief Searches through predefined folders for input data files.
206234
*/
207-
void InputDataManager::searchFolders()
235+
void OMSimInputData::searchFolders()
208236
{
209237
log_trace("Searching folders for data json files...");
210238
std::vector<std::string> directories = {
@@ -233,7 +261,7 @@ void InputDataManager::searchFolders()
233261
* @param pFilePath Full path to the file.
234262
* @param pFileName Name of the file (without the path).
235263
*/
236-
void InputDataManager::processFile(const std::string &pFilePath,
264+
void OMSimInputData::processFile(const std::string &pFilePath,
237265
const std::string &pFileName)
238266
{
239267
log_trace("Processing file {}", pFileName);
@@ -284,7 +312,7 @@ void InputDataManager::processFile(const std::string &pFilePath,
284312
* Scann for data files inside mDataDirectory and process files.
285313
* @param pName name of the material
286314
*/
287-
void InputDataManager::scannDataDirectory()
315+
void OMSimInputData::scannDataDirectory()
288316
{
289317
log_trace("Loading files in {}", mDataDirectory);
290318
DIR *lDirectory = opendir(mDataDirectory.data());

common/geometry_construction/include/OMSimDEGG.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DEggHarness;
2323
class DEGG : public OMSimOpticalModule
2424
{
2525
public:
26-
DEGG(InputDataManager *pData, G4bool pPlaceHarness = true);
26+
DEGG(G4bool pPlaceHarness = true);
2727
~DEGG(){};
2828

2929
void construction();

common/geometry_construction/include/OMSimDEGGHarness.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class DEGG;
1313
class DEggHarness : public abcDetectorComponent
1414
{
1515
public:
16-
// DEggHarness(InputDataManager *pData);
17-
DEggHarness(DEGG *pDEGG, InputDataManager *pData);
16+
DEggHarness(DEGG *pDEGG);
1817
void construction();
1918
G4String mDataKey = "om_DEGG_Harness";
2019

common/geometry_construction/include/OMSimDetectorConstruction.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public:
2727
void ConstructSDandField() override;
2828
void registerSensitiveDetector(G4LogicalVolume* logVol, G4VSensitiveDetector* aSD);
2929
G4VPhysicalVolume *mWorldPhysical;
30-
InputDataManager* getDataManager() {return mData;};
30+
OMSimInputData* getDataManager() {return mData;};
3131

3232
protected:
3333
G4VSolid *mWorldSolid;
3434
G4LogicalVolume *mWorldLogical;
3535
virtual void constructWorld() = 0;
3636
virtual void constructDetector() = 0;
37-
InputDataManager *mData;
37+
OMSimInputData *mData;
3838

3939
struct SDInfo {
4040
G4LogicalVolume* logicalVolume;

common/geometry_construction/include/OMSimLOM16.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class LOM16 : public OMSimOpticalModule
1313
{
1414
public:
15-
LOM16(InputDataManager* pData, G4bool pPlaceHarness = false);
15+
LOM16(G4bool pPlaceHarness = false);
1616
~LOM16();
1717
void construction();
1818
double getPressureVesselWeight() {return (5.38+5.35)*kg;};

common/geometry_construction/include/OMSimLOM18.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class LOM18 : public OMSimOpticalModule
1515
{
1616
public:
17-
LOM18(InputDataManager* pData, G4bool pPlaceHarness = false);
17+
LOM18(G4bool pPlaceHarness = false);
1818
~LOM18();
1919
void construction();
2020
double getPressureVesselWeight() {return 17.0*kg;};

0 commit comments

Comments
 (0)