CMS 3D CMS Logo

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