Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:23

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/InstanceCount.h>
0017 #include <DDG4/Geant4Context.h>
0018 #include <DDG4/Geant4Action.h>
0019 #include <DDG4/Geant4Kernel.h>
0020 #include <DDG4/Geant4UIMessenger.h>
0021 
0022 // Geant4 include files
0023 #include <G4UIdirectory.hh>
0024 
0025 // C/C++ include files
0026 #include <algorithm>
0027 
0028 using namespace dd4hep::sim;
0029 
0030 TypeName TypeName::split(const std::string& type_name, const std::string& delim) {
0031   size_t idx = type_name.find(delim);
0032   std::string typ = type_name, nam = type_name;
0033   if (idx != std::string::npos) {
0034     typ = type_name.substr(0, idx);
0035     nam = type_name.substr(idx + 1);
0036   }
0037   return TypeName(typ, nam);
0038 }
0039 
0040 TypeName TypeName::split(const std::string& type_name) {
0041   return split(type_name,"/");
0042 }
0043 #if 0
0044 void Geant4Action::ContextUpdate::operator()(Geant4Action* action) const  {
0045   action->m_context = context;
0046   if ( 0 == action->m_context )   {
0047 
0048     cout << "ERROR" << endl;
0049 
0050   }
0051 }
0052 #endif
0053 /// Standard constructor
0054 Geant4Action::Geant4Action(Geant4Context* ctxt, const std::string& nam)
0055   : m_context(ctxt), m_outputLevel(INFO), m_name(nam)
0056 {
0057   InstanceCount::increment(this);
0058   m_outputLevel = ctxt ? ctxt->kernel().getOutputLevel(nam) : (printLevel()-1);
0059   declareProperty("Name", m_name);
0060   declareProperty("name", m_name);
0061   declareProperty("OutputLevel", m_outputLevel);
0062   declareProperty("Control", m_needsControl);
0063 }
0064 
0065 /// Default destructor
0066 Geant4Action::~Geant4Action() {
0067   InstanceCount::decrement(this);
0068 }
0069 
0070 /// Implicit destruction
0071 long Geant4Action::addRef() {
0072   return ++m_refCount;
0073 }
0074 
0075 /// Decrease reference count. Implicit destruction
0076 long Geant4Action::release() {
0077   long count = --m_refCount;
0078   if (m_refCount <= 0) {
0079     printM1("Geant4Action: Deleting object %s of type %s Pointer:%p",
0080             m_name.c_str(),typeName(typeid(*this)).c_str(),(void*)this);
0081     delete this;
0082   }
0083   return count;
0084 }
0085 
0086 /// Set the output level; returns previous value
0087 dd4hep::PrintLevel Geant4Action::setOutputLevel(PrintLevel new_level)  {
0088   int old = m_outputLevel;
0089   m_outputLevel = new_level;
0090   return (PrintLevel)old;
0091 }
0092 
0093 /// Check property for existence
0094 bool Geant4Action::hasProperty(const std::string& nam) const    {
0095   return m_properties.exists(nam);
0096 }
0097 
0098 /// Access single property
0099 dd4hep::Property& Geant4Action::property(const std::string& nam)   {
0100   return properties()[nam];
0101 }
0102 
0103 /// Install all control messenger if wanted
0104 void Geant4Action::installMessengers() {
0105   //m_needsControl = true;
0106   if (m_needsControl && !m_control) {
0107     std::string path = context()->kernel().directoryName();
0108     path += name() + "/";
0109     m_control = new Geant4UIMessenger(name(), path);
0110     installPropertyMessenger();
0111     installCommandMessenger();
0112   }
0113 }
0114 
0115 /// Install property control messenger if wanted
0116 void Geant4Action::installPropertyMessenger() {
0117   m_control->exportProperties(m_properties);
0118 }
0119 
0120 /// Install command control messenger if wanted
0121 void Geant4Action::installCommandMessenger()   {
0122 }
0123 
0124 /// Access to the UI messenger
0125 Geant4UIMessenger* Geant4Action::control() const   {
0126   if ( m_control )   {
0127     return m_control;
0128   }
0129   except("No control was installed for this action item.");
0130   return 0;
0131 }
0132 
0133 /// Enable and install UI messenger
0134 void Geant4Action::enableUI()   {
0135   m_needsControl = true;
0136   installMessengers();
0137 }
0138 
0139 /// Set or update client for the use in a new thread fiber
0140 void Geant4Action::configureFiber(Geant4Context* /* thread_context */)   {
0141 }
0142 
0143 /// Support for messages with variable output level using output level
0144 void Geant4Action::print(const char* fmt, ...) const   {
0145   int level = std::max(int(outputLevel()),(int)VERBOSE);
0146   if ( level >= printLevel() )  {
0147     va_list args;
0148     va_start(args, fmt);
0149     dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
0150     va_end(args);
0151   }
0152 }
0153 
0154 /// Support for messages with variable output level using output level-1
0155 void Geant4Action::printM1(const char* fmt, ...) const   {
0156   int level = std::max(outputLevel()-1,(int)VERBOSE);
0157   if ( level >= printLevel() )  {
0158     va_list args;
0159     va_start(args, fmt);
0160     dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
0161     va_end(args);
0162   }
0163 }
0164 
0165 /// Support for messages with variable output level using output level-2
0166 void Geant4Action::printM2(const char* fmt, ...) const   {
0167   int level = std::max(outputLevel()-2,(int)VERBOSE);
0168   if ( level >= printLevel() )  {
0169     va_list args;
0170     va_start(args, fmt);
0171     dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
0172     va_end(args);
0173   }
0174 }
0175 
0176 /// Support for messages with variable output level using output level-1
0177 void Geant4Action::printP1(const char* fmt, ...) const   {
0178   int level = std::min(outputLevel()+1,(int)FATAL);
0179   if ( level >= printLevel() )  {
0180     va_list args;
0181     va_start(args, fmt);
0182     dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
0183     va_end(args);
0184   }
0185 }
0186 
0187 /// Support for messages with variable output level using output level-2
0188 void Geant4Action::printP2(const char* fmt, ...) const   {
0189   int level = std::min(outputLevel()+2,(int)FATAL);
0190   if ( level >= printLevel() )  {
0191     va_list args;
0192     va_start(args, fmt);
0193     dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
0194     va_end(args);
0195   }
0196 }
0197 
0198 /// Support of always printed messages.
0199 void Geant4Action::always(const char* fmt, ...) const {
0200   va_list args;
0201   va_start(args, fmt);
0202   dd4hep::printout(dd4hep::ALWAYS, m_name, fmt, args);
0203   va_end(args);
0204 }
0205 
0206 /// Support of debug messages.
0207 void Geant4Action::debug(const char* fmt, ...) const {
0208   va_list args;
0209   va_start(args, fmt);
0210   dd4hep::printout(dd4hep::DEBUG, m_name, fmt, args);
0211   va_end(args);
0212 }
0213 
0214 /// Support of info messages.
0215 void Geant4Action::info(const char* fmt, ...) const {
0216   va_list args;
0217   va_start(args, fmt);
0218   dd4hep::printout(dd4hep::INFO, m_name, fmt, args);
0219   va_end(args);
0220 }
0221 
0222 /// Support of warning messages.
0223 void Geant4Action::warning(const char* fmt, ...) const {
0224   va_list args;
0225   va_start(args, fmt);
0226   dd4hep::printout(dd4hep::WARNING, m_name, fmt, args);
0227   va_end(args);
0228 }
0229 
0230 /// Action to support error messages.
0231 void Geant4Action::error(const char* fmt, ...) const {
0232   va_list args;
0233   va_start(args, fmt);
0234   dd4hep::printout(dd4hep::ERROR, m_name, fmt, args);
0235   va_end(args);
0236 }
0237 
0238 /// Action to support error messages.
0239 bool Geant4Action::return_error(bool return_value, const char* fmt, ...) const {
0240   va_list args;
0241   va_start(args, fmt);
0242   dd4hep::printout(dd4hep::ERROR, m_name, fmt, args);
0243   va_end(args);
0244   return return_value;
0245 }
0246 
0247 /// Support of fatal messages. Throws exception if required.
0248 void Geant4Action::fatal(const char* fmt, ...) const {
0249   va_list args;
0250   va_start(args, fmt);
0251   dd4hep::printout(dd4hep::FATAL, m_name, fmt, args);
0252   va_end(args);
0253 }
0254 
0255 /// Support of exceptions: Print fatal message and throw runtime_error.
0256 void Geant4Action::except(const char* fmt, ...) const {
0257   va_list args;
0258   va_start(args, fmt);
0259   std::string err = dd4hep::format(m_name, fmt, args);
0260   dd4hep::printout(dd4hep::FATAL, m_name, err.c_str());
0261   va_end(args);
0262   throw std::runtime_error(err);
0263 }
0264 
0265 /// Abort Geant4 Run by throwing a G4Exception with type RunMustBeAborted
0266 void Geant4Action::abortRun(const std::string& exception, const char* fmt, ...) const {
0267   std::string desc, typ = typeName(typeid(*this));
0268   std::string issuer = name()+" ["+typ+"]";
0269   va_list args;
0270   va_start(args, fmt);
0271   desc = dd4hep::format("*** Geant4Action:", fmt, args);
0272   va_end(args);
0273   G4Exception(issuer.c_str(),exception.c_str(),RunMustBeAborted,desc.c_str());
0274   //throw std::runtime_error(issuer+"> "+desc);
0275 }
0276 
0277 /// Access to the main run action sequence from the kernel object
0278 Geant4RunActionSequence& Geant4Action::runAction() const {
0279   return m_context->kernel().runAction();
0280 }
0281 
0282 /// Access to the main event action sequence from the kernel object
0283 Geant4EventActionSequence& Geant4Action::eventAction() const {
0284   return m_context->kernel().eventAction();
0285 }
0286 
0287 /// Access to the main stepping action sequence from the kernel object
0288 Geant4SteppingActionSequence& Geant4Action::steppingAction() const {
0289   return m_context->kernel().steppingAction();
0290 }
0291 
0292 /// Access to the main tracking action sequence from the kernel object
0293 Geant4TrackingActionSequence& Geant4Action::trackingAction() const {
0294   return m_context->kernel().trackingAction();
0295 }
0296 
0297 /// Access to the main stacking action sequence from the kernel object
0298 Geant4StackingActionSequence& Geant4Action::stackingAction() const {
0299   return m_context->kernel().stackingAction();
0300 }
0301 
0302 /// Access to the main generator action sequence from the kernel object
0303 Geant4GeneratorActionSequence& Geant4Action::generatorAction() const {
0304   return m_context->kernel().generatorAction();
0305 }