Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:07:10

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2022 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_ALGTOOL_H
0012 #define GAUDIKERNEL_ALGTOOL_H
0013 // ============================================================================
0014 // Include files
0015 #include "GaudiKernel/CommonMessaging.h"
0016 #include "GaudiKernel/DataObjID.h"
0017 #include "GaudiKernel/IAlgTool.h"
0018 #include "GaudiKernel/IAuditorSvc.h"
0019 #include "GaudiKernel/IDataProviderSvc.h"
0020 #include "GaudiKernel/IMessageSvc.h"
0021 #include "GaudiKernel/IMonitorSvc.h"
0022 #include "GaudiKernel/IProperty.h"
0023 #include "GaudiKernel/IService.h"
0024 #include "GaudiKernel/IStateful.h"
0025 #include "GaudiKernel/ISvcLocator.h"
0026 #include "GaudiKernel/IToolSvc.h"
0027 #include "GaudiKernel/PropertyHolder.h"
0028 #include "GaudiKernel/ToolHandle.h"
0029 #include <Gaudi/PluginService.h>
0030 
0031 #include "GaudiKernel/DataHandle.h"
0032 #include "GaudiKernel/DataHandleHolderBase.h"
0033 #include "GaudiKernel/IDataHandleHolder.h"
0034 
0035 template <class T>
0036 class DataObjectHandle;
0037 
0038 class ToolHandleInfo;
0039 
0040 #include <list>
0041 #include <vector>
0042 
0043 // Forward declarations
0044 class ToolSvc;
0045 
0046 class ToolVisitor;
0047 
0048 /** @class AlgTool AlgTool.h GaudiKernel/AlgTool.h
0049  *
0050  *  Base class from which all the concrete tool classes
0051  *  should be derived. Specific methods for doing something
0052  *  useful should be implemented in the concrete tools.
0053  *  Sub-types of this class could implement an additional
0054  *  interface for behavior common to sets of concrete tools
0055  *  (for example vertexers).
0056  *
0057  *  @author Gloria Corti
0058  *  @author Pere Mato
0059  */
0060 class GAUDI_API AlgTool
0061     : public DataHandleHolderBase<
0062           PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
0063   friend ToolSvc;
0064   friend class ToolVisitor;
0065 
0066 public:
0067   using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
0068 
0069   /// Query for a given interface
0070   StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
0071 
0072   /// Retrieve full identifying name of the concrete tool object.
0073   const std::string& name() const override;
0074 
0075   /// Retrieve type (concrete class) of the sub-algtool.
0076   const std::string& type() const override;
0077 
0078   /// Retrieve parent of the sub-algtool.
0079   const IInterface* parent() const override;
0080 
0081   // State machine implementation
0082   StatusCode                 configure() override { return StatusCode::SUCCESS; }
0083   StatusCode                 initialize() override;
0084   StatusCode                 start() override;
0085   StatusCode                 stop() override;
0086   StatusCode                 finalize() override;
0087   StatusCode                 terminate() override { return StatusCode::SUCCESS; }
0088   StatusCode                 reinitialize() override;
0089   StatusCode                 restart() override;
0090   Gaudi::StateMachine::State FSMState() const override { return m_state; }
0091   Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
0092 
0093   /// Initialize AlgTool
0094   StatusCode sysInitialize() override;
0095 
0096   /// Start AlgTool
0097   StatusCode sysStart() override;
0098 
0099   /// Stop AlgTool
0100   StatusCode sysStop() override;
0101 
0102   /// Finalize AlgTool
0103   StatusCode sysFinalize() override;
0104 
0105   /// Initialize AlgTool
0106   StatusCode sysReinitialize() override;
0107 
0108   /// Start AlgTool
0109   StatusCode sysRestart() override;
0110 
0111 public:
0112   /** Standard Constructor.
0113    *  @param type the concrete class of the sub-algtool
0114    *  @param name the full name of the concrete sub-algtool
0115    *  @param parent the parent of the concrete sub-algtool
0116    */
0117   AlgTool( std::string type, std::string name, const IInterface* parent );
0118 
0119   /// Retrieve pointer to service locator.
0120   SmartIF<ISvcLocator>& serviceLocator() const override;
0121 
0122   /// shortcut for the method service locator
0123   ISvcLocator* svcLoc() const { return serviceLocator(); }
0124 
0125   /** accessor to event service  service
0126    *  @return pointer to detector service
0127    */
0128   IDataProviderSvc* evtSvc() const;
0129 
0130   /// The standard ToolSvc service, Return a pointer to the service if present
0131   IToolSvc* toolSvc() const;
0132 
0133   /** Access a service by name,
0134    *  creating it if it doesn't already exist.
0135    */
0136   template <class T>
0137   StatusCode service( std::string_view name, T*& svc, bool createIf = true ) const {
0138     return service_i( name, createIf, T::interfaceID(), (void**)&svc );
0139   }
0140 
0141   /** Access a service by name, type creating it if it doesn't already exist.
0142    */
0143   template <class T>
0144   StatusCode service( std::string_view type, std::string_view name, T*& svc ) const {
0145     return service_i( type, name, T::interfaceID(), reinterpret_cast<void**>( &svc ) );
0146   }
0147 
0148   /// Return a pointer to the service identified by name (or "type/name")
0149   SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
0150 
0151   template <typename T>
0152   SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
0153     return SmartIF<T>( service( name, createIf, quiet ) );
0154   }
0155 
0156 protected:
0157   template <typename I>
0158   void declareInterface( I* i ) {
0159     m_interfaceList.emplace_back( I::interfaceID(), i );
0160   }
0161 
0162 public:
0163   using PropertyHolderImpl::declareProperty;
0164 
0165   template <class T>
0166   Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
0167                                                  const std::string& doc = "none" ) {
0168     this->declareTool( hndl, hndl.typeAndName() ).ignore();
0169     return PropertyHolderImpl::declareProperty( name, hndl, doc );
0170   }
0171 
0172   template <class T>
0173   StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
0174     return this->declareTool( handle, handle.typeAndName(), createIf );
0175   }
0176 
0177   template <class T>
0178   StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
0179 
0180     StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
0181     if ( !sc ) {
0182       throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
0183                                 " tool " + toolTypeAndName,
0184                             name(), sc };
0185     }
0186 
0187     m_toolHandles.push_back( &handle );
0188 
0189     return sc;
0190   }
0191 
0192   // ==========================================================================
0193   // declare ToolHandleArrays to the AlgTool
0194   template <class T>
0195   Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
0196                                                  const std::string& doc = "none" ) {
0197     addToolsArray( hndlArr );
0198     return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
0199   }
0200 
0201   template <class T>
0202   void addToolsArray( ToolHandleArray<T>& hndlArr ) {
0203     m_toolHandleArrays.push_back( &hndlArr );
0204   }
0205 
0206 public:
0207   void acceptDHVisitor( IDataHandleVisitor* ) const override;
0208 
0209 public:
0210   void registerTool( IAlgTool* tool ) const {
0211     if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
0212     m_tools.push_back( tool );
0213   }
0214 
0215   void deregisterTool( IAlgTool* tool ) const {
0216     auto it = std::find( m_tools.begin(), m_tools.end(), tool );
0217     if ( it != m_tools.end() ) {
0218       if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
0219       m_tools.erase( it );
0220     } else {
0221       if ( msgLevel( MSG::DEBUG ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
0222     }
0223   }
0224 
0225   const std::vector<IAlgTool*>& tools() const;
0226 
0227 protected:
0228   std::vector<IAlgTool*>& tools();
0229 
0230   /// Hook for for derived classes to provide a custom visitor for data handles.
0231   std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
0232 
0233 private:
0234   // place IAlgTools defined via ToolHandles in m_tools
0235   void initToolHandles() const;
0236 
0237 public:
0238   // ==========================================================================
0239   /// Access the auditor service
0240   IAuditorSvc* auditorSvc() const;
0241 
0242   /** @brief Access the monitor service
0243    *
0244    *   @attention Note that this method will return a NULL pointer if no monitor service is
0245    *              configured to be present. You must take this possibility into account when
0246    *              using the pointer
0247    *   @return Pointer to the Monitor service
0248    *   @retval NULL No monitor service is present
0249    *   @retval non-NULL A monitor service is present and available to be used
0250    */
0251   inline IMonitorSvc* monitorSvc() const {
0252     // If not already located try to locate it without forcing a creation
0253     if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
0254     return m_pMonitorSvc.get();
0255   }
0256 
0257   /** Declare monitoring information
0258       @param name Monitoring information name known to the external system
0259       @param var  Monitoring Listener address (the item to monitor...)
0260       @param desc Textual description of the information being monitored
0261   */
0262   template <class T>
0263   void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
0264     IMonitorSvc* mS = monitorSvc();
0265     if ( mS ) mS->declareInfo( name, var, desc, this );
0266   }
0267 
0268   /** Declare monitoring information (special case)
0269       @param name Monitoring information name known to the external system
0270       @param format Format information
0271       @param var  Monitoring Listener address
0272       @param size Monitoring Listener address size
0273       @param desc Textual description of the information being monitored
0274   */
0275   void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
0276                     const std::string& desc ) const {
0277     IMonitorSvc* mS = monitorSvc();
0278     if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
0279   }
0280 
0281   // Standard destructor.
0282   ~AlgTool() override;
0283 
0284 private:
0285   typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
0286 
0287   std::string       m_type;             ///< AlgTool type (concrete class name)
0288   const std::string m_name;             ///< AlgTool full name
0289   const IInterface* m_parent = nullptr; ///< AlgTool parent
0290 
0291   mutable SmartIF<ISvcLocator>      m_svcLocator;  ///< Pointer to Service Locator service
0292   mutable SmartIF<IDataProviderSvc> m_evtSvc;      ///< Event data service
0293   mutable SmartIF<IToolSvc>         m_ptoolSvc;    ///< Tool service
0294   mutable SmartIF<IMonitorSvc>      m_pMonitorSvc; ///< Online Monitoring Service
0295   mutable SmartIF<IAuditorSvc>      m_pAuditorSvc; ///< Auditor Service
0296 
0297   InterfaceList m_interfaceList; ///< Interface list
0298 
0299   // Properties
0300   // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
0301   Gaudi::Property<int> m_outputLevel{
0302       this, "OutputLevel", setUpMessaging(),
0303       [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
0304       "output level" };
0305 
0306   Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
0307                                                  "name to use for Monitor Service" };
0308 
0309   Gaudi::Property<bool> m_auditInit{ this, "AuditTools", false, "[[deprecated]] unused" };
0310   Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
0311   Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
0312   Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
0313   Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
0314   Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
0315   Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
0316 
0317   // tools used by tool
0318   mutable std::vector<IAlgTool*>             m_tools;
0319   mutable std::vector<BaseToolHandle*>       m_toolHandles;
0320   mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
0321   mutable bool m_toolHandlesInit = false; /// flag indicating whether ToolHandle tools have been added to m_tools
0322 
0323   /** implementation of service method */
0324   StatusCode service_i( std::string_view algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
0325   StatusCode service_i( std::string_view svcType, std::string_view svcName, const InterfaceID& iid, void** ppS ) const;
0326 
0327   Gaudi::StateMachine::State m_state       = Gaudi::StateMachine::CONFIGURED; ///< state of the Tool
0328   Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::CONFIGURED; ///< state of the Tool
0329 };
0330 
0331 #endif // GAUDIKERNEL_ALGTOOL_H