CMS 3D CMS Logo

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