diff --git a/bind/G4Calo.py b/bind/G4Calo.py index f2f2ed6..90780ef 100644 --- a/bind/G4Calo.py +++ b/bind/G4Calo.py @@ -1,4 +1,4 @@ -from minicalo import ConstructionWrapper +from minicalo import GeometryDescriptor from minicalo import G4System as _G4System import plotly.graph_objects as go @@ -10,15 +10,8 @@ import uproot from IPython.display import Image, display -class G4System(_G4System): - def run_visualize( - self, particleSpec: str, minEnergy_GeV: float, maxEnergy_GeV: float = -1.0 - ): - if maxEnergy_GeV < 0: - maxEnergy_GeV = minEnergy_GeV - # anpassen: run 1 event and get stuff from construction wrapper - _G4System.run_visualize(self, particleSpec, minEnergy_GeV, maxEnergy_GeV) - self.displayEvent() +class __G4System(_G4System): + def run_batch( self, @@ -36,7 +29,7 @@ class G4System(_G4System): df = ttree["Hits;1"].arrays(library="pd") return df - def displayEvent(self, gd): + def displayEvent(self): # for loop over all layers to_plot = [] material_dict = {} @@ -44,7 +37,7 @@ class G4System(_G4System): # sum up total deposited energy total_dep_energy = 0 - for layer in gd.getLayers(): + for layer in self.getGeometryDescriptor().getLayers(): for sensor in layer.sensors: total_dep_energy += sensor.getEnergy() @@ -53,7 +46,7 @@ class G4System(_G4System): total_dep_energy = 10**-8 # to avoid division by zero # loop over materials - for layer in gd.getLayers(): + for layer in self.getGeometryDescriptor().getLayers(): layer_width = layer.nx * layer.sens_xwidth @@ -245,4 +238,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 587449b..63d5907 100644 --- a/bind/bindings.cpp +++ b/bind/bindings.cpp @@ -3,22 +3,23 @@ #include #include -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include "G4System.hh" namespace py = pybind11; template -void makeConstructionWrapper(M & m, std::string name){ +void makeGeometryDescriptor(M & m, std::string name){ - py::class_(m, name.data()).def(py::init()) - .def("addLayer", &ConstructionWrapper::addLayer, py::arg("thickness"), py::arg("material"), py::arg("isActive")=true, py::arg("nx")=1, py::arg("ny")=-1) - .def("getXYWidth", &ConstructionWrapper::getXYWidth) + py::class_(m, name.data()).def(py::init()) + .def("addLayer", &GeometryDescriptor::addLayer, py::arg("thickness"), py::arg("material"), py::arg("isActive")=true, py::arg("nx")=1, py::arg("ny")=-1) + .def("getXYWidth", &GeometryDescriptor::getXYWidth) // bind overloaded getLayers function - .def("getLayers", (std::vector & (ConstructionWrapper::*)()) &ConstructionWrapper::getLayers) - .def("getLayers", (const std::vector & (ConstructionWrapper::*)() const) &ConstructionWrapper::getLayers) - .def("getNSensors", &ConstructionWrapper::getNSensors); + .def("getLayers", (std::vector & (GeometryDescriptor::*)()) &GeometryDescriptor::getLayers) + .def("getLayers", (const std::vector & (GeometryDescriptor::*)() const) &GeometryDescriptor::getLayers) + .def("isAssigned", &GeometryDescriptor::isAssigned) + .def("getNSensors", &GeometryDescriptor::getNSensors); } @@ -31,8 +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("printMaterial", &G4System::printMaterial, py::arg("name")) + //.def("check", &G4System::check) + .def("getGeometryDescriptor", &G4System::getGeometryDescriptor); } // create bindings for Layer class @@ -69,7 +71,7 @@ void makeSensor(M &m, std::string name){ PYBIND11_MODULE(minicalo, m) { m.doc() = "pybind11 plugin"; // optional module docstring - makeConstructionWrapper(m, "ConstructionWrapper"); + makeGeometryDescriptor(m, "GeometryDescriptor"); makeG4System(m, "G4System"); makeSensor(m, "Sensor"); makeLayer(m, "Layer"); diff --git a/bind/example.ipynb b/bind/example.ipynb index ebc324e..3b0c452 100644 --- a/bind/example.ipynb +++ b/bind/example.ipynb @@ -2,984 +2,13 @@ "cells": [ { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.plotly.v1+json": { - "config": { - "plotlyServerURL": "https://plot.ly" - }, - "data": [ - { - "hovertemplate": "x=%{x}
y=%{y}
z=%{z}", - "legendgroup": "", - "marker": { - "color": "#636efa", - "symbol": "circle" - }, - "mode": "markers", - "name": "", - "scene": "scene", - "showlegend": false, - "type": "scatter3d", - "x": [ - 1, - 2, - 3, - 4, - 5 - ], - "y": [ - 5, - 4, - 3, - 2, - 1 - ], - "z": [ - 10, - 20, - 15, - 25, - 30 - ] - }, - { - "line": { - "color": "red", - "width": 2 - }, - "opacity": 0.2, - "type": "scatter3d", - "x": [ - -1, - 1, - 1, - -1, - -1, - 1, - 1, - -1, - -1 - ], - "y": [ - -1, - -1, - 1, - 1, - -1, - -1, - 1, - 1, - -1 - ], - "z": [ - 0, - 0, - 0, - 0, - 30, - 30, - 30, - 30, - 0 - ] - } - ], - "layout": { - "legend": { - "tracegroupgap": 0 - }, - "margin": { - "t": 60 - }, - "scene": { - "domain": { - "x": [ - 0, - 1 - ], - "y": [ - 0, - 1 - ] - }, - "dragmode": "orbit", - "xaxis": { - "title": { - "text": "X Label" - } - }, - "yaxis": { - "title": { - "text": "Y Label" - } - }, - "zaxis": { - "title": { - "text": "Z Label" - } - } - }, - "template": { - "data": { - "bar": [ - { - "error_x": { - "color": "#2a3f5f" - }, - "error_y": { - "color": "#2a3f5f" - }, - "marker": { - "line": { - "color": "#E5ECF6", - "width": 0.5 - }, - "pattern": { - "fillmode": "overlay", - "size": 10, - "solidity": 0.2 - } - }, - "type": "bar" - } - ], - "barpolar": [ - { - "marker": { - "line": { - "color": "#E5ECF6", - "width": 0.5 - }, - "pattern": { - "fillmode": "overlay", - "size": 10, - "solidity": 0.2 - } - }, - "type": "barpolar" - } - ], - "carpet": [ - { - "aaxis": { - "endlinecolor": "#2a3f5f", - "gridcolor": "white", - "linecolor": "white", - "minorgridcolor": "white", - "startlinecolor": "#2a3f5f" - }, - "baxis": { - "endlinecolor": "#2a3f5f", - "gridcolor": "white", - "linecolor": "white", - "minorgridcolor": "white", - "startlinecolor": "#2a3f5f" - }, - "type": "carpet" - } - ], - "choropleth": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "type": "choropleth" - } - ], - "contour": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "colorscale": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "type": "contour" - } - ], - "contourcarpet": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "type": "contourcarpet" - } - ], - "heatmap": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "colorscale": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "type": "heatmap" - } - ], - "heatmapgl": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "colorscale": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "type": "heatmapgl" - } - ], - "histogram": [ - { - "marker": { - "pattern": { - "fillmode": "overlay", - "size": 10, - "solidity": 0.2 - } - }, - "type": "histogram" - } - ], - "histogram2d": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "colorscale": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "type": "histogram2d" - } - ], - "histogram2dcontour": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "colorscale": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "type": "histogram2dcontour" - } - ], - "mesh3d": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "type": "mesh3d" - } - ], - "parcoords": [ - { - "line": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "parcoords" - } - ], - "pie": [ - { - "automargin": true, - "type": "pie" - } - ], - "scatter": [ - { - "fillpattern": { - "fillmode": "overlay", - "size": 10, - "solidity": 0.2 - }, - "type": "scatter" - } - ], - "scatter3d": [ - { - "line": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scatter3d" - } - ], - "scattercarpet": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scattercarpet" - } - ], - "scattergeo": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scattergeo" - } - ], - "scattergl": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scattergl" - } - ], - "scattermapbox": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scattermapbox" - } - ], - "scatterpolar": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scatterpolar" - } - ], - "scatterpolargl": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scatterpolargl" - } - ], - "scatterternary": [ - { - "marker": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "type": "scatterternary" - } - ], - "surface": [ - { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - }, - "colorscale": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "type": "surface" - } - ], - "table": [ - { - "cells": { - "fill": { - "color": "#EBF0F8" - }, - "line": { - "color": "white" - } - }, - "header": { - "fill": { - "color": "#C8D4E3" - }, - "line": { - "color": "white" - } - }, - "type": "table" - } - ] - }, - "layout": { - "annotationdefaults": { - "arrowcolor": "#2a3f5f", - "arrowhead": 0, - "arrowwidth": 1 - }, - "autotypenumbers": "strict", - "coloraxis": { - "colorbar": { - "outlinewidth": 0, - "ticks": "" - } - }, - "colorscale": { - "diverging": [ - [ - 0, - "#8e0152" - ], - [ - 0.1, - "#c51b7d" - ], - [ - 0.2, - "#de77ae" - ], - [ - 0.3, - "#f1b6da" - ], - [ - 0.4, - "#fde0ef" - ], - [ - 0.5, - "#f7f7f7" - ], - [ - 0.6, - "#e6f5d0" - ], - [ - 0.7, - "#b8e186" - ], - [ - 0.8, - "#7fbc41" - ], - [ - 0.9, - "#4d9221" - ], - [ - 1, - "#276419" - ] - ], - "sequential": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ], - "sequentialminus": [ - [ - 0, - "#0d0887" - ], - [ - 0.1111111111111111, - "#46039f" - ], - [ - 0.2222222222222222, - "#7201a8" - ], - [ - 0.3333333333333333, - "#9c179e" - ], - [ - 0.4444444444444444, - "#bd3786" - ], - [ - 0.5555555555555556, - "#d8576b" - ], - [ - 0.6666666666666666, - "#ed7953" - ], - [ - 0.7777777777777778, - "#fb9f3a" - ], - [ - 0.8888888888888888, - "#fdca26" - ], - [ - 1, - "#f0f921" - ] - ] - }, - "colorway": [ - "#636efa", - "#EF553B", - "#00cc96", - "#ab63fa", - "#FFA15A", - "#19d3f3", - "#FF6692", - "#B6E880", - "#FF97FF", - "#FECB52" - ], - "font": { - "color": "#2a3f5f" - }, - "geo": { - "bgcolor": "white", - "lakecolor": "white", - "landcolor": "#E5ECF6", - "showlakes": true, - "showland": true, - "subunitcolor": "white" - }, - "hoverlabel": { - "align": "left" - }, - "hovermode": "closest", - "mapbox": { - "style": "light" - }, - "paper_bgcolor": "white", - "plot_bgcolor": "#E5ECF6", - "polar": { - "angularaxis": { - "gridcolor": "white", - "linecolor": "white", - "ticks": "" - }, - "bgcolor": "#E5ECF6", - "radialaxis": { - "gridcolor": "white", - "linecolor": "white", - "ticks": "" - } - }, - "scene": { - "xaxis": { - "backgroundcolor": "#E5ECF6", - "gridcolor": "white", - "gridwidth": 2, - "linecolor": "white", - "showbackground": true, - "ticks": "", - "zerolinecolor": "white" - }, - "yaxis": { - "backgroundcolor": "#E5ECF6", - "gridcolor": "white", - "gridwidth": 2, - "linecolor": "white", - "showbackground": true, - "ticks": "", - "zerolinecolor": "white" - }, - "zaxis": { - "backgroundcolor": "#E5ECF6", - "gridcolor": "white", - "gridwidth": 2, - "linecolor": "white", - "showbackground": true, - "ticks": "", - "zerolinecolor": "white" - } - }, - "shapedefaults": { - "line": { - "color": "#2a3f5f" - } - }, - "ternary": { - "aaxis": { - "gridcolor": "white", - "linecolor": "white", - "ticks": "" - }, - "baxis": { - "gridcolor": "white", - "linecolor": "white", - "ticks": "" - }, - "bgcolor": "#E5ECF6", - "caxis": { - "gridcolor": "white", - "linecolor": "white", - "ticks": "" - } - }, - "title": { - "x": 0.05 - }, - "xaxis": { - "automargin": true, - "gridcolor": "white", - "linecolor": "white", - "ticks": "", - "title": { - "standoff": 15 - }, - "zerolinecolor": "white", - "zerolinewidth": 2 - }, - "yaxis": { - "automargin": true, - "gridcolor": "white", - "linecolor": "white", - "ticks": "", - "title": { - "standoff": 15 - }, - "zerolinecolor": "white", - "zerolinewidth": 2 - } - } - }, - "title": { - "text": "Interactive 3D Scatter Plot" - } - } - }, - "text/html": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ - "from G4Calo import ConstructionWrapper, G4System\n", + "from G4Calo import GeometryDescriptor, G4System\n", "\n", - "cw = ConstructionWrapper() #the width of the calorimeter is 50 cm times 50 cm (also steerable, but I'd leave it)\n", + "cw = GeometryDescriptor() #the width of the calorimeter is 50 cm times 50 cm (also steerable, but I'd leave it)\n", "\n", "cw.addLayer(3,\"G4_Si\",True, 7)\n", "cw.addLayer(4,\"G4_Pb\",False) \n", @@ -988,7 +17,7 @@ "cw.addLayer(3,\"G4_Si\",True, 7)\n", "cw.addLayer(4,\"G4_Pb\",False)\n", "\n", - "G4s = G4System()\n", + "G4s = G4System\n", "G4s.init(cw)" ] }, @@ -998,7 +27,7 @@ "metadata": {}, "outputs": [], "source": [ - "G4s.displayEvent(\"pi-\", 100000)" + "df = G4s.run_batch(10, 'gamma', 1)" ] }, { @@ -1007,7 +36,7 @@ "metadata": {}, "outputs": [], "source": [ - "df = G4s.run_batch(10, 'gamma', 1)" + "G4s.displayEvent()" ] }, { diff --git a/bind/example.py b/bind/example.py index cf3e907..aa373b1 100644 --- a/bind/example.py +++ b/bind/example.py @@ -1,14 +1,14 @@ -from G4Calo import ConstructionWrapper, G4System +from G4Calo import GeometryDescriptor, G4System -cw = ConstructionWrapper()#the width of the calorimeter is 50 cm times 50 cm (also steerable, but I'd leave it) +cw = GeometryDescriptor()#the width of the calorimeter is 50 cm times 50 cm (also steerable, but I'd leave it) 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/exampleB4a.cc b/exampleB4a.cc index 0cec81f..c9f4cf8 100644 --- a/exampleB4a.cc +++ b/exampleB4a.cc @@ -39,7 +39,7 @@ #include "FTFP_BERT.hh" #include "Randomize.hh" -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include "G4System.hh" @@ -121,7 +121,7 @@ int main2(int argc,char** argv) } #endif - ConstructionWrapper * cw = new ConstructionWrapper(); + GeometryDescriptor * cw = new GeometryDescriptor(); cw->addLayer(1, "G4_Pb", false); cw->addLayer(10, "G4_Si", true, 9); cw->addLayer(1, "G4_Pb", false); @@ -185,11 +185,11 @@ int main2(int argc,char** argv) //later //class Runner { //public: -// void initialize(ConstructionWrapper CW, bool gui=false); +// void initialize(GeometryDescriptor CW, bool gui=false); // void run(int nEvents, std::string partSpecies, double minEnergy, double maxEnergy); //} -void run(ConstructionWrapper CW, int nEvents, std::string partSpecies, double minEnergy, double maxEnergy, bool gui=false){ +void run(GeometryDescriptor CW, int nEvents, std::string partSpecies, double minEnergy, double maxEnergy, bool gui=false){ char* argv[]={(char*)"dummy"}; @@ -199,7 +199,7 @@ void run(ConstructionWrapper CW, int nEvents, std::string partSpecies, double mi G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default); runManager->SetNumberOfThreads(1); - ConstructionWrapper * cw = &CW; + GeometryDescriptor * cw = &CW; G4String session; G4UIExecutive* ui = nullptr; if ( gui ) { @@ -261,7 +261,7 @@ void run(ConstructionWrapper CW, int nEvents, std::string partSpecies, double mi int main(){ - ConstructionWrapper cw; + GeometryDescriptor cw; //to be moved cw.addLayer(1, "G4_Pb", false); cw.addLayer(10, "G4_Si", true, 9); diff --git a/include/DetectorConstruction.hh b/include/DetectorConstruction.hh index 082aba8..640c044 100644 --- a/include/DetectorConstruction.hh +++ b/include/DetectorConstruction.hh @@ -35,7 +35,7 @@ class G4VPhysicalVolume; class G4GlobalMagFieldMessenger; -class ConstructionWrapper; +class GeometryDescriptor; namespace B4 { @@ -67,7 +67,7 @@ class DetectorConstruction : public G4VUserDetectorConstruction return _global_detector_construction; } - DetectorConstruction( ConstructionWrapper* Cw): G4VUserDetectorConstruction(), cw(Cw){ + DetectorConstruction( GeometryDescriptor* Cw): G4VUserDetectorConstruction(), cw(Cw){ _global_detector_construction=this; } ~DetectorConstruction() override = default; @@ -76,14 +76,14 @@ class DetectorConstruction : public G4VUserDetectorConstruction G4VPhysicalVolume* Construct() override; void ConstructSDandField() override; - void setConstructionWrapper(ConstructionWrapper* Cw){ + void setGeometryDescriptor(GeometryDescriptor* Cw){ this->cw = Cw; } - ConstructionWrapper* getConstructionWrapper(){ + GeometryDescriptor* getGeometryDescriptor(){ return cw; } - const ConstructionWrapper* getConstructionWrapper()const{ + const GeometryDescriptor* getGeometryDescriptor()const{ return cw; } @@ -100,7 +100,7 @@ class DetectorConstruction : public G4VUserDetectorConstruction G4bool fCheckOverlaps = true; // option to activate checking of volumes overlaps - ConstructionWrapper* cw=nullptr; + GeometryDescriptor* cw=nullptr; }; diff --git a/include/EventAction.hh b/include/EventAction.hh index caa531d..43a4e14 100644 --- a/include/EventAction.hh +++ b/include/EventAction.hh @@ -34,7 +34,7 @@ #include "RunAction.hh" #include "PrimaryGeneratorAction.hh" #include "globals.hh" -class ConstructionWrapper; +class GeometryDescriptor; namespace B4a { diff --git a/include/G4System.hh b/include/G4System.hh index 3e85711..d9274fe 100644 --- a/include/G4System.hh +++ b/include/G4System.hh @@ -2,7 +2,7 @@ #ifndef G4SYSTEM_HH #define G4SYSTEM_HH -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include #include "G4RunManagerFactory.hh" #include "DetectorConstruction.hh" @@ -28,10 +28,11 @@ G4System(bool Gui=false):gui(Gui){}; delete runManager; delete ui; } + }; -void init(ConstructionWrapper &cw); +void init(GeometryDescriptor &cw); //will use dawn for visualization, also wrap more in python void run_visualize(const std::string& partSpecies, double minEnergy_GeV, double maxEnergy_GeV); //runs the whole gui if available @@ -47,9 +48,18 @@ void displayEvent()const{}; //just a placeholder, this will be implemented in py void printMaterial(const std::string& name)const; +GeometryDescriptor& getGeometryDescriptor(){ + check(); + if ( assigned_cw == nullptr ){ + throw std::runtime_error("GeometryDescriptor not assigned"); + } + return *assigned_cw; +} + private: void check()const; +GeometryDescriptor * assigned_cw=nullptr; bool gui; @@ -67,7 +77,7 @@ G4UIExecutive * ui=nullptr; /* -void run(ConstructionWrapper CW, int nEvents, std::string partSpecies, double minEnergy, double maxEnergy, bool gui=false){ +void run(GeometryDescriptor CW, int nEvents, std::string partSpecies, double minEnergy, double maxEnergy, bool gui=false){ char* argv[]={(char*)"dummy"}; @@ -77,7 +87,7 @@ void run(ConstructionWrapper CW, int nEvents, std::string partSpecies, double mi G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default); runManager->SetNumberOfThreads(1); - ConstructionWrapper * cw = &CW; + GeometryDescriptor * cw = &CW; G4String session; G4UIExecutive* ui = nullptr; if ( gui ) { diff --git a/include/ConstructionWrapper.hh b/include/GeometryDescriptor.hh similarity index 87% rename from include/ConstructionWrapper.hh rename to include/GeometryDescriptor.hh index 7a4ed49..d86aba7 100644 --- a/include/ConstructionWrapper.hh +++ b/include/GeometryDescriptor.hh @@ -1,6 +1,6 @@ -#ifndef CONSTRUCTIONWRAPPER_HH -#define CONSTRUCTIONWRAPPER_HH +#ifndef GeometryDescriptor_HH +#define GeometryDescriptor_HH #include "G4ThreeVector.hh" #include @@ -66,6 +66,11 @@ public: void setIsActive(bool isActive); void assignPhysicalVolume(G4VPhysicalVolume* physicalVolume); + void unAssign(){ + physicalVolume = nullptr; + sensors.clear(); + } + double thickness; double sens_xwidth; double sens_ywidth; @@ -81,11 +86,11 @@ public: }; -class ConstructionWrapper{ +class GeometryDescriptor{ public: - ConstructionWrapper(double xy_width=50); - ~ConstructionWrapper() {}; + GeometryDescriptor(double xy_width=50); + ~GeometryDescriptor() {}; void addLayer(double thickness_cm, std::string material, bool isActive=true, int nx=1, int ny=-1); @@ -123,6 +128,12 @@ public: } } + void unAssign(){ + for(auto& layer: layers){ + layer.unAssign(); + } + } + private: double xywidth; diff --git a/include/SteppingAction.hh b/include/SteppingAction.hh index d52869d..85cdd0f 100644 --- a/include/SteppingAction.hh +++ b/include/SteppingAction.hh @@ -31,7 +31,7 @@ #define B4aSteppingAction_h 1 #include "G4UserSteppingAction.hh" -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include "DetectorConstruction.hh" diff --git a/src/DetectorConstruction.cc b/src/DetectorConstruction.cc index 5b1259e..f9bd893 100644 --- a/src/DetectorConstruction.cc +++ b/src/DetectorConstruction.cc @@ -28,7 +28,7 @@ /// \brief Implementation of the B4::DetectorConstruction class #include "DetectorConstruction.hh" -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include "G4Material.hh" #include "G4NistManager.hh" diff --git a/src/EventAction.cc b/src/EventAction.cc index fc04b43..ce5ff3d 100644 --- a/src/EventAction.cc +++ b/src/EventAction.cc @@ -27,7 +27,7 @@ /// \file B4/B4a/src/EventAction.cc /// \brief Implementation of the B4a::EventAction class -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include "EventAction.hh" #include "RunAction.hh" @@ -49,11 +49,11 @@ namespace B4a void EventAction::BeginOfEventAction(const G4Event* /*event*/) { - auto cw = B4::DetectorConstruction::getDetectorConstruction()->getConstructionWrapper(); + auto cw = B4::DetectorConstruction::getDetectorConstruction()->getGeometryDescriptor(); // initialisation per event if(cw == nullptr){ - G4cout << "ConstructionWrapper not found" << G4endl; - throw std::runtime_error("ConstructionWrapper not found"); + G4cout << "GeometryDescriptor not found" << G4endl; + throw std::runtime_error("GeometryDescriptor not found"); } if(gen == nullptr){ G4cout << "PrimaryGeneratorAction not found" << G4endl; @@ -84,7 +84,7 @@ void EventAction::EndOfEventAction(const G4Event* event) // get analysis manager auto analysisManager = G4AnalysisManager::Instance(); - auto cw = B4::DetectorConstruction::getDetectorConstruction()->getConstructionWrapper(); + auto cw = B4::DetectorConstruction::getDetectorConstruction()->getGeometryDescriptor(); //cw->printSensorEnergies();//DEBUG diff --git a/src/G4System.cc b/src/G4System.cc index bb43651..c71eb59 100644 --- a/src/G4System.cc +++ b/src/G4System.cc @@ -4,11 +4,16 @@ #include #include -void G4System::init(ConstructionWrapper &CW){ +void G4System::init(GeometryDescriptor &CW){ if(CW.isAssigned()){ - throw std::runtime_error("ConstructionWrapper already assigned"); + throw std::runtime_error("GeometryDescriptor already assigned"); } + if(assigned_cw != nullptr){ + //reassign + assigned_cw->unAssign(); + } + assigned_cw = &CW; char* argv[]={(char*)"dummy"}; @@ -33,7 +38,6 @@ void G4System::init(ConstructionWrapper &CW){ //delete actionInitialization; } - G4String session; ui = nullptr; if ( gui ) { @@ -54,7 +58,7 @@ void G4System::init(ConstructionWrapper &CW){ runManager->SetUserInitialization(actionInitialization); } else{ - detConstruction->setConstructionWrapper(&CW); + detConstruction->setGeometryDescriptor(&CW); runManager->ReinitializeGeometry(true); } diff --git a/src/ConstructionWrapper.cc b/src/GeometryDescriptor.cc similarity index 73% rename from src/ConstructionWrapper.cc rename to src/GeometryDescriptor.cc index 7f2b2a1..6f9bd82 100644 --- a/src/ConstructionWrapper.cc +++ b/src/GeometryDescriptor.cc @@ -1,6 +1,6 @@ -#include "ConstructionWrapper.hh" +#include "GeometryDescriptor.hh" #include "G4VPhysicalVolume.hh" Layer::Layer(){ @@ -36,11 +36,11 @@ void Layer::assignPhysicalVolume(G4VPhysicalVolume* physicalVolume){ this->physicalVolume = physicalVolume; } -ConstructionWrapper::ConstructionWrapper(double xy_width){ +GeometryDescriptor::GeometryDescriptor(double xy_width){ xywidth = xy_width; } -void ConstructionWrapper::addLayer(double thickness, std::string material, bool isActive, int nx, int ny){ +void GeometryDescriptor::addLayer(double thickness, std::string material, bool isActive, int nx, int ny){ if(ny<0){ ny = nx; } @@ -60,20 +60,20 @@ void ConstructionWrapper::addLayer(double thickness, std::string material, bool layers.push_back(layer); } -const std::vector & ConstructionWrapper::getLayers() const{ +const std::vector & GeometryDescriptor::getLayers() const{ return layers; } -std::vector & ConstructionWrapper::getLayers(){ +std::vector & GeometryDescriptor::getLayers(){ return layers; } -double ConstructionWrapper::getXYWidth() const{ +double GeometryDescriptor::getXYWidth() const{ return xywidth; } -void ConstructionWrapper::resetSensorEnergies()const { +void GeometryDescriptor::resetSensorEnergies()const { for(const auto & layer : layers){ for(const auto & sensor : layer.sensors){ sensor.energy = 0; @@ -81,7 +81,7 @@ void ConstructionWrapper::resetSensorEnergies()const { } } -Layer* ConstructionWrapper::getLayerByVolume(G4VPhysicalVolume* volume){ +Layer* GeometryDescriptor::getLayerByVolume(G4VPhysicalVolume* volume){ for(auto & layer : layers){ if(layer.physicalVolume == volume){ return &layer; @@ -90,7 +90,7 @@ Layer* ConstructionWrapper::getLayerByVolume(G4VPhysicalVolume* volume){ return nullptr; } -const Layer* ConstructionWrapper::getLayerByVolume(G4VPhysicalVolume* volume)const{ +const Layer* GeometryDescriptor::getLayerByVolume(G4VPhysicalVolume* volume)const{ for(auto & layer : layers){ if(layer.physicalVolume == volume){ return &layer; @@ -99,7 +99,7 @@ const Layer* ConstructionWrapper::getLayerByVolume(G4VPhysicalVolume* volume)con return nullptr; } -Sensor* ConstructionWrapper::getSensorByVolume(G4VPhysicalVolume* volume){ +Sensor* GeometryDescriptor::getSensorByVolume(G4VPhysicalVolume* volume){ int copyNo = volume->GetCopyNo(); auto layer = getLayerByVolume(volume); if(layer == nullptr){ @@ -111,7 +111,7 @@ Sensor* ConstructionWrapper::getSensorByVolume(G4VPhysicalVolume* volume){ return &layer->sensors[copyNo]; } -const Sensor* ConstructionWrapper::getSensorByVolume(G4VPhysicalVolume* volume)const{ +const Sensor* GeometryDescriptor::getSensorByVolume(G4VPhysicalVolume* volume)const{ int copyNo = volume->GetCopyNo(); auto layer = getLayerByVolume(volume); if(layer == nullptr){ @@ -124,7 +124,7 @@ const Sensor* ConstructionWrapper::getSensorByVolume(G4VPhysicalVolume* volume)c } -void ConstructionWrapper::printSensorEnergies()const{ +void GeometryDescriptor::printSensorEnergies()const{ for(const auto& layer: layers){ for(const auto& sensor: layer.sensors){ G4cout << sensor.energy << " "; diff --git a/src/SteppingAction.cc b/src/SteppingAction.cc index e1f1b17..ec80e5d 100644 --- a/src/SteppingAction.cc +++ b/src/SteppingAction.cc @@ -59,15 +59,15 @@ void SteppingAction::UserSteppingAction(const G4Step* step) //return; - //find the layer through the constructionwrapper that corresponds to the volume - auto cw = DetectorConstruction::getDetectorConstruction()->getConstructionWrapper(); + //find the layer through the GeometryDescriptor that corresponds to the volume + auto cw = DetectorConstruction::getDetectorConstruction()->getGeometryDescriptor(); if(cw == nullptr){ - G4cout << "ConstructionWrapper not found" << G4endl; - throw std::runtime_error("ConstructionWrapper not found"); + G4cout << "GeometryDescriptor not found" << G4endl; + throw std::runtime_error("GeometryDescriptor not found"); } - auto sensor = DetectorConstruction::getDetectorConstruction()->getConstructionWrapper()->getSensorByVolume(volume); + auto sensor = DetectorConstruction::getDetectorConstruction()->getGeometryDescriptor()->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