CMS 3D CMS Logo

RunDCSHVDat.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <cmath>
4 #include <list>
5 #include <string>
6 #include <map>
7 #include <ctime>
8 
12 
13 using namespace std;
14 using namespace oracle::occi;
15 
17  m_env = nullptr;
18  m_conn = nullptr;
19  m_writeStmt = nullptr;
20  m_readStmt = nullptr;
21 
22  m_hv = 0;
23  m_hvnom = 0;
24  m_status = 0;
25  m_tstatus = 0;
26 }
27 
29 
30 void RunDCSHVDat::prepareWrite() noexcept(false) {}
31 
32 void RunDCSHVDat::writeDB(const EcalLogicID* ecid, const RunDCSHVDat* item, RunIOV* iov) noexcept(false) {}
33 
34 void RunDCSHVDat::fetchData(map<EcalLogicID, RunDCSHVDat>* fillMap, RunIOV* iov) noexcept(false) {
35  fetchLastData(fillMap);
36 }
37 
39  DateHandler dh(m_env, m_conn);
40 
41  ResultSet* rset = nullptr;
42  string query =
43  "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
44  " d.actual_vmon, h.nominal_value , d.change_date "
45  " FROM " +
46  getEBAccount() +
47  ".FWCAENCHANNEL d "
48  " JOIN " +
49  getEBAccount() +
50  ".HV_MAPPING h on h.DPID = d.DPID "
51  " join " +
52  getEBAccount() +
53  ".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
54  " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date ";
55  try {
56  m_readStmt->setSQL(query);
57 
58  m_readStmt->setDate(1, dh.tmToDate(timeStart));
59 
60  rset = m_readStmt->executeQuery();
61  } catch (SQLException& e) {
62 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
63  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): ") + e.getMessage() + " " + query));
64 #else
65  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): error code ") +
66  std::to_string(e.getErrorCode()) + " " + query));
67 #endif
68  }
69  return rset;
70 }
71 
73  DateHandler dh(m_env, m_conn);
74 
75  ResultSet* rset = nullptr;
76  string query =
77  "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
78  " d.actual_vmon, '800' nominal_value , d.change_date "
79  " FROM " +
80  getEEAccount() +
81  ".FWCAENCHANNEL d "
82  " JOIN " +
83  getEEAccount() +
84  ".EE_HVA_MAPPING h on h.DPID = d.DPID "
85  " join " +
86  getEEAccount() +
87  ".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
88  " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date ";
89  try {
90  m_readStmt->setSQL(query);
91 
92  m_readStmt->setDate(1, dh.tmToDate(timeStart));
93 
94  rset = m_readStmt->executeQuery();
95  } catch (SQLException& e) {
96 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
97  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): ") + e.getMessage() + " " + query));
98 #else
99  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): error code ") +
100  std::to_string(e.getErrorCode()) + " " + query));
101 #endif
102  }
103  return rset;
104 }
105 
107  DateHandler dh(m_env, m_conn);
108 
109  ResultSet* rset = nullptr;
110  string query =
111  "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
112  " d.actual_vmon, '600' nominal_value , d.change_date "
113  " FROM " +
114  getEEAccount() +
115  ".FWCAENCHANNEL d "
116  " JOIN " +
117  getEEAccount() +
118  ".EE_HVD_MAPPING h on h.DPID = d.DPID "
119  " join " +
120  getEEAccount() +
121  ".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
122  " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date ";
123  try {
124  m_readStmt->setSQL(query);
125 
126  m_readStmt->setDate(1, dh.tmToDate(timeStart));
127 
128  rset = m_readStmt->executeQuery();
129  } catch (SQLException& e) {
130 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
131  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): ") + e.getMessage() + " " + query));
132 #else
133  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): error code ") +
134  std::to_string(e.getErrorCode()) + " " + query));
135 #endif
136  }
137  return rset;
138 }
139 
140 /*
141 ResultSet *RunDCSHVDat::getBarrelRset() {
142  ResultSet* rset = NULL;
143  string query = "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
144  " d.value, h.nominal_value , d.since "
145  "FROM "+ getEBAccount()+".DCSLASTVALUE_VOLTAGE_VMON d "
146  " JOIN "+ getEBAccount()+".HV_MAPPING h on "
147  " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name";
148  try {
149  m_readStmt->setSQL(query);
150  rset = m_readStmt->executeQuery();
151  }
152  catch (SQLException &e) {
153  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): ") + e.getMessage() + " " + query));
154  }
155  return rset;
156 }
157 */
158 
160  ResultSet* rset = nullptr;
161  string query =
162  "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
163  " d.VALUE_NUMBER, h.nominal_value , d.CHANGE_DATE "
164  "FROM " +
165  getEBAccount() +
166  ".FWCAENCHANNEL_LV d "
167  " JOIN " +
168  getEBAccount() +
169  ".HV_MAPPING h on "
170  " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and "
171  "DPE_NAME='ACTUAL_VMON' ";
172  try {
173  m_readStmt->setSQL(query);
174  rset = m_readStmt->executeQuery();
175  } catch (SQLException& e) {
176 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
177  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): ") + e.getMessage() + " " + query));
178 #else
179  throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset(): error code ") +
180  std::to_string(e.getErrorCode()) + " " + query));
181 #endif
182  }
183  return rset;
184 }
185 
187  ResultSet* rset = nullptr;
188  string query =
189  "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
190  " d.value_number, '800' NOMINAL_VALUE , d.CHANGE_DATE "
191  "FROM " +
192  getEEAccount() +
193  ".FWCAENCHANNEL_LV d "
194  " JOIN " +
195  getEEAccount() +
196  ".EE_HVA_MAPPING h on "
197  " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and "
198  "dpe_name='ACTUAL_VMON' ";
199  try {
200  m_readStmt->setSQL(query);
201  rset = m_readStmt->executeQuery();
202  } catch (SQLException& e) {
203 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
204  throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapAnodeRset(): ") + e.getMessage() + " " + query));
205 #else
206  throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapAnodeRset(): error code ") +
207  std::to_string(e.getErrorCode()) + " " + query));
208 #endif
209  }
210  return rset;
211 }
212 
214  ResultSet* rset = nullptr;
215  string query =
216  "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
217  " d.value_number, '600' NOMINAL_VALUE , d.CHANGE_DATE "
218  "FROM " +
219  getEEAccount() +
220  ".FWCAENCHANNEL_LV d "
221  " JOIN " +
222  getEEAccount() +
223  ".EE_HVD_MAPPING h on "
224  " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and "
225  "dpe_name='ACTUAL_VMON' ";
226  try {
227  m_readStmt->setSQL(query);
228  rset = m_readStmt->executeQuery();
229  } catch (SQLException& e) {
230 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
231  throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapDynodeRset(): ") + e.getMessage() + " " + query));
232 #else
233  throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapDynodeRset(): error code ") +
234  std::to_string(e.getErrorCode()) + " " + query));
235 #endif
236  }
237  return rset;
238 }
239 
240 void RunDCSHVDat::fillTheMap(ResultSet* rset, map<EcalLogicID, RunDCSHVDat>* fillMap) {
241  // method for last value queries
242 
243  std::pair<EcalLogicID, RunDCSHVDat> p;
244  RunDCSHVDat dat;
245  DateHandler dh(m_env, m_conn);
246 
247  try {
248  while (rset->next()) {
249  p.first = EcalLogicID(rset->getString(1), // name
250  rset->getInt(2), // logic_id
251  rset->getInt(3), // id1
252  rset->getInt(4), // id2
253  rset->getInt(5), // id3
254  rset->getString(6)); // maps_to
255 
256  dat.setHV(rset->getFloat(7));
257  dat.setHVNominal(rset->getFloat(8));
258  Date sinceDate = rset->getDate(9);
259  Tm sinceTm = dh.dateToTm(sinceDate);
260  dat.setStatus(0);
261  if (p.first.getName() == "EB_HV_channel") {
262  setStatusForBarrel(dat, sinceTm);
263  } else {
264  setStatusForEndcaps(dat, sinceTm);
265  }
266  p.second = dat;
267  fillMap->insert(p);
268  }
269  } catch (SQLException& e) {
270 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
271  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): ") + e.getMessage()));
272 #else
273  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): error code ") + std::to_string(e.getErrorCode())));
274 #endif
275  }
276 }
277 
279  ResultSet* rset, std::list<DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat> >* my_data_list) noexcept(false) {
280  // std::list< std::pair< Tm, std::map< EcalLogicID, RunDCSHVDat > > >* fillMap) {
281 
282  // method for historic queries
283 
284  RunDCSHVDat dat;
285  DateHandler dh(m_env, m_conn);
286 
287  try {
288  int count = -1;
289  while (rset->next()) {
290  EcalLogicID ec = EcalLogicID(rset->getString(1), // name
291  rset->getInt(2), // logic_id
292  rset->getInt(3), // id1
293  rset->getInt(4), // id2
294  rset->getInt(5), // id3
295  rset->getString(6)); // maps_to
296 
297  dat.setHV(rset->getFloat(7));
298  dat.setHVNominal(rset->getFloat(8));
299  // Date sinceDate = rset->getDate(9);
300  // Tm sinceTm = dh.dateToTm( sinceDate );
301  // Date sinceDate = rset->getDate(9);
302  Timestamp ora_timestamp = rset->getTimestamp(9);
303  Tm sinceTm; // YYYY-MM-DD HH:MM:SS
304 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
305  sinceTm.setToString(ora_timestamp.toText("yyyy-mm-dd hh24:mi:ss", 0));
306 #else
307  int year = 0;
308  unsigned int month = 0;
309  unsigned int day = 0;
310  unsigned int hour = 0;
311  unsigned int minute = 0;
312  unsigned int second = 0;
313  unsigned int fs = 0;
314  ora_timestamp.getDate(year, month, day);
315  ora_timestamp.getTime(hour, minute, second, fs);
316  const std::tm tt = {.tm_sec = static_cast<int>(second),
317  .tm_min = static_cast<int>(minute),
318  .tm_hour = static_cast<int>(hour),
319  .tm_mday = static_cast<int>(day),
320  .tm_mon = static_cast<int>(month),
321  .tm_year = year - 1900,
322  .tm_wday = 0,
323  .tm_yday = 0,
324  .tm_isdst = 0,
325  .tm_gmtoff = 0,
326  .tm_zone = nullptr};
327  char tt_str[30] = {0};
328  if (std::strftime(tt_str, sizeof(tt_str), "%F %T", &tt)) {
329  sinceTm.setToString(std::string(tt_str));
330  } else {
331  throw std::runtime_error("RunDCSHVDat::fillTheMapByTime: failed to generate the date string");
332  }
333 #endif
334 
335  dat.setStatus(0);
336  if (ec.getName() == "EB_HV_channel") {
337  setStatusForBarrel(dat, sinceTm);
338  } else {
339  setStatusForEndcaps(dat, sinceTm);
340  }
341 
342  std::pair<EcalLogicID, RunDCSHVDat> d;
343  d.first = ec;
344  d.second = dat;
345  std::pair<Tm, std::pair<EcalLogicID, RunDCSHVDat> > p;
346  p.first = sinceTm;
347  p.second = d;
348 
350  pp.m_iData = p;
351 
352  my_data_list->push_back(pp);
353  count++;
354  if (count < 100)
355  std::cout << "DCS DB : size:" << my_data_list->size() << " Tm " << sinceTm.str() << " " << ec.getID1() << " "
356  << ec.getID2() << " " << dat.getHV() << std::endl;
357  }
358  std::cout << "DCS DB : size:" << my_data_list->size() << std::endl;
359 
360  } catch (SQLException& e) {
361 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
362  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): ") + e.getMessage()));
363 #else
364  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): error code ") + std::to_string(e.getErrorCode())));
365 #endif
366  }
367 }
368 
370  Tm t_now_gmt;
371 
372  t_now_gmt.setToCurrentGMTime();
373  int t_now_gmt_micros = t_now_gmt.microsTime();
374  return t_now_gmt_micros;
375 }
376 
377 void RunDCSHVDat::setStatusForBarrel(RunDCSHVDat& dat, const Tm& sinceTm) {
378  int t_now_gmt_micros = nowMicroseconds();
379 
380  float hv_diff = dat.getHV() - dat.getHVNominal();
381  if (hv_diff < 0)
382  hv_diff = -hv_diff;
383  if (hv_diff * 1000 > maxHVDifferenceEB) {
384  dat.setStatus(HVNOTNOMINAL);
385  }
386  if (dat.getHV() * 1000 < minHV) {
387  dat.setStatus(HVOFF);
388  }
389 
390  int result = 0;
391  long long d = (t_now_gmt_micros - sinceTm.microsTime());
392  if (d > maxDifference) {
393  result = -d / 1000000;
394  }
395  dat.setTimeStatus(result);
396 }
397 
398 void RunDCSHVDat::setStatusForEndcaps(RunDCSHVDat& dat, const Tm& sinceTm) {
399  int t_now_gmt_micros = nowMicroseconds();
400 
401  if (fabs(dat.getHV() - dat.getHVNominal()) * 1000 > maxHVDifferenceEE) {
402  dat.setStatus(HVNOTNOMINAL);
403  }
404  if (dat.getHV() * 1000 < minHV) {
405  dat.setStatus(HVOFF);
406  }
407 
408  int result = 0;
409  long long d = (t_now_gmt_micros - sinceTm.microsTime());
410  if (d > maxDifference) {
411  result = -d / 1000000;
412  }
413  dat.setTimeStatus(result);
414 }
415 
416 void RunDCSHVDat::fetchLastData(map<EcalLogicID, RunDCSHVDat>* fillMap) noexcept(false) {
417  this->checkConnection();
418 
419  fillMap->clear();
420 
421  try {
422  std::pair<EcalLogicID, RunDCSHVDat> p;
423  RunDCSHVDat dat;
424 
425  ResultSet* rset = getBarrelRset();
426 
427  fillTheMap(rset, fillMap);
428  rset = getEndcapAnodeRset();
429 
430  fillTheMap(rset, fillMap);
431  rset = getEndcapDynodeRset();
432 
433  fillTheMap(rset, fillMap);
434  } catch (SQLException& e) {
435 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
436  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): ") + e.getMessage()));
437 #else
438  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): error code ") + std::to_string(e.getErrorCode())));
439 #endif
440  }
441 }
442 
443 void RunDCSHVDat::fetchHistoricalData(std::list<std::pair<Tm, std::map<EcalLogicID, RunDCSHVDat> > >* fillMap,
444  const Tm& timeStart) noexcept(false) {
445  this->checkConnection();
446 
447  fillMap->clear();
448 
449  std::list<DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat> > my_data_list;
450 
451  try {
452  std::pair<EcalLogicID, RunDCSHVDat> p;
453  RunDCSHVDat dat;
454 
455  ResultSet* rset1 = getBarrelRset(timeStart);
456  fillTheMapByTime(rset1, &my_data_list);
457 
458  ResultSet* rset2 = getEndcapAnodeRset(timeStart);
459  fillTheMapByTime(rset2, &my_data_list);
460 
461  ResultSet* rset3 = getEndcapDynodeRset(timeStart);
462  fillTheMapByTime(rset3, &my_data_list);
463 
465  my_dr.setDataList(my_data_list);
466  my_dr.getReducedDataList(fillMap);
467 
468  } catch (SQLException& e) {
469 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
470  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): ") + e.getMessage()));
471 #else
472  throw(std::runtime_error(std::string("RunDCSHVDat::fetchData(): error code ") + std::to_string(e.getErrorCode())));
473 #endif
474  }
475 }
void setDataList(std::list< MyData< T > > _list)
Definition: DataReducer.h:49
oracle::occi::ResultSet ResultSet
Definition: RunDCSHVDat.h:17
void setStatusForEndcaps(RunDCSHVDat &, const Tm &)
Definition: RunDCSHVDat.cc:398
ResultSet * getEndcapDynodeRset()
Definition: RunDCSHVDat.cc:213
void writeDB(const EcalLogicID *ecid, const RunDCSHVDat *item, RunIOV *iov) noexcept(false)
Definition: RunDCSHVDat.cc:32
int getID1() const
Definition: EcalLogicID.cc:30
std::string getName() const
Definition: EcalLogicID.cc:26
ResultSet * getEndcapAnodeRset()
Definition: RunDCSHVDat.cc:186
int getID2() const
Definition: EcalLogicID.cc:32
std::string to_string(const V &value)
Definition: OMSAccess.h:77
ResultSet * getBarrelRset()
Definition: RunDCSHVDat.cc:159
void fetchHistoricalData(std::list< std::pair< Tm, std::map< EcalLogicID, RunDCSHVDat > > > *fillMap, const Tm &timeStart) noexcept(false)
Definition: RunDCSHVDat.cc:443
void setToCurrentGMTime()
Definition: Tm.cc:141
void fetchData(std::map< EcalLogicID, RunDCSHVDat > *fillMap, RunIOV *iov) noexcept(false)
Definition: RunDCSHVDat.cc:34
~RunDCSHVDat() override
Definition: RunDCSHVDat.cc:28
void setStatusForBarrel(RunDCSHVDat &, const Tm &)
Definition: RunDCSHVDat.cc:377
Definition: query.py:1
U second(std::pair< T, U > const &p)
Definition: TTTypes.h:54
float getHV() const
Definition: RunDCSHVDat.h:38
void getReducedDataList(std::list< std::pair< Tm, DataMap > > *my_new_list)
Definition: DataReducer.h:54
void fetchLastData(std::map< EcalLogicID, RunDCSHVDat > *fillMap) noexcept(false)
Definition: RunDCSHVDat.cc:416
void prepareWrite() noexcept(false) override
Definition: RunDCSHVDat.cc:30
d
Definition: ztail.py:151
void setTimeStatus(int t)
Definition: RunDCSHVDat.h:42
void setToString(const std::string s) noexcept(false)
Definition: Tm.cc:150
void setStatus(int t)
Definition: RunDCSHVDat.h:36
uint64_t microsTime() const
Definition: Tm.cc:96
void setHVNominal(float t)
Definition: RunDCSHVDat.h:37
void fillTheMapByTime(ResultSet *rset, std::list< DataReducer< RunDCSHVDat >::MyData< RunDCSHVDat > > *my_data_list)
Definition: RunDCSHVDat.cc:278
float getHVNominal() const
Definition: RunDCSHVDat.h:39
def day(string)
Definition: getRunInfo.py:11
void fillTheMap(ResultSet *, std::map< EcalLogicID, RunDCSHVDat > *)
Definition: RunDCSHVDat.cc:240
int nowMicroseconds()
Definition: RunDCSHVDat.cc:369
void setHV(float t)
Definition: RunDCSHVDat.h:35
dh
Definition: cuy.py:354
Definition: RunIOV.h:13
std::string str() const
Definition: Tm.cc:66
Definition: Tm.h:13