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