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