StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_map.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fms_db_map(char* opt = "readdb") {
5 
6  // that's begin time for validity range for WRITING TO DB
7  // your data will be available from storeTime till 2037
8  TString storeTime = "2007-11-09 00:00:00";
9 
10  // this is data and time for READING FROM DB
11  int readDate = 20141220;
12  //int readDate = 20140222;
13  int readTime = 0;
14 
15  TString option(opt);
16  std::cout << "Opt =" << opt << "\n";
17  std::cout << "testinput = " << option.Contains("testinput") << "\n";
18  std::cout << "readtext = " << option.Contains("readtext") << "\n";
19  std::cout << "readdb = " << option.Contains("readdb") << "\n";
20  std::cout << "writedb = " << option.Contains("writedb") << "\n";
21  std::cout << "writetext = " << option.Contains("writetext") << "\n";
22 
23  gROOT->Macro("LoadLogger.C");
24  gSystem->Load("St_base.so");
25  gSystem->Load("libStDb_Tables.so");
26  gSystem->Load("StDbLib.so");
27 
28  // max index dimensions
29  const Int_t MAX = 2000;
30 
31  // structure to fill up
32  fmsMap_st ppmap[MAX];
33 
34  if(option.Contains("testinput")){
35  // fill only first entry out of 548 for testing purposes:
36  ppmap[0].ppPanel[0] = 2; // 1-2
37  ppmap[0].ppColumn[0] = 7; // 1-20
38  ppmap[0].ppRow[0] = 15; // 1-16
39 
40  ppmap[1].ppPanel[0] = 1; // 1-2
41  ppmap[1].ppColumn[0] = 3; // 1-20
42  ppmap[1].ppRow[0] = 7; // 1-16
43 
44  ppmap[2].ppPanel[0] = 2; // 1-2
45  ppmap[2].ppColumn[0] = 20; // 1-20
46  ppmap[2].ppRow[0] = 15; // 1-16
47 
48  ppmap[3].ppPanel[0] = 1; // 1-2
49  ppmap[3].ppColumn[0] = 13; // 1-20
50  ppmap[3].ppRow[0] = 9; // 1-16
51  }
52 
53  int module[4] = {1,3,2,4};
54  int maxch[4] = {578,578,288,288};
55 
56  if(option.Contains("readtext")){
57  char* file="qtmap2pp.txt";
58  FILE* fp;
59  int rmod,rch,pp,col,row;
60  cout << "Reading "<<file<<"\n";
61  if(fp=fopen(file,"r")){
62  for(int mod=0; mod<4; mod++){
63  for(int ch=0; ch<maxch[module[mod]-1]; ch++){
64  if(!feof(fp)) fscanf(fp,"%d %d %d %d %d",&rmod,&rch,&pp,&row,&col);
65  //printf("%5d %5d %5d %5d %5d\n",rmod,rch,pp,row,col);
66  if(rmod!=module[mod]) cout << "Error mod# ="<<rmod<<" "<<module[mod]<<"\n";
67  if(rch !=ch +1) cout << "Error ch# ="<<rch <<" "<<ch+1<<"\n";
68  ppmap[mod].ppPanel[ch]=pp;
69  ppmap[mod].ppRow[ch]=row;
70  ppmap[mod].ppColumn[ch]=col;
71  }
72  }
73  }
74  fclose(fp);
75  }
76 
77  if(option.Contains("readdb")){
78  gSystem->Load("StChain");
79  gSystem->Load("StBFChain");
80  gSystem->Load("StUtilities");
81  gSystem->Load("StIOMaker");
82  gSystem->Load("StarClassLibrary");
83  gSystem->Load("St_Tables");
84  gSystem->Load("StDbLib");
85  gSystem->Load("StDbBroker");
86  gSystem->Load("St_db_Maker");
87 
88  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
89  dbMk->SetDebug();
90  dbMk->SetDateTime(readDate,readTime);
91  dbMk->SetFlavor("ofl"); // for offline calibrations/mapping
92  // dbMk->SetFlavor("simu"); // for simulations
93  dbMk->Init();
94  dbMk->Make();
95 
96  // this is done inside ::Make method
97  TDataSet *DB = 0;
98  // "dbMk->" will NOT be needed.
99  // if done inside your FmsDbMaker. Simply use DB = GetInputDb("Calibrations/fms/mapping")
100  DB = dbMk->GetInputDB("Calibrations/fms/mapping");
101  if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
102 
103  // fetch ROOT descriptor of db table
104  St_fmsPatchPanelMap *dbppmap = 0;
105  dbppmap = (St_fmsPatchPanelMap*) DB->Find("fmsPatchPanelMap");
106  // fetch data and place it to appropriate structure
107  if (dbppmap) {
108  std::cout << "Reading fmsPatchPanelMap table\n";
109  fmsPatchPanelMap_st *pptable = dbppmap->GetTable();
110  for(int mod=0; mod<4; mod++){
111  for(int ch=0; ch<maxch[module[mod]-1]; ch++){
112  printf("%3d %3d %3d %3d %3d\n",module[mod],ch+1,
113  int(pptable[mod].ppPanel[ch]),
114  int(pptable[mod].ppRow[ch]),
115  int(pptable[mod].ppColumn[ch]) );
116  }
117  }
118  memcpy(ppmap,pptable,sizeof(ppmap));
119  } else {
120  std::cout << "WARNING: No data in fmsPatchPanelMap table (wrong timestamp?). Nothing to return, then.\n";
121  }
122  }
123 
124  if(option.Contains("writetext")){
125  char* file="qtmap2pp_out.txt";
126  FILE* fp;
127  cout << "Writing "<<file<<"\n";
128  if(fp=fopen(file,"w")){
129  for(int mod=0; mod<4; mod++){
130  for(int ch=0; ch<maxch[module[mod]-1]; ch++){
131  fprintf(fp,"%3d %3d %3d %3d %3d\n",module[mod],ch+1,
132  ppmap[mod].ppPanel[ch],
133  ppmap[mod].ppRow[ch],
134  ppmap[mod].ppColumn[ch]);
135  }
136  }
137  }
138  fclose(fp);
139  }
140 
141  if(option.Contains("writedb")) {
142  //putenv("DB_ACCESS_MODE=write");
143  //char* env = getenv("DB_ACCESS_MODE");
144  //cout << "Setting DB_ACCESS_MODE " << env << endl;
146  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
147  StDbTable* table = node->addDbTable("fmsPatchPanelMap");
148  mgr->setStoreTime(storeTime.Data());
149  // store data in the table
150  table->SetTable((char*)&ppmap,MAX_PP_MAP);
151  // set store time
152  // store table in dBase
153  mgr->storeDbTable(table);
154  //unsetenv("DB_ACCESS_MODE");
155  std::cout << "Done with database upload \n";
156  }
157 }
158 
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