15 #include "messagefacility/MessageLogger/MessageLogger.h"
16 #include "cetlib_except/exception.h"
36 conn = PQconnectdb(connection_str);
37 if (PQstatus(
conn) == CONNECTION_BAD) {
38 mf::LogWarning(
"DatabaseUtil") <<
"Connection to database failed, "<<PQerrorMessage(
conn)<<
"\n";
39 if( ( strstr(PQerrorMessage(
conn),
"remaining connection slots are reserved")!=NULL ||
40 strstr(PQerrorMessage(
conn),
"sorry, too many clients already")!=NULL )
43 mf::LogWarning(
"DatabaseUtil") <<
"retrying connection after " << conn_wait <<
" seconds \n";
44 return this->Connect(conn_wait);
46 if(fToughErrorTreatment)
47 throw cet::exception(
"DataBaseUtil") <<
" DB connection failed\n";
50 MF_LOG_DEBUG(
"DatabaseUtil")<<
"Connected OK\n";
62 MF_LOG_DEBUG(
"DatabaseUtil")<<
"Closing Connection \n";
72 fDBHostName = pset.get< std::string >(
"DBHostName" );
73 fDBName = pset.get< std::string >(
"DBName" );
74 fDBUser = pset.get< std::string >(
"DBUser");
75 fTableName = pset.get< std::string >(
"TableName");
76 fPort = pset.get<
int >(
"Port" );
78 fToughErrorTreatment = pset.get<
bool >(
"ToughErrorTreatment");
79 fShouldConnect = pset.get<
bool >(
"ShouldConnect");
82 std::string passfname;
83 cet::search_path sp(
"FW_SEARCH_PATH");
84 sp.find_file(pset.get< std::string >(
"PassFileName"), passfname);
86 if (!passfname.empty()) {
87 std::ifstream
in(passfname.c_str());
89 throw art::Exception(art::errors::NotFound)
90 <<
"Database password file '" << passfname
91 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
93 std::getline(
in, fPassword);
96 else if (fShouldConnect){
97 throw art::Exception(art::errors::NotFound)
98 <<
"Database password file '" << pset.get< std::string >(
"PassFileName")
99 <<
"' not found in FW_SEARCH_PATH; using an empty password.\n";
102 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());
115 if(this->Connect()==-1) {
117 mf::LogWarning(
"DatabaseUtil")<<
"DB Connection error \n";
119 mf::LogInfo(
"DatabaseUtil")<<
"Not connecting to DB by choice. \n";
123 result = PQexec(
conn, query);
126 mf::LogInfo(
"DatabaseUtil")<<
"PQexec command failed, no error code\n";
129 else if(PQresultStatus(result)!=PGRES_TUPLES_OK) {
130 if(PQresultStatus(result)==PGRES_COMMAND_OK)
131 MF_LOG_DEBUG(
"DatabaseUtil")<<
"Command executed OK, "<< PQcmdTuples(result) <<
" rows affected\n";
133 mf::LogWarning(
"DatabaseUtil")<<
"Command failed with code "
134 <<PQresStatus(PQresultStatus(result)) <<
", error message "
135 <<PQresultErrorMessage(result)<<
"\n";
146 if(PQntuples(result)>=1){
147 for(
int i=0;i<PQntuples(result);i++)
149 string_val=PQgetvalue(result,i,0);
150 value.push_back(string_val);
151 MF_LOG_DEBUG(
"DatabaseUtil")<<
" extracted value: "<<value[i] <<
"\n";
158 mf::LogWarning(
"DatabaseUtil")<<
"wrong number of rows returned:"<<PQntuples(result)<<
"\n";
172 std::vector<std::string> retvalue;
174 sprintf(cond,
"run = %d",run);
175 int err=SelectFieldByName(retvalue,
"temp",cond,fTableName.c_str());
177 if(err!=-1 && retvalue.size()==1){
179 temp_real=std::strtod(retvalue[0].c_str(),&endstr);
194 std::vector<std::string> retvalue;
197 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);
198 int err=SelectSingleFieldByQuery(retvalue,query);
200 if(err!=-1 && retvalue.size()>=1){
202 for(
unsigned int i=0;i<retvalue.size();i++) {
204 efield.push_back(std::strtod(retvalue[i].c_str(),&endstr));
217 const char * condition,
218 const char * table) {
221 sprintf(query,
"SELECT %s FROM %s WHERE %s",field, table, condition);
223 return SelectSingleFieldByQuery(value,query);
239 std::vector<std::string> retvalue;
241 sprintf(cond,
"run = %d",run);
242 int err=SelectFieldByName(retvalue,
"tau",cond,fTableName.c_str());
244 if(err!=-1 && retvalue.size()==1){
246 lftime_real=std::strtod(retvalue[0].c_str(),&endstr);
260 std::vector<std::string> retvalue;
262 sprintf(cond,
"run = %d",run);
263 int err=SelectFieldByName(retvalue,
"T0",cond,fTableName.c_str());
265 if(err!=-1 && retvalue.size()==1){
267 T0_real=std::strtod(retvalue[0].c_str(),&endstr);
282 std::vector<std::string> retvalue;
284 sprintf(cond,
"run = %d",run);
285 int err=SelectFieldByName(retvalue,
"pot",cond,fTableName.c_str());
287 if(err!=-1 && retvalue.size()==1){
289 POT=std::strtold(retvalue[0].c_str(),&endstr);
302 if ( fChannelMap.size()>0 ) {
310 if(PQstatus(
conn)!=CONNECTION_OK) {
311 mf::LogError(
"") << __PRETTY_FUNCTION__ <<
": Couldn't open connection to postgresql interface" << PQdb(
conn) <<
":"<<PQhost(
conn);
313 throw art::Exception( art::errors::FileReadError )
314 <<
"Failed to get channel map from DB."<< std::endl;
318 fChannelReverseMap.clear();
320 PGresult *res = PQexec(
conn,
"BEGIN");
321 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
322 mf::LogError(
"")<<
"postgresql BEGIN failed";
325 throw art::Exception( art::errors::FileReadError )
326 <<
"postgresql BEGIN failed." << std::endl;
338 sprintf(dbquery,
"SELECT get_map_double_sec(%i,%i);", data_taking_timestamp, swizzling_timestamp);
339 res = PQexec(
conn, dbquery);
341 if ((!res) || (PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) < 1))
343 mf::LogError(
"")<<
"SELECT command did not return tuples properly. \n" << PQresultErrorMessage(res) <<
"Number rows: "<< PQntuples(res);
346 throw art::Exception( art::errors::FileReadError )
347 <<
"postgresql SELECT failed." << std::endl;
350 int num_records=PQntuples(res);
352 for (
int i=0;i<num_records;i++) {
353 std::string tup = PQgetvalue(res, i, 0);
354 tup = tup.substr(1,tup.length()-2);
355 std::vector<std::string> fields;
356 split(tup,
',', fields);
358 int crate_id = atoi( fields[0].c_str() );
359 int slot = atoi( fields[1].c_str() );
360 int boardChan = atoi( fields[2].c_str() );
361 int larsoft_chan = atoi( fields[3].c_str() );
363 UBDaqID daq_id(crate_id,slot,boardChan);
364 std::pair<UBDaqID, UBLArSoftCh_t>
p(daq_id,larsoft_chan);
366 if ( fChannelMap.find(daq_id) != fChannelMap.end() ){
367 std::cout << __PRETTY_FUNCTION__ <<
": ";
368 std::cout <<
"Multiple entries!" << std::endl;
369 mf::LogWarning(
"")<<
"Multiple DB entries for same (crate,card,channel). "<<std::endl
370 <<
"Redefining (crate,card,channel)=>id link ("
372 << fChannelMap.find(daq_id)->second;
375 fChannelMap.insert( p );
376 fChannelReverseMap.insert( std::pair< UBLArSoftCh_t, UBDaqID >( larsoft_chan, daq_id ) );
382 LoadUBChannelMap( data_taking_timestamp, swizzling_timestamp );
387 LoadUBChannelMap( data_taking_timestamp, swizzling_timestamp );
388 return fChannelReverseMap;
393 std::vector<std::string> &
DatabaseUtil::split(
const std::string &
s,
char delim, std::vector<std::string> &elems) {
394 std::stringstream ss(s);
396 while (std::getline(ss, item, delim)) {
397 elems.push_back(item);
int GetTriggerOffsetFromDB(int run, double &T0_real)
int GetEfieldValuesFromDB(int run, std::vector< double > &efield)
EResult err(const char *call)
UBChannelMap_t GetUBChannelMap(int data_taking_timestamp=-1, int swizzling_timestamp=-1)
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)
then echo echo For and will not be changed by echo further linking echo echo B echo The symbol is in the uninitialized data multiple common symbols may appear with the echo same name If the symbol is defined the common echo symbols are treated as undefined references For more echo details on common see the discussion of warn common echo in *Note Linker see the discussion of warn common echo in *Note Linker such as a global int variable echo as opposed to a large global array echo echo I echo The symbol is an indirect reference to another symbol This echo is a GNU extension to the a out object file format which is echo rarely used echo echo N echo The symbol is a debugging symbol echo echo R echo The symbol is in a read only data section echo echo S echo The symbol is in an uninitialized data section for small echo objects echo echo T echo The symbol is in the the normal defined echo symbol is used with no error When a weak undefined symbol echo is linked and the symbol is not the value of the echo weak symbol becomes zero with no error echo echo W echo The symbol is a weak symbol that has not been specifically echo tagged as a weak object symbol When a weak defined symbol echo is linked with a normal defined the normal defined echo symbol is used with no error When a weak undefined symbol echo is linked and the symbol is not the value of the echo weak symbol becomes zero with no error echo echo echo The symbol is a stabs symbol in an a out object file In echo this the next values printed are the stabs other field
int GetLifetimeFromDB(int run, double &lftime_real)
DatabaseUtil(fhicl::ParameterSet const &pset)
void reconfigure(fhicl::ParameterSet const &pset)
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)
if &&[-z"$BASH_VERSION"] then echo Attempting to switch to bash bash shellSwitch exit fi &&["$1"= 'shellSwitch'] shift declare a IncludeDirectives for Dir in
std::map< UBDaqID, UBLArSoftCh_t > UBChannelMap_t
then echo File list $list not found else cat $list while read file do echo $file sed s
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
BEGIN_PROLOG could also be cout
UBChannelReverseMap_t fChannelReverseMap
int GetTemperatureFromDB(int run, double &temp_real)
UBChannelMap_t fChannelMap