10#include "MeasuredEcmsSvc/MeasuredEcmsSvc.h"
19 declareProperty(
"RunFrom",
m_runFrom=8093);
20 declareProperty(
"RunTo",
m_runTo=9025);
27 MsgStream log(
msgSvc(), name());
28 log << MSG::INFO <<
"destructor" << endreq;
34 MsgStream log(
msgSvc(), name());
35 log << MSG::INFO <<
"initialize begin here" << endreq;
37 StatusCode status = Service::initialize();
38 if (! status.isSuccess()) {
39 log << MSG::ERROR <<
"can not initialize" << endreq;
44 status = service(
"IncidentSvc", incsvc);
46 if (status.isSuccess()) incsvc->addListener(
this,
"NewRun", priority);
48 status = serviceLocator()->service(
"DatabaseSvc", m_dbSvc,
true);
49 if (! status.isSuccess()) {
50 log << MSG::ERROR<<
"unable to find DatabaseSvc" << endreq;
54 status = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true);
55 if (! status.isSuccess()) {
56 log << MSG::ERROR <<
"unable to find EventDataSvc " << endreq;
65 std::cout<<
"MeasuredEcmsSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
69 log << MSG::INFO <<
"initialized successfully" << endreq;
70 return StatusCode::SUCCESS;
76 MsgStream log(
msgSvc(), name());
77 log << MSG::INFO <<
"finalize begin here" << endreq;
79 StatusCode status = Service::finalize();
80 if (! status.isSuccess()) {
81 log << MSG::ERROR <<
"can not finalize" << endreq;
85 log << MSG::INFO <<
"finalized successfully" << endreq;
86 return StatusCode::SUCCESS;
93 else return Service::queryInterface(riid, ppvIF);
97 return StatusCode::SUCCESS;
103 MsgStream log(messageService(), name());
104 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
106 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
107 int runNo = eventHeader->runNumber();
108 m_runNo = std::abs(
runNo);
110 if (inc.type() ==
"NewRun") {
111 log << MSG::INFO <<
"RunNo is new, call the readMeasuredEcms again" << endreq;
116 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
117 int run = eventHeader->runNumber();
120 m_isRunNoValid =
true;
123 std::cout<<
"MeasuredEcmsSvc, could not get Ecm infor in handle new run"<<std::endl;
132 switch (m_tableOption) {
134 return (m_rowNo1 != 0) ? (double)m_dbRecordVector[
m_sele]->GetDouble(
"Ecms") : 0.00;
138 return (m_rowNo2 != 0) ? (double)m_dbRecordVector[
m_sele]->GetDouble(
"beam_energy") * 2.00 : 0.00;
142 if (m_rowNo1 != 0)
return (
double)m_dbRecordVector[
m_sele]->GetDouble(
"Ecms");
143 else if (m_rowNo2 != 0)
return (
double)m_dbRecordVector[
m_sele]->GetDouble(
"beam_energy") * 2.00;
153 m_runNo = std::abs(
runNo);
157 switch (m_tableOption) {
159 return (m_rowNo1 != 0) ? (double)m_dbRecordVector[
m_sele]->GetDouble(
"Ecms") : 0.00;
163 return (m_rowNo2 != 0) ? (double)m_dbRecordVector[
m_sele]->GetDouble(
"beam_energy") * 2.00 : 0.00;
167 if (m_rowNo1 != 0)
return (
double)m_dbRecordVector[
m_sele]->GetDouble(
"Ecms");
168 else if (m_rowNo2 != 0)
return (
double)m_dbRecordVector[
m_sele]->GetDouble(
"beam_energy") * 2.00;
177 if (
runNo == m_runflag)
return;
180 MsgStream log(
msgSvc(), name());
181 log << MSG::INFO <<
"begin read database MeasuredEcmsSvc" << endreq;
183 m_dbRecordVector.
clear();
188 m_isRunNoValid =
false;
189 int runNum = std::abs(
runNo);
191 if (
runNo < 0) log << MSG::INFO <<
"MeasuredEcmsSvc for MC events" << endreq;
192 else log << MSG::INFO <<
"MeasuredEcmsSvc for DT events" << endreq;
201 switch(m_tableOption) {
203 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);
207 snprintf(stmt, 1024,
"select beam_energy, px, py, pz, beam_energy_err from RunParams where run_number = %d", runNum);
211 snprintf(stmt1, 1024,
"select sample, RunFrom, RunTo, Ecms, Ecms_err, Ave_Ecms, Ave_Ecms_err from MeasuredEcms2 where RunFrom <= %d and RunTo >= %d", runNum, runNum);
212 snprintf(stmt2, 1024,
"select beam_energy, px, py, pz, beam_energy_err from RunParams where run_number = %d", runNum);
216 if (m_tableOption == 1 || m_tableOption == 2) {
217 int rowNo = m_dbSvc->
query(
"offlinedb", stmt, m_dbRecordVector);
220 log << MSG::ERROR <<
"RowNo can not be 0, failed to read DB for " <<
runNo << endreq;
223 if (m_tableOption == 1) m_rowNo1 = 1;
224 if (m_tableOption == 2) m_rowNo2 = 1;
225 log << MSG::INFO <<
"initialize the DatabaseRecordVector OK for " <<
runNo << endreq;
226 m_isRunNoValid =
true;
230 m_rowNo1 = m_dbSvc->
query(
"offlinedb", stmt1, m_dbRecordVector);
232 m_dbRecordVector.
clear();
233 m_rowNo2 = m_dbSvc->
query(
"offlinedb", stmt2, m_dbRecordVector);
236 if (m_rowNo1 == 0 && m_rowNo2 == 0) {
237 log << MSG::ERROR <<
"RowNo can not be 0, failed to read DB for " <<
runNo << endreq;
240 log << MSG::INFO <<
"initialize the DatabaseRecordVector OK for " <<
runNo << endreq;
241 m_isRunNoValid =
true;
249 MsgStream log(
msgSvc(), name());
250 log << MSG::INFO <<
"begin read database MeasuredEcmsSvc from " <<
m_runFrom <<
" to " <<
m_runTo << endreq;
251 m_dbRecordVector.
clear();
255 m_isRunNoValid =
false;
258 if ((runFrom >= 23463 && runFrom <= 38140) || (runFrom >= 47543 && runFrom <= 51498)) {
259 if ((runTo >= 23463 && runTo <= 38140) || (runTo >= 47543 && runTo <= 51498)) {
260 log << MSG::INFO <<
"read table MeasuredEcms2 for " << runFrom <<
":" << runTo << endreq;
261 snprintf(stmt, 1024,
"select sample, RunFrom, RunTo, Ecms, Ecms_err, Ave_Ecms, Ave_Ecms_err from MeasuredEcms2 where RunFrom >= %d and RunTo <= %d", runFrom, runTo);
265 else if ((runFrom >= 11414 && runFrom <= 23454) || (runFrom >= 43716 && runFrom <= 47066)) {
266 if ((runTo >= 11414 && runTo <= 23454) || (runTo >= 43716 && runTo <= 47066)) {
267 log << MSG::INFO <<
"read table RunParams for " << runFrom <<
":" << runTo << endreq;
268 snprintf(stmt, 1024,
"select run_number, beam_energy, px, py, pz, beam_energy_err from RunParams where run_number >= %d and run_number <= %d", runFrom, runTo);
276 if (m_tableOption == 1 || m_tableOption == 2) {
277 int rowNo = m_dbSvc->
query(
"offlinedb", stmt, m_dbRecordVector);
279 log << MSG::ERROR <<
"RowNo can not be 0, failed to read DB for " << runFrom <<
":" << runTo << endreq;
280 cout <<
"RowNo can not be 0, failed to read DB for " << runFrom <<
":" << runTo << endl;
282 if (m_tableOption == 1) m_rowNo1 = 1;
283 if (m_tableOption == 2) m_rowNo2 = 1;
284 log << MSG::INFO <<
"initialize the DatabaseRecordVector OK for " << runFrom <<
":" << runTo << endreq;
285 cout <<
"MeasuredEcmsSvc: initialize the DatabaseRecordVector OK for " << runFrom <<
":" << runTo <<
", Nrecord = " << rowNo <<
"." << endl;
286 m_isRunNoValid =
true;
289 for (
int i=0; i<rowNo; i++ ) {
290 int runnumber = m_dbRecordVector[i]->GetInt(
"run_number");
295 for (
int i=0; i<rowNo; i++ ) {
296 int run0 = m_dbRecordVector[i]->GetInt(
"RunFrom");
297 int run1 = m_dbRecordVector[i]->GetInt(
"RunTo");
298 for (
int j = run0; j<=run1; j++) {
309 MsgStream log(
msgSvc(), name());
310 log << MSG::INFO <<
"triggerDBTable based on runNo" << endl;
312 int runNum = std::abs(
runNo);
314 if ((runNum >= 23463 && runNum <= 38140) || (runNum >= 47543 && runNum <= 51498)) {
315 log << MSG::INFO <<
"read table MeasuredEcms2 for " <<
runNo << endreq;
319 else if ((runNum >= 11414 && runNum <= 23454) || (runNum >= 43716 && runNum <= 47066)) {
320 log << MSG::INFO <<
"read table RunParams for " <<
runNo << endreq;
324 else m_tableOption = -1;
326 return m_tableOption;
333 std::string strRecord;
334 m_isRunNoValid =
false;
336 MsgStream log(
msgSvc(), name());
337 log << MSG::INFO <<
"MeasuredEcmsSvc begin read database " << tab <<
"-" << col << endreq;
339 int runNum = std::abs(
runNo);
341 if (
runNo < 0) log << MSG::INFO <<
"MeasuredEcmsSvc for MC events" << endreq;
342 else log << MSG::INFO <<
"MeasuredEcmsSvc for DT events" << endreq;
345 snprintf(stmt, 1024,
"select %s from %s where %s <= %d and %s >= %d", col, tab, min_col, runNum, max_col, runNum);
348 int row_no = m_dbSvc->
query(
"offlinedb", stmt, res);
351 log << MSG::ERROR <<
"RowNo can not be 0 for " << tab <<
", failed to read DB for " <<
runNo << endreq;
354 log << MSG::INFO <<
"MeasuredEcmsSvc first read the runNo, read the " << tab <<
" database " <<
runNo << endreq;
355 strRecord = res[
m_sele]->GetString(col);
356 m_isRunNoValid =
true;
virtual int query(const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
static const InterfaceID & interfaceID()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvIF)
virtual ~MeasuredEcmsSvc()
void handle(const Incident &)
int triggerDBTable(int runNo)
virtual StatusCode initialize()
MeasuredEcmsSvc(const std::string &name, ISvcLocator *svcLoc)
std::map< int, int > m_mapBeamE
virtual StatusCode finalize()
std::string getRecord(int runNo, char *tab, char *col, char *min_col, char *max_col)
void readDBTable(int runNo)