DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4UIMessenger.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/Primitives.h>
17 #include <DDG4/Geant4UIMessenger.h>
18 
19 // Geant4 include files
20 #include <G4UIcmdWithoutParameter.hh>
21 #include <G4UIcmdWithAString.hh>
22 
23 // C/C++ include files
24 #include <algorithm>
25 
26 using namespace dd4hep::sim;
27 
28 namespace {
29  struct InstallProperties {
31  const std::string& path;
32  G4UImessenger* msg;
33  InstallProperties(Geant4UIMessenger::Commands& c, const std::string& p, G4UImessenger* m)
34  : cmds(c), path(p), msg(m) {
35  }
36  void operator()(const std::pair<std::string, dd4hep::Property>& o) {
37  std::string n = path + o.first;
38  G4UIcmdWithAString* cmd = new G4UIcmdWithAString(n.c_str(), msg);
39  cmd->SetParameterName(o.first.c_str(), true);
40  cmd->SetGuidance(("Property item of type " + o.second.type()).c_str());
41  cmds[cmd] = o.first;
42  }
43  };
44 }
45 
46 Geant4UIMessenger::Geant4UIMessenger(const std::string& name, const std::string& path)
47  : G4UImessenger(), m_directory(0), m_properties(0), m_name(name), m_path(path) {
48  m_directory = new G4UIdirectory(path.c_str());
49  printout(INFO, "Geant4UI", "+++ %s> Install Geant4 control directory:%s", name.c_str(), path.c_str());
50  m_directory->SetGuidance(("Control hierarchy for Geant4 action:" + name).c_str());
51 }
52 
55  detail::destroyFirst(m_propertyCmd);
56  detail::destroyFirst(m_actionCmd);
57 }
58 
60 void Geant4UIMessenger::addCall(const std::string& name, const std::string& description, const Callback& cb, size_t npar) {
61  if ( 0 == npar ) {
62  G4UIcommand* cmd = new G4UIcmdWithoutParameter((m_path + name).c_str(), this);
63  cmd->SetGuidance(description.c_str());
64  m_actionCmd[cmd] = cb;
65  }
66  else if ( 1 == npar ) {
67  G4UIcmdWithAString* cmd = new G4UIcmdWithAString((m_path + name).c_str(), this);
68  cmd->SetParameterName("p1", true);
69  cmd->SetGuidance(description.c_str());
70  m_actionCmd[cmd] = cb;
71  }
72  else {
73  except("Geant4UIMessenger","+++ Currently only callbacks with one argument are handled! [Contact developers if more are required]");
74  }
75 }
76 
79  InstallProperties installer(m_propertyCmd, m_path, this);
80  m_properties = &mgr;
81  addCall("show", "Show all properties of Geant4 component:" + m_name,
83  m_properties->for_each(installer);
84 }
85 
87 G4String Geant4UIMessenger::GetCurrentValue(G4UIcommand * c) {
88  Commands::iterator i = m_propertyCmd.find(c);
89  if (m_properties && i != m_propertyCmd.end()) {
90  const std::string& n = (*i).second;
91  return (*m_properties)[n].str();
92  }
93  printout(INFO, "Geant4UI",
94  "+++ %s> Failed to access property value.", m_name.c_str());
95  return "";
96 }
97 
99 void Geant4UIMessenger::SetNewValue(G4UIcommand *c, G4String v) {
100  Commands::iterator i = m_propertyCmd.find(c);
101  if (m_properties && i != m_propertyCmd.end()) {
102  const std::string& n = (*i).second;
103  try {
104  if (!v.empty()) {
105  Property& p = (*m_properties)[n];
106  p.str(v);
107  printout(INFO, "Geant4UI",
108  "+++ %s> Setting property value %s = %s native:%s.",
109  m_name.c_str(), n.c_str(), v.c_str(), p.str().c_str());
110  }
111  else {
112  std::string value = (*m_properties)[n].str();
113  printout(INFO, "Geant4UI", "+++ %s> Unchanged property value %s = %s.",
114  m_name.c_str(), n.c_str(), value.c_str());
115  }
116  }
117  catch(const std::exception& e) {
118  printout(INFO, "Geant4UI", "+++ %s> Exception: Failed to change property %s = '%s'.",
119  m_name.c_str(), n.c_str(), v.c_str());
120  printout(INFO, "Geant4UI", "+++ %s> Exception: %s", m_name.c_str(), e.what());
121  }
122  catch(...) {
123  printout(INFO, "Geant4UI", "+++ %s> UNKNOWN Exception: Failed to change property %s = '%s'.",
124  m_name.c_str(), n.c_str(), v.c_str());
125  }
126  return;
127  }
128  else {
129  Actions::iterator j = m_actionCmd.find(c);
130  if (j != m_actionCmd.end()) {
131  try {
132  const void* args[] = {v.c_str(), 0};
133  (*j).second.execute(args);
134  }
135  catch(const std::exception& e) {
136  printout(INFO, "Geant4UI", "+++ %s> Exception: Failed to exec action '%s' [%s].",
137  m_name.c_str(), c->GetCommandName().c_str(), c->GetCommandPath().c_str());
138  printout(INFO, "Geant4UI", "+++ %s> Exception: %s",e.what());
139  }
140  catch(...) {
141  printout(INFO, "Geant4UI", "+++ %s> UNKNOWN Exception: Failed to exec action '%s' [%s].",
142  m_name.c_str(), c->GetCommandName().c_str(), c->GetCommandPath().c_str());
143  }
144  return;
145  }
146  }
147  printout(INFO, "Geant4UI", "+++ %s> Unknown command callback!", m_name.c_str());
148 }
dd4hep::PropertyManager::for_each
void for_each(FUNCTOR &func)
Apply functor on properties.
Definition: ComponentProperties.h:217
dd4hep::Property
The property class to assign options to actions.
Definition: ComponentProperties.h:48
dd4hep::sim::Geant4UIMessenger::Geant4UIMessenger
Geant4UIMessenger(const std::string &name, const std::string &path)
Initializing constructor.
Definition: Geant4UIMessenger.cpp:46
dd4hep::sim::Geant4UIMessenger::exportProperties
void exportProperties(PropertyManager &mgr)
Export all properties to the Geant4 UI.
Definition: Geant4UIMessenger.cpp:78
v
View * v
Definition: MultiView.cpp:28
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::Property::str
std::string str() const
Conversion to string value.
Definition: ComponentProperties.cpp:48
dd4hep::sim::Geant4UIMessenger::~Geant4UIMessenger
virtual ~Geant4UIMessenger()
Default destructor.
Definition: Geant4UIMessenger.cpp:54
Geant4UIMessenger.h
dd4hep::sim::Geant4UIMessenger::m_path
std::string m_path
Path in the UI hierarchy of this component.
Definition: Geant4UIMessenger.h:47
G4UImessenger
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:15
dd4hep::sim::Geant4UIMessenger::m_actionCmd
Actions m_actionCmd
Action map.
Definition: Geant4UIMessenger.h:51
dd4hep::Callback
Definition of the generic callback structure for member functions.
Definition: Callback.h:38
dd4hep::PropertyManager::dump
void dump() const
Dump string values.
Definition: ComponentProperties.cpp:160
dd4hep::sim::Geant4UIMessenger::m_propertyCmd
Commands m_propertyCmd
Property update command map.
Definition: Geant4UIMessenger.h:49
dd4hep::sim::Geant4UIMessenger::Commands
std::map< G4UIcommand *, std::string > Commands
Definition: Geant4UIMessenger.h:37
dd4hep::sim::Geant4UIMessenger::m_properties
PropertyManager * m_properties
Reference to the property manager corresponding to the component.
Definition: Geant4UIMessenger.h:43
Primitives.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4UIMessenger::SetNewValue
void SetNewValue(G4UIcommand *c, G4String val) override
Accept ne property value from Geant4 UI.
Definition: Geant4UIMessenger.cpp:99
dd4hep::PropertyManager
Manager to ease the handling of groups of properties.
Definition: ComponentProperties.h:174
dd4hep::sim::Geant4UIMessenger::addCall
void addCall(const std::string &name, const std::string &description, const Callback &cb, size_t npar=0)
Add a new callback structure.
Definition: Geant4UIMessenger.cpp:60
dd4hep::sim::Geant4UIMessenger::m_name
std::string m_name
Component name.
Definition: Geant4UIMessenger.h:45
dd4hep::sim::Geant4UIMessenger::m_directory
G4UIdirectory * m_directory
The UI directory of this component.
Definition: Geant4UIMessenger.h:41
Printout.h
dd4hep::sim::Geant4UIMessenger::GetCurrentValue
G4String GetCurrentValue(G4UIcommand *c) override
Pass current property value to Geant4 UI.
Definition: Geant4UIMessenger.cpp:87