ToolSvc Class Reference

This service manages tools. More...

#include <src/ApplicationMgr/ToolSvc.h>

Inheritance diagram for ToolSvc:
Collaboration diagram for ToolSvc:

Public Member Functions

StatusCode initialize () override
 Initialize the service. More...
 
StatusCode finalize () override
 Finalize the service. More...
 
StatusCode start () override
 
StatusCode stop () override
 
StatusCode retrieve (const std::string &type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
 Retrieve tool, create it by default as common tool if it does not already exist. More...
 
StatusCode retrieve (const std::string &tooltype, const std::string &toolname, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
 Retrieve tool, create it by default as common tool if it does not already exist. More...
 
std::vector< std::string > getInstances (const std::string &toolType) override
 Get names of all tool instances of a given type. More...
 
std::vector< std::string > getInstances () const override
 Get names of all tool instances. More...
 
std::vector< IAlgTool * > getTools () const override
 Get pointers to all tool instances. More...
 
StatusCode releaseTool (IAlgTool *tool) override
 Release tool. More...
 
StatusCode create (const std::string &type, const IInterface *parent, IAlgTool *&tool)
 Create Tool standard way with automatically assigned name. More...
 
StatusCode create (const std::string &type, const std::string &name, const IInterface *parent, IAlgTool *&tool)
 Create Tool standard way with specified name. More...
 
bool existsTool (const std::string &toolname) const
 Check if the tool instance exists. More...
 
std::string nameTool (const std::string &nameByUser, const IInterface *parent)
 Get Tool full name by combining nameByUser and "parent" part. More...
 
 ToolSvc (const std::string &name, ISvcLocator *svc)
 Standard Constructor. More...
 
 ~ToolSvc () override=default
 Destructor. More...
 
void registerObserver (IToolSvc::Observer *obs) override
 
void unRegisterObserver (IToolSvc::Observer *obs) override
 
- Public Member Functions inherited from extends< BASE, Interfaces >
void * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
 ~extends () override=default
 Virtual destructor. More...
 
void * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
 ~extends () override=default
 Virtual destructor. More...
 
- Public Member Functions inherited from extend_interfaces< Interfaces...>
 ~extend_interfaces () override=default
 Virtual destructor. More...
 
 ~extend_interfaces () override=default
 Virtual destructor. More...
 

Private Member Functions

unsigned long totalToolRefCount () const
 The total number of refCounts on all tools in the instancesTools list. More...
 
unsigned long minimumToolRefCount () const
 The minimum number of refCounts of all tools. More...
 
StatusCode finalizeTool (IAlgTool *itool) const
 Finalize the given tool, with exception handling. More...
 

Private Attributes

std::vector< IAlgTool * > m_instancesTools
 Common Tools. More...
 
IHistorySvcm_pHistorySvc = nullptr
 Pointer to HistorySvc. More...
 
std::vector< IToolSvc::Observer * > m_observers
 

Additional Inherited Members

- Public Types inherited from extends< BASE, Interfaces >
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces...>
 Typedef to the base of this class. More...
 
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces...>
 Typedef to the base of this class. More...
 
- Public Types inherited from extend_interfaces< Interfaces...>
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids...>::type
 take union of the ext_iids of all Interfaces... More...
 
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids...>::type
 take union of the ext_iids of all Interfaces... More...
 

Detailed Description

This service manages tools.

Tools can be common, in which case a single instance can be shared by different algorithms, or private in which case it is necessary to specify the parent when requesting it. The parent of a tool can be an algortihm or a Service The environment of a tool is set by using that of the parent. A common tool is considered to belong to the ToolSvc itself.

Author
G. Corti, P. Mato

Definition at line 22 of file ToolSvc.h.

Constructor & Destructor Documentation

ToolSvc::ToolSvc ( const std::string &  name,
ISvcLocator svc 
)

Standard Constructor.

Parameters
nameString with service name
svcPointer to service locator interface

Definition at line 47 of file ToolSvc.cpp.

49  : base_class( name, svc) { }
extends base_class
Typedef to this class.
Definition: extends.h:14
ToolSvc::~ToolSvc ( )
overridedefault

Destructor.

Member Function Documentation

StatusCode ToolSvc::create ( const std::string &  type,
const IInterface parent,
IAlgTool *&  tool 
)

Create Tool standard way with automatically assigned name.

Definition at line 407 of file ToolSvc.cpp.

411 {
412  const std::string & toolname = tooltype;
413  return create( tooltype, toolname, parent, tool);
414 }
StatusCode create(const std::string &type, const IInterface *parent, IAlgTool *&tool)
Create Tool standard way with automatically assigned name.
Definition: ToolSvc.cpp:407
tuple toolname
Definition: gaudirun.py:327
StatusCode ToolSvc::create ( const std::string &  tooltype,
const std::string &  toolname,
const IInterface parent,
IAlgTool *&  tool 
)

Create Tool standard way with specified name.

Now able to handle clones.

The test of tool existence is performed according to three criteria: name, type and parent. If a tool is private, i.e. the parent is not the tool Svc, and it exist but the parent is not the specified one, a clone is handed over. No clones of public tools are allowed since they would be undistinguishable.

invoke create callbacks...

Definition at line 472 of file ToolSvc.cpp.

477 {
478 
479  // protect against empty type
480  if ( UNLIKELY(tooltype.empty()) ) {
481  error() << "create(): No Tool Type given" << endmsg;
482  return StatusCode::FAILURE;
483  }
484 
485  // If parent has not been specified, assume it is the ToolSvc
486  if ( !parent ) parent = this;
487 
488  tool = nullptr;
489  // Automatically deletes the tool if not explicitly kept (i.e. on success).
490  // The tool is removed from the list of known tools too.
491  auto toolguard = make_toolCreateGuard(m_instancesTools);
492 
493  // Check if the tool already exist : this could happen with clones
494  std::string fullname = nameTool(toolname, parent);
495  if( UNLIKELY(existsTool(fullname)) ) {
496  // Now check if the parent is the same. This allows for clones
498  if ( iAlgTool->name() == toolname && iAlgTool->parent() == parent){
499  // The tool exist with this name, type and parent: this is bad!
500  // This excludes the possibility of cloning public tools intrinsecally
501  error() << "Tool " << fullname << " already exists with the same parent" << endmsg;
502  if (parent == this)
503  error() << "... In addition, the parent is the ToolSvc: public tools cannot be cloned!" << endmsg;
504 
505  return StatusCode::FAILURE;
506  }
507  }
508  ON_DEBUG debug() << "Creating clone of " << fullname << endmsg;
509  }
510  // instantiate the tool using the factory
511  try {
512  toolguard.create( tooltype, fullname, parent );
513  if ( UNLIKELY(! toolguard.get()) ){
514  error() << "Cannot create tool " << tooltype << " (No factory found)" << endmsg;
515  return StatusCode::FAILURE;
516  }
517  }
518  catch ( const GaudiException& Exception ) {
519  // (1) perform the printout of message
520  fatal() << "Exception with tag=" << Exception.tag()
521  << " is caught whilst instantiating tool '" << tooltype << "'" << endmsg;
522  // (2) print the exception itself
523  // (NB! - GaudiException is a linked list of all "previous exceptions")
524  fatal() << Exception << endmsg;
525  return StatusCode::FAILURE;
526  }
527  catch( const std::exception& Exception ) {
528  // (1) perform the printout of message
529  fatal() << "Standard std::exception is caught whilst instantiating tool '"
530  << tooltype << "'" << endmsg;
531  // (2) print the exception itself
532  // (NB! - GaudiException is a linked list of all "previous exceptions")
533  fatal() << Exception.what() << endmsg;
534  return StatusCode::FAILURE;
535  }
536  catch(...) {
537  // (1) perform the printout
538  fatal() << "UNKNOWN Exception is caught whilst instantiating tool '"
539  << tooltype << "'" << endmsg;
540  return StatusCode::FAILURE;
541  }
542  ON_VERBOSE verbose() << "Created tool " << tooltype << "/" << fullname << endmsg;
543 
544  // Since only AlgTool has the setProperties() method it is necessary to cast
545  // to downcast IAlgTool to AlgTool in order to set the properties via the JobOptions
546  // service
547  AlgTool* mytool = dynamic_cast<AlgTool*> (toolguard.get());
548  if ( mytool ) {
549  StatusCode sc = mytool->setProperties();
550  if ( UNLIKELY(sc.isFailure()) ) {
551  error() << "Error setting properties for tool '"
552  << fullname << "'" << endmsg;
553  return sc;
554  }
555  }
556 
557  // Initialize the Tool
559  try {
560  sc = toolguard->sysInitialize();
561  }
562  // Catch any exceptions
563  catch ( const GaudiException & Exception )
564  {
565  error()
566  << "GaudiException with tag=" << Exception.tag()
567  << " caught whilst initializing tool '" << fullname << "'" << endmsg
568  << Exception << endmsg;
569  return StatusCode::FAILURE;
570  }
571  catch( const std::exception & Exception )
572  {
573  error()
574  << "Standard std::exception caught whilst initializing tool '"
575  << fullname << "'" << endmsg << Exception.what() << endmsg;
576  return StatusCode::FAILURE;
577  }
578  catch (...)
579  {
580  error()
581  << "UNKNOWN Exception caught whilst initializing tool '"
582  << fullname << "'" << endmsg;
583  return StatusCode::FAILURE;
584  }
585 
586  // Status of tool initialization
587  if ( UNLIKELY(sc.isFailure()) ) {
588  error() << "Error initializing tool '" << fullname << "'" << endmsg;
589  return sc;
590  }
591 
592  // Start the tool if we are running.
593  if (m_state == Gaudi::StateMachine::RUNNING) {
594  sc = toolguard->sysStart();
595 
596  if (UNLIKELY(sc.isFailure())) {
597  error() << "Error starting tool '" << fullname << "'" << endmsg;
598  return sc;
599  }
600  }
601 
602 
603  // The tool has been successfully created and initialized,
604  // so we inform the guard that it can release it
605  tool = toolguard.release();
606 
610  std::for_each( m_observers.begin(),
611  m_observers.end(),
612  [&](IToolSvc::Observer* obs) { obs->onCreate(tool); } );
613  // TODO: replace by generic callback
614  // Register the tool with the HistorySvc
615  if (m_pHistorySvc || service("HistorySvc",m_pHistorySvc,false).isSuccess() ) {
617  }
618  return StatusCode::SUCCESS;
619 
620 }
IHistorySvc * m_pHistorySvc
Pointer to HistorySvc.
Definition: ToolSvc.h:104
Define general base for Gaudi exception.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::string nameTool(const std::string &nameByUser, const IInterface *parent)
Get Tool full name by combining nameByUser and "parent" part.
Definition: ToolSvc.cpp:623
allow call-backs when a tool is a created or retrieved
Definition: IToolSvc.h:244
std::vector< IToolSvc::Observer * > m_observers
Definition: ToolSvc.h:106
bool existsTool(const std::string &toolname) const
Check if the tool instance exists.
Definition: ToolSvc.cpp:647
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
StatusCode setProperties()
Method for setting declared properties to the values specified in the jobOptions via the job option s...
Definition: AlgTool.cpp:177
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual StatusCode registerAlgTool(const IAlgTool &)=0
virtual const std::string & tag() const
name tag for the exception, or exception type
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:44
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
#define ON_DEBUG
Definition: ToolSvc.cpp:20
#define UNLIKELY(x)
Definition: Kernel.h:126
void ignore() const
Definition: StatusCode.h:108
#define ON_VERBOSE
Definition: ToolSvc.cpp:21
tuple toolname
Definition: gaudirun.py:327
bool ToolSvc::existsTool ( const std::string &  toolname) const

Check if the tool instance exists.

Definition at line 647 of file ToolSvc.cpp.

649 {
650  auto i = std::find_if( std::begin(m_instancesTools), std::end(m_instancesTools),
651  [&](const IAlgTool* tool) { return tool->name() == fullname; } );
652  return i != std::end(m_instancesTools);
653 }
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
list i
Definition: ana.py:128
StatusCode ToolSvc::finalize ( )
override

Finalize the service.

Algorithm: 2 passes. First pass:

  • do not explicitly release any tools
  • finalize tools, in the order of increasing number of refCounts Second pass:
  • explicitly release all tools, one release() on all tools per loop. -> tools are deleted in the order of increasing number of refCounts.

Inner loop: full loop over all left-over tools

  • finalize tools with the minimum number of refCounts in the list.
  • Remove finalized tools from list of tools, and add them to the list of finalized tools, to be deleted at the end. This way, any non-finalized tools who still reference already finalized tools in their finalize() will still find a live tool. Outer loop: keep on going until nothing changes in the list of tools. Checking for:
  • number of left-over tools
  • total number of refcounts
  • minimum number of refcounts

Definition at line 74 of file ToolSvc.cpp.

76 {
77  // Finalize and delete all left-over tools. Normally all tools created with
78  // ToolSvc are left over, since ToolSvc holds a refCount (via AlgTool ctor).
79  // Several cases need to be covered:
80  // 1) Simple dependencies: no circular dependencies between tools,
81  // and tools not using other tools
82  // 2) Tools-using-tools (but no circular dependencies)
83  // a) release() is called in the tool::finalize() (e.g. via GaudiAlgorithm)
84  // b) release() is called in the tool destructor (e.g. via ToolHandle)
85  // 3) Circular dependencies between tools
86  // a) release() is called in the tool::finalize() (e.g. via GaudiAlgorithm)
87  // b) release() is called in the tool destructor (e.g. via ToolHandle)
88  // 4) In addition to each of the above cases, refCounting of a particular tool
89  // might not have been done correctly in the code. Typically release()
90  // is not called, so we are left with a too high refCount.
91  // What to do with those, and how to avoid a crash while handling them...
92 
101  info() << "Removing all tools created by ToolSvc" << endmsg;
102 
103  // Print out list of tools
104  ON_DEBUG {
105  MsgStream &log = debug();
106  log << " Tool List : ";
107  for ( const auto& iTool : m_instancesTools ) {
108  log << iTool->name() << ":" << iTool->refCount( ) << " ";
109  }
110  log << endmsg;
111  }
112 
113  //
114  // first pass: Finalize all tools (but don't delete them)
115  //
128  boost::circular_buffer<IAlgTool*> finalizedTools(m_instancesTools.size()); // list of tools that have been finalized
129  bool fail(false);
130  size_t toolCount = m_instancesTools.size();
131  unsigned long startRefCount = 0;
132  unsigned long endRefCount = totalToolRefCount();
133  unsigned long startMinRefCount = 0;
134  unsigned long endMinRefCount = minimumToolRefCount();
135  while ( toolCount > 0 &&
136  endRefCount > 0 &&
137  (endRefCount != startRefCount || endMinRefCount != startMinRefCount) ) {
138  ON_DEBUG if ( endMinRefCount != startMinRefCount ) {
139  debug() << toolCount << " tools left to finalize. Summed refCounts: "
140  << endRefCount << endmsg;
141  debug() << "Will finalize tools with refCount <= "
142  << endMinRefCount << endmsg;
143  }
144  startMinRefCount = endMinRefCount;
145  startRefCount = endRefCount;
146  unsigned long maxLoop = toolCount + 1;
147  while ( --maxLoop > 0 && !m_instancesTools.empty() ) {
148  IAlgTool* pTool = m_instancesTools.back();
149  // removing tool from list makes ToolSvc::releaseTool( IAlgTool* ) a noop
150  m_instancesTools.pop_back();
151  unsigned long count = pTool->refCount( );
152  // cache tool name
153  const std::string& toolName = pTool->name();
154  if ( count <= startMinRefCount ) {
155  ON_DEBUG debug() << " Performing finalization of " << toolName
156  << " (refCount " << count << ")" << endmsg;
157  // finalize of one tool may trigger a release of another tool
158  // pTool->sysFinalize().ignore();
159  if (!finalizeTool(pTool).isSuccess()) {
160  warning() << " FAILURE finalizing " << toolName << endmsg;
161  fail = true;
162  }
163  // postpone deletion
164  finalizedTools.push_back(pTool);
165  } else {
166  // Place back at the front of the list to try again later
167  // ToolSvc::releaseTool( IAlgTool* ) remains active for this tool
168  ON_DEBUG debug() << " Delaying finalization of " << toolName
169  << " (refCount " << count << ")" << endmsg;
170  m_instancesTools.insert( std::begin(m_instancesTools), pTool );
171  }
172  } // end of inner loop
173  toolCount = m_instancesTools.size();
174  endRefCount = totalToolRefCount();
175  endMinRefCount = minimumToolRefCount();
176  }; // end of outer loop
177 
178  //
179  // Second pass: Delete all finalized tools
180  //
181  // Delete in the order of increasing number of refCounts.
182  // Loop over tools in the same order as the order in which they were finalized.
183  // All tools in the list of finalized tools are no longer in the instancesTools list.
184  // If a tool destructor calls releaseTool() on another tool, this will have no
185  // effect on the 'other tool' if this 'other tool' is in the list of finalized tools.
186  // If this 'other tool' is still in the instancesTools list, it may trigger finalization
187  // (in releaseTool()), deletion and removal from the instancesTools list.
188  // Therefore, the check on non-finalised tools should happen *after* the deletion
189  // of the finalized tools.
190  ON_DEBUG debug() << "Deleting " << finalizedTools.size() << " finalized tools" << endmsg;
191  auto maxLoop = totalRefCount( finalizedTools ) + 1;
192  while ( --maxLoop > 0 && !finalizedTools.empty() ) {
193  IAlgTool* pTool = finalizedTools.front();
194  finalizedTools.pop_front();
195  auto count = pTool->refCount( );
196  if ( count == 1 ) {
197  ON_DEBUG debug() << " Performing deletion of " << pTool->name() << endmsg;
198  } else {
199  ON_VERBOSE verbose() << " Delaying deletion of " << pTool->name()
200  << " (refCount " << count << ")" << endmsg;
201  // Move to the end when refCount still not zero
202  finalizedTools.push_back(pTool);
203  }
204  // do a forced release
205  pTool->release();
206  }
207 
208  // Error if by now not all tools are properly finalised
209  if ( !m_instancesTools.empty() ) {
210  error() << "Unable to finalize and delete the following tools : ";
211  for ( const auto& iTool : m_instancesTools ) {
212  error() << iTool->name() << ": " << iTool->refCount( ) << " ";
213  }
214  error() << endmsg;
215  }
216 
217  // by now, all tools should be deleted and removed.
218  if ( !finalizedTools.empty() ) {
219  error() << "Failed to delete the following " << finalizedTools.size()
220  << " finalized tools. Bug in ToolSvc::finalize()?: ";
221  for ( const auto& iTool : finalizedTools ) {
222  error() << iTool->name() << ": " << iTool->refCount( ) << " ";
223  }
224  error() << endmsg;
225  }
226 
227  if ( m_pHistorySvc ) m_pHistorySvc->release();
228 
229  // Finalize this specific service
230  return ( Service::finalize().isSuccess() && !fail ) ?
233 
234 }
IHistorySvc * m_pHistorySvc
Pointer to HistorySvc.
Definition: ToolSvc.h:104
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalize() override
Definition: Service.cpp:188
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
unsigned long minimumToolRefCount() const
The minimum number of refCounts of all tools.
Definition: ToolSvc.cpp:703
unsigned long totalToolRefCount() const
The total number of refCounts on all tools in the instancesTools list.
Definition: ToolSvc.cpp:697
StatusCode finalizeTool(IAlgTool *itool) const
Finalize the given tool, with exception handling.
Definition: ToolSvc.cpp:656
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
#define ON_DEBUG
Definition: ToolSvc.cpp:20
#define ON_VERBOSE
Definition: ToolSvc.cpp:21
StatusCode ToolSvc::finalizeTool ( IAlgTool itool) const
private

Finalize the given tool, with exception handling.

Definition at line 656 of file ToolSvc.cpp.

658 {
659 
660  // Cache tool name in case of errors
661  const std::string toolName = itool->name();
662  StatusCode sc;
663 
664  // Finalise the tool inside a try block
665  try {
666  sc = itool->sysFinalize();
667  }
668  // Catch any exceptions
669  catch ( const GaudiException & Exception )
670  {
671  error()
672  << "GaudiException with tag=" << Exception.tag()
673  << " caught whilst finalizing tool '" << toolName << "'" << endmsg
674  << Exception << endmsg;
675  sc = StatusCode::FAILURE;
676  }
677  catch( const std::exception & Exception )
678  {
679  error()
680  << "Standard std::exception caught whilst finalizing tool '"
681  << toolName << "'" << endmsg << Exception.what() << endmsg;
682  sc = StatusCode::FAILURE;
683  }
684  catch (...)
685  {
686  error()
687  << "UNKNOWN Exception caught whilst finalizing tool '"
688  << toolName << "'" << endmsg;
689  sc = StatusCode::FAILURE;
690  }
691 
692  return sc;
693 
694 }
Define general base for Gaudi exception.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual StatusCode sysFinalize()=0
Finalization of the Tool.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual const std::string & tag() const
name tag for the exception, or exception type
std::vector< std::string > ToolSvc::getInstances ( const std::string &  toolType)
override

Get names of all tool instances of a given type.

Definition at line 349 of file ToolSvc.cpp.

351 {
352  std::vector<std::string> tools;
353  for(const auto& tool: m_instancesTools) {
354  if (tool->type() == toolType) tools.push_back( tool->name() );
355  }
356  return tools;
357 }
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
std::vector< std::string > ToolSvc::getInstances ( ) const
override

Get names of all tool instances.

Definition at line 359 of file ToolSvc.cpp.

361 {
362  std::vector<std::string> tools{m_instancesTools.size()};
364  std::begin(tools), [](const IAlgTool* t) { return t->name(); } );
365  return tools;
366 }
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
std::vector< IAlgTool * > ToolSvc::getTools ( ) const
override

Get pointers to all tool instances.

Definition at line 368 of file ToolSvc.cpp.

370 {
371  return m_instancesTools;
372 }
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
StatusCode ToolSvc::initialize ( )
override

Initialize the service.

Definition at line 52 of file ToolSvc.cpp.

54 {
55 
56  // initialize the Service Base class
58  if (UNLIKELY(status.isFailure()))
59  {
60  error() << "Unable to initialize the Service" << endmsg;
61  return status;
62  }
63 
64  // set my own (ToolSvc) properties via the jobOptionService
65  if (UNLIKELY(setProperties().isFailure())) {
66  error() << "Unable to set base properties" << endmsg;
67  return StatusCode::FAILURE;
68  }
69 
70  return status;
71 }
StatusCode initialize() override
Definition: Service.cpp:63
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
#define UNLIKELY(x)
Definition: Kernel.h:126
unsigned long ToolSvc::minimumToolRefCount ( ) const
private

The minimum number of refCounts of all tools.

Definition at line 703 of file ToolSvc.cpp.

705 {
706  auto i = std::min_element( std::begin(m_instancesTools), std::end(m_instancesTools),
707  [](const IAlgTool* lhs, const IAlgTool* rhs) {
708  return lhs->refCount() < rhs->refCount();
709  } );
710  return i!=std::end(m_instancesTools) ? (*i)->refCount() : 0;
711 }
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
list i
Definition: ana.py:128
std::string ToolSvc::nameTool ( const std::string &  nameByUser,
const IInterface parent 
)

Get Tool full name by combining nameByUser and "parent" part.

Definition at line 623 of file ToolSvc.cpp.

626 {
627 
628  if ( !parent ) { return this->name() + "." + toolname; } // RETURN
629 
630  // check that parent has a name!
631  auto named_parent = SmartIF<INamedInterface>( const_cast<IInterface*>(parent) );
632  if ( named_parent )
633  {
634  auto fullname = named_parent->name() + "." + toolname ;
635  return fullname ; // RETURN
636  }
637 
638  MsgStream log ( msgSvc(), name() );
639  log << MSG::ERROR
640  << "Private Tools only allowed for components implementing INamedInterface"
641  << endmsg;
642  //
643  return "." + toolname ;
644 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
Definition of the basic interface.
Definition: IInterface.h:234
tuple toolname
Definition: gaudirun.py:327
void ToolSvc::registerObserver ( IToolSvc::Observer obs)
override

Definition at line 713 of file ToolSvc.cpp.

713  {
714  if ( !obs )
715  throw GaudiException( "Received NULL pointer", this->name() + "::registerObserver", StatusCode::FAILURE );
716  m_observers.push_back(obs);
717 }
Define general base for Gaudi exception.
std::vector< IToolSvc::Observer * > m_observers
Definition: ToolSvc.h:106
StatusCode ToolSvc::releaseTool ( IAlgTool tool)
override

Release tool.

Definition at line 374 of file ToolSvc.cpp.

376 {
378  // test if tool is in known list (protect trying to access a previously deleted tool)
379  if ( m_instancesTools.rend() != std::find( m_instancesTools.rbegin(),
380  m_instancesTools.rend(),
381  tool ) ) {
382  unsigned long count = tool->refCount();
383  if ( count == 1 ) {
384  MsgStream log( msgSvc(), name() );
385  // finalize the tool
386 
387  if ( Gaudi::StateMachine::OFFLINE == m_targetState ) {
388  // We are being called during ToolSvc::finalize()
389  // message format matches the one in ToolSvc::finalize()
390  log << MSG::DEBUG << " Performing finalization of " << tool->name()
391  << " (refCount " << count << ")" << endmsg;
392  // message format matches the one in ToolSvc::finalize()
393  log << MSG::DEBUG << " Performing deletion of " << tool->name() << endmsg;
394  } else {
395  log << MSG::DEBUG << "Performing finalization and deletion of " << tool->name() << endmsg;
396  }
397  sc = finalizeTool(tool);
398  // remove from known tools...
399  remove(m_instancesTools,tool);
400  }
401  tool->release();
402  }
403  return sc;
404 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalizeTool(IAlgTool *itool) const
Finalize the given tool, with exception handling.
Definition: ToolSvc.cpp:656
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
StatusCode ToolSvc::retrieve ( const std::string &  type,
const InterfaceID iid,
IAlgTool *&  tool,
const IInterface parent,
bool  createIf 
)
override

Retrieve tool, create it by default as common tool if it does not already exist.

Definition at line 247 of file ToolSvc.cpp.

253 {
254  // check for tools, which by name are required to be public:
255  if ( ba::ends_with( tooltype, s_PUBLIC ) ) {
256  // parent for PUBLIC tool is 'this', i.e. ToolSvc
257  return retrieve ( ba::erase_tail_copy(tooltype, s_PUBLIC.size()),
258  iid , tool , this , createIf ) ;
259  }
260 
261  // protect against empty type
262  if ( tooltype.empty() ) {
263  error() << "retrieve(): No Tool Type/Name given" << endmsg;
264  return StatusCode::FAILURE;
265  }
266  auto pos = tooltype.find('/');
267  if ( std::string::npos == pos ) {
268  return retrieve ( tooltype , tooltype , iid , tool , parent , createIf );
269  }
270  return retrieve ( tooltype.substr( 0 , pos ),
271  tooltype.substr( pos + 1 ),
272  iid , tool , parent , createIf ) ;
273 }
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode retrieve(const std::string &type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
Retrieve tool, create it by default as common tool if it does not already exist.
Definition: ToolSvc.cpp:247
StatusCode ToolSvc::retrieve ( const std::string &  tooltype,
const std::string &  toolname,
const InterfaceID iid,
IAlgTool *&  tool,
const IInterface parent,
bool  createIf 
)
override

Retrieve tool, create it by default as common tool if it does not already exist.

invoke retrieve callbacks...

Definition at line 278 of file ToolSvc.cpp.

285 {
286  // check the applicability of another method:
287  // ignore the provided name if it is empty or the type contains a name
288  if( toolname.empty() || (std::string::npos != tooltype.find('/')) )
289  { return retrieve ( tooltype , iid , tool , parent , createIf ) ; }
290 
291  // check for tools, which by name are required to be public:
292  if ( ba::ends_with( toolname, s_PUBLIC ) )
293  {
294  // parent for PUBLIC tool is this, i.e. ToolSvc
295  return retrieve ( tooltype , ba::erase_tail_copy(toolname, s_PUBLIC.size() ),
296  iid , tool , this , createIf ) ;
297  }
298 
299  IAlgTool* itool = nullptr;
301 
302  tool = nullptr;
303 
304  // If parent is not specified it means it is the ToolSvc itself
305  if( !parent ) parent = this;
306  const std::string fullname = nameTool( toolname, parent );
307 
308  // Find tool in list of those already existing, and tell its
309  // interface that it has been used one more time
310  auto it = std::find_if( std::begin(m_instancesTools), std::end(m_instancesTools),
311  [&](const IAlgTool* i) {
312  return i->name() == fullname && i->parent() == parent;
313  });
314  if (it!=std::end(m_instancesTools)) {
315  ON_DEBUG debug() << "Retrieved tool " << toolname << " with parent " << parent << endmsg;
316  itool = *it;
317  }
318 
319  if ( !itool ) {
320  // Instances of this tool do not exist, create an instance if desired
321  // otherwise return failure
322  if( UNLIKELY(!createIf) ) {
323  warning() << "Tool " << toolname
324  << " not found and creation not requested" << endmsg;
325  return sc;
326  }
327  sc = create( tooltype, toolname, parent, itool );
328  if ( sc.isFailure() ) { return sc; }
329  }
330 
331  // Get the right interface of it
332  sc = itool->queryInterface( iid, pp_cast<void>(&tool) );
333  if( UNLIKELY(sc.isFailure()) ) {
334  error() << "Tool " << toolname
335  << " either does not implement the correct interface, or its version is incompatible"
336  << endmsg;
337  return sc;
338  }
339 
343  std::for_each( std::begin(m_observers),
345  [&]( IToolSvc::Observer* obs ) { obs->onRetrieve(itool); } );
346  return sc;
347 }
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::string nameTool(const std::string &nameByUser, const IInterface *parent)
Get Tool full name by combining nameByUser and "parent" part.
Definition: ToolSvc.cpp:623
allow call-backs when a tool is a created or retrieved
Definition: IToolSvc.h:244
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
StatusCode create(const std::string &type, const IInterface *parent, IAlgTool *&tool)
Create Tool standard way with automatically assigned name.
Definition: ToolSvc.cpp:407
std::vector< IToolSvc::Observer * > m_observers
Definition: ToolSvc.h:106
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual const IInterface * parent() const =0
The parent of the concrete AlgTool.
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
virtual void onRetrieve(const IAlgTool *)
Definition: IToolSvc.h:248
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
#define ON_DEBUG
Definition: ToolSvc.cpp:20
#define UNLIKELY(x)
Definition: Kernel.h:126
list i
Definition: ana.py:128
tuple toolname
Definition: gaudirun.py:327
StatusCode retrieve(const std::string &type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
Retrieve tool, create it by default as common tool if it does not already exist.
Definition: ToolSvc.cpp:247
StatusCode ToolSvc::start ( )
override

Definition at line 726 of file ToolSvc.cpp.

728 {
729 
730  ON_DEBUG debug() << "START transition for AlgTools" << endmsg;
731 
732  bool fail(false);
733  for ( auto& iTool : m_instancesTools ) {
734  ON_VERBOSE verbose() << iTool->name() << "::start()" << endmsg;
735 
736  if (UNLIKELY(!iTool->sysStart().isSuccess())) {
737  fail = true;
738  error() << iTool->name() << " failed to start()" << endmsg;
739  }
740 
741  }
742 
743  if (UNLIKELY(fail)) {
744  error() << "One or more AlgTools failed to start()" << endmsg;
745  return StatusCode::FAILURE;
746  }
747  return StatusCode::SUCCESS;
748 }
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
#define ON_DEBUG
Definition: ToolSvc.cpp:20
#define UNLIKELY(x)
Definition: Kernel.h:126
#define ON_VERBOSE
Definition: ToolSvc.cpp:21
StatusCode ToolSvc::stop ( )
override

Definition at line 752 of file ToolSvc.cpp.

754 {
755 
756  ON_DEBUG debug() << "STOP transition for AlgTools" << endmsg;
757 
758  bool fail(false);
759  for ( auto& iTool : m_instancesTools ) {
760  ON_VERBOSE verbose() << iTool->name() << "::stop()" << endmsg;
761 
762  if (UNLIKELY(!iTool->sysStop().isSuccess())) {
763  fail = true;
764  error() << iTool->name() << " failed to stop()" << endmsg;
765  }
766 
767  }
768 
769  if (UNLIKELY(fail)) {
770  error() << "One or more AlgTools failed to stop()" << endmsg;
771  return StatusCode::FAILURE;
772  }
773  return StatusCode::SUCCESS;
774 }
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
#define ON_DEBUG
Definition: ToolSvc.cpp:20
#define UNLIKELY(x)
Definition: Kernel.h:126
#define ON_VERBOSE
Definition: ToolSvc.cpp:21
unsigned long ToolSvc::totalToolRefCount ( ) const
private

The total number of refCounts on all tools in the instancesTools list.

Definition at line 697 of file ToolSvc.cpp.

699 {
700  return totalRefCount( m_instancesTools );
701 }
std::vector< IAlgTool * > m_instancesTools
Common Tools.
Definition: ToolSvc.h:101
void ToolSvc::unRegisterObserver ( IToolSvc::Observer obs)
override

Definition at line 719 of file ToolSvc.cpp.

719  {
720  auto i = std::find(m_observers.begin(),m_observers.end(),obs);
721  if (i!=m_observers.end()) m_observers.erase(i);
722 }
std::vector< IToolSvc::Observer * > m_observers
Definition: ToolSvc.h:106
list i
Definition: ana.py:128

Member Data Documentation

std::vector<IAlgTool*> ToolSvc::m_instancesTools
private

Common Tools.

Definition at line 101 of file ToolSvc.h.

std::vector<IToolSvc::Observer*> ToolSvc::m_observers
private

Definition at line 106 of file ToolSvc.h.

IHistorySvc* ToolSvc::m_pHistorySvc = nullptr
private

Pointer to HistorySvc.

Definition at line 104 of file ToolSvc.h.


The documentation for this class was generated from the following files: