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