5 #include <TSQLServer.h>
6 #include <TSQLResult.h>
10 inline Int_t to_int(
const T& t)
13 std::stringstream ss(t);
19 inline Double_t to_double(
const T& t)
22 std::stringstream ss(t);
28 void ftpc_sqldraw(std::string dbpath =
"Conditions_ftpc/ftpcTemps/extra1East", std::string minTime =
"2009-06-16 00:00:00",
29 std::string maxTime =
"2009-06-29 23:59:59") {
31 std::string::size_type pos = dbpath.find_last_not_of(
"/");
32 if ( pos == (dbpath.length()-1) ) {
36 std::stringstream ss(dbpath);
39 std::string mDatabase, mTable, mParam;
40 std::getline(ss, mDatabase,
'/');
41 std::getline(ss, mTable,
'/');
42 std::getline(ss, mParam,
'/');
44 std::string dsn =
"mysql://onldb2.starp.bnl.gov:3502/";
46 dsn.append(mDatabase);
48 db = TSQLServer::Connect(dsn.c_str(),
"staruser",
"");
50 std::cerr <<
"Error: cannot connect to database!" << std::endl;
56 std::string sql =
"SELECT MIN(UNIX_TIMESTAMP(beginTime)), MAX(UNIX_TIMESTAMP(beginTime)), COUNT(beginTime), MIN(";
58 sql.append(
"), MAX(");
60 sql.append(
") FROM ");
62 sql.append(
" WHERE beginTime > '");
64 sql.append(
"' AND beginTime < '");
68 res = db->Query(sql.c_str());
69 int nrows = res->GetRowCount();
71 std::err <<
"Error: no entries for selected period!" << std::endl;
77 if (row->GetField(0) == NULL || row->GetField(1) == NULL || row->GetField(2) == NULL || row->GetField(3) == NULL || row->GetField(4) == NULL) {
78 std::cerr <<
"Error: count query returned null, probably no entries for selected period" << std::endl;
82 Int_t timeMin = to_int(row->GetField(0));
83 Int_t timeMax = to_int(row->GetField(1));
84 Int_t timeDiff = timeMax - timeMin;
85 Int_t timeNbins = to_int(row->GetField(2));
89 Double_t valMin = 20.0;
90 Double_t valMax = 30.0;
98 h1title.append(mParam);
100 h1title.append(mDatabase);
102 h1title.append(mTable);
104 h1title.append(
" vs. beginTime");
106 gStyle->SetOptStat(0);
108 TH2D* h1 =
new TH2D(
"th2d", h1title.c_str(), timeNbins, 0, timeDiff+1, 100, valMin - fabs(valMin*0.01), valMax + fabs(valMax*0.01));
110 h1->SetTitle(h1title.c_str());
111 h1->GetXaxis()->SetTimeOffset(dh.Convert());
112 h1->GetXaxis()->SetTimeDisplay(1);
113 h1->GetXaxis()->SetTimeFormat(
"#splitline{%Y-%m-%d}{%H:%M:%S}");
114 h1->GetXaxis()->SetLabelSize(0.02);
115 h1->GetXaxis()->SetLabelOffset(0.02);
116 h1->SetMarkerSize(0.8);
117 h1->SetMarkerStyle(20);
118 h1->SetMarkerColor(4);
120 std::string sql =
"SELECT UNIX_TIMESTAMP(beginTime), ";
122 sql.append(
" FROM ");
124 sql.append(
" WHERE beginTime > '");
126 sql.append(
"' AND beginTime < '");
128 sql.append(
"' ORDER BY beginTime ASC");
130 res = db->Query(sql.c_str());
132 nrows = res->GetRowCount();
133 std::cout <<
"Got " << nrows <<
" rows from database, please wait...\n";
135 std::cout <<
"[found many rows, it may take a few minutes to create histogram]" << std::endl;
139 while ((row = res->Next())) {
142 std::cout <<
" working on " << i <<
"th row \n";
144 h1->Fill(to_int(row->GetField(0)) - timeMin, to_double(row->GetField(1)));