18#include "GaudiKernel/IDataProviderSvc.h"
19#include "GaudiKernel/IInterface.h"
20#include "GaudiKernel/ISvcLocator.h"
21#include "GaudiKernel/Kernel.h"
22#include "GaudiKernel/MsgStream.h"
23#include "GaudiKernel/PropertyMgr.h"
24#include "GaudiKernel/SmartDataPtr.h"
25#include "GaudiKernel/StatusCode.h"
26#include "GaudiKernel/SvcFactory.h"
29#include "GaudiKernel/IIncidentListener.h"
30#include "GaudiKernel/IIncidentSvc.h"
31#include "GaudiKernel/Incident.h"
36#include "GaudiKernel/Bootstrap.h"
37#include "GaudiKernel/ISvcLocator.h"
51 declareProperty(
"Host", m_host = std::string(
"202.122.33.123"));
52 declareProperty(
"DbName", m_dbName = std::string(
"tof40t_linux"));
53 declareProperty(
"TableName", m_table = std::string(
"MDCLayerVmon"));
54 declareProperty(
"UserName", m_userName = std::string(
"guest"));
55 declareProperty(
"Password", m_password = std::string(
"guestpass"));
56 declareProperty(
"Port", m_port=6175);
57 declareProperty(
"IgnoreLayer_21_24",m_ignoreLayer_21_24=
true);
58 declareProperty(
"RelativeHvDropThreshold",m_relativeHvDropThreshold=0.007);
59 declareProperty(
"FetchHvDataLengthInSeconds",m_fetchLength=5*3600);
60 declareProperty(
"UseEtsT1",m_useEtsT1=0);
61 declareProperty(
"EtsT1OffSet",m_etsT1Offset=0);
64 declareProperty(
"Host2", m_host2 = std::string(
"bes3db2.ihep.ac.cn"));
65 declareProperty(
"DbName2", m_dbName2 = std::string(
"run"));
66 declareProperty(
"TableName2", m_table2 = std::string(
"RunParams"));
67 declareProperty(
"UserName2", m_userName2 = std::string(
"guest"));
68 declareProperty(
"Password2", m_password2 = std::string(
"guestpass"));
69 declareProperty(
"Port2", m_port2=3306);
76 if (IID_IMdcHvDropSvc.versionMatch(riid)) {
79 return Service::queryInterface(riid, ppvInterface);
81 return StatusCode::SUCCESS;
85 MsgStream log(messageService(), name());
86 log << MSG::INFO <<
"MdcHvDropSvc::initialize()" << endreq;
88 StatusCode sc = Service::initialize();
89 if (sc.isFailure())
return sc;
92 sc = service(
"IncidentSvc", incsvc);
95 incsvc->addListener(
this,
"NewRun", priority);
104 sc = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true);
105 if (sc.isFailure()) {
106 log << MSG::ERROR <<
"Unable to find EventDataSvc " << endreq;
112 log << MSG::ERROR <<
"Unable to initialize mysql " << endreq;
116 return StatusCode::SUCCESS;
120 MsgStream log(messageService(), name());
121 log << MSG::INFO <<
"MdcHvDropSvc::finalize()" << endreq;
123 return StatusCode::SUCCESS;
127 MsgStream log(messageService(), name());
128 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
166StatusCode MdcHvDropSvc::initMySql(){
174 mysql_init(&m_mysql);
176 if(!mysql_real_connect(&m_mysql,m_host.c_str(),m_userName.c_str(),m_password.c_str(),m_dbName.c_str(),m_port,
NULL,0)){
177 std::cerr<<
"Connect to database" <<m_host<<
":"<<m_port<<
" failed\n"<<
178 "due to "<<mysql_error(&m_mysql)<<std::endl;
179 return StatusCode::FAILURE;
181 return StatusCode::SUCCESS;
195size_t MdcHvDropSvc::Time_t2str(
Time_t utctime,
char* str,
int length){
196 const int tzOffset=60*60*8;
197 std::time_t timeShanghai=utctime+tzOffset;
198 std::tm *tm=std::gmtime(&timeShanghai);
199 return std::strftime(str,length,
"%Y-%m-%d %H:%M:%S", tm);
210Time_t MdcHvDropSvc::datetimeStr2Time_t(
const char* str){
211 const int tzOffset=60*60*8;
213 int year, month, day, hour,
min, sec;
214 std::sscanf(str,
"%d-%d-%d %d:%d:%d",
215 & year, & month, & day, & hour, &
min, & sec);
216 tm.tm_year = year-1900;
223 tm.tm_gmtoff=tzOffset;
236StatusCode MdcHvDropSvc::FetchHvInfo(
Time_t timeBegin,
Time_t timeEnd) {
238 MsgStream log(messageService(), name());
239 log << MSG::INFO <<
"MdcHvDropSvc::FetchHvInfo()" << endreq;
240 char querystring[300];
241 char sTimeBegin[100], sTimeEnd[100];
242 Time_t2str(timeBegin,sTimeBegin,100);
243 Time_t2str(timeEnd,sTimeEnd,100);
246 sprintf(querystring,
"select * \
248 where date_time >= '%s' \
249 and date_time <= '%s' ",
250 m_table.c_str(),sTimeBegin, sTimeEnd);
251 log << MSG::INFO <<
"sql_request=" << querystring << endreq;
256 if (mysql_query(&m_mysql,querystring)){
257 log << MSG::ERROR <<
"Error! mysql_query failed"<<endreq;
258 return StatusCode::FAILURE;
261 m_result=mysql_store_result(&m_mysql);
263 while ((m_row = mysql_fetch_row(m_result))) {
269 sqlRow2Item(m_row,m_result,item,
time);
279 log <<MSG::INFO <<
"successfully fetched HV data from database." << endreq;
303 return StatusCode::SUCCESS;
311inline static double average(MYSQL_ROW row,
int sliceBegin,
int sliceEnd) {
314 for (
int i=sliceBegin;i<sliceEnd;i++ ){
317 return sum/(sliceEnd-sliceBegin);
332 MsgStream log(messageService(), name());
333 log << MSG::DEBUG <<
"MdcHvDropSvc::sqlRow2Item()" << endreq;
334 int nColomn = mysql_num_fields(result);
336 log << MSG::ERROR <<
"ERROR: SQL output does not meet expectation"<<endreq;
337 return StatusCode::FAILURE;
341 time=datetimeStr2Time_t(row[1]);
342 item[0]=average(row,39,44);
343 item[1]=average(row,44,50);
344 item[2]=average(row,50,56);
345 item[3]=average(row,56,62);
346 item[4]=atof(row[80]);
347 item[5]=atof(row[81]);
348 for (
int i =2; i<39;i++){
350 item[layer]=atof(row[i]);
352 return StatusCode::SUCCESS;
414 MsgStream log(messageService(), name());
416 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
419 int run = eventHeader->runNumber();
420 unsigned long time_etsT1=eventHeader->etsT1();
421 long time_etsT1_timelike32=((time_etsT1/2000000)&0x00000000ffffffffLU);
430 if(m_etsT1Offset==0){
432 time=time_etsT1_timelike32+getRunBeginTime(run);
435 time=time_etsT1_timelike32+m_etsT1Offset;
441 if (run < 0)
return 0;
443 log << MSG::INFO <<
"query time " <<
time << endreq;
448 if (m_vData.
size() == 0) {
450 Time_t2str(
time, time1str, 100);
451 log << MSG::INFO <<
"INFO: MdcHvDropSvc time " <<
time <<
" aka " << time1str <<
" is not in cache yet.\n"
452 <<
"\tfetching HV info for 1h ago" <<
time - 3600 <<
" til fetch Length" <<
time + m_fetchLength - 3600 << endreq;
453 fetchBeginTime =
time - 3600;
459 Time_t2str(
time, time1str, 100);
463 log << MSG::INFO <<
"INFO:MdcHvDropSvc time " <<
time <<
" aka " << time1str <<
" is not in cache yet.\n"
465 <<
"\tfetching HV info for dbTime " << upTimeCachedDBTime <<
" to fetch Length later" << upTimeCachedDBTime + m_fetchLength << endreq;
466 fetchBeginTime = upTimeCachedDBTime;
470 log << MSG::INFO <<
"INFO:MdcHvDropSvc time " <<
time <<
" aka " << time1str <<
" is not in cache or canbe fetched in a interval.\n"
473 log << MSG::INFO <<
"aborted cache and fetching new data.\n"
474 <<
"\tfetching HV info for 1h ago" <<
time - 3600 <<
" til fetch Length" <<
time + m_fetchLength - 3600 << endreq;
476 fetchBeginTime =
time - 3600;
481 Time_t time2 = fetchBeginTime + m_fetchLength;
482 if (!FetchHvInfo(fetchBeginTime - 600, time2) == StatusCode::SUCCESS) {
483 cout <<
"ERROR: MdcHvDropSvc failed to fetch HV info\n";
489 if (m_ignoreLayer_21_24)
496Time_t MdcHvDropSvc::getRunBeginTime(
int runid){
498 if (m_run_begin.find(runid)!=m_run_begin.end()){
499 return m_run_begin[runid];
508 if(!mysql_real_connect(&mysql,m_host2.c_str(),m_userName2.c_str(),m_password2.c_str(),m_dbName2.c_str(),m_port2,
NULL,0)){
509 std::cerr<<
"Connect to database" <<m_host<<
":"<<m_port<<
" failed\n"<<
510 "due to "<<mysql_error(&mysql)<<std::endl;
511 return m_run_begin[runid]=ret_err;
513 char querystring[300];
515 sprintf(querystring,
"SELECT run_number, startTime, endTime FROM RunParams WHERE run_number = %d",runid);
516 if (mysql_query(&mysql,querystring)){
517 std::cerr<<
"Error! mysql_query failed"<<std::endl;
518 return m_run_begin[runid]=ret_err;
521 result=mysql_store_result(&mysql);
522 if (mysql_num_rows(result) < 1){
523 std::cerr<<
"Error! mysql_query returns "<<mysql_num_rows(result)<<
" rows"<<std::endl;
524 return m_run_begin[runid]=ret_err;
526 row = mysql_fetch_row(result);
528 mysql_free_result(result);
530 std::cout<<
"got run begin time for run "<<runid<<
" is "<<
time<<std::endl;
531 return m_run_begin[runid]=
time;
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
struct st_mysql_res MYSQL_RES
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
void handle(const Incident &)
virtual StatusCode initialize()
virtual double queryRelativeHvDrop()
virtual StatusCode finalize()
bool isValid(Time_t time) const
double getAvgDrop(Time_t time)
Time_t getUpperBoundaryEventTime()
void push_back_sorted(Time_t time_HV_SLOWCTRL, const VDataItem &data)
add a entry to our cache. the entry has to be pushed back in ascending order, with no gap....
double getAvgDropButVeryDrop(Time_t time)
Time_t getLowerBoundaryEventTime()
void clear()
throw all cached data. next time you might have to prepare cache again..