17 #include "cetlib_except/exception.h" 38 if (PQstatus(
conn) == CONNECTION_BAD) {
39 mf::LogWarning(
"DatabaseUtil") <<
"Connection to database failed, "<<PQerrorMessage(
conn)<<
"\n";
40 if( ( strstr(PQerrorMessage(
conn),
"remaining connection slots are reserved")!=NULL ||
41 strstr(PQerrorMessage(
conn),
"sorry, too many clients already")!=NULL )
44 mf::LogWarning(
"DatabaseUtil") <<
"retrying connection after " << conn_wait <<
" seconds \n";
45 return this->
Connect(conn_wait);
87 if (!passfname.empty()) {
88 std::ifstream in(passfname.c_str());
91 <<
"Database password file '" << passfname
92 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
94 std::getline(in, fPassword);
97 else if (fShouldConnect){
99 <<
"Database password file '" << pset.
get<
std::string >(
"PassFileName")
100 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
103 sprintf(
connection_str,
"host=%s dbname=%s user=%s port=%d password=%s ",
fDBHostName.c_str(),fDBName.c_str(),fDBUser.c_str(),
fPort,fPassword.c_str());
120 mf::LogInfo(
"DatabaseUtil")<<
"Not connecting to DB by choice. \n";
124 result = PQexec(
conn, query);
127 mf::LogInfo(
"DatabaseUtil")<<
"PQexec command failed, no error code\n";
130 else if(PQresultStatus(result)!=PGRES_TUPLES_OK) {
131 if(PQresultStatus(result)==PGRES_COMMAND_OK)
132 MF_LOG_DEBUG(
"DatabaseUtil")<<
"Command executed OK, "<< PQcmdTuples(result) <<
" rows affected\n";
135 <<PQresStatus(PQresultStatus(result)) <<
", error message " 136 <<PQresultErrorMessage(result)<<
"\n";
147 if(PQntuples(result)>=1){
148 for(
int i=0;i<PQntuples(result);i++)
150 string_val=PQgetvalue(result,i,0);
151 value.push_back(string_val);
152 MF_LOG_DEBUG(
"DatabaseUtil")<<
" extracted value: "<<value[i] <<
"\n";
159 mf::LogWarning(
"DatabaseUtil")<<
"wrong number of rows returned:"<<PQntuples(result)<<
"\n";
173 std::vector<std::string> retvalue;
175 sprintf(cond,
"run = %d",run);
178 if(err!=-1 && retvalue.size()==1){
180 temp_real=std::strtod(retvalue[0].c_str(),&endstr);
195 std::vector<std::string> retvalue;
198 sprintf(query,
"SELECT EFbet FROM EField,%s WHERE Efield.FID = %s.FID AND run = %d ORDER BY planegap",
fTableName.c_str(),
fTableName.c_str(),
run);
201 if(err!=-1 && retvalue.size()>=1){
203 for(
unsigned int i=0;i<retvalue.size();i++) {
205 efield.push_back(std::strtod(retvalue[i].c_str(),&endstr));
218 const char * condition,
219 const char * table) {
222 sprintf(query,
"SELECT %s FROM %s WHERE %s",field, table, condition);
240 std::vector<std::string> retvalue;
242 sprintf(cond,
"run = %d",run);
245 if(err!=-1 && retvalue.size()==1){
247 lftime_real=std::strtod(retvalue[0].c_str(),&endstr);
261 std::vector<std::string> retvalue;
263 sprintf(cond,
"run = %d",run);
266 if(err!=-1 && retvalue.size()==1){
268 T0_real=std::strtod(retvalue[0].c_str(),&endstr);
283 std::vector<std::string> retvalue;
285 sprintf(cond,
"run = %d",run);
288 if(err!=-1 && retvalue.size()==1){
290 POT=std::strtold(retvalue[0].c_str(),&endstr);
311 if(PQstatus(
conn)!=CONNECTION_OK) {
312 mf::LogError(
"") << __PRETTY_FUNCTION__ <<
": Couldn't open connection to postgresql interface" << PQdb(
conn) <<
":"<<PQhost(
conn);
315 <<
"Failed to get channel map from DB."<<
std::endl;
321 PGresult *res = PQexec(
conn,
"BEGIN");
322 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
327 <<
"postgresql BEGIN failed." <<
std::endl;
339 sprintf(dbquery,
"SELECT get_map_double_sec(%i,%i);", data_taking_timestamp, swizzling_timestamp);
340 res = PQexec(
conn, dbquery);
342 if ((!res) || (PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) < 1))
344 mf::LogError(
"")<<
"SELECT command did not return tuples properly. \n" << PQresultErrorMessage(res) <<
"Number rows: "<< PQntuples(res);
348 <<
"postgresql SELECT failed." <<
std::endl;
351 int num_records=PQntuples(res);
353 for (
int i=0;i<num_records;i++) {
355 tup = tup.substr(1,tup.length()-2);
356 std::vector<std::string> fields;
357 split(tup,
',', fields);
359 int crate_id = atoi( fields[0].c_str() );
360 int slot = atoi( fields[1].c_str() );
361 int boardChan = atoi( fields[2].c_str() );
362 int larsoft_chan = atoi( fields[3].c_str() );
364 UBDaqID daq_id(crate_id,slot,boardChan);
365 std::pair<UBDaqID, UBLArSoftCh_t>
p(daq_id,larsoft_chan);
368 std::cout << __PRETTY_FUNCTION__ <<
": ";
369 std::cout <<
"Multiple entries!" <<
std::endl;
370 mf::LogWarning(
"")<<
"Multiple DB entries for same (crate,card,channel). "<<std::endl
371 <<
"Redefining (crate,card,channel)=>id link (" 377 fChannelReverseMap.insert( std::pair< UBLArSoftCh_t, UBDaqID >( larsoft_chan, daq_id ) );
395 std::stringstream ss(s);
397 while (std::getline(ss, item, delim)) {
398 elems.push_back(item);
int GetTriggerOffsetFromDB(int run, double &T0_real)
Namespace for general, non-LArSoft-specific utilities.
int GetEfieldValuesFromDB(int run, std::vector< double > &efield)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
UBChannelMap_t GetUBChannelMap(int data_taking_timestamp=-1, int swizzling_timestamp=-1)
bool fToughErrorTreatment
UBChannelReverseMap_t GetUBChannelReverseMap(int data_taking_timestamp=-1, int swizzling_timestamp=-1)
int SelectSingleFieldByQuery(std::vector< std::string > &value, const char *query)
int GetPOTFromDB(int run, long double &POT)
T get(std::string const &key) const
int GetLifetimeFromDB(int run, double &lftime_real)
DatabaseUtil(fhicl::ParameterSet const &pset)
void err(const char *fmt,...)
void reconfigure(fhicl::ParameterSet const &pset)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
#define DEFINE_ART_SERVICE(svc)
void LoadUBChannelMap(int data_taking_timestamp=-1, int swizzling_timestamp=-1)
int SelectFieldByName(std::vector< std::string > &value, const char *field, const char *condition, const char *table)
std::map< UBDaqID, UBLArSoftCh_t > UBChannelMap_t
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
int Connect(int conn_wait=0)
std::string find_file(std::string const &filename) const
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
std::map< UBLArSoftCh_t, UBDaqID > UBChannelReverseMap_t
query_result< Args... > query(sqlite3 *db, std::string const &ddl)
cet::coded_exception< error, detail::translate > exception
QTextStream & endl(QTextStream &s)
UBChannelReverseMap_t fChannelReverseMap
int GetTemperatureFromDB(int run, double &temp_real)
UBChannelMap_t fChannelMap