| # Gaudi Framework, version v21r8 |

Home |  | Generated: 17 Mar 2010

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