StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fPostPosition_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  float length; /* length ( Depends on S1,S2,etc. cm) */
15  float width; /* width (5 cm) */
16  float thickness; /* thickness (1 cm) */
17  float angle_xy; /* ? angle in the xy plan measured wrt the positive x-axis (45 South, 135 North)*/
18  float xoffset; /* xoffset from beam line to center of detector (cm) */
19  float yoffset; /* yoffset from beam line to center of detector (cm) */
20  float zoffset; /* z position from IR (cm) */
21 } fpostPosition_st;
22 #endif
23 
24 void fPostPosition_db(
25  const char* opt = "",
26  const char* year = "17sim",
27  const char* input = "fPostPosition.txt")
28 {
29  // storeTime is beginning time for validity range in case of WRITING DB
30  TString option(opt), yr(year), storeTime;
31  int date, time; // time for READING DB
32 
33  std::cout <<"year = " <<year <<std::endl;
34  if (yr.Contains("17sim"))
35  {
36  storeTime = "2016-12-10 00:00:01";
37  date = 20161210;
38  time = 1;
39  }
40  else if (yr.Contains("17ofl"))
41  {
42  storeTime = "2016-12-20 00:00:01";
43  date = 20161220;
44  time = 1;
45  }
46  else { std::cout << "Please specify valid year tag\n"; exit; }
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  fpostPosition_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,"%d %f %f %f %f %f %f %f ",
68  &in[n].slatid,
69  &in[n].length, &in[n].width, &in[n].thickness, &in[n].angle_xy,
70  &in[n].xoffset, &in[n].yoffset, &in[n].zoffset);
71  printf("Id=%3d D=%9.4f %9.4f %9.4f %9.4f X=%9.4f %9.4f %9.4f\n",
72  in[n].slatid,
73  in[n].length, in[n].width, in[n].thickness, in[n].angle_xy,
74  in[n].xoffset, in[n].yoffset, in[n].zoffset);
75  n++;
76  }
77  printf("Found %d entries\n", n);
78 
79  //-------------------------------------------
80 
81  #if 1
82  if (option.Contains("writedb"))
83  {
84  gSystem->Setenv("DB_ACCESS_MODE", "write");
86  StDbConfigNode* node = mgr->initConfig("Geometry_fps");
87  StDbTable* dbtable = node->addDbTable("fpostPosition");
88  mgr->setStoreTime(storeTime.Data());
89  dbtable->SetTable((char*)&in, MAX_DB_INDEX);
90  if (yr.Contains("sim")) dbtable->setFlavor("sim");
91  mgr->storeDbTable(dbtable);
92  std::cout << "INFO: table saved to database" << std::endl;
93  }
94 
95  //-------------------------------------------
96 
97  if (option.Contains("readdb"))
98  {
99  std::cout << "INFO: Reading database" << std::endl;
100  gSystem->Unsetenv("DB_ACCESS_MODE");
101  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
102 
103  St_db_Maker* dbMk = new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
104  dbMk->SetDebug();
105  dbMk->SetDateTime(date, time); // event or run start time, set to your liking
106  if (yr.Contains("ofl")) { dbMk->SetFlavor("ofl"); }
107  else if (yr.Contains("sim")) { dbMk->SetFlavor("sim"); }
108  dbMk->Init();
109  dbMk->Make();
110 
111  TDataSet *DB = 0;
112  DB = dbMk->GetDataBase("Geometry/fps/fpostPosition");
113  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
114 
115  St_fpostPosition* dataset = 0;
116  dataset = (St_fpostPosition*) DB->Find("fpostPosition");
117  if (!dataset) { std::cout << "ERROR: dataset does not contain requested table" << std::endl; return; }
118  Int_t rows = dataset->GetNRows();
119  if (rows > 1) std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
120 
121  TDatime val[2];
122  dbMk->GetValidity((TTable*)dataset, val);
123  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
124  << val[1].GetDate() << "." << val[1].GetTime() << " ] " << std::endl;
125 
126  fpostPosition_st *table = dataset->GetTable();
127  for (Int_t i = 0; i < rows; i++)
128  {
129  std::cout << Form("Row=%3d slatid=%3d dim=%9.4f %9.4f %9.4f %9.4f offset=%9.4f %9.4f %9.4f\n",
130  i, table[i].slatid,
131  table[i].length, table[i].width, table[i].thickness, table[i].angle_xy,
132  table[i].xoffset, table[i].yoffset, table[i].zoffset);
133  }
134  }
135  #endif
136 
137  return;
138 }//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