00001
00002
00003 #include "GaudiKernel/SvcFactory.h"
00004 #include "GaudiKernel/IRegistry.h"
00005 #include "GaudiKernel/IOpaqueAddress.h"
00006
00007 #include "DetDesc/ValidDataObject.h"
00008 #include "DetDesc/RunChangeIncident.h"
00009
00010 #include <boost/format.hpp>
00011
00012
00013 #include "RunChangeHandlerSvc.h"
00014
00015 DECLARE_SERVICE_FACTORY( RunChangeHandlerSvc );
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 RunChangeHandlerSvc::RunChangeHandlerSvc(const std::string &name, ISvcLocator *svcLoc):
00027 Service(name,svcLoc),
00028 m_currentRun(0),
00029 m_evtSvc(0),m_detSvc(0),m_incSvc(0),m_ums(0),m_evtProc(0)
00030 {
00031 declareProperty("Conditions", m_condDesc,
00032 "Map defining what to use to replace the location of the source XML files.");
00033 }
00034
00035
00036
00037
00038 RunChangeHandlerSvc::~RunChangeHandlerSvc() {}
00039
00040
00041
00042
00043 StatusCode RunChangeHandlerSvc::queryInterface(const InterfaceID& riid, void** ppvUnknown){
00044 if ( IIncidentListener::interfaceID().versionMatch(riid) ) {
00045 *ppvUnknown = (IIncidentListener*)this;
00046 addRef();
00047 return StatusCode::SUCCESS;
00048 }
00049 return Service::queryInterface(riid,ppvUnknown);
00050 }
00051
00052
00053
00054
00055 StatusCode RunChangeHandlerSvc::initialize(){
00056
00057 StatusCode sc = Service::initialize();
00058 if (!sc.isSuccess()) return sc;
00059
00060 MsgStream log(msgSvc(),name());
00061 log << MSG::DEBUG << "--- initialize ---" << endmsg;
00062
00063 incidentSvc()->addListener(this, IncidentType::RunChange);
00064
00065 evtProc();
00066 updMgrSvc();
00067
00068
00069 CondDescMap::iterator condDesc;
00070 for (condDesc = m_condDesc.begin(); condDesc != m_condDesc.end(); ++condDesc) {
00071 m_conditions.push_back(CondData(detectorSvc(),condDesc->first,condDesc->second));
00072 updMgrSvc()->registerCondition(this,condDesc->first);
00073 }
00074
00075
00076 return updMgrSvc()->update(this);
00077 }
00078
00079
00080
00081
00082 StatusCode RunChangeHandlerSvc::finalize(){
00083
00084 MsgStream log(msgSvc(),name());
00085 log << MSG::DEBUG << "--- finalize ---" << endmsg;
00086
00087 if (m_incSvc)
00088 incidentSvc()->removeListener(this, IncidentType::RunChange);
00089
00090
00091 release(m_evtSvc);
00092 release(m_detSvc);
00093 release(m_incSvc);
00094 release(m_ums);
00095 release(m_evtProc);
00096
00097
00098 return Service::finalize();
00099 }
00100
00101
00102
00103
00104 void RunChangeHandlerSvc::handle(const Incident &inc) {
00105 MsgStream log( msgSvc(), name() );
00106 log << MSG::DEBUG << inc.type() << " incident received" << endmsg;
00107
00108 const RunChangeIncident* rci = dynamic_cast<const RunChangeIncident*>(&inc);
00109 if (!rci) {
00110 log << MSG::ERROR << "Cannot dynamic_cast the incident to RunChangeIncident, "
00111 " run change ignored" << endmsg;
00112 return;
00113 }
00114
00115 if (m_currentRun != rci->runNumber()) {
00116 log << MSG::DEBUG << "Change of run number detected " << m_currentRun;
00117 m_currentRun = rci->runNumber();
00118 log << "->" << m_currentRun << endmsg;
00119
00120
00121 Conditions::iterator cond;
00122 for (cond = m_conditions.begin(); cond != m_conditions.end(); ++cond) {
00123 update(*cond);
00124 }
00125 }
00126 }
00127
00128
00129
00130
00131 void RunChangeHandlerSvc::update(CondData &cond){
00132
00133 if (cond.object) {
00134 IRegistry *reg = cond.object->registry();
00135 if (reg) {
00136
00137 IOpaqueAddress *addr = reg->address();
00138 if (addr) {
00139
00140
00141 std::string* par = const_cast<std::string*>(addr->par());
00142 par[0] = (boost::format(cond.pathTemplate) % m_currentRun).str();
00143
00144 cond.object->forceUpdateMode();
00145 updMgrSvc()->invalidate(cond.object.ptr());
00146
00147 return;
00148 }
00149 }
00150 }
00151
00152 evtProc()->stopRun();
00153 throw GaudiException("Cannot modify address for object at " + cond.object.path(), name(), StatusCode::FAILURE);
00154 }