00001 #include "DetVerSvc/DetVerSvc.h"
00002 #include "GaudiKernel/SmartIF.h"
00003 #include "GaudiKernel/IJobOptionsSvc.h"
00004 #include "GaudiKernel/MsgStream.h"
00005 #include <fstream>
00006 #include <dlfcn.h>
00007
00008 namespace DetVerSvcPack {
00009 int (*pf_helper)(ISvcLocator*);
00010 }
00011
00012 DetVerSvc::DetVerSvc(const std::string& name, ISvcLocator* svcloc)
00013 : Service(name, svcloc),
00014 m_phase(-1)
00015 {
00016 declareProperty("Config", m_conf);
00017 }
00018
00019 DetVerSvc::~DetVerSvc()
00020 {
00021 }
00022
00023 StatusCode DetVerSvc::queryInterface(const InterfaceID& riid, void** ppvInterface)
00024 {
00025 if ( IID_IDetVerSvc.versionMatch(riid) ) {
00026 *ppvInterface = static_cast<IDetVerSvc*> (this);
00027 } else {
00028 return Service::queryInterface(riid, ppvInterface) ;
00029 }
00030 return StatusCode::SUCCESS;
00031 }
00032
00033 StatusCode DetVerSvc::initialize()
00034 {
00035 Service::initialize();
00036
00037 MsgStream log(messageService(), name());
00038 log << MSG::INFO << name() << ": Start of run initialisation" << endreq;
00039
00040
00041 if ( access( m_conf.c_str(), F_OK ) < 0 ) {
00042 log << MSG::FATAL << "Cann't find config file: " << m_conf << endreq;
00043 return StatusCode::FAILURE;
00044 }
00045
00046 return StatusCode::SUCCESS;
00047 }
00048
00049 StatusCode DetVerSvc::finalize ( ) {
00050 MsgStream log(messageService(), name());
00051 log << MSG::INFO << name() << ": End of Run finalize" << endreq;
00052
00053 return StatusCode::SUCCESS;
00054 }
00055
00056 int DetVerSvc::phase()
00057 {
00058 if ( m_phase < 0) {
00059 SmartIF<IJobOptionsSvc> iSvc(serviceLocator()->service("JobOptionsSvc"));
00060 if ( iSvc.isValid() ) {
00061 std::string dll;
00062 const std::vector<const Property*>* ps = 0;
00063 if ( (ps = iSvc->getProperties("RawDataInputSvc")) != 0 ) {
00064 dll = "libDetVerSvc_IRaw.so";
00065 }
00066 else if ( (ps = iSvc->getProperties("EventCnvSvc")) != 0 ) {
00067 dll = "libDetVerSvc_IRoot.so";
00068 }
00069 else if ( (ps = iSvc->getProperties("RealizationSvc")) != 0 ) {
00070 dll = "libDetVerSvc_Sim.so";
00071 }
00072
00073
00074 void *dl_handler = dlopen(dll.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00075 if ( dl_handler != 0 ) {
00076 m_phase = fromRun( (*DetVerSvcPack::pf_helper)(serviceLocator()) );
00077 }
00078 dlclose(dl_handler);
00079 }
00080 }
00081 return m_phase;
00082 }
00083
00084 int DetVerSvc::fromRun(unsigned int run)
00085 {
00086 std::vector<int> runList;
00087
00088 int iTmp;
00089 std::ifstream fs(m_conf.c_str());
00090 fs >> iTmp;
00091 while ( ! fs.eof() ) {
00092 runList.push_back(iTmp);
00093 fs >> iTmp;
00094 }
00095
00096
00097 int iPhase = 1;
00098 for ( std::vector<int>::iterator it = runList.begin(); it != runList.end(); ++it ) {
00099 if ( run < (*it) ) {
00100 break;
00101 }
00102 ++iPhase;
00103 }
00104 return iPhase;
00105 }