CMS 3D CMS Logo

LMFSeqDat.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <sstream>
3 #include <climits>
6 
7 using namespace std;
8 using namespace oracle::occi;
9 
11 {
12  setClassName("LMFSeqDat");
13 
14  m_runIOV = RunIOV();
15  m_intFields["seq_num"] = 0;
16  Tm t;
17  t = t.plusInfinity();
18  m_stringFields["seq_start"] = t.str();
19  m_stringFields["seq_stop"] = t.str();
20  m_intFields["vmin"] = 1;
21  m_intFields["vmax"] = 0;
22 }
23 
25  setClassName("LMFSeqDat");
26 
27  m_runIOV = RunIOV();
28  m_intFields["seq_num"] = 0;
29  Tm t;
30  t = t.plusInfinity();
31  m_stringFields["seq_start"] = t.str();
32  m_stringFields["seq_stop"] = t.str();
33  m_intFields["vmin"] = 1;
34  m_intFields["vmax"] = 0;
35 }
36 
37 LMFSeqDat::LMFSeqDat(oracle::occi::Environment* env,
38  oracle::occi::Connection* conn) : LMFUnique(env, conn) {
39  setClassName("LMFSeqDat");
40 
41  m_runIOV = RunIOV();
42  m_intFields["seq_num"] = 0;
43  Tm t;
44  t = t.plusInfinity();
45  m_stringFields["seq_start"] = t.str();
46  m_stringFields["seq_stop"] = t.str();
47  m_intFields["vmin"] = 1;
48  m_intFields["vmax"] = 0;
49 }
50 
52 {
53 }
54 
56  Tm t;
57  t.setToString(getString("seq_stop"));
58  return t;
59 }
60 
62 {
63  return m_runIOV;
64 }
65 
67 {
68  if (iov != m_runIOV) {
69  m_ID = 0;
70  m_runIOV = iov;
71  }
72  return *this;
73 }
74 
76 {
77  int runIOVID = m_runIOV.getID();
78  if (!runIOVID) {
79  if (m_debug) {
80  std::cout << m_className << ": RunIOV not set" << endl;
81  }
82  return "";
83  }
84 
85  if (m_debug) {
86  std::cout << "Run IOV ID: " << runIOVID << std::endl;
87  std::cout << "SEQ # : " << getSequenceNumber() << std::endl;
88  std::cout << "Versions : " << getVmin() << " - " << getVmax() << endl;
89  }
90  std::string sql = "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
91  "WHERE "
92  "RUN_IOV_ID = :1 AND "
93  "SEQ_NUM = :2 AND "
94  "VMIN = :3 ";
95  if (getVmax() > 0) {
96  sql += "AND VMAX = :4";
97  } else {
98  sql += "ORDER BY VMAX DESC";
99  }
100  stmt->setSQL(sql);
101  stmt->setInt(1, runIOVID);
102  stmt->setInt(2, getSequenceNumber());
103  stmt->setInt(3, getVmin());
104  if (getVmax() > 0) {
105  stmt->setInt(4, getVmax());
106  }
107  return sql;
108 }
109 
111  std::string sql = "SELECT RUN_IOV_ID, SEQ_NUM, SEQ_START, SEQ_STOP, "
112  "VMIN, VMAX FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT WHERE SEQ_ID = :1";
113  stmt->setSQL(sql);
114  stmt->setInt(1, id);
115  return sql;
116 }
117 
120  int runIOVID = rset->getInt(1);
121  setInt("seq_num", rset->getInt(2));
122  Date startDate = rset->getDate(3);
123  Date endDate = rset->getDate(4);
124  setInt("vmin", rset->getInt(5));
125  setInt("vmax", rset->getInt(6));
126 
127  setString("seq_start", dh.dateToTm( startDate ).str());
128  setString("seq_stop", dh.dateToTm( endDate ).str());
129 
131  m_runIOV.setByID(runIOVID);
132 }
133 
134 bool LMFSeqDat::isValid() const {
135  bool ret = true;
136  if (getSequenceStart().isNull()) {
137  ret = false;
138  }
139  if ((getSequenceStop().str().length() > 0) &&
140  (getSequenceStop().microsTime() < getSequenceStart().microsTime())) {
141  ret = false;
142  }
143  if (getSequenceStop() == Tm().plusInfinity()) {
144  ret = false;
145  }
146  return ret;
147 }
148 
150 {
151  // Validate the data, use infinity-till convention
153 
154  if (!isValid()) {
155  dump();
156  throw(std::runtime_error("LMFSeqDat::writeDB: not valid"));
157  }
158 
159  if (getSequenceStop().str().length() == 0) {
161  }
162  int runIOVID = m_runIOV.getID();
163  if (runIOVID == 0) {
164  throw(std::runtime_error("LMFSeqDat::writeDB: RunIOV not set"));
165  }
167  std::string sql = "INSERT INTO LMF_SEQ_DAT (SEQ_ID, RUN_IOV_ID, SEQ_NUM, "
168  "SEQ_START, SEQ_STOP, VMIN, VMAX) "
169  "VALUES (SEQ_ID_" + sp + "_SQ.NextVal, :1, :2, :3, :4, :5, :6)";
170  cout << sql << endl;
171  stmt->setSQL(sql);
172  stmt->setInt(1, runIOVID);
173  stmt->setInt(2, getSequenceNumber());
174  stmt->setDate(3, dh.tmToDate(getSequenceStart()));
175  stmt->setDate(4, dh.tmToDate(getSequenceStop()));
176  stmt->setInt(5, getVmin());
177  stmt->setInt(6, getVmax());
178  return sql;
179 }
180 
182  noexcept(false)
183 {
184  // get the RunIOV
186  int runIOVID = m_runIOV.getID();
187  m_runIOV.setByID(runIOVID);
188 
189  if (m_runIOV.getID() == 0) {
190  throw(std::runtime_error("LMFSeqDat: Given RunIOV does not exist in DB"));
191  }
192 
193 }
194 
195 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(std::string sql,
197  noexcept(false)
198 {
199  std::vector<std::string> pars;
200  return fetchByRunIOV(pars, sql, method);
201 }
202 
203 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(int par,
204  std::string sql,
206  noexcept(false)
207 {
208  std::vector<std::string> pars;
209  std::stringstream ss;
210  ss << "I" << par;
211  pars.push_back(ss.str());
212  return fetchByRunIOV(pars, sql, method);
213 }
214 
215 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(const std::vector<std::string>& pars,
216  std::string sql,
218  noexcept(false)
219 {
220  std::map<int, LMFSeqDat> l;
221  this->checkConnection();
222  try {
223  Statement *stmt = m_conn->createStatement();
224  stmt->setSQL(sql);
225  for (unsigned int i = 0; i < pars.size(); i++) {
226  if (pars[i][0] == 'I') {
227  stmt->setInt(i + 1, atoi(pars[i].c_str() + 1));
228  } else if (pars[i][0] == 'S') {
229  stmt->setString(i + 1, pars[i].c_str() + 1);
230  } else {
231  throw(std::runtime_error(m_className + "::" + method + ": " +
232  "Invalid type"));
233  }
234  }
235  ResultSet *rset = stmt->executeQuery();
236  while (rset->next() != 0) {
237  int seq_id = rset->getInt(1);
238  LMFSeqDat s;
240  s.setByID(seq_id);
241  l[s.getSequenceNumber()] = s;
242  }
243  m_conn->terminateStatement(stmt);
244  } catch (SQLException &e) {
245  throw(std::runtime_error(m_className + "::" + method + ": " +
246  e.getMessage()));
247  }
248  return l;
249 }
250 
252  LMFSeqDat ret;
253  std::map<int, LMFSeqDat> m =
254  fetchByRunIOV("SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
255  "WHERE SEQ_ID = "
256  "(SELECT MAX(SEQ_ID) FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT)",
257  "fetchLast");
258  if (!m.empty()) {
259  ret = m.begin()->second;
260  }
261  return ret;
262 }
263 
265  return fetchLast().getRunIOV();
266 }
267 
268 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov) {
269  int runIOVID = iov.getID();
270  return fetchByRunIOV(runIOVID,
271  "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
272  "WHERE RUN_IOV_ID = :1",
273  "fetchByRunIOV");
274 }
275 
276 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov,
277  const LMFColor &col) {
278  int runIOVID = iov.getID();
279  int colorId = col.getID();
280  std::vector<std::string> pars;
281  std::stringstream ss;
282  ss << "I" << runIOVID;
283  pars.push_back(ss.str());
284  ss.str(std::string());
285  ss << "I" << colorId;
286  pars.push_back(ss.str());
287  return fetchByRunIOV(pars,
288  "SELECT S.SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
289  "S JOIN CMS_ECAL_LASER_COND.LMF_RUN_IOV R"
290  " ON S.SEQ_ID = R.SEQ_ID WHERE RUN_IOV_ID = :1 AND "
291  " COLOR_ID = :2",
292  "fetchByRunIOVAndColor");
293 }
294 
295 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunNumber(int runno) {
296  return fetchByRunIOV(runno,
297  "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT D "
298  "JOIN CMS_ECAL_COND.RUN_IOV R ON "
299  "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1",
300  "fetchByRunNumber");
301 }
302 
304  return fetchByRunNumber(runno, taken_at.str());
305 }
306 
308  std::map<int, LMFSeqDat> l;
309  std::vector<std::string> pars;
310  std::stringstream ss;
311  ss << "I" << runno;
312  pars.push_back(ss.str());
313  ss.str(std::string());
314  ss << "S" << taken_at;
315  pars.push_back(ss.str());
316  std::string q = "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT D "
317  "JOIN CMS_ECAL_COND.RUN_IOV R ON "
318  "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1 AND "
319  "SEQ_START >= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS') "
320  "AND SEQ_STOP <= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS')";
321  l = fetchByRunIOV(pars, q, "fetchByRunNumberAt");
322  LMFSeqDat ret;
323  if (l.size() == 1) {
324  std::map<int, LMFSeqDat>::const_iterator x = l.begin();
325  ret = x->second;
326  } else if (l.size() > 1) {
327  std::cout << "WARNING: Your query returned more than one result. "
328  << std::endl;
329  std::cout << " This was not expected. Please check the DB!!!"
330  << std::endl;
331  std::cout << "Your query: " << std::endl << q << std::endl;
332  std::cout << "Your parameters: " << std::endl;
333  for (unsigned int i = 0; i < pars.size(); i++) {
334  std::cout << i << ": " << pars[i] << std::endl;
335  }
336  }
337  return ret;
338 }
339 
std::string m_className
Definition: LMFUnique.h:98
void setByID(int id) noexcept(false) override
Definition: RunIOV.cc:152
LMFUnique & setInt(std::string key, int value)
Definition: LMFUnique.cc:33
oracle::occi::Environment * m_env
Definition: IDBObject.h:38
std::map< int, LMFSeqDat > fetchByRunNumber(int runno)
Definition: LMFSeqDat.cc:295
Tm getSequenceStop() const
Definition: LMFSeqDat.cc:55
oracle::occi::Connection * m_conn
Definition: IDBObject.h:39
LMFSeqDat & setRunIOV(const RunIOV &iov)
Definition: LMFSeqDat.cc:66
LMFSeqDat & setSequenceStop(const Tm &end)
Definition: LMFSeqDat.h:31
int getID() const
Definition: LMFUnique.h:52
std::map< int, LMFSeqDat > fetchByRunIOV(RunIOV &iov)
Definition: LMFSeqDat.cc:268
LMFSeqDat fetchLast()
Definition: LMFSeqDat.cc:251
int getVmax() const
Definition: LMFSeqDat.h:49
int getSequenceNumber() const
Definition: LMFSeqDat.h:41
std::string sequencePostfix(const Tm &t)
Definition: LMFUnique.cc:10
oracle::occi::ResultSet ResultSet
Definition: LMFUnique.h:19
virtual void setClassName(std::string s)
Definition: LMFUnique.h:96
void fetchParentIDs() noexcept(false) override
Definition: LMFSeqDat.cc:181
virtual void dump() const
Definition: LMFUnique.cc:112
void setByID(int id) noexcept(false) override
Definition: LMFUnique.cc:286
std::string setByIDSql(Statement *stmt, int id) override
Definition: LMFSeqDat.cc:110
std::map< std::string, std::string > m_stringFields
Definition: LMFUnique.h:101
RunIOV m_runIOV
Definition: LMFSeqDat.h:79
void checkConnection() const noexcept(false)
Definition: IDBObject.h:41
oracle::occi::Statement Statement
Definition: LMFUnique.h:20
Tm getSequenceStart() const
Definition: LMFSeqDat.h:42
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:20
~LMFSeqDat() override
Definition: LMFSeqDat.cc:51
LMFUnique & setString(std::string key, std::string value)
Definition: LMFUnique.cc:15
Tm getPlusInfTm() const
Definition: DateHandler.h:15
RunIOV getRunIOV() const
Definition: LMFSeqDat.cc:61
void getParameters(ResultSet *rset) override
Definition: LMFSeqDat.cc:118
#define noexcept
void setToString(const std::string s) noexcept(false)
Definition: Tm.cc:193
std::string writeDBSql(Statement *stmt) override
Definition: LMFSeqDat.cc:149
RunIOV fetchLastRun()
Definition: LMFSeqDat.cc:264
std::string fetchIdSql(Statement *stmt) override
Definition: LMFSeqDat.cc:75
std::string str() const
Definition: Tm.cc:89
bool isValid() const override
Definition: LMFSeqDat.cc:134
col
Definition: cuy.py:1010
int getID()
Definition: RunIOV.h:36
std::string getString(std::string fieldname) const
Definition: LMFUnique.cc:194
char m_debug
Definition: LMFUnique.h:99
void setConnection(oracle::occi::Environment *env, oracle::occi::Connection *conn)
Definition: IDBObject.h:23
dh
Definition: cuy.py:355
#define str(s)
Definition: RunIOV.h:13
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:31
Definition: Tm.h:13
static Tm plusInfinity()
Definition: Tm.h:46
int getVmin() const
Definition: LMFSeqDat.h:48
std::map< std::string, int > m_intFields
Definition: LMFUnique.h:103