00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifdef __ROOT__
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039
00040
00041 #include "TROOT.h"
00042 #include "TInterpreter.h"
00043
00044 #ifndef __STDB_STANDALONE__
00045 #include "Stiostream.h"
00046 #include "StMessMgr.h"
00047 #else
00048 #define LOG_DEBUG cout
00049 #define LOG_INFO cout
00050 #define LOG_WARN cout
00051 #define LOG_ERROR cerr
00052 #define LOG_FATAL cerr
00053 #define LOG_QA cout
00054 #define endm "\n"
00055 #endif
00056
00057
00058 #include "TTable.h"
00059
00060 #include "StDbManager.hh"
00061 #include "StDbConfigNode.hh"
00062 #include "StDbTable.h"
00063 #include "StDbDefs.hh"
00064 #include "StDbTableDescriptor.h"
00065
00066 #include "StDbModifier.h"
00067
00068
00069 ClassImp(StDbModifier)
00070
00071
00072 StDbModifier::StDbModifier() : funixTime(0), fTimestamp(0)
00073 {
00074
00075
00076
00077
00078
00079
00080
00081
00082 fDebug = 0;
00083
00084 fDbName = 0;
00085 fTableName = 0;
00086
00087
00088
00089 fVersionName = 0;
00090
00091 fFlavorName = 0;
00092
00093 fOutputFileName = new char[200];
00094 strcpy(fOutputFileName,"./database_data.C");
00095
00096
00097 fInputFileName = new char[200];
00098 strcpy(fInputFileName,"./database_data.C");
00099
00100 }
00101
00102
00103 StDbModifier::~StDbModifier()
00104 {
00105
00106 if(fTimestamp) delete [] fTimestamp;
00107
00108 }
00109
00110
00111 Int_t StDbModifier::ReadDataFromDB()
00112 {
00113
00114
00115
00116 if( funixTime==0 && !(fTimestamp)) {
00117 cout<< " No timestamp specified " << endl;
00118 return 0;
00119 }
00120
00121 if ( fDbName == 0 ){
00122 cout << " StDbModifier; Set Database name by " << endl;
00123 cout << " StDbModifier::setDbName(TString dbname)";
00124 cout << endl;
00125 return 0;
00126 }
00127 if ( fTableName == 0 ){
00128 cout << " StDbModifier; Set table name by " << endl;
00129 cout << " StDbModifier::setTableName(TString tablename)";
00130 cout << endl;
00131 return 0;
00132 }
00133
00134 StDbManager* mgr = StDbManager::Instance();
00135 if ( fDebug == 1 ){
00136 bool isVerbose = true;
00137 mgr->setVerbose(isVerbose);
00138 }
00139
00140 StDbConfigNode* configNode = mgr -> initConfig(fDbName);
00141
00142 StDbTable* dbtable;
00143 if ( fVersionName == 0 ){
00144 dbtable = configNode -> addDbTable(fTableName);
00145
00146 }else{
00147 dbtable = configNode -> addDbTable(fTableName,fVersionName);
00148
00149 }
00150 if ( dbtable == 0 ){
00151 cout << " No Table : " << fTableName << endl;
00152 return 0;
00153 }
00154
00155 if ( fFlavorName != 0 ){
00156 dbtable -> setFlavor(fFlavorName);
00157 cout << "Flavor is set as " << fFlavorName << " by StDbTable::setFlavor." << endl;
00158 }else{
00159 cout << "Flavor is NOT assigned. Default value is set as 'ofl'. " << endl;
00160 dbtable -> setFlavor("ofl");
00161 }
00162
00163
00164 if(funixTime) {
00165 mgr->setRequestTime(funixTime);
00166 } else {
00167 mgr->setRequestTime(fTimestamp);
00168 }
00169
00170
00171 mgr -> fetchDbTable(dbtable);
00172
00173 void* cstruct = dbtable -> GetTableCpy();
00174 Int_t nrows = dbtable -> GetNRows();
00175
00176 TTable* table = TTable::New(fTableName,fTableName,cstruct,nrows);
00177
00178
00179 ofstream ofs(fOutputFileName);
00180 table -> SavePrimitive(ofs,0);
00181 ofs.close();
00182
00183 return 1;
00184 }
00185
00186
00187
00188 Int_t StDbModifier::WriteDataToDB()
00189 {
00190
00191
00192
00193
00194
00195
00196
00197 if( funixTime==0 && !(fTimestamp)) {
00198 cout<< " No timestamp specified " << endl;
00199 return 0;
00200 }
00201
00202 if ( fDbName == 0 ){
00203 cout << " StDbModifier; Set Database name by " << endl;
00204 cout << " StDbModifier::setDbName(TString dbname)";
00205 cout << endl;
00206 return 0;
00207 }
00208 if ( fTableName == 0 ){
00209 cout << " StDbModifier; Set table name by " << endl;
00210 cout << " StDbModifier::setTableName(TString tablename)";
00211 cout << endl;
00212 return 0;
00213 }
00214
00215 TString command;
00216 command = ".L ";
00217 command += fInputFileName;
00218
00219 if ( fDebug == 1 ) LOG_INFO<<" LoadTable: "<<(const char*)command<<endm;
00220
00221 gInterpreter->ProcessLine(command);
00222 TTable* table = (TTable*) gInterpreter->Calc("CreateTable()");
00223 command.ReplaceAll(".L ",".U ");
00224 gInterpreter->ProcessLine(command);
00225
00226 void* cstruct = table -> GetArray();
00227 Int_t nrows = table -> GetNRows();
00228
00229
00230
00231 Int_t rowSize = table -> GetRowSize();
00232
00233
00234 StDbTableDescriptor* TD = new StDbTableDescriptor();
00235 TD->storeRowSize(rowSize);
00236
00237
00238
00239
00240 StDbManager* mgr = StDbManager::Instance();
00241 if ( fDebug == 1 ){
00242 bool isVerbose = true;
00243 mgr->setVerbose(isVerbose);
00244 }
00245 StDbConfigNode* configNode = mgr -> initConfig(fDbName);
00246
00247 StDbTable* dbtable;
00248 if ( fVersionName == 0 ){
00249 dbtable = configNode -> addDbTable(fTableName);
00250
00251 }else{
00252 dbtable = configNode -> addDbTable(fTableName,fVersionName);
00253
00254 }
00255
00256
00257
00258 if ( dbtable == 0 ){
00259 cout << " No Table : " << fTableName << endl;
00260 return 0;
00261 }
00262
00263 if ( fFlavorName != 0 ){
00264 cout << "set Flavor" << endl;
00265 dbtable -> setFlavor(fFlavorName);
00266 cout << "Flavor is set as " << fFlavorName << " by StDbTable::setFlavor." << endl;
00267 }else{
00268 cout << "Flavor is NOT assigned. Default value is set as 'ofl'. " << endl;
00269 dbtable -> setFlavor("ofl");
00270 }
00271
00272 int dbnrows=dbtable->GetNRows();
00273 int* eidList=0;
00274 int tmp;
00275 int* dbeidList=dbtable->getElementID(tmp);
00276 if(nrows>dbnrows){
00277 eidList=new int[nrows];
00278 int i;
00279 for(i=0;i<dbnrows;i++)eidList[i]=dbeidList[i];
00280 for(i=dbnrows;i<nrows;i++)eidList[i]=eidList[dbnrows-1]+i;
00281 }
00282
00283
00284 dbtable -> SetTable((char*)cstruct,nrows,eidList);
00285
00286
00287
00288 if(funixTime) {
00289 mgr->setStoreTime(funixTime);
00290 } else {
00291 mgr->setStoreTime(fTimestamp);
00292 }
00293
00294 int retVal=0;
00295 if(mgr -> storeDbTable(dbtable)) retVal=1;
00296 if(eidList) delete [] eidList;
00297
00298
00299 return retVal;
00300 }
00301
00302
00303 void StDbModifier::SetDateTime(const char* timestamp)
00304 {
00305 fTimestamp = new char[strlen(timestamp)+1];
00306 strcpy(fTimestamp,timestamp);
00307 }
00308
00309 void StDbModifier::SetTime(unsigned int time){ funixTime=time;}
00310
00311
00312 void StDbModifier::SetDbName(const char* dbname)
00313 {
00314 fDbName = new char[strlen(dbname)+1];
00315 strcpy(fDbName,dbname);
00316 }
00317
00318
00319 void StDbModifier::SetInputFileName(const char* inputfilename)
00320 {
00321 fInputFileName = new char[strlen(inputfilename)+1];
00322 strcpy(fInputFileName,inputfilename);
00323 }
00324
00325
00326 void StDbModifier::SetOutputFileName(const char* outputfilename)
00327 {
00328 fOutputFileName = new char[strlen(outputfilename)+1];
00329 strcpy(fOutputFileName,outputfilename);
00330 }
00331
00332
00333 void StDbModifier::SetTableName(const char* tablename)
00334 {
00335 fTableName = new char[strlen(tablename)+1];
00336 strcpy(fTableName,tablename);
00337 }
00338
00339
00340 void StDbModifier::SetVersionName(const char* versionname)
00341 {
00342 fVersionName = new char[strlen(versionname)+1];
00343 strcpy(fVersionName,versionname);
00344 }
00345
00346
00347 void StDbModifier::SetFlavor(const char* flavorname)
00348 {
00349 fFlavorName = new char[strlen(flavorname)+1];
00350 strcpy(fFlavorName,flavorname);
00351 cout << " Flavor is set " << flavorname << endl;
00352 cout << " Flavor is set " << fFlavorName << endl;
00353 }
00354
00355 #endif