12 #include <TSQLServer.h>
13 #include <TSQLResult.h>
17 inline Int_t to_int(
const T& t)
20 std::stringstream ss(t);
26 inline Double_t to_double(
const T& t)
29 std::stringstream ss(t);
35 void getAvgExtraTemps(std::string dbpath =
"Conditions_ftpc/ftpcTemps/extra1East", std::string minTime =
"2009-06-16 00:00:00",
36 std::string maxTime =
"2009-06-29 23:59:59", Double_t minValueForAverage = 20.0) {
38 std::string::size_type pos = dbpath.find_last_not_of(
"/");
39 if ( pos == (dbpath.length()-1) ) {
43 std::stringstream ss(dbpath);
46 std::string mDatabase, mTable, mParam;
47 std::getline(ss, mDatabase,
'/');
48 std::getline(ss, mTable,
'/');
49 std::getline(ss, mParam,
'/');
51 std::string dsn =
"mysql://onldb2.starp.bnl.gov:3502/";
54 dsn.append(mDatabase);
56 db = TSQLServer::Connect(dsn.c_str(),
"staruser",
"");
58 std::cerr <<
"Error: cannot connect to database!" << std::endl;
64 std::string sql =
"SELECT MIN(UNIX_TIMESTAMP(beginTime)), MAX(UNIX_TIMESTAMP(beginTime)), COUNT(beginTime), MIN(";
66 sql.append(
"), MAX(");
68 sql.append(
") FROM ");
70 sql.append(
" WHERE beginTime > '");
72 sql.append(
"' AND beginTime < '");
77 res = db->Query(sql.c_str());
78 int nrows = res->GetRowCount();
80 std::err <<
"Error: no entries for selected period!" << std::endl;
86 if (row->GetField(0) == NULL || row->GetField(1) == NULL || row->GetField(2) == NULL || row->GetField(3) == NULL || row->GetField(4) == NULL) {
87 std::cerr <<
"Error: count query returned null, probably no entries for selected period" << std::endl;
91 Int_t timeMin = to_int(row->GetField(0));
92 Int_t timeMax = to_int(row->GetField(1));
93 Int_t timeDiff = timeMax - timeMin;
94 Int_t timeNbins = to_int(row->GetField(2));
95 cout<<
"minTime ="<<minTime<<
" maxTime = "<<maxTime<<
" timeNbins = "<<timeNbins<<endl;
99 Double_t valMin = 20.0;
100 Double_t valMax = 35.0;
108 h1title.append(mParam);
110 h1title.append(mDatabase);
112 h1title.append(mTable);
114 h1title.append(
" vs. beginTime");
116 gStyle->SetOptStat(0);
118 TH2D* h1 =
new TH2D(
"th2d", h1title.c_str(), timeNbins, 0, timeDiff+1, 100, valMin - fabs(valMin*0.01), valMax + fabs(valMax*0.01));
120 h1->SetTitle(h1title.c_str());
121 h1->GetXaxis()->SetTimeOffset(dh.Convert());
122 h1->GetXaxis()->SetTimeDisplay(1);
123 h1->GetXaxis()->SetTimeFormat(
"#splitline{%Y-%m-%d}{%H:%M:%S}");
124 h1->GetXaxis()->SetLabelSize(0.02);
125 h1->GetXaxis()->SetLabelOffset(0.02);
126 h1->SetMarkerSize(0.8);
127 h1->SetMarkerStyle(20);
128 h1->SetMarkerColor(4);
130 std::string sql =
"SELECT UNIX_TIMESTAMP(beginTime), ";
132 sql.append(
" FROM ");
134 sql.append(
" WHERE beginTime > '");
136 sql.append(
"' AND beginTime < '");
138 sql.append(
"' ORDER BY beginTime ASC");
140 res = db->Query(sql.c_str());
142 nrows = res->GetRowCount();
143 std::cout <<
"Got " << nrows <<
" rows from database, please wait...\n";
145 std::cout <<
"[found many rows, it may take a few minutes to create histogram]" << std::endl;
151 Double_t extraAverage = 0;
152 while ((row = res->Next())) {
155 std::cout <<
" working on " << i <<
"th row \n";
157 if (to_double(row->GetField(1))>=minValueForAverage) {
158 h1->Fill(to_int(row->GetField(0)) - timeMin, to_double(row->GetField(1)));
159 extraAverage += to_double(row->GetField(1));
160 cout<<
"i = "<<i<<
" extra temp "<<row->GetField(1)<<endl;
164 cout<<
"i = "<<i<<
" extra temp "<<row->GetField(1)<<
" rejected"<<endl;
169 cout<<mParam<<
" = ("<<extraAverage<<
"/ ngood = "<<ngood<<
") = "<<extraAverage/ngood<<endl;
170 cout<<
"Rejected "<<nreject<<
" readings lower than "<<minValueForAverage<<endl;