towards vis
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
|
||||||
|
from minicalo import ConstructionWrapper
|
||||||
|
from minicalo import G4System as _G4System
|
||||||
|
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
from IPython.display import Image, display
|
||||||
|
|
||||||
|
|
||||||
|
class G4System(_G4System):
|
||||||
|
|
||||||
|
def run_visualize(self,particleSpec : str, minEnergy: float, maxEnergy:float = -1.):
|
||||||
|
if maxEnergy < 0:
|
||||||
|
maxEnergy = minEnergy
|
||||||
|
super(G4System).run_visualize(particleSpec, minEnergy, maxEnergy)
|
||||||
|
self.displayEvent()
|
||||||
|
|
||||||
|
def displayEvent(self):
|
||||||
|
f_to_conv = max(filter(lambda x: x.endswith(".prim"), os.listdir()),
|
||||||
|
key=os.path.getctime)
|
||||||
|
# Convert the .prim file to an eps graphic.
|
||||||
|
subprocess.run(["dawn",
|
||||||
|
"-d",
|
||||||
|
f_to_conv],
|
||||||
|
stderr=subprocess.DEVNULL)
|
||||||
|
# Convert the eps graphic to png graphic.
|
||||||
|
subprocess.run(["gs",
|
||||||
|
"-DEPSCrop", "-dSAFER", "-sDEVICE=png256",
|
||||||
|
"-r600",
|
||||||
|
"-o",
|
||||||
|
"event_raw.png",
|
||||||
|
f_to_conv.replace(".prim", ".eps")],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
subprocess.run(["convert",
|
||||||
|
"event_raw.png",
|
||||||
|
"-trim",
|
||||||
|
"event.png"
|
||||||
|
])
|
||||||
|
display(Image("event.png", width=500))
|
||||||
|
|
||||||
|
|
||||||
+9
-8
@@ -4,7 +4,7 @@
|
|||||||
#include <pybind11/operators.h>
|
#include <pybind11/operators.h>
|
||||||
|
|
||||||
#include "ConstructionWrapper.hh"
|
#include "ConstructionWrapper.hh"
|
||||||
#include "SystemBuilder.hh"
|
#include "G4System.hh"
|
||||||
|
|
||||||
namespace py = pybind11;
|
namespace py = pybind11;
|
||||||
|
|
||||||
@@ -17,16 +17,17 @@ void makeConstructionWrapper(M & m, std::string name){
|
|||||||
|
|
||||||
}
|
}
|
||||||
template<class M>
|
template<class M>
|
||||||
void makeSystemBuilder(M& m, std::string name){
|
void makeG4System(M& m, std::string name){
|
||||||
py::class_<SystemBuilder>(m, name.data()).def(py::init())
|
py::class_<G4System>(m, name.data()).def(py::init())
|
||||||
.def("init", &SystemBuilder::init, py::arg("cw"), py::arg("gui")=false)
|
.def("init", &G4System::init, py::arg("cw"), py::arg("gui")=false)
|
||||||
.def("run_gui", &SystemBuilder::run_gui)
|
.def("run_gui", &G4System::run_gui)
|
||||||
.def("run_batch", &SystemBuilder::run_batch, py::arg("nEvents"), py::arg("partSpecies"), py::arg("minEnergy"), py::arg("maxEnergy"))
|
.def("run_batch", &G4System::run_batch, py::arg("nEvents"), py::arg("partSpecies"), py::arg("minEnergy"), py::arg("maxEnergy"))
|
||||||
.def("visualize", &SystemBuilder::visualize);
|
.def("run_visualize", &G4System::run_visualize, py::arg("partSpecies"), py::arg("minEnergy"), py::arg("maxEnergy"))
|
||||||
|
.def("applyUICommand", &G4System::applyUICommand, py::arg("command"));
|
||||||
}
|
}
|
||||||
|
|
||||||
PYBIND11_MODULE(minicalo, m) {
|
PYBIND11_MODULE(minicalo, m) {
|
||||||
m.doc() = "pybind11 plugin"; // optional module docstring
|
m.doc() = "pybind11 plugin"; // optional module docstring
|
||||||
makeConstructionWrapper(m, "ConstructionWrapper");
|
makeConstructionWrapper(m, "ConstructionWrapper");
|
||||||
makeSystemBuilder(m, "SystemBuilder");
|
makeG4System(m, "G4System");
|
||||||
}
|
}
|
||||||
+7
-7
@@ -1,11 +1,11 @@
|
|||||||
|
|
||||||
from minicalo import ConstructionWrapper, SystemBuilder
|
from G4Calo import ConstructionWrapper, G4System
|
||||||
|
|
||||||
cw = ConstructionWrapper()
|
cw = ConstructionWrapper()#the width of the calorimeter is 50 cm times 50 cm (also steerable, but I'd leave it)
|
||||||
cw.addLayer(10,"G4_Si",True)
|
cw.addLayer(10,"G4_Si",True) #10 cm of Silicon, active
|
||||||
cw.addLayer(1,"G4_Pb",False)
|
cw.addLayer(1,"G4_Pb",False) #1 cm of Lead, passive
|
||||||
cw.addLayer(10,"G4_Si",False)
|
cw.addLayer(10,"G4_Si",True, 3) #10 cm of Silicon, active, 3x3 segmentation
|
||||||
|
|
||||||
sb = SystemBuilder()
|
sb = G4System()
|
||||||
sb.init(cw)
|
sb.init(cw)
|
||||||
sb.run_batch(100, "gamma", 1000, 5000)
|
sb.run_batch(100, "gamma", 1000, 5000) #100 events, photons, 1000 MeV to 5000 MeV
|
||||||
|
|||||||
+2
-2
@@ -41,7 +41,7 @@
|
|||||||
|
|
||||||
#include "ConstructionWrapper.hh"
|
#include "ConstructionWrapper.hh"
|
||||||
|
|
||||||
#include "SystemBuilder.hh"
|
#include "G4System.hh"
|
||||||
|
|
||||||
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
|
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
|
||||||
|
|
||||||
@@ -270,7 +270,7 @@ int main(){
|
|||||||
cw.addLayer(4, "G4_Pb", false);
|
cw.addLayer(4, "G4_Pb", false);
|
||||||
cw.addLayer(3, "G4_Si", true, 23);
|
cw.addLayer(3, "G4_Si", true, 23);
|
||||||
|
|
||||||
SystemBuilder builder;
|
G4System builder;
|
||||||
builder.init(cw, true);
|
builder.init(cw, true);
|
||||||
//builder.run_gui();
|
//builder.run_gui();
|
||||||
builder.run_batch(10000, ((std::string)"e-").data(), 1, 100);
|
builder.run_batch(10000, ((std::string)"e-").data(), 1, 100);
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
|
||||||
|
#ifndef G4SYSTEM_HH
|
||||||
|
#define G4SYSTEM_HH
|
||||||
|
|
||||||
#include "ConstructionWrapper.hh"
|
#include "ConstructionWrapper.hh"
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "G4RunManagerFactory.hh"
|
#include "G4RunManagerFactory.hh"
|
||||||
@@ -15,11 +18,11 @@
|
|||||||
#include "FTFP_BERT.hh"
|
#include "FTFP_BERT.hh"
|
||||||
#include "Randomize.hh"
|
#include "Randomize.hh"
|
||||||
|
|
||||||
class SystemBuilder{
|
class G4System{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SystemBuilder(){};
|
G4System(){};
|
||||||
~SystemBuilder(){
|
~G4System(){
|
||||||
if(visManager != nullptr){
|
if(visManager != nullptr){
|
||||||
delete visManager;
|
delete visManager;
|
||||||
delete runManager;
|
delete runManager;
|
||||||
@@ -29,32 +32,23 @@ SystemBuilder(){};
|
|||||||
|
|
||||||
|
|
||||||
void init(ConstructionWrapper &cw, bool gui=false);
|
void init(ConstructionWrapper &cw, bool gui=false);
|
||||||
void visualize()const{} //TBI
|
//will use dawn for visualization, also wrap more in python
|
||||||
|
void run_visualize(const std::string& partSpecies, double minEnergy, double maxEnergy);
|
||||||
|
//runs the whole gui if available
|
||||||
void run_gui();
|
void run_gui();
|
||||||
void run_batch(int nEvents, const std::string& partSpecies, double minEnergy, double maxEnergy);
|
void run_batch(int nEvents, const std::string& partSpecies, double minEnergy, double maxEnergy);
|
||||||
|
|
||||||
|
void applyUICommand(const std::string& command){
|
||||||
|
check();
|
||||||
|
UImanager->ApplyCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
void displayEvent()const{}; //just a placeholder, this will be implemented in python
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
inline void check(){
|
void check()const;
|
||||||
if(runManager == nullptr){
|
|
||||||
throw std::runtime_error("runManager not found");
|
|
||||||
}
|
|
||||||
if(detConstruction == nullptr){
|
|
||||||
throw std::runtime_error("DetectorConstruction not found");
|
|
||||||
}
|
|
||||||
if(actionInitialization == nullptr){
|
|
||||||
throw std::runtime_error("ActionInitialization not found");
|
|
||||||
}
|
|
||||||
if(visManager == nullptr){
|
|
||||||
throw std::runtime_error("VisManager not found");
|
|
||||||
}
|
|
||||||
if(UImanager == nullptr){
|
|
||||||
throw std::runtime_error("UImanager not found");
|
|
||||||
}
|
|
||||||
if(ui == nullptr){
|
|
||||||
throw std::runtime_error("UI not found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
G4RunManager * runManager=nullptr;
|
G4RunManager * runManager=nullptr;
|
||||||
@@ -66,6 +60,8 @@ G4UIExecutive * ui=nullptr;
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|
||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
|
||||||
|
#include "G4System.hh"
|
||||||
|
|
||||||
|
void G4System::init(ConstructionWrapper &CW, bool gui){
|
||||||
|
|
||||||
|
char* argv[]={(char*)"dummy"};
|
||||||
|
|
||||||
|
// Construct the default run manager
|
||||||
|
//
|
||||||
|
runManager =
|
||||||
|
G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
|
||||||
|
runManager->SetNumberOfThreads(1);
|
||||||
|
|
||||||
|
G4String session;
|
||||||
|
ui = nullptr;
|
||||||
|
if ( gui ) {
|
||||||
|
ui = new G4UIExecutive((int)1, argv, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set mandatory initialization classes
|
||||||
|
//
|
||||||
|
detConstruction = new B4::DetectorConstruction(&CW);
|
||||||
|
|
||||||
|
runManager->SetUserInitialization(detConstruction);
|
||||||
|
|
||||||
|
auto physicsList = new FTFP_BERT;
|
||||||
|
runManager->SetUserInitialization(physicsList);
|
||||||
|
|
||||||
|
actionInitialization = new B4a::ActionInitialization(detConstruction);
|
||||||
|
runManager->SetUserInitialization(actionInitialization);
|
||||||
|
|
||||||
|
//actionInitialization->setGeneratorProperties(100, 10000, "e-");
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// Process macro or start UI session
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void G4System::run_visualize(const std::string& partSpecies, double minEnergy, double maxEnergy){
|
||||||
|
//most of it from https://gitlab.etp.kit.edu/Lehre/Teilchenphysik/-/blob/master/ws_2223/Uebung/geant4_simulation/vis.py
|
||||||
|
// hard coded macro here
|
||||||
|
|
||||||
|
check();
|
||||||
|
|
||||||
|
std::vector<G4String> commands={
|
||||||
|
"/vis/open DAWNFILE",
|
||||||
|
"/vis/drawVolume",
|
||||||
|
"/vis/modeling/trajectories/create/drawByParticleID",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set gamma green",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set e- blue",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set e+ magenta",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set mu- blue",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set mu+ magenta",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set tau- blue",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set tau+ magenta",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set nu_e red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set anti_nu_e red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set nu_mu red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set anti_nu_mu red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set nu_tau red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set anti_nu_tau red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set neutron grey",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set pi- cyan",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set pi+ red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set kaon+ red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set kaon- cyan",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set proton red",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set anti_proton cyan",
|
||||||
|
"/vis/modeling/trajectories/drawByParticleID-0/set alpha red",
|
||||||
|
"/vis/modeling/trajectories/select drawByParticleID-0",
|
||||||
|
"/vis/scene/add/trajectories smooth",
|
||||||
|
"/vis/scene/endOfRunAction refresh",
|
||||||
|
"/vis/viewer/flush"
|
||||||
|
};
|
||||||
|
|
||||||
|
for(const auto& c : commands){
|
||||||
|
UImanager->ApplyCommand(c);
|
||||||
|
}
|
||||||
|
run_batch(1, partSpecies, minEnergy, maxEnergy);
|
||||||
|
}
|
||||||
|
|
||||||
|
void G4System::run_gui(){
|
||||||
|
if(ui == nullptr){
|
||||||
|
throw std::runtime_error("GUI not initialized");
|
||||||
|
}
|
||||||
|
if ( true ) {
|
||||||
|
// interactive mode : define UI session
|
||||||
|
UImanager->ApplyCommand("/control/execute init_vis.mac");
|
||||||
|
if (ui->IsGUI()) {
|
||||||
|
UImanager->ApplyCommand("/control/execute gui.mac");
|
||||||
|
}
|
||||||
|
ui->SessionStart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void G4System::run_batch(int nEvents,const std::string& partSpecies, double minEnergy, double maxEnergy){
|
||||||
|
G4String partSpec = partSpecies;
|
||||||
|
G4cout << "running with particle species " << partSpec << G4endl;
|
||||||
|
UImanager->ApplyCommand("/run/initialize");
|
||||||
|
actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpec);
|
||||||
|
runManager->BeamOn(nEvents);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void G4System::check()const{
|
||||||
|
if(runManager == nullptr){
|
||||||
|
throw std::runtime_error("runManager not found");
|
||||||
|
}
|
||||||
|
if(detConstruction == nullptr){
|
||||||
|
throw std::runtime_error("DetectorConstruction not found");
|
||||||
|
}
|
||||||
|
if(actionInitialization == nullptr){
|
||||||
|
throw std::runtime_error("ActionInitialization not found");
|
||||||
|
}
|
||||||
|
if(visManager == nullptr){
|
||||||
|
throw std::runtime_error("VisManager not found");
|
||||||
|
}
|
||||||
|
if(UImanager == nullptr){
|
||||||
|
throw std::runtime_error("UImanager not found");
|
||||||
|
}
|
||||||
|
if(ui == nullptr){
|
||||||
|
throw std::runtime_error("UI not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
|
|
||||||
#include "SystemBuilder.hh"
|
|
||||||
|
|
||||||
void SystemBuilder::init(ConstructionWrapper &CW, bool gui){
|
|
||||||
|
|
||||||
char* argv[]={(char*)"dummy"};
|
|
||||||
|
|
||||||
// Construct the default run manager
|
|
||||||
//
|
|
||||||
runManager =
|
|
||||||
G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
|
|
||||||
runManager->SetNumberOfThreads(1);
|
|
||||||
|
|
||||||
G4String session;
|
|
||||||
ui = nullptr;
|
|
||||||
if ( gui ) {
|
|
||||||
ui = new G4UIExecutive((int)1, argv, session);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set mandatory initialization classes
|
|
||||||
//
|
|
||||||
detConstruction = new B4::DetectorConstruction(&CW);
|
|
||||||
|
|
||||||
runManager->SetUserInitialization(detConstruction);
|
|
||||||
|
|
||||||
auto physicsList = new FTFP_BERT;
|
|
||||||
runManager->SetUserInitialization(physicsList);
|
|
||||||
|
|
||||||
actionInitialization = new B4a::ActionInitialization(detConstruction);
|
|
||||||
runManager->SetUserInitialization(actionInitialization);
|
|
||||||
|
|
||||||
//actionInitialization->setGeneratorProperties(100, 10000, "e-");
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
// Process macro or start UI session
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void SystemBuilder::run_gui(){
|
|
||||||
if(ui == nullptr){
|
|
||||||
throw std::runtime_error("GUI not initialized");
|
|
||||||
}
|
|
||||||
if ( true ) {
|
|
||||||
// interactive mode : define UI session
|
|
||||||
UImanager->ApplyCommand("/control/execute init_vis.mac");
|
|
||||||
if (ui->IsGUI()) {
|
|
||||||
UImanager->ApplyCommand("/control/execute gui.mac");
|
|
||||||
}
|
|
||||||
ui->SessionStart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SystemBuilder::run_batch(int nEvents,const std::string& partSpecies, double minEnergy, double maxEnergy){
|
|
||||||
G4String partSpec = partSpecies;
|
|
||||||
G4cout << "running with particle species " << partSpec << G4endl;
|
|
||||||
UImanager->ApplyCommand("/run/initialize");
|
|
||||||
actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpec);
|
|
||||||
runManager->BeamOn(nEvents);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user