StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fPostStatus_db.C
1 #include "TROOT.h"
2 #include "TDataSet.h"
3 #include "TDatime.h"
4 #include "TString.h"
5 #include "TSystem.h"
6 
7 #include <iostream>
8 #include <fstream>
9 
10 #if 0
11 typedef struct
12 {
13  unsigned short slatid; /* 0-240: slat Id */
14  unsigned short status; /* 0=Good, 1=bad, 9=Unused */
15 } fpostStatus_st;
16 #endif
17 
18 void fPostStatus_db(
19  const char* opt = "",
20  const char* year = "17sim",
21  const char* input = "fPostStatus.txt",
22  int readdate = 0,
23  int readtime = 0)
24 {
25  // storeTime is beginning time for validity range in case of WRITING DB
26  TString option(opt), yr(year), storeTime;
27  int date, time; // time for READING DB
28 
29  std::cout <<"year = " <<year <<std::endl;
30  if (yr.Contains("17sim"))
31  {
32  storeTime = "2016-12-10 00:00:00";
33  date = 20161210;
34  time = 0;
35  }
36  else if (yr.Contains("17ofl"))
37  {
38  storeTime = "2016-12-20 00:00:00";
39  date = 20161220;
40  time = 0;
41  }
42  else { std::cout << "Please specify valid year tag\n"; exit; }
43 
44  if (readdate > 0) date = readdate;
45  if (readtime > 0) time = readtime;
46 
47  std::cout << "Opt =" << opt << "\n";
48  std::cout << "write = " << option.Contains("writedb") << "\n";
49  std::cout << "storetime = " << storeTime << "\n";
50  std::cout << "date, time = " << date <<" "<< time << "\n";
51 
52  gROOT->Macro("./loadlib.C");
53 
54  //-------------------------------------------
55 
56  const Int_t MAX_DB_INDEX = 241;
57  fpostStatus_st in[MAX_DB_INDEX];
58 
59  FILE *FP = fopen(input, "r");
60  if (!FP) { printf("Could not open %s\n", input); exit; }
61  printf("\nReading %s\n", input);
62 
63  char line[1000];
64  int n = 0;
65  while (fgets(line, 1000, FP) != NULL)
66  {
67  sscanf(line,"%hu %hu", &in[n].slatid, &in[n].status);
68  printf("slatId=%3d status=%1d\n", in[n].slatid, in[n].status);
69  n++;
70  }
71  printf("Found %d entries\n", n);
72  fclose(FP);
73  printf("File Closed\n", n);
74 
75  //-------------------------------------------
76 
77  #if 1
78  if (option.Contains("writedb"))
79  {
80  gSystem->Setenv("DB_ACCESS_MODE", "write");
82  StDbConfigNode* node = mgr->initConfig("Calibrations_fps");
83  StDbTable* dbtable = node->addDbTable("fpostStatus");
84  mgr->setStoreTime(storeTime.Data());
85  dbtable->SetTable((char*)&in, MAX_DB_INDEX);
86  if (yr.Contains("sim")) dbtable->setFlavor("sim");
87  mgr->storeDbTable(dbtable);
88  std::cout << "INFO: table saved to database" << std::endl;
89  }
90 
91  //-------------------------------------------
92 
93  if (option.Contains("readdb"))
94  {
95  std::cout << "INFO: Reading database" << std::endl;
96  gSystem->Unsetenv("DB_ACCESS_MODE");
97  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
98 
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 
107  TDataSet *DB = 0;
108  DB = dbMk->GetDataBase("Calibrations/fps/fpostStatus");
109  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
110 
111  St_fpostStatus *dataset = 0;
112  dataset = (St_fpostStatus*) DB->Find("fpostStatus");
113  if (!dataset) { std::cout << "ERROR: dataset does not contain requested table" << std::endl; return; }
114  Int_t rows = dataset->GetNRows();
115  if (rows > 1) std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
116 
117  TDatime val[2];
118  dbMk->GetValidity((TTable*)dataset, val);
119  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
120  << val[1].GetDate() << "." << val[1].GetTime() << " ] " << std::endl;
121 
122  fpostStatus_st *table = (fpostStatus_st*)dataset->GetTable();
123  for (Int_t i = 0; i < rows; i++)
124  {
125  std::cout << Form("Row=%3d slatid=%3d status=%1d\n", i, table[i].slatid, table[i].status);
126  }
127  }
128  #endif
129 
130  return;
131 }//Main
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