StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fPostMap_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  short QTaddr; /* 0-7, QT address */
15  short QTch; /* 0-31, QT channel */
16 } fpostMap_st;
17 #endif
18 
19 void fPostMap_db(
20  const char* opt = "",
21  const char* year = "17_2_ofl",
22  const char* input = "fPostMap2.txt")
23 {
24  // storeTime is beginning time for validity range in case of WRITING DB
25  TString option(opt), yr(year), storeTime;
26  int date, time; // time for READING DB
27 
28  std::cout <<"year = " <<year <<std::endl;
29  if (yr.Contains("17sim"))
30  {
31  storeTime = "2016-12-10 00:00:01";
32  date = 20161210;
33  time = 1;
34  }
35  else if (yr.Contains("17ofl"))
36  {
37  storeTime = "2016-12-20 00:00:01";
38  date = 20161220;
39  time = 1;
40  }
41  else if (yr.Contains("17_2_ofl"))
42  {
43  storeTime = "2017-05-03 15:40:00";
44  date = 20170504;
45  time = 1;
46  }
47  else { std::cout << "Please specify valid year tag\n"; exit; }
48  std::cout << "Opt =" << opt << "\n";
49  std::cout << "write = " << option.Contains("writedb") << "\n";
50  std::cout << "storetime = " << storeTime << "\n";
51  std::cout << "date, time = " << date <<" "<< time << "\n";
52 
53  gROOT->Macro("./loadlib.C");
54 
55  //-------------------------------------------
56 
57  const Int_t MAX_DB_INDEX = 241;
58  fpostMap_st in[MAX_DB_INDEX];
59 
60  FILE *FP = fopen(input, "r");
61  if (!FP) { printf("Could not open %s\n", input); exit; }
62  printf("\nReading %s\n", input);
63 
64  char line[1000];
65  int n = 0;
66  while (fgets(line, 1000, FP) != NULL)
67  {
68  sscanf(line,"%d %d %d ",
69  &in[n].slatid, &in[n].QTaddr, &in[n].QTch);
70  printf("slatId=%3d QTaddr=%d QTch=%2d\n",
71  in[n].slatid, in[n].QTaddr, in[n].QTch);
72  n++;
73  }
74  printf("Found %d entries\n", n);
75 
76  //-------------------------------------------
77 
78  #if 1
79  if (option.Contains("writedb"))
80  {
81  gSystem->Setenv("DB_ACCESS_MODE", "write");
83  StDbConfigNode* node = mgr->initConfig("Geometry_fps");
84  StDbTable* dbtable = node->addDbTable("fpostMap");
85  mgr->setStoreTime(storeTime.Data());
86  dbtable->SetTable((char*)&in, MAX_DB_INDEX);
87  if (yr.Contains("sim")) dbtable->setFlavor("sim");
88  mgr->storeDbTable(dbtable);
89  std::cout << "INFO: table saved to database" << std::endl;
90  }
91 
92  //-------------------------------------------
93 
94  if (option.Contains("readdb"))
95  {
96  std::cout << "INFO: Reading database" << std::endl;
97  gSystem->Unsetenv("DB_ACCESS_MODE");
98  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
99 
100  St_db_Maker* dbMk = new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
101  dbMk->SetDebug();
102  dbMk->SetDateTime(date, time); // event or run start time, set to your liking
103  if (yr.Contains("ofl")) { dbMk->SetFlavor("ofl"); }
104  else if (yr.Contains("sim")) { dbMk->SetFlavor("sim"); }
105  dbMk->Init();
106  dbMk->Make();
107 
108  TDataSet *DB = 0;
109  DB = dbMk->GetDataBase("Geometry/fps/fpostMap");
110  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
111 
112  St_fpostMap *dataset = 0;
113  dataset = (St_fpostMap*) DB->Find("fpostMap");
114  if (!dataset) { std::cout << "ERROR: dataset does not contain requested table" << std::endl; return; }
115  Int_t rows = dataset->GetNRows();
116  if (rows > 1) std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
117 
118  TDatime val[2];
119  dbMk->GetValidity((TTable*)dataset, val);
120  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
121  << val[1].GetDate() << "." << val[1].GetTime() << " ] " << std::endl;
122 
123  fpostMap_st *table = (fpostMap_st*)dataset->GetTable();
124  for (Int_t i = 0; i < rows; i++)
125  {
126  std::cout << Form("Row=%3d slatid=%3d QTaddr=%d QTch=%2d\n",
127  i, table[i].slatid, table[i].QTaddr, table[i].QTch);
128  }
129  }
130  #endif
131 
132  return;
133 }//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