00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/SvcFactory.h"
00003 #include "GaudiKernel/ISvcLocator.h"
00004 #include "GaudiKernel/SmartDataPtr.h"
00005 #include "GaudiKernel/Bootstrap.h"
00006
00007 #include "EmcCalibConstSvc/EmcCalibConstSvc.h"
00008 #include "GaudiKernel/MsgStream.h"
00009 #include "GaudiKernel/SvcFactory.h"
00010 #include "GaudiKernel/ISvcLocator.h"
00011 #include "GaudiKernel/Bootstrap.h"
00012 #include "RealizationSvc/ConnectionProvider.h"
00013 #include "facilities/Util.h"
00014 #include <iostream>
00015 #include <cstdio>
00016 #include <boost/lexical_cast.hpp>
00017 #include <cstdlib>
00018
00019 using namespace std;
00020
00021 namespace RealDBUtil {
00022
00023 #define _T(str) ("\'"+str+"\'")
00024 typedef basic_string<char>::size_type S_T;
00025 static const S_T npos = -1;
00026
00027 std::vector<std::string> ConnectionProvider::split(const std::string& src, std::string delimit, std::string null_subst)
00028 {
00029 if( src.empty() || delimit.empty() ) throw "split: empty string\0";
00030 vector<string> v;
00031 S_T deli_len = delimit.size();
00032 long index = npos, last_search_position = 0;
00033 while( (index=src.find(delimit, last_search_position))!=npos )
00034 {
00035 if(index==last_search_position)
00036 v.push_back(null_subst);
00037 else
00038 v.push_back( src.substr(last_search_position, index-
00039 last_search_position) );
00040 last_search_position = index + deli_len;
00041 }
00042 string last_one = src.substr(last_search_position);
00043 v.push_back( last_one.empty()? null_subst:last_one );
00044 return v;
00045 }
00046
00047 bool ConnectionProvider::getcal(int runNo, std::string ids){
00048
00049
00050 char stmt1[1024];
00051 int run_No =std::abs(runNo);
00052 sprintf(stmt1,"select EQM_fileid,EMCGain from RunParams where run_number = %d",run_No);
00053
00054 DatabaseRecordVector results;
00055 results.clear();
00056 int rowNumber = m_dbsvc->query("run",stmt1,results);
00057 if(rowNumber <= 0){
00058 std::cout << "ERROR Read EQM_fileid from the Database, exit." << endreq;
00059 exit(1);
00060 }
00061
00062 int fileid = std::atoi((*results[0])["EQM_fileid"]);
00063
00064 string gainnum((*results[0])["EMCGain"]);
00065
00066 sprintf(stmt1, "select OID,Position,Channel1,Channel2,Channel3,Channel4,Channel5,Channel6,Channel7,Channel8,Channel9,Channel10,Channel11,Channel12,Channel13,Channel14,Channel15,Channel16,Channel17,Channel18,Channel19,Channel20,Channel21,Channel22,Channel23,Channel24,Channel25,Channel26,Channel27,Channel28,Channel29,Channel30,Channel31,Channel32 from EQM where FileId=%d", fileid);
00067
00068 results.clear();
00069 rowNumber = m_dbsvc->query("run",stmt1,results);
00070 if(rowNumber <= 0){
00071 std::cout << "ERROR Read EQM from the Database, exit." << endreq;
00072 exit(1);
00073 }
00074
00075 std::map<std::string, std::vector<std::string> > map_EQM;
00076 map_EQM.clear();
00077 for(int i=0;i<rowNumber;i++)
00078 { for(int j=2;j<34;j++)
00079 {
00080 std::vector<std::string> vec_tmp;
00081 vec_tmp.clear();
00082 vec_tmp.push_back(toString<int>(j-1));
00083 vec_tmp.push_back((*results[i])["OID"]);
00084 vec_tmp.push_back((*results[i])["Position"]);
00085 typedef pair<std::string, std::vector<std::string> > vpair;
00086 map_EQM.insert(vpair((*results[i])["Channel"+toString<int>(j-1)], vec_tmp));
00087
00088 }
00089 }
00090
00091 vector<string> id = split(ids, ",", "<null>");
00092 id_num=id.size();
00093
00094 for(unsigned int m=0;m<id.size();m++){
00095 if(id[m] == "0") { std::cout << " error: No." << m+1 << " id=0,please delete it! " << std::endl; break; }
00096 if(id[m] == "NULL") { std::cout << " end of loop! " << std::endl; break; }
00097 string channelnum,oid,modu;
00098 bool if_found = false;
00099 for(std::map<std::string,std::vector<std::string> >::iterator iter = map_EQM.begin(); iter != map_EQM.end(); iter++)
00100 {
00101 if(iter->first == id[m]) {
00102 if_found = true;
00103 channelnum = iter->second[0];
00104 oid = iter->second[1];
00105 modu = iter->second[2];
00106 }
00107 }
00108
00109 if(!if_found) { std::cout << "can not find id " << id[m] << " in EQM, exit." << std::endl; exit(1); }
00110
00111 string b="AAAA";
00112 string trgcratenum;
00113 for(int i=0;i<4;i++){
00114 b[i] =oid[i+4];
00115 }
00116
00117
00118 string a[16][2]={{"E3_1","96"},{"E2_0","97"},{"E2_1","98"},{"E1_0","99"},{"E1_1","100"},{"E3_0","101"},{"E2_2","102"},{"E3_2","103"},{"W3_1","112"},{"W2_0","113"},{"W2_1","114"},{"W1_0","115"},{"W1_1","116"},{"W3_0","117"},{"W2_2","118"},{"W3_2","119"}};
00119 string trgCrate[16]={"0x60","0x61","0x62","0x63","0x64","0x65","0x66","0x67","0x70","0x71","0x72","0x73","0x74","0x75","0x76","0x77"};
00120
00121 for(int i=0;i<16;i++){
00122 if(b.compare(a[i][0])) continue;
00123 else {
00124 trgcratenum = trgCrate[i];
00125 }
00126 }
00127
00128 std::string query_name = "select gain from EMC_Gain where run_id="+gainnum+" and ppc_id=\""+trgcratenum+"\" and Qmodule_no="+modu+" and Qchannel_no="+channelnum;
00129 results.clear();
00130 rowNumber = m_dbsvc->query("run",query_name.c_str(),results);
00131 if(rowNumber <= 0){
00132 std::cout << "ERROR Read gain from EMC_Gain Database, exit." << endreq;
00133 exit(1);
00134 }
00135
00136
00137
00138 trgGain[m] = new std::string((*results[0])["gain"]);
00139
00140 }
00141 return true;
00142 }
00143
00144 ConnectionProvider::ConnectionProvider() {
00145 StatusCode sc = Gaudi::svcLocator()->service("DatabaseSvc", m_dbsvc, true);
00146 if (sc .isFailure() ) {
00147 std::cout << "ERROR: In ConnectionProvider()--> Unable to find DatabaseSvc " << std::endl;
00148 }
00149 m_trgTpye = -1;
00150 m_tfee_fileid = -1;
00151 }
00152
00153 ConnectionProvider::~ConnectionProvider() {
00154 }
00155
00156 ConnectionProvider::eRet ConnectionProvider::getReadLumInfo(std::string& Lum, int runNo, std::string SftVer, std::string ParVer, std::string BossRelease) {
00157
00158 char stmt1[200];
00159 int run_No =std::abs(runNo);
00160
00161
00162 if(SftVer == "default") {
00163 if(BossRelease == "default") {
00164 std::cout << "ERROR BossRelease must be set! Current value is " << BossRelease << "." << endreq;
00165 exit(1);
00166 }
00167 else {
00168 sprintf(stmt1, "select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", BossRelease.c_str(), run_No, run_No);
00169
00170 DatabaseRecordVector records;
00171 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
00172 if(rowNo == 0) {
00173 std::cout << "ERROR: can not find records for run = " << run_No << " and BossRelease = " << BossRelease << endreq;
00174 exit(1);
00175 }
00176 DatabaseRecord* recordst = records[0];
00177 SftVer = recordst->GetString("SftVer");
00178 ParVer = recordst->GetString("ParVer");
00179 std::cout << "Using the new SftVer and ParVer (" << SftVer << ", " << ParVer << ") for run " << run_No << ". " << endreq;
00180 }
00181 }
00182
00183
00184 sprintf(stmt1,"select OfflineTwoGam,SftVer,ParVer from OfflineLum where RunNo = %d",run_No);
00185
00186
00187
00188
00189
00190
00191
00192
00193 DatabaseRecordVector results;
00194 results.clear();
00195 int status = m_dbsvc->query("offlinedb",stmt1,results);
00196 if(status<0){
00197 std::cout << "ERROR Read the luminosity from the Database" << endreq;
00198 exit(1);
00199 }
00200
00201 int RowNumber = results.size();
00202 if(RowNumber == 0){
00203 std::cout<<"WARNING: No Luminosity Data in this run, return 0."<<std::endl;
00204 Lum = "0";
00205 return RETOk;
00206 }
00207
00208 int ParVerID = 0;
00209 int RecordNo = 0;
00210 if(std::atoi(ParVer.c_str()) == 0) {
00211 for(int i = 0; i < RowNumber; i++) {
00212 if((std::atoi((*results[i])["ParVer"]) > ParVerID)&&((*results[i])["SftVer"]==SftVer)) { Lum = (*results[i])["OfflineTwoGam"]; ParVerID = std::atoi((*results[i])["ParVer"]); RecordNo++;}
00213 }
00214 }
00215 else {
00216 RecordNo = 0;
00217 for(int i = 0; i < RowNumber; i++) {
00218 if((std::atoi((*results[i])["ParVer"]) == std::atoi(ParVer.c_str()))&&((*results[i])["SftVer"]==SftVer)) { Lum = (*results[i])["OfflineTwoGam"]; ParVerID = std::atoi((*results[i])["ParVer"]); RecordNo++;}
00219 }
00220 }
00221
00222 if(RecordNo == 0) {
00223
00224
00225 std::cout<<"WARNING: No Luminosity infor. found by your selection criteria, please check it (SftVer, ParVer)"<<std::endl;
00226 Lum = "0";
00227 return RETOk;
00228
00229 }
00230
00231 if(RecordNo >= 2 && std::atoi(ParVer.c_str()) != 0) {
00232 std::cout<<"ERROR: More than two Lum. records found by your selection criteria, please check it (SftVer, ParVer)"<<std::endl;
00233 exit(1);
00234 }
00235
00236 return RETOk;
00237 }
00238
00239 ConnectionProvider::eRet ConnectionProvider::getLumCurvePar(std::string& runTotalTime, std::string& tau_value, int runNo, std::string SftVer, std::string ParVer, std::string BossRelease) {
00240
00241 char stmt1[200];
00242 int run_No =std::abs(runNo);
00243
00244 if(SftVer == "default") {
00245 if(BossRelease == "default") {
00246 std::cout << "ERROR BossRelease must be set! Current value is " << BossRelease << "." << endreq;
00247 exit(1);
00248 }
00249 else {
00250 sprintf(stmt1, "select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", BossRelease.c_str(), run_No, run_No);
00251
00252 DatabaseRecordVector records;
00253 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
00254 if(rowNo == 0) {
00255 std::cout << "ERROR: can not find records for run = " << run_No << " and BossRelease = " << BossRelease << endreq;
00256 exit(1);
00257 }
00258 DatabaseRecord* recordst = records[0];
00259 SftVer = recordst->GetString("SftVer");
00260 ParVer = recordst->GetString("ParVer");
00261 std::cout << "Using the new SftVer and ParVer (" << SftVer << ", " << ParVer << ") for run " << run_No << ". " << endreq;
00262 }
00263 }
00264
00265 sprintf(stmt1,"select runTime,Tau_Value,SftVer,ParVer from OfflineLum where RunNo = %d",run_No);
00266
00267 DatabaseRecordVector results;
00268 results.clear();
00269 int status = m_dbsvc->query("offlinedb",stmt1,results);
00270 if(status<0){
00271 std::cout << "ERROR Read the luminosity curve parameters from the Database" << endreq;
00272 exit(1);
00273 }
00274
00275 int RowNumber = results.size();
00276 if(RowNumber == 0){
00277 std::cout<<"ERROR: No Luminosity curve parameters in this run, aborted."<<std::endl;
00278 exit(1);
00279 }
00280
00281 int ParVerID = 0;
00282 int RecordNo = 0;
00283 if(std::atoi(ParVer.c_str()) == 0) {
00284 for(int i = 0; i < RowNumber; i++) {
00285 if((std::atoi((*results[i])["ParVer"]) > ParVerID)&&((*results[i])["SftVer"]==SftVer)) {
00286 runTotalTime = (*results[i])["runTime"];
00287 tau_value = (*results[i])["Tau_Value"];
00288 ParVerID = std::atoi((*results[i])["ParVer"]);
00289 RecordNo++;
00290 }
00291 }
00292 }
00293 else {
00294 RecordNo = 0;
00295 for(int i = 0; i < RowNumber; i++) {
00296 if((std::atoi((*results[i])["ParVer"]) == std::atoi(ParVer.c_str()))&&((*results[i])["SftVer"]==SftVer)) {
00297 runTotalTime = (*results[i])["runTime"];
00298 tau_value = (*results[i])["Tau_Value"];
00299 ParVerID = std::atoi((*results[i])["ParVer"]);
00300 RecordNo++;
00301 }
00302 }
00303 }
00304 if(RecordNo == 0) {
00305 std::cout<<"ERROR: No Luminosity curve parameters found by your selection criteria, please check it (SftVer, ParVer)"<<std::endl;
00306 exit(1);
00307 }
00308
00309 if(RecordNo >= 2 && std::atoi(ParVer.c_str()) != 0) {
00310 std::cout<<"ERROR: More than two Lum. curve parameters records found by your selection criteria, please check it (SftVer, ParVer)"<<std::endl;
00311 exit(1);
00312 }
00313
00314 return RETOk;
00315 }
00316
00317 ConnectionProvider::eRet ConnectionProvider::getReadBunchInfo(std::vector<std::string>& bunch, int runNo, std::string SftVer, std::string ParVer, std::string BossRelease) {
00318
00319 char stmt1[200];
00320 int run_No =std::abs(runNo);
00321
00322 if(SftVer == "default") {
00323 if(BossRelease == "default") {
00324 std::cout << "ERROR BossRelease must be set! Current value is " << BossRelease << "." << endreq;
00325 exit(1);
00326 }
00327 else {
00328 sprintf(stmt1, "select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", BossRelease.c_str(), run_No, run_No);
00329
00330 DatabaseRecordVector records;
00331 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
00332 if(rowNo == 0) {
00333 std::cout << "ERROR: can not find records for run = " << run_No << " and BossRelease = " << BossRelease << endreq;
00334 exit(1);
00335 }
00336 DatabaseRecord* recordst = records[0];
00337 SftVer = recordst->GetString("SftVer");
00338 ParVer = recordst->GetString("ParVer");
00339 std::cout << "Using the new SftVer and ParVer (" << SftVer << ", " << ParVer << ") for run " << run_No << ". " << endreq;
00340 }
00341 }
00342
00343 sprintf(stmt1,"select Vx,Vy,Vz,AcSigmaVx,AcSigmaVy,SigmaVz,SftVer,ParVer from BeamPar where runNo = %d",run_No);
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 DatabaseRecordVector results;
00354 results.clear();
00355 int status = m_dbsvc->query("offlinedb",stmt1,results);
00356 if(status<0){
00357 std::cout << "ERROR Read the bunch from the Database" << endreq;
00358 exit(1);
00359 }
00360
00361 int RowNumber = results.size();
00362 if(RowNumber == 0){
00363 std::cout<<"WARNING: No bunch Data in this run! Please check your selection criteria (runNo, SftVer, ParVer)"<<std::endl;
00364 return RETNoSchemaMatch;
00365 }
00366
00367 int ParVerID = 0;
00368 int RecordNo = 0;
00369 if(std::atoi(ParVer.c_str()) == 0) {
00370 for(int i = 0; i < RowNumber; i++) {
00371 if((std::atoi((*results[i])["ParVer"]) > ParVerID)&&((*results[i])["SftVer"]==SftVer)) {
00372 bunch.clear();
00373 bunch.push_back((*results[i])["Vx"]);
00374 bunch.push_back((*results[i])["Vy"]);
00375 bunch.push_back((*results[i])["Vz"]);
00376 bunch.push_back((*results[i])["AcSigmaVx"]);
00377 bunch.push_back((*results[i])["AcSigmaVy"]);
00378 bunch.push_back((*results[i])["SigmaVz"]);
00379 ParVerID = std::atoi((*results[i])["ParVer"]);
00380 RecordNo++;
00381 }
00382 }
00383 }
00384 else {
00385 RecordNo = 0;
00386 for(int i = 0; i < RowNumber; i++) {
00387 if((std::atoi((*results[i])["ParVer"]) == std::atoi(ParVer.c_str()))&&((*results[i])["SftVer"]==SftVer)) {
00388 bunch.clear();
00389 bunch.push_back((*results[i])["Vx"]);
00390 bunch.push_back((*results[i])["Vy"]);
00391 bunch.push_back((*results[i])["Vz"]);
00392 bunch.push_back((*results[i])["AcSigmaVx"]);
00393 bunch.push_back((*results[i])["AcSigmaVy"]);
00394 bunch.push_back((*results[i])["SigmaVz"]);
00395 RecordNo++;
00396 }
00397 }
00398 }
00399
00400 if(RecordNo == 0) {
00401 std::cout<<"WARNING: No bunch infor. found in this run, it will use other run's"<<std::endl;
00402 return RETNoSchemaMatch;
00403 }
00404
00405 if(RecordNo >= 2 && std::atoi(ParVer.c_str()) != 0) {
00406 std::cout<<"ERROR: More than two bunch records found by your selection criteria, please check it (SftVer, ParVer)"<<std::endl;
00407 exit(1);
00408 }
00409
00410 return RETOk;
00411 }
00412
00413 ConnectionProvider::eRet ConnectionProvider::getReadTrgTableInfo( std::vector<std::string>& trgTable, int runNo) {
00414
00415 char stmt1[200];
00416 int run_No =std::abs(runNo);
00417
00418 sprintf(stmt1,"select trgtable_id from RunParams where run_number = %d ",run_No);
00419
00420 DatabaseRecordVector results;
00421 results.clear();
00422 int status = m_dbsvc->query("run",stmt1,results);
00423 if(status<0){
00424 std::cout << "ERROR Read the trigger table from the Database" << endreq;
00425 exit(1);
00426 }
00427
00428 int RowNumber = results.size();
00429 if(RowNumber!=1){
00430 std::cout<<"ERROR:error searching Trigger Type Data in the database, check your selection criteria"<<std::endl;
00431 return RETMySQLError;
00432 }
00433 int i_trgTpye = std::atoi((*results[0])["trgtable_id"]);
00434 std::cout<<"Obj_id is "<<i_trgTpye<<std::endl;
00435 results.clear();
00436
00437 if(i_trgTpye == m_trgTpye) {
00438 std::cout<<"Don't need to update trigger table, it is same with the last run!"<<std::endl;
00439 }
00440 else {
00441 m_trgTpye = i_trgTpye;
00442 m_trgTable.clear();
00443
00444 sprintf(stmt1,"select EventChannelEnableRegister from TrgTable where Obj_id = %d ", i_trgTpye);
00445 int status1 = m_dbsvc->query("run",stmt1,results);
00446 if(status1<0){
00447 std::cout << "ERROR Read the trigger table from the Database 1" << endreq;
00448 exit(1);
00449 }
00450 RowNumber = results.size();
00451 if(RowNumber!=1){
00452 std::cout<<"ERROR:error searching Trigger Type Data in the database, check your selection criterions"<<std::endl;
00453 return RETMySQLError;
00454 }
00455 m_trgTable.push_back((*results[0])["EventChannelEnableRegister"]);
00456 results.clear();
00457
00458
00459 for(int i = 1; i < 17; i++)
00460 for(int j = 1; j < 5; j++) {
00461 if(i < 10) sprintf(stmt1,"select TrgTable_CH0%d_%d from TrgTable where Obj_id = %d ", i, j, i_trgTpye);
00462 else sprintf(stmt1,"select TrgTable_CH%d_%d from TrgTable where Obj_id = %d ", i, j, i_trgTpye);
00463 int status2 = m_dbsvc->query("run",stmt1,results);
00464 if(status2<0){
00465 std::cout << "ERROR Read the trigger table from the Database 2" << endreq;
00466 exit(1);
00467 }
00468 RowNumber = results.size();
00469 if(RowNumber!=1){
00470 std::cout<<"ERROR:error searching TrgTable Data in the database, check your selection criterions"<<std::endl;
00471 return RETMySQLError;
00472 }
00473 if(i<10) sprintf(stmt1,"TrgTable_CH0%d_%d", i, j);
00474 else sprintf(stmt1,"TrgTable_CH%d_%d", i, j);
00475 m_trgTable.push_back((*results[0])[stmt1]);
00476 results.clear();
00477 }
00478 }
00479 trgTable = m_trgTable;
00480
00481 return RETOk;
00482 }
00483
00484 ConnectionProvider::eRet ConnectionProvider::getEmcGain( std::vector<double>& emcGain, int runNo) {
00485
00486 IEmcCalibConstSvc *emcCalibConstSvc;
00487 ISvcLocator* svcLocator = Gaudi::svcLocator();
00488 StatusCode sc = svcLocator->service("EmcCalibConstSvc",emcCalibConstSvc);
00489 if(sc!=StatusCode::SUCCESS) {
00490 cout << "ConnectionProvider Error: Can't get EmcCalibConstSvc." << endl;
00491 }
00492
00493 string ids="128005";
00494
00495 ostringstream str;
00496 int idBegin=0;
00497 int idEnd=6240;
00498
00499 for(int i=idBegin;i<idEnd;i++) {
00500 unsigned int npart = emcCalibConstSvc->getPartID(i);
00501 unsigned int ntheta = emcCalibConstSvc->getThetaIndex(i);
00502 unsigned int nphi = emcCalibConstSvc->getPhiIndex(i);
00503
00504 int triPart,triTheta,triPhi;
00505 if(npart==0) {
00506 triPart=0;
00507 triTheta=23+ntheta;
00508 } else if(npart==1&&ntheta<22) {
00509 triPart=0;
00510 triTheta=22-ntheta;
00511 } else if(npart==1&&ntheta>=22) {
00512 triPart=1;
00513 triTheta=ntheta-21;
00514 } else {
00515 triPart=1;
00516 triTheta=23+ntheta;
00517 }
00518 triPhi=nphi+1;
00519
00520 ostringstream ssTheta;
00521 if(triPart==1&&triTheta<10) {
00522 ssTheta<<"0"<<triTheta;
00523 } else {
00524 ssTheta<<triTheta;
00525 }
00526
00527 ostringstream ssPhi;
00528 if(triPhi<10) {
00529 ssPhi<<"00"<<triPhi;
00530 } else if(triPhi<100) {
00531 ssPhi<<"0"<<triPhi;
00532 } else {
00533 ssPhi<<triPhi;
00534 }
00535
00536 if(triPart==0) {
00537 str<<ssTheta.str()<<ssPhi.str();
00538 } else {
00539 str<<triPart<<ssTheta.str()<<ssPhi.str();
00540 }
00541 if(i!=idEnd-1) {
00542 str<<",";
00543 }
00544
00545
00546
00547 }
00548
00549 ids=str.str();
00550
00551
00552 getcal( runNo, ids);
00553
00554 emcGain.clear();
00555 for(int i=0;i<id_num;i++) {
00556 istringstream isin(*trgGain[i]);
00557 double value;
00558 isin>>value;
00559 value = ((50./255.)*(value)/60.)*5*0.5;
00560 emcGain.push_back(value);
00561 delete trgGain[i];
00562 }
00563
00564 return RETOk;
00565 }
00566
00567
00568 ConnectionProvider::eRet ConnectionProvider::getTrgConfigInfo(int runNo) {
00569
00570 char stmt1[1024];
00571 int run_No =std::abs(runNo);
00572
00573 sprintf(stmt1,"select Trg_fileid from RunParams where run_number = %d ",run_No);
00574
00575 DatabaseRecordVector results;
00576 results.clear();
00577 int rowNumber = m_dbsvc->query("run",stmt1,results);
00578 if( rowNumber <= 0){
00579 std::cout << "ERROR: Read the Trg_fileid from RunParams, exit." << endl;
00580 exit(1);
00581 }
00582
00583 if(rowNumber > 1){
00584 std::cout<<"WARNNING: there are more than one record for Trg_fileid in run " << runNo << ", the first one will be selected."<<std::endl;
00585 }
00586 int trg_fileid = std::atoi((*results[0])["Trg_fileid"]);
00587
00588 sprintf(stmt1,"select EtotDataSteps,VthBEtotH,VthEEtotH,VthEtotL,VthEtotM,VthBLZ,VthDiffB,VthDiffE,VthBalBLK,VthBalEEMC,VthDiffMin from Trg_EACC where FileId = %d ", trg_fileid);
00589
00590 results.clear();
00591 rowNumber = m_dbsvc->query("run",stmt1,results);
00592 if( rowNumber <= 0){
00593 std::cout << "ERROR: Read the EACC config infor. from Trg_EACC, exit." << endl;
00594 exit(1);
00595 }
00596
00597 if(rowNumber > 1){
00598 std::cout<<"WARNNING: there are more than one record for EACC in run " << runNo << ", the first one will be selected."<<std::endl;
00599 }
00600
00601 m_EtotDataSteps = std::atoi((*results[0])["EtotDataSteps"]);
00602 m_VthBEtotH = std::atoi((*results[0])["VthBEtotH"]);
00603 m_VthEEtotH = std::atoi((*results[0])["VthEEtotH"]);
00604 m_VthEtotL = std::atoi((*results[0])["VthEtotL"]);
00605 m_VthEtotM = std::atoi((*results[0])["VthEtotM"]);
00606 m_VthBLZ = std::atoi((*results[0])["VthBLZ"]);
00607 m_VthDiffB = std::atoi((*results[0])["VthDiffB"]);
00608 m_VthDiffE = std::atoi((*results[0])["VthDiffE"]);
00609 m_VthBalBLK = std::atoi((*results[0])["VthBalBLK"]);
00610 m_VthBalEEMC = std::atoi((*results[0])["VthBalEEMC"]);
00611 m_VthDiffMin = std::atoi((*results[0])["VthDiffMin"]);
00612
00613 return RETOk;
00614 }
00615
00616
00617
00618 ConnectionProvider::eRet ConnectionProvider::getReadBackgroundInfo(std::vector<std::string>& fileInfor, int runNo) {
00619
00620 char stmt1[200];
00621 int run_No =std::abs(runNo);
00622
00623
00624 sprintf(stmt1,"select FilePath,FileName from RanTrgData where RunNo = %d ",run_No);
00625
00626 DatabaseRecordVector results;
00627 results.clear();
00628 int status = m_dbsvc->query("offlinedb",stmt1,results);
00629 if(status<0){
00630 std::cout << "ERROR Read the background file from the Database" << endreq;
00631 exit(1);
00632 }
00633
00634 int RowNumber = results.size();
00635 if(RowNumber == 0){
00636 std::cout<<"WARNING: No Bg infor. in this run!"<<std::endl;
00637 return RETNoSchemaMatch;
00638 }
00639
00640 fileInfor.clear();
00641 for(int i = 0; i < RowNumber; i++) {
00642
00643
00644
00645
00646 fileInfor.push_back((*results[i])["FilePath"]);
00647 fileInfor.push_back((*results[i])["FileName"]);
00648
00649 }
00650
00651 if(fileInfor.size() == 0) {
00652 std::cout<<"WARNING: the total bg event number in run " << runNo << " is zero, it will use other run's bg" << std::endl;
00653 return RETNoSchemaMatch;
00654 }
00655
00656 return RETOk;
00657 }
00658
00659
00660 ConnectionProvider::eRet ConnectionProvider::getReadBackgroundInfo( std::vector<std::string>& fileInfor, std::string stmt1) {
00661
00662
00663
00664
00665
00666
00667 DatabaseRecordVector results;
00668 results.clear();
00669 int status = m_dbsvc->query("offlinedb",stmt1,results);
00670 if(status<0){
00671 std::cout << "ERROR Read the background file from the Database" << endreq;
00672 exit(1);
00673 }
00674 int RowNumber = results.size();
00675 if(RowNumber == 0){
00676 std::cout<<"ERROR:error searching background Data in the database, check your selection criterions"<<std::endl;
00677 return RETMySQLError;
00678 }
00679
00680 fileInfor.clear();
00681 for(int i = 0; i < RowNumber; i++) {
00682
00683
00684
00685
00686 fileInfor.push_back((*results[i])["FilePath"]);
00687 fileInfor.push_back((*results[i])["FileName"]);
00688
00689 }
00690
00691 if(fileInfor.size() == 0) {
00692 std::cout<<"WARNING: the total bg event number in your selected bg files is zero, it will abort after error message" << std::endl;
00693 return RETNoSchemaMatch;
00694 }
00695
00696 return RETOk;
00697 }
00698
00699 ConnectionProvider::eRet ConnectionProvider::getReadTofThreshInfo( std::vector<std::string>& tofThresh, int runNo) {
00700
00701 char stmt1[200];
00702 int run_No =std::abs(runNo);
00703
00704 sprintf(stmt1,"select TFEE_fileid from RunParams where run_number = %d ",run_No);
00705
00706 DatabaseRecordVector results;
00707 results.clear();
00708 int status = m_dbsvc->query("run",stmt1,results);
00709 if(status<0){
00710 std::cout << "ERROR Read the Tof threshold from the Database" << endreq;
00711 exit(1);
00712 }
00713 int RowNumber = results.size();
00714 if(RowNumber!=1){
00715 std::cout<<"ERROR:error searching TFEE_fileid Data in the database, check your selection criterions"<<std::endl;
00716 return RETMySQLError;
00717 }
00718 int i_tfee_fileid = std::atoi((*results[0])["TFEE_fileid"]);
00719 std::cout<<"TFEE file id is "<<i_tfee_fileid<<std::endl;
00720 results.clear();
00721
00722 if(i_tfee_fileid == m_tfee_fileid) {
00723 std::cout<<"Don't need to update TOF FEE threshold, it is same with the last run!"<<std::endl;
00724 }
00725 else {
00726 m_tfee_fileid = i_tfee_fileid;
00727 m_tofThresh.clear();
00728
00729 sprintf(stmt1,"select PhysAddress,BarrelEnd,HighThreshold,LowThreshold from TFEE where FileId = %d ",i_tfee_fileid);
00730 int status1 = m_dbsvc->query("run",stmt1,results);
00731 if(status1<0){
00732 std::cout << "ERROR Read the Tof threshold from the Database 1" << endreq;
00733 exit(1);
00734 }
00735 int RowNumber = results.size();
00736 if(RowNumber == 0){
00737 std::cout<<"WARNING: No TFEE infor. in this run!"<<std::endl;
00738 return RETNoSchemaMatch;
00739 }
00740 for(int i = 0; i < RowNumber; i++) {
00741 m_tofThresh.push_back((*results[i])["PhysAddress"]);
00742 m_tofThresh.push_back((*results[i])["BarrelEnd"]);
00743 m_tofThresh.push_back((*results[i])["HighThreshold"]);
00744 m_tofThresh.push_back((*results[i])["LowThreshold"]);
00745 }
00746 results.clear();
00747 }
00748
00749 tofThresh = m_tofThresh;
00750
00751 return RETOk;
00752 }
00753
00754 ConnectionProvider::eRet ConnectionProvider::getRunInfo(std::vector<std::string>& runInfo, int runNo) {
00755
00756 char stmt1[500];
00757 int run_No =std::abs(runNo);
00758
00759 sprintf(stmt1,"select R_TM_BPR_PatNum,R_TM_BER_PatNum,S_R3O_BI_DCCT_current,E_R3O_BI_DCCT_current,AV_R3O_BI_DCCT_current,S_R4O_BI_DCCT_current,E_R4O_BI_DCCT_current,AV_R4O_BI_DCCT_current,S_BPR_LIFE,E_BPR_LIFE,AV_BPR_LIFE,S_BER_LIFE,E_BER_LIFE,AV_BER_LIFE from RunParams where run_number = %d ",run_No);
00760
00761 DatabaseRecordVector results;
00762 results.clear();
00763 int status = m_dbsvc->query("run",stmt1,results);
00764 if(status<0){
00765 std::cout << "ERROR Read the run information from the Database" << endreq;
00766 exit(1);
00767 }
00768 int RowNumber = results.size();
00769 if(RowNumber == 0){
00770 std::cout<<"WARNING: No run information in this run!"<<std::endl;
00771 return RETNoSchemaMatch;
00772 }
00773
00774 for(int i = 0; i < RowNumber; i++) {
00775 for(std::map<std::string,char*>::iterator iter = (*results[i]).begin(); iter != (*results[i]).end(); iter++) {
00776 runInfo.push_back(iter->second);
00777 }
00778 }
00779
00780 return RETOk;
00781 }
00782
00783 }