StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
storetofZbCorr.C
1 // $Id: storetofZbCorr.C,v 1.3 2014/11/24 22:18:54 geurts Exp $
2 // macro to upload tofr5 INL tables to database
3 //
4 // based on http://www.star.bnl.gov/STAR/comp/db/StoreDbTable.cc.html
5 //
6 // Xin Dong, 02/18/2005
7 // ---
8 // $Log: storetofZbCorr.C,v $
9 // Revision 1.3 2014/11/24 22:18:54 geurts
10 // Add striciter protection against non-existing files (bail out), and reduce excessive std output
11 //
12 // Revision 1.2 2011/06/01 00:44:01 geurts
13 // Store board-based input files as cell-based database entries
14 //
15 // Revision 1.1 2010/12/14 19:27:28 geurts
16 // *** empty log message ***
17 //
18 // ---
19 
20 #include <iostream>
21 #include <fstream>
22 #include <string>
23 #include "iomanip.h"
24 using namespace std;
25 
26 
27 void storetofZbCorr(const Bool_t mTest = 1)
28 {
29  const int mNTray = 120;
30  const int mNTDIG = 8;
31  const int mNVPD = 19;
32  const int mNMODULE = 32;
33  const int mNCELL = 6;
34  const int mNMODPERBOARD = 4;
35 
36  //-- load dBase and Table definition libraries
37  gSystem->Load("St_base");
38  gSystem->Load("StChain");
39  gSystem->Load("StUtilities");
40  gSystem->Load("St_Tables.so");
41 
42  gSystem->Load("StDbLib.so");
43  gSystem->Load("libStDb_Tables.so");
44 
45  //-- get the singleton manager
46  StDbManager* dbManager = StDbManager::Instance();
47 
48  //-- connect to the db & get an empty container
49  StDbConfigNode* configNode = dbManager->initConfig("Calibrations_tof");
50 
51  //----------------------------------------
52 // TString StoreTime = "2008-02-01 00:00:03";
53 // TString StoreTime = "2008-03-04 16:00:01";
54 // TString StoreTime = "2009-03-15 00:00:00";
55 // TString StoreTime = "2009-11-01 00:00:00"; // 200 GeV preliminary
56 // TString StoreTime = "2010-03-18 18:00:00"; // 62 GeV preliminary
57 // TString StoreTime = "2010-04-08 15:00:00"; // 39 GeV preliminary
58  ifstream inTime;
59  inTime.open("input/timestamp");
60  string time;
61  if (inTime.is_open()) {
62  getline(inTime, time);
63  inTime.close();
64  } else {
65  cout << " Unable to open the TimeStamp file! EXIT! " << endl;
66  return;
67  }
68  TString StoreTime = time;
69  cout << " Store Time " << StoreTime.Data() << endl;
70 
71  //-- add table to the container with descriptor given by Database
72  StDbTable* tofZbCorr = configNode->addDbTable("tofZbCorr");
73 
74  //-- fill structures & store times
75  //tofZbCorr_st *zcorr= new tofZbCorr_st[mNTray*mNTDIG];
76  tofZbCorr_st *zcorr= new tofZbCorr_st[23040];
77 
78  //=======================================
79  // read in pvpdTot file by file.
80  ifstream infile;
81  //Double_t X[mNTray][mNTDIG][60];
82  //Double_t Y[mNTray][mNTDIG][60];
83  Double_t X[mNTray][mNMODULE][mNCELL][60];
84  Double_t Y[mNTray][mNMODULE][mNCELL][60];
85 
86  infile.open("input/zCali_4DB.dat");
87  if (!infile.is_open()){
88  cerr <<" unable to open input/zCali_4DB.dat; bailing out ..." << endl;
89  exit(-1);
90  }
91 
92  int calibSize;
93  infile >> calibSize;
94  cout << "reading in " << calibSize << " calibration records ... " << endl;
95  switch (calibSize) {
96  case 960 : // TDIG BOARD BASED
97  for(int i=0;i<mNTray;i++) {
98  for(int j=0;j<mNTDIG;j++) {
99  int tray, board, nnn;
100  infile >> tray >> board;
101  infile >> nnn;
102  //cout << " tray = " << tray << " board = " << board << endl;
103  for(int k=0;k<60;k++) {
104  if(nnn>0&&k<nnn+1) {
105  infile >> X[tray-1][board-1][0][k];
106  } else {
107  X[tray-1][board-1][0][k] = 0.0;
108  }
109  }
110  for(int k=0;k<60;k++) {
111  if(nnn>0&&k<nnn+1) {
112  infile >> Y[tray-1][board-1][0][k];
113  } else {
114  Y[tray-1][board-1][0][k] = 0.0;
115  }
116  }
117  }
118  }
119  break;
120  case 23040 : // CELL BASED
121  for(int i=0;i<mNTray;i++) {
122  for(int j=0;j<mNMODULE;j++) {
123  for(int jj=0;jj<mNCELL;jj++) {
124  int tray, module, cell, nnn;
125  infile >> tray >> module >> cell;
126  infile >> nnn;
127  //cout << " tray = " << tray << " module = " << module << " cell = " << cell << endl;
128  for(int k=0;k<60;k++) {
129  if(nnn>0&&k<nnn+1) {
130  infile >> X[tray-1][module-1][cell-1][k];
131  } else {
132  X[tray-1][module-1][cell-1][k] = 0.0;
133  }
134  }
135  for(int k=0;k<60;k++) {
136  if(nnn>0&&k<nnn+1) {
137  infile >> Y[tray-1][module-1][cell-1][k];
138  } else {
139  Y[tray-1][module-1][cell-1][k] = 0.0;
140  }
141  }
142  } //cell
143  } //module
144  } //tray
145  break;
146  default: // DON'T KNOW -- BAIL OUT
147  cerr<< "Unknown calib-size " << calibSize << "; bailing out ... " << endl;
148  exit(-2);
149 } // switch
150 
151  infile.close();
152 
153 // prepare database records
154  cout << "preparing database records ... " << endl;
155 switch (calibSize) {
156  case 960:
157  int index=-1;
158  for (int tray=1;tray<mNTray+1;tray++){
159  for (int module=1;module<mNMODULE+1;module++){
160  for (int cell=1;cell<mNCELL+1;cell++){
161  index++;
162  zcorr[index].trayId = (Short_t)tray;
163  zcorr[index].moduleId = (Short_t)module;
164  zcorr[index].cellId = (Short_t)cell;
165  int board = ((module-1)/mNMODPERBOARD) + 1;
166  for(int j=0;j<60;j++) {
167  zcorr[index].z[j] = X[tray-1][board-1][0][j];
168  zcorr[index].corr[j] = Y[tray-1][board-1][0][j];
169  }
170  } // cell
171  } // module
172  } // tray
173 // for(int i=0;i<mNTray*mNTDIG;i++) {
174 // int tray = i/mNTDIG + 1;
175 // int board = i%mNTDIG + 1;
176 // int module = (i%mNTDIG) * 4 + 1; // set to be the first module on this board
177 // int cell = 1; // set to 1
178 // zcorr[i].trayId = tray;
179 // zcorr[i].moduleId = module;
180 // zcorr[i].cellId = cell;
181 // for(int j=0;j<60;j++) {
182 // zcorr[i].z[j] = X[tray-1][board-1][j];
183 // zcorr[i].corr[j] = Y[tray-1][board-1][j];
184 // }
185 // }
186  break;
187 case 23040:
188  int index=-1;
189  for (int tray=1;tray<mNTray+1;tray++){
190  for (int module=1;module<mNMODULE+1;module++){
191  for (int cell=1;cell<mNCELL+1;cell++){
192  index++;
193  zcorr[index].trayId = tray;
194  zcorr[index].moduleId = module;
195  zcorr[index].cellId = cell;
196  for(int j=0;j<60;j++) {
197  zcorr[index].z[j] = X[tray-1][module-1][cell-1][j];
198  zcorr[index].corr[j] = Y[tray-1][module-1][cell-1][j];
199  }
200  } // cell
201  } // module
202  } // tray
203  break;
204  default: // DON'T KNOW -- BAIL OUT
205  cerr<< "Unknown calib-size " << calibSize << "; bailing out ... " << endl;
206  exit(-2);
207 }// switch
208 
209 // Store records in test file
210  cout << "Storing records in zCorr_test.dat (this may take a long time) ... " << endl;
211 // int nRow = mNTray * mNTDIG;
212 // int nRow = calibSize;
213  int nRow = 23040;
214  ofstream outData;
215  outData.open("zCorr_test.dat");
216  for(int i=0;i<nRow;i++) {
217  outData << setw(6) << zcorr[i].trayId << setw(6) << zcorr[i].moduleId << setw(6) << zcorr[i].cellId << endl;
218  for(int j=0;j<60;j++) {
219  if(fabs(zcorr[i].z[j])<1.e-4 && fabs(zcorr[i].corr[j])<1.e-4) continue;
220  outData << setw(15) << zcorr[i].z[j];
221  }
222  outData << endl;
223  for(int j=0;j<60;j++) {
224  if(fabs(zcorr[i].z[j])<1.e-4 && fabs(zcorr[i].corr[j])<1.e-4) continue;
225  outData << setw(15) << zcorr[i].corr[j];
226  }
227  outData << endl;
228  }
229  outData.close();
230 
231  if(!mTest) {
232  cout<<" prepare to upload data to DB"<<endl;
233  //- store data in table
234  tofZbCorr->SetTable((char*)zcorr, nRow);
235  //- set store time
236  dbManager->setStoreTime(StoreTime.Data());
237  //- store table in dBase
238  dbManager->storeDbTable(tofZbCorr);
239  cout<<"uploaded"<<endl;
240  // return 0;
241  }
242 }
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155