Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RunToTimePg.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RunToTimePg.cc
1 #include "RunToTimePg.h"
2 
3 #include <pdbcalbase/RunToTime.h> // for RunToTime::__instance
4 
5 #include <phool/PHTimeStamp.h>
6 #include <phool/phool.h>
7 
8 #include <odbc++/connection.h>
9 #include <odbc++/drivermanager.h>
10 #include <odbc++/resultset.h>
11 #include <odbc++/statement.h>
12 #include <odbc++/types.h>
13 
14 #include <cstdlib>
15 #include <ctime>
16 #include <iostream>
17 #include <iterator> // for reverse_iterator
18 #include <sstream>
19 #include <string>
20 #include <utility> // for pair
21 
22 using namespace odbc;
23 using namespace std;
24 
25 // cache entries for 10 runs which should be sufficient for everybody
26 unsigned int maxentries = 10;
27 
29 
31 {
32  con = nullptr;
33  return;
34 }
35 
37 {
38  mySpecificCopy = nullptr;
39  __instance = nullptr;
40  while (beginruntimes.begin() != beginruntimes.end())
41  {
42  delete beginruntimes.begin()->second;
43  beginruntimes.erase(beginruntimes.begin());
44  }
45  while (endruntimes.begin() != endruntimes.end())
46  {
47  delete endruntimes.begin()->second;
48  endruntimes.erase(endruntimes.begin());
49  }
50  return;
51 }
52 
54 {
55  if (__instance)
56  {
57  return -1;
58  }
59  mySpecificCopy = new RunToTimePg();
60  __instance = mySpecificCopy;
61  return 0;
62 }
63 
65 {
66  if (!con)
67  {
68  try
69  {
70  con = DriverManager::getConnection("daq", "phnxrc", "");
71  }
72  catch (SQLException& e)
73  {
74  cout << PHWHERE
75  << " Fatal Exception caught during DriverManager::getConnection" << endl;
76  cout << "Message: " << e.getMessage() << endl;
77  exit(1);
78  }
79  }
80  return 0;
81 }
82 
84 {
85  delete con;
86  con = nullptr;
87  return 0;
88 }
89 
91 RunToTimePg::getTime(const int runNumber, const string& what)
92 {
93  PHTimeStamp* whatTime = nullptr;
94  // Establish connection to Postgres...
95  GetConnection(); // on error this method will exit
96  Statement* stmt = con->createStatement();
97 
98  std::ostringstream cmd;
99  cmd << "select brunixtime,erunixtime,updateunixtime from run where runnumber = " << runNumber
100  << " limit 1";
101 
102 #ifdef DEBUG
103 
104  cout << cmd.str() << endl;
105 #endif
106 
107  // Get results of search...
108  ResultSet* rs = nullptr;
109  try
110  {
111  rs = stmt->executeQuery(cmd.str());
112  }
113  catch (SQLException& e)
114  {
115  cout << "Fatal Exception caught during stmt->executeQuery(" << cmd.str() << ")" << endl;
116  cout << "Message: " << e.getMessage() << endl;
117  exit(1);
118  }
119 
120  // Fill stl maps with timestamps
121  if (rs->next())
122  {
123  // if the maps grow too large we need to ditch them (e.g. a process in the online monitoring
124  // which runs over a lot of runs. Chances are the last entry is the one for the current run
125  // and this one might be reused. Chances are the lower run(s) have some special meaning
126  // (forcing the use of a special calibration) and might be reused as well. So we remove the second
127  // largest runnumber in the list
128  if (beginruntimes.size() > maxentries)
129  {
130  // this picks the last run
131  map<const int, PHTimeStamp*>::reverse_iterator iter = beginruntimes.rbegin();
132  // go to the run before
133  ++iter;
134  int delrun = iter->first;
135  delete iter->second;
136  beginruntimes.erase(delrun);
137  map<const int, PHTimeStamp*>::iterator iter2;
138  iter2 = endruntimes.find(delrun);
139  if (iter2 != endruntimes.end())
140  {
141  delete iter2->second;
142  endruntimes.erase(iter2);
143  }
144  }
145  // begin run time
146  try
147  {
148  whatTime = new PHTimeStamp(rs->getInt("brunixtime"));
149  }
150  catch (SQLException& e)
151  {
152  cout << "Fatal Exception caught during rs->getInt(\"brunixtime\")" << endl;
153  cout << "Message: " << e.getMessage() << endl;
154  exit(1);
155  }
156  beginruntimes[runNumber] = whatTime;
157  // end run time
158  try
159  {
160  unsigned int eruntics = rs->getInt("erunixtime");
161  if (eruntics == 0)
162 
163  {
164  eruntics = rs->getInt("updateunixtime");
165  }
166  whatTime = new PHTimeStamp(eruntics);
167  }
168  catch (SQLException& e)
169  {
170  cout << "Fatal Exception caught during rs->getInt(\"brunixtime\")" << endl;
171  cout << "Message: " << e.getMessage() << endl;
172  exit(1);
173  }
174  endruntimes[runNumber] = whatTime;
175  if (what == "brunixtime")
176  {
177  delete rs;
178  return beginruntimes[runNumber];
179  }
180  else if (what == "erunixtime")
181  {
182  delete rs;
183  return endruntimes[runNumber];
184  }
185  else
186  {
187  cout << "invalid time selection " << what << endl;
188  exit(1);
189  }
190  }
191  delete rs;
192  return 0;
193 }
194 
196 RunToTimePg::getBeginTime(const int runNumber)
197 {
198  PHTimeStamp* BeginRunTime;
199  map<const int, PHTimeStamp*>::const_iterator iter = beginruntimes.find(runNumber);
200  if (iter == beginruntimes.end())
201  {
202  BeginRunTime = getTime(runNumber, "brunixtime");
203  }
204  else
205  {
206  BeginRunTime = iter->second;
207  }
208  if (!BeginRunTime)
209  {
210  return nullptr;
211  }
212  PHTimeStamp* TS = new PHTimeStamp(*BeginRunTime);
213  return TS;
214 }
215 
217 RunToTimePg::getEndTime(const int runNumber)
218 {
219  PHTimeStamp* EndRunTime;
220  map<const int, PHTimeStamp*>::const_iterator iter = endruntimes.find(runNumber);
221  if (iter == endruntimes.end())
222  {
223  EndRunTime = getTime(runNumber, "erunixtime");
224  }
225  else
226  {
227  EndRunTime = iter->second;
228  }
229  if (!EndRunTime)
230  {
231  return nullptr;
232  }
233  PHTimeStamp* TS = new PHTimeStamp(*EndRunTime);
234  return TS;
235 }
236 
238 {
239  GetConnection();
240  Statement* stmt = con->createStatement();
241 
242  // Make search string...
243  std::ostringstream cmd;
244 
245  Timestamp timestp(ts.getTics());
246 
247  time_t tics = ts.getTics();
248 
249  cmd << "select runnumber from run where brunixtime <= " << tics
250  << " order by runnumber desc limit 1";
251 
252  // Get results of search...
253  ResultSet* rs = nullptr;
254  try
255  {
256  rs = stmt->executeQuery(cmd.str().c_str());
257  }
258  catch (SQLException& e)
259  {
260  cout << "Fatal Exception caught during stmt->executeQuery(" << cmd.str() << ")" << endl;
261  cout << "Message: " << e.getMessage() << endl;
262  exit(1);
263  }
264 
265  int runnumber = -1;
266  time_t eruntics = 0;
267  // Set runnumber based upon return from
268  if (rs->next())
269  {
270  runnumber = rs->getInt("runnumber");
271  }
272  delete rs;
273  if (runnumber == -1)
274  {
275  goto cleanup;
276  }
277 
278  cmd.str("");
279  cmd << "select erunixtime from run where runnumber = " << runnumber;
280 
281  try
282  {
283  rs = stmt->executeQuery(cmd.str().c_str());
284  }
285  catch (SQLException& e)
286  {
287  cout << "Fatal Exception caught during stmt->executeQuery(" << cmd.str() << ")" << endl;
288  cout << "Message: " << e.getMessage() << endl;
289  exit(1);
290  }
291 
292  // Set runnumber based upon return from
293  if (rs->next())
294  {
295  eruntics = rs->getInt("erunixtime");
296  if (eruntics > 0 && eruntics < tics)
297  {
298  cout << "Timestamp " << tics
299  << " not covered by any run, closest smaller begin run time is from run "
300  << runnumber << endl;
301  runnumber = -1;
302  }
303  }
304  delete rs;
305 
306 cleanup:
307  return runnumber;
308 }