From 8d2302b5dcc97fa92863ba03c38f44bddb05383b Mon Sep 17 00:00:00 2001 From: Jan Kieseler Date: Thu, 21 Sep 2023 09:06:20 +0200 Subject: [PATCH] seems to work with different geometries --- include/DetectorConstruction.hh | 14 +++++++++++++- include/EventAction.hh | 4 ---- include/SteppingAction.hh | 10 ++-------- src/ActionInitialization.cc | 3 +-- src/DetectorConstruction.cc | 17 ++++++++++++++++- src/EventAction.cc | 4 +++- src/G4System.cc | 34 ++++++++++++++++++--------------- src/SteppingAction.cc | 10 ++++------ 8 files changed, 58 insertions(+), 38 deletions(-) diff --git a/include/DetectorConstruction.hh b/include/DetectorConstruction.hh index e32ae51..bba9cdb 100644 --- a/include/DetectorConstruction.hh +++ b/include/DetectorConstruction.hh @@ -40,6 +40,7 @@ class ConstructionWrapper; namespace B4 { + /// Detector construction class to define materials and geometry. /// The calorimeter is a box made of a given number of layers. A layer consists /// of an absorber plate and of a detection gap. The layer is replicated. @@ -59,13 +60,24 @@ namespace B4 class DetectorConstruction : public G4VUserDetectorConstruction { public: - DetectorConstruction( ConstructionWrapper* Cw): G4VUserDetectorConstruction(), cw(Cw){} + + static DetectorConstruction* _global_detector_construction; + static DetectorConstruction* getDetectorConstruction(){ + return _global_detector_construction; + } + + DetectorConstruction( ConstructionWrapper* Cw): G4VUserDetectorConstruction(), cw(Cw){ + _global_detector_construction=this; + } ~DetectorConstruction() override = default; public: G4VPhysicalVolume* Construct() override; void ConstructSDandField() override; + void setConstructionWrapper(ConstructionWrapper* Cw){ + this->cw = Cw; + } ConstructionWrapper* getConstructionWrapper(){ return cw; diff --git a/include/EventAction.hh b/include/EventAction.hh index d8e9160..caa531d 100644 --- a/include/EventAction.hh +++ b/include/EventAction.hh @@ -56,9 +56,6 @@ class EventAction : public G4UserEventAction void BeginOfEventAction(const G4Event* event) override; void EndOfEventAction(const G4Event* event) override; - void setConstructionWrapper(const ConstructionWrapper * Cw){ - this->cw = Cw; - } void setPrimaryGeneratorAction(const B4::PrimaryGeneratorAction * Gen){ this->gen = Gen; } @@ -67,7 +64,6 @@ class EventAction : public G4UserEventAction } private: - const ConstructionWrapper * cw=nullptr; const B4::PrimaryGeneratorAction * gen=nullptr; const B4::RunAction * run=nullptr; }; diff --git a/include/SteppingAction.hh b/include/SteppingAction.hh index 40485a6..d52869d 100644 --- a/include/SteppingAction.hh +++ b/include/SteppingAction.hh @@ -32,11 +32,8 @@ #include "G4UserSteppingAction.hh" #include "ConstructionWrapper.hh" +#include "DetectorConstruction.hh" -namespace B4 -{ - class DetectorConstruction; -} namespace B4a { @@ -52,15 +49,12 @@ class EventAction; class SteppingAction : public G4UserSteppingAction { public: - SteppingAction(const B4::DetectorConstruction* detConstruction, - EventAction* eventAction); + SteppingAction(EventAction* eventAction); ~SteppingAction() override = default; void UserSteppingAction(const G4Step* step) override; - private: - const B4::DetectorConstruction* fDetConstruction = nullptr; EventAction* fEventAction = nullptr; }; diff --git a/src/ActionInitialization.cc b/src/ActionInitialization.cc index d3bec8b..e025696 100644 --- a/src/ActionInitialization.cc +++ b/src/ActionInitialization.cc @@ -65,11 +65,10 @@ void ActionInitialization::Build() const auto runact = new RunAction; SetUserAction(runact); auto eventAction = new EventAction; - eventAction->setConstructionWrapper(fDetConstruction->getConstructionWrapper()); eventAction->setRunAction(runact); eventAction->setPrimaryGeneratorAction(gen); SetUserAction(eventAction); - auto steppingAction = new SteppingAction(fDetConstruction,eventAction); + auto steppingAction = new SteppingAction(eventAction); SetUserAction(steppingAction); } diff --git a/src/DetectorConstruction.cc b/src/DetectorConstruction.cc index 7c0ccd8..5b1259e 100644 --- a/src/DetectorConstruction.cc +++ b/src/DetectorConstruction.cc @@ -53,9 +53,15 @@ #include "G4PVParameterised.hh" #include "G4VPVParameterisation.hh" +#include "G4GeometryManager.hh" +#include "G4PhysicalVolumeStore.hh" +#include "G4LogicalVolumeStore.hh" +#include "G4SolidStore.hh" + + namespace B4 { - +DetectorConstruction* DetectorConstruction::_global_detector_construction=nullptr; //helper class LayerParametrisation: public G4VPVParameterisation{ @@ -129,6 +135,13 @@ nistManager->FindOrBuildMaterial("G4_AIR"); G4VPhysicalVolume* DetectorConstruction::DefineVolumes() { + + + G4GeometryManager::GetInstance()->OpenGeometry(); + G4PhysicalVolumeStore::GetInstance()->Clean(); + G4LogicalVolumeStore::GetInstance()->Clean(); + G4SolidStore::GetInstance()->Clean(); + // Geometry parameters auto & cwLayers = cw->getLayers(); G4int nofLayers = cwLayers.size(); @@ -201,6 +214,8 @@ G4VPhysicalVolume* DetectorConstruction::DefineVolumes() layer.name = "Layer_"+std::to_string(layerNumber); layerNumber++; + G4cout << "building layer " << layer.name << G4endl; + position += layer.thickness / 2 *cm; auto layerS = new G4Box(layer.name, // its name diff --git a/src/EventAction.cc b/src/EventAction.cc index d2d5d02..fc04b43 100644 --- a/src/EventAction.cc +++ b/src/EventAction.cc @@ -40,6 +40,7 @@ #include "Randomize.hh" #include +#include "DetectorConstruction.hh" namespace B4a { @@ -48,6 +49,7 @@ namespace B4a void EventAction::BeginOfEventAction(const G4Event* /*event*/) { + auto cw = B4::DetectorConstruction::getDetectorConstruction()->getConstructionWrapper(); // initialisation per event if(cw == nullptr){ G4cout << "ConstructionWrapper not found" << G4endl; @@ -82,7 +84,7 @@ void EventAction::EndOfEventAction(const G4Event* event) // get analysis manager auto analysisManager = G4AnalysisManager::Instance(); - + auto cw = B4::DetectorConstruction::getDetectorConstruction()->getConstructionWrapper(); //cw->printSensorEnergies();//DEBUG diff --git a/src/G4System.cc b/src/G4System.cc index 49c5220..b3fd4e6 100644 --- a/src/G4System.cc +++ b/src/G4System.cc @@ -11,16 +11,19 @@ void G4System::init(ConstructionWrapper &CW){ bool first_init=true; + if(runManager == nullptr){ // Construct the default run manager // runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default); runManager->SetNumberOfThreads(20);//more doesn't make sense + runManager->SetVerboseLevel(0); } else{ + // Abort the current run - G4RunManager::GetRunManager()->AbortRun(true); + //G4RunManager::GetRunManager()->AbortRun(true); first_init = false; //delete actionInitialization; //delete detConstruction; @@ -28,6 +31,7 @@ void G4System::init(ConstructionWrapper &CW){ } + G4String session; ui = nullptr; if ( gui ) { @@ -36,10 +40,11 @@ void G4System::init(ConstructionWrapper &CW){ // Set mandatory initialization classes // - - detConstruction = new B4::DetectorConstruction(&CW); if(first_init){ + + detConstruction = new B4::DetectorConstruction(&CW); runManager->SetUserInitialization(detConstruction); + auto physicsList = new FTFP_BERT; runManager->SetUserInitialization(physicsList); @@ -47,26 +52,25 @@ void G4System::init(ConstructionWrapper &CW){ runManager->SetUserInitialization(actionInitialization); } else{ - actionInitialization->setDetectorConstruction(detConstruction); - - G4RunManager::GetRunManager()->GeometryHasBeenModified(); - G4RunManager::GetRunManager()->ReinitializeGeometry(true); - //runManager->SetUserInitialization(detConstruction); + detConstruction->setConstructionWrapper(&CW); + runManager->ReinitializeGeometry(true); } if(visManager == nullptr){ - // Initialize visualization - // - visManager = new G4VisExecutive; - // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance. - // G4VisManager* visManager = new G4VisExecutive("Quiet"); - - visManager->Initialize(); + // Initialize visualization + // + visManager = new G4VisExecutive; + // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance. + // G4VisManager* visManager = new G4VisExecutive("Quiet"); + + visManager->Initialize(); } // Get the pointer to the User Interface manager UImanager = G4UImanager::GetUIpointer(); + runManager->Initialize(); + // Process macro or start UI session // } diff --git a/src/SteppingAction.cc b/src/SteppingAction.cc index eeabfff..e1f1b17 100644 --- a/src/SteppingAction.cc +++ b/src/SteppingAction.cc @@ -41,10 +41,8 @@ namespace B4a //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... -SteppingAction::SteppingAction(const DetectorConstruction* detConstruction, - EventAction* eventAction) - : fDetConstruction(detConstruction), - fEventAction(eventAction) +SteppingAction::SteppingAction(EventAction* eventAction) + : fEventAction(eventAction) {} //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... @@ -62,14 +60,14 @@ void SteppingAction::UserSteppingAction(const G4Step* step) //return; //find the layer through the constructionwrapper that corresponds to the volume - auto cw = fDetConstruction->getConstructionWrapper(); + auto cw = DetectorConstruction::getDetectorConstruction()->getConstructionWrapper(); if(cw == nullptr){ G4cout << "ConstructionWrapper not found" << G4endl; throw std::runtime_error("ConstructionWrapper not found"); } - auto sensor = fDetConstruction->getConstructionWrapper()->getSensorByVolume(volume); + auto sensor = DetectorConstruction::getDetectorConstruction()->getConstructionWrapper()->getSensorByVolume(volume); if(sensor == nullptr){ //can simply be not an active volume, so no need to throw an error //G4cout << "Sensor not found in" << volume->GetName() << G4endl; //DEBUG