StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
getAvgExtraTemps.C
1 // Averages the requested FTPC extra temperature reading for the requested time period
2 // All readings below minValueForAverage are rejected
3 // Run twice:
4 // The first time with minValueForAverage = minGasTemperature
5 // The second time with minValueForAverage = minumum temperature according to
6 // temperature plot
7 
8 #include <string>
9 #include <iostream>
10 #include <sstream>
11 
12 #include <TSQLServer.h>
13 #include <TSQLResult.h>
14 #include <TSQLRow.h>
15 
16 template <class T>
17 inline Int_t to_int(const T& t)
18 {
19  Int_t res;
20  std::stringstream ss(t);
21  ss >> res;
22  return res;
23 }
24 
25 template <class T>
26 inline Double_t to_double(const T& t)
27 {
28  Double_t res;
29  std::stringstream ss(t);
30  ss >> res;
31  return res;
32 }
33 
34 
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) {
37 
38  std::string::size_type pos = dbpath.find_last_not_of("/");
39  if ( pos == (dbpath.length()-1) ) {
40  dbpath.append("/");
41  }
42 
43  std::stringstream ss(dbpath);
44  std::string s;
45 
46  std::string mDatabase, mTable, mParam;
47  std::getline(ss, mDatabase, '/');
48  std::getline(ss, mTable, '/');
49  std::getline(ss, mParam, '/');
50 
51  std::string dsn = "mysql://onldb2.starp.bnl.gov:3502/"; // online
52  //std::string dsn = "mysql://heston.star.bnl.gov:3502/"; // offline
53 
54  dsn.append(mDatabase);
55  TSQLServer *db = 0;
56  db = TSQLServer::Connect(dsn.c_str(),"staruser", "");
57  if (!db) {
58  std::cerr << "Error: cannot connect to database!" << std::endl;
59  }
60 
61  TSQLRow *row;
62  TSQLResult *res;
63 
64  std::string sql = "SELECT MIN(UNIX_TIMESTAMP(beginTime)), MAX(UNIX_TIMESTAMP(beginTime)), COUNT(beginTime), MIN(";
65  sql.append(mParam);
66  sql.append("), MAX(");
67  sql.append(mParam);
68  sql.append(") FROM ");
69  sql.append(mTable);
70  sql.append(" WHERE beginTime > '");
71  sql.append(minTime);
72  sql.append("' AND beginTime < '");
73  sql.append(maxTime);
74  sql.append("'");
75 
76 
77  res = db->Query(sql.c_str());
78  int nrows = res->GetRowCount();
79  if (nrows <= 0) {
80  std::err << "Error: no entries for selected period!" << std::endl;
81  return;
82  }
83 
84  row = res->Next();
85 
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;
88  return;
89  }
90 
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;
96 
97 // Double_t valMin = to_double(row->GetField(3));
98 // Double_t valMax = to_double(row->GetField(4));
99  Double_t valMin = 20.0;
100  Double_t valMax = 35.0;
101 
102  TDatime dh(timeMin);
103 
104  delete row;
105  delete res;
106 
107  std::string h1title;
108  h1title.append(mParam);
109  h1title.append("(");
110  h1title.append(mDatabase);
111  h1title.append("/");
112  h1title.append(mTable);
113  h1title.append(")");
114  h1title.append(" vs. beginTime");
115 
116  gStyle->SetOptStat(0);
117 
118  TH2D* h1 = new TH2D("th2d", h1title.c_str(), timeNbins, 0, timeDiff+1, 100, valMin - fabs(valMin*0.01), valMax + fabs(valMax*0.01));
119  h1->SetStats(0);
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);
129 
130  std::string sql = "SELECT UNIX_TIMESTAMP(beginTime), ";
131  sql.append(mParam);
132  sql.append(" FROM ");
133  sql.append(mTable);
134  sql.append(" WHERE beginTime > '");
135  sql.append(minTime);
136  sql.append("' AND beginTime < '");
137  sql.append(maxTime);
138  sql.append("' ORDER BY beginTime ASC");
139 
140  res = db->Query(sql.c_str());
141 
142  nrows = res->GetRowCount();
143  std::cout << "Got " << nrows << " rows from database, please wait...\n";
144  if (nrows > 500) {
145  std::cout << "[found many rows, it may take a few minutes to create histogram]" << std::endl;
146  }
147 
148  int i = 0;
149  int ngood = 0;
150  int nreject = 0;
151  Double_t extraAverage = 0;
152  while ((row = res->Next())) {
153  i++;
154  if (i%100 == 0) {
155  std::cout << " working on " << i << "th row \n";
156  }
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;
161  ngood++;
162  }
163  else {
164  cout<<"i = "<<i<<" extra temp "<<row->GetField(1)<<" rejected"<<endl;
165  nreject++;
166  }
167  }
168 
169  cout<<mParam<<" = ("<<extraAverage<<"/ ngood = "<<ngood<<") = "<<extraAverage/ngood<<endl;
170  cout<<"Rejected "<<nreject<<" readings lower than "<<minValueForAverage<<endl;
171  delete res;
172  delete db;
173 
174  h1->Draw("L");
175 
176 }
177