00001
00002 #include "MeasuredEcmsSvc/MeasuredEcmsSvc.h"
00003 using namespace std;
00004 MeasuredEcmsSvc::MeasuredEcmsSvc(const std::string& name, ISvcLocator* svcLoc):Service(name, svcLoc){
00005 declareProperty("RunParamsMin", RunParamsMin=11414);
00006 declareProperty("RunParamsMax", RunParamsMax=23454);
00007 declareProperty("MeasuredEcmsMin", MeasuredEcmsMin=23463);
00008 declareProperty("MeasuredEcmsMax", MeasuredEcmsMax=38140);
00009 declareProperty("TableName", table="RunParams");
00010 declareProperty("ColumnName", column="beam_energy");
00011 }
00012
00013 MeasuredEcmsSvc::~MeasuredEcmsSvc(){}
00014
00015 StatusCode MeasuredEcmsSvc::initialize(){
00016 MsgStream log(msgSvc(), name());
00017 log<<MSG::INFO<<"MeasuredEcmsSvc initialize begin here"<<endreq;
00018 StatusCode status=Service::initialize();
00019 if(!status.isSuccess()){
00020 log<<MSG::ERROR<<"MeasuredEcmsSvc Can not initialize"<<endreq;
00021 return status;
00022 }
00023 IIncidentSvc* incsvc;
00024 status=service("IncidentSvc", incsvc);
00025 int priority = 100;
00026 if(status.isSuccess()) incsvc->addListener(this, "NewRun", priority);
00027 status=serviceLocator()->service("DatabaseSvc", m_dbSvc, true);
00028 if(!status.isSuccess()){
00029 log<<MSG::ERROR<<"MeasuredEcmsSvc Unable to find DatabaseSvc"<<endreq;
00030 return status;
00031 }
00032 status=serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00033 if(!status.isSuccess()){
00034 log<<MSG::ERROR<<"MeasuredEcmsSvc Unable to find EventDataSvc "<<endreq;
00035 return status;
00036 }
00037 log<<MSG::INFO<<"MeasuredEcmsSvc initialized successfully"<<endreq;
00038 return StatusCode::SUCCESS;
00039 }
00040
00041 StatusCode MeasuredEcmsSvc::finalize(){
00042 MsgStream log(msgSvc(), name());
00043 log<<MSG::INFO<<"MeasuredEcmsSvc finalize begin here"<<endreq;
00044 StatusCode status= Service::finalize();
00045 if(!status.isSuccess()){
00046 log<<MSG::ERROR<<"MeasuredEcmsSvc Can not finalize"<<endreq;
00047 return status;
00048 }
00049 log<<MSG::INFO<<"MeasuredEcmsSvc finalized successfully"<<endreq;
00050 return StatusCode::SUCCESS;
00051 }
00052
00053 StatusCode MeasuredEcmsSvc::queryInterface(const InterfaceID& riid, void** ppvIF){
00054 if(IMeasuredEcmsSvc::interfaceID().versionMatch(riid)) *ppvIF = dynamic_cast<IMeasuredEcmsSvc*>(this);
00055 else return Service::queryInterface(riid, ppvIF);
00056 addRef();
00057 return StatusCode::SUCCESS;
00058 }
00059
00060 void MeasuredEcmsSvc::handle(const Incident& inc){
00061 MsgStream log(messageService(), name());
00062 log<<MSG::DEBUG<<"handle: "<<inc.type()<<endreq;
00063 m_callRead=false; m_recallRead=true;
00064 if(inc.type()=="NewRun"){
00065 log<<MSG::INFO<<"RunNo is new, call the readMeasuredEcms again"<<endreq;
00066 m_callRead=true;
00067 m_sampleName=0; m_ecms=0; m_ecmsErr=0;
00068 m_aveEcms=0; m_aveEcmsErr=0; m_runFrom=0;
00069 m_runTo=0; m_runNoValid=false;
00070 m_px=0; m_py=0; m_pz=0;
00071 m_info=0;
00072 }
00073 }
00074
00075 void MeasuredEcmsSvc::readMeasuredEcms(int runNo){
00076 MsgStream log(msgSvc(), name());
00077 log<<MSG::INFO<<"MeasuredEcmsSvc begin read database MeasuredEcmsSvc"<<endreq;
00078 int runNum=std::abs(runNo);
00079 if(runNo<0) log<<MSG::INFO<<"MeasuredEcmsSvc for MC events"<<endreq;
00080 else log<<MSG::INFO<<"MeasuredEcmsSvc for DT events"<<endreq;
00081 char stmt[400];
00082 snprintf(stmt, 1024, "select sample, RunFrom, RunTo, Ecms, Ecms_err, Ave_Ecms, Ave_Ecms_err from MeasuredEcms2 where RunFrom <= %d and RunTo >= %d", runNum, runNum);
00083 DatabaseRecordVector res;
00084 int rowNo=m_dbSvc->query("offlinedb", stmt, res);
00085 if(rowNo==0){ log<<MSG::ERROR<<"RowNo can not be 0 for MeasuredEcmsSvc, failed to read DB"<<endreq; }
00086 else{
00087 DatabaseRecord* records=res[0];
00088 log<<MSG::INFO<<"MeasuredEcmsSvc first read the runNo, read the MeasuredEcms database"<<runNo<<endreq;
00089 m_runFrom=records->GetDouble("RunFrom");
00090 m_runTo=records->GetDouble("RunTo");
00091 m_sampleName=records->GetDouble("sample");
00092 m_ecms=records->GetDouble("Ecms");
00093 m_ecmsErr=records->GetDouble("Ecms_err");
00094 m_aveEcms=records->GetDouble("Ave_Ecms");
00095 m_aveEcmsErr=records->GetDouble("Ave_Ecms_err");
00096 m_runNoValid=true;
00097 }
00098 }
00099
00100 void MeasuredEcmsSvc::readRunParams(int runNo){
00101 MsgStream log(msgSvc(), name());
00102 log<<MSG::INFO<<"MeasuredEcmsSvc begin read database RunParams"<<endreq;
00103 int runNum=std::abs(runNo);
00104 if(runNo<0) log<<MSG::INFO<<"MeasuredEcmsSvc for MC events"<<endreq;
00105 else log<<MSG::INFO<<"MeasuredEcmsSvc for DT events"<<endreq;
00106 char stmt1[400];
00107 snprintf(stmt1, 1024, "select beam_energy, px, py, pz from RunParams where run_number = %d", runNum);
00108 DatabaseRecordVector res;
00109 int row_no = m_dbSvc->query("offlinedb", stmt1, res);
00110 if(row_no==0){log<<MSG::ERROR<<"RowNo can not be 0 for RunParams, failed to read DB"<<endreq;}
00111 else{
00112 DatabaseRecord* records = res[0];
00113 log<<MSG::INFO<<"MeasuredEcmsSvc first read the runNo, read the RunParams database"<<runNo<<endreq;
00114 double bE=0;
00115 bE=records->GetDouble("beam_energy");
00116 m_px=records->GetDouble("px");
00117 m_py=records->GetDouble("py");
00118 m_pz=records->GetDouble("pz");
00119 m_ecms=bE*2.00;
00120 m_runNoValid=true;
00121 }
00122 }
00123
00124 void MeasuredEcmsSvc::readInfo(int runNo, char *tab, char *col){
00125 MsgStream log(msgSvc(), name());
00126 log<<MSG::INFO<<"MeasuredEcmsSvc begin read database "<<tab<<"-"<<col<<endreq;
00127 int runNum=std::abs(runNo);
00128 if(runNo<0) log<<MSG::INFO<<"MeasuredEcmsSvc for MC events"<<endreq;
00129 else log<<MSG::INFO<<"MeasuredEcmsSvc for DT events"<<endreq;
00130 char stmt1[400];
00131 snprintf(stmt1, 1024, "select %s from %s where run_number = %d", col, tab, runNum);
00132 DatabaseRecordVector res;
00133 int row_no = m_dbSvc->query("offlinedb", stmt1, res);
00134 if(row_no==0){log<<MSG::ERROR<<"RowNo can not be 0 for "<<tab<<", failed to read DB"<<endreq;}
00135 else{
00136 DatabaseRecord* records = res[0];
00137 log<<MSG::INFO<<"MeasuredEcmsSvc first read the runNo, read the "<<tab<<" database "<<runNo<<endreq;
00138 m_info=records->GetDouble(col);
00139 m_runNoValid=true;
00140 }
00141 }
00142
00143 void MeasuredEcmsSvc::readInfo(int runNo){
00144 char tab[100];
00145 char col[100];
00146 strcpy(tab, table.c_str());
00147 strcpy(col, column.c_str());
00148 MsgStream log(msgSvc(), name());
00149 log<<MSG::INFO<<"MeasuredEcmsSvc begin read database "<<tab<<"-"<<col<<endreq;
00150 int runNum=std::abs(runNo);
00151 if(runNo<0) log<<MSG::INFO<<"MeasuredEcmsSvc for MC events"<<endreq;
00152 else log<<MSG::INFO<<"MeasuredEcmsSvc for DT events"<<endreq;
00153 char stmt1[500];
00154 snprintf(stmt1, 1024, "select %s from %s where run_number = %d", col, tab, runNum);
00155 DatabaseRecordVector res;
00156 int row_no = m_dbSvc->query("offlinedb", stmt1, res);
00157 if(row_no==0){log<<MSG::ERROR<<"RowNo can not be 0 for "<<tab<<", failed to read DB"<<endreq;}
00158 else{
00159 DatabaseRecord* records = res[0];
00160 log<<MSG::INFO<<"MeasuredEcmsSvc first read the runNo, read the "<<tab<<" database "<<runNo<<endreq;
00161 m_info=records->GetDouble(col);
00162 m_runNoValid=true;
00163 }
00164 }
00165
00166 void MeasuredEcmsSvc::readDB(int runNo){
00167 if(std::abs(runNo)>=RunParamsMin&&std::abs(runNo)<=RunParamsMax) readRunParams(runNo);
00168 if(std::abs(runNo)>=MeasuredEcmsMin&&std::abs(runNo)<=MeasuredEcmsMax) readMeasuredEcms(runNo);
00169 }
00170
00171 double MeasuredEcmsSvc::getInfo(int runNo, char *tab, char *col){
00172 MsgStream log(msgSvc(), name());
00173 log<<MSG::INFO<<"MeasuredEcmsSvc read the "<<tab<<"-"<<col<<endreq;
00174 if(m_recallRead&&m_callRead){
00175 readInfo(runNo, tab, col);
00176 }
00177 return m_info;
00178 }
00179
00180 double MeasuredEcmsSvc::getInfo(int runNo){
00181 char tab[100];
00182 char col[100];
00183 strcpy(tab, table.c_str());
00184 strcpy(col, column.c_str());
00185 MsgStream log(msgSvc(), name());
00186 log<<MSG::INFO<<"MeasuredEcmsSvc read the "<<tab<<"-"<<col<<endreq;
00187 if(m_recallRead&&m_callRead){
00188 readInfo(runNo);
00189 }
00190 return m_info;
00191 }
00192
00193 int MeasuredEcmsSvc::getSampleName(int runNo){
00194 MsgStream log(msgSvc(), name());
00195 log<<MSG::INFO<<"MeasuredEcmsSvc read the sample name"<<endreq;
00196 if(m_recallRead&&m_callRead){
00197 readDB(runNo);
00198 m_recallRead=false;
00199 }
00200 return m_sampleName;
00201 }
00202
00203 int MeasuredEcmsSvc::getRunFrom(int runNo){
00204 MsgStream log(msgSvc(), name());
00205 log<<MSG::INFO<<"MeasuredEcmsSvc read the RunFrom"<<endreq;
00206 if(m_recallRead&&m_callRead){
00207 readDB(runNo);
00208 m_recallRead=false;
00209 }
00210 return m_runFrom;
00211 }
00212
00213 int MeasuredEcmsSvc::getRunTo(int runNo){
00214 MsgStream log(msgSvc(), name());
00215 log<<MSG::INFO<<"MeasuredEcmsSvc read the RunTo"<<endreq;
00216 if(m_recallRead&&m_callRead){
00217 readDB(runNo);
00218 m_recallRead=false;
00219 }
00220 return m_runTo;
00221 }
00222
00223 double MeasuredEcmsSvc::getEcms(int runNo){
00224 MsgStream log(msgSvc(), name());
00225 log<<MSG::INFO<<"MeasuredEcmsSvc read the ecms"<<endreq;
00226 if(m_recallRead&&m_callRead){
00227 readDB(runNo);
00228 m_recallRead=false;
00229 }
00230 return m_ecms;
00231 }
00232
00233 double MeasuredEcmsSvc::getPx(int runNo){
00234 MsgStream log(msgSvc(), name());
00235 log<<MSG::INFO<<"MeasuredEcmsSvc read the ecms"<<endreq;
00236 if(m_recallRead&&m_callRead){
00237 readDB(runNo);
00238 m_recallRead=false;
00239 }
00240 return m_px;
00241 }
00242
00243
00244 double MeasuredEcmsSvc::getPy(int runNo){
00245 MsgStream log(msgSvc(), name());
00246 log<<MSG::INFO<<"MeasuredEcmsSvc read the ecms"<<endreq;
00247 if(m_recallRead&&m_callRead){
00248 readDB(runNo);
00249 m_recallRead=false;
00250 }
00251 return m_py;
00252 }
00253
00254 double MeasuredEcmsSvc::getPz(int runNo){
00255 MsgStream log(msgSvc(), name());
00256 log<<MSG::INFO<<"MeasuredEcmsSvc read the ecms"<<endreq;
00257 if(m_recallRead&&m_callRead){
00258 readDB(runNo);
00259 m_recallRead=false;
00260 }
00261 return m_pz;
00262 }
00263
00264 double MeasuredEcmsSvc::getEcmsErr(int runNo){
00265 MsgStream log(msgSvc(), name());
00266 log<<MSG::INFO<<"MeasuredEcmsSvc read the error of ecms"<<endreq;
00267 if(m_recallRead&&m_callRead){
00268 readDB(runNo);
00269 m_recallRead=false;
00270 }
00271 return m_ecmsErr;
00272 }
00273
00274 double MeasuredEcmsSvc::getAveEcms(int runNo){
00275 MsgStream log(msgSvc(), name());
00276 log<<MSG::INFO<<"MeasuredEcmsSvc read the average ecms"<<endreq;
00277 if(m_recallRead&&m_callRead){
00278 readDB(runNo);
00279 m_recallRead=false;
00280 }
00281 return m_aveEcms;
00282 }
00283
00284 double MeasuredEcmsSvc::getAveEcmsErr(int runNo){
00285 MsgStream log(msgSvc(), name());
00286 log<<MSG::INFO<<"MeasuredEcmsSvc read the error of the average ecms"<<endreq;
00287 if(m_recallRead&&m_callRead){
00288 readDB(runNo);
00289 m_recallRead=false;
00290 }
00291 return m_aveEcmsErr;
00292 }
00293
00294 bool MeasuredEcmsSvc::isReadDBValid(int runNo, char *tab, char *col){
00295 MsgStream log(msgSvc(), name());
00296 log<<MSG::INFO<<"MeasuredEcmsSvc judge the valid of "<<tab<<"-"<<col<<endreq;
00297 if(m_callRead){
00298 readInfo(runNo, tab, col);
00299 m_recallRead=false;
00300 }
00301 return m_runNoValid;
00302 }
00303
00304 bool MeasuredEcmsSvc::isReadDBValid(int runNo){
00305 char tab[100];
00306 char col[100];
00307 strcpy(tab, table.c_str());
00308 strcpy(col, column.c_str());
00309 MsgStream log(msgSvc(), name());
00310 log<<MSG::INFO<<"MeasuredEcmsSvc judge the valid of "<<tab<<"-"<<col<<endreq;
00311 if(m_callRead){
00312 readInfo(runNo);
00313 m_recallRead=false;
00314 }
00315 return m_runNoValid;
00316 }
00317
00318 bool MeasuredEcmsSvc::isRunNoValid(int runNo){
00319 MsgStream log(msgSvc(), name());
00320 log<<MSG::INFO<<"MeasuredEcmsSvc judge the valid of runNo"<<endreq;
00321 if(m_callRead){
00322 readDB(runNo);
00323 m_recallRead=false;
00324 }
00325 return m_runNoValid;
00326 }