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