StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_fmsgaincorr.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_fmsgaincorr(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  }
32  //else std::cout<<"Invalid year range"<<std::endl;
33 
34  std::cout << "Opt =" << option << "\n";
35  std::cout << "testinput = " << opt.Contains("testinput") << "\n";
36  std::cout << "readtext = " << opt.Contains("readtext") << "\n";
37  std::cout << "readdb = " << opt.Contains("readdb") << "\n";
38  std::cout << "writedb = " << opt.Contains("writedb") << "\n";
39  std::cout << "writetext = " << opt.Contains("writetext") << "\n";
40  std::cout << "dataspec = " << dataspec << endl;
41  std::cout << "storeTime = " << storeTime << endl;
42  std::cout << "readData = " << readDate << " readTime="<<readTime<<endl;
43  std::cout << "isSim = " << isSim << endl;
44 
45  gROOT->Macro("LoadLogger.C");
46  gSystem->Load("St_base.so");
47  gSystem->Load("libStDb_Tables.so");
48  gSystem->Load("StDbLib.so");
49 
50  // structure to fill up
51  fmsGainCorrection_st corr[MAX];
52  int idx[MAXD][MAXCH];
53  memset(idx,-1,sizeof(idx));
54  for(int i=0; i<MAX; i++){
55  corr[i].detectorId=-1;
56  corr[i].ch =-1;
57  corr[i].corr =-1.0;
58  }
59 
60  int detId[MAXD] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
61  int detType[MAXD]= { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5};
62  int detNx[MAXD] = { 7, 7, 7, 7, 48, 48, 1, 1, 17, 17, 12, 12, 9, 9};
63  int detNy[MAXD] = { 7, 7, 1, 1, 1, 1, 48, 48, 34, 34, 24, 24, 12, 12};
64  int ew[MAXD] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
65  int nstb[MAXD] = { 1, 2, 5, 6, 3, 4, 7, 8, 1, 2, 3, 4, 1, 2};
66  int maxch[MAXD] = {49, 49, 7, 7, 48, 48, 48, 48, 578, 578, 288, 288, 108, 108};
67 
68  int index=0;
69  for(int det=0; det<MAXD; det++){
70  for(int ch=0; ch<maxch[det]; ch++){
71  corr[index].detectorId=det;
72  corr[index].ch =ch+1;
73  corr[index].corr =0.0;
74  idx[det][ch] =index;
75  if(data.Contains("allone")) corr[index].corr=1.0;
76  index++;
77  }
78  }
79 
80  if(opt.Contains("readtext")){
81  FILE* fp;
82  int rew,rnstb,rch;
83  float rcorr;
84  int nread = 0;
85  cout << "Reading "<<dataspec<<"\n";
86  if(fp=fopen(dataspec,"r")){
87  while(fscanf(fp,"%d %d %d %f",&rew,&rnstb,&rch,&rcorr) != EOF){
88  int detid=getDetectorId(rew,rnstb);
89  int index=idx[detid][rch-1];
90  printf("EW=%5d NSTB=%5d CH=%5d Gain=%6.4f | det=%2d idx=%4d\n",rew,rnstb,rch,rcorr,detid,index);
91  if(rew==1) continue; //hack! ignore east
92  if(detid != corr[index].detectorId) printf("ERR DetId %d != %d\n",detid,corr[index].detectorId);
93  if(rch != corr[index].ch) printf("ERR Ch %d != %d\n",rch,corr[index].ch);
94  corr[index].corr=rcorr;
95  nread++;
96  }
97  }
98  fclose(fp);
99  printf("read %d channels of gain corr\n",nread);
100  }
101 
102  if(opt.Contains("readdb")){
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(readDate,readTime);
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");
126  if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
127 
128  // fetch ROOT descriptor of db table
129  St_fmsGainCorrection *dbppmap = 0;
130  dbppmap = (St_fmsGainCorrection*) DB->Find("fmsGainCorrection");
131  // fetch data and place it to appropriate structure
132  int ngain = 0;
133  if (dbppmap) {
134  std::cout << "Reading fmsGainCorrection table\n";
135  fmsGainCorrection_st *pptable = dbppmap->GetTable();
136  for(int iew=0; iew<2; iew++){
137  for(int mod=0; mod<4; mod++){
138  for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
139  printf("%3d%8d%8d%8.4f\n",int(pptable[ngain].detectorId/8+1),int(pptable[ngain].detectorId%8+1),
140  int(pptable[ngain].ch),float(pptable[ngain].corr) );
141  ngain++;
142  }
143  }
144  }
145  memcpy(corr,pptable,sizeof(corr));
146  }
147  else {
148  std::cout << "WARNING: No data in fmsGain table (wrong timestamp?). Nothing to return, then.\n";
149  }
150  }
151 
152  if(opt.Contains("writetext")){
153  int ii = data.Length();
154  data.Insert(ii-4,"_out");
155  char* file=data.Data();
156  FILE* fp;
157  cout << "Writing "<<file<<"\n";
158  int ngain = 0;
159  if(fp=fopen(file,"w")){
160  for(int i=0; i<MAX; i++){
161  if(corr[i].corr>=0.0){
162  int detid=corr[i].detectorId;
163  int iew=getEW(detid);
164  int instb=getNSTB(detid);
165  fprintf(fp,"%d %d %d %5.3f\n",iew,instb,
166  corr[i].ch,corr[i].corr);
167  ngain++;
168  }
169  }
170  }
171  fclose(fp);
172  printf("Write %d channels\n",ngain);
173  }
174 
175  if(opt.Contains("writedb")) {
176  gSystem->Setenv("DB_ACCESS_MODE","write");
177  //putenv("DB_ACCESS_MODE=write");
178  //char* env = getenv("DB_ACCESS_MODE");
179  //cout << "Setting DB_ACCESS_MODE " << env << endl;
181  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
182  StDbTable* table = node->addDbTable("fmsGainCorrection");
183  mgr->setStoreTime(storeTime.Data());
184  // store data in the table
185  table->SetTable((char*)&corr,index+1);
186  // set store time
187  // store table in dBase
188  if(isSim) table->setFlavor("sim");
189  mgr->storeDbTable(table);
190  //StDbTable* table = node->findTable("fmsGain");
191  //node->removeTable(table);
192  gSystem->Unsetenv("DB_ACCESS_MODE");
193  //unsetenv("DB_ACCESS_MODE");
194  std::cout << "Done with database upload with nrow=" << index+1 << endl;
195  }
196 }
197 
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