14 #include "cetlib_except/exception.h" 32 if (conn_wait) sleep(conn_wait);
35 if (PQstatus(
conn) == CONNECTION_BAD) {
37 <<
"Connection to database failed, " << PQerrorMessage(
conn) <<
"\n";
38 if ((strstr(PQerrorMessage(
conn),
"remaining connection slots are reserved") != NULL ||
39 strstr(PQerrorMessage(
conn),
"sorry, too many clients already") != NULL) &&
42 mf::LogWarning(
"DatabaseUtil") <<
"retrying connection after " << conn_wait <<
" seconds \n";
43 return this->
Connect(conn_wait);
76 std::string passfname;
77 cet::search_path sp(
"FW_SEARCH_PATH");
78 sp.find_file(pset.
get<std::string>(
"PassFileName"), passfname);
80 if (!passfname.empty()) {
81 std::ifstream
in(passfname.c_str());
84 <<
"Database password file '" << passfname
85 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
87 std::getline(
in, fPassword);
90 else if (fShouldConnect) {
92 <<
"Database password file '" << pset.
get<std::string>(
"PassFileName")
93 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
97 "host=%s dbname=%s user=%s port=%d password=%s ",
116 mf::LogInfo(
"DatabaseUtil") <<
"Not connecting to DB by choice. \n";
120 result = PQexec(
conn, query);
123 mf::LogInfo(
"DatabaseUtil") <<
"PQexec command failed, no error code\n";
126 else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
127 if (PQresultStatus(result) == PGRES_COMMAND_OK)
129 <<
"Command executed OK, " << PQcmdTuples(result) <<
" rows affected\n";
132 <<
"Command failed with code " << PQresStatus(PQresultStatus(result)) <<
", error message " 133 << PQresultErrorMessage(result) <<
"\n";
144 if (PQntuples(result) >= 1) {
145 for (
int i = 0; i < PQntuples(result); i++) {
146 string_val = PQgetvalue(result, i, 0);
147 value.push_back(string_val);
148 MF_LOG_DEBUG(
"DatabaseUtil") <<
" extracted value: " << value[i] <<
"\n";
156 <<
"wrong number of rows returned:" << PQntuples(result) <<
"\n";
166 std::vector<std::string> retvalue;
168 sprintf(cond,
"run = %d", run);
171 if (err != -1 && retvalue.size() == 1) {
173 temp_real = std::strtod(retvalue[0].c_str(), &endstr);
183 std::vector<std::string> retvalue;
187 "SELECT EFbet FROM EField,%s WHERE Efield.FID = %s.FID AND run = %d ORDER BY planegap",
193 if (err != -1 && retvalue.size() >= 1) {
195 for (
unsigned int i = 0; i < retvalue.size(); i++) {
197 efield.push_back(std::strtod(retvalue[i].c_str(), &endstr));
207 const char* condition,
212 sprintf(query,
"SELECT %s FROM %s WHERE %s", field, table, condition);
223 std::vector<std::string> retvalue;
225 sprintf(cond,
"run = %d", run);
228 if (err != -1 && retvalue.size() == 1) {
230 lftime_real = std::strtod(retvalue[0].c_str(), &endstr);
243 std::vector<std::string> retvalue;
245 sprintf(cond,
"run = %d", run);
248 if (err != -1 && retvalue.size() == 1) {
250 T0_real = std::strtod(retvalue[0].c_str(), &endstr);
263 std::vector<std::string> retvalue;
265 sprintf(cond,
"run = %d", run);
268 if (err != -1 && retvalue.size() == 1) {
270 POT = std::strtold(retvalue[0].c_str(), &endstr);
289 if (PQstatus(
conn) != CONNECTION_OK) {
291 <<
": Couldn't open connection to postgresql interface" << PQdb(
conn) <<
":" 295 <<
"Failed to get channel map from DB." << std::endl;
302 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
319 dbquery,
"SELECT get_map_double_sec(%i,%i);", data_taking_timestamp, swizzling_timestamp);
320 res = PQexec(
conn, dbquery);
322 if ((!res) || (PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) < 1)) {
323 mf::LogError(
"") <<
"SELECT command did not return tuples properly. \n" 324 << PQresultErrorMessage(res) <<
"Number rows: " << PQntuples(res);
330 int num_records = PQntuples(res);
332 for (
int i = 0; i < num_records; i++) {
333 std::string tup = PQgetvalue(res, i, 0);
334 tup = tup.substr(1, tup.length() - 2);
335 std::vector<std::string> fields;
336 split(tup,
',', fields);
338 int crate_id = atoi(fields[0].c_str());
339 int slot = atoi(fields[1].c_str());
340 int boardChan = atoi(fields[2].c_str());
341 int larsoft_chan = atoi(fields[3].c_str());
343 UBDaqID daq_id(crate_id, slot, boardChan);
344 std::pair<UBDaqID, UBLArSoftCh_t> p(daq_id, larsoft_chan);
347 std::cout << __PRETTY_FUNCTION__ <<
": ";
348 std::cout <<
"Multiple entries!" << std::endl;
349 mf::LogWarning(
"") <<
"Multiple DB entries for same (crate,card,channel). " << std::endl
350 <<
"Redefining (crate,card,channel)=>id link (" << daq_id.
crate <<
", " 368 int swizzling_timestamp)
378 std::vector<std::string>& elems)
380 std::stringstream ss(s);
382 while (std::getline(ss, item, delim)) {
383 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)
std::map< UBDaqID, UBLArSoftCh_t > UBChannelMap_t
void reconfigure(fhicl::ParameterSet const &pset)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
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)
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