StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fmsBitShiftGainB_db.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fmsBitShiftGainB_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 = 2500;
17  fmsBitShiftGainB_st table;
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="heston",*bakserver="db04",*server=0;
27  int port;
28  int y=run/1000000 -1;
29  if(y == 18){
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.star.bnl.gov --port=%d -N -s -e \"SELECT startRunTime FROM RunLog.runDescriptor WHERE runNumber=%d LIMIT 1\"",
37  server,port,run);
38  printf("%s\n",cmd);
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  }
47  if(yr.Contains("11ofl")){
48  storeTime="2010-12-20 00:00:00";
49  date = 20101225; time = 0;
50  flag=1;
51  }else if(yr.Contains("11sim")){
52  storeTime="2010-12-10 00:00:00";
53  date = 20101215; time = 0;
54  flag=1;
55  }else if(yr.Contains("15ofl")){
56  storeTime="2014-12-20 00:00:00";
57  date = 20141225; time = 0;
58  flag=1;
59  }else if(yr.Contains("15sim")){
60  storeTime="2014-12-10 00:00:00";
61  date = 20141215; time = 0;
62  flag=1;
63  }else if(yr.Contains("16ofl")){
64  storeTime="2015-12-20 00:00:00";
65  date = 20151225; time = 0;
66  flag=1;
67  }else if(yr.Contains("16sim")){
68  storeTime="2015-12-10 00:00:00";
69  date = 20151215; time = 0;
70  flag=1;
71  }else if(yr.Contains("17ofl")){
72  storeTime="2016-12-20 00:00:00";
73  date = 20161225; time = 0;
74  flag=1;
75  }else if(yr.Contains("17sim")){
76  storeTime="2016-12-10 00:00:00";
77  date = 20161215; time = 0;
78  flag=1;
79  }
80  if(flag==0){
81  std::cout << "unix storetime =" << starttime-10 << endl;
82  std::cout << "read date,time =" << date <<" "<< time << "\n";
83  }else{
84  std::cout << "storetime =" << storeTime << "\n";
85  std::cout << "read date,time =" << date <<" "<< time << "\n";
86  }
87 
88  FILE* fp;
89  int d,c,s;
90  int n = 0;
91  cout << "Reading "<<filename<<" run#="<<run<<endl;
92  if(fp=fopen(filename,"r")){
93  while(fscanf(fp,"%d %d %d",&d,&c,&s) != EOF){
94  table.detectorId[n]=d;
95  table.ch[n]=c;
96  table.bitshift[n]=s;
97  printf("n=%3d d=%3d c=%3d bitshift=%3d\n",
98  n,table.detectorId[n],table.ch[n],table.bitshift[n]);
99  n++;
100  }
101  fclose(fp);
102  }
103  printf("read %d channels from %s\n",n,filename);
104 
105  if(option.Contains("writedb")) {
106  gSystem->Setenv("DB_ACCESS_MODE","write");
108  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
109  StDbTable* dbtable = node->addDbTable("fmsBitShiftGainB");
110  if(flag==0){
111  mgr->setStoreTime(starttime-10);
112  }else{
113  mgr->setStoreTime(storeTime.Data());
114  }
115  dbtable->SetTable((char*)&table, 1);
116  if(yr.Contains("sim")) dbtable->setFlavor("sim");
117  mgr->storeDbTable(dbtable);
118  std::cout << "INFO: table saved to database" << std::endl;
119  }
120 
121  std::cout << "INFO: Reading database" << std::endl;
122  gSystem->Unsetenv("DB_ACCESS_MODE");
123  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
124  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
125  dbMk->SetDebug();
126  dbMk->SetDateTime(date,time); // event or run start time, set to your liking
127  if(yr.Contains("ofl")) {dbMk->SetFlavor("ofl");}
128  else if(yr.Contains("sim")) {dbMk->SetFlavor("sim");}
129  dbMk->Init();
130  dbMk->Make();
131  TDataSet *DB = 0;
132  DB = dbMk->GetDataBase("Calibrations/fms/fmsBitShiftGainB");
133  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
134  St_fmsBitShiftGainB *dataset = 0;
135  dataset = (St_fmsBitShiftGainB*) DB->Find("fmsBitShiftGainB");
136  if (!dataset) {
137  td::cout << "ERROR: dataset does not contain requested table" << std::endl;
138  return;
139  }
140  Int_t rows = dataset->GetNRows();
141  if (rows > 0) {
142  std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
143  }
144  TDatime val[2];
145  dbMk->GetValidity((TTable*)dataset,val);
146  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
147  << val[1].GetDate() << "." << val[1].GetTime() << " ] "
148  << std::endl;
149  fmsBitShiftGainB_st *tbl = dataset->GetTable();
150  for (Int_t i = 0; i < MAX_DB_INDEX; i++) {
151  if(tbl.ch[i]>0){
152  std::cout << Form("%2d %3d %2d\n",
153  tbl.detectorId[i],tbl.ch[i],tbl.bitshift[i]);
154  }
155  }
156 }
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