#include <AEMDBInterface.h>
Public Member Functions | |
| AEMDBInterface (std::string const Host) | |
| ~AEMDBInterface () | |
| int | GetRunNumber () |
| Returns the run number of the run in progress or if no run is currently running, returns the number of the run that has finished. | |
| int | GetnSubrun (int const fRun) |
| Returns the number of subruns for run fRun. | |
| int | GetnSpill (int const fRun) |
| Returns the number of spills with beam for run fRun. | |
| int | GetnEvent (int const fRun) |
| Returns the number of events with beam for run fRun. | |
| DBRunEntry | GetRunEntry (int const fRun) |
| Returns run information for run fRun. | |
| DBRunEntries | GetRunEntry (int const fRunMin, int const fRunMax) |
| Returns run information for a range of runs. | |
| long | GetRunEntry (std::string const fDateStart, DBRunEntries &fDBRuns) |
| Returns run information for one week of runs starting at fDateString. | |
| void | GetRunEntry (DBRunEntries &fDBRuns) |
| Returns run information for total run. | |
Private Member Functions | |
| void | Open () |
| Opens connection to database, throws an exception if errors occurred when doing so. | |
| PGresult * | ExecQuery (const char *query) |
| Wrap doing queries so as to clean up code a little Throws a const char* exception if query didn't succeed. | |
| void | ExecCommand (const char *command) |
| Wrap execution of SQL commands (not queries!) so as to clean up code a little, throws a const char* exception if command didn't succeed. | |
| void | PutQueryToVector (PGresult *res, DBRunEntries &vec) |
| copy run information from query result | |
Private Attributes | |
| PGconn * | fDBConn |
| std::string | fDBHost |
Definition at line 61 of file AEMDBInterface.h.
| AEMDBInterface::AEMDBInterface | ( | std::string const | Host | ) |
| AEMDBInterface::~AEMDBInterface | ( | ) |
Definition at line 85 of file AEMDBInterface.cxx.
References fDBConn.
00086 { 00087 PQfinish(fDBConn); 00088 }
| void AEMDBInterface::ExecCommand | ( | const char * | command | ) | [private] |
Wrap execution of SQL commands (not queries!) so as to clean up code a little, throws a const char* exception if command didn't succeed.
Definition at line 389 of file AEMDBInterface.cxx.
References fDBConn.
00390 { 00391 PGresult* res = PQexec(fDBConn, command); 00392 if (PQresultStatus(res) != PGRES_COMMAND_OK) { 00393 std::ostringstream str; 00394 str << "AEMDBInterface: couldn't execute command [" 00395 << command << "]: " << PQresultErrorMessage(res); 00396 PQclear(res); 00397 throw(str.str().c_str()); 00398 } 00399 PQclear(res); 00400 }
| PGresult * AEMDBInterface::ExecQuery | ( | const char * | query | ) | [private] |
Wrap doing queries so as to clean up code a little Throws a const char* exception if query didn't succeed.
User has to clean up the returned query by calling PQclear(res);
Definition at line 370 of file AEMDBInterface.cxx.
References fDBConn.
Referenced by GetnEvent(), GetnSpill(), GetnSubrun(), GetRunEntry(), and GetRunNumber().
00371 { 00372 PGresult* res = PQexec(fDBConn, query); 00373 if (PQresultStatus(res) != PGRES_TUPLES_OK) { 00374 std::ostringstream str; 00375 str << "AEMDBInterface: couldn't execute query [" 00376 << query << "]: " << PQresultErrorMessage(res); 00377 PQclear(res); 00378 throw(str.str().c_str()); 00379 } 00380 return res; 00381 }
| int AEMDBInterface::GetnEvent | ( | int const | fRun | ) |
Returns the number of events with beam for run fRun.
Definition at line 187 of file AEMDBInterface.cxx.
References CheckDBConn, and ExecQuery().
00188 { 00189 CheckDBConn(); 00190 00191 std::ostringstream str; 00192 00193 str << "SELECT trigcount[1]+trigcount[5]+trigcount[6]+trigcount[7]+trigcount[8]+trigcount[9]+trigcount[10]+trigcount[11]+trigcount[12] FROM trigstat WHERE run=" << fRun; 00194 00195 PGresult* res = 00196 ExecQuery(str.str().c_str()); 00197 00198 int nevt; 00199 if (PQntuples(res) == 0) { 00200 nevt = 0; 00201 } 00202 else if (PQntuples(res) == 1) { 00203 nevt = atoi(PQgetvalue(res, 0, 0)); 00204 } 00205 else { 00206 throw("Invalid number of rows in Runs table"); 00207 } 00208 00209 PQclear(res); 00210 00211 return nevt; 00212 }
| int AEMDBInterface::GetnSpill | ( | int const | fRun | ) |
Returns the number of spills with beam for run fRun.
Definition at line 156 of file AEMDBInterface.cxx.
References CheckDBConn, and ExecQuery().
00157 { 00158 CheckDBConn(); 00159 00160 std::ostringstream str; 00161 00162 str << "SELECT nspill-nemptyspill FROM trigstat WHERE run=" << fRun; 00163 00164 PGresult* res = 00165 ExecQuery(str.str().c_str()); 00166 00167 int nspill; 00168 if (PQntuples(res) == 0) { 00169 nspill = 0; 00170 } 00171 else if (PQntuples(res) == 1) { 00172 nspill = atoi(PQgetvalue(res, 0, 0)); 00173 } 00174 else { 00175 throw("Invalid number of rows in Runs table"); 00176 } 00177 00178 PQclear(res); 00179 00180 return nspill; 00181 }
| int AEMDBInterface::GetnSubrun | ( | int const | fRun | ) |
Returns the number of subruns for run fRun.
Definition at line 131 of file AEMDBInterface.cxx.
References CheckDBConn, and ExecQuery().
00132 { 00133 CheckDBConn(); 00134 00135 std::ostringstream str; 00136 00137 str << "SELECT nsubrun FROM runs WHERE run=" << fRun; 00138 00139 PGresult* res = 00140 ExecQuery(str.str().c_str()); 00141 00142 if (PQntuples(res) != 1) { 00143 throw("Invalid number of rows in Runs table"); 00144 } 00145 int nsubrun = atoi(PQgetvalue(res, 0, 0)); 00146 00147 PQclear(res); 00148 00149 return nsubrun; 00150 }
| void AEMDBInterface::GetRunEntry | ( | DBRunEntries & | fDBRuns | ) |
Returns run information for total run.
Definition at line 292 of file AEMDBInterface.cxx.
References CheckDBConn, ExecQuery(), and PutQueryToVector().
00293 { 00294 CheckDBConn(); 00295 00296 std::ostringstream str; 00297 00298 str << "SELECT nsubrun,nevent,target,momentum,datestart,dateend,DATE_PART('epoch',datestart),DATE_PART('epoch',dateend),nspill,nemptyspill,goodrun,trigcount,runs.run FROM runs,trigstat WHERE trigstat.run=runs.run ORDER BY runs.run"; 00299 00300 PGresult* res = ExecQuery(str.str().c_str()); 00301 00302 PutQueryToVector(res,fDBRuns); 00303 00304 PQclear(res); 00305 00306 return; 00307 }
| long AEMDBInterface::GetRunEntry | ( | std::string const | fDateStart, | |
| DBRunEntries & | fDBRuns | |||
| ) |
Returns run information for one week of runs starting at fDateString.
Definition at line 261 of file AEMDBInterface.cxx.
References CheckDBConn, ExecQuery(), and PutQueryToVector().
00262 { 00263 CheckDBConn(); 00264 00265 std::ostringstream st; 00266 00267 st << "SELECT DATE_PART('epoch','"<<fDateStart<<"'::date)"; 00268 00269 PGresult* r = ExecQuery(st.str().c_str()); 00270 00271 long epoch_start = atoi(PQgetvalue(r, 0, 0)); 00272 long epoch_end = epoch_start+(7*24*60*60); 00273 00274 PQclear(r); 00275 00276 std::ostringstream str; 00277 00278 str << "SELECT nsubrun,nevent,target,momentum,datestart,dateend,DATE_PART('epoch',datestart),DATE_PART('epoch',dateend),nspill,nemptyspill,goodrun,trigcount,runs.run FROM runs,trigstat WHERE trigstat.run=runs.run and DATE_PART('epoch',runs.dateend)>=" << epoch_start << " and DATE_PART('epoch',runs.datestart)<=" << epoch_end << "ORDER BY runs.run"; 00279 00280 PGresult* res = ExecQuery(str.str().c_str()); 00281 00282 PutQueryToVector(res,fDBRuns); 00283 00284 PQclear(res); 00285 00286 return epoch_start; 00287 }
| DBRunEntries AEMDBInterface::GetRunEntry | ( | int const | fRunMin, | |
| int const | fRunMax | |||
| ) |
Returns run information for a range of runs.
Definition at line 233 of file AEMDBInterface.cxx.
References CheckDBConn, ExecQuery(), and PutQueryToVector().
00234 { 00235 CheckDBConn(); 00236 00237 std::ostringstream str; 00238 00239 str << "SELECT nsubrun,nevent,target,momentum,datestart,dateend,DATE_PART('epoch',datestart),DATE_PART('epoch',dateend),nspill,nemptyspill,goodrun,trigcount,runs.run FROM runs,trigstat WHERE trigstat.run=runs.run and runs.run>=" << fRunMin << " and runs.run<=" << fRunMax << "ORDER BY runs.run"; 00240 00241 PGresult* res = ExecQuery(str.str().c_str()); 00242 00243 std::vector<DBRunEntry> fDBRuns; 00244 00245 if (fRunMax-fRunMin != PQntuples(res)+1) { 00246 std::cout << "Warning: "<<fRunMax-fRunMin-PQntuples(res)+1<< 00247 " runs in this range have no DB entry!!!"<<std::endl; 00248 } 00249 00250 PutQueryToVector(res,fDBRuns); 00251 00252 PQclear(res); 00253 00254 return fDBRuns; 00255 }
| DBRunEntry AEMDBInterface::GetRunEntry | ( | int const | fRun | ) |
Returns run information for run fRun.
Definition at line 218 of file AEMDBInterface.cxx.
Referenced by main().
00219 { 00220 DBRunEntries fDBREs = GetRunEntry(fRun, fRun); 00221 DBRunEntry fDBRE; 00222 00223 if (fDBREs.size() != 0) { 00224 fDBRE = fDBREs[0]; 00225 } 00226 return fDBRE; 00227 }
| int AEMDBInterface::GetRunNumber | ( | ) |
Returns the run number of the run in progress or if no run is currently running, returns the number of the run that has finished.
Definition at line 110 of file AEMDBInterface.cxx.
References CheckDBConn, ExecQuery(), and run.
00111 { 00112 CheckDBConn(); 00113 00114 PGresult* res = 00115 ExecQuery("SELECT run,status FROM runs ORDER BY run DESC LIMIT 1"); 00116 00117 if (PQntuples(res) != 1) { 00118 throw("Invalid number of rows in Runs table"); 00119 } 00120 int run = atoi(PQgetvalue(res, 0, 0)); 00121 00122 PQclear(res); 00123 00124 return run; 00125 }
| void AEMDBInterface::Open | ( | ) | [private] |
Opens connection to database, throws an exception if errors occurred when doing so.
Definition at line 94 of file AEMDBInterface.cxx.
References CheckDBConn, fDBConn, and fDBHost.
Referenced by AEMDBInterface().
00095 { 00096 std::ostringstream str; 00097 00098 str << "dbname=runs host=" << fDBHost << " user=mippdbread"; 00099 00100 fDBConn = PQconnectdb(str.str().c_str()); 00101 00102 CheckDBConn(); 00103 }
| void AEMDBInterface::PutQueryToVector | ( | PGresult * | res, | |
| DBRunEntries & | vec | |||
| ) | [private] |
copy run information from query result
Definition at line 313 of file AEMDBInterface.cxx.
Referenced by GetRunEntry().
00314 { 00315 for (int i=0; i!=PQntuples(res); ++i) { 00316 DBRunEntry fDBRE; 00317 fDBRE.run = atoi(PQgetvalue(res, i, 12)); 00318 fDBRE.nsubrun = atoi(PQgetvalue(res, i, 0)); 00319 fDBRE.nevent = atoi(PQgetvalue(res, i, 1)); 00320 fDBRE.target = std::string(PQgetvalue(res, i, 2)); 00321 fDBRE.momentum = atoi(PQgetvalue(res, i, 3)); 00322 fDBRE.datestart = std::string(PQgetvalue(res, i, 4)); 00323 fDBRE.dateend = std::string(PQgetvalue(res, i, 5)); 00324 fDBRE.epochstart = atoi(PQgetvalue(res, i, 6)); 00325 fDBRE.epochend = atoi(PQgetvalue(res, i, 7)); 00326 fDBRE.nspill = atoi(PQgetvalue(res, i, 8)); 00327 fDBRE.nemptyspill= atoi(PQgetvalue(res, i, 9)); 00328 fDBRE.goodrun = atoi(PQgetvalue(res, i, 10)); 00329 std::string trigstr(PQgetvalue(res, i, 11)); 00330 int elem = 0; // Element number 00331 std::string::size_type pos = trigstr.find("{"); 00332 // If we couldn't find the bracket, then it's not a "correct" 00333 // representation of array 00334 ++pos; 00335 while (pos != std::string::npos) { 00336 std::string::size_type endPos = trigstr.find_first_of(",}", pos); 00337 if (endPos == std::string::npos) break; 00338 fDBRE.trigcount[elem++] = atoi(trigstr.substr(pos, endPos - pos).c_str()); 00339 pos = endPos + 1; 00340 } 00341 00342 fDBRE.fRunTime = fDBRE.epochend-fDBRE.epochstart; 00343 if (fDBRE.run <=14141) { 00344 fDBRE.fSpillLength = .6; 00345 } 00346 else { 00347 fDBRE.fSpillLength = 4.; 00348 } 00349 fDBRE.fSpills = fDBRE.nspill-fDBRE.nemptyspill; 00350 fDBRE.fSpillTime = fDBRE.fSpillLength * fDBRE.fSpills; 00351 fDBRE.fSpillEvents = fDBRE.trigcount[0]+fDBRE.trigcount[4]+fDBRE.trigcount[5]+fDBRE.trigcount[6]+fDBRE.trigcount[7]+fDBRE.trigcount[8]+fDBRE.trigcount[9]+fDBRE.trigcount[10]+fDBRE.trigcount[11]; 00352 if (fDBRE.fSpillTime != 0.) { 00353 fDBRE.fDAQRate = fDBRE.fSpillEvents/fDBRE.fSpillTime; 00354 } 00355 else { 00356 fDBRE.fDAQRate = 0.; 00357 } 00358 00359 vec.push_back(fDBRE); 00360 } 00361 return; 00362 }
PGconn* AEMDBInterface::fDBConn [private] |
Definition at line 85 of file AEMDBInterface.h.
Referenced by ExecCommand(), ExecQuery(), Open(), and ~AEMDBInterface().
std::string AEMDBInterface::fDBHost [private] |
1.4.7