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