00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/SmartDataPtr.h"
00003
00004 #include "DatabaseSvc/TestDbAlg.h"
00005 #include "DatabaseSvc/IDatabaseSvc.h"
00006
00007 #include "TTree.h"
00008 #include "TBufferFile.h"
00009 #include <iostream>
00010
00011 using namespace std;
00012
00014
00015 TestDbAlg::TestDbAlg(const std::string& name, ISvcLocator* pSvcLocator) : Algorithm(name, pSvcLocator){
00016 }
00017
00018
00019
00020 StatusCode TestDbAlg::initialize(){
00021 MsgStream log(messageService(), name());
00022
00023 IDatabaseSvc* m_dbsvc;
00024 StatusCode sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
00025 if (sc .isFailure() ) {
00026 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
00027 return sc;
00028 }
00029
00030 std::cout << "*************************************************************" << std::endl;
00031 std::cout << "Test 1: Numbers" << std::endl;
00032 std::cout << "*************************************************************" << std::endl;
00033
00034 char stmt[255];
00035 sprintf(stmt,"select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz,RunNo from BeamPar where RunNo > 9950 and RunNo < 9980 and SftVer='6.5.1'");
00036
00037 DatabaseRecordVector res;
00038 int row_no;
00039 row_no = m_dbsvc->query("offlinedb",stmt,res);
00040 if(row_no<0){
00041 std::cerr << "Query \"" << stmt << "\" failed" << std::endl;
00042 return StatusCode::FAILURE;
00043 }
00044
00045 int row;
00046 double vx, svx;
00047 vx = 0;
00048 svx = 0;
00049
00050 for (row=0;row<row_no;row++){
00051 DatabaseRecord& records = *res[row];
00052 sscanf(records["Vx"], "%lf", &vx);
00053 sscanf(records["SigmaVx"], "%lf", &svx);
00054 cout << "Read from DB: RunNo " << records["RunNo"] << " Vx= " << vx << " SigmaVx= " << svx << endl;
00055 cout << " " << records.GetLong("RunNo") << " Vx= " << records.GetDouble("Vx") << " SigmaVx= " << records.GetDouble("SigmaVx") << endl;
00056 }
00057
00058
00059 std::cout << "*************************************************************" << std::endl;
00060 std::cout << "Test 2: BLOBs" << std::endl;
00061 std::cout << "*************************************************************" << std::endl;
00062
00063 res.clear();
00064 sprintf(stmt,"select EndTofPar,BarTofPar from TofCalConst where RunFrom <= 11000 and RunTo >= 11000 and SftVer='6.5.1'");
00065 row_no = m_dbsvc->query("offlinedb", stmt, res);
00066 if(row_no<0){
00067 std::cerr << "Query \"" << stmt << "\" failed" << std::endl;
00068 return StatusCode::FAILURE;
00069 }
00070 for (row=0;row<row_no;row++)
00071 {
00072 DatabaseRecord& records = *res[row];
00073
00074 TBufferFile *buf1 = new TBufferFile(TBuffer::kRead);
00075 buf1->SetBuffer(records["EndTofPar"],32768,kFALSE);
00076 TTree* curvetree = new TTree();
00077 curvetree->Streamer(*buf1);
00078 double cnvAtten[8];
00079 curvetree -> SetBranchAddress("Atten0", &cnvAtten[0]);
00080 curvetree -> SetBranchAddress("Atten1", &cnvAtten[1]);
00081 curvetree -> SetBranchAddress("Atten2", &cnvAtten[2]);
00082 curvetree -> SetBranchAddress("Atten3", &cnvAtten[3]);
00083 curvetree -> SetBranchAddress("Atten4", &cnvAtten[4]);
00084 int entries=curvetree->GetEntries();
00085 if(entries>10) entries = 10;
00086 for(int iiii=0; iiii<entries;iiii++)
00087 {
00088 curvetree->GetEntry(iiii);
00089 for(int jjj=0;jjj<5;jjj++){
00090 std::cout<<"cnvAtten["<<jjj<<"]="<<cnvAtten[jjj]<<" ";
00091 }
00092 std::cout<<std::endl;
00093 }
00094 }
00095
00096
00097
00098 std::cout << "*************************************************************" << std::endl;
00099 std::cout << "Test 3: Strings" << std::endl;
00100 std::cout << "*************************************************************" << std::endl;
00101
00102 res.clear();
00103 sprintf(stmt,"select XtTree,QtTree,T0Tree,SdTree,RunFrom,RunTo,CalParVer,FileName from MdcCalConst where SftVer = '6.5.3'");
00104 row_no = m_dbsvc->query("offlinedb",stmt,res);
00105 if(row_no<0){
00106 std::cerr << "Query \"" << stmt << "\" failed" << std::endl;
00107 return StatusCode::FAILURE;
00108 }
00109
00110 for (row=0;row<row_no;row++){
00111 DatabaseRecord& records = *res[row];
00112 cout << "Read from DB: Runs " << records["RunFrom"] << " " << records["RunTo"] << " FileName = " << records["FileName"] << endl;
00113 }
00114
00115
00116 return StatusCode::SUCCESS;
00117 }
00118
00119
00120 StatusCode TestDbAlg::execute() {
00121 MsgStream log(msgSvc(), name());
00122
00123 log << "TestDbAlg execute()" << endl;
00124
00125 return StatusCode::SUCCESS;
00126 }
00127
00128
00129
00130 StatusCode TestDbAlg::finalize() {
00131 return StatusCode::SUCCESS;
00132 }