Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /DD4hep/DDDigi/src/DigiAction.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 <DDDigi/DigiAction.h>
0018 
0019 // C/C++ include files
0020 #include <algorithm>
0021 #include <list>
0022 
0023 using namespace dd4hep::digi;
0024 
0025 TypeName TypeName::split(const std::string& type_name, const std::string& delim) {
0026   std::size_t idx = type_name.find(delim);
0027   std::string typ = type_name, nam = type_name;
0028   if (idx != std::string::npos) {
0029     typ = type_name.substr(0, idx);
0030     nam = type_name.substr(idx + 1);
0031   }
0032   return TypeName(typ, nam);
0033 }
0034 
0035 TypeName TypeName::split(const std::string& type_name) {
0036   return split(type_name,"/");
0037 }
0038 
0039 /// Standard constructor
0040 DigiAction::DigiAction(const DigiKernel& krnl, const std::string& nam)
0041   : m_kernel(krnl), m_name(nam), m_outputLevel(INFO)
0042 {
0043   InstanceCount::increment(this);
0044   declareProperty("name", m_name);
0045   declareProperty("OutputLevel", m_outputLevel=printLevel());
0046 }
0047 
0048 /// Default destructor
0049 DigiAction::~DigiAction() {
0050   for( auto& ptr : m_extensions )
0051     ptr.second->destruct();
0052   m_extensions.clear();
0053   InstanceCount::decrement(this);
0054 }
0055 
0056 /// Implicit destruction
0057 long DigiAction::addRef() {
0058   return ++m_refCount;
0059 }
0060 
0061 /// Decrease reference count. Implicit destruction
0062 long DigiAction::release() {
0063   long count = --m_refCount;
0064   if (m_refCount <= 0) {
0065     debug("Deleting object %s of type %s Pointer:%p",
0066       m_name.c_str(),typeName(typeid(*this)).c_str(),(void*)this);
0067     delete this;
0068   }
0069   return count;
0070 }
0071 
0072 /// Set DigiAction property
0073 void DigiAction::printProperties()   const {
0074   const auto& props = properties().properties();
0075   for( const auto& p : props )   {
0076     std::string pn = name()+"."+p.first;
0077     always("+++ %-32s = %-42s  [%s]", pn.c_str(), p.second.str().c_str(), p.second.type().c_str());
0078   }
0079 }
0080 
0081 /// Adopt named property of another action for data processing
0082 void DigiAction::adopt_property(DigiAction* action, const std::string& foreign_name, const std::string& local_name)   {
0083   if ( action )    {
0084     PropertyManager& mgr = action->properties();
0085     Property& prop = mgr.property(foreign_name);
0086     properties().add(local_name, prop);
0087     return;
0088   }
0089   except("+++ adoptProperty: Invalid source action to access property %s", foreign_name.c_str());
0090 }
0091 
0092 /// Adopt named tool to delegate actions
0093 void DigiAction::adopt_tool(DigiAction* /* action */, const std::string& typ)    {
0094   except("+++ adoptTool: Invalid call: A tool type %s is not useful for action %s",
0095      typ.c_str(), c_name());
0096 }
0097 
0098 /// Set the output level; returns previous value
0099 dd4hep::PrintLevel DigiAction::setOutputLevel(PrintLevel new_level)  {
0100   int old = m_outputLevel;
0101   m_outputLevel = new_level;
0102   return (PrintLevel)old;
0103 }
0104 
0105 /// Check property for existence
0106 bool DigiAction::hasProperty(const std::string& nam) const    {
0107   return m_properties.exists(nam);
0108 }
0109 
0110 /// Access single property
0111 dd4hep::Property& DigiAction::property(const std::string& nam)   {
0112   return properties()[nam];
0113 }
0114 
0115 /// Access single property
0116 const dd4hep::Property& DigiAction::property(const std::string& nam)   const  {
0117   return properties()[nam];
0118 }
0119 
0120 /// Add an extension object to the detector element
0121 uint64_t DigiAction::addExtension(uint64_t key, std::unique_ptr<ExtensionEntry>&& e)  {
0122   if ( m_extensions.emplace(key, std::move(e)).second )
0123     return key;
0124   return 0UL;
0125 }
0126 
0127 /// Access an existing extension object from the detector element
0128 void* DigiAction::extension(uint64_t key)  {
0129   auto iter = m_extensions.find(key);
0130   if ( iter != m_extensions.end() )
0131     return iter->second.get();
0132   return nullptr;
0133 }
0134 
0135 /// Support of debug messages.
0136 std::string DigiAction::format(const char* fmt, ...) const {
0137   va_list args;
0138   va_start(args, fmt);
0139   std::string str = dd4hep::format(nullptr, fmt, args);
0140   va_end(args);
0141   return str;
0142 }
0143 
0144 /// Support for messages with variable output level using output level
0145 void DigiAction::print(const char* fmt, ...) const   {
0146   int level = std::max(int(outputLevel()),(int)VERBOSE);
0147   if ( level >= printLevel() )  {
0148     va_list args;
0149     va_start(args, fmt);
0150     dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
0151     va_end(args);
0152   }
0153 }
0154 
0155 /// Support of debug messages.
0156 void DigiAction::always(const char* fmt, ...) const {
0157   va_list args;
0158   va_start(args, fmt);
0159   dd4hep::printout(dd4hep::FORCE_ALWAYS, m_name, fmt, args);
0160   va_end(args);
0161 }
0162 
0163 /// Support of debug messages.
0164 void DigiAction::debug(const char* fmt, ...) const {
0165   int level = std::max(int(outputLevel()),(int)VERBOSE);
0166   if ( level <= DEBUG )  {
0167     va_list args;
0168     va_start(args, fmt);
0169     dd4hep::printout(dd4hep::FORCE_DEBUG, m_name, fmt, args);
0170     va_end(args);
0171   }
0172 }
0173 
0174 /// Support of info messages.
0175 void DigiAction::info(const char* fmt, ...) const {
0176   int level = std::max(int(outputLevel()),(int)VERBOSE);
0177   if ( level <= INFO )  {
0178     va_list args;
0179     va_start(args, fmt);
0180     dd4hep::printout(dd4hep::FORCE_INFO, m_name, fmt, args);
0181     va_end(args);
0182   }
0183 }
0184 
0185 /// Support of warning messages.
0186 void DigiAction::warning(const char* fmt, ...) const {
0187   int level = std::max(int(outputLevel()),(int)VERBOSE);
0188   if ( level <= WARNING )  {
0189     va_list args;
0190     va_start(args, fmt);
0191     dd4hep::printout(dd4hep::FORCE_WARNING, m_name, fmt, args);
0192     va_end(args);
0193   }
0194 }
0195 
0196 /// Action to support error messages.
0197 void DigiAction::error(const char* fmt, ...) const {
0198   int level = std::max(int(outputLevel()),(int)VERBOSE);
0199   if ( level <= ERROR )  {
0200     va_list args;
0201     va_start(args, fmt);
0202     dd4hep::printout(dd4hep::FORCE_ERROR, m_name, fmt, args);
0203     va_end(args);
0204   }
0205 }
0206 
0207 /// Action to support error messages.
0208 bool DigiAction::return_error(bool return_value, const char* fmt, ...) const {
0209   int level = std::max(int(outputLevel()),(int)VERBOSE);
0210   if ( level <= ERROR )  {
0211     va_list args;
0212     va_start(args, fmt);
0213     dd4hep::printout(dd4hep::FORCE_ERROR, m_name, fmt, args);
0214     va_end(args);
0215   }
0216   return return_value;
0217 }
0218 
0219 /// Support of fatal messages. Throws exception if required.
0220 void DigiAction::fatal(const char* fmt, ...) const {
0221   va_list args;
0222   va_start(args, fmt);
0223   dd4hep::printout(dd4hep::FORCE_FATAL, m_name, fmt, args);
0224   va_end(args);
0225 }
0226 
0227 /// Support of exceptions: Print fatal message and throw runtime_error.
0228 void DigiAction::except(const char* fmt, ...) const {
0229   va_list args;
0230   va_start(args, fmt);
0231   std::string err = dd4hep::format(m_name, fmt, args);
0232   dd4hep::printout(dd4hep::FORCE_FATAL, m_name, err.c_str());
0233   va_end(args);
0234   throw std::runtime_error(err);
0235 }
0236 
0237 /// Namespace for the AIDA detector description toolkit
0238 namespace dd4hep {
0239 
0240   /// Namespace for the Digitization part of the AIDA detector description toolkit
0241   namespace digi {
0242     template <typename VAL>
0243     int add_action_property(DigiAction* action, const std::string& name, VAL value)   {
0244       action->addProperty(name, value);
0245       printout(INFO, "addProperty", "+++ Added property %s of type %s",
0246                name.c_str(), typeName(typeid(VAL)).c_str());
0247       return 1;
0248     }
0249 
0250 #define ADD_SINGLE_PROPERTY(X)                                          \
0251     template int add_action_property<X>(DigiAction* action, const std::string& name, X value);
0252 
0253 #define ADD_MAPPED_PROPERTY(K,X)                                        \
0254     template int add_action_property<std::map<std::string,X> >(DigiAction* action, const std::string& name, std::map<K,X> value);
0255 
0256 #define ADD_PROPERTY(X)                         \
0257     ADD_SINGLE_PROPERTY(X)                      \
0258     ADD_SINGLE_PROPERTY(std::set<X>)            \
0259     ADD_SINGLE_PROPERTY(std::list<X>)           \
0260     ADD_SINGLE_PROPERTY(std::vector<X>)                 \
0261     ADD_MAPPED_PROPERTY(std::string,X)
0262 
0263     ADD_PROPERTY(int)
0264     ADD_PROPERTY(float)
0265     ADD_PROPERTY(double)
0266     ADD_PROPERTY(std::string)
0267     ADD_PROPERTY(dd4hep::Position)
0268 
0269     ADD_SINGLE_PROPERTY(size_t)
0270   }
0271 }
0272 
0273 #include "DD4hep/GrammarUnparsed.h"
0274 namespace dd4hep   {
0275 
0276   static bool _from_string(const BasicGrammar&, void* ptr, const std::string& value)    {
0277     static constexpr const char MATCH[] = " object at 0x";
0278     size_t idx = value.find(MATCH);
0279     if ( idx != std::string::npos )    {
0280       void* p = 0;
0281       const char* cptr = value.c_str()+idx+strlen(MATCH)-2;
0282       if ( 1 == ::sscanf(cptr,"%p", &p) )   {
0283     *(void**)ptr = p;
0284     return true;
0285       }
0286     }
0287     dd4hep::except("FAIL","FAIL");
0288     return false;
0289   }
0290 
0291   template <> const GrammarRegistry& GrammarRegistry::pre_note<DigiAction*>(int)   {
0292     BasicGrammar::specialization_t spec;
0293     spec.bind = nullptr;
0294     spec.copy = nullptr;
0295     spec.eval = nullptr;
0296     spec.str  = nullptr;
0297     spec.fromString = _from_string;
0298     return pre_note_specs<DigiAction*>(spec);
0299   }
0300 }
0301 
0302 static auto s_registry = dd4hep::GrammarRegistry::pre_note<DigiAction*>(1);