CMS 3D CMS Logo

RunList.cc
Go to the documentation of this file.
1 #include <stdexcept>
3 
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
13 RunList::RunList() { m_conn = nullptr; }
14 
16 
18  if (tag != m_runTag) {
19  m_runTag = tag;
20  }
21 }
22 
23 RunTag RunList::getRunTag() const { return m_runTag; }
24 
25 std::vector<RunIOV> RunList::getRuns() { return m_vec_runiov; }
26 
27 void RunList::fetchNonEmptyRuns() noexcept(false) { fetchRuns(-1, -1, true, false); }
28 
29 void RunList::fetchNonEmptyGlobalRuns() noexcept(false) { fetchRuns(-1, -1, false, true); }
30 
31 void RunList::fetchNonEmptyRuns(int min_run, int max_run) noexcept(false) { fetchRuns(min_run, max_run, true, false); }
32 
33 void RunList::fetchNonEmptyGlobalRuns(int min_run, int max_run) noexcept(false) {
34  fetchRuns(min_run, max_run, false, true);
35 }
36 
37 void RunList::fetchRuns() noexcept(false) { fetchRuns(-1, -1); }
38 
39 void RunList::fetchRuns(int min_run, int max_run) noexcept(false) { fetchRuns(min_run, max_run, false, false); }
40 
41 void RunList::fetchRuns(int min_run, int max_run, bool withTriggers, bool withGlobalTriggers) noexcept(false) {
42  /*
43  withTriggers and withGlobalTriggers selects those non empty runs.
44  Possible combinations are
45 
46  withTriggers withGlobalTriggers select
47  ------------ ------------------ ------------------------------
48  false false all
49  false true only runs with global triggers
50  true false only runs with any trigger
51  true true only runs with global triggers
52  */
53  this->checkConnection();
54  int nruns = 0;
55 
56  m_runTag.setConnection(m_env, m_conn);
57  int tagID = m_runTag.fetchID();
58  cout << "tag id=" << tagID << endl;
59  if (!tagID) {
60  return;
61  }
62 
63  int my_min_run = min_run - 1;
64  int my_max_run = max_run + 1;
65  try {
66  Statement* stmt0 = m_conn->createStatement();
67  string sql =
68  "SELECT count(iov_id) FROM run_iov "
69  "WHERE tag_id = :tag_id ";
70  if (min_run > 0) {
71  // don't need to specify empty/non empty here. This is needed
72  // just to allocate the memory for the vector
73  sql += " and run_iov.run_num> :min_run and run_iov.run_num< :max_run ";
74  }
75  stmt0->setSQL(sql);
76  stmt0->setInt(1, tagID);
77  if (min_run > 0) {
78  stmt0->setInt(2, my_min_run);
79  stmt0->setInt(3, my_max_run);
80  }
81 
82  ResultSet* rset0 = stmt0->executeQuery();
83  if (rset0->next()) {
84  nruns = rset0->getInt(1);
85  }
86  m_conn->terminateStatement(stmt0);
87 
88  cout << "number of runs=" << nruns << endl;
89  m_vec_runiov.reserve(nruns);
90 
91  Statement* stmt = m_conn->createStatement();
92  sql =
93  "SELECT DISTINCT i.iov_id, tag_id, run_num, run_start, run_end, "
94  "db_timestamp FROM run_iov i ";
95  if ((withTriggers) || (withGlobalTriggers)) {
96  sql +=
97  "join cms_ecal_cond.run_dat d on d.iov_id = i.iov_id "
98  "left join CMS_WBM.RUNSUMMARY R on R.RUNNUMBER = i.RUN_NUM ";
99  }
100  sql += "WHERE tag_id = :tag_id ";
101  if (min_run > 0) {
102  sql += "and i.run_num> :min_run and i.run_num< :max_run ";
103  }
104  if (withGlobalTriggers) {
105  sql += "and R.TRIGGERS > 0 ";
106  } else if (withTriggers) {
107  sql += "and ((R.TRIGGERS > 0) or (num_events > 0)) ";
108  }
109  sql += " order by run_num ";
110  stmt->setSQL(sql);
111  stmt->setInt(1, tagID);
112  if (min_run > 0) {
113  stmt->setInt(2, my_min_run);
114  stmt->setInt(3, my_max_run);
115  }
116 
117  DateHandler dh(m_env, m_conn);
118  Tm runStart;
119  Tm runEnd;
120  Tm dbtime;
121 
122  ResultSet* rset = stmt->executeQuery();
123  int i = 0;
124  while ((i < nruns) && (rset->next())) {
125  int iovID = rset->getInt(1);
126  // int tagID = rset->getInt(2);
127  int runNum = rset->getInt(3);
128  Date startDate = rset->getDate(4);
129  Date endDate = rset->getDate(5);
130  Date dbDate = rset->getDate(6);
131 
132  runStart = dh.dateToTm(startDate);
133  runEnd = dh.dateToTm(endDate);
134  dbtime = dh.dateToTm(dbDate);
135 
136  RunIOV r;
137  r.setRunNumber(runNum);
138  r.setRunStart(runStart);
139  r.setRunEnd(runEnd);
140  r.setDBInsertionTime(dbtime);
141  r.setRunTag(m_runTag);
142  r.setID(iovID);
143  m_vec_runiov.push_back(r);
144 
145  i++;
146  }
147  m_vec_runiov.resize(i);
148  m_conn->terminateStatement(stmt);
149  } catch (SQLException& e) {
150  throw(std::runtime_error(std::string("RunList::fetchRuns: ") + e.getMessage()));
151  }
152 }
153 
154 void RunList::fetchLastNRuns(int max_run, int n_runs) noexcept(false) {
155  // fetch the last n_runs that come just before max_run (including max_run)
156 
157  this->checkConnection();
158 
159  m_runTag.setConnection(m_env, m_conn);
160  int tagID = m_runTag.fetchID();
161  cout << "tag id=" << tagID << endl;
162  if (!tagID) {
163  return;
164  }
165 
166  int my_max_run = max_run + 1;
167  try {
168  int nruns = n_runs + 1;
169  m_vec_runiov.reserve(nruns);
170 
171  Statement* stmt = m_conn->createStatement();
172  stmt->setSQL(
173  "select iov_id, tag_id, run_num, run_start, run_end, DB_TIMESTAMP from "
174  " (SELECT * from RUN_IOV "
175  " WHERE tag_id = :tag_id "
176  " and run_num< :max_run "
177  " order by run_num DESC ) where rownum< :n_runs ORDER BY run_num ASC ");
178  stmt->setInt(1, tagID);
179  stmt->setInt(2, my_max_run);
180  stmt->setInt(3, nruns);
181 
182  DateHandler dh(m_env, m_conn);
183  Tm runStart;
184  Tm runEnd;
185  Tm dbtime;
186 
187  ResultSet* rset = stmt->executeQuery();
188  int i = 0;
189  while (i < n_runs) {
190  rset->next();
191  int iovID = rset->getInt(1);
192  // int tagID = rset->getInt(2);
193  int runNum = rset->getInt(3);
194  Date startDate = rset->getDate(4);
195  Date endDate = rset->getDate(5);
196  Date dbDate = rset->getDate(6);
197 
198  runStart = dh.dateToTm(startDate);
199  runEnd = dh.dateToTm(endDate);
200  dbtime = dh.dateToTm(dbDate);
201 
202  RunIOV r;
203  r.setRunNumber(runNum);
204  r.setRunStart(runStart);
205  r.setRunEnd(runEnd);
206  r.setDBInsertionTime(dbtime);
207  r.setRunTag(m_runTag);
208  r.setID(iovID);
209  m_vec_runiov.push_back(r);
210 
211  i++;
212  }
213 
214  m_conn->terminateStatement(stmt);
215  } catch (SQLException& e) {
216  throw(std::runtime_error(std::string("RunList::fetchLastNRuns: ") + e.getMessage()));
217  }
218 }
219 
220 void RunList::fetchRunsByLocation(int min_run, int max_run, const LocationDef& locDef) noexcept(false) {
221  this->checkConnection();
222  int nruns = 0;
223 
224  int my_min_run = min_run - 1;
225  int my_max_run = max_run + 1;
226  try {
227  Statement* stmt0 = m_conn->createStatement();
228  stmt0->setSQL(
229  "SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
230  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 "
231  " and r.run_num> :2 and r.run_num< :3 ");
232  stmt0->setString(1, locDef.getLocation());
233  stmt0->setInt(2, my_min_run);
234  stmt0->setInt(3, my_max_run);
235 
236  ResultSet* rset0 = stmt0->executeQuery();
237  if (rset0->next()) {
238  nruns = rset0->getInt(1);
239  }
240  m_conn->terminateStatement(stmt0);
241 
242  cout << "number of runs=" << nruns << endl;
243 
244  m_vec_runiov.reserve(nruns);
245 
246  Statement* stmt = m_conn->createStatement();
247  stmt->setSQL(
248  "SELECT r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
249  " t.gen_tag, rt.RUN_TYPE "
250  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
251  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
252  " AND l.LOCATION= :1 "
253  " and r.run_num> :2 and r.run_num< :3 "
254  " order by run_num ");
255  stmt->setString(1, locDef.getLocation());
256  stmt->setInt(2, my_min_run);
257  stmt->setInt(3, my_max_run);
258 
259  DateHandler dh(m_env, m_conn);
260  Tm runStart;
261  Tm runEnd;
262  Tm dbtime;
263 
264  ResultSet* rset = stmt->executeQuery();
265  int i = 0;
266  while (i < nruns) {
267  rset->next();
268  int iovID = rset->getInt(1);
269  // int tagID = rset->getInt(2);
270  int runNum = rset->getInt(3);
271  Date startDate = rset->getDate(4);
272  Date endDate = rset->getDate(5);
273  Date dbDate = rset->getDate(6);
274 
275  runStart = dh.dateToTm(startDate);
276  runEnd = dh.dateToTm(endDate);
277  dbtime = dh.dateToTm(dbDate);
278 
279  RunTag atag;
280  atag.setLocationDef(locDef);
281  atag.setGeneralTag(rset->getString(7));
282  RunTypeDef rundef;
283  rundef.setRunType(rset->getString(8));
284  atag.setRunTypeDef(rundef);
285 
286  RunIOV r;
287  r.setRunNumber(runNum);
288  r.setRunStart(runStart);
289  r.setRunEnd(runEnd);
290  r.setDBInsertionTime(dbtime);
291  r.setRunTag(atag);
292  r.setID(iovID);
293  m_vec_runiov.push_back(r);
294 
295  i++;
296  }
297 
298  m_conn->terminateStatement(stmt);
299 
300  } catch (SQLException& e) {
301  throw(std::runtime_error(std::string("RunList::fetchRunsByLocation: ") + e.getMessage()));
302  }
303 }
304 
305 void RunList::fetchGlobalRunsByLocation(int min_run, int max_run, const LocationDef& locDef) noexcept(false) {
306  this->checkConnection();
307  int nruns = 0;
308 
309  int my_min_run = min_run - 1;
310  int my_max_run = max_run + 1;
311  try {
312  Statement* stmt0 = m_conn->createStatement();
313  stmt0->setSQL(
314  "SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
315  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 "
316  " and t.gen_tag='GLOBAL' "
317  " and r.run_num> :2 and r.run_num< :3 ");
318  stmt0->setString(1, locDef.getLocation());
319  stmt0->setInt(2, my_min_run);
320  stmt0->setInt(3, my_max_run);
321 
322  ResultSet* rset0 = stmt0->executeQuery();
323  if (rset0->next()) {
324  nruns = rset0->getInt(1);
325  }
326  m_conn->terminateStatement(stmt0);
327 
328  cout << "number of runs=" << nruns << endl;
329 
330  m_vec_runiov.reserve(nruns);
331 
332  Statement* stmt = m_conn->createStatement();
333  stmt->setSQL(
334  "SELECT r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
335  " t.gen_tag, rt.RUN_TYPE "
336  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
337  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
338  " AND l.LOCATION= :1 "
339  " and t.gen_tag='GLOBAL' "
340  " and r.run_num> :2 and r.run_num< :3 "
341  " order by run_num ");
342  stmt->setString(1, locDef.getLocation());
343  stmt->setInt(2, my_min_run);
344  stmt->setInt(3, my_max_run);
345 
346  DateHandler dh(m_env, m_conn);
347  Tm runStart;
348  Tm runEnd;
349  Tm dbtime;
350 
351  ResultSet* rset = stmt->executeQuery();
352  int i = 0;
353  while (i < nruns) {
354  rset->next();
355  int iovID = rset->getInt(1);
356  // int tagID = rset->getInt(2);
357  int runNum = rset->getInt(3);
358  Date startDate = rset->getDate(4);
359  Date endDate = rset->getDate(5);
360  Date dbDate = rset->getDate(6);
361 
362  runStart = dh.dateToTm(startDate);
363  runEnd = dh.dateToTm(endDate);
364  dbtime = dh.dateToTm(dbDate);
365 
366  RunTag atag;
367  atag.setLocationDef(locDef);
368  atag.setGeneralTag(rset->getString(7));
369  RunTypeDef rundef;
370  rundef.setRunType(rset->getString(8));
371  atag.setRunTypeDef(rundef);
372 
373  RunIOV r;
374  r.setRunNumber(runNum);
375  r.setRunStart(runStart);
376  r.setRunEnd(runEnd);
377  r.setDBInsertionTime(dbtime);
378  r.setRunTag(atag);
379  r.setID(iovID);
380  m_vec_runiov.push_back(r);
381 
382  i++;
383  }
384 
385  m_conn->terminateStatement(stmt);
386 
387  } catch (SQLException& e) {
388  throw(std::runtime_error(std::string("RunList::fetchRunsByLocation: ") + e.getMessage()));
389  }
390 }
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
void fetchRuns() noexcept(false)
Definition: RunList.cc:37
void setRunTag(const RunTag &tag)
Definition: RunList.cc:17
Definition: RunTag.h:13
void fetchRunsByLocation(int min_run, int max_run, const LocationDef &locDef) noexcept(false)
Definition: RunList.cc:220
void fetchNonEmptyRuns() noexcept(false)
Definition: RunList.cc:27
~RunList() override
Definition: RunList.cc:15
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:21
void fetchLastNRuns(int max_run, int n_runs) noexcept(false)
Definition: RunList.cc:154
RunTag getRunTag() const
Definition: RunList.cc:23
dh
Definition: cuy.py:354
Definition: RunIOV.h:13
RunList()
Definition: RunList.cc:13
void fetchNonEmptyGlobalRuns() noexcept(false)
Definition: RunList.cc:29
Definition: Tm.h:13
void fetchGlobalRunsByLocation(int min_run, int max_run, const LocationDef &locDef) noexcept(false)
Definition: RunList.cc:305