00001 #include <string>
00002 #include <iostream>
00003 #include <sstream>
00004
00005 #include <TSQLServer.h>
00006 #include <TSQLResult.h>
00007 #include <TSQLRow.h>
00008
00009 template <class T>
00010 inline Int_t to_int(const T& t)
00011 {
00012 Int_t res;
00013 std::stringstream ss(t);
00014 ss >> res;
00015 return res;
00016 }
00017
00018 template <class T>
00019 inline Double_t to_double(const T& t)
00020 {
00021 Double_t res;
00022 std::stringstream ss(t);
00023 ss >> res;
00024 return res;
00025 }
00026
00027
00028 void ftpc_sqldraw(std::string dbpath = "Conditions_ftpc/ftpcTemps/extra1East", std::string minTime = "2009-06-16 00:00:00",
00029 std::string maxTime = "2009-06-29 23:59:59") {
00030
00031 std::string::size_type pos = dbpath.find_last_not_of("/");
00032 if ( pos == (dbpath.length()-1) ) {
00033 dbpath.append("/");
00034 }
00035
00036 std::stringstream ss(dbpath);
00037 std::string s;
00038
00039 std::string mDatabase, mTable, mParam;
00040 std::getline(ss, mDatabase, '/');
00041 std::getline(ss, mTable, '/');
00042 std::getline(ss, mParam, '/');
00043
00044 std::string dsn = "mysql://onldb.starp.bnl.gov:3502/";
00045 dsn.append(mDatabase);
00046 TSQLServer *db = 0;
00047 db = TSQLServer::Connect(dsn.c_str(),"staruser", "");
00048 if (!db) {
00049 std::cerr << "Error: cannot connect to database!" << std::endl;
00050 }
00051
00052 TSQLRow *row;
00053 TSQLResult *res;
00054
00055 std::string sql = "SELECT MIN(UNIX_TIMESTAMP(beginTime)), MAX(UNIX_TIMESTAMP(beginTime)), COUNT(beginTime), MIN(";
00056 sql.append(mParam);
00057 sql.append("), MAX(");
00058 sql.append(mParam);
00059 sql.append(") FROM ");
00060 sql.append(mTable);
00061 sql.append(" WHERE beginTime > '");
00062 sql.append(minTime);
00063 sql.append("' AND beginTime < '");
00064 sql.append(maxTime);
00065 sql.append("'");
00066
00067 res = db->Query(sql.c_str());
00068 int nrows = res->GetRowCount();
00069 if (nrows <= 0) {
00070 std::err << "Error: no entries for selected period!" << std::endl;
00071 return;
00072 }
00073
00074 row = res->Next();
00075
00076 if (row->GetField(0) == NULL || row->GetField(1) == NULL || row->GetField(2) == NULL || row->GetField(3) == NULL || row->GetField(4) == NULL) {
00077 std::cerr << "Error: count query returned null, probably no entries for selected period" << std::endl;
00078 return;
00079 }
00080
00081 Int_t timeMin = to_int(row->GetField(0));
00082 Int_t timeMax = to_int(row->GetField(1));
00083 Int_t timeDiff = timeMax - timeMin;
00084 Int_t timeNbins = to_int(row->GetField(2));
00085
00086
00087
00088 Double_t valMin = 20.0;
00089 Double_t valMax = 30.0;
00090
00091 TDatime dh(timeMin);
00092
00093 delete row;
00094 delete res;
00095
00096 std::string h1title;
00097 h1title.append(mParam);
00098 h1title.append("(");
00099 h1title.append(mDatabase);
00100 h1title.append("/");
00101 h1title.append(mTable);
00102 h1title.append(")");
00103 h1title.append(" vs. beginTime");
00104
00105 gStyle->SetOptStat(0);
00106
00107 TH2D* h1 = new TH2D("th2d", h1title.c_str(), timeNbins, 0, timeDiff+1, 100, valMin - fabs(valMin*0.01), valMax + fabs(valMax*0.01));
00108 h1->SetStats(0);
00109 h1->SetTitle(h1title.c_str());
00110 h1->GetXaxis()->SetTimeOffset(dh.Convert());
00111 h1->GetXaxis()->SetTimeDisplay(1);
00112 h1->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M:%S}");
00113 h1->GetXaxis()->SetLabelSize(0.02);
00114 h1->GetXaxis()->SetLabelOffset(0.02);
00115 h1->SetMarkerSize(0.8);
00116 h1->SetMarkerStyle(20);
00117 h1->SetMarkerColor(4);
00118
00119 std::string sql = "SELECT UNIX_TIMESTAMP(beginTime), ";
00120 sql.append(mParam);
00121 sql.append(" FROM ");
00122 sql.append(mTable);
00123 sql.append(" WHERE beginTime > '");
00124 sql.append(minTime);
00125 sql.append("' AND beginTime < '");
00126 sql.append(maxTime);
00127 sql.append("' ORDER BY beginTime ASC");
00128
00129 res = db->Query(sql.c_str());
00130
00131 nrows = res->GetRowCount();
00132 std::cout << "Got " << nrows << " rows from database, please wait...\n";
00133 if (nrows > 500) {
00134 std::cout << "[found many rows, it may take a few minutes to create histogram]" << std::endl;
00135 }
00136
00137 int i = 0;
00138 while ((row = res->Next())) {
00139 i++;
00140 if (i%100 == 0) {
00141 std::cout << " working on " << i << "th row \n";
00142 }
00143 h1->Fill(to_int(row->GetField(0)) - timeMin, to_double(row->GetField(1)));
00144 }
00145
00146 delete res;
00147 delete db;
00148
00149 h1->Draw("L");
00150
00151 }
00152