StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_qtmap.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fms_db_qtmap(char* opt = "readdb writetext",int year = 15) {
5 
6  TString option(opt);
7  std::cout << "Opt =" << opt << "\n";
8  std::cout << "testinput = " << option.Contains("testinput") << "\n";
9  std::cout << "readtext = " << option.Contains("readtext") << "\n";
10  std::cout << "readdb = " << option.Contains("readdb") << "\n";
11  std::cout << "writedb = " << option.Contains("writedb") << "\n";
12  std::cout << "writetext = " << option.Contains("writetext") << "\n";
13  std::cout << "\n";
14 
15  TString filename; // file name for READING TEXT FILE
16  TString storeTime; // storetime is begin time for validity range for WRITING DB
17  int date,time; // time for READING DB
18  std::cout << "year = " << year << "\n";
19  if(year==8){
20  filename="qtmap_run8.txt";
21  storeTime="2007-11-09 00:00:00";
22  date = 20080301;
23  time = 0;
24  }else if(year==9){
25  filename="qtmap2009V1.txt";
26  storeTime="2008-11-09 00:00:00";
27  date = 20090301;
28  time = 0;
29  }else if(year==15){
30  filename="qtmap2009V1.txt";
31  storeTime="2014-12-20 00:00:00";
32  //date = 20140222;
33  date = 20141220;
34  time = 0;
35  }
36  else{
37  std::cout << "Please specify year\n";
38  exit;
39  }
40  std::cout << "filename =" << filename << "\n";
41  std::cout << "storetime =" << storeTime << "\n";
42  std::cout << "date,time =" << date <<" "<< time << "\n";
43 
44  gROOT->Macro("LoadLogger.C");
45  gSystem->Load("St_base.so");
46  gSystem->Load("libStDb_Tables.so");
47  gSystem->Load("StDbLib.so");
48 
49  // max index dimensions
50  const Int_t MAX_QT_MAP = 2;
51 
52  // structure to fill up
53  fmsQTMap_st qtmap[MAX_QT_MAP];
54 
55  if(option.Contains("testinput")){
56  // fill only first entry out of 548 for testing purposes:
57  qtmap[0].qtCrate[0][0][0] = 15;
58  qtmap[0].qtSlot[0][0][0] = 15;
59  qtmap[0].qtChannel[0][0][0] = 15;
60 
61  qtmap[1].qtCrate[0][0][0] = 17;
62  qtmap[1].qtSlot[0][0][0] = 17;
63  qtmap[1].qtChannel[0][0][0] = 17;
64  }
65 
66  if(option.Contains("readtext")){
67  char* file=filename.Data();
68  FILE* fp;
69  int rns,rpp,rrow,rcol,rqt,rcrt,rch;
70  cout << "Reading "<<file<<"\n";
71  if(fp=fopen(file,"r")){
72  for(int ns=0; ns<2; ns++){
73  for(int pp=0; pp<2; pp++){
74  for(int row=0; row<20; row++){
75  for(int col=0; col<16; col++){
76  if(!feof(fp)) fscanf(fp,"%d %d %d %d %d %d %d",&rns,&rpp,&rrow,&rcol,&rqt,&rcrt,&rch);
77  //printf("%d %d %d %d %d %d %d\n",rns,rpp,rrow,rcol,rqt,rcrt,rch);
78  if(rns == -1 && rpp == -1 && rrow == -1 && rcol == -1){
79  qtmap[ns].qtCrate[pp][row][col] = 0;
80  qtmap[ns].qtSlot[pp][row][col] = 0;
81  qtmap[ns].qtChannel[pp][row][col] = 0;
82  }else{
83  if(rns !=ns +1) cout << "Error ns# ="<<rns <<" "<<ns +1<<"\n";
84  if(rpp !=pp +1) cout << "Error pp# ="<<rpp <<" "<<pp +1<<"\n";
85  if(rrow!=row+1) cout << "Error row# ="<<rrow<<" "<<row+1<<"\n";
86  if(rcol!=col+1) cout << "Error col# ="<<rcol<<" "<<col+1<<"\n";
87  qtmap[ns].qtCrate[pp][row][col] = rqt;
88  qtmap[ns].qtSlot[pp][row][col] = rcrt;
89  qtmap[ns].qtChannel[pp][row][col] = rch;
90  }
91  }
92  }
93  }
94  }
95  }
96  fclose(fp);
97  }
98 
99  if(option.Contains("readdb")){
100  gSystem->Unsetenv("DB_ACCESS_MODE");
101  gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
102 
103  gSystem->Load("StChain");
104  gSystem->Load("StBFChain");
105  gSystem->Load("StUtilities");
106  gSystem->Load("StIOMaker");
107  gSystem->Load("StarClassLibrary");
108  gSystem->Load("St_Tables");
109  gSystem->Load("StDbLib");
110  gSystem->Load("StDbBroker");
111  gSystem->Load("St_db_Maker");
112 
113  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
114  dbMk->SetDebug();
115  dbMk->SetDateTime(date,time);
116  dbMk->SetFlavor("ofl"); // for offline calibrations/mapping
117  // dbMk->SetFlavor("simu"); // for simulations
118  dbMk->Init();
119  dbMk->Make();
120 
121  // this is done inside ::Make method
122  TDataSet *DB = 0;
123  // "dbMk->" will NOT be needed.
124  // if done inside your FmsDbMaker. Simply use DB = GetInputDb("Calibrations/fms/mapping")
125  DB = dbMk->GetInputDB("Calibrations/fms/mapping");
126  if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
127 
128  // fetch ROOT descriptor of db table
129  St_fmsQTMap *dbmap = 0;
130  dbmap = (St_fmsQTMap*) DB->Find("fmsQTMap");
131  // fetch data and place it to appropriate structure
132  if (dbmap){
133  std::cout << "Reading fmsQTMap table\n";
134  fmsQTMap_st *qttable = dbmap->GetTable();
135  memcpy(qtmap,qttable,sizeof(qtmap));
136  } else {
137  std::cout << "WARNING: No data in fmsQTMap table (wrong timestamp?). Nothing to return, then.\n";
138  }
139  }
140 
141  if(option.Contains("writetext")){
142  filename+="_dbout";
143  char* file=filename.Data();
144  FILE* fp;
145  int crt,slt,ch;
146  cout << "Writing "<<file<<"\n";
147  if(fp=fopen(file,"w")){
148  for(int ns=0; ns<2; ns++){
149  for(int pp=0; pp<2; pp++){
150  for(int row=0; row<20; row++){
151  for(int col=0; col<16; col++){
152  if(qtmap[ns].qtCrate[pp][row][col] ==0 &&
153  qtmap[ns].qtSlot[pp][row][col] ==0 &&
154  qtmap[ns].qtChannel[pp][row][col] ==0 ){
155  if(year==8) fprintf(fp,"%3d %2d %3d %3d %3d %3d %3d\n",-1,-1,-1,-1,-1,-1,-1);
156  if(year==9) fprintf(fp,"%3d %3d %4d %4d %4d %4d %4d\n",-1,-1,-1,-1,-1,-1,-1);
157  }else{
158  if(year==8) fprintf(fp,"%3d %2d %3d %3d %3d %3d %3d\n",ns+1,pp+1,row+1,col+1,
159  qtmap[ns].qtCrate[pp][row][col],
160  qtmap[ns].qtSlot[pp][row][col],
161  qtmap[ns].qtChannel[pp][row][col]);
162  if(year==9||year==15) fprintf(fp,"%3d %3d %4d %4d %4d %4d %4d\n",ns+1,pp+1,row+1,col+1,
163  qtmap[ns].qtCrate[pp][row][col],
164  qtmap[ns].qtSlot[pp][row][col],
165  qtmap[ns].qtChannel[pp][row][col]);
166  }
167  }
168  }
169  }
170  }
171  }
172  fclose(fp);
173  }
174 
175  if(option.Contains("writedb")) {
176  gSystem->Setenv("DB_ACCESS_MODE","write");
177  cout << "DB_ACCESS_MODE="<<gSystem->Getenv("DB_ACCESS_MODE")<<endl;
179  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
180  StDbTable* table = node->addDbTable("fmsQTMap");
181  mgr->setStoreTime(storeTime.Data());
182  // store data in the table
183  table->SetTable((char*)&qtmap,MAX_QT_MAP);
184  // set store time
185  // store table in dBase
186  mgr->storeDbTable(table);
187  gSystem->Unsetenv("DB_ACCESS_MODE");
188  std::cout << "Done with database upload \n";
189  }
190 }
191 
virtual Int_t Make()
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
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362