BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
BeamEnergySvc.cxx
Go to the documentation of this file.
1#include "GaudiKernel/MsgStream.h"
3
4BeamEnergySvc::BeamEnergySvc(const std::string& name, ISvcLocator* svcLoc)
5: Service(name, svcLoc),
6 m_run(-1),
7 m_isRunValid(false),
8 m_beamE(0)
9{
10 //declareProperty("", m_);
11 declareProperty("Host" , m_host = std::string("bes3db2.ihep.ac.cn"));
12 declareProperty("DbName" , m_dbName = std::string("run"));
13 declareProperty("UserName" , m_userName = std::string("guest"));
14 declareProperty("Password" , m_password = std::string("guestpass"));
15 declareProperty("PortNum" , m_port_num = 3306);
16}
17
19{
20}
21
23{
24 MsgStream log(messageService(), name());
25 log << MSG::INFO << "@initialize()" << endreq;
26
27 StatusCode sc = Service::initialize();
28
29 return sc;
30}
31
33{
34 MsgStream log(messageService(), name());
35 log << MSG::INFO << "@initialize()" << endreq;
36
37 StatusCode sc = Service::finalize();
38
39 return sc;
40}
41
42StatusCode BeamEnergySvc::queryInterface(const InterfaceID& riid, void** ppvIF)
43{
44 if ( IBeamEnergySvc::interfaceID().versionMatch(riid) ) {
45 *ppvIF = dynamic_cast<IBeamEnergySvc*>(this);
46 }
47 else {
48 return Service::queryInterface(riid, ppvIF);
49 }
50 addRef();
51 return StatusCode::SUCCESS;
52}
53
54MYSQL* BeamEnergySvc::OpenDb() const {
55
56 const char* host = m_host.c_str() ;
57 const char* user = m_userName.c_str() ;
58 const char* passwd = m_password.c_str() ;
59 const char* db = m_dbName.c_str() ;
60 unsigned int port_num = m_port_num;
61
62 MYSQL* mysql = mysql_init(NULL);
63 mysql = mysql_real_connect(mysql, host, user, passwd, db, port_num,
64 NULL, // socket
65 0); // client_flag
66
67 if (mysql == NULL) {
68 fprintf(stderr, "can not open database: offlinedb\n");
69 }
70
71 return mysql;
72}
73
74
75void BeamEnergySvc::CloseDb(MYSQL* mysql) const {
76 mysql_close(mysql);
77}
78
79
80void BeamEnergySvc::ReadDb(int run ){
81 MsgStream log(messageService(), name());
82 if ( run < 0 )
83 {
84 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
85 run = -run;
86 }
87 m_run= run;
88 m_isRunValid = false;
89
90 MYSQL* mysql=OpenDb();
91
92 char stmt[1024];
93 snprintf(stmt, 1024,
94 "select BER_PRB, BPR_PRB "
95 "from RunParams where run_number = %d", run);
96 if (mysql_real_query(mysql, stmt, strlen(stmt))) {
97 fprintf(stderr, "query error\n");
98 return ;
99 }
100
101
102 MYSQL_RES* result_set = mysql_store_result(mysql);
103 MYSQL_ROW row = mysql_fetch_row(result_set);
104 if (!row) {
105 fprintf(stderr, "cannot find data for RunNo %d\n", run);
106 return ;
107 }
108
109 m_isRunValid = true;
110
111 double E_E=0, E_P=0;
112 sscanf(row[0], "%lf", &E_E);
113 sscanf(row[1], "%lf", &E_P);
114
115 m_beamE=(E_E+E_P)/2.0;
116
117 // Free the memory to prevent memory leak!
118 mysql_free_result(result_set);
119
120 CloseDb(mysql);
121}
122
124 if (m_run != run) {
125 ReadDb(run);
126 }
127 return m_isRunValid;
128}
129
130
132 if (!isRunValid(run)){
133 std::cerr << "ERROR in BeamEnergySvc: runNo is invalid!" << std::endl;
134 }
135 return m_beamE;
136}
struct st_mysql_res MYSQL_RES
struct st_mysql MYSQL
bool isRunValid(int run)
virtual StatusCode initialize()
BeamEnergySvc(const std::string &name, ISvcLocator *svcLoc)
virtual StatusCode finalize()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvIF)
double getbeamE(int run)
virtual ~BeamEnergySvc()
static const InterfaceID & interfaceID()