Gaudi Framework, version v20r3

Generated: 24 Nov 2008

DetDataSvc.cpp

Go to the documentation of this file.
00001 // $Id: DetDataSvc.cpp,v 1.24 2008/10/27 19:22:21 marcocle Exp $
00002 #define  DETECTORDATASVC_DETDATASVC_CPP
00003 
00004 // Include files
00005 #include "DetDataSvc.h"
00006 #include "GaudiKernel/IAddressCreator.h"
00007 #include "GaudiKernel/IConversionSvc.h"
00008 #include "GaudiKernel/IOpaqueAddress.h"
00009 #include "GaudiKernel/ISvcLocator.h"
00010 #include "GaudiKernel/IValidity.h"
00011 #include "GaudiKernel/DataObject.h"
00012 #include "GaudiKernel/MsgStream.h"
00013 #include "GaudiKernel/SvcFactory.h"
00014 
00015 // Instantiation of a static factory class used by clients to create
00016 // instances of this service
00017 DECLARE_SERVICE_FACTORY(DetDataSvc)
00018 
00019 // Service initialisation
00020 StatusCode DetDataSvc::initialize()   {
00021   MsgStream log(msgSvc(), name());
00022 
00023   // Call base class initialisation
00024   StatusCode sc  = DataSvc::initialize();
00025   if( sc.isFailure() ) return sc;
00026 
00027   // Set Data Loader
00028   IConversionSvc* cnv_svc;
00029   sc = serviceLocator()->service(m_persistencySvcName, cnv_svc, true);
00030   if( sc .isFailure() ) {
00031     log << MSG::ERROR << "Unable to retrieve " << m_persistencySvcName << endreq;
00032     return sc;
00033   }
00034 
00035   sc = setDataLoader( cnv_svc );
00036   cnv_svc->release();
00037   if( sc .isFailure() ) {
00038     log << MSG::ERROR << "Unable to set DataLoader" << endreq;
00039     return sc;
00040   }
00041 
00042   // Get address creator fron the DetectorPersistencySvc
00043   sc = cnv_svc->queryInterface(IID_IAddressCreator, pp_cast<void>(&m_addrCreator));
00044   if (sc.isFailure()) {
00045     log << MSG::ERROR << "Unable to get AddressCreator." << endreq;
00046     return StatusCode::FAILURE;
00047   }
00048 
00049   return setupDetectorDescription();
00050 }
00051 
00052 StatusCode DetDataSvc::setupDetectorDescription() {
00053   // Now you can use the MsgSvc
00054   MsgStream log(msgSvc(), name());
00055 
00056   // Initialize the detector data transient store
00057   log << MSG::DEBUG << "Storage type used is: " << m_detStorageType << endreq;
00058   log << MSG::DEBUG << "Setting DetectorDataSvc root node... " << endreq;
00059 
00060   if( m_usePersistency ) {
00061 
00062     IOpaqueAddress* rootAddr;
00063     if( m_detDbLocation.empty() || "empty" == m_detDbLocation ) {
00064 
00065       // if the name of DBlocation is not given - construct it!
00066             // by retrieving the value of XMLDDBROOT
00067 
00068       if ( 0 != getenv("XMLDDDBROOT") ) {
00069         m_detDbLocation  = getenv("XMLDDDBROOT");
00070         m_detDbLocation += "/DDDB/lhcb.xml";
00071       }
00072     }
00073     if( m_detDbLocation.empty() || "empty" == m_detDbLocation ) {
00074       log << MSG::ERROR
00075           << "Detector data location name not set. Detector data will "
00076           << "not be found." << endreq;
00077       return StatusCode::FAILURE;
00078     }
00079     else {
00080       // Create address
00081       unsigned long iargs[]={0,0};
00082       const std::string args[] = {m_detDbLocation, m_detDbRootName};
00083       StatusCode sc = m_addrCreator->createAddress (m_detStorageType,
00084                                                     CLID_Catalog,
00085                                                     args,
00086                                                     iargs,
00087                                                     rootAddr);
00088       if( sc.isSuccess() ) {
00089         std::string dbrName = "/" + m_detDbRootName;
00090         sc = i_setRoot( dbrName, rootAddr );
00091         if( sc.isFailure() ) {
00092           log << MSG::ERROR << "Unable to set detector data store root"
00093               << endreq;
00094           return sc;
00095         }
00096       }
00097       else {
00098         log << MSG::ERROR << "Unable to create address for  /dd" << endreq;
00099         return sc;
00100       }
00101     }
00102     // Writing the description file in the output log file [bugs #2854]
00103     log << MSG::ALWAYS << "Detector description database: " << m_detDbLocation << endreq;
00104   }
00105   else {
00106     log << MSG::INFO << "Detector description not requested to be loaded"
00107         << endreq;
00108   }
00109 
00110   return StatusCode::SUCCESS;
00111 }
00112 
00113 // Service initialisation
00114 StatusCode DetDataSvc::reinitialize()   {
00115   MsgStream log(msgSvc(), name());
00116 
00117   // The DetectorDataSvc does not need to be re-initialized. If it is done
00118   // all the Algorithms having references to DetectorElements will become
00119   // invalid and crash the program.  (Pere Mato)
00120 
00121   // Call base class reinitialization
00122   //StatusCode sc  = DataSvc::reinitialize();
00123   //if( sc.isFailure() ) return sc;
00124 
00125   // Delete the associated event time
00126   //if( 0 != m_eventTime ) delete m_eventTime;
00127   //m_eventTimeDefined = false;
00128 
00129   //return setupDetectorDescription();
00130   return StatusCode::SUCCESS;
00131 }
00132 
00134 StatusCode DetDataSvc::finalize()
00135 {
00136   MsgStream log(msgSvc(), name());
00137   log << MSG::DEBUG << "Finalizing" << endreq;
00138 
00139   // clears the store
00140   m_usePersistency = false; // avoid creation of an empty store when clearing
00141   clearStore().ignore();
00142 
00143   // Releases the address creator
00144   if (0 != m_addrCreator) {
00145     m_addrCreator->release();
00146   }
00147 
00148   // Releases the DataLoader
00149   setDataLoader(0).ignore();
00150 
00151   // Finalize the base class
00152   return DataSvc::finalize();
00153 }
00154 
00156 StatusCode DetDataSvc::queryInterface(const InterfaceID& riid,
00157                                       void** ppvInterface)
00158 {
00159   // With the highest priority return the specific interfaces
00160   // If interfaces are not directly available, try out a base class
00161   if ( IID_IDetDataSvc.versionMatch(riid) ) {
00162     *ppvInterface = (IDetDataSvc*)this;
00163   } else if ( IID_IIncidentListener.versionMatch(riid) ) {
00164     *ppvInterface = (IIncidentListener*)this;
00165   } else {
00166     return DataSvc::queryInterface(riid, ppvInterface);
00167   }
00168   addRef();
00169   return StatusCode::SUCCESS;
00170 }
00171 
00173 StatusCode DetDataSvc::clearStore()   {
00174 
00175   MsgStream log(msgSvc(), name());
00176 
00177   DataSvc::clearStore().ignore();
00178 
00179   if( m_usePersistency ) {
00180     // Create root address
00181     unsigned long iargs[]    = {0,0};
00182     const std::string args[] = {m_detDbLocation, m_detDbRootName};
00183     IOpaqueAddress*   rootAddr;
00184     StatusCode sc = m_addrCreator->createAddress (m_detStorageType,
00185                                                   CLID_Catalog,
00186                                                   args,
00187                                                   iargs,
00188                                                   rootAddr);
00189     // Set detector data store root
00190     if( sc.isSuccess() ) {
00191       std::string dbrName = "/" + m_detDbRootName;
00192       sc = i_setRoot( dbrName, rootAddr );
00193       if( sc.isFailure() ) {
00194         log << MSG::ERROR
00195             << "Unable to set detector data store root" << endreq;
00196       }
00197     } else {
00198       log << MSG::ERROR << "Unable to create address for  /dd" << endreq;
00199     }
00200     return sc;
00201 
00202   }
00203   return StatusCode::SUCCESS;
00204 
00205 }
00206 
00208 DetDataSvc::DetDataSvc(const std::string& name,ISvcLocator* svc) :
00209   DataSvc(name,svc), m_eventTime(0)  {
00210   declareProperty("DetStorageType",  m_detStorageType = XML_StorageType );
00211   declareProperty("DetDbLocation",   m_detDbLocation  = "empty" );
00212   declareProperty("DetDbRootName",   m_detDbRootName  = "dd" );
00213   declareProperty("UsePersistency",  m_usePersistency = false );
00214   declareProperty("PersistencySvc",  m_persistencySvcName = "DetectorPersistencySvc" );
00215   m_rootName = "/dd";
00216   m_rootCLID = CLID_Catalog;
00217   m_addrCreator = 0;
00218 }
00219 
00221 DetDataSvc::~DetDataSvc()  {
00222 }
00223 
00225 void DetDataSvc::setEventTime ( const Gaudi::Time& time ) {
00226   // m_eventTime = TimePoint( time );
00227   m_eventTime = time;
00228   if ( msgSvc()->outputLevel() <= MSG::DEBUG) {
00229     MsgStream log( msgSvc(), name() );
00230     log << MSG::DEBUG << "Event Time set to " << eventTime() << endmsg;
00231   }
00232 }
00233 
00235 bool DetDataSvc::validEventTime ( ) const {
00236   return true;
00237 }
00238 
00240 const Gaudi::Time& DetDataSvc::eventTime ( ) const {
00241   return m_eventTime;
00242 }
00243 
00245 void DetDataSvc::handle ( const Incident& inc ) {
00246   MsgStream log( msgSvc(), name() );
00247   log << MSG::DEBUG << "New incident received" << endreq;
00248   log << MSG::DEBUG << "Incident source: " << inc.source() << endreq;
00249   log << MSG::DEBUG << "Incident type: " << inc.type() << endreq;
00250   return;
00251 }
00252 
00255 StatusCode DetDataSvc::updateObject( DataObject* toUpdate ) {
00256 
00257   MsgStream log( msgSvc(), name() );
00258   log << MSG::DEBUG << "Method updateObject starting" << endreq;
00259 
00260   // Check that object to update exists
00261   if ( 0 == toUpdate ) {
00262     log << MSG::ERROR
00263         << "There is no DataObject to update" << endreq;
00264     return INVALID_OBJECT;
00265   }
00266 
00267   // Retrieve IValidity interface of object to update
00268   IValidity* condition = dynamic_cast<IValidity*>( toUpdate );
00269   if ( 0 == condition ) {
00270     log << MSG::WARNING
00271         << "Cannot update DataObject: DataObject does not implement IValidity"
00272         << endreq;
00273     return StatusCode::SUCCESS;
00274   }
00275 
00276   // Check that the event time has been defined
00277   if ( !validEventTime() ) {
00278     log << MSG::WARNING
00279         << "Cannot update DataObject: event time undefined"
00280         << endreq;
00281     return StatusCode::SUCCESS;
00282   }
00283 
00284   // No need to update if condition is valid
00285   if ( condition->isValid( eventTime() ) ) {
00286     log << MSG::DEBUG
00287         << "DataObject is valid: no need to update" << endreq;
00288     return StatusCode::SUCCESS;
00289   } else {
00290     log << MSG::DEBUG
00291         << "DataObject is invalid: update it" << endreq;
00292   }
00293 
00294   // TODO: before loading updated object, update HERE its parent in data store
00295 
00296   // Now delegate update to the conversion service by calling the base class
00297   log << MSG::DEBUG
00298       << "Delegate update to relevant conversion service" << endreq;
00299   StatusCode status = DataSvc::updateObject(toUpdate);
00300   if ( !status.isSuccess() ) {
00301     log << MSG::ERROR
00302         << "Could not update DataObject" << endreq;
00303     if ( status == NO_DATA_LOADER )
00304       log << MSG::ERROR << "There is no data loader" << endreq;
00305     return status;
00306   }
00307 
00308   // Now cross-check that the new condition is valid
00309   condition = dynamic_cast<IValidity*>(toUpdate);
00310   if ( 0 == condition ) {
00311     log << MSG::ERROR
00312         << "Updated DataObject does not implement IValidity" << endreq;
00313     return StatusCode::FAILURE;
00314   }
00315   if ( !condition->isValid( eventTime() ) ) {
00316     log << MSG::ERROR
00317         << "Updated DataObject is not valid" << endreq;
00318     log << MSG::ERROR
00319         << "Are you sure the conversion service has updated it?" << endreq;
00320     return StatusCode::FAILURE;
00321   }
00322 
00323   // DataObject was successfully updated
00324   log << MSG::DEBUG << "Method updateObject exiting successfully" << endreq;
00325   return StatusCode::SUCCESS;
00326 
00327 }
00328 

Generated at Mon Nov 24 14:38:48 2008 for Gaudi Framework, version v20r3 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004