/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Reconstruction/DTagAlg/DTagAlg-00-01-04/src/ReadBeamInfFromDb.cxx

Go to the documentation of this file.
00001 #include "GaudiKernel/Bootstrap.h"
00002 #include "GaudiKernel/IJobOptionsSvc.h"
00003 #include "GaudiKernel/ISvcLocator.h"
00004 #include "GaudiKernel/PropertyMgr.h"
00005 
00006 
00007 #include "DTagAlg/ReadBeamInfFromDb.h"
00008 #include <string>
00009 #include <string.h>
00010 ReadBeamInfFromDb::ReadBeamInfFromDb() :
00011   m_run(-1),
00012   m_isRunValid(false),
00013   m_beamE(0),
00014   m_beta(0.011,0,0),
00015   m_usecbE(false){
00016 }
00017 
00018 MYSQL* ReadBeamInfFromDb::OpenDb() const {
00019   
00020   const char host[]     = "bes3db2.ihep.ac.cn";
00021   const char user[]     = "guest";
00022   const char passwd[]   = "guestpass";
00023   //const char db[]       = "run";
00024   const char db[]       = "offlinedb";
00025   unsigned int port_num = 3306;
00026 
00027   MYSQL* mysql = mysql_init(NULL);
00028   mysql = mysql_real_connect(mysql, host, user, passwd, db, port_num,
00029                              NULL,  // socket
00030                              0);    // client_flag
00031 
00032   if (mysql == NULL) {
00033     fprintf(stderr, "can not open database: offlinedb\n");
00034   }
00035 
00036   return mysql;
00037 }
00038 
00039 
00040 void ReadBeamInfFromDb::CloseDb(MYSQL* mysql) const {
00041   mysql_close(mysql);
00042 }
00043 
00044 
00045 double ReadBeamInfFromDb::ReadDb(int run){
00046   
00047   m_run=run;
00048   m_isRunValid = false;
00049   
00050   //read db directly
00051   //turned off now
00052   /*
00053   MYSQL* mysql=OpenDb();
00054   
00055   char stmt[1024];
00056   snprintf(stmt, 1024,
00057            "select BER_PRB, BPR_PRB "
00058           "from RunParams where run_number = %d", run);
00059   if (mysql_real_query(mysql, stmt, strlen(stmt))) {
00060     fprintf(stderr, "query error\n");
00061     CloseDb(mysql);
00062     return m_beamE ;
00063   }
00064   
00065   
00066   MYSQL_RES* result_set = mysql_store_result(mysql);
00067   MYSQL_ROW row = mysql_fetch_row(result_set);
00068   if (!row) {
00069     fprintf(stderr, "cannot find data for RunNo %d\n", run);
00070     mysql_free_result(result_set);
00071     CloseDb(mysql);
00072     return m_beamE;
00073   }
00074   
00075 
00076 
00077   double E_E=0, E_P=0;
00078   sscanf(row[0], "%lf", &E_E);
00079   sscanf(row[1], "%lf", &E_P);
00080   
00081   m_beamE=(E_E+E_P)/2.0;
00082   
00083   // Free the memory to prevent memory leak!
00084   mysql_free_result(result_set);
00085   CloseDb(mysql);
00086   */
00087   
00088   //read db use service
00089   Gaudi::svcLocator()->service("DatabaseSvc",m_dbsvc,true);
00090   //calibrated beam Energy
00091   if(m_usecbE){
00092     char stmt1[400];
00093     snprintf(stmt1, 1024,
00094              "select beam_energy, px, py, pz "
00095              "from RunParams where run_number = %d", run);
00096     DatabaseRecordVector res;
00097     int row_no = m_dbsvc->query("offlinedb", stmt1, res);
00098     if(row_no==0){
00099       return m_beamE;
00100     }
00101     
00102     DatabaseRecord* records = res[0];
00103     double bE=0;
00104     bE = records->GetDouble("beam_energy");
00105     m_beamE=bE;
00106 
00107     double px=records->GetDouble("px");
00108     double py=records->GetDouble("py");
00109     double pz=records->GetDouble("pz");
00110 
00111     m_beta.setX(px);
00112     m_beta.setY(py);
00113     m_beta.setZ(pz);
00114     
00115     //std::cout<<"beam e is:"<<bE<<", px="<<px<<",py="<<py<<",pz="<<pz<<std::endl;
00116   }
00117   //use online beam Energy 
00118   else{
00119     char stmt1[400];
00120     snprintf(stmt1, 1024,
00121              "select BER_PRB, BPR_PRB "
00122              "from RunParams where run_number = %d", run);
00123     DatabaseRecordVector res;
00124     int row_no = m_dbsvc->query("run", stmt1, res);
00125     if(row_no==0){
00126       return m_beamE;
00127     }
00128     
00129     DatabaseRecord* records = res[0];
00130     double E_E=0, E_P=0;
00131     E_E = records->GetDouble("BER_PRB");
00132     E_P = records->GetDouble("BPR_PRB");
00133     m_beamE=(E_E+E_P)/2.0;
00134   }
00135 
00136   
00137   m_isRunValid = true;
00138   return m_beamE;
00139 }
00140 
00141 bool ReadBeamInfFromDb::isRunValid(int run) {
00142   
00143   if (run == -1 || m_run != run) {
00144     ReadDb(run);
00145     return m_isRunValid;
00146   }
00147   
00148   return false;
00149   
00150 }
00151 
00152 
00153 double ReadBeamInfFromDb::getbeamE(int run, double defaultbeamE){
00154   int absrun=fabs(run);
00155   
00156   if (!isRunValid(absrun)){
00157     return defaultbeamE;
00158     fprintf(stderr, "ERROR in ReadBeamInfFromDb: runNo is invalid!\n");
00159   }
00160   return m_beamE;
00161 }
00162 
00163 

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