File indexing completed on 2025-07-11 08:30:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DDG4_GEANT4KERNEL_H
0014 #define DDG4_GEANT4KERNEL_H
0015
0016
0017 #include <DDG4/Geant4ActionContainer.h>
0018
0019
0020 #include <map>
0021 #include <typeinfo>
0022 #include <functional>
0023
0024
0025 class G4RunManager;
0026 class G4UIdirectory;
0027 class G4VPhysicalVolume;
0028
0029
0030 namespace dd4hep {
0031
0032
0033 class Geant4Interrupts;
0034
0035
0036 namespace sim {
0037
0038
0039 class Geant4Interrupts;
0040 class Geant4ActionPhase;
0041
0042
0043 class DD4hep_End_Of_File : public std::exception {
0044 public:
0045 DD4hep_End_Of_File() : std::exception() {}
0046 virtual const char* what() const noexcept override { return "Reached end of input file"; }
0047 };
0048
0049
0050 class DD4hep_Stop_Processing : public std::exception {
0051 public:
0052 DD4hep_Stop_Processing() : std::exception() {}
0053 virtual const char* what() const noexcept override { return "Event loop STOP signalled. Processing stops"; }
0054 };
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 class Geant4Kernel : public Geant4ActionContainer {
0065 public:
0066 typedef std::map<unsigned long, Geant4Kernel*> Workers;
0067 typedef std::map<std::string, Geant4ActionPhase*> Phases;
0068 typedef std::map<std::string, Geant4Action*> GlobalActions;
0069 typedef std::map<std::string,int> ClientOutputLevels;
0070 typedef std::pair<void*, const std::type_info*> UserFramework;
0071 using UserCallbacks = std::vector<std::function<void()> >;
0072
0073 enum event_loop_status {
0074 EVENTLOOP_HALT = 0,
0075 EVENTLOOP_RUNNING = 1,
0076 };
0077
0078 protected:
0079
0080 G4RunManager* m_runManager { nullptr };
0081
0082 G4UIdirectory* m_control { nullptr };
0083
0084 G4TrackingManager* m_trackMgr { nullptr };
0085
0086 Detector* m_detDesc { nullptr };
0087
0088 PropertyManager m_properties { };
0089
0090 UserFramework m_userFramework { };
0091
0092
0093 Phases m_phases { };
0094
0095 Workers m_workers { };
0096
0097 GlobalActions m_globalActions { };
0098
0099 GlobalActions m_globalFilters { };
0100
0101 ClientOutputLevels m_clientLevels { };
0102
0103 std::string m_controlName { };
0104
0105 std::string m_uiName { };
0106
0107 std::string m_runManagerType;
0108
0109 std::string m_dfltSensitiveDetectorType;
0110
0111 std::map<std::string, std::string> m_sensitiveDetectorTypes;
0112
0113 long m_numEvent = 10;
0114
0115 int m_outputLevel = 0;
0116
0117
0118 int m_numThreads = 0;
0119
0120 int m_haveScoringMgr = false;
0121
0122 int m_processEvents = EVENTLOOP_RUNNING;
0123
0124
0125
0126 UserCallbacks m_actionConfigure { };
0127
0128 UserCallbacks m_actionInitialize { };
0129
0130 UserCallbacks m_actionTerminate { };
0131
0132
0133
0134 unsigned long m_id = 0, m_ident = 0;
0135
0136 G4VPhysicalVolume* m_world = 0;
0137
0138
0139 Geant4Kernel* m_master { nullptr };
0140
0141 Geant4Context* m_threadContext { nullptr };
0142
0143 Geant4Interrupts* m_interrupts { nullptr };
0144
0145 bool isMaster() const { return this == m_master; }
0146 bool isWorker() const { return this != m_master; }
0147
0148 #ifndef __CINT__
0149
0150 Geant4Kernel(Geant4Kernel* m, unsigned long identifier);
0151 #endif
0152
0153 public:
0154
0155 Geant4Kernel(Detector& description);
0156
0157
0158 Geant4Kernel& master() const { return *m_master; }
0159
0160
0161 bool isMultiThreaded() const { return m_numThreads > 0; }
0162
0163
0164 static unsigned long int thread_self();
0165
0166 public:
0167
0168
0169
0170
0171
0172
0173
0174 class PhaseSelector {
0175 public:
0176
0177 Geant4Kernel* m_kernel;
0178
0179 PhaseSelector(Geant4Kernel* kernel);
0180
0181 PhaseSelector(const PhaseSelector& c);
0182
0183 PhaseSelector& operator=(const PhaseSelector& c);
0184
0185 Geant4ActionPhase& operator[](const std::string& name) const;
0186 } phase;
0187
0188 enum State {
0189 SETTING_UP, INITIALIZED
0190 };
0191
0192 virtual ~Geant4Kernel();
0193
0194 #ifndef __CINT__
0195
0196 static Geant4Kernel& instance(Detector& description);
0197 #endif
0198
0199 const Phases& phases() const { return m_phases; }
0200
0201 Detector& detectorDescription() const { return *m_detDesc; }
0202
0203 G4TrackingManager* trackMgr() const { return m_trackMgr; }
0204
0205 void setTrackMgr(G4TrackingManager* mgr) { m_trackMgr = mgr; }
0206
0207 const std::string& directoryName() const { return m_controlName; }
0208
0209 unsigned long id() const { return m_ident; }
0210
0211 G4RunManager& runManager();
0212
0213 UserFramework& userFramework() { return m_userFramework; }
0214
0215 template <typename T> void setUserFramework(T* object) {
0216 m_userFramework = UserFramework(object,&typeid(T));
0217 }
0218
0219 const std::string defaultSensitiveDetectorType() const {
0220 return m_dfltSensitiveDetectorType;
0221 }
0222
0223 const std::map<std::string, std::string>& sensitiveDetectorTypes() const {
0224 return m_sensitiveDetectorTypes;
0225 }
0226
0227
0228
0229
0230
0231
0232 void defineSensitiveDetectorType(const std::string& type, const std::string& factory);
0233
0234 G4VPhysicalVolume* world() const;
0235
0236 void setWorld(G4VPhysicalVolume* volume);
0237
0238
0239
0240 PropertyManager& properties() { return m_properties; }
0241
0242 void printProperties() const;
0243
0244 template <typename T> Geant4Kernel& declareProperty(const std::string& nam, T& val);
0245
0246 template <typename T> Geant4Kernel& declareProperty(const char* nam, T& val);
0247
0248 bool hasProperty(const std::string& name) const;
0249
0250 Property& property(const std::string& name);
0251
0252
0253
0254 PrintLevel outputLevel() const { return (PrintLevel)m_outputLevel; }
0255
0256 PrintLevel setOutputLevel(PrintLevel new_level);
0257
0258 void setOutputLevel(const std::string object, PrintLevel new_level);
0259
0260 PrintLevel getOutputLevel(const std::string object) const;
0261
0262
0263 void register_configure(const std::function<void()>& callback);
0264
0265 void register_initialize(const std::function<void()>& callback);
0266
0267 void register_terminate(const std::function<void()>& callback);
0268
0269
0270 Geant4Interrupts& interruptHandler() const;
0271
0272 void triggerStop();
0273
0274 bool processEvents() const;
0275
0276 bool registerInterruptHandler(int sig_num);
0277
0278
0279
0280 void applyInterruptHandlers();
0281
0282
0283
0284
0285
0286
0287 Geant4Kernel& registerGlobalAction(Geant4Action* action);
0288
0289 Geant4Action* globalAction(const std::string& action_name, bool throw_if_not_present = true);
0290
0291
0292
0293
0294
0295 Geant4Kernel& registerGlobalFilter(Geant4Action* filter);
0296
0297 Geant4Action* globalFilter(const std::string& filter_name, bool throw_if_not_present = true);
0298
0299
0300 Geant4ActionPhase* getPhase(const std::string& name);
0301
0302
0303 virtual Geant4ActionPhase* addSimplePhase(const std::string& name, bool throw_on_exist);
0304
0305
0306 virtual Geant4ActionPhase* addPhase(const std::string& name, const std::type_info& arg1, const std::type_info& arg2,
0307 const std::type_info& arg3, bool throw_on_exist);
0308
0309 template <typename A0> Geant4ActionPhase* addPhase(const std::string& name, bool throw_on_exist = true) {
0310 return addPhase(name, typeid(A0), typeid(void), typeid(void), throw_on_exist);
0311 }
0312
0313 template <typename A0, typename A1>
0314 Geant4ActionPhase* addPhase(const std::string& name, bool throw_on_exist = true) {
0315 return addPhase(name, typeid(A0), typeid(A1), typeid(void), throw_on_exist);
0316 }
0317
0318 template <typename A0, typename A1, typename A2>
0319 Geant4ActionPhase* addPhase(const std::string& name, bool throw_on_exist = true) {
0320 return addPhase(name, typeid(A0), typeid(A1), typeid(A2), throw_on_exist);
0321 }
0322
0323 virtual bool removePhase(const std::string& name);
0324
0325 virtual void destroyPhases();
0326
0327
0328 virtual bool executePhase(const std::string& name, const void** args) const;
0329
0330
0331 virtual void loadGeometry(const std::string& compact_file);
0332
0333 virtual void loadXML(const char* fname);
0334
0335
0336
0337 virtual Geant4Kernel& createWorker();
0338
0339 Geant4Kernel& worker(unsigned long thread_identifier, bool create_if=false);
0340
0341 int numWorkers() const;
0342
0343
0344 virtual int configure();
0345
0346 virtual int initialize();
0347
0348 virtual int run();
0349
0350 virtual int runEvents(int num_events);
0351
0352 virtual int terminate() override;
0353 };
0354
0355 template <typename T> Geant4Kernel& Geant4Kernel::declareProperty(const std::string& nam, T& val) {
0356 m_properties.add(nam, val);
0357 return *this;
0358 }
0359
0360
0361 template <typename T> Geant4Kernel& Geant4Kernel::declareProperty(const char* nam, T& val) {
0362 m_properties.add(nam, val);
0363 return *this;
0364 }
0365
0366
0367 class Geant4Exec {
0368 public:
0369
0370 static int configure(Geant4Kernel& kernel);
0371
0372 static int initialize(Geant4Kernel& kernel);
0373
0374 static int run(Geant4Kernel& kernel);
0375
0376 static int terminate(Geant4Kernel& kernel);
0377 };
0378
0379 }
0380 }
0381 #endif