StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fpsPosition_db.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 void fpsPosition_db(char* opt="", char* year="15ofl", char* input="fpsgeom_run15_20170925.txt") {
5  TString option(opt), yr(year);
6  TString storeTime; // storetime is begin time for validity range for WRITING DB
7  int date,time; // time for READING DB
8  std::cout << "year = " << year << "\n";
9  if(yr.Contains("15ofl")){
10  storeTime="2014-12-20 00:00:05";
11  date = 20141225; time = 1;
12  }else if(yr.Contains("15sim")){
13  storeTime="2014-12-10 00:00:00";
14  date = 20141215; time = 0;
15  }else if(yr.Contains("17ofl")){
16  storeTime="2016-12-20 00:00:02";
17  date = 20161225; time = 0;
18  }else if(yr.Contains("17sim")){
19  storeTime="2016-12-10 00:00:01";
20  date = 20161210; time = 1;
21  }else{
22  std::cout << "Please specify valid year tag\n"; exit;
23  }
24  std::cout << "Opt =" << opt << "\n";
25  std::cout << "write = " << option.Contains("writedb") << "\n";
26  std::cout << "storetime =" << storeTime << "\n";
27  std::cout << "date,time =" << date <<" "<< time << "\n";
28 
29  gROOT->Macro("./loadlib.C");
30 
31  const Int_t MAX_DB_INDEX = 252;
32  fpsPosition_st in[MAX_DB_INDEX];
33 
34  FILE *FP = fopen(input,"r");
35  if(!FP) { printf("Could not open %s\n",input); exit;}
36  printf("Reading %s\n",input);
37  char line[1000];
38  int q,l,s,n=0;
39  while(fgets(line,1000,FP)!=NULL){
40  sscanf(line,"%d %d %d %d %f %f %f %f %f %f",
41  &in[n].slatid,&q,&l,&s,
42  &in[n].xwidth, &in[n].ywidth, &in[n].zwidth,
43  &in[n].xoffset, &in[n].yoffset, &in[n].zoffset);
44  printf("Id=%3d Q=%3d L=%3d S=%3d D=%9.4f %9.4f %9.4f X=%9.4f %9.4f %9.4f\n",
45  in[n].slatid,q,l,s,
46  in[n].xwidth, in[n].ywidth, in[n].zwidth,
47  in[n].xoffset, in[n].yoffset, in[n].zoffset);
48  n++;
49  }
50  printf("Found %d entries\n",n);
51 
52  if(option.Contains("writedb")) {
53  gSystem->Setenv("DB_ACCESS_MODE","write");
55  StDbConfigNode* node = mgr->initConfig("Geometry_fps");
56  StDbTable* dbtable = node->addDbTable("fpsPosition");
57  mgr->setStoreTime(storeTime.Data());
58  dbtable->SetTable((char*)&in, MAX_DB_INDEX);
59  if(yr.Contains("sim")) dbtable->setFlavor("sim");
60  mgr->storeDbTable(dbtable);
61  std::cout << "INFO: table saved to database" << std::endl;
62  }
63 
64  std::cout << "INFO: Reading database" << std::endl;
65  gSystem->Unsetenv("DB_ACCESS_MODE");
66  //gSystem->Unsetenv("DB_SERVER_LOCAL_CONFIG");
67  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
68  dbMk->SetDebug();
69  dbMk->SetDateTime(date,time); // event or run start time, set to your liking
70  if(yr.Contains("ofl")) {dbMk->SetFlavor("ofl");}
71  else if(yr.Contains("sim")) {dbMk->SetFlavor("sim");}
72  dbMk->Init();
73  dbMk->Make();
74  TDataSet *DB = 0;
75  DB = dbMk->GetDataBase("Geometry/fps/fpsPosition");
76  if (!DB) std::cout << "ERROR: no table found in db, or malformed local db config" << std::endl;
77  St_fpsPosition *dataset = 0;
78  dataset = (St_fpsPosition*) DB->Find("fpsPosition");
79  if (!dataset) {
80  td::cout << "ERROR: dataset does not contain requested table" << std::endl;
81  return;
82  }
83  Int_t rows = dataset->GetNRows();
84  if (rows > 1) {
85  std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
86  }
87  TDatime val[2];
88  dbMk->GetValidity((TTable*)dataset,val);
89  std::cout << "Dataset validity range: [ " << val[0].GetDate() << "." << val[0].GetTime() << " - "
90  << val[1].GetDate() << "." << val[1].GetTime() << " ] "
91  << std::endl;
92  fpsPosition_st *table = dataset->GetTable();
93  for (Int_t i = 0; i < rows; i++) {
94  std::cout << Form("Row=%4d slatid=%3d x=%9.4f %9.4f %9.4f d=%9.4f %9.4f %9.4f\n",i,
95  table[i].slatid,
96  table[i].xwidth,table[i].ywidth,table[i].zwidth,
97  table[i].xoffset,table[i].yoffset,table[i].zoffset);
98  }
99 }
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