00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "GaudiKernel/Kernel.h"
00010 #include "GaudiKernel/IInterface.h"
00011 #include "GaudiKernel/StatusCode.h"
00012 #include "GaudiKernel/SvcFactory.h"
00013 #include "GaudiKernel/MsgStream.h"
00014 #include "GaudiKernel/IIncidentSvc.h"
00015 #include "GaudiKernel/Incident.h"
00016 #include "GaudiKernel/IIncidentListener.h"
00017 #include "GaudiKernel/IDataProviderSvc.h"
00018 #include "GaudiKernel/Service.h"
00019 #include "GaudiKernel/SmartDataPtr.h"
00020 #include "CalibData/Tof/TofCalibData.h"
00021 #include "CalibData/Tof/bTofCalibBase.h"
00022 #include "EventModel/EventModel.h"
00023 #include "EventModel/Event.h"
00024 #include "EventModel/EventHeader.h"
00025 #include "TofQCorrSvc/TofQCorrSvc.h"
00026 #include <math.h>
00027
00028 using namespace std;
00029
00030 TofQCorrSvc::TofQCorrSvc( const std::string& name, ISvcLocator* svcloc ) : Service(name, svcloc) {
00031 }
00032
00033
00034 StatusCode TofQCorrSvc::queryInterface (const InterfaceID& riid, void** ppvInterface ){
00035
00036 if ( IID_ITofQCorrSvc.versionMatch(riid) ) {
00037 *ppvInterface = static_cast<ITofQCorrSvc*> (this);
00038 } else {
00039 return Service::queryInterface(riid, ppvInterface) ;
00040 }
00041 return StatusCode::SUCCESS;
00042 }
00043
00044
00045 StatusCode TofQCorrSvc::initialize ( ) {
00046
00047 MsgStream log(msgSvc(), name());
00048 log << MSG::INFO << name() << ": Start of run initialisation" << endreq;
00049
00050 StatusCode sc = Service::initialize();
00051 if ( sc.isFailure() ) return sc;
00052
00053 IIncidentSvc* incsvc;
00054 sc = service("IncidentSvc", incsvc);
00055 int priority = 100;
00056 if( sc.isSuccess() ){
00057 incsvc -> addListener(this, "NewRun", priority);
00058 }
00059
00060 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
00061 if (sc .isFailure() ) {
00062 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
00063 return sc;
00064 }
00065
00066
00067 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00068 if (sc .isFailure() ) {
00069 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
00070 return sc;
00071 }
00072
00073 StatusCode scc;
00074 log<<MSG::INFO << "setProperties()" << endreq;
00075 scc = setProperties();
00076 m_runNo=0;
00077 return sc;
00078 }
00079
00080
00081 StatusCode TofQCorrSvc::finalize ( ) {
00082 MsgStream log(msgSvc(), name());
00083 log << MSG::INFO << name() << ": End of Run" << endreq;
00084 return StatusCode::SUCCESS;
00085 }
00086
00087
00088 TofQCorrSvc::~TofQCorrSvc(){
00089 }
00090
00091
00092 const double TofQCorrSvc::getBarEast(int layer,int no) {
00093 return m_Bar_East[layer][no];
00094 }
00095
00096
00097 const double TofQCorrSvc::getBarWest(int layer,int no){
00098 return m_Bar_West[layer][no];
00099 }
00100
00101
00102 const double TofQCorrSvc::getEndcap(int layer,int no){
00103 return m_Endcap[layer][no];
00104 }
00105
00106
00107 void TofQCorrSvc::handle(const Incident& inc){
00108 MsgStream log( messageService(), name() );
00109 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00110
00111 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00112 int run = eventHeader->runNumber();
00113 if( run<0 || run>11000 ) return;
00114
00115 if ( inc.type() == "NewRun" ){
00116 log << MSG::DEBUG << "New Run:" << run << endreq;
00117 getTofQCorrInfo();
00118 }
00119 }
00120
00121
00122 void TofQCorrSvc::getTofQCorrInfo(){
00123 MsgStream log(messageService(), name());
00124 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00125 int run = eventHeader->runNumber();
00126 if(run<0){
00127 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
00128 run=-run;
00129 }
00130
00131 char stmt1[200];
00132 sprintf(stmt1,"select TOFCalib "
00133 "from RunParams where run_number = %d", run);
00134
00135 DatabaseRecordVector res;
00136 int row_no = m_dbsvc->query("run",stmt1,res);
00137 if(!row_no){
00138 log << MSG::ERROR<<"search errror"<<endreq;
00139 exit(1);
00140 }
00141 long int runNo;
00142 DatabaseRecord* records1 = res[0];
00143 sscanf((*records1)["TOFCalib"], "%ld", &runNo);
00144
00145 char stmt2[200];
00146 sprintf(stmt2,"select be,end,counter,a1,a2,a3 "
00147 "from TOF_Q where run_number = %ld", runNo);
00148
00149 res.clear();
00150 row_no = m_dbsvc->query("run",stmt2,res);
00151 if(!row_no){
00152 log << MSG::ERROR<<"search errror"<<endreq;
00153 exit(1);
00154 }
00155
00156 long int be,end,counter;
00157 double sql_result[3];
00158
00159 for(int jjj=0;jjj<row_no;jjj++){
00160 DatabaseRecord* records2 = res[jjj];
00161 sscanf((*records2)["be"], "%ld", &be);
00162 sscanf((*records2)["end"], "%ld", &end);
00163 sscanf((*records2)["counter"], "%ld", &counter);
00164 sscanf((*records2)["a1"], "%lf", &sql_result[0]);
00165 sscanf((*records2)["a2"], "%lf", &sql_result[1]);
00166 sscanf((*records2)["a3"], "%lf", &sql_result[2]);
00167 if(be==0&&end==0){
00168 m_Endcap[counter][0]=sql_result[0];
00169 m_Endcap[counter][1]=sql_result[1];
00170 m_Endcap[counter][2]=sql_result[2];
00171 }
00172 if(be==1&&end==0){
00173 m_Bar_East[counter][0]=sql_result[0];
00174 m_Bar_East[counter][1]=sql_result[1];
00175 m_Bar_East[counter][2]=sql_result[2];
00176 }
00177 if(be==1&&end==1){
00178 m_Bar_West[counter][0]=sql_result[0];
00179 m_Bar_West[counter][1]=sql_result[1];
00180 m_Bar_West[counter][2]=sql_result[2];
00181 }
00182 }
00183 res.clear();
00184 }
00185
00186
00187 const double TofQCorrSvc::BQRaw1(int id, double q) {
00188 MsgStream log(msgSvc(), name());
00189 double qraw = -999.0;
00190 if( id<0 || id>175 ) {
00191 log << MSG::WARNING << "BQRaw1 : TofId is out of range, tofid = " << id << endreq;
00192 return qraw;
00193 }
00194 if( fabs(q-10000.0)<1.0e-6 ) { return 10000.0; }
00195 else if( fabs(q+999.0)<1.0e-6 ) { return -999.0; }
00196 double a = m_Bar_East[id][2];
00197 double b = m_Bar_East[id][1];
00198 double c = m_Bar_East[id][0] - q/2.5;
00199 qraw = ( sqrt( b*b - 4.0*a*c ) - b )/( 2.0*a );
00200
00201 return qraw;
00202 }
00203
00204
00205 const double TofQCorrSvc::BQRaw2(int id, double q) {
00206 MsgStream log(msgSvc(), name());
00207 double qraw = -999.0;
00208 if( id<0 || id>175 ) {
00209 log << MSG::WARNING << "BQRaw2 : TofId is out of range, tofid = " << id << endreq;
00210 return qraw;
00211 }
00212 if( fabs(q-10000.0)<1.0e-6 ) { return 10000.0; }
00213 else if( fabs(q+999.0)<1.0e-6 ) { return -999.0; }
00214 double a = m_Bar_West[id][2];
00215 double b = m_Bar_West[id][1];
00216 double c = m_Bar_West[id][0] - q/2.5;
00217 qraw = ( sqrt( b*b - 4.0*a*c ) - b )/( 2.0*a );
00218 return qraw;
00219 }
00220
00221
00222 const double TofQCorrSvc::EQRaw(int id, double q) {
00223 MsgStream log(msgSvc(), name());
00224 double qraw = -999.0;
00225 if( id<0 || id>95 ) {
00226 log << MSG::WARNING << "EQRaw : TofId is out of range, tofid = " << id << endreq;
00227 return qraw;
00228 }
00229 if( fabs(q-10000.0)<1.0e-6 ) { return 10000.0; }
00230 else if( fabs(q+999.0)<1.0e-6 ) { return -999.0; }
00231 double a = m_Endcap[id][2];
00232 double b = m_Endcap[id][1];
00233 double c = m_Endcap[id][0] - q/2.5;
00234 qraw = ( sqrt( b*b - 4.0*a*c ) - b )/( 2.0*a );
00235 return qraw;
00236 }