StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_patchpanelmap.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fms_db_patchpanelmap(char* opt = "readdb writetext") {
5 
6  // that's begin time for validity range for WRITING DB
7  // your data will be available from storeTime till
8  // next entry or 2037 for last entry
9  TString storeTime = "2007-11-09 00:00:01";
10 
11  // that's time for READING DB /
12  int date = 20141220;
13  int time = 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_PP_MAP = 4;
30 
31  // structure to fill up
32  fmsPatchPanelMap_st ppmap[MAX_PP_MAP];
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[rmod-1].ppPanel[ch]=pp;
69  ppmap[rmod-1].ppRow[ch]=row;
70  ppmap[rmod-1].ppColumn[ch]=col;
71  }
72  }
73  }
74  fclose(fp);
75  }
76 
77  if(option.Contains("readdb")){
78  gSystem->Unsetenv("DB_ACCESS_MODE");
79  gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
80 
81  gSystem->Load("StChain");
82  gSystem->Load("StBFChain");
83  gSystem->Load("StUtilities");
84  gSystem->Load("StIOMaker");
85  gSystem->Load("StarClassLibrary");
86  gSystem->Load("St_Tables");
87  gSystem->Load("StDbLib");
88  gSystem->Load("StDbBroker");
89  gSystem->Load("St_db_Maker");
90 
91  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
92  dbMk->SetDebug();
93  dbMk->SetDateTime(date,time);
94  dbMk->SetFlavor("ofl"); // for offline calibrations/mapping
95  // dbMk->SetFlavor("simu"); // for simulations
96  dbMk->Init();
97  dbMk->Make();
98 
99  // this is done inside ::Make method
100  TDataSet *DB = 0;
101  // "dbMk->" will NOT be needed.
102  // if done inside your FmsDbMaker. Simply use DB = GetInputDb("Calibrations/fms/mapping")
103  DB = dbMk->GetInputDB("Calibrations/fms/mapping");
104  if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
105 
106  // fetch ROOT descriptor of db table
107  St_fmsPatchPanelMap *dbppmap = 0;
108  dbppmap = (St_fmsPatchPanelMap*) DB->Find("fmsPatchPanelMap");
109  // fetch data and place it to appropriate structure
110  if (dbppmap) {
111  std::cout << "Reading fmsPatchPanelMap table\n";
112  fmsPatchPanelMap_st *pptable = dbppmap->GetTable();
113  memcpy(ppmap,pptable,sizeof(ppmap));
114  } else {
115  std::cout << "WARNING: No data in fmsPatchPanelMap table (wrong timestamp?). Nothing to return, then.\n";
116  }
117  }
118 
119  if(option.Contains("writetext")){
120  char* file="qtmap2pp.txt_dbout";
121  FILE* fp;
122  cout << "Writing "<<file<<"\n";
123  if(fp=fopen(file,"w")){
124  for(int mod=0; mod<4; mod++){
125  for(int ch=0; ch<maxch[module[mod]-1]; ch++){
126  fprintf(fp,"%3d %3d %3d %3d %3d\n",module[mod],ch+1,
127  ppmap[module[mod]-1].ppPanel[ch],
128  ppmap[module[mod]-1].ppRow[ch],
129  ppmap[module[mod]-1].ppColumn[ch]);
130  }
131  }
132  }
133  fclose(fp);
134  }
135 
136  if(option.Contains("writedb")) {
137  gSystem->Setenv("DB_ACCESS_MODE","write");
138  cout << "DB_ACCESS_MODE="<<gSystem->Getenv("DB_ACCESS_MODE")<<endl;
140  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
141  StDbTable* table = node->addDbTable("fmsPatchPanelMap");
142  // set store time
143  mgr->setStoreTime(storeTime.Data());
144  // store data in the table
145  table->SetTable((char*)&ppmap,MAX_PP_MAP);
146  // store table in dBase
147  mgr->storeDbTable(table);
148  gSystem->Unsetenv("DB_ACCESS_MODE");
149  std::cout << "Done with database upload \n";
150  }
151 }
152 
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