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