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