StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEmcDbHandler.cxx
1 //******************************************************************************
2 
9 //******************************************************************************
10 
11 #include "StEmcDbHandler.h"
12 #include "TUnixTime.h"
13 #include "StMessMgr.h"
14 
15 #include "TFile.h"
16 #include "TTable.h"
17 #include "TSystem.h"
18 #include "TKey.h"
19 
20 ClassImp(StEmcDbHandler)
21 
22 //------------------------------------------------------------------------------
25 {
26  // Default table node is Calibrations_emc (see .h file)
27  setTableNode();
28  setTimeStamp();
29  setMaxEntryTime();
30  setFlavor();
31 }
32 //------------------------------------------------------------------------------
35 {
36 }
37 //------------------------------------------------------------------------------
40 {
41  StDbTable *table = 0;
43  if (dbMngr) {
44  StDbConfigNode *node = dbMngr->initConfig(mTableNode.c_str());
45  table = node ? node->addDbTable(mTableName.c_str()) : 0;
46  if (table) {
47  //set beginTime
48  dbMngr->setRequestTime(mTimeStamp.c_str());
49 
50  //set flavor
51  table->setFlavor(mFlavor.c_str());
52 
53  //set maxEntryTime
54  StDataBaseI* dbi = dbMngr->findDb(mTableNode.c_str());
55  if (dbi) {
56  UInt_t unixEntryTime = dbi->getUnixTime(mMaxTime.c_str());
57  table->setProdTime(unixEntryTime);
58 
59  dbMngr->fetchDbTable(table);
60 
61  mTimeStamp = timeToSqlTime(table->getBeginDateTime());
62  }
63  }
64  }
65 
66  return table;
67 }
68 //------------------------------------------------------------------------------
70 std::vector<std::string> StEmcDbHandler::getTimeStampList(const char * beginTime, const char * endTime)
71 {
72  mTimeStampList.clear();
73 
75  if (dbMngr) {
76  StDbTable* table = (dbMngr->initConfig(mTableNode.c_str()))->addDbTable(mTableName.c_str());
77  if (table) {
78  dbMngr->setStoreTime(mMaxTime.c_str());
79  unsigned int unixStoreTime = dbMngr->getUnixStoreTime();
80  StDataBaseI* db=dbMngr->findDb(mTableNode.c_str());
81  if (db) {
82  char whereClause[256];
83  sprintf(whereClause, " where flavor='%s' and beginTime>='%s' and beginTime<='%s' and entryTime<='%s' and (deactive=0 or deactive>=%d) order by beginTime",
84  mFlavor.c_str(), beginTime, endTime, mMaxTime.c_str(), unixStoreTime);
85  LOG_INFO <<"::getTimeStampList() using query = "<<whereClause<<endm;
86 
87  UInt_t* timeStampList = db->QueryDbTimes(table,whereClause,1); // opt=1 means don't get any data!
88 
89  int nRows=table->GetNRows();
90  int nRows2;
91  table->getElementID(nRows2);
92  if (nRows != nRows2) {
93  LOG_WARN<<"problem: nRows!=nRows2"<<endm;
94  }
95  if (timeStampList) {
96  for (int i = 0; i < nRows ; i++) {
97  dbMngr->setRequestTime(timeStampList[i]); //let db translate to Human Readable
98  mTimeStampList.push_back( timeToSqlTime(dbMngr->getDateRequestTime()) );
99  }
100  delete [] timeStampList;
101  }
102  }
103  }
104  }
105  return mTimeStampList;
106 }
107 
108 //------------------------------------------------------------------------------
109 
110 std::string StEmcDbHandler::timeToSqlTime(const char* apiTime) {
111  TString string=apiTime;
112  TString dateString=string(0,4)+ "-" +string(4,2)+ "-" + string(6,2);
113  TString timeString=string(8,2)+ ":" +string(10,2)+ ":" + string(12,2);
114  TString datetime = dateString+ " " +timeString;
115  //cout << " datetime.c_str() = " << datetime.c_str() << endl;
116  return std::string(datetime.Data());
117 }
118 
119 void StEmcDbHandler::writeToDb(char* data) {
120  if (!data) return;
122  if (dbMngr) {
123  StDbTable* table = (dbMngr->initConfig(mTableNode.c_str()))->addDbTable(mTableName.c_str());
124  if (table) {
125  table->setFlavor(mFlavor.c_str());
126  table->SetTable(data,1);
127  dbMngr->setStoreTime(mTimeStamp.c_str());
128  dbMngr->storeDbTable(table);
129  }
130  }
131 }
std::string mMaxTime
EMC database max entry time.
std::string mFlavor
flavor for BEMC tables
std::string mTableName
EMC database table name, according to input.
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
std::string mTimeStamp
EMC database table time stamp.
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
std::string mTableNode
Database node for EMC tables.
std::vector< std::string > mTimeStampList
Time stamp list of specified EMC database table.
std::vector< std::string > getTimeStampList(const char *beginTime, const char *endTime)
virtual ~StEmcDbHandler()
Class dtor.
StDbTable * getDbTable()