Gaudi Guide:  Accessing Services

How to setup the jobOptions to make a service available for a particular run.

Use the jobOptions file to setup the services available for a particular run.  Two items can be present in the jobOptions file in order to use a service:
1.) The shared library containing the service must be loaded.  **required**
2.) The service may be listed in the list of services available for a particular run. **optional**

Here is an example, suppose you want to use a service called, myNewSvc.  The service is defined in a shared library called, glastServices.  You would then add the following lines to your jobOptions file:

ApplicationMgr.Dlls += { "glastServices"};  ** required **
ApplicationMgr.ExtSvc += { "myNewSvc" }; **optional**

Accessing a default Gaudi Service from a Gaudi Algorithm

By default, all Gaudi algorithms include methods to access the standard Gaudi services, so that no special calls are required to gain access to these services, the following methods are available to all Gaudi algorithms:

IAuditorSvc*            auditorSvc( ) const;
IChronoStatSvc*     chronoSvc( ) const;
IDataProviderSvc*  detSvc( ) const;
IConversionSvc*     detCnvSvc( ) const;
IDataProviderSvc*  eventSvc( ) const;
IConversionSvc*     eventCnvSvc( ) const;
IHistogramSvc*       histoSvc( ) const;
IMessageSvc*         msgSvc( ) const;
INTupleSvc*            ntupleSvc( ) const;
IRndmGenSvc*        randSvc( ) const;
IToolSvc*                 toolSvc( ) const;

Notice that each of these returns an interface.  Each service implements one or more interfaces.  When we interact with a service, we are actually using a specific interface.

Accessing a Service from a Gaudi Algorithm or Service

If one desires to access an interface of a service that is not among one of the defaults provided to algorithms, how do we gain access to it?  Assuming a service is available for a particular run, how do we access one of its interfaces?  Gaudi has setup a special way for Algorithms and Services to access services, for example:

IIncidentSvc* incsvc = 0;
StatusCode sc = service("IncidentSvc", incSvc, true);
if (sc.isFailure()) {
  log << MSG::ERROR << "Could not find the Gaudi Incident Service!" << endreq;
  return sc;
}

How does this code work?
IIncidentSvc *incsvc = 0;
declares a new variable which is of type IIncidentSvc.  This is the interface we want to gain access to. 

StatusCode sc = service("IncidentSvc", incSvc, true);
 This actually does the work of accessing the interface.  The first parameter is the name of the service, the second is a variable which is a pointer to the interface you are interested in, and the third parameter is optional - true means that the service will be loaded if it is not already.
If this call returns StatusCode::FAILURE, we were unable to access the service.  

Accessing a service from code that is NOT a Gaudi Algorithm or Service

We may also desire to access services from code that is not a Gaudi algorithm or service, we must access the services using a call to serviceLocator( ):

IService *isvc = 0;
StatusCode sc = serviceLocator( )->getService("GlastDetSvc", isvc, true);
if (sc.isSuccess() ) {
  sc = isvc->queryInterface(IID_IGlastDetSvc, (void**)&m_detSvc);
}else {
  log << MSG::WARNING << "Failed to access the GlastDetSvc!" << endreq;
}

What is this code doing?
IService *isvc = 0;
Sets up a generic variable isvc that is a pointer to any service.  
StatusCode sc = serviceLocator( )->getService("GlastDetSvc", isvc, true);
Retrieves a pointer to the service named, GlastDetSvc.  isvc will point to the service.  The third parameter, true, meaning that if the service not already loaded, Gaudi should go ahead and attempt to load the service.
if (sc.isSuccess() ) {
Now we want to check whether or not we were successful in retrieving a pointer to our service.  If so, we do one more check:
sc = isvc->queryInterface(IID_IGlastDetSvc, (void**)&m_detSvc);
We want to make sure that our GlastDetSvc actually implements the IGlastDetSvc interface.

Once your code has accessed the service, you may use the methods defined in the interface(s) that the service implements.

Gaudi Developer Guide Section 11.2 Requesting and accessing services

H. Kelly Last Modified:  2002-12-02 11:37:26 -0800