00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include <sstream>
00012 #include <cstring>
00013 #include <cstdlib>
00014 #include <cstdio>
00015
00016 #include "TofEnergyCalibSvc/TofEnergyCalibSvc.h"
00017 #include "GaudiKernel/Kernel.h"
00018 #include "GaudiKernel/IInterface.h"
00019 #include "GaudiKernel/StatusCode.h"
00020 #include "GaudiKernel/SvcFactory.h"
00021 #include "GaudiKernel/MsgStream.h"
00022 #include "GaudiKernel/ISvcLocator.h"
00023 #include "GaudiKernel/SmartDataPtr.h"
00024 #include "GaudiKernel/IDataProviderSvc.h"
00025 #include "GaudiKernel/PropertyMgr.h"
00026
00027
00028 #include "GaudiKernel/IIncidentSvc.h"
00029 #include "GaudiKernel/Incident.h"
00030 #include "GaudiKernel/IIncidentListener.h"
00031
00032 #include "GaudiKernel/ISvcLocator.h"
00033 #include "GaudiKernel/Bootstrap.h"
00034 #include "EventModel/EventModel.h"
00035 #include "EventModel/Event.h"
00036 #include "EventModel/EventHeader.h"
00037
00038 #include <TMath.h>
00039
00040 using namespace std;
00041
00042
00043 TofEnergyCalibSvc::TofEnergyCalibSvc( const string& name, ISvcLocator* svcloc) :
00044 Service (name, svcloc){
00045
00046 declareProperty("Host" , host = std::string("bes3db2.ihep.ac.cn"));
00047 declareProperty("DbName" , dbName = std::string("offlinedb"));
00048 declareProperty("UserName" , userName = std::string("guest"));
00049 declareProperty("Password" , password = std::string("guestpass"));
00050 declareProperty("IsData" , m_IsData = std::string("default"));
00051 declareProperty("BossRelease",m_bossRelease = std::string("default"));
00052
00053 }
00054
00055 TofEnergyCalibSvc::~TofEnergyCalibSvc(){
00056 }
00057
00058 StatusCode TofEnergyCalibSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
00059 if( IID_ITofEnergyCalibSvc.versionMatch(riid) ){
00060 *ppvInterface = static_cast<ITofEnergyCalibSvc*> (this);
00061 } else{
00062 return Service::queryInterface(riid, ppvInterface);
00063 }
00064 return StatusCode::SUCCESS;
00065 }
00066
00067 StatusCode TofEnergyCalibSvc::initialize(){
00068 MsgStream log(messageService(), name());
00069 log << MSG::INFO << "TofEnergyCalibSvc::initialize()" << endreq;
00070
00071 StatusCode sc = Service::initialize();
00072 if( sc.isFailure() ) return sc;
00073
00074
00075 IIncidentSvc* incsvc;
00076 sc = service("IncidentSvc", incsvc);
00077 int priority = 100;
00078 if( sc.isSuccess() ){
00079 incsvc -> addListener(this, "NewRun", priority);
00080 }
00081
00082 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
00083 if (sc .isFailure() ) {
00084 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
00085 return sc;
00086 }
00087
00088 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00089 if (sc .isFailure() ) {
00090 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
00091 return sc;
00092 }
00093
00094 return StatusCode::SUCCESS;
00095 }
00096
00097 StatusCode TofEnergyCalibSvc::finalize(){
00098 MsgStream log(messageService(), name());
00099 log << MSG::INFO << "TofEnergyCalibSvc::finalize()" << endreq;
00100
00101 return StatusCode::SUCCESS;
00102 }
00103
00104 void TofEnergyCalibSvc::handle(const Incident& inc){
00105 MsgStream log( messageService(), name() );
00106 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00107
00108 if ( inc.type() == "NewRun" ){
00109 log << MSG::DEBUG << "NewRun" << endreq;
00110 if(!getTofEnergyCalibSvcInfo()){
00111 log << MSG::ERROR << "can not initilize Tof energy Calib Constants" << endreq;
00112 }
00113 }
00114 }
00115
00116
00117 bool TofEnergyCalibSvc::getTofEnergyCalibSvcInfo(){
00118 MsgStream log(messageService(), name());
00119 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00120 int run = eventHeader->runNumber();
00121 cout<<"Run in getTofEnergyCalibSvcInfo() is: "<<run<<endl;
00122
00123 char stmt1[400];
00124 if(run<0) {
00125 cout << "This data is the MC sample with the Run Number: " << run << endl;
00126 run = -run;
00127 }
00128
00129 if(m_bossRelease=="default") m_bossRelease = getenv("BES_RELEASE");
00130 sprintf(stmt1, "select CalibConst,Para1,Para2,Para3,Para4,Para5 from TofEnergyCalib where BossVer = '%s' and RunFrom <= %d and RunTo >= %d and IsData = '%s'", m_bossRelease.c_str(), run, run, m_IsData.c_str());
00131 cout<<"stmt is:"<<stmt1<<endl;
00132 DatabaseRecordVector res;
00133 int row_no = m_dbsvc->query("offlinedb",stmt1,res);
00134 if( row_no > 0 ) {
00135 DatabaseRecord& dbrec = *res[row_no-1];
00136 m_CalibConst = dbrec.GetDouble("CalibConst");
00137 m_Para1 = dbrec.GetDouble("Para1");
00138 m_Para2 = dbrec.GetDouble("Para2");
00139 m_Para3 = dbrec.GetDouble("Para3");
00140 m_Para4 = dbrec.GetDouble("Para4");
00141 m_Para5 = dbrec.GetDouble("Para5");
00142 cout<<"m_CalibConst is:"<<m_CalibConst<<endl;
00143 cout << "Successfully fetch TofEnergyCalibSvc information for run: "
00144 << run << endl;
00145 return true;
00146 } else if( row_no<=0 ) {
00147 cout << " TofEnergyCalibSvc:: can not found TofEnergyCalibSvc information of run:"
00148 << run << ", boss version " << m_bossRelease << endl;
00149 exit(1);
00150 return false;
00151 }
00152 return true;
00153 }
00154