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