Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnCalServer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file OnCalServer.cc
1 #include "OnCalServer.h"
2 #include "OnCal.h"
3 #include "OnCalDBCodes.h"
4 #include "OnCalHistoBinDefs.h"
5 
9 #include <fun4all/Fun4AllUtils.h>
10 #include <phool/recoConsts.h>
11 
12 #include <Event/Event.h>
13 #include <phool/PHCompositeNode.h>
14 #include <phool/PHNodeIOManager.h>
15 #include <phool/PHNodeIterator.h>
16 #include <phool/phool.h>
17 
18 #include <pdbcalbase/PdbApplication.h>
19 #include <pdbcalbase/PdbBankID.h>
20 #include <pdbcalbase/PdbBankList.h>
21 #include <pdbcalbase/PdbBankListIterator.h>
22 #include <pdbcalbase/PdbBankManager.h>
23 #include <pdbcalbase/PdbCalBank.h>
24 #include <pgcal/PgPostCalBank.h>
25 
26 #include <pdbcalbase/RunToTime.h>
27 
28 #include <TFile.h>
29 #include <TH1.h>
30 #include <TROOT.h>
31 #include <TSystem.h>
32 
33 // odbc++ classes
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
36 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
37 #include <odbc++/connection.h>
38 #include <odbc++/drivermanager.h>
40 #include <odbc++/resultset.h>
42 #pragma GCC diagnostic pop
43 
44 #include <boost/foreach.hpp>
45 
46 #include <sys/utsname.h>
47 #include <unistd.h>
48 #include <algorithm>
49 #include <cstdlib>
50 #include <fstream>
51 #include <iostream>
52 #include <list>
53 #include <map>
54 #include <memory>
55 #include <sstream>
56 
57 using namespace std;
58 using namespace odbc;
59 
60 const static string cvstag = "OnCalv86";
61 
63 
65 {
66  if (__instance)
67  {
68  OnCalServer *oncal = dynamic_cast<OnCalServer *>(__instance);
69  return oncal;
70  }
71  __instance = new OnCalServer();
72  OnCalServer *oncal = dynamic_cast<OnCalServer *>(__instance);
73  return oncal;
74 }
75 
76 //---------------------------------------------------------------------
77 
79  : Fun4AllServer(name)
80  , testmode(0)
81  , recordDB(false)
82  , SetEndTimeStampByHand(false)
83  , SetBeginTimeStampByHand(false)
84  , successTable("OnCal_status")
85  , runNum(0)
86  , nEvents(0)
87  , eventcheckfrequency(1000)
88  , database("calBookKeep")
89 {
92 
93  OnCalServerVars = new TH1D("OnCalServerVars", "OnCalServerVars", OnCalHistoBinDefs::LASTBINPLUSONE, -0.5, (OnCalHistoBinDefs::LASTBINPLUSONE - 0.5));
95  return;
96 }
97 //---------------------------------------------------------------------
98 
100 {
101  delete DBconnection;
102  return;
103 }
104 //---------------------------------------------------------------------
105 
106 PHTimeStamp *
108 {
109  if (endTimeStamp.getTics())
110  {
112  return ts;
113  }
114  else
115  {
116  cout << PHWHERE << "Screwup - the end validity time is not set" << endl;
117  exit(1);
118  }
119 }
120 //---------------------------------------------------------------------
121 
123 {
124  if (beginTimeStamp.getTics())
125  {
127  return ts;
128  }
129  else
130  {
131  cout << PHWHERE << "Screwup - the begin validity time is not set" << endl;
132  exit(1);
133  }
134 }
135 //---------------------------------------------------------------------
136 
138 {
139  ostringstream filename;
140  string fileprefix = "./";
141 
142  if (getenv("ONCAL_SAVEDIR"))
143  {
144  fileprefix = getenv("ONCAL_SAVEDIR");
145  fileprefix += "/";
146  }
147 
148  int compress = 3;
149  map<string, set<string> >::const_iterator iter;
150  // map<string, TH1 *>::const_iterator hiter;
151  TH1 *histo;
152  set<string>::const_iterator siter;
153  for (iter = calibratorhistomap.begin(); iter != calibratorhistomap.end(); ++iter)
154  {
155  filename.str("");
156  filename << fileprefix << "Run_"
157  << RunNumber()
158  << "_" << iter->first << ".root";
159  TFile *hfile = new TFile(filename.str().c_str(), "RECREATE",
160  "Created by Online Calibrator", compress);
161  cout << "OnCalServer::dumpHistos() Output root file: " << filename.str() << endl;
162  for (siter = (iter->second).begin(); siter != (iter->second).end(); ++siter)
163  {
164  histo = dynamic_cast<TH1 *>(getHisto(*siter));
165  if (histo)
166  {
167  histo->Write();
168  }
169  else
170  {
171  cout << PHWHERE << "Histogram "
172  << *siter << " not found, will not be saved in "
173  << filename.str() << endl;
174  }
175  }
176  hfile->Close();
177 
178  delete hfile;
179  }
180  return;
181 }
182 
183 void OnCalServer::registerHisto(TH1 *h1d, OnCal *Calibrator, const int replace)
184 {
185  if (Calibrator)
186  {
187  string calibratorname = Calibrator->Name();
188  map<string, set<string> >::iterator iter;
189  iter = calibratorhistomap.find(calibratorname);
190  if (iter != calibratorhistomap.end())
191  {
192  (iter->second).insert(h1d->GetName());
193  }
194  else
195  {
196  set<string> newset;
197  newset.insert(h1d->GetName());
198  newset.insert("OnCalServerVars");
199  calibratorhistomap[calibratorname] = newset;
200  }
201  }
202  Fun4AllServer::registerHisto(h1d, replace);
203  return;
204 }
205 
206 void OnCalServer::unregisterHisto(const string &calibratorname)
207 {
208  calibratorhistomap.erase(calibratorname);
209  return;
210 }
211 
213 {
215  int i = 0;
216  nEvents++;
217  if ((nEvents % eventcheckfrequency) == 0) // check every 1000 events
218  {
219  cout << nEvents << " events, testing" << endl;
220  unsigned int j = 0;
221  unsigned int ical = 0;
222  vector<pair<SubsysReco *, PHCompositeNode *> >::const_iterator iter;
223  for (iter = Subsystems.begin(); iter != Subsystems.end(); ++iter)
224  {
225  OnCal *oncal = dynamic_cast<OnCal *>(iter->first);
226  if (oncal)
227  {
228  ical++;
229  cout << "Name: " << oncal->Name()
230  << " is " << oncal->AllDone() << endl;
231  j += oncal->AllDone();
232  }
233  }
234  if (j == ical)
235  {
236  cout << "Everyone is done after "
237  << nEvents << " Events" << endl;
238  i = 1;
239  }
240  }
241  return i;
242 }
243 
244 int OnCalServer::BeginRun(const int runno)
245 {
246  if (runno <= 0)
247  {
248  cout << PHWHERE << "Invalid Run Number: " << runno << endl;
249  exit(1);
250  }
253  // we stick to the first runnumber, but after inheriting from
254  // Fun4All we get a EndRun/BeginRun when the run number changes
255  // so we have to catch this here
256  if (RunNumber() != 0)
257  {
258  rc->set_IntFlag("RUNNUMBER", RunNumber()); // set rc flag back to previous run
259  analysed_runs.push_back(runno);
260  return 0;
261  }
262  RunNumber(runno);
263  vector<pair<SubsysReco *, PHCompositeNode *> >::iterator iter;
264  // copy the subsys reco pointers to another set for
265  // easier search (we only need the pointers to find
266  // the subsystems with special timestamp/runnumber needs
267  set<SubsysReco *> NeedOtherTimeStamp;
268  map<string, set<SubsysReco *> >::const_iterator miter;
269  set<SubsysReco *>::const_iterator siter;
270  for (miter = requiredCalibrators.begin();
271  miter != requiredCalibrators.end(); ++miter)
272  {
273  for (siter = miter->second.begin(); siter != miter->second.end(); ++siter)
274  {
275  NeedOtherTimeStamp.insert(*siter);
276  }
277  }
278 
279  int iret;
280  int i = 0;
281  int oncalrun = runno;
282  int fun4allrun = runno;
283 
284  RunToTime *runTime = RunToTime::instance();
285  PHTimeStamp *ts = runTime->getBeginTime(fun4allrun);
286  PHTimeStamp OnCalBORTimeStamp = *ts;
287  PHTimeStamp Fun4AllBORTimeStamp(OnCalBORTimeStamp);
288  delete ts;
289  if (requiredCalibrators.size() > 0)
290  {
291  fun4allrun = FindClosestCalibratedRun(runno);
292  ts = runTime->getBeginTime(fun4allrun);
293  Fun4AllBORTimeStamp = *ts;
294  delete ts;
295  }
296 
297  // we have to do the same TDirectory games as in the Init methods
298  // save the current dir, cd to the subsystem name dir (which was
299  // created in init) call the InitRun of the module and cd back
300 
301  gROOT->cd(default_Tdirectory.c_str());
302  string currdir = gDirectory->GetPath();
303  set<string> droplist;
304  for (iter = Subsystems.begin(); iter != Subsystems.end(); ++iter)
305  {
306  ostringstream newdirname;
307  newdirname << (*iter).second->getName() << "/" << (*iter).first->Name();
308  if (!gROOT->cd(newdirname.str().c_str()))
309  {
310  cout << PHWHERE << "Unexpected TDirectory Problem cd'ing to "
311  << (*iter).second->getName()
312  << " - send e-mail to off-l with your macro" << endl;
313  exit(1);
314  }
315  OnCal *oncal = dynamic_cast<OnCal *>((*iter).first);
316  if (oncal)
317  {
318  string table = "OnCal";
319  table += (*iter).first->Name();
321  insertRunNumInDB(table, runNum);
322  string calibname = (*iter).first->Name();
324  set<int>::const_iterator runiter;
325  int calibstatus = GetCalibStatus(calibname, runNum);
326  if (calibstatus > 0 && testmode == 0)
327  {
328  cout << calibname << " already ran for run " << runNum << endl;
329  droplist.insert(calibname);
330  unregisterSubsystem(oncal);
331  unregisterHisto(calibname);
332  }
333  else
334  {
335  ostringstream stringarg;
336  stringarg << OnCalDBCodes::STARTED;
337  for (runiter = runlist.begin(); runiter != runlist.end(); ++runiter)
338  {
339  updateDB(successTable, calibname, stringarg.str(), *runiter);
340  }
341  }
342  }
343  if (NeedOtherTimeStamp.find((*iter).first) != NeedOtherTimeStamp.end())
344  {
345  cout << "changing timestamp for " << (*iter).first->Name() << endl;
346  rc->set_IntFlag("RUNNUMBER", fun4allrun);
347  // rc->set_TimeStamp(Fun4AllBORTimeStamp);
348  }
349  else
350  {
351  rc->set_IntFlag("RUNNUMBER", oncalrun);
352  // rc->set_TimeStamp(OnCalBORTimeStamp);
353  }
354  if (droplist.find((*iter).first->Name()) == droplist.end())
355  {
356  iret = (*iter).first->InitRun(TopNode);
357  if (iret == Fun4AllReturnCodes::ABORTRUN)
358  {
359  cout << PHWHERE << "Module " << (*iter).first->Name() << " issued Abort Run, exiting" << endl;
360  exit(-1);
361  }
362  i += iret;
363  }
364  }
365  gROOT->cd(currdir.c_str());
366 
367  rc->set_IntFlag("RUNNUMBER", oncalrun);
368  // rc->set_TimeStamp(OnCalBORTimeStamp);
369  if (OnCalServerVars->GetBinContent(OnCalHistoBinDefs::FIRSTRUNBIN) == 0)
370  {
371  OnCalServerVars->SetBinContent(OnCalHistoBinDefs::FIRSTRUNBIN, runno);
372  OnCalServerVars->SetBinContent(OnCalHistoBinDefs::BORTIMEBIN, (Stat_t) OnCalBORTimeStamp.getTics());
373  }
374  OnCalServerVars->SetBinContent(OnCalHistoBinDefs::LASTRUNBIN, (Stat_t) runno);
375  ts = runTime->getEndTime(runno);
376  if (ts)
377  {
378  OnCalServerVars->SetBinContent(OnCalHistoBinDefs::EORTIMEBIN, (Stat_t) ts->getTics());
379  delete ts;
380  }
381 
382  // disconnect from DB to save resources on DB machine
383  // PdbCal leaves the DB connection open (PdbCal will reconnect without
384  // problem if neccessary)
385  DisconnectDB();
386  // finally drop calibrators which have run already from module list
388  return i;
389 }
390 
392 {
393  if (nEvents == 0)
394  {
395  cout << "No Events read, you probably gave me an empty filelist" << endl;
396  return -1;
397  }
399  app->setDBName("oncal");
400  int i = 0;
401  vector<pair<SubsysReco *, PHCompositeNode *> >::iterator iter;
402  gROOT->cd(default_Tdirectory.c_str());
403  string currdir = gDirectory->GetPath();
404 
405  for (iter = Subsystems.begin(); iter != Subsystems.end(); ++iter)
406  {
407  ostringstream newdirname;
408  newdirname << (*iter).second->getName() << "/" << (*iter).first->Name();
409  if (!gROOT->cd(newdirname.str().c_str()))
410  {
411  cout << PHWHERE << "Unexpected TDirectory Problem cd'ing to "
412  << (*iter).second->getName()
413  << " - send e-mail to off-l with your macro" << endl;
414  exit(1);
415  }
416  else
417  {
418  if (Verbosity() > 2)
419  {
420  cout << "End: cded to " << newdirname.str().c_str() << endl;
421  }
422  }
423  i += (*iter).first->End((*iter).second);
424  }
425 
426  gROOT->cd(default_Tdirectory.c_str());
427  currdir = gDirectory->GetPath();
428  for (iter = Subsystems.begin(); iter != Subsystems.end(); ++iter)
429  {
430  OnCal *oncal = dynamic_cast<OnCal *>((*iter).first);
431  if (!oncal)
432  {
433  continue;
434  }
435  ostringstream newdirname;
436  newdirname << (*iter).second->getName() << "/" << (*iter).first->Name();
437  if (!gROOT->cd(newdirname.str().c_str()))
438  {
439  cout << PHWHERE << "Unexpected TDirectory Problem cd'ing to "
440  << (*iter).second->getName()
441  << " - send e-mail to off-l with your macro" << endl;
442  exit(1);
443  }
444 
445  string CalibratorName = oncal->Name();
446 
447  int verificationstatus = oncal->VerificationOK();
448  int databasecommitstatus = oncal->CommitedToPdbCalOK();
449 
450  // report success database the status of the calibration
451  if (recordDB)
452  {
453  string table = "OnCal";
454  table += CalibratorName;
455 
456  ostringstream stringarg;
457  if (databasecommitstatus == OnCalDBCodes::SUCCESS)
458  {
459  stringarg << OnCalDBCodes::COVERED;
460  }
461  else
462  {
463  stringarg << OnCalDBCodes::FAILED;
464  }
465  set<int>::const_iterator runiter;
466  for (runiter = runlist.begin(); runiter != runlist.end(); ++runiter)
467  {
468  updateDB(successTable, CalibratorName, stringarg.str(), *runiter);
469  }
470  // update the first run which was used in the calibration
471  // with the real status
472  updateDB(successTable.c_str(), CalibratorName.c_str(),
473  databasecommitstatus);
474 
475  stringarg.str("");
476  stringarg << databasecommitstatus;
477  updateDB(table, "committed", stringarg.str(), RunNumber());
478 
479  stringarg.str("");
480  stringarg << verificationstatus;
481  updateDB(table, "verified", stringarg.str(), RunNumber());
482 
483  odbc::Timestamp stp(time(nullptr));
484  updateDB(table, "date", stp.toString(), RunNumber());
485  updateDB(table, "comment", oncal->Comment(), RunNumber());
486  time_t beginticks = beginTimeStamp.getTics();
487  stringarg.str("");
488  stringarg << beginticks;
489  updateDB(table, "startvaltime", stringarg.str(), RunNumber());
490  stp.setTime(beginticks);
491  updateDB(table, "begintime", stp.toString(), RunNumber());
492  time_t endticks = endTimeStamp.getTics();
493  stringarg.str("");
494  stringarg << endticks;
495  updateDB(table, "endvaltime", stringarg.str(), RunNumber());
496  stp.setTime(endticks);
497  updateDB(table, "endtime", stp.toString(), RunNumber());
498 
499  string filelist = "";
500  BOOST_FOREACH (Fun4AllSyncManager *sync, SyncManagers)
501  {
502  BOOST_FOREACH (Fun4AllInputManager *inmgr, sync->GetInputManagers())
503  {
504  BOOST_FOREACH (string infile, inmgr->GetFileOpenedList())
505  {
506  filelist += (infile).substr(((infile).find_last_of('/') + 1), (infile).size());
507  filelist += " "; // this needs to be stripped again for last entry
508  }
509  }
510  }
511  filelist.pop_back(); // strip empty space at end from loop
512  cout << "FileList: " << filelist << endl;
513  updateDB(table, "files", filelist, RunNumber());
514  updateDB(table, "cvstag", cvstag, RunNumber());
515  }
516 
517  cout << "SERVER SUMMARY: " << oncal->Name() << " "
518  << (verificationstatus == 1 ? "Verification: SUCCESS " : "Verification: FAILURE ")
519  << (databasecommitstatus == 1 ? "DB commit: SUCCESS " : "DB commit: FAILURE ")
520  << endl;
521 
522  printStamps();
523  }
524  gROOT->cd(currdir.c_str());
525  dumpHistos(); // save the histograms in files
526  return i;
527 }
528 //---------------------------------------------------------------------
529 
530 void OnCalServer::Print(const string &what) const
531 {
532  Fun4AllServer::Print(what);
533  if (what == "ALL" || what == "CALIBRATOR")
534  {
535  // loop over the map and print out the content
536  // (name and location in memory)
537 
538  cout << "--------------------------------------" << endl
539  << endl;
540  cout << "List of Calibrators in OnCalServer:" << endl;
541 
542  vector<pair<SubsysReco *, PHCompositeNode *> >::const_iterator miter;
543  for (miter = Subsystems.begin();
544  miter != Subsystems.end(); ++miter)
545  {
546  OnCal *oncal = dynamic_cast<OnCal *>((*miter).first);
547  if (oncal)
548  {
549  cout << oncal->Name() << endl;
550  }
551  }
552  cout << endl;
553  }
554  if (what == "ALL" || what == "REQUIRED")
555  {
556  // loop over the map and print out the content
557  // (name and location in memory)
558 
559  cout << "--------------------------------------" << endl
560  << endl;
561  cout << "List of required Calibrations in OnCalServer:" << endl;
562 
563  map<string, set<SubsysReco *> >::const_iterator iter;
564  set<SubsysReco *>::const_iterator siter;
565  for (iter = requiredCalibrators.begin();
566  iter != requiredCalibrators.end(); ++iter)
567  {
568  cout << iter->first << " calibrations are needed by " << endl;
569  for (siter = iter->second.begin(); siter != iter->second.end(); ++siter)
570  {
571  cout << (*siter)->Name() << endl;
572  }
573  }
574  cout << endl;
575  }
576  if (what == "ALL" || what == "FILES")
577  {
578  cout << "--------------------------------------" << endl
579  << endl;
580  cout << "List of PRDF Files in OnCalServer:" << endl;
581  BOOST_FOREACH (Fun4AllSyncManager *sync, SyncManagers)
582  {
583  BOOST_FOREACH (Fun4AllInputManager *inmgr, sync->GetInputManagers())
584  {
585  BOOST_FOREACH (string infile, inmgr->GetFileList())
586  {
587  cout << "File: " << infile << endl;
588  }
589  }
590  }
591  }
592  if (what == "ALL" || what == "RUNS")
593  {
594  cout << "--------------------------------------" << endl
595  << endl;
596  cout << "List of Run Numbers in OnCalServer:" << endl;
597  set<int>::const_iterator liter;
598  for (liter = runlist.begin(); liter != runlist.end(); ++liter)
599  {
600  cout << "Run : " << *liter << endl;
601  }
602  }
603 
604  return;
605 }
606 
608 {
609  cout << endl
610  << endl;
611  cout << "*******************************************" << endl;
612  cout << "* VALIDITY RANGE FOR THIS CALIBRATION *" << endl;
613  cout << "* *" << endl;
614  cout << "* Used Run : ";
615  cout << runNum << endl;
616  cout << endl;
617  cout << "* Begin Valid : ";
619  cout << endl;
620  cout << "* End Valid : ";
622  cout << endl;
623  cout << "* *" << endl;
624  cout << "*******************************************" << endl;
625  cout << endl
626  << endl
627  << endl;
628 }
629 
630 //---------------------------------------------------------------------
631 
632 void OnCalServer::RunNumber(const int runnum)
633 {
634  runNum = runnum;
635  SetBorTime(runnum);
636  if (recordDB)
637  {
638  set<int>::const_iterator runiter;
639  time_t beginrunticks;
640  time_t endrunticks;
641  ostringstream stringarg;
642  odbc::Timestamp stp;
643  for (runiter = runlist.begin(); runiter != runlist.end(); ++runiter)
644  {
645  insertRunNumInDB(successTable, *runiter);
646  GetRunTimeTicks(*runiter, beginrunticks, endrunticks);
647  stringarg.str("");
648  stringarg << beginrunticks;
649  updateDB(successTable, "startvaltime", stringarg.str(), *runiter);
650  stp.setTime(beginrunticks);
651  updateDB(successTable, "beginrun", stp.toString(), *runiter);
652  stringarg.str("");
653  stringarg << endrunticks;
654  updateDB(successTable, "endvaltime", stringarg.str(), *runiter);
655  stp.setTime(endrunticks);
656  updateDB(successTable, "endrun", stp.toString(), *runiter);
657  }
658  }
659  if (runlist.size() > 0)
660  {
661  SetEorTime(*runlist.rbegin());
662  }
663  return;
664 }
665 
666 //---------------------------------------------------------------------
667 
669 {
670  if (DBconnection)
671  {
672  return true;
673  }
674 
675  bool failure = true;
676  int countdown = 10;
677  while (failure && countdown > 0)
678  {
679  failure = false;
680  try
681  {
682  DBconnection =
683  DriverManager::getConnection(database.c_str(), "phnxrc", "");
684  }
685  catch (SQLException &e)
686  {
687  cout << "Cannot connect to " << database.c_str() << endl;
688  cout << e.getMessage() << endl;
689  cout << "countdown: " << countdown << endl;
690  countdown--;
691  failure = true;
692  sleep(100); // try again in 100 secs
693  }
694  }
695  if (failure)
696  {
697  cout << "could not connect to DB after 10 tries in 1000 secs, giving up" << endl;
698  exit(-1);
699  }
700  cout << "connected to " << database.c_str() << " database." << endl;
701  return true;
702 }
703 //---------------------------------------------------------------------
704 
706 {
707  delete DBconnection;
708  DBconnection = nullptr;
709  return 0;
710 }
711 //---------------------------------------------------------------------
712 
713 bool OnCalServer::insertRunNumInDB(const string &DBtable, const int runno)
714 {
715  if (findRunNumInDB(DBtable, runno))
716  {
717  return true;
718  }
719 
720  cout << "new row will be created in DB for run " << runno << endl;
721 
722  Statement *statement = nullptr;
723  statement = DBconnection->createStatement();
724  ostringstream cmd;
725  cmd << "INSERT INTO "
726  << DBtable
727  << " (runnumber) VALUES ("
728  << runno << ")";
729 
730  if (Verbosity() == 1)
731  {
732  cout << "in function OnCalServer::insertRunNumInDB() ... ";
733  cout << "executing SQL statements ..." << endl;
734  cout << cmd.str() << endl;
735  }
736 
737  try
738  {
739  statement->executeUpdate(cmd.str());
740  }
741  catch (SQLException &e)
742  {
743  cout << e.getMessage() << endl;
744  return false;
745  }
746 
747  return true;
748 }
749 
750 //---------------------------------------------------------------------
751 
752 bool OnCalServer::findRunNumInDB(const string &DBtable, const int runno)
753 {
754  if (!DBconnection)
755  {
756  connectDB();
757  }
758  Statement *statement = nullptr;
759  ResultSet *rs = nullptr;
760  ostringstream cmd;
761  cmd << "SELECT runnumber FROM "
762  << DBtable
763  << " WHERE runnumber = "
764  << runno;
765 
766  statement = DBconnection->createStatement();
767 
768  if (Verbosity() == 1)
769  {
770  cout << "in function OnCalServer::findRunNumInDB() ";
771  cout << "executing SQL statement ..." << endl
772  << cmd.str() << endl;
773  }
774 
775  try
776  {
777  rs = statement->executeQuery(cmd.str());
778  }
779  catch (SQLException &e)
780  {
781  cout << PHWHERE << " exception caught: " << e.getMessage() << endl;
782  return false;
783  }
784 
785  int entry = 0;
786  if (rs->next())
787  {
788  try
789  {
790  entry = rs->getInt("runnumber");
791  }
792  catch (SQLException &e)
793  {
794  cout << PHWHERE << " exception caught: " << e.getMessage() << endl;
795  return false;
796  }
797  }
798  else
799  {
800  return false;
801  }
802  cout << "run number " << entry << " already exists in DB" << endl;
803  return true;
804 }
805 
806 bool OnCalServer::updateDBRunRange(const char *table, const char *column, const int entry, const int firstrun, const int lastrun)
807 {
808  if (!DBconnection)
809  {
810  connectDB();
811  }
812 
813  Statement *statement = nullptr;
814 
815  TString command = "UPDATE ";
816  command += table;
817  command += " SET ";
818  command += column;
819  command += " = ";
820  command += entry;
821  command += " WHERE runnumber >= ";
822  command += firstrun;
823  command += " and runnumber <= ";
824  command += lastrun;
825 
826  if (Verbosity() == 1)
827  {
828  cout << "in function OnCalServer::updateDB() ... ";
829  cout << "executin SQL statement ... " << endl;
830  cout << command.Data() << endl;
831  }
832  statement = DBconnection->createStatement();
833 
834  try
835  {
836  statement->executeUpdate(command.Data());
837  }
838  catch (SQLException &e)
839  {
840  cout << e.getMessage() << endl;
841  return false;
842  }
843 
844  return true;
845 }
846 
847 //---------------------------------------------------------------------
848 
849 bool OnCalServer::updateDB(const char *table, const char *column, int entry)
850 {
851  if (!DBconnection)
852  {
853  connectDB();
854  }
855 
856  Statement *statement = nullptr;
857 
858  TString command = "UPDATE ";
859  command += table;
860  command += " SET ";
861  command += column;
862  command += " = ";
863  command += entry;
864  command += " WHERE runnumber = ";
865  command += runNum;
866 
867  if (Verbosity() == 1)
868  {
869  cout << "in function OnCalServer::updateDB() ... ";
870  cout << "executin SQL statement ... " << endl;
871  cout << command.Data() << endl;
872  }
873  statement = DBconnection->createStatement();
874 
875  try
876  {
877  statement->executeUpdate(command.Data());
878  }
879  catch (SQLException &e)
880  {
881  cout << e.getMessage() << endl;
882  return false;
883  }
884 
885  return true;
886 }
887 //---------------------------------------------------------------------
888 
889 bool OnCalServer::updateDB(const char *table, const char *column, bool entry)
890 {
891  if (!DBconnection)
892  {
893  connectDB();
894  }
895  Statement *statement = nullptr;
896 
897  TString command = "UPDATE ";
898  command += table;
899  command += " set ";
900  command += column;
901  command += " = '";
902  command += static_cast<int>(entry);
903  command += "' WHERE runnumber = ";
904  command += runNum;
905 
906  if (Verbosity() == 1)
907  {
908  cout << "in function OnCalServer::updateDB() ... ";
909  cout << "executin SQL statement ... " << endl;
910  cout << command.Data() << endl;
911  }
912  statement = DBconnection->createStatement();
913 
914  try
915  {
916  statement->executeUpdate(command.Data());
917  }
918  catch (SQLException &e)
919  {
920  cout << e.getMessage() << endl;
921  return false;
922  }
923 
924  return true;
925 }
926 
927 //---------------------------------------------------------------------
928 
929 int OnCalServer::updateDB(const string &table, const string &column,
930  const time_t ticks)
931 {
932  if (!DBconnection)
933  {
934  connectDB();
935  }
936  Statement *statement = nullptr;
937 
938  ostringstream cmd;
939  statement = DBconnection->createStatement();
940  cmd << "UPDATE "
941  << table
942  << " set "
943  << column
944  << " = "
945  << ticks
946  << " WHERE runnumber = "
947  << runNum;
948 
949  if (Verbosity() == 1)
950  {
951  cout << "in function OnCalServer::updateDB() ... ";
952  cout << "executin SQL statement ... " << endl;
953  cout << cmd.str() << endl;
954  }
955 
956  try
957  {
958  statement->executeUpdate(cmd.str());
959  }
960  catch (SQLException &e)
961  {
962  cout << e.getMessage() << endl;
963  return -1;
964  }
965  return 0;
966 }
967 //---------------------------------------------------------------------
968 
969 bool OnCalServer::updateDB(const string &table, const string &column,
970  const string &entry, const int runno, const bool append)
971 {
972  if (!DBconnection)
973  {
974  connectDB();
975  }
976 
977  Statement *statement = nullptr;
978 
979  statement = DBconnection->createStatement();
980 
981  string comment = "";
982  ostringstream cmd;
983  if (append)
984  {
985  ResultSet *rs = nullptr;
986  ostringstream query;
987  query << "SELECT * FROM "
988  << table
989  << " WHERE runnumber = "
990  << runno;
991 
992  try
993  {
994  rs = statement->executeQuery(query.str());
995  }
996  catch (SQLException &e)
997  {
998  cout << "in function OnCalServer::updateDB() ... ";
999  cout << "run number " << runno << "not found in DB" << endl;
1000  cout << e.getMessage() << endl;
1001  }
1002 
1003  rs->next();
1004  try
1005  {
1006  comment = rs->getString(column);
1007  comment += " "; // add empty space between comments
1008  }
1009  catch (SQLException &e)
1010  {
1011  cout << "in function OnCalServer::updateDB() ... " << endl;
1012  cout << "nothing to append." << endl;
1013  cout << e.getMessage() << endl;
1014  }
1015  delete rs;
1016  }
1017 
1018  comment += entry;
1019  cmd << "UPDATE "
1020  << table
1021  << " set "
1022  << column
1023  << " = '"
1024  << comment
1025  << "' WHERE runnumber = "
1026  << runno;
1027 
1028  if (Verbosity() == 1)
1029  {
1030  cout << "in function OnCalServer::updateDB() ... ";
1031  cout << "executin SQL statement ... " << endl;
1032  cout << cmd.str() << endl;
1033  }
1034 
1035  try
1036  {
1037  statement->executeUpdate(cmd.str());
1038  }
1039  catch (SQLException &e)
1040  {
1041  cout << e.getMessage() << endl;
1042  return false;
1043  }
1044  delete statement;
1045  return true;
1046 }
1047 
1048 //---------------------------------------------------------------------
1049 
1051 {
1052  if (!connectDB())
1053  {
1054  cout << "could not connect to " << database << endl;
1055  return -1;
1056  }
1057  vector<pair<string, string> > calibrator_columns;
1058  vector<pair<string, string> >::const_iterator coliter;
1059  calibrator_columns.emplace_back("runnumber", "int NOT NULL");
1060  calibrator_columns.emplace_back("verified", "int default -2");
1061  calibrator_columns.emplace_back("committed", "int default -2");
1062  calibrator_columns.emplace_back("date", "timestamp(0) with time zone");
1063  calibrator_columns.emplace_back("comment", "text");
1064  calibrator_columns.emplace_back("files", "text");
1065  calibrator_columns.emplace_back("cvstag", "text");
1066  calibrator_columns.emplace_back("startvaltime", "bigint");
1067  calibrator_columns.emplace_back("endvaltime", "bigint");
1068  calibrator_columns.emplace_back("begintime", "timestamp(0) with time zone");
1069  calibrator_columns.emplace_back("endtime", "timestamp(0) with time zone");
1070 
1072  ostringstream cmd;
1073  cmd << "SELECT * FROM " << tablename << " LIMIT 1" << ends;
1074  ResultSet *rs = nullptr;
1075  try
1076  {
1077  rs = stmt->executeQuery(cmd.str());
1078  }
1079  catch (SQLException &e)
1080  {
1081  cout << "Table " << tablename << " does not exist, will create it" << endl;
1082  // cout << "Message: " << e.getMessage() << endl;
1083  }
1084  if (!rs)
1085  {
1086  cmd.str("");
1087  cmd << "CREATE TABLE "
1088  << tablename
1089  << "(";
1090  for (coliter = calibrator_columns.begin(); coliter != calibrator_columns.end(); ++coliter)
1091  {
1092  cmd << (*coliter).first << " " << (*coliter).second << ", ";
1093  }
1094 
1095  cmd << "primary key(runnumber))";
1096  stmt->executeUpdate(cmd.str());
1097  }
1098  else // check if the all columns exist
1099  {
1100  for (coliter = calibrator_columns.begin(); coliter != calibrator_columns.end(); ++coliter)
1101  {
1102  try
1103  {
1104  rs->findColumn((*coliter).first);
1105  }
1106  catch (SQLException &e)
1107  {
1108  const string &exceptionmessage = e.getMessage();
1109  if (exceptionmessage.find("not found in result set") != string::npos)
1110  {
1111  cout << "Column " << (*coliter).first << " does not exist in "
1112  << tablename << ", creating it" << endl;
1113  cmd.str("");
1114  cmd << "ALTER TABLE "
1115  << tablename
1116  << " ADD "
1117  << (*coliter).first
1118  << " "
1119  << (*coliter).second;
1120  try
1121  {
1122  Statement *stmtup = DBconnection->createStatement();
1123  stmtup->executeUpdate(cmd.str());
1124  }
1125  catch (SQLException &e1)
1126  {
1127  cout << PHWHERE << " Exception caught: " << e1.getMessage() << endl;
1128  }
1129  }
1130  }
1131  }
1132  delete rs;
1133  }
1134  return 0;
1135 }
1136 
1138 {
1139  if (!connectDB())
1140  {
1141  cout << "could not connect to " << database << endl;
1142  return -1;
1143  }
1144  if (check_calibrator_in_statustable(calibratorname) == 0)
1145  {
1146  return 0;
1147  }
1148  const string &calibname = calibratorname;
1150  ostringstream cmd;
1151  cmd.str("");
1152  cmd << "ALTER TABLE " << successTable << " ADD COLUMN "
1153  << calibname << " int";
1154  try
1155  {
1156  stmt->executeUpdate(cmd.str());
1157  }
1158  catch (SQLException &e)
1159  {
1160  cout << "Message: " << e.getMessage() << endl;
1161  cout << "cmd: " << cmd.str() << endl;
1162  exit(1);
1163  }
1164  cmd.str("");
1165  cmd << "ALTER TABLE " << successTable << " ALTER COLUMN "
1166  << calibname << " SET DEFAULT " << OnCalDBCodes::INIT;
1167  try
1168  {
1169  stmt->executeUpdate(cmd.str());
1170  }
1171  catch (SQLException &e)
1172  {
1173  cout << "Message: " << e.getMessage() << endl;
1174  cout << "cmd: " << cmd.str() << endl;
1175  exit(1);
1176  }
1177  cmd.str("");
1178  cmd << "UPDATE " << successTable << " SET "
1179  << calibname << " = " << OnCalDBCodes::INIT;
1180  try
1181  {
1182  stmt->executeUpdate(cmd.str());
1183  }
1184  catch (SQLException &e)
1185  {
1186  cout << "Message: " << e.getMessage() << endl;
1187  cout << "cmd: " << cmd.str() << endl;
1188  exit(1);
1189  }
1190 
1191  return 0;
1192 }
1193 
1195 {
1196  // replace this contraption by this sql command which returns 1 row if column exists
1197  // select * from information_schema.columns where table_name = 'oncal_status' and column_name = 'svxstripdeadmapcal';
1198  if (!connectDB())
1199  {
1200  cout << "could not connect to " << database << endl;
1201  return -1;
1202  }
1203  string calibname = calibratorname;
1205  ostringstream cmd;
1206  cmd << "SELECT * FROM " << successTable << " LIMIT 1" << ends;
1207  ResultSet *rs = nullptr;
1208  try
1209  {
1210  rs = stmt->executeQuery(cmd.str());
1211  }
1212  catch (SQLException &e)
1213  {
1214  cout << "Message: " << e.getMessage() << endl;
1215  cout << "Table " << successTable << " does not exist, your logic is off" << endl;
1216  exit(1);
1217  }
1218  ResultSetMetaData *meta = rs->getMetaData();
1219  unsigned int nocolumn = rs->getMetaData()->getColumnCount();
1220  // column names are lower case only, so convert string to lowercase
1221  // The bizarre cast here is needed for newer gccs
1222  transform(calibname.begin(), calibname.end(), calibname.begin(), (int (*)(int)) tolower);
1223 
1224  for (unsigned int i = 1; i <= nocolumn; i++)
1225  {
1226  if (meta->getColumnName(i) == calibname)
1227  {
1228  if (Verbosity() > 0)
1229  {
1230  cout << calibname << " is in " << successTable << endl;
1231  }
1232  return 0;
1233  }
1234  }
1235  // if we get here, the calibrator is not yet in the table
1236  delete rs;
1237  return -1;
1238 }
1239 
1241 {
1242  if (!connectDB())
1243  {
1244  cout << "could not connect to " << database << endl;
1245  return -1;
1246  }
1248  ostringstream cmd;
1249  cmd << "SELECT runnumber FROM " << tablename << " LIMIT 1" << ends;
1250  ResultSet *rs = nullptr;
1251  try
1252  {
1253  rs = stmt->executeQuery(cmd.str());
1254  }
1255  catch (SQLException &e)
1256  {
1257  cout << "Table " << tablename << " does not exist, will create it" << endl;
1258  // cout << "Message: " << e.getMessage() << endl;
1259  }
1260  if (!rs)
1261  {
1262  cmd.str("");
1263  cmd << "CREATE TABLE " << tablename << "(runnumber int NOT NULL, "
1264  << "startvaltime bigint, "
1265  << "endvaltime bigint, "
1266  << "beginrun timestamp(0) with time zone, "
1267  << "endrun timestamp(0) with time zone, "
1268  << "comment text, "
1269  << "primary key(runnumber))";
1270  cout << cmd.str() << endl;
1271  try
1272  {
1273  stmt->executeUpdate(cmd.str());
1274  }
1275  catch (SQLException &e)
1276  {
1277  cout << "Error, Message: " << e.getMessage() << endl;
1278  // cout << "Message: " << e.getMessage() << endl;
1279  }
1280  }
1281  return 0;
1282 }
1283 
1284 void OnCalServer::recordDataBase(const bool bookkeep)
1285 {
1286  recordDB = bookkeep;
1287  if (recordDB)
1288  {
1290  }
1291  return;
1292 }
1293 
1295 {
1296  beginTimeStamp = TimeStp;
1297  cout << "OnCalServer::BeginTimeStamp: Setting BOR TimeStamp to " << beginTimeStamp << endl;
1298 }
1299 
1301 {
1302  endTimeStamp = TimeStp;
1303  cout << "OnCalServer::EndTimeStamp: Setting EOR TimeStamp to " << endTimeStamp << endl;
1304 }
1305 
1306 PHTimeStamp *
1307 OnCalServer::GetLastGoodRunTS(OnCal *calibrator, const int irun)
1308 {
1309  PHTimeStamp *ts = nullptr;
1310  if (!connectDB())
1311  {
1312  cout << "could not connect to " << database << endl;
1313  return ts;
1314  }
1316  ostringstream cmd;
1317  ostringstream subsystable;
1318  subsystable << "oncal" << calibrator->Name();
1319  cmd << "SELECT runnumber FROM " << successTable << " where runnumber < "
1320  << irun << " and "
1321  << calibrator->Name() << " > 0 order by runnumber desc limit 1";
1322  ResultSet *rs = nullptr;
1323  try
1324  {
1325  rs = stmt->executeQuery(cmd.str());
1326  }
1327  catch (SQLException &e)
1328  {
1329  cout << "Table " << subsystable.str() << " does not exist" << endl;
1330  return ts;
1331  }
1332  if (rs->next())
1333  {
1335  int oldrun = rs->getInt("runnumber");
1336  ts = rt->getBeginTime(oldrun);
1337  cout << "Getting previous good run, current run: " << irun
1338  << ", previous good run: " << oldrun
1339  << " began ";
1340  ts->print();
1341  cout << endl;
1342  }
1343  else
1344  {
1345  cout << PHWHERE << " No previous good run found for run " << irun << endl;
1346  }
1347  delete rs;
1348  return ts;
1349 }
1350 
1351 int OnCalServer::SyncCalibTimeStampsToOnCal(const OnCal *calibrator, const int commit)
1352 {
1353  vector<string> caltab;
1354  calibrator->GetPdbCalTables(caltab);
1355  vector<string>::const_iterator iter;
1356  for (iter = caltab.begin(); iter != caltab.end(); ++iter)
1357  {
1358  cout << "dealing with table: " << *iter << endl;
1359  SyncCalibTimeStampsToOnCal(calibrator, *iter, commit);
1360  }
1361  return 0;
1362 }
1363 
1364 int OnCalServer::SyncCalibTimeStampsToOnCal(const OnCal *calibrator, const std::string &table, const int commit)
1365 {
1366  string name = calibrator->Name();
1367  odbc::Connection *con = nullptr;
1368  odbc::Connection *concalib = nullptr;
1369  ostringstream cmd;
1370  try
1371  {
1372  con = DriverManager::getConnection(database.c_str(), "phnxrc", "");
1373  }
1374  catch (SQLException &e)
1375  {
1376  cout << "Cannot connect to " << database.c_str() << endl;
1377  cout << e.getMessage() << endl;
1378  return -1;
1379  }
1380  try
1381  {
1382  concalib = DriverManager::getConnection("oncal", "phnxrc", "");
1383  }
1384  catch (SQLException &e)
1385  {
1386  cout << "Cannot connect to "
1387  << "oncal" << endl;
1388  cout << e.getMessage() << endl;
1389  return -1;
1390  }
1391  Statement *stmt = nullptr;
1392  try
1393  {
1394  stmt = con->createStatement();
1395  }
1396  catch (SQLException &e)
1397  {
1398  cout << "Cannot create statement" << endl;
1399  cout << e.getMessage() << endl;
1400  return -1;
1401  }
1402 
1403  PreparedStatement *stmt1 = nullptr;
1404  ResultSet *rs1 = nullptr;
1405  try
1406  {
1407  cmd.str("");
1408  cmd << "SELECT * from " << table << " where startvaltime = ?";
1409  stmt1 = concalib->prepareStatement(cmd.str());
1410  }
1411  catch (SQLException &e)
1412  {
1413  cout << "Cannot create statement" << endl;
1414  cout << e.getMessage() << endl;
1415  return -1;
1416  }
1417 
1418  PreparedStatement *stmtupd = nullptr;
1419  try
1420  {
1421  cmd.str("");
1422  cmd << "update " << table << " set endvaltime = ? where startvaltime = ?";
1423  stmtupd = concalib->prepareStatement(cmd.str());
1424  }
1425  catch (SQLException &e)
1426  {
1427  cout << "Cannot create statement" << endl;
1428  cout << e.getMessage() << endl;
1429  return -1;
1430  }
1431 
1432  cmd.str("");
1433  cmd << "select * from "
1434  << successTable
1435  << " where "
1436  << name
1437  << " > 0";
1438  // << " > 0 and runnumber < 150000";
1439  ResultSet *rs = nullptr;
1440  try
1441  {
1442  rs = stmt->executeQuery(cmd.str());
1443  }
1444  catch (SQLException &e)
1445  {
1446  cout << "Message: " << e.getMessage() << endl;
1447  return -1;
1448  }
1449  while (rs->next())
1450  {
1451  int run = rs->getInt("runnumber");
1452  int startticks = rs->getLong("startvaltime");
1453  int endticks = rs->getLong("endvaltime");
1454  // int status = rs->getInt(name);
1455  // cout << "run: " << run
1456  // << ", status: " << status
1457  // << ", startticks: " << startticks
1458  // << ", endticks: " << endticks << endl;
1459  stmt1->setInt(1, startticks);
1460  try
1461  {
1462  rs1 = stmt1->executeQuery();
1463  }
1464  catch (SQLException &e)
1465  {
1466  cout << "Message: " << e.getMessage() << endl;
1467  return -1;
1468  }
1469  int ionce = 0;
1470  int isproblem = 0;
1471  int calibendval = 0;
1472  while (rs1->next())
1473  {
1474  calibendval = rs1->getInt("endvaltime");
1475  if (endticks != rs1->getInt("endvaltime"))
1476  {
1477  if (!isproblem)
1478  {
1479  cout << "endvaltime problem with run " << run << endl;
1480  cout << "endvaltime from oncal_status: " << endticks << endl;
1481  cout << "startvaltime from oncal_status: " << startticks << endl;
1482  cout << "endvaltime from calibrations DB: " << rs1->getInt("endvaltime") << endl;
1483  if (endticks < rs1->getInt("endvaltime"))
1484  {
1485  cout << "ENDTICKS smaller CALIB" << endl;
1486  // return -1;
1487  }
1488  }
1489  isproblem = 1;
1490  }
1491  else
1492  {
1493  if (isproblem)
1494  {
1495  cout << "endvaltime changes, check run " << run << endl;
1496  // return -1;
1497  }
1498  }
1499  // cout << "starttime: " << rs1->getInt("startvaltime") << endl;
1500  // cout << "endtime: " << rs1->getInt("endvaltime") << endl;
1501  ionce++;
1502  }
1503  if (isproblem)
1504  {
1505  cout << "Adjusting run " << run << endl;
1506  cout << "changing endvaltime from " << calibendval
1507  << " to " << endticks << endl;
1508  if (commit)
1509  {
1510  stmtupd->setInt(1, endticks);
1511  stmtupd->setInt(2, startticks);
1512  stmtupd->executeUpdate();
1513  }
1514  }
1515  if (!ionce)
1516  {
1517  cout << "Run " << run << " not found" << endl;
1518  }
1519  delete rs1;
1520  }
1521  delete rs;
1522  delete con;
1523  delete concalib;
1524  return 0;
1525 }
1526 
1528 {
1529  odbc::Connection *con = nullptr;
1531  ostringstream cmd;
1532  try
1533  {
1534  con = DriverManager::getConnection(database.c_str(), "phnxrc", "");
1535  }
1536  catch (SQLException &e)
1537  {
1538  cout << "Cannot connect to " << database.c_str() << endl;
1539  cout << e.getMessage() << endl;
1540  return -1;
1541  }
1542  Statement *stmt = nullptr;
1543  try
1544  {
1545  stmt = con->createStatement();
1546  }
1547  catch (SQLException &e)
1548  {
1549  cout << "Cannot create statement" << endl;
1550  cout << e.getMessage() << endl;
1551  return -1;
1552  }
1553 
1554  PreparedStatement *stmtupd = nullptr;
1555  try
1556  {
1557  cmd.str("");
1558  cmd << "UPDATE oncal_status set endvaltime = ? where runnumber = ?";
1559  stmtupd = con->prepareStatement(cmd.str());
1560  }
1561  catch (SQLException &e)
1562  {
1563  cout << "Cannot create statement" << endl;
1564  cout << e.getMessage() << endl;
1565  return -1;
1566  }
1567 
1568  cmd.str("");
1569  cmd << "select * from "
1570  << successTable; //<< " where runnumber > 160000";
1571  ResultSet *rs = nullptr;
1572  try
1573  {
1574  rs = stmt->executeQuery(cmd.str());
1575  }
1576  catch (SQLException &e)
1577  {
1578  cout << "Message: " << e.getMessage() << endl;
1579  return -1;
1580  }
1581  while (rs->next())
1582  {
1583  int run = rs->getInt("runnumber");
1584  int startticks = rs->getLong("startvaltime");
1585  int endticks = rs->getLong("endvaltime");
1586  int rtstartticks = 0;
1587  int rtendticks = 0;
1588  PHTimeStamp *rtstart = rt->getBeginTime(run);
1589  PHTimeStamp *rtend = rt->getEndTime(run);
1590  if (rtstart)
1591  {
1592  rtstartticks = rtstart->getTics();
1593  delete rtstart;
1594  }
1595  if (rtend)
1596  {
1597  rtendticks = rtend->getTics();
1598  delete rtend;
1599  }
1600  if (rtstartticks != startticks)
1601  {
1602  cout << "Run " << run
1603  << ": Start mismatch, oncal: " << startticks
1604  << ", rt: " << rtstartticks << endl;
1605  }
1606  if (rtendticks != endticks)
1607  {
1608  // exclude starttime=endtime in runtotime (some crashed calibrations can do this)
1609  // in this case the calibration adds 1 sec to starttime
1610  if (rtstartticks != rtendticks)
1611  {
1612  cout << "Run " << run
1613  << ": End mismatch, oncal: " << endticks
1614  << ", rt: " << rtendticks << endl;
1615  if (endticks > rtendticks)
1616  {
1617  cout << "BAD: endticks: " << endticks
1618  << ", rtendticks: " << rtendticks
1619  << endl;
1620  return -1;
1621  }
1622  if (commit)
1623  {
1624  stmtupd->setLong(1, rtendticks);
1625  stmtupd->setLong(2, run);
1626  stmtupd->executeUpdate();
1627  }
1628  }
1629  else
1630  {
1631  if (startticks != endticks - 1)
1632  {
1633  cout << "Run " << run
1634  << ": Start/End mismatch, Start: " << startticks
1635  << ", End: " << endticks << endl;
1636  endticks = startticks + 1;
1637  if (commit)
1638  {
1639  stmtupd->setLong(1, endticks);
1640  stmtupd->setLong(2, run);
1641  stmtupd->executeUpdate();
1642  }
1643  }
1644  else
1645  {
1646  if (Verbosity() > 0)
1647  {
1648  cout << "run " << run << " was twiddled by OnCal" << endl;
1649  }
1650  }
1651  }
1652  }
1653  // cout << "run: " << run
1654  // << ", status: " << status
1655  // << ", startticks: " << startticks
1656  // << ", endticks: " << endticks << endl;
1657  }
1658  delete rs;
1659  delete con;
1660  return 0;
1661 }
1662 
1663 int OnCalServer::CopyTables(const OnCal *calibrator, const int FromRun, const int ToRun, const int commit) const
1664 {
1665  int iret = calibrator->CopyTables(FromRun, ToRun, commit);
1666  return iret;
1667 }
1668 
1669 int OnCalServer::CreateCalibration(OnCal *calibrator, const int myrunnumber, const string &what, const int commit)
1670 {
1671  int iret = -1;
1672  runNum = myrunnumber;
1673  SetBorTime(myrunnumber);
1674  SetEorTime(myrunnumber);
1675  if (!connectDB())
1676  {
1677  cout << "could not connect to " << database << endl;
1678  return -1;
1679  }
1680  add_calibrator_to_statustable(calibrator->Name());
1681  string table = "OnCal";
1682  table += calibrator->Name();
1683  check_create_subsystable(table);
1685  ostringstream cmd;
1686 
1687  cmd << "SELECT runnumber FROM "
1688  << successTable << " where runnumber = "
1689  << myrunnumber;
1690  ResultSet *rs = nullptr;
1691  try
1692  {
1693  rs = stmt->executeQuery(cmd.str());
1694  }
1695  catch (SQLException &e)
1696  {
1697  cout << "Table " << successTable << " does not exist" << endl;
1698  return -1;
1699  }
1700  if (!rs->next())
1701  {
1702  insertRunNumInDB(successTable, myrunnumber);
1703  }
1704  delete rs;
1705  cmd.str("");
1706  cmd << "SELECT runnumber FROM "
1707  << successTable << " where runnumber = "
1708  << myrunnumber << " and "
1709  << calibrator->Name() << " <= 0";
1710  try
1711  {
1712  rs = stmt->executeQuery(cmd.str());
1713  }
1714  catch (SQLException &e)
1715  {
1716  cout << PHWHERE << " Exception caught, Message: "
1717  << e.getMessage() << endl;
1718  return -1;
1719  }
1720  if (rs->next() || testmode)
1721  {
1722  PdbBankManager *bankManager = PdbBankManager::instance();
1723  PdbApplication *application = bankManager->getApplication();
1724  application->setDBName("oncal");
1725  string tablecomment = "Subsytem provided";
1726  iret = calibrator->CreateCalibration(runnumber, what, tablecomment, commit);
1727  if (!iret)
1728  {
1729  cout << "Comment: " << tablecomment << endl;
1730  cout << "updating oncal status tables for " << runnumber << endl;
1731  if (commit)
1732  {
1733  CreateCalibrationUpdateStatus(calibrator, table, tablecomment, OnCalDBCodes::SUBSYSTEM);
1734  }
1735  }
1736  else
1737  {
1738  cout << "Calibratior " << calibrator->Name() << " for run " << runnumber << " failed" << endl;
1739  if (commit)
1740  {
1741  CreateCalibrationUpdateStatus(calibrator, table, tablecomment, OnCalDBCodes::FAILED);
1742  }
1743  }
1744  }
1745  else
1746  {
1747  cout << PHWHERE << " Run " << runnumber << " is already successfully calibrated for "
1748  << calibrator->Name() << endl;
1749  }
1750  return iret;
1751 }
1752 
1753 void OnCalServer::CreateCalibrationUpdateStatus(OnCal *calibrator, const string &table, const string &tablecomment, const int dbcode)
1754 {
1755  updateDB(successTable.c_str(), calibrator->Name(), dbcode);
1756  insertRunNumInDB(table, RunNumber());
1757  updateDB(table, "comment", tablecomment, RunNumber(), true);
1758  ostringstream stringarg;
1759  stringarg.str("");
1760  stringarg << calibrator->CommitedToPdbCalOK();
1761  updateDB(table, "committed", stringarg.str(), RunNumber());
1762  stringarg.str("");
1763  stringarg << calibrator->VerificationOK();
1764  updateDB(table, "verified", stringarg.str(), RunNumber());
1765  odbc::Timestamp stp(time(nullptr));
1766  updateDB(table, "date", stp.toString(), RunNumber());
1767  time_t beginticks = beginTimeStamp.getTics();
1768  stringarg.str("");
1769  stringarg << beginticks;
1770  updateDB(table, "startvaltime", stringarg.str(), RunNumber());
1771  stp.setTime(beginticks);
1772  updateDB(table, "begintime", stp.toString(), RunNumber());
1773  time_t endticks = endTimeStamp.getTics();
1774  stringarg.str("");
1775  stringarg << endticks;
1776  updateDB(table, "endvaltime", stringarg.str(), RunNumber());
1777  stp.setTime(endticks);
1778  updateDB(table, "endtime", stp.toString(), RunNumber());
1779  updateDB(table, "cvstag", cvstag, RunNumber());
1780  vector<string> flist = calibrator->GetLocalFileList();
1781  if (flist.size())
1782  {
1783  string filelist = "";
1784  BOOST_FOREACH (string infile, flist)
1785  {
1786  filelist += infile;
1787  filelist += " ";
1788  }
1789  filelist.pop_back(); // strip empty space at end from loop
1790  cout << "FileList: " << filelist << endl;
1791  updateDB(table, "files", filelist, RunNumber());
1792  }
1793  return;
1794 }
1795 
1796 int OnCalServer::CopySnglTable(const string &pdbclass, const string &tablename, const int bankid, const int FromRun, const int ToRun, const int commit)
1797 {
1798  int iret = CopySnglTableNewBankId(pdbclass, tablename, bankid, bankid, FromRun, ToRun, commit);
1799  return iret;
1800 }
1801 
1802 int OnCalServer::CopySnglTableNewBankId(const string &pdbclass, const string &tablename, const int bankid, const int Tobankid, const int FromRun, const int ToRun, const int commit)
1803 {
1805  PHTimeStamp *ts = rt->getBeginTime(FromRun);
1806  assert(ts);
1807  PdbBankManager *bankManager = PdbBankManager::instance();
1808  PdbApplication *application = bankManager->getApplication();
1809  application->setDBName("oncal");
1810  PdbBankID BankID(bankid);
1811  PdbCalBank *pdbBank = bankManager->fetchBank(pdbclass.c_str(), BankID, tablename.c_str(), *ts);
1812  assert(pdbBank);
1813  PHTimeStamp *tstartnew = rt->getBeginTime(ToRun);
1814  PHTimeStamp *tendnew = rt->getEndTime(ToRun);
1815  application->startUpdate();
1816  PdbCalBank *pdbBanknew = (PdbCalBank *) (pdbBank->clone());
1817  ostringstream newdesc;
1818  newdesc << "copied from run " << FromRun;
1819  if (pdbBanknew)
1820  {
1821  pdbBanknew->setLength(pdbBank->getLength());
1822  if (Verbosity() > 0)
1823  {
1824  for (unsigned int i = 0; i < pdbBank->getLength(); i++)
1825  {
1826  cout << "orig: " << endl;
1827  pdbBank->printEntry(i);
1828  cout << "new: " << endl;
1829  pdbBanknew->printEntry(i);
1830  }
1831  }
1832  PHTimeStamp tsins;
1833  pdbBanknew->setInsertTime(tsins);
1834  pdbBanknew->setStartValTime(*tstartnew);
1835  pdbBanknew->setEndValTime(*tendnew);
1836  pdbBanknew->setDescription(newdesc.str().c_str());
1837  if (bankid != Tobankid)
1838  {
1839  pdbBanknew->setBankID(Tobankid);
1840  }
1841  if (Verbosity() > 0)
1842  {
1843  cout << "StartValTime: orig " << pdbBank->getStartValTime()
1844  << ", new: " << pdbBanknew->getStartValTime() << endl;
1845  cout << "EndValTime: orig " << pdbBank->getEndValTime()
1846  << ", new: " << pdbBanknew->getEndValTime() << endl;
1847  }
1848  if (commit)
1849  {
1850  application->commit(pdbBanknew);
1851  }
1852  else
1853  {
1854  application->abort();
1855  }
1856  }
1857  delete pdbBank;
1858 
1859  delete ts;
1860  delete tstartnew;
1861  delete tendnew;
1862  return 0;
1863 }
1864 
1865 int OnCalServer::ClosestGoodRun(OnCal *calibrator, const int irun, const int previous)
1866 {
1868  PHTimeStamp *ts = rt->getBeginTime(irun);
1869  if (!ts)
1870  {
1871  cout << PHWHERE << "Unknown Run " << irun << endl;
1872  return -1;
1873  }
1874  int curstart = ts->getTics();
1875  delete ts;
1876  ts = rt->getEndTime(irun);
1877  int curend = curstart;
1878  if (ts)
1879  {
1880  curend = ts->getTics();
1881  delete ts;
1882  }
1883  int closestrun = -1;
1884  if (!connectDB())
1885  {
1886  cout << "could not connect to " << database << endl;
1887  return -1;
1888  }
1890  ostringstream cmd;
1891 
1892  // look only for runs which were actually successfully calibrated (status = 1)
1893  cmd << "SELECT runnumber,startvaltime,endvaltime FROM "
1894  << successTable << " where runnumber < "
1895  << irun << " and "
1896  << calibrator->Name() << " = 1 order by runnumber desc limit 1";
1897  ResultSet *rs = nullptr;
1898  try
1899  {
1900  rs = stmt->executeQuery(cmd.str());
1901  }
1902  catch (SQLException &e)
1903  {
1904  cout << "Table " << successTable << " does not exist" << endl;
1905  return -1;
1906  }
1907  int prevrun = -1;
1908  unsigned int prevend = 0;
1909  if (rs->next())
1910  {
1911  prevrun = rs->getInt("runnumber");
1912  unsigned int prevstart = rs->getLong("startvaltime");
1913  prevend = rs->getLong("endvaltime");
1914  cout << "previous run: " << prevrun
1915  << ", start: " << prevstart
1916  << ", end: " << prevend
1917  << endl;
1918  }
1919  else
1920  {
1921  if (Verbosity() > 0)
1922  {
1923  cout << PHWHERE << " No previous good run found for run " << irun << endl;
1924  }
1925  }
1926  delete rs;
1927  closestrun = prevrun;
1928  if (previous == fetchrun::PREVIOUS)
1929  {
1930  if (Verbosity() > 0)
1931  {
1932  cout << "Closest previous run is " << closestrun << endl;
1933  }
1934  return closestrun;
1935  }
1936  cmd.str("");
1937  cmd << "SELECT runnumber,startvaltime,endvaltime FROM "
1938  << successTable << " where runnumber > "
1939  << irun << " and "
1940  << calibrator->Name() << " = 1 order by runnumber asc limit 1";
1941  try
1942  {
1943  rs = stmt->executeQuery(cmd.str());
1944  }
1945  catch (SQLException &e)
1946  {
1947  cout << "Table " << successTable << " does not exist" << endl;
1948  return -1;
1949  }
1950  int nextrun = -1;
1951  unsigned int nextstart = 0;
1952  if (rs->next())
1953  {
1954  nextrun = rs->getInt("runnumber");
1955  nextstart = rs->getLong("startvaltime");
1956  unsigned int nextend = rs->getLong("endvaltime");
1957  if (Verbosity() > 0)
1958  {
1959  cout << "next run: " << nextrun
1960  << ", start: " << nextstart
1961  << ", end: " << nextend
1962  << endl;
1963  }
1964  }
1965  else
1966  {
1967  if (Verbosity() > 0)
1968  {
1969  cout << PHWHERE << " No next good run found for run " << irun << endl;
1970  }
1971  }
1972  delete rs;
1973  int tdiffprev = curstart - prevend;
1974  int tdiffnext;
1975  if (nextstart > 0)
1976  {
1977  tdiffnext = nextstart - curend;
1978  }
1979  else
1980  {
1981  // just make it larger then previous run time diff
1982  tdiffnext = tdiffprev + 1;
1983  }
1984  if (Verbosity() > 0)
1985  {
1986  cout << "diff prev: " << tdiffprev
1987  << ", next: " << tdiffnext
1988  << endl;
1989  }
1990  if (tdiffprev < tdiffnext)
1991  {
1992  closestrun = prevrun;
1993  }
1994  else
1995  {
1996  closestrun = nextrun;
1997  }
1998  if (Verbosity() > 0)
1999  {
2000  cout << "closest run: " << closestrun << endl;
2001  }
2002  return closestrun;
2003 }
2004 
2005 int OnCalServer::OverwriteCalibration(OnCal *calibrator, const int runno, const int commit, const int FromRun)
2006 {
2007  if (FromRun < 0)
2008  {
2009  return -1;
2010  }
2011  int iret = CopyTables(calibrator, FromRun, runno, commit);
2012  return iret;
2013 }
2014 
2015 int OnCalServer::FixMissingCalibration(OnCal *calibrator, const int runno, const int commit, const int fromrun)
2016 {
2017  int iret = -1;
2018  // find this run in oncal_status
2019  if (!connectDB())
2020  {
2021  cout << "could not connect to " << database << endl;
2022  return -1;
2023  }
2024  runNum = runno;
2026  ostringstream cmd;
2027 
2028  cmd << "SELECT runnumber FROM "
2029  << successTable << " where runnumber = "
2030  << runno;
2031  ResultSet *rs = nullptr;
2032  try
2033  {
2034  rs = stmt->executeQuery(cmd.str());
2035  }
2036  catch (SQLException &e)
2037  {
2038  cout << "Table " << successTable << " does not exist" << endl;
2039  return -1;
2040  }
2041  if (!rs->next())
2042  {
2044  }
2045  delete rs;
2046  cmd.str("");
2047  cmd << "SELECT runnumber FROM "
2048  << successTable << " where runnumber = "
2049  << runno << " and "
2050  << calibrator->Name() << " <= 0";
2051  try
2052  {
2053  rs = stmt->executeQuery(cmd.str());
2054  }
2055  catch (SQLException &e)
2056  {
2057  cout << PHWHERE << " Exception caught, Message: "
2058  << e.getMessage() << endl;
2059  return -1;
2060  }
2061  if (rs->next())
2062  {
2063  int FromRun;
2064  if (fromrun > 0)
2065  {
2066  FromRun = fromrun;
2067  }
2068  else
2069  {
2070  FromRun = ClosestGoodRun(calibrator, runno);
2071  if (FromRun < 0)
2072  {
2073  cout << "ClosestGoodRun returned bad runnumber: " << FromRun << endl;
2074  return -1;
2075  }
2076  }
2077  cout << "Going to copy calibration for run " << runno
2078  << " from run " << FromRun << endl;
2079 
2080  iret = OverwriteCalibration(calibrator, runno, commit, FromRun);
2081  if (!iret)
2082  {
2083  int newstatus = 0;
2084  if (FromRun < runno)
2085  {
2086  newstatus = OnCalDBCodes::COPIEDPREVIOUS;
2087  }
2088  else
2089  {
2090  newstatus = OnCalDBCodes::COPIEDLATER;
2091  }
2092  string table = "OnCal";
2093  table += calibrator->Name();
2094  ostringstream comment;
2095  comment << " CopiedRun(" << FromRun << ")";
2096  cout << "updating oncal status tables for " << runno << endl;
2097  if (commit)
2098  {
2099  updateDB(successTable.c_str(), calibrator->Name(), newstatus);
2100  insertRunNumInDB(table, runNum);
2101  updateDB(table, "comment", comment.str(), runNum, true);
2102  updateDB(table.c_str(), "committed", true);
2103  }
2104  }
2105  }
2106  else
2107  {
2108  cout << "Run " << runno
2109  << " has a good calibrations, doing nothing" << endl;
2110  }
2111  delete rs;
2112  return iret;
2113 }
2114 
2115 int OnCalServer::SetBorTime(const int runno)
2116 {
2117  // recoConsts *rc = recoConsts::instance();
2118  RunToTime *runTime = RunToTime::instance();
2119 
2120  PHTimeStamp *BorTimeStp(runTime->getBeginTime(runno));
2121  if (!BorTimeStp)
2122  {
2123  cout << PHWHERE << "Cannot get begin time for run " << runno << endl;
2124  cout << "Exiting" << endl;
2125  exit(1);
2126  }
2127  BeginTimeStamp(*BorTimeStp);
2128 
2129  // enter begin run timestamp into rc flags
2130  PHTimeStamp BeginRunTimeStamp(*BorTimeStp);
2131  // rc->set_TimeStamp(BeginRunTimeStamp);
2132  cout << "OnCalServer::SetBorTime from RunToTime was found for run : " << runno << " to ";
2133  BeginRunTimeStamp.print();
2134  cout << endl;
2135 
2136  delete BorTimeStp;
2137  return 0;
2138 }
2139 
2140 int OnCalServer::SetEorTime(const int runno)
2141 {
2142  // recoConsts *rc = recoConsts::instance();
2143  RunToTime *runTime = RunToTime::instance();
2144 
2145  time_t eorticks = 0;
2146 
2147  time_t borticks = 0; //(rc->get_TimeStamp()).getTics();
2148  PHTimeStamp *EorTimeStp(runTime->getEndTime(runno));
2149  if (EorTimeStp)
2150  {
2151  eorticks = EorTimeStp->getTics();
2152  }
2153  else
2154  {
2155  EorTimeStp = new PHTimeStamp(eorticks);
2156  }
2157  // if end of run timestamp missing or smaller-equal borstamp eor = bor+1 sec
2158  if (eorticks <= borticks)
2159  {
2160  eorticks = borticks + 1;
2161  EorTimeStp->setTics(eorticks);
2162  }
2163  EndTimeStamp(*EorTimeStp);
2164  cout << "OnCalServer::SetEorTime: setting eor time to ";
2165  EorTimeStp->print();
2166  cout << endl;
2167  delete EorTimeStp;
2168  return 0;
2169 }
2170 
2171 int OnCalServer::GetRunTimeTicks(const int runno, time_t &borticks, time_t &eorticks)
2172 {
2173  RunToTime *runTime = RunToTime::instance();
2174  PHTimeStamp *TimeStp(runTime->getBeginTime(runno));
2175  if (!TimeStp)
2176  {
2177  cout << PHWHERE << "Cannot get begin time for run " << runno << endl;
2178  cout << "Exiting" << endl;
2179  exit(1);
2180  }
2181  borticks = TimeStp->getTics();
2182  delete TimeStp;
2183  TimeStp = runTime->getEndTime(runno);
2184  if (TimeStp)
2185  {
2186  eorticks = TimeStp->getTics();
2187  delete TimeStp;
2188  }
2189  else
2190  {
2191  eorticks = 0;
2192  }
2193  // if end of run timestamp missing or smaller-equal borstamp eor = bor+1 sec
2194  if (eorticks <= borticks)
2195  {
2196  eorticks = borticks + 1;
2197  }
2198  return 0;
2199 }
2200 
2202 {
2203  map<string, set<SubsysReco *> >::iterator iter;
2204  if (check_calibrator_in_statustable(calibratorname))
2205  {
2206  cout << PHWHERE << " the calibrator " << calibratorname << " is unknown to me" << endl;
2207  return -1;
2208  }
2209  iter = requiredCalibrators.find(calibratorname);
2210  if (iter != requiredCalibrators.end())
2211  {
2212  iter->second.insert(reco);
2213  }
2214  else
2215  {
2216  set<SubsysReco *> subsys;
2217  subsys.insert(reco);
2218  requiredCalibrators[calibratorname] = subsys;
2219  }
2220  return 0;
2221 }
2222 
2224 {
2226  PHTimeStamp *ts = rt->getBeginTime(irun);
2227  if (!ts)
2228  {
2229  cout << PHWHERE << "Unknown Run " << irun << endl;
2230  return -1;
2231  }
2232  if (requiredCalibrators.size() == 0)
2233  {
2234  cout << PHWHERE << "No required calibrations given" << endl;
2235  return irun;
2236  }
2237  int curstart = ts->getTics();
2238  delete ts;
2239  ts = rt->getEndTime(irun);
2240  int curend = curstart;
2241  if (ts)
2242  {
2243  curend = ts->getTics();
2244  delete ts;
2245  }
2246  int closestrun = -1;
2247  if (!connectDB())
2248  {
2249  cout << "could not connect to " << database << endl;
2250  return -1;
2251  }
2253  ostringstream cmd;
2254  map<string, set<SubsysReco *> >::const_iterator iter;
2255  // look only for runs which were actually successfully calibrated (status = 1)
2256  cmd << "SELECT runnumber,startvaltime,endvaltime FROM "
2257  << successTable << " where runnumber <= "
2258  << irun;
2259  for (iter = requiredCalibrators.begin(); iter != requiredCalibrators.end(); ++iter)
2260  {
2261  cmd << " and " << iter->first << " > 0 ";
2262  }
2263 
2264  cmd << " order by runnumber desc limit 1";
2265  ResultSet *rs = nullptr;
2266  try
2267  {
2268  rs = stmt->executeQuery(cmd.str());
2269  }
2270  catch (SQLException &e)
2271  {
2272  cout << "Table " << successTable << " does not exist" << endl;
2273  return -1;
2274  }
2275  int prevrun = 0;
2276  unsigned int prevend = 0;
2277  if (rs->next())
2278  {
2279  prevrun = rs->getInt("runnumber");
2280  unsigned int prevstart = rs->getLong("startvaltime");
2281  prevend = rs->getLong("endvaltime");
2282  if (prevrun != irun)
2283  {
2284  cout << "previous run: " << prevrun
2285  << ", start: " << prevstart
2286  << ", end: " << prevend
2287  << endl;
2288  }
2289  }
2290  else
2291  {
2292  cout << PHWHERE << " No previous good run found for run " << irun << endl;
2293  }
2294  delete rs;
2295  // if the current run fullfills requirements return immediately
2296  if (prevrun == irun)
2297  {
2298  cout << "closest run with required calibs is current run: " << irun << endl;
2299  return irun;
2300  }
2301  cmd.str("");
2302  cmd << "SELECT runnumber,startvaltime,endvaltime FROM "
2303  << successTable << " where runnumber > "
2304  << irun;
2305  for (iter = requiredCalibrators.begin(); iter != requiredCalibrators.end(); ++iter)
2306  {
2307  cmd << " and " << iter->first << " > 0 ";
2308  }
2309 
2310  cmd << " order by runnumber asc limit 1";
2311  try
2312  {
2313  rs = stmt->executeQuery(cmd.str());
2314  }
2315  catch (SQLException &e)
2316  {
2317  cout << "Table " << successTable << " does not exist" << endl;
2318  return -1;
2319  }
2320  int nextrun = 0;
2321  unsigned int nextstart = 0;
2322  if (rs->next())
2323  {
2324  nextrun = rs->getInt("runnumber");
2325  nextstart = rs->getLong("startvaltime");
2326  unsigned int nextend = rs->getLong("endvaltime");
2327  cout << "next run: " << nextrun
2328  << ", start: " << nextstart
2329  << ", end: " << nextend
2330  << endl;
2331  }
2332  else
2333  {
2334  cout << PHWHERE << " No next good run found for run " << irun << endl;
2335  }
2336  delete rs;
2337  int tdiffprev = curstart - prevend;
2338  int tdiffnext;
2339  if (nextstart > 0)
2340  {
2341  tdiffnext = nextstart - curend;
2342  }
2343  else
2344  {
2345  // just make it larger then previous run time diff
2346  tdiffnext = tdiffprev + 1;
2347  }
2348  if (tdiffprev < tdiffnext)
2349  {
2350  closestrun = prevrun;
2351  }
2352  else
2353  {
2354  closestrun = nextrun;
2355  }
2356  cout << "closest run with required calibs: " << closestrun << endl;
2357  return closestrun;
2358 }
2359 
2361 {
2362  // get sync managers and ask them for input managers and ask those for
2363  // their respective file lists. BOOST_FOREACH allows us to do loops without
2364  // having to explicietly declare all those vectors/lists
2365  BOOST_FOREACH (Fun4AllSyncManager *sync, SyncManagers)
2366  {
2367  BOOST_FOREACH (Fun4AllInputManager *inmgr, sync->GetInputManagers())
2368  {
2369  BOOST_FOREACH (string infile, inmgr->GetFileList())
2370  {
2371  pair<int, int> runseg = Fun4AllUtils::GetRunSegment(infile);
2372  runlist.insert(runseg.first);
2373  }
2374  }
2375  }
2376  return 0;
2377 }
2378 
2380 {
2381  int firstrun = *runlist.begin();
2382  int lastrun = *runlist.rbegin();
2383  time_t dummy;
2384  time_t beginticks;
2385  time_t endticks;
2386  string table = "OnCalRichCal";
2387  check_create_subsystable(table);
2388  GetRunTimeTicks(firstrun, beginticks, dummy);
2389  GetRunTimeTicks(lastrun, dummy, endticks);
2390  ostringstream stringarg;
2391  stringarg << OnCalDBCodes::COVERED;
2392  // set<int>::const_iterator runiter;
2393  /*
2394  for (runiter = runlist.begin(); runiter != runlist.end(); runiter++)
2395  {
2396  updateDB(successTable, "RichCal", stringarg.str(), *runiter);
2397  }
2398  stringarg.str("");
2399  stringarg << OnCalDBCodes::SUCCESS;
2400 
2401  updateDB(successTable, "RichCal", stringarg.str(), firstrun);
2402  */
2403  odbc::Timestamp stp;
2404  stringarg.str("");
2405  stringarg << beginticks;
2406  updateDB(table, "startvaltime", stringarg.str(), firstrun);
2407  stp.setTime(beginticks);
2408  updateDB(table, "begintime", stp.toString(), firstrun);
2409  stringarg.str("");
2410  stringarg << endticks;
2411  updateDB(table, "endvaltime", stringarg.str(), firstrun);
2412  stp.setTime(endticks);
2413  updateDB(table, "endtime", stp.toString(), firstrun);
2414  /*
2415  string tablename = "calibrichadc";
2416  odbc::Connection *con = 0;
2417  ostringstream cmd;
2418  try
2419  {
2420  con = DriverManager::getConnection("oncal", "phnxrc", "");
2421  }
2422  catch (SQLException& e)
2423  {
2424  cout << "Cannot connect to " << database.c_str() << endl;
2425  cout << e.getMessage() << endl;
2426  return -1;
2427  }
2428  Statement *stmt = 0;
2429  Statement *stmtup = 0;
2430  try
2431  {
2432  stmt = con->createStatement();
2433  stmtup = con->createStatement();
2434  }
2435  catch (SQLException& e)
2436  {
2437  cout << "Cannot create statement" << endl;
2438  cout << e.getMessage() << endl;
2439  return -1;
2440  }
2441 
2442  ResultSet *rs1 = 0;
2443  cmd.str("");
2444  cmd << "SELECT endvaltime from " << tablename
2445  << " where bankid = 1 and startvaltime = " << beginticks;
2446  cout << "sql cmd: " << cmd.str() << endl;
2447  try
2448  {
2449  rs1 = stmt->executeQuery(cmd.str());
2450  }
2451  catch (SQLException& e)
2452  {
2453  cout << "Cannot create statement" << endl;
2454  cout << e.getMessage() << endl;
2455  return -1;
2456  }
2457  if (rs1->next())
2458  {
2459  cout << "Endcaltime: " << rs1->getInt("endvaltime") << endl;
2460  cout << "future endvaltime: " << endticks << endl;
2461  cmd.str("");
2462  cmd << "Update " << tablename
2463  << " set endvaltime = " << endticks
2464  << " where bankid = 1 and startvaltime = "
2465  << beginticks;
2466  stmtup->executeUpdate(cmd.str());
2467 
2468  }
2469  else
2470  {
2471  cout << "Could not find startvaltime " << beginticks
2472  << "from run " << firstrun << endl;
2473  }
2474 
2475  */
2476 
2477  return 0;
2478 }
2479 
2480 int OnCalServer::GetCalibStatus(const string &calibname, const int runno)
2481 {
2482  int iret = -3;
2483  if (!connectDB())
2484  {
2485  cout << "could not connect to " << database << endl;
2486  return -4;
2487  }
2489  ostringstream cmd;
2490 
2491  // look only for runs which were actually successfully calibrated (status = 1)
2492  cmd << "SELECT " << calibname << " FROM "
2493  << successTable << " where runnumber = "
2494  << runno;
2495  cout << "exec " << cmd.str() << endl;
2496  ResultSet *rs = nullptr;
2497  try
2498  {
2499  rs = stmt->executeQuery(cmd.str());
2500  }
2501  catch (SQLException &e)
2502  {
2503  cout << "Table " << successTable << " does not exist" << endl;
2504  return -5;
2505  }
2506  if (rs->next())
2507  {
2508  iret = rs->getInt(calibname.c_str());
2509  }
2510  else
2511  {
2512  cout << PHWHERE << " No calib status for " << calibname
2513  << " for " << runno << endl;
2514  }
2515  delete rs;
2516  return iret;
2517 }
2518 
2519 void OnCalServer::TestMode(const int i)
2520 {
2521  const char *logname = getenv("LOGNAME");
2522  if (logname)
2523  {
2524  if (strcmp(logname, "phnxcal") == 0 || strcmp(logname, "anatrain") == 0)
2525  {
2526  cout << "phnxcal,anatrain account is not allowed to run in testmode" << endl;
2527  }
2528  else
2529  {
2530  testmode = i;
2531  }
2532  }
2533  else
2534  {
2535  cout << "could not get account via env var LOGNAME, not setting testmode" << endl;
2536  }
2537  return;
2538 }