StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fpsGainSV_db.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fpsGainSV_db(char* opt="", char* year="", char* filename="fpsgain/pAu200_16125035.txt"){
5  TString option(opt), yr(year),f1(filename);
6  TString storeTime; // storetime is begin time for validity range for WRITING DB
7  time_t starttime=0; //unix time for writing, if reading from file
8  int date=0,time=0; // time for READING DB
9  int flag=0;
10 
11  std::cout << "Opt =" << opt << "\n";
12  std::cout << "write = " << option.Contains("writedb") << "\n";
13 
14  gROOT->Macro("loadlib.C");
15 
16  const Int_t MAX_DB_INDEX = 252;
17  fpsGain_st table[MAX_DB_INDEX];
18  int idList[MAX_DB_INDEX];
19  memset(table,0,sizeof(table));
20  memset(idList,0,sizeof(idList));
21 
22  int idx=f1.Index("_");
23  TString f2=f1(idx+1,8);
24  int run=f2.Atoi();
25  printf("%s idx=%d f2=%s run=%d\n",f1.Data(),idx,f2.Data(),run);
26 
27  char *onlserver="onldb2",*bakserver="dbbak",*server=0;
28  int port;
29  int y=run/1000000 -1;
30  if(y == 17){
31  server=onlserver;
32  port=3501;
33  }else{
34  server=bakserver;
35  port=3400+y-1;
36  }
37  char cmd[300]=Form("mysql -h %s.starp.bnl.gov --port=%d -N -s -e \"SELECT startRunTime FROM RunLog.runDescriptor WHERE runNumber=%d LIMIT 1\"",
38  server,port,run);
39  TString st=gSystem->GetFromPipe(cmd);
40  starttime=st.Atoi();
41 
42  date=gSystem->GetFromPipe(Form("date -u -d \@%d +%%Y%%m%%d",starttime)).Atoi();
43  time=gSystem->GetFromPipe(Form("date -u -d \@%d +%%H%%M%%S",starttime)).Atoi();
44  cout << cmd << endl;
45  cout << "start time="<<starttime<<" date="<<date<<" time="<<time<< endl;
46  if(yr.Contains("15ofl")){
47  storeTime="2014-12-20 00:00:01";
48  date = 20141225; time = 0;
49  flag=1;
50  }else if(yr.Contains("15sim")){
51  storeTime="2014-12-10 00:00:02";
52  date = 20141215; time = 0;
53  flag=1;
54  }
55  if(flag==0){
56  std::cout << "unix storetime =" << starttime-10 << endl;
57  std::cout << "read date,time =" << date <<" "<< time << "\n";
58  }else{
59  std::cout << "storetime =" << storeTime << "\n";
60  std::cout << "read date,time =" << date <<" "<< time << "\n";
61  }
62 
63  FILE* fp;
64  int id;
65  float mip,sigma,valley;
66  int n = 0;
67  cout << "Reading "<<filename<<" run#="<<run<<endl;
68  if(fp=fopen(filename,"r")){
69  while(fscanf(fp,"%d %f %f %f",&id,&mip,&sigma,&valley) != EOF){
70  table[n].slatid=id;
71  table[n].MIP=mip;
72  table[n].Sigma=sigma;
73  table[n].Valley=valley;
74  idList[n]=id+1;
75  printf("n=%3d Id=%5d MIP=%6.2f Sigma=%6.2f Valley=%6.2f idList=%3d\n",
76  n,table[n].slatid,table[n].MIP,table[n].Sigma,table[n].Valley,idList[n]);
77  n++;
78  }
79  fclose(fp);
80  }
81  printf("read %d channels from %s\n",n,filename);
82 
83  if(option.Contains("writedb")) {
84  gSystem->Setenv("DB_ACCESS_MODE","write");
86  StDbConfigNode* node = mgr->initConfig("Calibrations_fps");
87  StDbTable* dbtable = node->addDbTable("fpsGain");
88  if(flag==0){
89  mgr->setStoreTime(starttime-10);
90  }else{
91  mgr->setStoreTime(storeTime.Data());
92  }
93  dbtable->SetTable((char*)&table, n, idList);
94  if(yr.Contains("sim")) dbtable->setFlavor("sim");
95  mgr->storeDbTable(dbtable);
96  std::cout << "INFO: table saved to database" << std::endl;
97  }
98 
99  std::cout << "INFO: Reading database" << std::endl;
100  gSystem->Unsetenv("DB_ACCESS_MODE");
101  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
102  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
103  dbMk->SetDebug();
104  dbMk->SetDateTime(date,time); // event or run start time, set to your liking
105  if(yr.Contains("ofl")) {dbMk->SetFlavor("ofl");}
106  else if(yr.Contains("sim")) {dbMk->SetFlavor("sim");}
107  dbMk->Init();
108  dbMk->Make();
109  TDataSet *DB = 0;
110  DB = dbMk->GetDataBase("Calibrations/fps/fpsGain");
111  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
112  St_fpsGain *dataset = 0;
113  dataset = (St_fpsGain*) DB->Find("fpsGain");
114  if (!dataset) {
115  td::cout << "ERROR: dataset does not contain requested table" << std::endl;
116  return;
117  }
118  Int_t rows = dataset->GetNRows();
119  if (rows > 1) {
120  std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
121  }
122  TDatime val[2];
123  dbMk->GetValidity((TTable*)dataset,val);
124  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
125  << val[1].GetDate() << "." << val[1].GetTime() << " ] "
126  << std::endl;
127  fpsGain_st *tbl = dataset->GetTable();
128  for (Int_t i = 0; i < rows; i++) {
129  std::cout << Form("Row=%4d slatid=%3d MIP=%8.4f Sig=%8.4f Val=%8.4f\n",i,
130  tbl[i].slatid,tbl[i].MIP,tbl[i].Sigma,tbl[i].Valley);
131  }
132 }
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
Definition: TTable.h:48
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362