Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:20

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 #ifndef DDDIGI_DIGIACTION_H
0014 #define DDDIGI_DIGIACTION_H
0015 
0016 // Framework include files
0017 #include <DD4hep/Printout.h>
0018 #include <DD4hep/ExtensionEntry.h>
0019 #include <DD4hep/ComponentProperties.h>
0020 #include <DDDigi/DigiContext.h>
0021 
0022 // C/C++ include files
0023 #include <string>
0024 #include <memory>
0025 #include <cstdarg>
0026 #include <cstdint>
0027 
0028 #if defined(G__ROOT) || defined(__CLING__) || defined(__ROOTCLING__)
0029 #define DDDIGI_DEFINE_ACTION_DEFAULT_CTOR(action)  public: action() = default;
0030 #else
0031 #define DDDIGI_DEFINE_ACTION_DEFAULT_CTOR(action)  protected: action() = delete;
0032 #endif
0033 
0034 /// 1) Allow default constructor (necessary for ROOT)
0035 /// 2) Inhibit move constructor
0036 /// 3) Inhibit copy constructor
0037 /// 4) Inhibit move operator
0038 /// 5) Inhibit assignment operator
0039 #define DDDIGI_DEFINE_ACTION_CONSTRUCTORS(action)  \
0040   DDDIGI_DEFINE_ACTION_DEFAULT_CTOR(action)        \
0041   protected:                                       \
0042   action(action&& copy) = delete;                  \
0043   action(const action& copy) = delete;             \
0044   action& operator=(action&& copy) = delete;       \
0045   action& operator=(const action& copy) = delete
0046 
0047 /// Namespace for the AIDA detector description toolkit
0048 namespace dd4hep {
0049 
0050   /// Forward declarations
0051   class ExtensionEntry;
0052   
0053   /// Namespace for the Digitization part of the AIDA detector description toolkit
0054   namespace digi {
0055 
0056     /// Helper class to handle strings of the format "type/name"
0057     /**
0058      *  \author  M.Frank
0059      *  \version 1.0
0060      *  \ingroup DD4HEP_DIGITIZATION
0061      */
0062     class TypeName {
0063     public:
0064       std::string first;
0065       std::string second;
0066       /// Default constructor
0067       TypeName() = default;
0068       /// Copy constructor
0069       TypeName(const TypeName& copy) = default;
0070       /// Copy constructor from pair
0071       TypeName(const std::pair<std::string, std::string>& c)
0072         : first(c.first), second(c.second) { }
0073       /// Initializing constructor
0074       TypeName(const std::string& typ, const std::string& nam)
0075         : first(typ), second(nam) { }
0076       /// Assignment operator
0077       TypeName& operator=(const TypeName& copy) = default;
0078       /// Split string pair according to default delimiter ('/')
0079       static TypeName split(const std::string& type_name);
0080       /// Split string pair according to custom delimiter
0081       static TypeName split(const std::string& type_name, const std::string& delim);
0082     };
0083 
0084     /// Default base class for all Digitizer actions and derivates thereof.
0085     /**
0086      *  This is a utility class supporting properties, output and access to
0087      *  event and run objects through the context.
0088      *
0089      *  \author  M.Frank
0090      *  \version 1.0
0091      *  \ingroup DD4HEP_DIGITIZATION
0092      */
0093     class DigiAction {
0094 
0095       friend class DigiKernel;
0096 
0097     public:
0098       using context_t    = DigiContext;
0099       using kernel_t     = DigiKernel;
0100       using extensions_t = std::map<uint64_t, std::unique_ptr<ExtensionEntry> >;
0101 
0102     protected:
0103       template <typename T> struct Extension : public ExtensionEntry {
0104         T* ptr = 0;
0105         
0106         /// Inhibit default constructor
0107         Extension() = delete;
0108         /// Typed objects constructor
0109         Extension(T* p) : ptr(p) { }
0110         /// Copy constructor
0111         Extension(const Extension& copy) = delete;
0112         /// Assignment operator
0113         Extension& operator=(const Extension& copy) = delete;
0114         /// Copy constructor
0115         Extension(Extension&& copy) = delete;
0116         /// Assignment operator
0117         Extension& operator=(Extension&& copy) = delete;
0118         /// Default destructor
0119         virtual ~Extension() = default;
0120         /// Wrapper for the object destruction
0121         virtual void* object()  const override    { return ptr;  }
0122         /// Wrapper for the object destruction
0123         virtual void  destruct()  const override  { delete ptr;  }
0124         virtual void* copy(void*) const override  { invalidCall("copy"); return nullptr; }
0125         virtual ExtensionEntry* clone(void*) const override  { invalidCall("clone"); return nullptr; }
0126         virtual unsigned long long int hash64() const override  { return uint64_t(ptr); }
0127       };
0128 
0129 
0130       /// Reference to the Digi context
0131 #if defined(G__ROOT) || defined(__CLING__) || defined(__ROOTCLING__)
0132       const kernel_t*  m_kernel;
0133 
0134     public:
0135       const kernel_t*  kernel()  const   {
0136     return m_kernel;
0137       }
0138 
0139     protected:
0140 #else
0141       const kernel_t&    m_kernel;
0142 #endif
0143       /// Action name
0144       std::string        m_name;
0145       /// Property pool
0146       PropertyManager    m_properties;
0147       /// Object extensions if used
0148       extensions_t       m_extensions;
0149       /// Reference count. Initial value: 1
0150       long               m_refCount    = 1;
0151       /// Default property: Output level
0152       int                m_outputLevel = 3;
0153       
0154     protected:
0155       /// Define standard assignments and constructors
0156       DDDIGI_DEFINE_ACTION_CONSTRUCTORS(DigiAction);
0157 
0158       /// Default destructor
0159       virtual ~DigiAction();
0160 
0161     public:
0162       /// Standard constructor
0163       DigiAction(const kernel_t& kernel, const std::string& nam);
0164       /// Increase reference count
0165       long addRef();
0166       /// Decrease reference count. Implicit destruction
0167       long release();
0168       /// Access name of the action
0169       const std::string& name() const {
0170         return m_name;
0171       }
0172       /// Access name of the action
0173       const char* c_name() const {
0174         return m_name.c_str();
0175       }
0176       /// Set the object name.
0177       void setName(const std::string& new_name) {
0178         m_name = new_name;
0179       }
0180       /// Access to the properties of the object
0181       PropertyManager& properties()  {
0182         return m_properties;
0183       }
0184       /// Access to the properties of the object (CONST)
0185       const PropertyManager& properties()   const  {
0186         return m_properties;
0187       }
0188       /// Access the output level
0189       PrintLevel outputLevel() const  {
0190         return (PrintLevel)m_outputLevel;
0191       }
0192       /// Set the output level; returns previous value
0193       PrintLevel setOutputLevel(PrintLevel new_level);
0194       
0195       /** Property access                            */
0196       /// Declare property
0197       template <typename T> DigiAction& declareProperty(const std::string& nam, T& val);
0198       /// Declare property
0199       template <typename T> DigiAction& declareProperty(const char* nam, T& val);
0200       /// Declare property
0201       template <typename T> DigiAction& addProperty(const std::string& nam, T& val);
0202       /// Declare property
0203       template <typename T> DigiAction& addProperty(const char* nam, T& val);
0204       /// Check property for existence
0205       bool hasProperty(const std::string& name) const;
0206       /// Access single property
0207       Property& property(const std::string& name);
0208       /// Access single property (CONST)
0209       const Property& property(const std::string& name)  const;
0210       /// Print the property values
0211       virtual void printProperties() const;
0212 
0213       /// Adopt named property of another action for data processing
0214       virtual void adopt_property(DigiAction* action, const std::string& foreign_name, const std::string& local_name);
0215 
0216       /// Adopt named tool to delegate actions
0217       virtual void adopt_tool(DigiAction* action, const std::string& typ);
0218 
0219       /// Add an extension object to the detector element
0220       uint64_t addExtension(uint64_t key, std::unique_ptr<ExtensionEntry>&& e);
0221 
0222       /// Access an existing extension object from the detector element
0223       void* extension(uint64_t key);
0224 
0225       /// Extend the detector element with an arbitrary structure accessible by the type
0226       template <typename T> uint64_t addExtension(T* c)  {
0227         std::unique_ptr<ExtensionEntry> e = std::make_unique<Extension<T> >(c);
0228         return this->addExtension(uint64_t(c), std::move(e));
0229       }
0230       
0231       /** Support for output messages       */
0232       /// Support for messages with variable output level using output level
0233       void print(const char* fmt, ...) const;
0234       /// Support for building formatted messages
0235       std::string format(const char* fmt, ...) const;
0236       /// Support of messages always printed.
0237       void always(const char* fmt, ...) const;
0238       /// Support of debug messages.
0239       void debug(const char* fmt, ...) const;
0240       /// Support of info messages.
0241       void info(const char* fmt, ...) const;
0242       /// Support of warning messages.
0243       void warning(const char* fmt, ...) const;
0244       /// Support of error messages.
0245       void error(const char* fmt, ...) const;
0246       /// Action to support error messages.
0247       bool return_error(bool return_value, const char* fmt, ...) const;
0248       /// Support of fatal messages. Throws exception
0249       void fatal(const char* fmt, ...) const;
0250       /// Support of exceptions: Print fatal message and throw runtime_error.
0251       void except(const char* fmt, ...) const;
0252     };
0253 
0254     /// Declare property
0255     template <typename T> DigiAction& DigiAction::declareProperty(const std::string& nam, T& val) {
0256       this->m_properties.add(nam, val);
0257       return *this;
0258     }
0259 
0260     /// Declare property
0261     template <typename T> DigiAction& DigiAction::declareProperty(const char* nam, T& val) {
0262       this->m_properties.add(nam, val);
0263       return *this;
0264     }
0265     /// Declare property
0266     template <typename T> DigiAction& DigiAction::addProperty(const std::string& nam, T& val) {
0267       void* ptr = ::operator new(sizeof(T));
0268       T* prop = new(ptr) T(val);
0269       this->m_properties.add(nam, *prop);
0270       this->addExtension(prop);
0271       return *this;
0272     }
0273 
0274     /// Declare property
0275     template <typename T> DigiAction& DigiAction::addProperty(const char* nam, T& val) {
0276       void* ptr = ::operator new(sizeof(T));
0277       T* prop = new(ptr) T(val);
0278       this->m_properties.add(nam, *prop);
0279       this->addExtension(prop);
0280       return *this;
0281     }
0282   }    // End namespace digi
0283 }      // End namespace dd4hep
0284 
0285 #endif // DDDIGI_DIGIACTION_H