Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

StDbModifier.cxx

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * 00003 * $Id: StDbModifier.cxx,v 1.5 2005/09/07 22:04:02 deph Exp $ 00004 * 00005 * Author: Masashi Kaneta, updated by R. Jeff Porter 00006 *************************************************************************** 00007 * 00008 * Description: Class to act between Root-Cint files and database 00009 * 00010 *************************************************************************** 00011 * 00012 * $Log: StDbModifier.cxx,v $ 00013 * Revision 1.5 2005/09/07 22:04:02 deph 00014 * update to correct padding issue for packed tables 00015 * 00016 * Revision 1.4 2003/09/02 17:57:49 perev 00017 * gcc 3.2 updates + WarnOff 00018 * 00019 * Revision 1.3 2002/03/15 22:08:07 porter 00020 * fixed retval in modifier 00021 * 00022 * Revision 1.2 2002/03/13 22:14:53 porter 00023 * added variable length table defautls for simplifying writes of l3 counters to db 00024 * 00025 * Revision 1.1 2000/08/15 22:51:52 porter 00026 * Added Root2DB class from Masashi Kaneta 00027 * + made code more robust against requesting data from non-existent databases 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 // constructor of StDbModifier 00055 00056 00057 // fTimestamp = new char[30]; 00058 // strcpy(fTimestamp,"2038-01-01 04:59:59"); 00059 // Timestamp of the data requested. 00060 // To get recent one, here, it is assigned as end of UNIX time. 00061 00062 fDebug = 0; // set No-debug mode. 00063 00064 fDbName = 0; // set Database name on DB server as brank. 00065 fTableName = 0; // set Table name on DB server as brank. 00066 // If either fDbName or fTableName is still 0 00067 // in ReadDataFromBD() and WriteDataToDB(), 00068 // the program will be terminated. 00069 fVersionName = 0; 00070 00071 fFlavorName = 0; 00072 00073 fOutputFileName = new char[200]; 00074 strcpy(fOutputFileName,"./database_data.C"); 00075 // default name of a file to be filled data from database. 00076 00077 fInputFileName = new char[200]; 00078 strcpy(fInputFileName,"./database_data.C"); 00079 // default name of a file to be read data from ascii file. 00080 } 00081 00082 //_____________________________________________________________________________ 00083 StDbModifier::~StDbModifier() 00084 { 00085 // destructor of StDbModifier 00086 if(fTimestamp) delete [] fTimestamp; 00087 00088 } 00089 00090 //_____________________________________________________________________________ 00091 Int_t StDbModifier::ReadDataFromDB() 00092 { 00093 // This funciton will connect database server, get informaion of table and 00094 // make a file that contains data of table in database. 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(); // Get the singleton manager 00115 if ( fDebug == 1 ){ 00116 bool isVerbose = true; 00117 mgr->setVerbose(isVerbose); // Set verbose mode for debuging for fDebug=1 00118 } 00119 00120 StDbConfigNode* configNode = mgr -> initConfig(fDbName); // Connect to the db & get an empty container 00121 00122 StDbTable* dbtable; 00123 if ( fVersionName == 0 ){ 00124 dbtable = configNode -> addDbTable(fTableName); 00125 // Add a table to the container with descriptor given by Database with wsing version name as "default" 00126 }else{ 00127 dbtable = configNode -> addDbTable(fTableName,fVersionName); 00128 // Add a table to the container with descriptor given by Database with wsing version name specified . 00129 } 00130 if ( dbtable == 0 ){ // If table asigned by fTableName does not exist in Dababase 00131 cout << " No Table : " << fTableName << endl; // program is stoped and exit from this function. 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 // mgr -> setRequestTime(fTimestamp); // Set the request time 00151 mgr -> fetchDbTable(dbtable); // Fetch the data from Database 00152 00153 void* cstruct = dbtable -> GetTableCpy(); // Get pointer of table and copy to c-structure 00154 Int_t nrows = dbtable -> GetNRows(); // Get number of raws in the table 00155 00156 TTable* table = TTable::New(fTableName,fTableName,cstruct,nrows); 00157 // Create new TTable object for c-structure 00158 00159 ofstream ofs(fOutputFileName); // Open a file 00160 table -> SavePrimitive(ofs,0); // Write information of c-structure from object of TTable to the file 00161 ofs.close(); // Close the file 00162 00163 return 1; 00164 } 00165 00166 00167 //_____________________________________________________________________________ 00168 Int_t StDbModifier::WriteDataToDB() 00169 { 00170 // This funciton will make a table as object of TTable. 00171 // Data of the table will be read from a file that is generated from 00172 // StDbModifier::ReadDataFromDB(). 00173 // After data is read, connection to database server is made and data will 00174 // be stored in database. 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; // Make a string to load Root C macro generated 00196 command = ".L "; // by ReadDataFromDB() 00197 command += fInputFileName; 00198 00199 if ( fDebug == 1 ) printf(" LoadTable: %s\n",(const char*)command); 00200 00201 gInterpreter->ProcessLine(command); // Load the file in CINT 00202 TTable* table = (TTable*) gInterpreter->Calc("CreateTable()"); // execute comand in the file 00203 command.ReplaceAll(".L ",".U "); // chage comand .L to .U 00204 gInterpreter->ProcessLine(command); // unload file from CINT 00205 00206 void* cstruct = table -> GetArray(); // Get pointer of table and copy to c-structure 00207 Int_t nrows = table -> GetNRows(); // Get number of raws in the table 00208 //MPD - below is need TTable info to correct c-array 8 byte padding issue 00209 //could have broke it out - but I chose to keep all the TTable (root) stuff together 00210 00211 Int_t rowSize = table -> GetRowSize(); // Get the size (in bytes) of each row - fixes gap/padding problem 00212 // Int_t nCols = table -> GetNumberOfColumns(); 00213 00214 StDbTableDescriptor* TD = new StDbTableDescriptor(); 00215 TD->storeRowSize(rowSize); 00216 //table -> Print(0,2); //uncomment for debugging 00217 00218 00219 00220 StDbManager* mgr = StDbManager::Instance(); // Get the singleton manager 00221 if ( fDebug == 1 ){ 00222 bool isVerbose = true; 00223 mgr->setVerbose(isVerbose); // Set verbose mode for debuging for fDebug=1 00224 } 00225 StDbConfigNode* configNode = mgr -> initConfig(fDbName); // Connect to the db & get an empty container 00226 00227 StDbTable* dbtable; 00228 if ( fVersionName == 0 ){ 00229 dbtable = configNode -> addDbTable(fTableName); 00230 // Add a table to the container with descriptor given by Database with wsing version name as "default" 00231 }else{ 00232 dbtable = configNode -> addDbTable(fTableName,fVersionName); 00233 // Add a table to the container with descriptor given by Database with wsing version name specified . 00234 } 00235 00236 00237 00238 if ( dbtable == 0 ){ // If table asigned by fTableName does not exist in Dababase, 00239 cout << " No Table : " << fTableName << endl; // program is stoped and exit from this function. 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); // Put data in local table on memory 00265 // dbtable -> SetNRows(nrows); // Set number of rows on table in database 00266 00267 // mgr -> setStoreTime(fTimestamp); // Set the time stamp 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; // Fetch the data 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

Generated on Thu Aug 24 14:45:26 2006 for Doxygen by doxygen 1.3.7