00001 #include "GaudiKernel/Kernel.h"
00002 #include "GaudiKernel/IInterface.h"
00003 #include "GaudiKernel/StatusCode.h"
00004 #include "GaudiKernel/SvcFactory.h"
00005 #include "GaudiKernel/MsgStream.h"
00006 #include "GaudiKernel/IIncidentSvc.h"
00007 #include "GaudiKernel/Incident.h"
00008 #include "GaudiKernel/IIncidentListener.h"
00009 #include "GaudiKernel/IDataProviderSvc.h"
00010 #include "GaudiKernel/Service.h"
00011 #include "GaudiKernel/SmartDataPtr.h"
00012 #include "EventModel/EventModel.h"
00013 #include "EventModel/Event.h"
00014 #include "EventModel/EventHeader.h"
00015 #include "BeamEnergySvc/BeamEnergySvc.h"
00016
00017 #include <math.h>
00018
00019 using namespace std;
00020
00021
00022 BeamEnergySvc::BeamEnergySvc( const std::string& name, ISvcLocator* svcloc ) : Service(name, svcloc),
00023 m_run(-1),
00024 m_isRunValid(false),
00025 m_beamE(0)
00026 {
00027 declareProperty("Host",m_host="bes3db2.ihep.ac.cn");
00028 }
00029
00030 BeamEnergySvc::~BeamEnergySvc()
00031 {
00032 }
00033
00034 StatusCode BeamEnergySvc::initialize()
00035 {
00036 MsgStream log(messageService(), name());
00037 log << MSG::INFO << "@initialize()" << endreq;
00038
00039 StatusCode sc = Service::initialize();
00040
00041 if ( sc.isFailure() ) return sc;
00042
00043 IIncidentSvc* incsvc;
00044 sc = service("IncidentSvc", incsvc);
00045 int priority = 100;
00046 if( sc.isSuccess() ){
00047 incsvc -> addListener(this, "NewRun", priority);
00048 }
00049
00050
00051 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
00052 if (sc .isFailure() ) {
00053 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
00054 return sc;
00055 }
00056
00057
00058 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00059 if (sc .isFailure() ) {
00060 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
00061 return sc;
00062 }
00063 m_run=0;
00064 return sc;
00065 }
00066
00067 StatusCode BeamEnergySvc::finalize()
00068 {
00069 MsgStream log(messageService(), name());
00070 log << MSG::INFO << "@initialize()" << endreq;
00071
00072 StatusCode sc = Service::finalize();
00073
00074 return sc;
00075 }
00076
00077 StatusCode BeamEnergySvc::queryInterface(const InterfaceID& riid, void** ppvIF)
00078 {
00079 if ( IBeamEnergySvc::interfaceID().versionMatch(riid) ) {
00080 *ppvIF = dynamic_cast<IBeamEnergySvc*>(this);
00081 }
00082 else {
00083 return Service::queryInterface(riid, ppvIF);
00084 }
00085 addRef();
00086 return StatusCode::SUCCESS;
00087 }
00088
00089 void BeamEnergySvc::handle(const Incident& inc){
00090 MsgStream log( messageService(), name() );
00091 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00092
00093 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00094 int run = eventHeader->runNumber();
00095
00096
00097 if ( inc.type() == "NewRun" ){
00098 log << MSG::DEBUG << "New Run:" << run << endreq;
00099 getBeamEnergyInfo();
00100 }
00101 }
00102
00103 bool BeamEnergySvc::isRunValid() {
00104 return m_isRunValid;
00105 }
00106
00107 double BeamEnergySvc::getbeamE(){
00108 if (!isRunValid()){
00109 std::cerr << "ERROR in BeamEnergySvc: runNo is invalid!" << std::endl;
00110 }
00111 return m_beamE;
00112 }
00113
00114 void BeamEnergySvc::getBeamEnergyInfo(){
00115 MsgStream log(messageService(), name());
00116 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00117 int run = eventHeader->runNumber();
00118
00119 if(run<0){
00120 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
00121 m_isRunValid = true;
00122 m_beamE = 1.8865;
00123 return ;
00124
00125 }
00126 m_run = run;
00127 char stmt1[400];
00128
00129 snprintf(stmt1, 1024,
00130 "select BER_PRB, BPR_PRB "
00131 "from RunParams where run_number = %d", run);
00132
00133 DatabaseRecordVector res;
00134 int row_no = m_dbsvc->query("run", stmt1, res);
00135 if(row_no==0){
00136 log << MSG::FATAL << "can not find result for BeamEnergySvc" << endreq;
00137 exit(1);
00138 }
00139
00140 m_isRunValid = true;
00141
00142 DatabaseRecord* records = res[0];
00143 double E_E=0, E_P=0;
00144 E_E = records->GetDouble("BER_PRB");
00145 E_P = records->GetDouble("BPR_PRB");
00146 m_beamE=(E_E+E_P)/2.0;
00147
00148 return;
00149 }