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