Files
minicalosim/src/G4System.cc
T
2024-01-25 10:44:39 +01:00

194 lines
6.1 KiB
C++

#include "G4System.hh"
#include <G4Material.hh>
#include <G4NistManager.hh>
#include <G4String.hh>
#include <G4INCLGeant4Random.hh>
#include <G4INCLRandomSeedVector.hh>
#include <ctime>
void G4System::init(GeometryDescriptor &CW){
if(CW.isAssigned()){
throw std::runtime_error("GeometryDescriptor already assigned");
}
if(CW.isEmpty()){
throw std::runtime_error("GeometryDescriptor is empty");
}
if(assigned_cw != nullptr){
//reassign
assigned_cw->unAssign();
}
assigned_cw = &CW;
CW.setG4System(this);//link both ways
char* argv[]={(char*)"dummy"};
bool first_init=true;
int seed=std::time(NULL) ;
CLHEP::HepRandom::setTheSeed(seed); G4Random::setTheSeed(seed);
if(runManager == nullptr){
// Construct the default run manager
//
runManager =
G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
runManager->SetNumberOfThreads(1);//more doesn't make sense
runManager->SetVerboseLevel(0);
}
else{
// Abort the current run
//G4RunManager::GetRunManager()->AbortRun(true);
first_init = false;
//delete actionInitialization;
//delete detConstruction;
//delete actionInitialization;
}
G4String session;
ui = nullptr;
if ( gui ) {
ui = new G4UIExecutive((int)1, argv, session);
}
// Set mandatory initialization classes
//
if(first_init){
detConstruction = new B4::DetectorConstruction(&CW);
runManager->SetUserInitialization(detConstruction);
auto physicsList = new FTFP_BERT;
runManager->SetUserInitialization(physicsList);
actionInitialization = new B4a::ActionInitialization(detConstruction);
runManager->SetUserInitialization(actionInitialization);
}
else{
detConstruction->setGeometryDescriptor(&CW);
runManager->ReinitializeGeometry(true);
}
if(visManager == nullptr){
// 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();
runManager->Initialize();
// 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={
"/run/initialize",
"/vis/open DAWNFILE",
"/vis/drawVolume",
"/vis/viewer/set/viewpointThetaPhi -90 0",
"/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);
}
G4String partSpec = partSpecies;
G4cout << "running visualisation with particle species " << partSpec << G4endl;
actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpec);
runManager->BeamOn(1);
}
void G4System::printMaterial(const std::string& materialName)const{
// Get the material from the material name
return ; //ToDo
}
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, std::string filename){
check();
G4String partSpec = partSpecies;
G4cout << "running with particle species " << partSpec << G4endl;
UImanager->ApplyCommand("/run/initialize");
UImanager->ApplyCommand("/vis/disable");
actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpec);
actionInitialization->setFileName(filename);
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");
}
}