From 34a030560165154530d16978da3ece905f34e003 Mon Sep 17 00:00:00 2001 From: Jan Kieseler Date: Sat, 7 Oct 2023 11:10:57 +0200 Subject: [PATCH] fixed a few little things --- bind/G4Calo.py | 8 +++++--- bind/bindings.cpp | 7 ++++--- bind/example.py | 8 ++++---- include/ConstructionWrapper.hh | 11 +++++++++++ include/G4System.hh | 12 ++++++++++-- src/G4System.cc | 9 +++++---- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/bind/G4Calo.py b/bind/G4Calo.py index 3907630..c85d654 100644 --- a/bind/G4Calo.py +++ b/bind/G4Calo.py @@ -10,7 +10,7 @@ import uproot from IPython.display import Image, display -class G4System(_G4System): +class __G4System(_G4System): def run_visualize( self, particleSpec: str, minEnergy_GeV: float, maxEnergy_GeV: float = -1.0 ): @@ -47,7 +47,7 @@ class G4System(_G4System): z0=0 # loop over materials - for layer_i, layer in enumerate(self.cw.getLayers()): + for layer_i, layer in enumerate(self.getConstructionWrapper().getLayers()): #layer_i = len(self.cw.getLayers()) - layer_i -1 print(layer_i) @@ -241,4 +241,6 @@ col_dict = { "G4_W": 'grey', "G4_Cu": 'dimgray', "G4_BRASS": 'slategrey', -} \ No newline at end of file +} + +G4System = __G4System()#singleton instance \ No newline at end of file diff --git a/bind/bindings.cpp b/bind/bindings.cpp index 304eaa3..1e8784d 100644 --- a/bind/bindings.cpp +++ b/bind/bindings.cpp @@ -18,6 +18,7 @@ void makeConstructionWrapper(M & m, std::string name){ // bind overloaded getLayers function .def("getLayers", (std::vector & (ConstructionWrapper::*)()) &ConstructionWrapper::getLayers) .def("getLayers", (const std::vector & (ConstructionWrapper::*)() const) &ConstructionWrapper::getLayers) + .def("isAssigned", &ConstructionWrapper::isAssigned) .def("getNSensors", &ConstructionWrapper::getNSensors); } @@ -31,9 +32,9 @@ void makeG4System(M &m, std::string name) .def("run_gui", &G4System::run_gui).def("run_batch", &G4System::run_batch, py::arg("nEvents"), py::arg("partSpecies"), py::arg("minEnergy_GeV"), py::arg("maxEnergy_GeV")) .def("applyUICommand", &G4System::applyUICommand, py::arg("command")) .def("displayEvent", &G4System::displayEvent) - .def("printMaterial", &G4System::printMaterial, py::arg("name")) - .def("check", &G4System::check) - .def_readwrite("cw", &G4System::cw); + // .def("printMaterial", &G4System::printMaterial, py::arg("name")) + //.def("check", &G4System::check) + .def("getConstructionWrapper", &G4System::getConstructionWrapper); } diff --git a/bind/example.py b/bind/example.py index cf3e907..26d9176 100644 --- a/bind/example.py +++ b/bind/example.py @@ -6,9 +6,9 @@ cw.addLayer(10,"G4_Si",True) #10 cm of Silicon, active cw.addLayer(1,"G4_Pb",False) #1 cm of Lead, passive cw.addLayer(10,"G4_Si",True, 3) #10 cm of Silicon, active, 3x3 segmentation -G4s = G4System() -G4s.init(cw) -G4s.run_batch(100, "gamma", 1, 5) #100 events, photons, 1000 MeV to 5000 MeV +G4System = G4System +G4System.init(cw) +G4System.run_batch(100, "gamma", 1, 5) #100 events, photons, 1000 MeV to 5000 MeV #G4s.run_visualize("gamma", 1, 5) -G4s.printMaterial("G4_Pb") \ No newline at end of file +G4System.printMaterial("G4_Pb") \ No newline at end of file diff --git a/include/ConstructionWrapper.hh b/include/ConstructionWrapper.hh index 0ad2378..067d374 100644 --- a/include/ConstructionWrapper.hh +++ b/include/ConstructionWrapper.hh @@ -43,6 +43,11 @@ public: void setIsActive(bool isActive); void assignPhysicalVolume(G4VPhysicalVolume* physicalVolume); + void unAssign(){ + physicalVolume = nullptr; + sensors.clear(); + } + double thickness; double sens_xwidth; double sens_ywidth; @@ -99,6 +104,12 @@ public: } } + void unAssign(){ + for(auto& layer: layers){ + layer.unAssign(); + } + } + private: double xywidth; diff --git a/include/G4System.hh b/include/G4System.hh index 9f59b67..aa34389 100644 --- a/include/G4System.hh +++ b/include/G4System.hh @@ -28,6 +28,7 @@ G4System(bool Gui=false):gui(Gui){}; delete runManager; delete ui; } + }; @@ -43,15 +44,22 @@ void applyUICommand(const std::string& command){ UImanager->ApplyCommand(command); } -ConstructionWrapper cw; - void displayEvent()const{}; //just a placeholder, this will be implemented in python void printMaterial(const std::string& name)const; +ConstructionWrapper& getConstructionWrapper(){ + check(); + if ( assigned_cw == nullptr ){ + throw std::runtime_error("ConstructionWrapper not assigned"); + } + return *assigned_cw; +} + private: void check()const; +ConstructionWrapper * assigned_cw=nullptr; bool gui; diff --git a/src/G4System.cc b/src/G4System.cc index 2e82f0b..b86d2e4 100644 --- a/src/G4System.cc +++ b/src/G4System.cc @@ -9,6 +9,11 @@ void G4System::init(ConstructionWrapper &CW){ if(CW.isAssigned()){ throw std::runtime_error("ConstructionWrapper already assigned"); } + if(assigned_cw != nullptr){ + //reassign + assigned_cw->unAssign(); + } + assigned_cw = &CW; char* argv[]={(char*)"dummy"}; @@ -33,10 +38,6 @@ void G4System::init(ConstructionWrapper &CW){ //delete actionInitialization; } - // save layers of CW to G4System class - G4System::cw = CW; - - G4String session; ui = nullptr; if ( gui ) {