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