Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 08:20:18

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/Primitives.h>
0017 #include <DDG4/Geant4UIMessenger.h>
0018 
0019 // Geant4 include files
0020 #include <G4UIcmdWithoutParameter.hh>
0021 #include <G4UIcmdWithAString.hh>
0022 
0023 // C/C++ include files
0024 #include <algorithm>
0025 
0026 using namespace dd4hep::sim;
0027 
0028 namespace {
0029   struct InstallProperties {
0030     Geant4UIMessenger::Commands& cmds;
0031     const std::string& path;
0032     G4UImessenger* msg;
0033     InstallProperties(Geant4UIMessenger::Commands& c, const std::string& p, G4UImessenger* m)
0034       : cmds(c), path(p), msg(m) {
0035     }
0036     void operator()(const std::pair<std::string, dd4hep::Property>& o) {
0037       std::string n = path + o.first;
0038       G4UIcmdWithAString* cmd = new G4UIcmdWithAString(n.c_str(), msg);
0039       cmd->SetParameterName(o.first.c_str(), true);
0040       cmd->SetGuidance(("Property item of type " + o.second.type()).c_str());
0041       cmds[cmd] = o.first;
0042     }
0043   };
0044 }
0045 
0046 Geant4UIMessenger::Geant4UIMessenger(const std::string& name, const std::string& path)
0047   : G4UImessenger(), m_directory(0), m_properties(0), m_name(name), m_path(path) {
0048   m_directory = new G4UIdirectory(path.c_str());
0049   printout(INFO, "Geant4UI", "+++ %s> Install Geant4 control directory:%s", name.c_str(), path.c_str());
0050   m_directory->SetGuidance(("Control hierarchy for Geant4 action:" + name).c_str());
0051 }
0052 
0053 /// Default destructor
0054 Geant4UIMessenger::~Geant4UIMessenger() {
0055   detail::destroyFirst(m_propertyCmd);
0056   detail::destroyFirst(m_actionCmd);
0057 }
0058 
0059 /// Add a new callback structure
0060 void Geant4UIMessenger::addCall(const std::string& name, const std::string& description, const Callback& cb, size_t npar) {
0061   if ( 0 == npar )    {
0062     G4UIcommand* cmd = new G4UIcmdWithoutParameter((m_path + name).c_str(), this);
0063     cmd->SetGuidance(description.c_str());
0064     m_actionCmd[cmd] = cb;
0065   }
0066   else if ( 1 == npar )    {
0067     G4UIcmdWithAString* cmd = new G4UIcmdWithAString((m_path + name).c_str(), this);
0068     cmd->SetParameterName("p1", true);
0069     cmd->SetGuidance(description.c_str());
0070     m_actionCmd[cmd] = cb;
0071   }
0072   else    {
0073     except("Geant4UIMessenger","+++ Currently only callbacks with one argument are handled! [Contact developers if more are required]");
0074   }
0075 }
0076 
0077 /// Export all properties to the Geant4 UI
0078 void Geant4UIMessenger::exportProperties(PropertyManager& mgr) {
0079   InstallProperties installer(m_propertyCmd, m_path, this);
0080   m_properties = &mgr;
0081   addCall("show", "Show all properties of Geant4 component:" + m_name,
0082           Callback(m_properties).make(&PropertyManager::dump));
0083   m_properties->for_each(installer);
0084 }
0085 
0086 /// Pass current property value to Geant4 UI
0087 G4String Geant4UIMessenger::GetCurrentValue(G4UIcommand * c) {
0088   Commands::iterator i = m_propertyCmd.find(c);
0089   if (m_properties && i != m_propertyCmd.end()) {
0090     const std::string& n = (*i).second;
0091     return (*m_properties)[n].str();
0092   }
0093   printout(INFO, "Geant4UI",
0094            "+++ %s> Failed to access property value.", m_name.c_str());
0095   return "";
0096 }
0097 
0098 /// Accept ne property value from Geant4 UI
0099 void Geant4UIMessenger::SetNewValue(G4UIcommand *c, G4String v) {
0100   Commands::iterator i = m_propertyCmd.find(c);
0101   if (m_properties && i != m_propertyCmd.end()) {
0102     const std::string& n = (*i).second;
0103     try  {
0104       if (!v.empty()) {
0105         Property& p = (*m_properties)[n];
0106         p.str(v);
0107         printout(INFO, "Geant4UI",
0108                  "+++ %s> Setting property value %s = %s  native:%s.",
0109                  m_name.c_str(), n.c_str(), v.c_str(), p.str().c_str());
0110       }
0111       else {
0112         std::string value = (*m_properties)[n].str();
0113         printout(INFO, "Geant4UI", "+++ %s> Unchanged property value %s = %s.",
0114                  m_name.c_str(), n.c_str(), value.c_str());
0115       }
0116     }
0117     catch(const std::exception& e)   {
0118       printout(INFO, "Geant4UI", "+++ %s> Exception: Failed to change property %s = '%s'.",
0119                m_name.c_str(), n.c_str(), v.c_str());
0120       printout(INFO, "Geant4UI", "+++ %s> Exception: %s", m_name.c_str(), e.what());
0121     }
0122     catch(...)   {
0123       printout(INFO, "Geant4UI", "+++ %s> UNKNOWN Exception: Failed to change property %s = '%s'.",
0124                m_name.c_str(), n.c_str(), v.c_str());
0125     }
0126     return;
0127   }
0128   else  {
0129     Actions::iterator j = m_actionCmd.find(c);
0130     if (j != m_actionCmd.end()) {
0131       try  {
0132         const void* args[] = {v.c_str(), 0};
0133         (*j).second.execute(args);
0134       }
0135       catch(const std::exception& e)   {
0136         printout(INFO, "Geant4UI", "+++ %s> Exception: Failed to exec action '%s' [%s].",
0137                  m_name.c_str(), c->GetCommandName().c_str(), c->GetCommandPath().c_str());
0138         printout(INFO, "Geant4UI", "+++ %s> Exception: %s",e.what());
0139       }
0140       catch(...)   {
0141         printout(INFO, "Geant4UI", "+++ %s> UNKNOWN Exception: Failed to exec action '%s' [%s].",
0142                  m_name.c_str(), c->GetCommandName().c_str(), c->GetCommandPath().c_str());
0143       }
0144       return;
0145     }
0146   }
0147   printout(INFO, "Geant4UI", "+++ %s> Unknown command callback!", m_name.c_str());
0148 }