BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
Emc/EmcCalib/EmcBhaCalib/EmcBhaCalib-00-00-26/src/ReadBeamInfFromDb.cxx
Go to the documentation of this file.
2#include <string.h>
3
5 m_run(-1),
6 m_isRunValid(false),
7 m_beamE(0){
8 }
9
10MYSQL* ReadBeamInfFromDb::OpenDb() const {
11
12 const char host[] = "202.122.37.70";
13 const char user[] = "guest";
14 const char passwd[] = "guestpass";
15 const char db[] = "run";
16 unsigned int port_num = 3306;
17
18 MYSQL* mysql = mysql_init(NULL);
19 mysql = mysql_real_connect(mysql, host, user, passwd, db, port_num,
20 NULL, // socket
21 0); // client_flag
22
23 if (mysql == NULL) {
24 fprintf(stderr, "can not open database: offlinedb\n");
25 }
26
27 return mysql;
28}
29
30
31void ReadBeamInfFromDb::CloseDb(MYSQL* mysql) const {
32 mysql_close(mysql);
33}
34
35
36void ReadBeamInfFromDb::ReadDb(int run){
37
38 m_run=run;
39 m_isRunValid = false;
40
41 MYSQL* mysql=OpenDb();
42
43 char stmt[1024];
44 snprintf(stmt, 1024,
45 "select BER_PRB, BPR_PRB "
46 "from RunParams where run_number = %d", run);
47 if (mysql_real_query(mysql, stmt, strlen(stmt))) {
48 fprintf(stderr, "query error\n");
49 return ;
50 }
51
52
53 MYSQL_RES* result_set = mysql_store_result(mysql);
54 MYSQL_ROW row = mysql_fetch_row(result_set);
55 if (!row) {
56 fprintf(stderr, "cannot find data for RunNo %d\n", run);
57 return ;
58 }
59
60 m_isRunValid = true;
61
62 double E_E=0, E_P=0;
63 sscanf(row[0], "%lf", &E_E);
64 sscanf(row[1], "%lf", &E_P);
65
66 m_beamE=(E_E+E_P)/2.0;
67
68 // Free the memory to prevent memory leak!
69 mysql_free_result(result_set);
70
71 CloseDb(mysql);
72}
73
75 if (run == -1 || m_run != run) {
76 ReadDb(run);
77 }
78
79 return m_isRunValid;
80}
81
82
83double ReadBeamInfFromDb::getbeamE(int run){
84 if (!isRunValid(run)){
85 fprintf(stderr, "ERROR in ReadBeamInfFromDb: runNo is invalid!\n");
86 }
87 return m_beamE;
88}
89
90
struct st_mysql_res MYSQL_RES
struct st_mysql MYSQL