diff --git a/bind/bindings.cpp b/bind/bindings.cpp index 63d5907..a38bbb3 100644 --- a/bind/bindings.cpp +++ b/bind/bindings.cpp @@ -29,7 +29,8 @@ void makeG4System(M &m, std::string name) { py::class_(m, name.data()).def(py::init()) .def("init", &G4System::init, py::arg("cw")).def("run_visualize", &G4System::run_visualize, py::arg("partSpecies"), py::arg("minEnergy_GeV"), py::arg("maxEnergy_GeV")) - .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("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"), py::arg("filename")="_1234567890_Hits.root") .def("applyUICommand", &G4System::applyUICommand, py::arg("command")) .def("displayEvent", &G4System::displayEvent) // .def("printMaterial", &G4System::printMaterial, py::arg("name")) diff --git a/include/ActionInitialization.hh b/include/ActionInitialization.hh index 0ceda01..0812553 100644 --- a/include/ActionInitialization.hh +++ b/include/ActionInitialization.hh @@ -32,6 +32,7 @@ #include "G4VUserActionInitialization.hh" #include "PrimaryGeneratorAction.hh" +#include "RunAction.hh" namespace B4 { @@ -66,12 +67,21 @@ class ActionInitialization : public G4VUserActionInitialization fDetConstruction = det; } + void setFileName(G4String fileName){ + this->filename = fileName; + if(runact!=nullptr){ + runact -> setFileName(fileName); + } + } + private: B4::DetectorConstruction* fDetConstruction = nullptr; mutable B4::PrimaryGeneratorAction * gen = nullptr; + mutable B4::RunAction * runact = nullptr; G4double minEnergy=1000; G4double maxEnergy=1000; G4String partSpecies="e-"; + G4String filename=""; }; } diff --git a/include/G4System.hh b/include/G4System.hh index d9274fe..7e118b8 100644 --- a/include/G4System.hh +++ b/include/G4System.hh @@ -19,6 +19,7 @@ #include "Randomize.hh" class G4System{ + friend class GeometryDescriptor; public: G4System(bool Gui=false):gui(Gui){}; @@ -37,7 +38,8 @@ void init(GeometryDescriptor &cw); void run_visualize(const std::string& partSpecies, double minEnergy_GeV, double maxEnergy_GeV); //runs the whole gui if available void run_gui(); -void run_batch(int nEvents, const std::string& partSpecies, double minEnergy_GeV, double maxEnergy_GeV); +void run_batch(int nEvents, const std::string& partSpecies, double minEnergy_GeV, + double maxEnergy_GeV, std::string filename="_1234567890_Hits.root"); void applyUICommand(const std::string& command){ check(); diff --git a/include/GeometryDescriptor.hh b/include/GeometryDescriptor.hh index d86aba7..619ff42 100644 --- a/include/GeometryDescriptor.hh +++ b/include/GeometryDescriptor.hh @@ -7,7 +7,7 @@ #include //#include "G4VPhysicalVolume.hh" class G4VPhysicalVolume; - +class G4System; class Sensor{ public: @@ -90,7 +90,7 @@ class GeometryDescriptor{ public: GeometryDescriptor(double xy_width=50); - ~GeometryDescriptor() {}; + ~GeometryDescriptor(); void addLayer(double thickness_cm, std::string material, bool isActive=true, int nx=1, int ny=-1); @@ -134,10 +134,13 @@ public: } } - + void setG4System(G4System* g4system){ + this->g4system = g4system; + } private: double xywidth; std::vector layers; + G4System * g4system; }; #endif \ No newline at end of file diff --git a/include/RunAction.hh b/include/RunAction.hh index 7324984..6c89fc2 100644 --- a/include/RunAction.hh +++ b/include/RunAction.hh @@ -65,6 +65,10 @@ class RunAction : public G4UserRunAction void BeginOfRunAction(const G4Run*) override; void EndOfRunAction(const G4Run*) override; + void setFileName(G4String name){ + filename = name; + } + //now this is dumb but the way this is setup it has to be done like this //yeah mutable fine whatever @@ -78,6 +82,8 @@ class RunAction : public G4UserRunAction mutable std::vector hitLayer; mutable std::vector hitCopyNumber; + mutable G4String filename; + }; } diff --git a/src/ActionInitialization.cc b/src/ActionInitialization.cc index e025696..d862832 100644 --- a/src/ActionInitialization.cc +++ b/src/ActionInitialization.cc @@ -62,7 +62,8 @@ void ActionInitialization::Build() const gen->setMaxPartEnergy(maxEnergy); gen->setPartSpecies(partSpecies); SetUserAction(gen); - auto runact = new RunAction; + runact = new RunAction; + runact -> setFileName(filename); SetUserAction(runact); auto eventAction = new EventAction; eventAction->setRunAction(runact); diff --git a/src/G4System.cc b/src/G4System.cc index a24ed72..9bd61b6 100644 --- a/src/G4System.cc +++ b/src/G4System.cc @@ -19,6 +19,7 @@ void G4System::init(GeometryDescriptor &CW){ assigned_cw->unAssign(); } assigned_cw = &CW; + CW.setG4System(this);//link both ways char* argv[]={(char*)"dummy"}; @@ -156,7 +157,8 @@ void G4System::run_gui(){ } } -void G4System::run_batch(int nEvents,const std::string& partSpecies, double minEnergy, double maxEnergy){ +void G4System::run_batch(int nEvents,const std::string& partSpecies, double minEnergy, +double maxEnergy, std::string filename){ check(); @@ -165,6 +167,7 @@ void G4System::run_batch(int nEvents,const std::string& partSpecies, double minE UImanager->ApplyCommand("/run/initialize"); UImanager->ApplyCommand("/vis/disable"); actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpec); + actionInitialization->setFileName(filename); runManager->BeamOn(nEvents); } diff --git a/src/GeometryDescriptor.cc b/src/GeometryDescriptor.cc index 6f9bd82..ba63616 100644 --- a/src/GeometryDescriptor.cc +++ b/src/GeometryDescriptor.cc @@ -2,6 +2,7 @@ #include "GeometryDescriptor.hh" #include "G4VPhysicalVolume.hh" +#include "G4System.hh" Layer::Layer(){ thickness = 0; @@ -40,6 +41,13 @@ GeometryDescriptor::GeometryDescriptor(double xy_width){ xywidth = xy_width; } +GeometryDescriptor::~GeometryDescriptor() { + if(g4system != nullptr){ + if(this == g4system->assigned_cw){ //unassign + g4system->assigned_cw = nullptr;} + } +}; + void GeometryDescriptor::addLayer(double thickness, std::string material, bool isActive, int nx, int ny){ if(ny<0){ ny = nx; diff --git a/src/RunAction.cc b/src/RunAction.cc index 85b7360..160f566 100644 --- a/src/RunAction.cc +++ b/src/RunAction.cc @@ -96,12 +96,11 @@ void RunAction::BeginOfRunAction(const G4Run* /*run*/) // Open an output file // - G4String fileName = "_1234567890_Hits.root"; // Other supported output types: // G4String fileName = "B4.csv"; // G4String fileName = "B4.hdf5"; // G4String fileName = "B4.xml"; - analysisManager->OpenFile(fileName); + analysisManager->OpenFile(filename); G4cout << "Using " << analysisManager->GetType() << G4endl; }