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