towards vis

This commit is contained in:
Jan Kieseler
2023-09-11 08:33:19 +02:00
parent b456e07018
commit 37d5b1eb3e
7 changed files with 212 additions and 109 deletions
+133
View File
@@ -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");
}
}