/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/DQA/DQADtagAlg/DQADtagAlg-00-00-13/src/util/ReadBeamInfFromDb.cxx

Go to the documentation of this file.
00001 #include "DQADtagAlg/util/ReadBeamInfFromDb.h"
00002 #include <string.h>
00003 #include <cstdio>
00004 
00005 ReadBeamInfFromDb::ReadBeamInfFromDb() :
00006   m_run(-1),
00007   m_isRunValid(false),
00008   m_beamE(0){
00009   }
00010 
00011 MYSQL* ReadBeamInfFromDb::OpenDb() const {
00012   
00013   const char host[]     = "bes3db2.ihep.ac.cn";
00014   const char user[]     = "guest";
00015   const char passwd[]   = "guestpass";
00016   const char db[]       = "run";
00017   unsigned int port_num = 3306;
00018 
00019   MYSQL* mysql = mysql_init(NULL);
00020   mysql = mysql_real_connect(mysql, host, user, passwd, db, port_num,
00021                              NULL,  // socket
00022                              0);    // client_flag
00023 
00024   if (mysql == NULL) {
00025     fprintf(stderr, "can not open database: offlinedb\n");
00026   }
00027 
00028   return mysql;
00029 }
00030 
00031 
00032 void ReadBeamInfFromDb::CloseDb(MYSQL* mysql) const {
00033   mysql_close(mysql);
00034 }
00035 
00036 
00037 void ReadBeamInfFromDb::ReadDb(int run){
00038   
00039   m_run=run;
00040   m_isRunValid = false;
00041   
00042   MYSQL* mysql=OpenDb();
00043   
00044   char stmt[1024];
00045   snprintf(stmt, 1024,
00046            "select BER_PRB, BPR_PRB "
00047           "from RunParams where run_number = %d", run);
00048   if (mysql_real_query(mysql, stmt, strlen(stmt))) {
00049     fprintf(stderr, "query error\n");
00050     return ;
00051   }
00052   
00053   
00054   MYSQL_RES* result_set = mysql_store_result(mysql);
00055   MYSQL_ROW row = mysql_fetch_row(result_set);
00056   if (!row) {
00057     fprintf(stderr, "cannot find data for RunNo %d\n", run);
00058     return ;
00059   }
00060   
00061   m_isRunValid = true;
00062 
00063   double E_E=0, E_P=0;
00064   sscanf(row[0], "%lf", &E_E);
00065   sscanf(row[1], "%lf", &E_P);
00066   
00067   m_beamE=(E_E+E_P)/2.0;
00068   
00069   // Free the memory to prevent memory leak!
00070   mysql_free_result(result_set);
00071   
00072   CloseDb(mysql);
00073 }
00074 
00075 bool ReadBeamInfFromDb::isRunValid(int run) {
00076   if (run == -1 || m_run != run) {
00077     ReadDb(run);
00078   }
00079 
00080   return m_isRunValid;
00081 }
00082 
00083 
00084 double ReadBeamInfFromDb::getbeamE(int run){
00085   if (!isRunValid(run)){
00086     fprintf(stderr, "ERROR in ReadBeamInfFromDb: runNo is invalid!\n");
00087   }
00088   return m_beamE;
00089 }
00090 
00091 

Generated on Tue Nov 29 22:58:06 2016 for BOSS_7.0.2 by  doxygen 1.4.7