CMS 3D CMS Logo

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