CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 = NULL;
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 
41  throw(std::runtime_error)
42 {
43  fetchRuns(-1, -1, true, false);
44 }
45 
47  throw(std::runtime_error)
48 {
49  fetchRuns(-1, -1, false, true);
50 }
51 
52 void RunList::fetchNonEmptyRuns(int min_run, int max_run)
53  throw(std::runtime_error)
54 {
55  fetchRuns(min_run, max_run, true, false);
56 }
57 
58 void RunList::fetchNonEmptyGlobalRuns(int min_run, int max_run)
59  throw(std::runtime_error)
60 {
61  fetchRuns(min_run, max_run, false, true);
62 }
63 
65  throw(std::runtime_error)
66 {
67  fetchRuns(-1, -1);
68 }
69 
70 void RunList::fetchRuns(int min_run, int max_run)
71  throw(std::runtime_error)
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  throw(std::runtime_error)
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_RUNINFO.RUNSESSION_PARAMETER G on "
135  "(i.run_num = G.RUNNUMBER and G.NAME = 'CMS.TRG:NumTriggers') ";
136  }
137  sql += "WHERE tag_id = :tag_id ";
138  if (min_run > 0) {
139  sql += "and i.run_num> :min_run and i.run_num< :max_run ";
140  }
141  if (withGlobalTriggers) {
142  sql += "and G.STRING_VALUE != '0' ";
143  } else if (withTriggers) {
144  sql += "and (G.STRING_VALUE != '0' or num_events > 0) ";
145  }
146  sql += " order by run_num ";
147  stmt->setSQL(sql);
148  stmt->setInt(1, tagID);
149  if (min_run > 0) {
150  stmt->setInt(2, my_min_run);
151  stmt->setInt(3, my_max_run);
152  }
153 
154  DateHandler dh(m_env, m_conn);
155  Tm runStart;
156  Tm runEnd;
157  Tm dbtime;
158 
159  ResultSet* rset = stmt->executeQuery();
160  int i=0;
161  while ((i<nruns) && (rset->next())) {
162  int iovID = rset->getInt(1);
163  // int tagID = rset->getInt(2);
164  int runNum = rset->getInt(3);
165  Date startDate = rset->getDate(4);
166  Date endDate = rset->getDate(5);
167  Date dbDate = rset->getDate(6);
168 
169  runStart = dh.dateToTm( startDate );
170  runEnd = dh.dateToTm( endDate );
171  dbtime = dh.dateToTm( dbDate );
172 
173  RunIOV r ;
174  r.setRunNumber(runNum);
175  r.setRunStart(runStart);
176  r.setRunEnd(runEnd);
177  r.setDBInsertionTime(dbtime);
178  r.setRunTag(m_runTag);
179  r.setID(iovID);
180  m_vec_runiov.push_back(r);
181 
182  i++;
183  }
184  m_vec_runiov.resize(i);
185  m_conn->terminateStatement(stmt);
186  } catch (SQLException &e) {
187  throw(std::runtime_error("RunList::fetchRuns: "+e.getMessage()));
188  }
189 }
190 
191 void RunList::fetchLastNRuns( int max_run, int n_runs )
192  throw(std::runtime_error)
193 {
194 
195  // fetch the last n_runs that come just before max_run (including max_run)
196 
197  this->checkConnection();
198 
199 
200  m_runTag.setConnection(m_env, m_conn);
201  int tagID = m_runTag.fetchID();
202  cout <<"tag id="<< tagID << endl;
203  if (!tagID) {
204  return ;
205  }
206 
207  int my_max_run=max_run+1;
208  try {
209 
210  int nruns=n_runs+1;
211  m_vec_runiov.reserve(nruns);
212 
213  Statement* stmt = m_conn->createStatement();
214  stmt->setSQL("select iov_id, tag_id, run_num, run_start, run_end, DB_TIMESTAMP from "
215  " (SELECT * from RUN_IOV "
216  " WHERE tag_id = :tag_id "
217  " and run_num< :max_run "
218  " order by run_num DESC ) where rownum< :n_runs ORDER BY run_num ASC " );
219  stmt->setInt(1, tagID);
220  stmt->setInt(2, my_max_run);
221  stmt->setInt(3, nruns);
222 
223  DateHandler dh(m_env, m_conn);
224  Tm runStart;
225  Tm runEnd;
226  Tm dbtime;
227 
228  ResultSet* rset = stmt->executeQuery();
229  int i=0;
230  while (i<n_runs) {
231  rset->next();
232  int iovID = rset->getInt(1);
233  // int tagID = rset->getInt(2);
234  int runNum = rset->getInt(3);
235  Date startDate = rset->getDate(4);
236  Date endDate = rset->getDate(5);
237  Date dbDate = rset->getDate(6);
238 
239  runStart = dh.dateToTm( startDate );
240  runEnd = dh.dateToTm( endDate );
241  dbtime = dh.dateToTm( dbDate );
242 
243  RunIOV r ;
244  r.setRunNumber(runNum);
245  r.setRunStart(runStart);
246  r.setRunEnd(runEnd);
247  r.setDBInsertionTime(dbtime);
248  r.setRunTag(m_runTag);
249  r.setID(iovID);
250  m_vec_runiov.push_back(r);
251 
252  i++;
253  }
254 
255  m_conn->terminateStatement(stmt);
256  } catch (SQLException &e) {
257  throw(std::runtime_error("RunList::fetchLastNRuns: "+e.getMessage()));
258  }
259 
260 
261 }
262 
263 
264 
265 void RunList::fetchRunsByLocation (int min_run, int max_run, const LocationDef& locDef )
266  throw(std::runtime_error)
267 {
268 
269  this->checkConnection();
270  int nruns=0;
271 
272  int my_min_run=min_run-1;
273  int my_max_run=max_run+1;
274  try {
275  Statement* stmt0 = m_conn->createStatement();
276  stmt0->setSQL("SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
277  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 "
278  " and r.run_num> :2 and r.run_num< :3 ");
279  stmt0->setString(1,locDef.getLocation() );
280  stmt0->setInt(2, my_min_run);
281  stmt0->setInt(3, my_max_run);
282 
283  ResultSet* rset0 = stmt0->executeQuery();
284  if (rset0->next()) {
285  nruns = rset0->getInt(1);
286  }
287  m_conn->terminateStatement(stmt0);
288 
289  cout <<"number of runs="<< nruns << endl;
290 
291  m_vec_runiov.reserve(nruns);
292 
293  Statement* stmt = m_conn->createStatement();
294  stmt->setSQL("SELECT r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
295  " t.gen_tag, rt.RUN_TYPE "
296  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
297  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
298  " AND l.LOCATION= :1 "
299  " and r.run_num> :2 and r.run_num< :3 "
300  " order by run_num " );
301  stmt->setString(1,locDef.getLocation() );
302  stmt->setInt(2, my_min_run);
303  stmt->setInt(3, my_max_run);
304 
305  DateHandler dh(m_env, m_conn);
306  Tm runStart;
307  Tm runEnd;
308  Tm dbtime;
309 
310  ResultSet* rset = stmt->executeQuery();
311  int i=0;
312  while (i<nruns) {
313  rset->next();
314  int iovID = rset->getInt(1);
315  // int tagID = rset->getInt(2);
316  int runNum = rset->getInt(3);
317  Date startDate = rset->getDate(4);
318  Date endDate = rset->getDate(5);
319  Date dbDate = rset->getDate(6);
320 
321  runStart = dh.dateToTm( startDate );
322  runEnd = dh.dateToTm( endDate );
323  dbtime = dh.dateToTm( dbDate );
324 
325  RunTag atag;
326  atag.setLocationDef(locDef);
327  atag.setGeneralTag(rset->getString(7));
328  RunTypeDef rundef;
329  rundef.setRunType(rset->getString(8));
330  atag.setRunTypeDef(rundef);
331 
332  RunIOV r ;
333  r.setRunNumber(runNum);
334  r.setRunStart(runStart);
335  r.setRunEnd(runEnd);
336  r.setDBInsertionTime(dbtime);
337  r.setRunTag(atag);
338  r.setID(iovID);
339  m_vec_runiov.push_back(r);
340 
341  i++;
342  }
343 
344 
345  m_conn->terminateStatement(stmt);
346 
347  } catch (SQLException &e) {
348  throw(std::runtime_error("RunList::fetchRunsByLocation: "+e.getMessage()));
349  }
350 
351 
352 }
353 
354 void RunList::fetchGlobalRunsByLocation (int min_run, int max_run, const LocationDef& locDef )
355  throw(std::runtime_error)
356 {
357 
358  this->checkConnection();
359  int nruns=0;
360 
361  int my_min_run=min_run-1;
362  int my_max_run=max_run+1;
363  try {
364  Statement* stmt0 = m_conn->createStatement();
365  stmt0->setSQL("SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
366  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 "
367  " and t.gen_tag='GLOBAL' "
368  " and r.run_num> :2 and r.run_num< :3 ");
369  stmt0->setString(1,locDef.getLocation() );
370  stmt0->setInt(2, my_min_run);
371  stmt0->setInt(3, my_max_run);
372 
373  ResultSet* rset0 = stmt0->executeQuery();
374  if (rset0->next()) {
375  nruns = rset0->getInt(1);
376  }
377  m_conn->terminateStatement(stmt0);
378 
379  cout <<"number of runs="<< nruns << endl;
380 
381  m_vec_runiov.reserve(nruns);
382 
383  Statement* stmt = m_conn->createStatement();
384  stmt->setSQL("SELECT r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
385  " t.gen_tag, rt.RUN_TYPE "
386  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
387  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
388  " AND l.LOCATION= :1 "
389  " and t.gen_tag='GLOBAL' "
390  " and r.run_num> :2 and r.run_num< :3 "
391  " order by run_num " );
392  stmt->setString(1,locDef.getLocation() );
393  stmt->setInt(2, my_min_run);
394  stmt->setInt(3, my_max_run);
395 
396  DateHandler dh(m_env, m_conn);
397  Tm runStart;
398  Tm runEnd;
399  Tm dbtime;
400 
401  ResultSet* rset = stmt->executeQuery();
402  int i=0;
403  while (i<nruns) {
404  rset->next();
405  int iovID = rset->getInt(1);
406  // int tagID = rset->getInt(2);
407  int runNum = rset->getInt(3);
408  Date startDate = rset->getDate(4);
409  Date endDate = rset->getDate(5);
410  Date dbDate = rset->getDate(6);
411 
412  runStart = dh.dateToTm( startDate );
413  runEnd = dh.dateToTm( endDate );
414  dbtime = dh.dateToTm( dbDate );
415 
416  RunTag atag;
417  atag.setLocationDef(locDef);
418  atag.setGeneralTag(rset->getString(7));
419  RunTypeDef rundef;
420  rundef.setRunType(rset->getString(8));
421  atag.setRunTypeDef(rundef);
422 
423  RunIOV r ;
424  r.setRunNumber(runNum);
425  r.setRunStart(runStart);
426  r.setRunEnd(runEnd);
427  r.setDBInsertionTime(dbtime);
428  r.setRunTag(atag);
429  r.setID(iovID);
430  m_vec_runiov.push_back(r);
431 
432  i++;
433  }
434 
435 
436  m_conn->terminateStatement(stmt);
437 
438  } catch (SQLException &e) {
439  throw(std::runtime_error("RunList::fetchRunsByLocation: "+e.getMessage()));
440  }
441 
442 
443 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:70
int i
Definition: DBlmapReader.cc:9
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
void setRunStart(const Tm &start)
Definition: RunIOV.cc:52
void setRunTag(const RunTag &tag)
Definition: RunList.cc:22
Definition: RunTag.h:13
void fetchLastNRuns(int max_run, int n_runs)
Definition: RunList.cc:191
RunTag getRunTag() const
Definition: RunList.cc:30
#define NULL
Definition: scimark2.h:8
void setDBInsertionTime(const Tm &dbtime)
Definition: RunIOV.h:31
void fetchRuns()
Definition: RunList.cc:64
void setRunTag(const RunTag &tag)
Definition: RunIOV.cc:86
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
return((rh^lh)&mask)
void setRunNumber(run_t run)
Definition: RunIOV.cc:29
~RunList()
Definition: RunList.cc:18
void fetchNonEmptyRuns()
Definition: RunList.cc:40
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void fetchRunsByLocation(int min_run, int max_run, const LocationDef &locDef)
Definition: RunList.cc:265
void fetchGlobalRunsByLocation(int min_run, int max_run, const LocationDef &locDef)
Definition: RunList.cc:354
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
void setRunEnd(const Tm &end)
Definition: RunIOV.cc:69
tuple cout
Definition: gather_cfg.py:121
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
Definition: RunIOV.h:13
RunList()
Definition: RunList.cc:13
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:31
Definition: Tm.h:13
void fetchNonEmptyGlobalRuns()
Definition: RunList.cc:46
void setID(int id)
Definition: RunIOV.cc:37
tuple dh
Definition: cuy.py:353