StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fmsBitShiftGain_db.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fmsBitShiftGain_db(char* opt="", char* year="15ofl", char* filename="bitshift/fmsBitShiftGain_all0.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 = 1732;
17  fmsBitShiftGain_st table[MAX_DB_INDEX];
18  memset(table,0,sizeof(table));
19 
20  int idx=f1.Index("run");
21  TString f2=f1(idx+3,8);
22  int run=f2.Atoi();
23  printf("%s idx=%d f2=%s run=%d\n",f1.Data(),idx,f2.Data(),run);
24 
25  if(run>0){
26  char *onlserver="onldb2",*bakserver="dbbak",*server=0;
27  int port;
28  int y=run/1000000 -1;
29  if(y == 17){
30  server=onlserver;
31  port=3501;
32  }else{
33  server=bakserver;
34  port=3400+y-1;
35  }
36  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\"",
37  server,port,run);
38  TString st=gSystem->GetFromPipe(cmd);
39  starttime=st.Atoi();
40 
41  date=gSystem->GetFromPipe(Form("date -u -d \@%d +%%Y%%m%%d",starttime)).Atoi();
42  time=gSystem->GetFromPipe(Form("date -u -d \@%d +%%H%%M%%S",starttime)).Atoi();
43  cout << cmd << endl;
44  cout << "start time="<<starttime<<" date="<<date<<" time="<<time<< endl;
45  }
46  if(yr.Contains("15ofl")){
47  storeTime="2014-12-20 00:00:00";
48  date = 20141225; time = 0;
49  flag=1;
50  }else if(yr.Contains("15sim")){
51  storeTime="2014-12-10 00:00:00";
52  date = 20141215; time = 0;
53  flag=1;
54  }else if(yr.Contains("16ofl")){
55  storeTime="2015-12-20 00:00:00";
56  date = 20151225; time = 0;
57  flag=1;
58  }else if(yr.Contains("16sim")){
59  storeTime="2015-12-10 00:00:00";
60  date = 20151215; time = 0;
61  flag=1;
62  }else if(yr.Contains("17ofl")){
63  storeTime="2016-12-20 00:00:00";
64  date = 20161225; time = 0;
65  flag=1;
66  }else if(yr.Contains("17sim")){
67  storeTime="2016-12-10 00:00:00";
68  date = 20161215; time = 0;
69  flag=1;
70  }
71  if(flag==0){
72  std::cout << "unix storetime =" << starttime-10 << endl;
73  std::cout << "read date,time =" << date <<" "<< time << "\n";
74  }else{
75  std::cout << "storetime =" << storeTime << "\n";
76  std::cout << "read date,time =" << date <<" "<< time << "\n";
77  }
78 
79  FILE* fp;
80  int d,c,s;
81  int n = 0;
82  cout << "Reading "<<filename<<" run#="<<run<<endl;
83  if(fp=fopen(filename,"r")){
84  while(fscanf(fp,"%d %d %d",&d,&c,&s) != EOF){
85  table[n].detectorId=d;
86  table[n].ch=c;
87  table[n].bitshift=s;
88  printf("n=%3d d=%3d c=%3d bitshift=%3d\n",
89  n,table[n].detectorId,table[n].ch,table[n].bitshift);
90  n++;
91  }
92  fclose(fp);
93  }
94  printf("read %d channels from %s\n",n,filename);
95 
96  if(option.Contains("writedb")) {
97  gSystem->Setenv("DB_ACCESS_MODE","write");
99  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
100  StDbTable* dbtable = node->addDbTable("fmsBitShiftGain");
101  if(flag==0){
102  mgr->setStoreTime(starttime-10);
103  }else{
104  mgr->setStoreTime(storeTime.Data());
105  }
106  dbtable->SetTable((char*)&table, n);
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/fms/fmsBitShiftGain");
124  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
125  St_fmsBitShiftGain *dataset = 0;
126  dataset = (St_fmsBitShiftGain*) DB->Find("fmsBitShiftGain");
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  fmsBitShiftGain_st *tbl = dataset->GetTable();
141  for (Int_t i = 0; i < rows; i++) {
142  std::cout << Form("Row=%4d d=%3d c=%3d bitshift=%3d\n",i,
143  tbl[i].detectorId,tbl[i].ch,tbl[i].bitshift);
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