ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PgPostBankManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PgPostBankManager.cc
1 #include "PgPostBankManager.h"
2 #include "PgPostApplication.h"
3 #include "PgPostBankWrapper.h"
5 
6 #include <pdbcalbase/PdbBankID.h>
7 #include <pdbcalbase/PdbBankManagerFactory.h>
8 #include <pdbcalbase/PdbCalBank.h>
9 #include <pdbcalbase/PdbClassMap.h>
10 #include <pdbcalbase/PHGenericFactoryT.h>
11 #include <pdbcalbase/RunToTime.h>
12 #include <pdbcalbase/PdbBankManager.h>
13 
14 #include <phool/PHObject.h> // for PHObject
15 #include <phool/PHTimeStamp.h> // for PHTimeStamp, operator<<
16 #include <phool/phool.h>
17 
18 #include <RDBC/TSQLConnection.h>
19 #include <RDBC/TSQLResultSet.h>
20 #include <RDBC/TSQLStatement.h>
21 
22 #include <TString.h>
23 
24 #include <cstdlib>
25 #include <ctime>
26 #include <iostream>
27 #include <memory>
28 #include <sstream>
29 
30 class PdbApplication;
31 class PdbCalBankIterator;
32 
33 using namespace std;
34 
35 namespace
36 {
37 PdbBankManager *singletonCreator()
38 {
39  // rememeber that this will not neccessarily return a
40  // pointer to the singleton PgPostBankManager. If
41  // an Objy application is instantiated, it will return 0.
43 }
44 
45 const std::string name = "Pg";
46 const bool registered =
47  PdbBankManagerFactory::instance().registerCreator(name, singletonCreator, "PdbBankManager");
48 } // namespace
49 
51 
53 {
54  return mySpecificCopy;
55 }
56 
58 {
59  if (__instance) return -1;
60  mySpecificCopy = new PgPostBankManager();
61  __instance = mySpecificCopy;
62  return 0;
63 }
64 
66 {
67  tMaxInsertTime.setToFarFuture();
68 #ifdef DEBUG
69  cout << PHWHERE << endl;
70 #endif
71 }
72 
74 {
75  mySpecificCopy = 0;
76 }
77 
80 {
81  return new PgPostCalBankIterator(*this);
82 }
83 
84 PdbCalBank *
85 PgPostBankManager::createBank(const int beginRunNumber, const int endRunNumber, const string &className, PdbBankID bankID, const string &description, const string &bankName)
86 {
87  RunToTime *runTime = RunToTime::instance();
88 
89  PHTimeStamp *beginRunTime = runTime->getBeginTime(beginRunNumber);
90  if (beginRunTime != 0)
91  {
92  PHTimeStamp startTime = *(beginRunTime);
93 
94  PHTimeStamp *endRunTime = runTime->getEndTime(endRunNumber);
95  if (endRunTime != 0)
96  {
97  PHTimeStamp endTime = *(endRunTime);
98  delete beginRunTime;
99  delete endRunTime;
100  if (startTime >= endTime)
101  {
102  cout << PHWHERE << "Bad Start/EndRun Time: Start Time: "
103  << startTime << " >= End Time: "
104  << endTime << endl;
105  return 0;
106  }
107  return createBank(className, bankID, description, startTime, endTime, bankName);
108  }
109  else
110  {
111  delete beginRunTime;
112  cout << PHWHERE << "endTime = 0" << endl;
113  return 0;
114  }
115  delete beginRunTime;
116  }
117  else
118  {
119  cout << PHWHERE << "beginTime = 0" << endl;
120  }
121  return 0;
122 }
123 
124 PdbCalBank *
125 PgPostBankManager::createBank(const int runNumber, const string &className, PdbBankID bankID, const string &description, const string &bankName, const time_t duration)
126 {
127  RunToTime *runTime = RunToTime::instance();
128 
129  PHTimeStamp *runBeginTime = runTime->getBeginTime(runNumber);
130  if (runBeginTime != 0)
131  {
132  PHTimeStamp startTime = *(runBeginTime);
133  PHTimeStamp endTime = startTime;
134 
135  if (duration == 0)
136  {
137  // duration == 0 is "flag" for end of unix time!!
138  endTime = PHTimeStamp(2038, 1, 17, 0, 0, 0);
139  }
140  else
141  {
142  endTime += duration;
143  }
144  delete runBeginTime;
145  return createBank(className, bankID, description, startTime, endTime, bankName);
146  }
147  return 0;
148 }
149 
150 PdbCalBank *
151 PgPostBankManager::createBank(const string &className, PdbBankID bankID, const string &descr, PHTimeStamp &tStart, PHTimeStamp &tStop, const string &tablename)
152 {
153  string realName = getRealName(className);
154  const char *rName = realName.c_str();
156  if (classMap->find(rName) != classMap->end())
157  {
158  PdbCalBank *b = (*classMap)[rName];
159  PdbCalBank *b1 = dynamic_cast<PdbCalBank *> (b->CloneMe());
160  PgPostBankWrapper *bw = new PgPostBankWrapper(b1);
161  bw->setBankID(bankID.getInternalValue());
162  PHTimeStamp ts;
163  bw->setInsertTime(ts);
164  bw->setStartValTime(tStart);
165  bw->setEndValTime(tStop);
166  bw->setDescription(descr);
167  bw->setUserName("pdbcal");
168  bw->setTableName(tablename);
169  return bw;
170  }
171  else
172  {
173  std::cerr << PHWHERE << "\t NO BANK " << rName
174  << " IN THE MAP" << std::endl;
175  return 0;
176  }
177 }
178 
179 PdbCalBank *
180 PgPostBankManager::fetchBank(const string &className, PdbBankID bankID, const string &bankName, const int runNumber)
181 {
182  RunToTime *runTime = RunToTime::instance();
183 
184  PHTimeStamp *runBeginTime = runTime->getBeginTime(runNumber);
185  if (runBeginTime != 0)
186  {
187  PHTimeStamp searchTime = *(runBeginTime);
188  delete runBeginTime;
189  return fetchBank(className, bankID, bankName, searchTime);
190  }
191  return 0;
192 }
193 
194 PdbCalBank *
195 PgPostBankManager::fetchClosestBank(const string &className, PdbBankID bankID, const string &bankName, const int runNumber)
196 {
197  RunToTime *runTime = RunToTime::instance();
198 
199  PHTimeStamp *runBeginTime = runTime->getBeginTime(runNumber);
200  if (runBeginTime != 0)
201  {
202  PHTimeStamp searchTime = *(runBeginTime);
203  delete runBeginTime;
204  return fetchClosestBank(className, bankID, bankName, searchTime);
205  }
206  return 0;
207 }
208 
209 // void PgPostBankManager::fetchAllBanks(PdbBankList & bankList, const string &className, PdbBankID bankID, const string &bankName, const int runNumber)
210 // {
211 // RunToTime *runTime = RunToTime::instance();
212 
213 // PHTimeStamp *runBeginTime = runTime->getBeginTime(runNumber);
214 // if (runBeginTime != 0)
215 // {
216 // PHTimeStamp searchTime = *(runBeginTime);
217 // delete runBeginTime;
218 // fetchAllBanks(bankList, className, bankID, bankName, searchTime);
219 // }
220 
221 // }
222 
223 // void PgPostBankManager::fetchAllBanks(PdbBankList & bankList, const string &className, const string &bankName, const int runNumber)
224 // {
225 // RunToTime *runTime = RunToTime::instance();
226 
227 // PHTimeStamp *runBeginTime = runTime->getBeginTime(runNumber);
228 // if (runBeginTime != 0)
229 // {
230 // PHTimeStamp searchTime = *(runBeginTime);
231 // delete runBeginTime;
232 // fetchAllBanks(bankList, className, bankName, searchTime);
233 // }
234 // }
235 
236 //__________________________________________________________________________________
237 PdbCalBank *PgPostBankManager::fetchBank(const string &/*className*/, PdbBankID bankID, const string &bankName, const PHTimeStamp &searchTime)
238 {
239 #ifdef DEBUG
240  cout << "Fetching " << className << " from " << bankName << endl;
241 #endif
242 
244  if (!ap)
245  {
246  cout << PHWHERE << " PgPostApplication instance is nullptr, exiting" << endl;
247  exit(1);
248  }
249 
250  TSQLConnection *con = ap->getConnection();
251  if (!con)
252  {
253  cout << PHWHERE << " Cannot get TSQLConnection, exiting" << endl;
254  exit(1);
255  }
256 
257  TSQLStatement *stmt = con->CreateStatement();
258  time_t sT = searchTime.getTics();
259  std::ostringstream tem;
260  std::ostringstream t2;
261 
262  // cout << bankID.getInternalValue() << endl;
263  t2 << "select * from " << bankName
264  << " where bankID = " << bankID.getInternalValue()
265  << " and startvaltime <= " << sT
266  << " and endvaltime > " << sT;
267 
268  tem << "select * from ("
269  << t2.str()
270  << ") as foo where inserttime = "
271  << "(select max(inserttime) from ("
272  << t2.str()
273  << " and inserttime <= "
274  << tMaxInsertTime.getTics()
275  << ") as foobar)"
276  << " order by rid desc";
277 
278 #ifdef DEBUG
279  cout << "exe : " << tem.str() << endl;
280 #endif
281 
282  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
283  if ((rs) && rs->Next())
284  {
285  PdbCalBank *bank = (PdbCalBank *) (rs->GetObject(7));
286  PgPostBankWrapper *bw = new PgPostBankWrapper(bank);
287  bw->setBankID(rs->GetInt(1));
288  bw->setInsertTime(rs->GetLong(2));
289  bw->setStartValTime(rs->GetLong(3));
290  bw->setEndValTime(rs->GetLong(4));
291  bw->setDescription(string(rs->GetString(5)));
292  bw->setUserName(string(rs->GetString(6)));
293  bw->setTableName(bankName);
294 #ifdef DEBUG
295  bw->printHeader();
296 #endif
297  int rid = rs->GetInt("rid");
298 
299  // insert new id in bank list matching name
300  /*
301  Remark: when the key "a" is not already in the map, it is inserted automatically by the call below,
302  using the default constructor of the object associated to the key, here std::set<int>
303  */
304  BankRid[bankName].insert(rid);
305  return bw;
306  }
307  else
308  {
309  std::cerr << PHWHERE << "NO Bank found : " << tem.str() << std::endl;
310  return 0;
311  }
312 }
313 
314 //__________________________________________________________________________________
315 PdbCalBank *
316 PgPostBankManager::fetchClosestBank(const string &/*className*/, PdbBankID /*bankID*/, const string &/*bankName*/, PHTimeStamp &/*searchTime*/)
317 {
318  cout << PHWHERE << " PdbBankManager::fetchClosestBank: This method is not implemented" << endl;
319  exit(1);
320 }
321 
322 // void PgPostBankManager::fetchAllBanks(PdbBankList & bankList, const string &className, PdbBankID bankID, const string &bankName, PHTimeStamp &searchTime)
323 // {
324 // #ifdef DEBUG
325 // cout << "Fetching " << className << " from " << bankName << endl;
326 // #endif
327 
328 // std::string tablename = getTableName(bankName);
329 
330 // TSQLResultSet *rs;
331 // PgPostBankWrapper *bw;
332 
333 // PgPostApplication *ap = PgPostApplication::instance();
334 // if (!ap)
335 // {
336 // cout << PHWHERE << " PgPostApplication instance is nullptr, exiting" << endl;
337 // exit(1);
338 // }
339 // TSQLConnection *con = ap->getConnection();
340 // if (!con)
341 // {
342 // cout << PHWHERE << " Cannot get TSQLConnection, exiting" << endl;
343 // exit(1);
344 // }
345 // TSQLStatement* stmt = con->CreateStatement();
346 // if (!stmt)
347 // {
348 // cout << PHWHERE << " Cannot create TSQL statement, exiting" << endl;
349 // exit(1);
350 // }
351 
352 // time_t sT = searchTime.getTics();
353 // ostringstream tem;
354 // tem << "select * from " << tablename << " where bankID = "
355 // << bankID.getInternalValue() << " and startvaltime <= "
356 // << sT << " and endvaltime > " << sT;
357 // cout << "exe: " << tem.str() << endl;
358 // rs = stmt->ExecuteQuery(tem.str());
359 // while (rs->Next())
360 // {
361 // bw = static_cast<PgPostBankWrapper*>(rs->GetObject(7));
362 // #ifdef DEBUG
363 // bw->printHeader();
364 // #endif
365 // bankList.append(bw);
366 // }
367 // cout << "bankList len= " << bankList.length() << endl;
368 // }
369 
370 // void PgPostBankManager::fetchAllBanks(PdbBankList & bankList, const string &className, const string &bankName, PHTimeStamp &searchTime)
371 // {
372 // #ifdef DEBUG
373 // cout << "Fetching " << className << " from " << bankName << endl;
374 // #endif
375 
376 // std::string tablename = getTableName(bankName);
377 
378 // TSQLResultSet *rs;
379 // PgPostBankWrapper *bw;
380 
381 // PgPostApplication *ap = PgPostApplication::instance();
382 // if (!ap)
383 // {
384 // cout << PHWHERE << " PgPostApplication instance is nullptr, exiting" << endl;
385 // exit(1);
386 // }
387 
388 // TSQLConnection *con = ap->getConnection();
389 // if (!con)
390 // {
391 // cout << PHWHERE << " Cannot get TSQLConnection, exiting" << endl;
392 // exit(1);
393 // }
394 
395 // TSQLStatement* stmt = con->CreateStatement();
396 // if (!stmt)
397 // {
398 // cout << PHWHERE << " Cannot create TSQL statement, exiting" << endl;
399 // exit(1);
400 // }
401 
402 // time_t sT = searchTime.getTics();
403 // ostringstream tem;
404 // tem << "select * from " << tablename << " where startvaltime <= "
405 // << sT << " and endvaltime > " << sT;
406 // cout << "exe: " << tem.str() << endl;
407 // rs = stmt->ExecuteQuery(tem.str());
408 // while (rs->Next())
409 // {
410 // bw = static_cast<PgPostBankWrapper*>(rs->GetObject(7));
411 // #ifdef DEBUG
412 // bw->printHeader();
413 // #endif
414 // bankList.append(bw);
415 // }
416 // cout << "bankList len= " << bankList.length() << endl;
417 // }
418 
420 {
422 }
423 
424 string
425 PgPostBankManager::getRealName(const string &searchName)
426 {
427  string realName = searchName;
428  string pdbsubstring = "Pdb";
429  realName.replace(realName.find(pdbsubstring), realName.find(pdbsubstring) + pdbsubstring.size(), "PgPost");
430  return realName;
431 }
432 
433 void PgPostBankManager::GetUsedBankRids(map<string, set<int> > &usedbanks) const
434 {
435  usedbanks = BankRid;
436  // map<string,set<int> >::const_iterator bankiter;
437  // for (bankiter = usedbanks.begin(); bankiter != usedbanks.end(); bankiter++)
438  // {
439  // cout << "BankName: " << bankiter->first << endl;
440  // set<int>:: const_iterator siter;
441  // for (siter = (bankiter->second).begin(); siter != (bankiter->second).end(); siter++)
442  // {
443  // cout << "rid: " << *siter << endl;
444  // }
445  // }
446  return;
447 }
448 
450 {
451  cout << "Setting latest inserttime for calibrations to " << tMax
452  << " (" << tMax.getTics() << ")" << endl;
453  tMaxInsertTime = tMax;
454  return;
455 }