CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MonRunIOV.cc
Go to the documentation of this file.
1 #include <stdexcept>
3 
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
14  m_conn = nullptr;
15  m_ID = 0;
16  m_monRunTag = MonRunTag();
17  m_runIOV = RunIOV();
18  m_subRunNum = 0;
19  m_subRunStart = Tm();
20  m_subRunEnd = Tm();
21 }
22 
24 
25 void MonRunIOV::setID(int id) { m_ID = id; }
26 
28  if (tag != m_monRunTag) {
29  m_ID = 0;
30  m_monRunTag = tag;
31  }
32 }
33 
34 MonRunTag MonRunIOV::getMonRunTag() const { return m_monRunTag; }
35 
36 void MonRunIOV::setRunIOV(const RunIOV& iov) {
37  if (iov != m_runIOV) {
38  m_ID = 0;
39  m_runIOV = iov;
40  }
41 }
42 
43 RunIOV MonRunIOV::getRunIOV() { return m_runIOV; }
44 
46  if (subrun != m_subRunNum) {
47  m_ID = 0;
48  m_subRunNum = subrun;
49  }
50 }
51 
52 run_t MonRunIOV::getSubRunNumber() const { return m_subRunNum; }
53 
55  if (start != m_subRunStart) {
56  m_ID = 0;
57  m_subRunStart = start;
58  }
59 }
60 
61 Tm MonRunIOV::getSubRunStart() const { return m_subRunStart; }
62 
64  if (end != m_subRunEnd) {
65  m_ID = 0;
66  m_subRunEnd = end;
67  }
68 }
69 
70 Tm MonRunIOV::getSubRunEnd() const { return m_subRunEnd; }
71 
72 int MonRunIOV::fetchID() noexcept(false) {
73  // Return from memory if available
74  if (m_ID) {
75  return m_ID;
76  }
77 
78  this->checkConnection();
79 
80  // fetch the parent IDs
81  int monRunTagID, runIOVID;
82  this->fetchParentIDs(&monRunTagID, &runIOVID);
83 
84  if (!monRunTagID || !runIOVID) {
85  return 0;
86  }
87 
88  DateHandler dh(m_env, m_conn);
89 
90  if (m_subRunEnd.isNull()) {
91  m_subRunEnd = dh.getPlusInfTm();
92  }
93 
94  try {
95  Statement* stmt = m_conn->createStatement();
96  stmt->setSQL(
97  "SELECT iov_id FROM mon_run_iov "
98  "WHERE tag_id = :1 AND "
99  "run_iov_id = :2 AND "
100  "subrun_num = :3 AND "
101  "subrun_start = :4 AND "
102  "subrun_end = :5");
103  stmt->setInt(1, monRunTagID);
104  stmt->setInt(2, runIOVID);
105  stmt->setInt(3, m_subRunNum);
106  stmt->setDate(4, dh.tmToDate(m_subRunStart));
107  stmt->setDate(5, dh.tmToDate(m_subRunEnd));
108 
109  ResultSet* rset = stmt->executeQuery();
110 
111  if (rset->next()) {
112  m_ID = rset->getInt(1);
113  } else {
114  m_ID = 0;
115  }
116  m_conn->terminateStatement(stmt);
117  } catch (SQLException& e) {
118  throw(std::runtime_error("MonRunIOV::fetchID: " + e.getMessage()));
119  }
120 
121  return m_ID;
122 }
123 
124 void MonRunIOV::setByID(int id) noexcept(false) {
125  this->checkConnection();
126 
127  DateHandler dh(m_env, m_conn);
128 
129  try {
130  Statement* stmt = m_conn->createStatement();
131 
132  stmt->setSQL("SELECT tag_id, run_iov_id, subrun_num, subrun_start, subrun_end FROM mon_run_iov WHERE iov_id = :1");
133  stmt->setInt(1, id);
134 
135  ResultSet* rset = stmt->executeQuery();
136  if (rset->next()) {
137  int monRunTagID = rset->getInt(1);
138  int runIOVID = rset->getInt(2);
139  m_subRunNum = rset->getInt(3);
140  Date startDate = rset->getDate(4);
141  Date endDate = rset->getDate(5);
142 
143  m_subRunStart = dh.dateToTm(startDate);
144  m_subRunEnd = dh.dateToTm(endDate);
145 
146  m_monRunTag.setConnection(m_env, m_conn);
147  m_monRunTag.setByID(monRunTagID);
148 
149  m_runIOV.setConnection(m_env, m_conn);
150  m_runIOV.setByID(runIOVID);
151 
152  m_ID = id;
153  } else {
154  throw(std::runtime_error("MonRunIOV::setByID: Given tag_id is not in the database"));
155  }
156 
157  m_conn->terminateStatement(stmt);
158  } catch (SQLException& e) {
159  throw(std::runtime_error("MonRunIOV::setByID: " + e.getMessage()));
160  }
161 }
162 
163 int MonRunIOV::writeDB() noexcept(false) {
164  this->checkConnection();
165 
166  // Check if this IOV has already been written
167  if (this->fetchID()) {
168  return m_ID;
169  }
170 
171  // fetch Parent IDs
172  int monRunTagID, runIOVID;
173  this->fetchParentIDs(&monRunTagID, &runIOVID);
174 
175  if (!monRunTagID) {
176  monRunTagID = m_monRunTag.writeDB();
177  }
178 
179  // Validate the data, use infinity-till convention
180  DateHandler dh(m_env, m_conn);
181 
182  if (m_subRunStart.isNull()) {
183  throw(std::runtime_error("MonRunIOV::writeDB: Must setSubRunStart before writing"));
184  }
185 
186  if (m_subRunEnd.isNull()) {
187  m_subRunEnd = dh.getPlusInfTm();
188  }
189 
190  try {
191  Statement* stmt = m_conn->createStatement();
192 
193  stmt->setSQL(
194  "INSERT INTO mon_run_iov (iov_id, tag_id, run_iov_id, subrun_num, subrun_start, subrun_end) "
195  "VALUES (mon_run_iov_sq.NextVal, :1, :2, :3, :4, :5)");
196  stmt->setInt(1, monRunTagID);
197  stmt->setInt(2, runIOVID);
198  stmt->setInt(3, m_subRunNum);
199  stmt->setDate(4, dh.tmToDate(m_subRunStart));
200  stmt->setDate(5, dh.tmToDate(m_subRunEnd));
201 
202  stmt->executeUpdate();
203 
204  m_conn->terminateStatement(stmt);
205  } catch (SQLException& e) {
206  throw(std::runtime_error("MonRunIOV::writeDB: " + e.getMessage()));
207  }
208 
209  // Now get the ID
210  if (!this->fetchID()) {
211  throw(std::runtime_error("MonRunIOV::writeDB: Failed to write"));
212  }
213 
214  return m_ID;
215 }
216 
217 void MonRunIOV::fetchParentIDs(int* monRunTagID, int* runIOVID) noexcept(false) {
218  // get the MonRunTag
219  m_monRunTag.setConnection(m_env, m_conn);
220  *monRunTagID = m_monRunTag.fetchID();
221 
222  // get the RunIOV
223  m_runIOV.setConnection(m_env, m_conn);
224  *runIOVID = m_runIOV.fetchID();
225 
226  if (!*runIOVID) {
227  throw(std::runtime_error("MonRunIOV: Given RunIOV does not exist in DB"));
228  }
229 }
230 
231 void MonRunIOV::setByRun(MonRunTag* montag, RunIOV* runiov, subrun_t subrun) noexcept(false) {
232  this->checkConnection();
233 
234  runiov->setConnection(m_env, m_conn);
235  int runIOVID = runiov->fetchID();
236 
237  if (!runIOVID) {
238  throw(std::runtime_error("MonRunIOV::setByRun: Given RunIOV does not exist in DB"));
239  }
240 
241  montag->setConnection(m_env, m_conn);
242  int monTagID = montag->fetchID();
243 
244  if (!monTagID) {
245  throw(std::runtime_error("MonRunIOV::setByRun: Given MonRunTag does not exist in the DB"));
246  }
247 
248  DateHandler dh(m_env, m_conn);
249 
250  try {
251  Statement* stmt = m_conn->createStatement();
252 
253  stmt->setSQL(
254  "SELECT iov_id, subrun_start, subrun_end FROM mon_run_iov "
255  "WHERE tag_id = :1 AND run_iov_id = :2 AND subrun_num = :3");
256  stmt->setInt(1, monTagID);
257  stmt->setInt(2, runIOVID);
258  stmt->setInt(3, subrun);
259 
260  ResultSet* rset = stmt->executeQuery();
261  if (rset->next()) {
262  m_monRunTag = *montag;
263  m_runIOV = *runiov;
264  m_subRunNum = subrun;
265 
266  m_ID = rset->getInt(1);
267  Date startDate = rset->getDate(2);
268  Date endDate = rset->getDate(3);
269 
270  m_subRunStart = dh.dateToTm(startDate);
271  m_subRunEnd = dh.dateToTm(endDate);
272  } else {
273  throw(std::runtime_error("MonRunIOV::setByRun: Given subrun is not in the database"));
274  }
275 
276  m_conn->terminateStatement(stmt);
277  } catch (SQLException& e) {
278  throw(std::runtime_error("MonRunIOV::setByRun: " + e.getMessage()));
279  }
280 }
int run_t
Definition: CaliIOV.h:11
void setByID(int id) noexcept(false) override
Definition: MonRunIOV.cc:124
void fetchParentIDs(int *monRunTagID, int *runIOVID) noexcept(false)
Definition: MonRunIOV.cc:217
RunIOV getRunIOV()
Definition: MonRunIOV.cc:43
uint16_t *__restrict__ id
int writeDB() noexcept(false)
Definition: MonRunIOV.cc:163
~MonRunIOV() override
Definition: MonRunIOV.cc:23
void setRunIOV(const RunIOV &iov)
Definition: MonRunIOV.cc:36
run_t getSubRunNumber() const
Definition: MonRunIOV.cc:52
int fetchID() noexcept(false) override
Definition: MonRunIOV.cc:72
Tm getSubRunStart() const
Definition: MonRunIOV.cc:61
MonRunTag getMonRunTag() const
Definition: MonRunIOV.cc:34
int subrun_t
Definition: MODRunIOV.h:11
void setSubRunEnd(const Tm &end)
Definition: MonRunIOV.cc:63
void setSubRunNumber(subrun_t subrun)
Definition: MonRunIOV.cc:45
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:19
void setSubRunStart(const Tm &start)
Definition: MonRunIOV.cc:54
Tm getPlusInfTm() const
Definition: DateHandler.h:13
Tm getSubRunEnd() const
Definition: MonRunIOV.cc:70
void setMonRunTag(const MonRunTag &tag)
Definition: MonRunIOV.cc:27
string end
Definition: dataset.py:937
void setByRun(MonRunTag *montag, RunIOV *runiov, subrun_t subrun) noexcept(false)
Definition: MonRunIOV.cc:231
Definition: RunIOV.h:13
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:28
Definition: Tm.h:13
tuple dh
Definition: cuy.py:354
void setID(int id)
Definition: MonRunIOV.cc:25