StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_fmsgaincorrB.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 // max index dimensions
5 static const Int_t MAX = 2500;
6 static const Int_t MAXD = 14;
7 static const Int_t MAXCH= 578;
8 
9 int getDetectorId(int ew, int nstb) {return (ew-1)*8+nstb-1;}
10 int getEW(int detid) {return detid/8 + 1;}
11 int getNSTB(int detid) {return detid%8 + 1;}
12 
13 void fms_db_fmsgaincorrB(char* option = "writetext 15sim", char* dataspec="allone",
14  char* storetime="2007-11-09 12:00:00",
15  int readDate=20071210, int readTime=0, int isSim=0) {
16  // that's begin time for validity range for WRITING TO DB
17  // your data will be available from storeTime till 2037
18  TString opt(option);
19  TString data(dataspec);
20  TString storeTime(storetime);
21  if(opt.Contains("run8") && opt.Contains("dAu200")){
22  storeTime = "2007-11-09 12:00:00";
23  }else if(opt.Contains("run8") && opt.Contains("pp200")){
24  storeTime = "2008-01-28 12:00:00";
25  }else if(opt.Contains("run9") && opt.Contains("pp200")){
26  storeTime = "2009-01-16 00:00:00";
27  }else if(opt.Contains("15sim")){
28  storeTime = "2014-12-10 00:00:01"; readDate = 20141215; readTime = 0; isSim=1;
29  }else if(opt.Contains("15ofl")){
30  storeTime = "2014-12-20 00:00:00"; readDate = 20141225; readTime = 0;
31  }else if(opt.Contains("17sim")){
32  storeTime = "2016-12-10 00:00:01"; readDate = 20161215; readTime = 0; isSim=1;
33  }else if(opt.Contains("17ofl")){
34  storeTime = "2016-12-20 00:00:00"; readDate = 20161225; readTime = 0;
35  }else {
36  std::cout<<"Invalid year range"<<std::endl;
37  }
38 
39  std::cout << "Opt =" << option << "\n";
40  std::cout << "testinput = " << opt.Contains("testinput") << "\n";
41  std::cout << "readtext = " << opt.Contains("readtext") << "\n";
42  std::cout << "readdb = " << opt.Contains("readdb") << "\n";
43  std::cout << "writedb = " << opt.Contains("writedb") << "\n";
44  std::cout << "writetext = " << opt.Contains("writetext") << "\n";
45  std::cout << "dataspec = " << dataspec << endl;
46  std::cout << "storeTime = " << storeTime << endl;
47  std::cout << "readData = " << readDate << " readTime="<<readTime<<endl;
48  std::cout << "isSim = " << isSim << endl;
49 
50  gROOT->Macro("LoadLogger.C");
51  gSystem->Load("St_base.so");
52  gSystem->Load("libStDb_Tables.so");
53  gSystem->Load("StDbLib.so");
54 
55  // structure to fill up
56  fmsGainCorrectionB_st corr;
57  memset(&corr,0,sizeof(corr));
58 
59  if(data.Contains("allone")){
60  int maxch[MAXD] = {49, 49, 7, 7, 48, 48, 48, 48, 578, 578, 288, 288, 108, 108};
61  int index=0;
62  for(int det=8; det<=11; det++){
63  for(int ch=0; ch<maxch[det]; ch++){
64  corr.detectorId[index]=det;
65  corr.ch [index]=ch+1;
66  corr.corr [index]=1.0;
67  printf("n=%4d det=%2d ch=%3d corr=%8.6f\n",
68  index,corr.detectorId[index],corr.ch[index],corr.corr[index]);
69  index++;
70  }
71  }
72  }
73 
74  if(opt.Contains("readtext")){
75  FILE* fp;
76  int rew,rnstb,rch;
77  float rcorr;
78  int nread = 0;
79  cout << "Reading "<<dataspec<<"\n";
80  if(fp=fopen(dataspec,"r")){
81  while(fscanf(fp,"%d %d %d %f",&rew,&rnstb,&rch,&rcorr) != EOF){
82  int detid=getDetectorId(rew,rnstb);
83  if(detid<0 || rch<1) {
84  printf("Invalid ch#: det=%d ew=%d nstb=%d rch=%d\n",detid,rew,rnstb,rch,rcorr);
85  continue; //hack! ignore
86  }
87  if(rew==1) continue; //hack! ignore east
88  corr.detectorId[nread]=detid;
89  corr.ch[nread]=rch;
90  corr.corr[nread]=rcorr;
91  printf("n=%4d det=%2d ew=%1d nstb=%1d ch=%3d corr=%8.6f\n",nread,detid,rew,rnstb,rch,rcorr);
92  nread++;
93  }
94  }
95  fclose(fp);
96  printf("read %d channels of gain corr\n",nread);
97  }
98 
99  if(opt.Contains("writedb")) {
100  gSystem->Setenv("DB_ACCESS_MODE","write");
101  //putenv("DB_ACCESS_MODE=write");
102  //char* env = getenv("DB_ACCESS_MODE");
103  //cout << "Setting DB_ACCESS_MODE " << env << endl;
105  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
106  StDbTable* table = node->addDbTable("fmsGainCorrectionB");
107  mgr->setStoreTime(storeTime.Data());
108  // store data in the table
109  table->SetTable((char*)&corr,1);
110  // set store time
111  // store table in dBase
112  if(isSim) table->setFlavor("sim");
113  mgr->storeDbTable(table);
114  //StDbTable* table = node->findTable("fmsGain");
115  //node->removeTable(table);
116  gSystem->Unsetenv("DB_ACCESS_MODE");
117  //unsetenv("DB_ACCESS_MODE");
118  std::cout << "Done with database upload"<< endl;
119  }
120 
121  if(opt.Contains("readdb")){
122  gSystem->Load("StChain");
123  gSystem->Load("StBFChain");
124  gSystem->Load("StUtilities");
125  gSystem->Load("StIOMaker");
126  gSystem->Load("StarClassLibrary");
127  gSystem->Load("St_Tables");
128  gSystem->Load("StDbLib");
129  gSystem->Load("StDbBroker");
130  gSystem->Load("St_db_Maker");
131 
132  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
133  dbMk->SetDebug();
134  dbMk->SetDateTime(readDate,readTime);
135  dbMk->SetFlavor("ofl"); // for offline calibrations/mapping
136  if(isSim) dbMk->SetFlavor("sim"); // for simulations
137  dbMk->Init();
138  dbMk->Make();
139 
140  // this is done inside ::Make method
141  TDataSet *DB = 0;
142  // "dbMk->" will NOT be needed.
143  // if done inside your FmsDbMaker. Simply use DB = GetInputDb("Calibrations/fms/mapping")
144  DB = dbMk->GetInputDB("Calibrations/fms");
145  if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
146 
147  // fetch ROOT descriptor of db table
148  St_fmsGainCorrectionB *dbppmap = 0;
149  dbppmap = (St_fmsGainCorrectionB*) DB->Find("fmsGainCorrectionB");
150  // fetch data and place it to appropriate structure
151  int ngain = 0;
152  if (dbppmap) {
153  std::cout << "Reading fmsGainCorrectionB table\n";
154  fmsGainCorrectionB_st *pptable = dbppmap->GetTable();
155  for(int i=0; i<MAX; i++){
156  if(pptable[0].ch[i]>0){
157  printf("%3d%8d%8d%8.4f\n",
158  int(pptable[0].detectorId[i]/8+1),int(pptable[0].detectorId[i]%8+1),
159  int(pptable[0].ch[i]),float(pptable[0].corr[i]) );
160  ngain++;
161  }
162  }
163  } else {
164  std::cout << "WARNING: No data in fmsGain table (wrong timestamp?). Nothing to return, then.\n";
165  }
166  }
167 }
168 
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