00001
00002 class TTable;
00003 TTable *table = 0;
00004 class StDbTable;
00005 StDbTable* dbTable = 0;
00006 class StDbManager;
00007 StDbManager* mgr = 0;
00008 Int_t DT = 0;
00009
00010 void Load() {
00011
00012 gSystem->Load("libStDb_Tables");
00013 gSystem->Load("libmysqlclient");
00014 gSystem->Load("liblog4cxx.so");
00015 gSystem->Load("libSt_base");
00016 gSystem->Load("libStStarLogger.so");
00017 gROOT->ProcessLine("StLoggerManager::StarLoggerInit();");
00018 gSystem->Load("StDbLib");
00019
00020 }
00021
00022 void put2DB(const char* file=
00023 "$STAR/StarDb/Geometry/svt/svtWafersPosition.20050101.000200.C"
00024
00025
00026
00027
00028
00029
00030 ){
00031 TString bName(gSystem->BaseName(file));
00032 Int_t indx = bName.Index(".");
00033 TString TName(bName.Data(),indx); cout << "Table name " << TName << endl;
00034 TString Time(bName.Data()+indx+1);
00035 Time.ReplaceAll("C","");
00036 Time.ReplaceAll("root","");
00037
00038
00039 int d=20000101;
00040 int t = 0;
00041
00042 sscanf(Time.Data(),"%d.%d",&d,&t);
00043 TDatime Date(d,t); cout << " Date " << Date.GetDate() << "\tTime " << Date.GetTime() << endl;
00044 TString dName(gSystem->DirName(file));
00045 TString DB("");
00046 Int_t indx = dName.Index("StarDb/");
00047 if (indx >= 0) {
00048 DB = dName.Data()+indx+7;
00049 DB.ReplaceAll("/","_");
00050 }
00051 TString fName(file); cout << "Load " << fName << " to DB: " << DB << endl;
00052 if (DB == "") return;
00053 if (gClassTable->GetID("StDbManager") < 0) Load();
00054 if (fName.EndsWith(".root")) {
00055 TFile *f = new TFile(file);
00056 table = (TTable *) f->Get(TName.Data());
00057 }
00058 else {
00059 gROOT->LoadMacro(file);
00060 table = (TTable *) CreateTable();
00061 }
00062 if (! table) {
00063 cout << "Table has not been found" << endl;
00064 return;
00065 }
00066 TString TType("St_");
00067 TType += table->GetTitle(); cout << "\t" << TType << endl;
00068 if (TType.EndsWith("_st")) {
00069 TType = TString(TType,TType.Length()-3);
00070 cout << "\t" << TType << endl;
00071 }
00072 cout << "name = " << table->GetName() << "\ttype = " << TType << endl;
00073 cout << "ProcessLine(\"" << Form("%s *myTable = (%s *) table",TType.Data(),TType.Data()) << "\");" << endl;
00074 gInterpreter->ProcessLine(Form("%s *myTable = (%s *) table",TType.Data(),TType.Data()));
00075 if (! myTable) return;
00076 cout<< " --- Save to DB "<<endl;
00077 Int_t rowSize = myTable -> GetRowSize();
00078 StDbTableDescriptor* TD = new StDbTableDescriptor();
00079 TD->storeRowSize(rowSize);
00080 mgr = StDbManager::Instance();
00081 mgr->setVerbose(kTRUE);
00082
00083
00084 StDbConfigNode* node=mgr->initConfig(DB.Data());
00085 dbTable=node->addDbTable(TName.Data());
00086 cout << " --- Store table " << TName << endl;
00087 dbTable->checkDescriptor();
00088 Int_t N = myTable->GetNRows();
00089 Int_t NN = N;
00090 if (NN > 10) NN = 10;
00091
00092 if ( myTable->IsA()->InheritsFrom( "St_tpcCorrection" ) ) {
00093
00094 const Int_t Nmax = 50;
00095 if (N > Nmax) {cout << "Table has " << N << " more than " << Nmax << " rows. Possible BUG " << endl; return;}
00096 myTable->ReAllocate(Nmax);
00097 tpcCorrection_st row;
00098 memset(&row, 0, sizeof(tpcCorrection_st));
00099 for (int i = N; i < Nmax; i++) myTable->AddAt(&row);
00100 tpcCorrection_st *r = myTable->GetTable();
00101 for (int i = 0; i < N; i++, r++) {
00102 r->idx = i+1;
00103 r->nrows = N;
00104 }
00105 myTable->Print(0,N+1);
00106 N = Nmax;
00107 }
00108 if ( myTable->IsA()->InheritsFrom( "St_svtHybridDriftVelocity" ) ) {
00109
00110 const Int_t Nmax = 432;
00111 if (N > Nmax) {cout << "Table has " << N << " more than " << Nmax << " rows. Possible BUG " << endl; return;}
00112 myTable->ReAllocate(Nmax);
00113 svtHybridDriftVelocity_st row;
00114 memset(&row, 0, sizeof(svtHybridDriftVelocity_st));
00115 for (int i = N; i < Nmax; i++) myTable->AddAt(&row);
00116 svtHybridDriftVelocity_st *r = myTable->GetTable();
00117 for (int i = 0; i < N; i++, r++) {
00118 r->idx = i+1;
00119 r->nrows = N;
00120 }
00121 myTable->Print(0,N+1);
00122 N = Nmax;
00123 }
00124 Int_t *rowIDs = new Int_t[N];
00125 Int_t offset = 1;
00126 if (N == 1) offset = 0;
00127 if (TName.Contains("tpcDriftVelocity") ||TName.Contains("ssdConfiguration") || TName.Contains("trgTimeOffset")) offset = 0;
00128 #ifndef _SvtIndexMap_h
00129 if (TName.Contains("svtWafersPosition")) {cout << "Un comment SvtIndexMap include" << endl; return;}
00130 for(int ti=0;ti<N;ti++) rowIDs[ti]=ti + offset;
00131 #else
00132 if (TName.Contains("svtWafersPosition")) {
00133 offset = 0;
00134 St_svtWafersPosition *S = (St_svtWafersPosition *) table;
00135 svtWafersPosition_st *s = S->GetTable();
00136 for(int ti=0;ti<N;ti++, s++) {
00137 Int_t elementID = -1;
00138 for (Int_t j = 0; j < N; j++) {
00139 Int_t layer = 2*SvtMap[j].Barrel - 1 + SvtMap[j].Ladder%2;
00140 Int_t Id = 1000*layer + 100*SvtMap[j].Wafer + SvtMap[j].Ladder;
00141 if (Id != SvtMap[j].Id) {
00142 cout << "Mismatch for ID " << s->ID
00143 << " in SvtMap "
00144 << SvtMap[j].name << "\t"
00145 << SvtMap[j].Id << "\t"
00146 << SvtMap[j].Index << "\t"
00147 << SvtMap[j].elementID << "\t"
00148 << SvtMap[j].Barrel << "\t"
00149 << SvtMap[j].Ladder << "\t"
00150 << SvtMap[j].Wafer << endl;
00151 return;
00152 }
00153 if (Id == s->ID) {
00154 cout << "Found match for ID " << s->ID
00155 << " in SvtMap "
00156 << SvtMap[j].name << "\t"
00157 << SvtMap[j].Id << "\t"
00158 << SvtMap[j].Index << "\t"
00159 << SvtMap[j].elementID << "\t"
00160 << SvtMap[j].Barrel << "\t"
00161 << SvtMap[j].Ladder << "\t"
00162 << SvtMap[j].Wafer << endl;
00163
00164 elementID = SvtMap[j].elementID;
00165 break;
00166 }
00167 }
00168 if (elementID < 0) {
00169 cout << "Don't find match for ID " << s->ID << endl;
00170 return;
00171 }
00172 rowIDs[ti]= elementID;
00173 }
00174 } else {
00175 for(int ti=0;ti<N;ti++) rowIDs[ti]=ti + offset;
00176 }
00177 #endif
00178 Char_t* gstr = (Char_t*) myTable->GetTable();
00179 dbTable->SetTable(gstr,N,rowIDs);
00180 #if 1
00181 Int_t status = 0;
00182 Int_t maxIter = 1;
00183 for (int i = 0; i < maxIter; i++) {
00184 TString TimeStamp(Form("%8d.%06d",d,t+i)); cout << "TimeStamp " << TimeStamp << endl;
00185
00186 TUnixTime dt(d,t+i,1);
00187 DT = dt();
00188
00189 mgr->setStoreTime(DT);
00190 status = mgr->storeDbTable(dbTable);
00191 if(!status) {
00192 cout<<" ------> error storing in DB --> try to change TimeStamp by 1 sec"<<endl;
00193 } else {break;}
00194 }
00195 if (status) cout << " ------> Done" << endl;
00196 else cout << " ------> Failed" << endl;
00197 delete [] rowIDs;
00198 #endif
00199 }