StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fpsStatus_db.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fpsStatus_db(char* opt="", char* year="15ofl", int badch=0, int readdate=0, int readtime=0) {
5  TString option(opt), yr(year);
6  TString storeTime; // storetime is begin time for validity range for WRITING DB
7  int date,time; // time for READING DB
8  std::cout << "year = " << year << "\n";
9  if(yr.Contains("15ofl")){
10  storeTime="2014-12-20 00:00:00";
11  date = 20141220; time = 1;
12  }else if(yr.Contains("15sim")){
13  storeTime="2014-12-10 00:00:00";
14  date = 20141210; time = 1;
15  }else{
16  std::cout << "Please specify valid year tag\n"; exit;
17  }
18  if(readdate>0) date=readdate;
19  if(readtime>0) time=readtime;
20  std::cout << "Opt =" << opt << "\n";
21  std::cout << "write = " << option.Contains("writedb") << "\n";
22  std::cout << "storetime =" << storeTime << "\n";
23  std::cout << "read date,time =" << date <<" "<< time << "\n";
24 
25  TString storeTime1, storeTime2;
26  int badq,badl,bads,status1,status2;
27  if(badch==1){
28  badq=3; badl=3; bads=16;
29  storeTime1="2015-02-20 00:00:00"; status1=1;
30  storeTime2="2015-07-04 00:00:00"; status2=0;
31  }else if(badch==2){
32  badq=1; badl=1; bads=1;
33  storeTime1="2015-04-15 12:00:00"; status1=1;
34  storeTime2="2015-04-27 12:00:00"; status2=0;
35  }else if(badch==3){
36  badq=4; badl=1; bads=14;
37  storeTime1="2015-04-15 12:00:01"; status1=1;
38  storeTime2="2015-04-22 12:00:00"; status2=0;
39  }
40 
41  gROOT->Macro("loadlib.C");
42 
43  const Int_t MAX_DB_INDEX = 252;
44  fpsStatus_st table[MAX_DB_INDEX];
45 
46  int id=0;
47  if(option.Contains("writedb")) {
48  gSystem->Setenv("DB_ACCESS_MODE","write");
50  StDbConfigNode* node = mgr->initConfig("Calibrations_fps");
51  StDbTable* dbtable = node->addDbTable("fpsStatus");
52  if(yr.Contains("sim")) dbtable->setFlavor("sim");
53 
54  if(badch==0){
55  mgr->setStoreTime(storeTime.Data());
56  id=0;
57  for (Int_t q = 1; q <= 4; q++) {
58  for (Int_t l = 1; l <= 3; l++) {
59  for (Int_t s = 1; s <= 21; s++) {
60  table[id].slatid = id;
61  int flag=0;
62  if(q==2 || q==4){
63  if(s>=20) flag=1;
64  }
65  if(flag==0){table[id].slatid = id; table[id].status=0;}
66  else {table[id].slatid = id; table[id].status=9;}
67  id++;
68  }
69  }
70  }
71  dbtable->SetTable((char*)&table, MAX_DB_INDEX);
72  mgr->storeDbTable(dbtable);
73  std::cout << "INFO: table saved to database" << std::endl;
74  }else{
75  int idList[1];
76  id=(badq-1)*3*21+(badl-1)*21+(bads-1);
77  idList[0]=id+1;
78  table[0].slatid = id;
79 
80  table[0].status= status1;
81  mgr->setStoreTime(storeTime1.Data());
82  dbtable->SetTable((char*)&table,1,idList);
83  mgr->storeDbTable(dbtable);
84  printf("Q%1dL%1dS%02d SlatId=%3d Row=%3d Stat=%d saved on %s\n",
85  badq,badl,bads,id,idList[0],table[0].status,storeTime1.Data());
86 
87  table[0].status= status2;
88  mgr->setStoreTime(storeTime2.Data());
89  dbtable->SetTable((char*)&table,1,idList);
90  mgr->storeDbTable(dbtable);
91  printf("Q%1dL%1dS%02d SlatId=%3d Row=%3d Stat=%d saved on %s\n",
92  badq,badl,bads,id,idList[0],table[0].status,storeTime2.Data());
93  }
94  }
95 
96  std::cout << "INFO: Reading database" << std::endl;
97  gSystem->Unsetenv("DB_ACCESS_MODE");
98  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
99  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
100  dbMk->SetDebug();
101  dbMk->SetDateTime(date,time); // event or run start time, set to your liking
102  if(yr.Contains("ofl")) {dbMk->SetFlavor("ofl");}
103  else if(yr.Contains("sim")) {dbMk->SetFlavor("sim");}
104  dbMk->Init();
105  dbMk->Make();
106  TDataSet *DB = 0;
107  DB = dbMk->GetDataBase("Calibrations/fps/fpsStatus");
108  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
109  St_fpsStatus *dataset = 0;
110  dataset = (St_fpsStatus*) DB->Find("fpsStatus");
111  if (!dataset) {
112  td::cout << "ERROR: dataset does not contain requested table" << std::endl;
113  return;
114  }
115  Int_t rows = dataset->GetNRows();
116  if (rows > 1) {
117  std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
118  }
119  TDatime val[2];
120  dbMk->GetValidity((TTable*)dataset,val);
121  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
122  << val[1].GetDate() << "." << val[1].GetTime() << " ] "
123  << std::endl;
124  fpsStatus_st *tbl = dataset->GetTable();
125  id=(badq-1)*3*21+(badl-1)*21+(bads-1);
126  for (Int_t i = 0; i < rows; i++) {
127  if(badch>0 && i!=id) continue;
128  std::cout << Form("Row=%4d slatid=%3d status=%d\n",i,
129  tbl[i].slatid,tbl[i].status);
130  }
131 }
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