StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ftpc_sqldraw.C
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 
5 #include <TSQLServer.h>
6 #include <TSQLResult.h>
7 #include <TSQLRow.h>
8 
9 template <class T>
10 inline Int_t to_int(const T& t)
11 {
12  Int_t res;
13  std::stringstream ss(t);
14  ss >> res;
15  return res;
16 }
17 
18 template <class T>
19 inline Double_t to_double(const T& t)
20 {
21  Double_t res;
22  std::stringstream ss(t);
23  ss >> res;
24  return res;
25 }
26 
27 
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") {
30 
31  std::string::size_type pos = dbpath.find_last_not_of("/");
32  if ( pos == (dbpath.length()-1) ) {
33  dbpath.append("/");
34  }
35 
36  std::stringstream ss(dbpath);
37  std::string s;
38 
39  std::string mDatabase, mTable, mParam;
40  std::getline(ss, mDatabase, '/');
41  std::getline(ss, mTable, '/');
42  std::getline(ss, mParam, '/');
43 
44  std::string dsn = "mysql://onldb2.starp.bnl.gov:3502/"; // online
45  //std::string dsn = "mysql://heston.star.bnl.gov:3502/"; // offline
46  dsn.append(mDatabase);
47  TSQLServer *db = 0;
48  db = TSQLServer::Connect(dsn.c_str(),"staruser", "");
49  if (!db) {
50  std::cerr << "Error: cannot connect to database!" << std::endl;
51  }
52 
53  TSQLRow *row;
54  TSQLResult *res;
55 
56  std::string sql = "SELECT MIN(UNIX_TIMESTAMP(beginTime)), MAX(UNIX_TIMESTAMP(beginTime)), COUNT(beginTime), MIN(";
57  sql.append(mParam);
58  sql.append("), MAX(");
59  sql.append(mParam);
60  sql.append(") FROM ");
61  sql.append(mTable);
62  sql.append(" WHERE beginTime > '");
63  sql.append(minTime);
64  sql.append("' AND beginTime < '");
65  sql.append(maxTime);
66  sql.append("'");
67 
68  res = db->Query(sql.c_str());
69  int nrows = res->GetRowCount();
70  if (nrows <= 0) {
71  std::err << "Error: no entries for selected period!" << std::endl;
72  return;
73  }
74 
75  row = res->Next();
76 
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;
79  return;
80  }
81 
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));
86 
87 // Double_t valMin = to_double(row->GetField(3));
88 // Double_t valMax = to_double(row->GetField(4));
89  Double_t valMin = 20.0;
90  Double_t valMax = 30.0;
91 
92  TDatime dh(timeMin);
93 
94  delete row;
95  delete res;
96 
97  std::string h1title;
98  h1title.append(mParam);
99  h1title.append("(");
100  h1title.append(mDatabase);
101  h1title.append("/");
102  h1title.append(mTable);
103  h1title.append(")");
104  h1title.append(" vs. beginTime");
105 
106  gStyle->SetOptStat(0);
107 
108  TH2D* h1 = new TH2D("th2d", h1title.c_str(), timeNbins, 0, timeDiff+1, 100, valMin - fabs(valMin*0.01), valMax + fabs(valMax*0.01));
109  h1->SetStats(0);
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);
119 
120  std::string sql = "SELECT UNIX_TIMESTAMP(beginTime), ";
121  sql.append(mParam);
122  sql.append(" FROM ");
123  sql.append(mTable);
124  sql.append(" WHERE beginTime > '");
125  sql.append(minTime);
126  sql.append("' AND beginTime < '");
127  sql.append(maxTime);
128  sql.append("' ORDER BY beginTime ASC");
129 
130  res = db->Query(sql.c_str());
131 
132  nrows = res->GetRowCount();
133  std::cout << "Got " << nrows << " rows from database, please wait...\n";
134  if (nrows > 500) {
135  std::cout << "[found many rows, it may take a few minutes to create histogram]" << std::endl;
136  }
137 
138  int i = 0;
139  while ((row = res->Next())) {
140  i++;
141  if (i%100 == 0) {
142  std::cout << " working on " << i << "th row \n";
143  }
144  h1->Fill(to_int(row->GetField(0)) - timeMin, to_double(row->GetField(1)));
145  }
146 
147  delete res;
148  delete db;
149 
150  h1->Draw("L");
151 
152 }
153