CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RunIOV.cc
Go to the documentation of this file.
1 #include <stdexcept>
3 
8 
9 using namespace std;
10 using namespace oracle::occi;
11 
13  m_conn = nullptr;
14  m_ID = 0;
15  m_runNum = 0;
16  m_runStart = Tm();
17  m_runEnd = Tm();
18 }
19 
21 
23  if (run != m_runNum) {
24  m_ID = 0;
25  m_runNum = run;
26  }
27 }
28 
29 void RunIOV::setID(int id) { m_ID = id; }
30 
31 run_t RunIOV::getRunNumber() const { return m_runNum; }
32 
34  if (start != m_runStart) {
35  m_ID = 0;
36  m_runStart = start;
37  }
38 }
39 
40 Tm RunIOV::getRunStart() const { return m_runStart; }
41 
42 void RunIOV::setRunEnd(const Tm& end) {
43  if (end != m_runEnd) {
44  m_ID = 0;
45  m_runEnd = end;
46  }
47 }
48 
49 Tm RunIOV::getRunEnd() const { return m_runEnd; }
50 
52  if (tag != m_runTag) {
53  m_ID = 0;
54  m_runTag = tag;
55  }
56 }
57 
58 RunTag RunIOV::getRunTag() const { return m_runTag; }
59 
60 int RunIOV::fetchID() noexcept(false) {
61  // Return from memory if available
62  if (m_ID) {
63  return m_ID;
64  }
65 
66  this->checkConnection();
67 
68  m_runTag.setConnection(m_env, m_conn);
69  int tagID = m_runTag.fetchID();
70  if (!tagID) {
71  return 0;
72  }
73 
74  DateHandler dh(m_env, m_conn);
75 
76  if (m_runEnd.isNull()) {
77  m_runEnd = dh.getPlusInfTm();
78  }
79 
80  try {
81  Statement* stmt = m_conn->createStatement();
82  stmt->setSQL(
83  "SELECT iov_id FROM run_iov "
84  "WHERE tag_id = :tag_id AND "
85  "run_num = :run_num AND "
86  "run_start = :run_start ");
87  stmt->setInt(1, tagID);
88  stmt->setInt(2, m_runNum);
89  stmt->setDate(3, dh.tmToDate(m_runStart));
90 
91  ResultSet* rset = stmt->executeQuery();
92 
93  if (rset->next()) {
94  m_ID = rset->getInt(1);
95  } else {
96  m_ID = 0;
97  }
98  m_conn->terminateStatement(stmt);
99  } catch (SQLException& e) {
100  throw(std::runtime_error("RunIOV::fetchID: " + e.getMessage()));
101  }
102 
103  return m_ID;
104 }
105 
106 void RunIOV::setByID(int id) noexcept(false) {
107  this->checkConnection();
108 
109  DateHandler dh(m_env, m_conn);
110 
111  try {
112  Statement* stmt = m_conn->createStatement();
113 
114  stmt->setSQL("SELECT tag_id, run_num, run_start, run_end FROM run_iov WHERE iov_id = :1");
115  stmt->setInt(1, id);
116 
117  ResultSet* rset = stmt->executeQuery();
118  if (rset->next()) {
119  int tagID = rset->getInt(1);
120  m_runNum = rset->getInt(2);
121  Date startDate = rset->getDate(3);
122  Date endDate = rset->getDate(4);
123 
124  m_runStart = dh.dateToTm(startDate);
125  m_runEnd = dh.dateToTm(endDate);
126 
127  m_runTag.setConnection(m_env, m_conn);
128  m_runTag.setByID(tagID);
129  m_ID = id;
130  } else {
131  throw(std::runtime_error("RunIOV::setByID: Given tag_id is not in the database"));
132  }
133 
134  m_conn->terminateStatement(stmt);
135  } catch (SQLException& e) {
136  throw(std::runtime_error("RunIOV::setByID: " + e.getMessage()));
137  }
138 }
139 
140 int RunIOV::writeDB() noexcept(false) {
141  this->checkConnection();
142 
143  // Check if this IOV has already been written
144  if (this->fetchID()) {
145  return m_ID;
146  }
147 
148  m_runTag.setConnection(m_env, m_conn);
149  int tagID = m_runTag.writeDB();
150 
151  // Validate the data, use infinity-till convention
152  DateHandler dh(m_env, m_conn);
153 
154  if (m_runStart.isNull()) {
155  throw(std::runtime_error("RunIOV::writeDB: Must setRunStart before writing"));
156  }
157 
158  if (m_runEnd.isNull()) {
159  m_runEnd = dh.getPlusInfTm();
160  }
161 
162  try {
163  Statement* stmt = m_conn->createStatement();
164 
165  stmt->setSQL(
166  "INSERT INTO run_iov (iov_id, tag_id, run_num, run_start, run_end) "
167  "VALUES (run_iov_sq.NextVal, :1, :2, :3, :4)");
168  stmt->setInt(1, tagID);
169  stmt->setInt(2, m_runNum);
170  stmt->setDate(3, dh.tmToDate(m_runStart));
171  stmt->setDate(4, dh.tmToDate(m_runEnd));
172 
173  stmt->executeUpdate();
174 
175  m_conn->terminateStatement(stmt);
176  } catch (SQLException& e) {
177  throw(std::runtime_error("RunIOV::writeDB: " + e.getMessage()));
178  }
179 
180  // Now get the ID
181  if (!this->fetchID()) {
182  throw(std::runtime_error("RunIOV::writeDB: Failed to write"));
183  }
184 
185  return m_ID;
186 }
187 
189  this->checkConnection();
190 
191  // Check if this IOV has already been written
192  if (!this->fetchID()) {
193  this->writeDB();
194  }
195 
196  m_runTag.setConnection(m_env, m_conn);
197  // int tagID = m_runTag.writeDB();
198 
199  // Validate the data, use infinity-till convention
200  DateHandler dh(m_env, m_conn);
201 
202  // we only update the run end here
203  if (m_runEnd.isNull()) {
204  m_runEnd = dh.getPlusInfTm();
205  }
206 
207  try {
208  Statement* stmt = m_conn->createStatement();
209 
210  stmt->setSQL("UPDATE run_iov set run_end=:1 where iov_id=:2 ");
211  stmt->setDate(1, dh.tmToDate(m_runEnd));
212  stmt->setInt(2, m_ID);
213 
214  stmt->executeUpdate();
215 
216  m_conn->terminateStatement(stmt);
217  } catch (SQLException& e) {
218  throw(std::runtime_error("RunIOV::writeDB: " + e.getMessage()));
219  }
220 
221  // Now get the ID
222  if (!this->fetchID()) {
223  throw(std::runtime_error("RunIOV::writeDB: Failed to write"));
224  }
225 
226  return m_ID;
227 }
228 
230  // Return from memory if available
231  if (m_ID) {
232  return m_ID;
233  }
234 
235  this->checkConnection();
236 
237  m_runTag.setConnection(m_env, m_conn);
238  int tagID = m_runTag.fetchID();
239  if (!tagID) {
240  return 0;
241  }
242 
243  DateHandler dh(m_env, m_conn);
244 
245  if (m_runEnd.isNull()) {
246  m_runEnd = dh.getPlusInfTm();
247  }
248 
249  try {
250  Statement* stmt = m_conn->createStatement();
251  stmt->setSQL(
252  "SELECT iov_id FROM run_iov "
253  "WHERE tag_id = :tag_id AND "
254  "run_num = :run_num ");
255  stmt->setInt(1, tagID);
256  stmt->setInt(2, m_runNum);
257 
258  ResultSet* rset = stmt->executeQuery();
259 
260  if (rset->next()) {
261  m_ID = rset->getInt(1);
262  } else {
263  m_ID = 0;
264  }
265  m_conn->terminateStatement(stmt);
266  } catch (SQLException& e) {
267  throw(std::runtime_error("RunIOV::fetchID: " + e.getMessage()));
268  }
269 
270  return m_ID;
271 }
272 
274  this->checkConnection();
275 
276  // Check if this IOV has already been written
277  if (!this->fetchIDByRunAndTag()) {
278  this->writeDB();
279  }
280 
281  // m_runTag.setConnection(m_env, m_conn);
282  // int tagID = m_runTag.writeDB();
283 
284  // Validate the data, use infinity-till convention
285  DateHandler dh(m_env, m_conn);
286 
287  // we only update the run start here
288  if (m_runEnd.isNull()) {
289  m_runEnd = dh.getPlusInfTm();
290  }
291 
292  try {
293  Statement* stmt = m_conn->createStatement();
294 
295  stmt->setSQL("UPDATE run_iov set run_start=:1 where iov_id=:2 ");
296  stmt->setDate(1, dh.tmToDate(m_runStart));
297  stmt->setInt(2, m_ID);
298 
299  stmt->executeUpdate();
300 
301  m_conn->terminateStatement(stmt);
302  } catch (SQLException& e) {
303  throw(std::runtime_error("RunIOV::writeDB: " + e.getMessage()));
304  }
305 
306  // Now get the ID
307  if (!this->fetchID()) {
308  throw(std::runtime_error("RunIOV::writeDB: Failed to write"));
309  }
310 
311  return m_ID;
312 }
313 
314 void RunIOV::setByRun(RunTag* tag, run_t run) noexcept(false) {
315  this->checkConnection();
316 
317  tag->setConnection(m_env, m_conn);
318  int tagID = tag->fetchID();
319  if (!tagID) {
320  throw(std::runtime_error("RunIOV::setByRun: Given tag is not in the database"));
321  }
322 
323  DateHandler dh(m_env, m_conn);
324 
325  try {
326  Statement* stmt = m_conn->createStatement();
327 
328  stmt->setSQL("SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
329  stmt->setInt(1, tagID);
330  stmt->setInt(2, run);
331 
332  ResultSet* rset = stmt->executeQuery();
333  if (rset->next()) {
334  m_runTag = *tag;
335  m_runNum = run;
336 
337  m_ID = rset->getInt(1);
338  Date startDate = rset->getDate(2);
339  Date endDate = rset->getDate(3);
340 
341  m_runStart = dh.dateToTm(startDate);
342  m_runEnd = dh.dateToTm(endDate);
343  } else {
344  throw(std::runtime_error("RunIOV::setByRun: Given run is not in the database"));
345  }
346 
347  m_conn->terminateStatement(stmt);
348  } catch (SQLException& e) {
349  throw(std::runtime_error("RunIOV::setByRun: " + e.getMessage()));
350  }
351 }
352 
353 void RunIOV::setByTime(std::string location, const Tm& t) noexcept(false) {
354  this->checkConnection();
355 
356  DateHandler dh(m_env, m_conn);
357 
358  try {
359  Statement* stmt = m_conn->createStatement();
360 
361  stmt->setSQL(
362  "SELECT iov_id FROM (SELECT iov_id FROM run_iov riov "
363  "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
364  "JOIN location_def loc ON rtag.location_id = loc.def_id "
365  "WHERE loc.location = :1 AND "
366  "run_start <= to_date(:2, 'YYYY-MM-DD HH24:MI:SS') AND "
367  "run_end >= to_date(:3, 'YYYY-MM-DD HH24:MI:SS') "
368  "AND rtag.gen_tag != 'INVALID' ORDER BY iov_id DESC) "
369  "where ROWNUM <=1");
370  stmt->setString(1, location);
371  stmt->setString(2, t.str());
372  stmt->setString(3, t.str());
373 
374  ResultSet* rset = stmt->executeQuery();
375  if (rset->next()) {
376  int id = rset->getInt(1);
377  this->setByID(id);
378  } else {
379  throw(std::runtime_error("RunIOV::setByTime(loc, run): Given run is not in the database"));
380  }
381 
382  // Check for uniqueness of run
383  if (rset->next()) {
384  throw(std::runtime_error("RunIOV::setByTime(loc, run): Run is nonunique for given location."));
385  }
386 
387  m_conn->terminateStatement(stmt);
388  } catch (SQLException& e) {
389  throw(std::runtime_error("RunIOV::setByTime(loc, run): " + e.getMessage()));
390  }
391 }
392 
393 void RunIOV::setByRun(std::string location, run_t run) noexcept(false) {
394  this->checkConnection();
395 
396  DateHandler dh(m_env, m_conn);
397 
398  try {
399  Statement* stmt = m_conn->createStatement();
400 
401  stmt->setSQL(
402  "SELECT iov_id FROM run_iov riov "
403  "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
404  "JOIN location_def loc ON rtag.location_id = loc.def_id "
405  "WHERE loc.location = :1 AND riov.run_num = :2 "
406  "AND rtag.gen_tag != 'INVALID'");
407  stmt->setString(1, location);
408  stmt->setInt(2, run);
409 
410  ResultSet* rset = stmt->executeQuery();
411  if (rset->next()) {
412  int id = rset->getInt(1);
413  this->setByID(id);
414  } else {
415  throw(std::runtime_error("RunIOV::setByRun(loc, run): Given run is not in the database"));
416  }
417 
418  // Check for uniqueness of run
419  if (rset->next()) {
420  throw(std::runtime_error("RunIOV::setByRun(loc, run): Run is nonunique for given location."));
421  }
422 
423  m_conn->terminateStatement(stmt);
424  } catch (SQLException& e) {
425  throw(std::runtime_error("RunIOV::setByRun(loc, run): " + e.getMessage()));
426  }
427 }
428 
429 void RunIOV::setByRecentData(std::string dataTable, RunTag* tag, run_t run) noexcept(false) {
430  this->checkConnection();
431 
432  tag->setConnection(m_env, m_conn);
433  int tagID = tag->fetchID();
434  if (!tagID) {
435  throw(std::runtime_error("RunIOV::setByRecentData: Given tag is not in the database"));
436  }
437 
438  DateHandler dh(m_env, m_conn);
439 
440  try {
441  Statement* stmt = m_conn->createStatement();
442 
443  stmt->setSQL(
444  "SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
445  "FROM run_iov riov "
446  "JOIN " +
447  dataTable +
448  " dat on dat.iov_id = riov.iov_id "
449  "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
450 
451  stmt->setInt(1, tagID);
452  stmt->setInt(2, run);
453 
454  ResultSet* rset = stmt->executeQuery();
455  if (rset->next()) {
456  m_runTag = *tag;
457 
458  m_ID = rset->getInt(1);
459  m_runNum = rset->getInt(2);
460  Date startDate = rset->getDate(3);
461  Date endDate = rset->getDate(4);
462 
463  m_runStart = dh.dateToTm(startDate);
464  m_runEnd = dh.dateToTm(endDate);
465  } else {
466  throw(std::runtime_error("RunIOV::setByRecentData: No data exists for given tag and run"));
467  }
468 
469  m_conn->terminateStatement(stmt);
470  } catch (SQLException& e) {
471  throw(std::runtime_error("RunIOV::setByRecentData: " + e.getMessage()));
472  }
473 }
474 
475 void RunIOV::setByRecentData(std::string dataTable, std::string location, run_t run) noexcept(false) {
476  this->checkConnection();
477 
478  DateHandler dh(m_env, m_conn);
479 
480  try {
481  Statement* stmt = m_conn->createStatement();
482 
483  stmt->setSQL(
484  "SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
485  "FROM run_iov riov "
486  "JOIN " +
487  dataTable +
488  " dat on dat.iov_id = riov.iov_id "
489  "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
490  "JOIN location_def loc ON rtag.location_id = loc.def_id "
491  "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
492 
493  stmt->setString(1, location);
494  stmt->setInt(2, run);
495 
496  ResultSet* rset = stmt->executeQuery();
497 
498  if (rset->next()) {
499  int id = rset->getInt(1);
500  this->setByID(id);
501  } else {
502  throw(std::runtime_error("RunIOV::setByRecentData(datatable, loc, run): Given run is not in the database"));
503  }
504 
505  m_conn->terminateStatement(stmt);
506  } catch (SQLException& e) {
507  throw(std::runtime_error("RunIOV::setByRecentData: " + e.getMessage()));
508  }
509 }
int updateEndTimeDB() noexcept(false)
Definition: RunIOV.cc:188
void setByTime(std::string location, const Tm &t) noexcept(false)
Definition: RunIOV.cc:353
void setByID(int id) noexcept(false) override
Definition: RunIOV.cc:106
int run_t
Definition: CaliIOV.h:11
void setRunStart(const Tm &start)
Definition: RunIOV.cc:33
uint16_t *__restrict__ id
int fetchIDByRunAndTag() noexcept(false)
Definition: RunIOV.cc:229
Definition: RunTag.h:13
RunIOV()
Definition: RunIOV.cc:12
int updateStartTimeDB() noexcept(false)
Definition: RunIOV.cc:273
void setRunTag(const RunTag &tag)
Definition: RunIOV.cc:51
void setRunNumber(run_t run)
Definition: RunIOV.cc:22
RunTag getRunTag() const
Definition: RunIOV.cc:58
Tm getRunStart() const
Definition: RunIOV.cc:40
void setByRun(RunTag *tag, run_t run) noexcept(false)
Definition: RunIOV.cc:314
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:19
int writeDB() noexcept(false)
Definition: RunIOV.cc:140
Tm getPlusInfTm() const
Definition: DateHandler.h:13
Tm getRunEnd() const
Definition: RunIOV.cc:49
void setRunEnd(const Tm &end)
Definition: RunIOV.cc:42
run_t getRunNumber() const
Definition: RunIOV.cc:31
string end
Definition: dataset.py:937
~RunIOV() override
Definition: RunIOV.cc:20
int fetchID() noexcept(false) override
Definition: RunIOV.cc:60
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:28
Definition: Tm.h:13
void setByRecentData(std::string dataTable, RunTag *tag, run_t run=(unsigned int)-1) noexcept(false)
Definition: RunIOV.cc:429
void setID(int id)
Definition: RunIOV.cc:29
tuple dh
Definition: cuy.py:354