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"
29#include "GaudiKernel/IIncidentListener.h"
30#include "GaudiKernel/IIncidentSvc.h"
31#include "GaudiKernel/Incident.h"
33#include "EventModel/Event.h"
34#include "EventModel/EventHeader.h"
35#include "EventModel/EventModel.h"
36#include "GaudiKernel/Bootstrap.h"
37#include "GaudiKernel/ISvcLocator.h"
49 : base_class( name, svcloc ) {
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",
66 declareProperty(
"Host2", m_host2 = std::string(
"bes3db2.ihep.ac.cn" ) );
67 declareProperty(
"DbName2", m_dbName2 = std::string(
"run" ) );
68 declareProperty(
"TableName2", m_table2 = std::string(
"RunParams" ) );
69 declareProperty(
"UserName2", m_userName2 = std::string(
"guest" ) );
70 declareProperty(
"Password2", m_password2 = std::string(
"guestpass" ) );
71 declareProperty(
"Port2", m_port2 = 3306 );
84 MsgStream log(
msgSvc(), name() );
85 log << MSG::INFO <<
"MdcHvDropSvc::initialize()" << endmsg;
87 StatusCode sc = Service::initialize();
88 if ( sc.isFailure() )
return sc;
91 sc = service(
"IncidentSvc", incsvc );
93 if ( sc.isSuccess() ) { incsvc->addListener(
this,
"NewRun", priority ); }
95 sc = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true );
98 log << MSG::ERROR <<
"Unable to find EventDataSvc " << endmsg;
103 if ( sc.isFailure() )
105 log << MSG::ERROR <<
"Unable to initialize mysql " << endmsg;
109 return StatusCode::SUCCESS;
113 MsgStream log(
msgSvc(), name() );
114 log << MSG::INFO <<
"MdcHvDropSvc::finalize()" << endmsg;
116 return StatusCode::SUCCESS;
120 MsgStream log(
msgSvc(), name() );
121 log << MSG::DEBUG <<
"handle: " << inc.type() << endmsg;
159StatusCode MdcHvDropSvc::initMySql() {
167 mysql_init( &m_mysql );
169 if ( !mysql_real_connect( &m_mysql, m_host.c_str(), m_userName.c_str(), m_password.c_str(),
170 m_dbName.c_str(), m_port, NULL, 0 ) )
172 std::cerr <<
"Connect to database" << m_host <<
":" << m_port <<
" failed\n"
173 <<
"due to " << mysql_error( &m_mysql ) << std::endl;
174 return StatusCode::FAILURE;
176 return StatusCode::SUCCESS;
191size_t MdcHvDropSvc::Time_t2str(
Time_t utctime,
char* str,
int length ) {
192 const int tzOffset = 60 * 60 * 8;
193 std::time_t timeShanghai = utctime + tzOffset;
194 std::tm* tm = std::gmtime( &timeShanghai );
195 return std::strftime( str, length,
"%Y-%m-%d %H:%M:%S", tm );
206Time_t MdcHvDropSvc::datetimeStr2Time_t(
const char* str ) {
207 const int tzOffset = 60 * 60 * 8;
209 int year, month, day, hour,
min, sec;
210 std::sscanf( str,
"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &
min, &sec );
211 tm.tm_year = year - 1900;
212 tm.tm_mon = month - 1;
218 tm.tm_gmtoff = tzOffset;
230StatusCode MdcHvDropSvc::FetchHvInfo(
Time_t timeBegin,
Time_t timeEnd ) {
232 MsgStream log(
msgSvc(), name() );
233 log << MSG::INFO <<
"MdcHvDropSvc::FetchHvInfo()" << endmsg;
234 char querystring[300];
235 char sTimeBegin[100], sTimeEnd[100];
236 Time_t2str( timeBegin, sTimeBegin, 100 );
237 Time_t2str( timeEnd, sTimeEnd, 100 );
240 sprintf( querystring,
"select * \
242 where date_time >= '%s' \
243 and date_time <= '%s' ",
244 m_table.c_str(), sTimeBegin, sTimeEnd );
245 log << MSG::INFO <<
"sql_request=" << querystring << endmsg;
250 if ( mysql_query( &m_mysql, querystring ) )
252 log << MSG::ERROR <<
"Error! mysql_query failed" << endmsg;
253 return StatusCode::FAILURE;
257 m_result = mysql_store_result( &m_mysql );
259 while ( ( m_row = mysql_fetch_row( m_result ) ) )
266 sqlRow2Item( m_row, m_result, item,
time ).ignore();
267 m_vData.push_back_sorted(
time, item );
276 log << MSG::INFO <<
"successfully fetched HV data from database." << endmsg;
279 return StatusCode::SUCCESS;
287inline static double average( MYSQL_ROW row,
int sliceBegin,
int sliceEnd ) {
290 for (
int i = sliceBegin; i < sliceEnd; i++ ) { sum += atof( row[i] ); }
291 return sum / ( sliceEnd - sliceBegin );
303StatusCode MdcHvDropSvc::sqlRow2Item( MYSQL_ROW row,
MYSQL_RES* result,
VDataItem& item,
306 MsgStream log(
msgSvc(), name() );
307 log << MSG::DEBUG <<
"MdcHvDropSvc::sqlRow2Item()" << endmsg;
308 int nColomn = mysql_num_fields( result );
309 if ( nColomn != 102 )
311 log << MSG::ERROR <<
"ERROR: SQL output does not meet expectation" << endmsg;
312 return StatusCode::FAILURE;
316 time = datetimeStr2Time_t( row[1] );
317 item[0] = average( row, 39, 44 );
318 item[1] = average( row, 44, 50 );
319 item[2] = average( row, 50, 56 );
320 item[3] = average( row, 56, 62 );
321 item[4] = atof( row[80] );
322 item[5] = atof( row[81] );
323 for (
int i = 2; i < 39; i++ )
326 item[layer] = atof( row[i] );
328 return StatusCode::SUCCESS;
335 MsgStream log(
msgSvc(), name() );
337 SmartDataPtr<Event::EventHeader> eventHeader( m_eventSvc,
"/Event/EventHeader" );
340 int run = eventHeader->runNumber();
341 unsigned long time_etsT1 = eventHeader->etsT1();
342 long time_etsT1_timelike32 = ( ( time_etsT1 / 2000000 ) & 0x00000000ffffffffLU );
353 if ( m_etsT1Offset == 0 ) {
time = time_etsT1_timelike32 + getRunBeginTime( run ); }
354 else {
time = time_etsT1_timelike32 + m_etsT1Offset; }
357 if ( run < 0 )
return 0;
359 log << MSG::INFO <<
"query time " <<
time << endmsg;
360 log << MSG::INFO <<
"current boundary " << m_vData.getLowerBoundaryEventTime() <<
" : "
361 << m_vData.getUpperBoundaryEventTime() << endmsg;
363 if ( m_vData.getUpperBoundaryEventTime() <
time ||
364 time < m_vData.getLowerBoundaryEventTime() )
367 if ( m_vData.size() == 0 )
370 Time_t2str(
time, time1str, 100 );
371 log << MSG::INFO <<
"INFO: MdcHvDropSvc time " <<
time <<
" aka " << time1str
372 <<
" is not in cache yet.\n"
373 <<
"\tfetching HV info for 1h ago" <<
time - 3600 <<
" til fetch Length"
374 <<
time + m_fetchLength - 3600 << endmsg;
375 fetchBeginTime =
time - 3600;
380 Time_t upTimeCachedDBTime =
381 m_vData.getUpperBoundaryEventTime() + m_vData.getOffsetEvt2Db();
383 Time_t2str(
time, time1str, 100 );
385 if ( m_vData.getLowerBoundaryEventTime() <
time &&
386 time < m_vData.getUpperBoundaryEventTime() + m_fetchLength )
389 log << MSG::INFO <<
"INFO:MdcHvDropSvc time " <<
time <<
" aka " << time1str
390 <<
" is not in cache yet.\n"
391 <<
"\tcurrent upperBoundary is " << m_vData.getUpperBoundaryEventTime() <<
"\n"
392 <<
"\tfetching HV info for dbTime " << upTimeCachedDBTime
393 <<
" to fetch Length later" << upTimeCachedDBTime + m_fetchLength << endmsg;
394 fetchBeginTime = upTimeCachedDBTime;
399 log << MSG::INFO <<
"INFO:MdcHvDropSvc time " <<
time <<
" aka " << time1str
400 <<
" is not in cache or canbe fetched in a interval.\n"
401 <<
"\tcurrent cache boundary is " << m_vData.getLowerBoundaryEventTime() <<
":"
402 << m_vData.getUpperBoundaryEventTime() <<
".\n"
403 <<
"\tcurrent fetch interval is " << m_vData.getUpperBoundaryEventTime() <<
":"
404 << m_vData.getUpperBoundaryEventTime() + m_fetchLength << endmsg;
405 log << MSG::INFO <<
"aborted cache and fetching new data.\n"
406 <<
"\tfetching HV info for 1h ago" <<
time - 3600 <<
" til fetch Length"
407 <<
time + m_fetchLength - 3600 << endmsg;
409 fetchBeginTime =
time - 3600;
413 fetchBeginTime += m_vData.getOffsetEvt2Db();
414 Time_t time2 = fetchBeginTime + m_fetchLength;
415 if ( !FetchHvInfo( fetchBeginTime - 600, time2 ).isSuccess() )
417 cout <<
"ERROR: MdcHvDropSvc failed to fetch HV info\n";
421 if ( !m_vData.isValid(
time ) )
return -9999;
422 if ( m_ignoreLayer_21_24 )
return m_vData.getAvgDropButVeryDrop(
time );
423 else return m_vData.getAvgDrop(
time );
426Time_t MdcHvDropSvc::getRunBeginTime(
int runid ) {
428 if ( m_run_begin.find( runid ) != m_run_begin.end() ) {
return m_run_begin[runid]; }
433 mysql_init( &mysql );
436 if ( !mysql_real_connect( &mysql, m_host2.c_str(), m_userName2.c_str(), m_password2.c_str(),
437 m_dbName2.c_str(), m_port2, NULL, 0 ) )
439 std::cerr <<
"Connect to database" << m_host <<
":" << m_port <<
" failed\n"
440 <<
"due to " << mysql_error( &mysql ) << std::endl;
441 return m_run_begin[runid] = ret_err;
443 char querystring[300];
447 "SELECT run_number, startTime, endTime FROM RunParams WHERE run_number = %d",
449 if ( mysql_query( &mysql, querystring ) )
451 std::cerr <<
"Error! mysql_query failed" << std::endl;
452 return m_run_begin[runid] = ret_err;
456 result = mysql_store_result( &mysql );
457 if ( mysql_num_rows( result ) < 1 )
459 std::cerr <<
"Error! mysql_query returns " << mysql_num_rows( result ) <<
" rows"
461 return m_run_begin[runid] = ret_err;
463 row = mysql_fetch_row( result );
465 mysql_free_result( result );
466 mysql_close( &mysql );
467 std::cout <<
"got run begin time for run " << runid <<
" is " <<
time << std::endl;
468 return m_run_begin[runid] =
time;
DECLARE_COMPONENT(BesBdkRc)
struct st_mysql_res MYSQL_RES
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)
void handle(const Incident &)
virtual StatusCode initialize()
virtual double queryRelativeHvDrop()
virtual StatusCode finalize()
MdcHvDropSvc(const std::string &name, ISvcLocator *svcloc)