16 #include "cetlib_except/exception.h" 43 if (PQstatus(
conn) == CONNECTION_BAD) {
44 mf::LogWarning(
"DatabaseUtil") <<
"Connection to database failed, "<<PQerrorMessage(
conn)<<
"\n";
45 if( ( strstr(PQerrorMessage(
conn),
"remaining connection slots are reserved")!=NULL ||
46 strstr(PQerrorMessage(
conn),
"sorry, too many clients already")!=NULL )
49 mf::LogWarning(
"DatabaseUtil") <<
"retrying connection after " << conn_wait <<
" seconds \n";
50 return this->
Connect(conn_wait);
56 LOG_DEBUG(
"DatabaseUtil")<<
"Connected OK\n";
68 LOG_DEBUG(
"DatabaseUtil")<<
"Closing Connection \n";
88 std::string passfname;
89 cet::search_path sp(
"FW_SEARCH_PATH");
90 sp.find_file(pset.
get< std::string >(
"PassFileName"), passfname);
92 if (!passfname.empty()) {
93 std::ifstream
in(passfname.c_str());
96 <<
"Database password file '" << passfname
97 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
99 std::getline(
in, fPassword);
102 else if (fShouldConnect){
104 <<
"Database password file '" << pset.
get< std::string >(
"PassFileName")
105 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
108 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());
125 mf::LogInfo(
"DatabaseUtil")<<
"Not connecting to DB by choice. \n";
129 result = PQexec(
conn, query);
132 mf::LogInfo(
"DatabaseUtil")<<
"PQexec command failed, no error code\n";
135 else if(PQresultStatus(result)!=PGRES_TUPLES_OK) {
136 if(PQresultStatus(result)==PGRES_COMMAND_OK)
137 LOG_DEBUG(
"DatabaseUtil")<<
"Command executed OK, "<< PQcmdTuples(result) <<
" rows affected\n";
140 <<PQresStatus(PQresultStatus(result)) <<
", error message " 141 <<PQresultErrorMessage(result)<<
"\n";
152 if(PQntuples(result)>=1){
153 for(
int i=0;i<PQntuples(result);i++)
155 string_val=PQgetvalue(result,i,0);
156 value.push_back(string_val);
157 LOG_DEBUG(
"DatabaseUtil")<<
" extracted value: "<<value[i] <<
"\n";
164 mf::LogWarning(
"DatabaseUtil")<<
"wrong number of rows returned:"<<PQntuples(result)<<
"\n";
178 std::vector<std::string> retvalue;
180 sprintf(cond,
"run = %d",run);
183 if(err!=-1 && retvalue.size()==1){
185 temp_real=std::strtod(retvalue[0].c_str(),&endstr);
200 std::vector<std::string> retvalue;
203 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);
206 if(err!=-1 && retvalue.size()>=1){
208 for(
unsigned int i=0;i<retvalue.size();i++) {
210 efield.push_back(std::strtod(retvalue[i].c_str(),&endstr));
223 const char * condition,
224 const char * table) {
227 sprintf(query,
"SELECT %s FROM %s WHERE %s",field, table, condition);
245 std::vector<std::string> retvalue;
247 sprintf(cond,
"run = %d",run);
250 if(err!=-1 && retvalue.size()==1){
252 lftime_real=std::strtod(retvalue[0].c_str(),&endstr);
266 std::vector<std::string> retvalue;
268 sprintf(cond,
"run = %d",run);
271 if(err!=-1 && retvalue.size()==1){
273 T0_real=std::strtod(retvalue[0].c_str(),&endstr);
288 std::vector<std::string> retvalue;
290 sprintf(cond,
"run = %d",run);
293 if(err!=-1 && retvalue.size()==1){
295 POT=std::strtold(retvalue[0].c_str(),&endstr);
316 if(PQstatus(
conn)!=CONNECTION_OK) {
317 mf::LogError(
"") << __PRETTY_FUNCTION__ <<
": Couldn't open connection to postgresql interface" << PQdb(
conn) <<
":"<<PQhost(
conn);
320 <<
"Failed to get channel map from DB."<< std::endl;
327 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
332 <<
"postgresql BEGIN failed." << std::endl;
344 sprintf(dbquery,
"SELECT get_map_double_sec(%i,%i);", data_taking_timestamp, swizzling_timestamp);
345 res = PQexec(
conn, dbquery);
347 if ((!res) || (PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) < 1))
349 mf::LogError(
"")<<
"SELECT command did not return tuples properly. \n" << PQresultErrorMessage(res) <<
"Number rows: "<< PQntuples(res);
353 <<
"postgresql SELECT failed." << std::endl;
356 int num_records=PQntuples(res);
358 for (
int i=0;i<num_records;i++) {
359 std::string tup = PQgetvalue(res, i, 0);
360 tup = tup.substr(1,tup.length()-2);
361 std::vector<std::string> fields;
362 split(tup,
',', fields);
364 int crate_id = atoi( fields[0].c_str() );
365 int slot = atoi( fields[1].c_str() );
366 int boardChan = atoi( fields[2].c_str() );
367 int larsoft_chan = atoi( fields[3].c_str() );
369 UBDaqID daq_id(crate_id,slot,boardChan);
370 std::pair<UBDaqID, UBLArSoftCh_t> p(daq_id,larsoft_chan);
373 std::cout << __PRETTY_FUNCTION__ <<
": ";
374 std::cout <<
"Multiple entries!" << std::endl;
375 mf::LogWarning(
"")<<
"Multiple DB entries for same (crate,card,channel). "<<std::endl
376 <<
"Redefining (crate,card,channel)=>id link (" 382 fChannelReverseMap.insert( std::pair< UBLArSoftCh_t, UBDaqID >( larsoft_chan, daq_id ) );
399 std::vector<std::string> &
DatabaseUtil::split(
const std::string &
s,
char delim, std::vector<std::string> &elems) {
400 std::stringstream
ss(s);
402 while (std::getline(ss, item, delim)) {
403 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
#define DEFINE_ART_SERVICE(svc)
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)
void reconfigure(fhicl::ParameterSet const &pset)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
DatabaseUtil(fhicl::ParameterSet const &pset, art::ActivityRegistry ®)
void LoadUBChannelMap(int data_taking_timestamp=-1, int swizzling_timestamp=-1)
std::string value(boost::any const &)
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::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
std::map< UBLArSoftCh_t, UBDaqID > UBChannelReverseMap_t
cet::coded_exception< error, detail::translate > exception
UBChannelReverseMap_t fChannelReverseMap
int GetTemperatureFromDB(int run, double &temp_real)
UBChannelMap_t fChannelMap