StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
put2DB.C
1 /*
2  root.exe lDb.C 'put2DB.C("StarDb/Geometry/tpc/TpcHalfPosition.r2016.C")'
3  */
4 //#include "SvtIndexMap.h"
5 class TTable;
6 TTable *table = 0;
7 class StDbTable;
8 StDbTable* dbTable = 0;
9 class StDbManager;
10 StDbManager* mgr = 0;
11 Int_t DT = 0;
12 #if 0
13 //________________________________________________________________________________
14 void Load() {
15  // gSystem->Load("libTable");
16  gSystem->Load("libStDb_Tables");
17  Char_t *mysql = "libmysqlclient";
18  Char_t *libs[] = {"", "/usr/mysql/lib/", "/usr/lib/mysql/","/usr/lib/", 0}; // "$ROOTSYS/mysql-4.1.20/lib/",
19  //Char_t *libs[] = {"/usr/lib/", 0};
20  Int_t i = 0;
21  while ((libs[i])) {
22  TString lib(libs[i]);
23  lib += mysql;
24  lib = gSystem->ExpandPathName(lib.Data());
25  if (gSystem->DynamicPathName(lib,kTRUE)) {
26  gSystem->Load(lib.Data()); cout << " + " << lib.Data() << endl;
27  break;
28  }
29  i++;
30  }
31  gSystem->Load("liblog4cxx.so");
32  gSystem->Load("libSt_base"); // TMemStat::PrintMem("load St_base");
33  gSystem->Load("libStStarLogger.so");
34  gROOT->ProcessLine("StLoggerManager::StarLoggerInit();"); // TMemStat::PrintMem("load StStarLogger");
35  gSystem->Load("StDbLib");
36 
37 }
38 #endif
39 //________________________________________________________________________________
40 void put2DB(const char* file=
41  "$STAR/StarDb/Geometry/svt/svtWafersPosition.20050101.000200.C"
42  /*
43  setenv DB_ACCESS_MODE write # from Michael 02/26/07
44  update beginTime if necessary
45  and
46  UPDATE svtDriftCorrection set entryTime=entryTime,beginTime=''2005-01-01 00:00:00' where beginTime like '2005-01-01%';
47  */
48  ){
49  gSystem->Setenv("DB_ACCESS_MODE","write");
50 #if 0
51  Load();
52 #endif
53  TString bName(gSystem->BaseName(file));// cout << bName << endl;
54  Int_t indx = bName.Index(".");
55  TString TName(bName.Data(),indx); cout << "Table name " << TName << endl;
56  TString Time(bName.Data()+indx+1);
57  Time.ReplaceAll("C","");
58  Time.ReplaceAll("root","");// cout << "Time " << Time << endl;
59  // 1996-12-01 23:59:59
60  // Int_t d=19960101;
61  Int_t d=20000101;
62  Int_t t = 0;
63  // sscanf(Time.Data(),"%d",&d);
64  Int_t n = sscanf(Time.Data(),"%d.%d",&d,&t);
65  if (n != 2) {
66  Char_t tag[10];
67  n = sscanf(Time.Data(),"%s",&tag);
68  if (n == 1) {
69  d = StMaker::AliasDate(tag);
70  t = StMaker::AliasTime(tag);
71  cout << "n = " << n << " tag: " << tag << " d = " << d << " t = " << t << endl;
72  }
73  }
74  TDatime Date(d,t); cout << " Date " << Date.GetDate() << "\tTime " << Date.GetTime() << endl;
75  TString dName(gSystem->DirName(file));
76  TString DB("");
77  Int_t indx = dName.Index("StarDb/");
78  if (indx >= 0) {
79  DB = dName.Data()+indx+7;
80  DB.ReplaceAll("/","_");
81  } else {
82  DB = dName;
83  }
84  TString fName(file); cout << "Load " << fName << " to DB: " << DB << endl;
85  if (DB == "") return;
86  if (gClassTable->GetID("StDbManager") < 0) Load();
87  if (fName.EndsWith(".root")) {
88  TFile *f = new TFile(file);
89  table = (TTable *) f->Get(TName.Data());
90  }
91  else {
92  gROOT->LoadMacro(file);
93  table = (TTable *) CreateTable();
94  }
95  if (! table) {
96  cout << "Table has not been found" << endl;
97  return;
98  }
99  TString TType("St_");
100  TType += table->GetTitle(); cout << "\t" << TType << endl;
101  if (TType.EndsWith("_st")) {
102  TType = TString(TType,TType.Length()-3);
103  cout << "\t" << TType << endl;
104  }
105  cout << "name = " << table->GetName() << "\ttype = " << TType << endl;
106  cout << "ProcessLine(\"" << Form("%s *myTable = (%s *) table",TType.Data(),TType.Data()) << "\");" << endl;
107  gInterpreter->ProcessLine(Form("%s *myTable = (%s *) table",TType.Data(),TType.Data()));
108  if (! myTable) return;
109  cout<< " --- Save to DB "<<endl;
110  Int_t rowSize = myTable -> GetRowSize(); // Get the size (in bytes) of each row - fixes gap/padding problem
112  TD->storeRowSize(rowSize);
113  mgr = StDbManager::Instance();
114  mgr->setVerbose(kTRUE);
115  // mgr->setUser("yuri","c@lib");
116  // mgr->setUser("","");
117  StDbConfigNode* node=mgr->initConfig(DB.Data());
118  dbTable=node->addDbTable(TName.Data());
119  cout << " --- Store table " << TName << endl;
120  dbTable->checkDescriptor();
121  Int_t N = myTable->GetNRows();
122  Int_t NN = N;
123  if (NN > 10) NN = 10;
124  // myTable->Print(0,NN);
125  if ( myTable->IsA()->InheritsFrom( "St_tpcCorrection" ) ) {
126  // enlarge table up to 50 rows
127  const Int_t Nmax = 50;
128  if (N > Nmax) {cout << "Table has " << N << " more than " << Nmax << " rows. Possible BUG " << endl; return;}
129  myTable->ReAllocate(Nmax);
130  tpcCorrection_st row;
131  memset(&row, 0, sizeof(tpcCorrection_st));
132  for (Int_t i = N; i < Nmax; i++) myTable->AddAt(&row);
133  tpcCorrection_st *r = myTable->GetTable();
134  for (Int_t i = 0; i < N; i++, r++) {
135  r->idx = i+1;
136  r->nrows = N;
137  }
138  myTable->Print(0,N+1);
139  N = Nmax;
140  }
141  if ( myTable->IsA()->InheritsFrom( "St_svtHybridDriftVelocity" ) ) {
142  // enlarge table up to 432 rows
143  const Int_t Nmax = 432;
144  if (N > Nmax) {cout << "Table has " << N << " more than " << Nmax << " rows. Possible BUG " << endl; return;}
145  myTable->ReAllocate(Nmax);
146  svtHybridDriftVelocity_st row;
147  memset(&row, 0, sizeof(svtHybridDriftVelocity_st));
148  for (Int_t i = N; i < Nmax; i++) myTable->AddAt(&row);
149  svtHybridDriftVelocity_st *r = myTable->GetTable();
150  for (Int_t i = 0; i < N; i++, r++) {
151  r->idx = i+1;
152  r->nrows = N;
153  }
154  myTable->Print(0,N+1);
155  N = Nmax;
156  }
157  Int_t *rowIDs = new Int_t[N];
158  Int_t offset = 1;
159  if (N == 1) offset = 0;
160  if (TName.Contains("tpcDriftVelocity") ||TName.Contains("ssdConfiguration") || TName.Contains("trgTimeOffset")) offset = 0;
161 #ifndef _SvtIndexMap_h
162  if (TName.Contains("svtWafersPosition")) {cout << "Un comment SvtIndexMap include" << endl; return;}
163  for(Int_t ti=0;ti<N;ti++) rowIDs[ti]=ti + offset;
164 #else
165  if (TName.Contains("svtWafersPosition")) {
166  offset = 0; // Mike found that for svt offset should be 0
167  St_svtWafersPosition *S = (St_svtWafersPosition *) table;
168  svtWafersPosition_st *s = S->GetTable();
169  for(Int_t ti=0;ti<N;ti++, s++) {
170  Int_t elementID = -1;
171  for (Int_t j = 0; j < N; j++) {
172  Int_t layer = 2*SvtMap[j].Barrel - 1 + SvtMap[j].Ladder%2;
173  Int_t Id = 1000*layer + 100*SvtMap[j].Wafer + SvtMap[j].Ladder;
174  if (Id != SvtMap[j].Id) {
175  cout << "Mismatch for ID " << s->ID
176  << " in SvtMap "
177  << SvtMap[j].name << "\t"
178  << SvtMap[j].Id << "\t"
179  << SvtMap[j].Index << "\t"
180  << SvtMap[j].elementID << "\t"
181  << SvtMap[j].Barrel << "\t"
182  << SvtMap[j].Ladder << "\t"
183  << SvtMap[j].Wafer << endl;
184  return;
185  }
186  if (Id == s->ID) {
187  cout << "Found match for ID " << s->ID
188  << " in SvtMap "
189  << SvtMap[j].name << "\t"
190  << SvtMap[j].Id << "\t"
191  << SvtMap[j].Index << "\t"
192  << SvtMap[j].elementID << "\t"
193  << SvtMap[j].Barrel << "\t"
194  << SvtMap[j].Ladder << "\t"
195  << SvtMap[j].Wafer << endl;
196 
197  elementID = SvtMap[j].elementID;
198  break;
199  }
200  }
201  if (elementID < 0) {
202  cout << "Don't find match for ID " << s->ID << endl;
203  return;
204  }
205  rowIDs[ti]= elementID;
206  }
207  } else {
208  for(Int_t ti=0;ti<N;ti++) rowIDs[ti]=ti + offset;
209  }
210 #endif
211  Char_t* gstr = (Char_t*) myTable->GetTable();
212  dbTable->SetTable(gstr,N,rowIDs);
213 #if 1
214  Int_t status = 0;
215  Int_t maxIter = 1; // 5
216  for (Int_t i = 0; i < maxIter; i++) {
217  TString TimeStamp(Form("%8d.%06d",d,t+i)); cout << "TimeStamp " << TimeStamp << endl;
218  // TDatime dt(d,t+i);
219  TUnixTime dt(d,t+i,1);
220  DT = dt();
221  // continue;
222  mgr->setStoreTime(DT);
223  status = mgr->storeDbTable(dbTable);
224  if(!status) {
225  if (i+1 < maxIter) cout<<" ------> error storing in DB --> try to change TimeStamp by 1 sec"<<endl;
226  } else {break;}
227  }
228  if (status) cout << " ------> Done" << endl;
229  else cout << " ------> Failed" << endl;
230  delete [] rowIDs;
231 #endif
232 }
233 /*
234  select * from tpcDriftVelocity where beginTime > "2014" and beginTime < "2014-03-14 19:37:58" and deactive=1423867472 order by beginTime ;
235  select * from tpcDriftVelocity where beginTime > "2014-06-16 10:06:06" and beginTime < "2014-07-30 19:38:58" and deactive=1423867472 order by beginTime ;
236 
237  update tpcDriftVelocity set deactive=0, entryTime = entryTime where deactive = 1423867472 and ( beginTime > "2014" and beginTime < "2014-03-14 19:37:58");
238  update tpcDriftVelocity set deactive=0, entryTime = entryTime where deactive = 1423867472 and ( beginTime >"2014-06-16 10:06:06" and beginTime < "2014-07-30 19:38:58");
239 
240 */
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
Definition: TTable.h:48
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155