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