00001 #ifndef DBLIB_H
00002 #define DBLIB_H
00003
00004 #include <cstdlib>
00005 #include <iostream>
00006 #include <fstream>
00007 #include <sstream>
00008 #include <string>
00009 #include <stdexcept>
00010 #include <vector>
00011
00012 #include <TSQLServer.h>
00013 #include <TSQLResult.h>
00014 #include <TSQLRow.h>
00015
00016
00017 using namespace std;
00018
00019 Bool_t DBLIB_DEBUG = kFALSE;
00020 Bool_t DBLIB_PRINT = kTRUE;
00021 string mysql_hostname="blackhole.lngs.infn.it";
00022 string mysql_user="analysis";
00023
00024
00025
00026 vector<string> tokenize(const string& str,const string& delimiters)
00027 {
00028 vector<string> tokens;
00029
00030
00031 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00032
00033
00034 string::size_type pos = str.find_first_of(delimiters, lastPos);
00035
00036 while (string::npos != pos || string::npos != lastPos)
00037 {
00038
00039 tokens.push_back(str.substr(lastPos, pos - lastPos));
00040
00041
00042 lastPos = str.find_first_not_of(delimiters, pos);
00043
00044
00045 pos = str.find_first_of(delimiters, lastPos);
00046 }
00047
00048 return tokens;
00049 }
00050
00051
00052
00053 void sql_interface(TString DBname)
00054 {
00055
00056 printf("message\n");
00057 TSQLServer *db = TSQLServer::Connect("mysql://localhost/","analysis", "");
00058 printf("Server info: %s\n", db->ServerInfo());
00059 TSQLRow *row;
00060 TSQLResult *res;
00061
00062
00063 printf("\nList all databases on server %s\n", db->GetHost());
00064 res = db->GetDataBases();
00065 while ((row = res->Next()))
00066 {
00067 printf("%s\n", row->GetField(0));
00068 delete row;
00069 }
00070 delete res;
00071
00072
00073 printf("\nList all tables in database \"rundb\" on server %s\n",
00074 db->GetHost());
00075 res = db->GetTables(DBname);
00076 while ((row = res->Next()))
00077 {
00078 printf("%s\n", row->GetField(0));
00079 delete row;
00080 }
00081 delete res;
00082
00083
00084 printf("\nList all columns in table \"daqruns\" in database \"rundb\" on server %s\n",
00085 db->GetHost());
00086 res = db->GetColumns(DBname, "daqruns");
00087 while ((row = res->Next()))
00088 {
00089 printf("%s\n", row->GetField(0));
00090 delete row;
00091 }
00092 delete res;
00093
00094
00095 const char *sql = "SELECT runid, amplification FROM rundb.daqruns WHERE (runid>2000) AND (runid<2040) ORDER BY runid";
00096 res = db->Query(sql);
00097
00098 int nrows = res->GetRowCount();
00099 printf("\nGot %d rows in result\n", nrows);
00100
00101 int nfields = res->GetFieldCount();
00102 for (int i = 0; i < nfields; i++)
00103 printf("%50s", res->GetFieldName(i));
00104 printf("\n");
00105 for (int i = 0; i < nfields*50; i++)
00106 printf("=");
00107 printf("\n");
00108
00109 for (int i = 0; i < nrows; i++)
00110 {
00111 row = res->Next();
00112 for (int j = 0; j < nfields; j++)
00113 {
00114 printf("%50s", row->GetField(j));
00115 }
00116 printf("\n");
00117 delete row;
00118 }
00119
00120 delete res;
00121 delete db;
00122
00123 }
00124
00125 string DB_get_string(TString DBname, TString TABLEname, Int_t run_id, TString var, Int_t channel = 0)
00126 {
00127
00128
00129
00130
00131
00132 ostringstream os;
00133
00134
00135 FILE *fp;
00136 char run_hostnamet[30];
00137 fp = popen("hostname -f","r");
00138 fscanf(fp,"%s",run_hostnamet);
00139 fclose(fp);
00140 if(DBLIB_DEBUG == kTRUE) printf("%s\n", run_hostnamet);
00141 string run_hostname(run_hostnamet);
00142
00143 os.str("");
00144 if(run_hostname == mysql_hostname)
00145 {
00146 os << "mysql://localhost/";
00147 }
00148 else
00149 {
00150 os << "mysql://localhost/";
00151 }
00152
00153 TSQLServer *db = TSQLServer::Connect(os.str().c_str(),mysql_user.c_str(), "");
00154 if(DBLIB_DEBUG == kTRUE)
00155 {
00156 if(db)
00157 {
00158 cout << "Connected to " << os.str().c_str() << " as " << mysql_user.c_str() << endl;
00159 }
00160 else
00161 {
00162 cout << "Unable to connect to " << os.str().c_str() << " as " << mysql_user.c_str() << endl;
00163 std::exit(0);
00164 }
00165 }
00166 TSQLRow *row;
00167 TSQLResult *res;
00168 string result_string;
00169
00170 os.str("");
00171 if(TABLEname == "daqruns")
00172 {
00173 os << "SELECT " << var << " FROM " << DBname << "." << TABLEname << " WHERE runid = " << run_id;
00174 }
00175 if(TABLEname == "lasercalibration")
00176 {
00177 os << "SELECT " << var << " FROM " << DBname << "." << TABLEname << " WHERE runid = " << run_id << " and channel=" << channel;
00178 }
00179 if(DBLIB_DEBUG == kTRUE)
00180 cout << os.str() << endl;
00181 res = db->Query(os.str().c_str());;
00182
00183 int nrows = res->GetRowCount();
00184 if(DBLIB_DEBUG == kTRUE)
00185 {
00186 cout << "Got " << nrows << " rows in result" << endl;
00187 }
00188
00189 int nfields = res->GetFieldCount();
00190 if(DBLIB_DEBUG == kTRUE)
00191 {
00192 cout << nfields << " variables selected" << endl;
00193 }
00194 for (int i = 0; i < nrows; i++)
00195 {
00196 row = res->Next();
00197 for (int j = 0; j < nfields; j++)
00198 {
00199 if(DBLIB_DEBUG == kTRUE)
00200 {
00201 cout << var << " = " << row->GetField(j) << endl;
00202 }
00203 result_string = row->GetField(j);
00204 }
00205 delete row;
00206 }
00207
00208 delete res;
00209 delete db;
00210 return result_string;
00211 }
00212
00213
00214 string DB_get_var(TString DBname, TString TABLEname, Int_t run_id, TString var, Int_t channel = 0)
00215 {
00216
00217
00218 string result_string;
00219 ostringstream os;
00220 if(TABLEname == "daqruns")
00221 {
00222 os.str("");
00223 os << DB_get_string(DBname, TABLEname, run_id, "channel_ids", channel);
00224 vector<string> tokens = tokenize(os.str(), ",");
00225 Int_t exist_id=-1;
00226 for(int i=0;i<(int)tokens.size();i++)
00227 {
00228
00229
00230
00231
00232 if(atoi(tokens[i].c_str())==channel)
00233 {
00234 if(DBLIB_DEBUG == kTRUE)
00235 {
00236 cout << "tokens[i].c_str()=" << tokens[i].c_str()
00237 << " atoi(tokens[i].c_str())=" << atoi(tokens[i].c_str())
00238 << " channel=" << channel << endl;
00239 cout << "Channel " << channel << " is active" << endl;
00240 }
00241 exist_id=i;
00242 break;
00243 }
00244 }
00245 if(exist_id==-1)
00246 {
00247 cout << "Channel " << channel << " was deactivated for the analysis (or does not exist)!" << endl;
00248 return 0;
00249 }
00250 os.str("");
00251 os << DB_get_string(DBname, TABLEname, run_id, var, channel);
00252 vector<string> tokens2 = tokenize(os.str(), ",");
00253 if(exist_id>(Int_t)tokens2.size())
00254 {
00255 cout << "The variable " << var << " refers to the run and not only to channel " << channel << endl;
00256 }
00257 else
00258 {
00259 if(DBLIB_DEBUG == kTRUE) cout << tokens2[exist_id] << endl;
00260 result_string = tokens2[exist_id];
00261 }
00262 }
00263 if(TABLEname == "lasercalibration")
00264 {
00265 result_string = DB_get_string(DBname, TABLEname, run_id, var, channel);
00266 }
00267 return result_string;
00268 }
00269
00270 Double_t DB_get_spe(TString DBname, TString TABLEname, Int_t run_id, Int_t channel = 0)
00271 {
00272
00273
00274 ostringstream os;
00275 Double_t spe=0;
00276
00277
00278 FILE *fp;
00279 char run_hostnamet[30];
00280 fp = popen("hostname -f","r");
00281 fscanf(fp,"%s",run_hostnamet);
00282 fclose(fp);
00283
00284 string run_hostname(run_hostnamet);
00285
00286 os.str("");
00287 if(run_hostname == mysql_hostname)
00288 {
00289 os << "mysql://localhost/";
00290 }
00291 else
00292 {
00293 os << "mysql://localhost/";
00294 }
00295
00296 TSQLServer *db = TSQLServer::Connect(os.str().c_str(),mysql_user.c_str(), "");
00297 if(DBLIB_DEBUG == kTRUE)
00298 {
00299 if(db)
00300 {
00301 cout << "Connected to " << os.str().c_str() << " as " << mysql_user.c_str() << endl;
00302 }
00303 else
00304 {
00305 cout << "Unable to connect to " << os.str().c_str() << " as " << mysql_user.c_str() << endl;
00306 std::exit(0);
00307 }
00308 }
00309 TSQLRow *row;
00310 TSQLResult *res;
00311 string result_string;
00312 const long max_time_sep = 60*60*24;
00313 os.str("");
00314
00315
00316
00317
00318
00319
00320 os << "SELECT runid,pdfmean,ABS(runid-" << run_id
00321 << ") AS dr,ABS(UNIX_TIMESTAMP('" << DB_get_var(DBname, "daqruns", run_id, "starttime")
00322 <<"')-UNIX_TIMESTAMP(runtime)) AS dt FROM " << DBname << "." << TABLEname
00323 << " WHERE useme=true and channel="<< channel
00324 << " HAVING dt < "<< max_time_sep
00325 << " ORDER BY dt ASC";
00326
00327 if(DBLIB_DEBUG == kTRUE) cout << os.str().c_str() << endl;
00328 res = db->Query(os.str().c_str());
00329
00330 int nrows = res->GetRowCount();
00331 if(DBLIB_DEBUG == kTRUE)
00332 printf("\nGot %d rows in result\n", nrows);
00333 if(nrows == 0)
00334 return 0.;
00335 int nfields = res->GetFieldCount();
00336 for (int i = 0; i < nfields; i++)
00337 if(DBLIB_DEBUG == kTRUE) printf("%40s", res->GetFieldName(i));
00338 if(DBLIB_DEBUG == kTRUE)
00339 printf("\n");
00340 for (int i = 0; i < nfields*40; i++)
00341 if(DBLIB_DEBUG == kTRUE) printf("=");
00342 if(DBLIB_DEBUG == kTRUE) printf("\n");
00343
00344 for (int i = 0; i < nrows; i++)
00345 {
00346 row = res->Next();
00347 if (DB_get_var(DBname, "daqruns", run_id, "voltage", channel).compare(DB_get_var(DBname, "daqruns", atoi(row->GetField(0)), "voltage", channel)) == 0){
00348 if(DBLIB_DEBUG == kTRUE)
00349 {
00350 for (int j = 0; j < nfields; j++)
00351 {
00352 printf("%40s", row->GetField(j));
00353 }
00354 }
00355 spe = atof(DB_get_var(DBname, "lasercalibration", atoi(row->GetField(0)), "pdfmean", channel).c_str());
00356 cout << endl;
00357 cout << "Calibration Run" << atoi(row->GetField(0)) << "- Channel[" << channel << "]=";
00358 break;
00359 }
00360 delete row;
00361 }
00362 delete res;
00363 delete db;
00364 return spe;
00365 }
00366
00367 bool DB_does_run_exist(TString DBname, TString TABLEname, Int_t run_id)
00368 {
00369
00370 ostringstream os;
00371
00372
00373 FILE *fp;
00374 char run_hostnamet[30];
00375 fp = popen("hostname -f","r");
00376 fscanf(fp,"%s",run_hostnamet);
00377 fclose(fp);
00378 if(DBLIB_DEBUG == kTRUE)
00379 printf("%s\n", run_hostnamet);
00380 string run_hostname(run_hostnamet);
00381
00382 os.str("");
00383 if(run_hostname == mysql_hostname)
00384 os << "mysql://localhost/";
00385 else
00386 os << "mysql://localhost/";
00387
00388
00389 TSQLServer *db = TSQLServer::Connect(os.str().c_str(),mysql_user.c_str(), "");
00390 if(DBLIB_DEBUG == kTRUE)
00391 {
00392 if(db)
00393 cout << "Connected to " << os.str().c_str() << " as " << mysql_user.c_str() << endl;
00394 else
00395 {
00396 cout << "Unable to connect to " << os.str().c_str() << " as " << mysql_user.c_str() << endl;
00397 std::exit(0);
00398 }
00399 }
00400
00401 TSQLResult *res;
00402 os.str("");
00403 os << "SELECT * FROM " << DBname << "." << TABLEname << " WHERE runid = " << run_id;
00404
00405 if(DBLIB_DEBUG == kTRUE)
00406 cout << os.str() << endl;
00407
00408 res = db->Query(os.str().c_str());;
00409
00410 int nrows = res->GetRowCount();
00411
00412 bool exist = false;
00413
00414 if (nrows > 0)
00415 exist = true;
00416
00417 if(DBLIB_DEBUG == kTRUE)
00418 cout << "Got " << nrows << " rows in result" << endl;
00419
00420 delete res;
00421 delete db;
00422 return exist;
00423 }
00424
00425 #endif