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