Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnlMonDB.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file OnlMonDB.cc
1 #include "OnlMonDB.h"
2 #include "OnlMonDBVar.h"
3 #include "OnlMonDBodbc.h"
4 
5 #include <onlmon/OnlMonBase.h> // for OnlMonBase
6 #include <onlmon/OnlMonServer.h>
7 
8 #include <ctype.h> // for tolower
9 #include <algorithm>
10 #include <cstdio>
11 #include <iostream>
12 #include <sstream>
13 #include <utility> // for pair
14 
16  : OnlMonBase(thisname)
17 {
18  return;
19 }
20 
22 {
23  delete db;
24  while (varmap.begin() != varmap.end())
25  {
26  delete varmap.begin()->second;
27  varmap.erase(varmap.begin());
28  }
29 }
30 
31 void OnlMonDB::Print() const
32 {
33  std::cout << "OnlMonDB Name: " << ThisName << std::endl;
34  std::map<const std::string, OnlMonDBVar *>::const_iterator iter;
35  for (iter = varmap.begin(); iter != varmap.end(); ++iter)
36  {
37  iter->second->Print();
38  }
39  return;
40 }
41 
43 {
44  std::string cpstring = varname;
45  transform(cpstring.begin(), cpstring.end(), cpstring.begin(), (int (*)(int)) tolower);
46  std::map<const std::string, OnlMonDBVar *>::const_iterator iter = varmap.find(cpstring);
47  if (iter != varmap.end())
48  {
49  std::cout << "Variable " << varname << " allready registered in DB" << std::endl;
50  return -1;
51  }
52  varmap[cpstring] = new OnlMonDBVar();
53  return 0;
54 }
55 
56 int OnlMonDB::SetVar(const std::string &varname, const float var, const float varerr, const float varqual)
57 {
58  float vararray[3];
59  vararray[0] = var;
60  vararray[1] = varerr;
61  vararray[2] = varqual;
62  return SetVar(varname, vararray);
63 }
64 
65 int OnlMonDB::SetVar(const std::string &varname, const float var[3])
66 {
67  std::string cpstring = varname;
68  transform(cpstring.begin(), cpstring.end(), cpstring.begin(), (int (*)(int)) tolower);
69  std::map<const std::string, OnlMonDBVar *>::iterator iter = varmap.find(cpstring);
70  if (iter != varmap.end())
71  {
72  iter->second->SetVar(var);
73  return 0;
74  }
75  std::cout << __PRETTY_FUNCTION__ << " Could not find Variable " << varname << " in DB list" << std::endl;
76  return -1;
77 }
78 
80 {
81  if (!db)
82  {
83  db = new OnlMonDBodbc(ThisName);
84  }
87  return 0;
88 }
89 
91 {
93 
94  int iret = 0;
95  if (!db)
96  {
97  std::cout << "Data Base not initialized, fix your code." << std::endl;
98  std::cout << "You need to call DBInit() after you registered your variables" << std::endl;
99  return -1;
100  }
101  iret = db->AddRow(se->CurrentTicks(), se->RunNumber(), varmap);
102  if (iret)
103  {
104  printf("error in dbcommit, ret code %d\n", iret);
105  return iret;
106  }
107  //db->Dump();
108  std::map<const std::string, OnlMonDBVar *>::iterator iter;
109  for (iter = varmap.begin(); iter != varmap.end(); ++iter)
110  {
111  iter->second->resetupdated();
112  }
113  return iret;
114 }
115 
117 {
118  static int ifirst = 1;
119  int iret = 0;
120  static time_t ticks = 0;
121  static int runnumber = 90000;
122  if (!db)
123  {
124  std::cout << "Data Base not initialized, fix your code." << std::endl;
125  std::cout << "You need to call DBInit() after you registered your variables" << std::endl;
126  return -1;
127  }
128  if (ifirst)
129  {
130  ticks = time(nullptr);
131  ticks -= 2 * 30 * 24 * 60 * 60;
132  ifirst = 0;
133  }
134  else
135  {
136  ticks += 3 * 60;
137  runnumber += 1;
138  }
139  db->AddRow(ticks, runnumber, varmap);
140  if (iret)
141  {
142  std::cout << "error in dbcommit" << std::endl;
143  return iret;
144  }
145  //db->Dump();
146  std::map<const std::string, OnlMonDBVar *>::iterator iter;
147  for (iter = varmap.begin(); iter != varmap.end(); ++iter)
148  {
149  iter->second->resetupdated();
150  }
151  return iret;
152 }
153 
154 int OnlMonDB::GetVar(const time_t begin, const time_t end, const std::string &varname, std::vector<time_t> &timestp, std::vector<int> &runnumber, std::vector<float> &var, std::vector<float> &varerr)
155 {
156  if (!db)
157  {
158  db = new OnlMonDBodbc(ThisName);
159  }
160  int iret = db->GetVar(begin, end, varname, timestp, runnumber, var, varerr);
161  return iret;
162 }
163 
165 {
166  std::map<const std::string, OnlMonDBVar *>::iterator iter;
167  for (iter = varmap.begin(); iter != varmap.end(); ++iter)
168  {
169  iter->second->resetupdated();
170  }
171  return;
172 }