StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_fmsgainB.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_fmsgainB(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  }else if(opt.Contains("17sim")){
30  TString storeTime = "2016-12-10 00:00:01"; readDate = 20161215; readTime = 0; isSim=1;
31  }else if(opt.Contains("17ofl")){
32  TString storeTime = "2016-12-20 00:00:00"; readDate = 20161225; readTime = 0;
33  }
34  else std::cout<<"Invalid year range"<<std::endl;
35 
36  std::cout << "Opt =" << option << "\n";
37  std::cout << "testinput = " << opt.Contains("testinput") << "\n";
38  std::cout << "readtext = " << opt.Contains("readtext") << "\n";
39  std::cout << "readdb = " << opt.Contains("readdb") << "\n";
40  std::cout << "writedb = " << opt.Contains("writedb") << "\n";
41  std::cout << "writetext = " << opt.Contains("writetext") << "\n";
42 
43  gROOT->Macro("LoadLogger.C");
44  gSystem->Load("St_base.so");
45  gSystem->Load("libStDb_Tables.so");
46  gSystem->Load("StDbLib.so");
47 
48  // structure to fill up
49  fmsGainB_st gain;
50  memset(&gain,0,sizeof(gain));
51 
52  if(opt.Contains("readtext")){
53  FILE* fp;
54  int rew,rnstb,rch;
55  float rgain;
56  int nread = 0;
57  cout << "Reading "<<dataspec<<"\n";
58  if(fp=fopen(dataspec,"r")){
59  while(fscanf(fp,"%d %d %d %f",&rew,&rnstb,&rch,&rgain) != EOF){
60  int detid=getDetectorId(rew,rnstb);
61  gain.detectorId[nread]=detid;
62  gain.ch[nread] =rch;
63  gain.gain[nread] =rgain;
64  //printf("%4d %2d %3d %8.4f\n",nread,detid,rch,rgain);
65  nread++;
66  }
67  }
68  fclose(fp);
69  printf("read %d channels of gain\n",nread);
70  }
71 
72  if(opt.Contains("writedb")) {
73  gSystem->Setenv("DB_ACCESS_MODE","write");
74  //putenv("DB_ACCESS_MODE=write");
75  //char* env = getenv("DB_ACCESS_MODE");
76  //cout << "Setting DB_ACCESS_MODE " << env << endl;
78  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
79  StDbTable* table = node->addDbTable("fmsGainB");
80  mgr->setStoreTime(storeTime.Data());
81  // store data in the table
82  table->SetTable((char*)&gain,1);
83  // set store time
84  // store table in dBase
85  if(isSim) table->setFlavor("sim");
86  mgr->storeDbTable(table);
87  //StDbTable* table = node->findTable("fmsGain");
88  //node->removeTable(table);
89  gSystem->Unsetenv("DB_ACCESS_MODE");
90  //unsetenv("DB_ACCESS_MODE");
91  std::cout << "Done with database upload \n";
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  if(isSim) dbMk->SetFlavor("sim"); // 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_fmsGainB *dbppmap = 0;
122  dbppmap = (St_fmsGainB*) DB->Find("fmsGainB");
123  // fetch data and place it to appropriate structure
124  int ngain = 0;
125  if (dbppmap) {
126  std::cout << "Reading fmsGainB table\n";
127  fmsGainB_st *pptable = dbppmap->GetTable();
128  for(int i=0; i<MAX; i++){
129  if(pptable.ch[i]>0){
130  printf("%3d%8d%8d%8.4f\n",
131  int(pptable.detectorId[i]/8+1),int(pptable.detectorId[i]%8+1),
132  int(pptable.ch[i]),float(pptable.gain[i]) );
133  ngain++;
134  }
135  }
136  memcpy(gain,pptable,sizeof(gain));
137  }else {
138  std::cout << "WARNING: No data in fmsGain table (wrong timestamp?). Nothing to return, then.\n";
139  }
140  }
141 
142  if(opt.Contains("writetext")){
143  int ii = data.Length();
144  data.Insert(ii-4,"_out");
145  char* file=data.Data();
146  FILE* fp;
147  cout << "Writing "<<file<<"\n";
148  int ngain = 0;
149  if(fp=fopen(file,"w")){
150  for(int i=0; i<MAX; i++){
151  if(gain.ch[i]>0){
152  int detid=gain.detectorId[i];
153  int iew=int(gain.detectorId[i]/8+1);
154  int instb=int(gain.detectorId[i]%8+1);
155  fprintf(fp,"%d %d %d %5.3f\n",iew,instb,
156  gain.ch[i],gain.gain[i]);
157  ngain++;
158  }
159  }
160  }
161  fclose(fp);
162  printf("Write %d channels\n",ngain);
163  }
164 }
165 
166 
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