Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RunDBodbc.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RunDBodbc.cc
1 #include "RunDBodbc.h"
2 
3 #include <odbc++/connection.h>
4 #include <odbc++/drivermanager.h>
5 #include <odbc++/statement.h> // for Statement
6 #include <odbc++/types.h> // for SQLException
7 
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
10 #include <odbc++/resultset.h>
11 #pragma GCC diagnostic pop
12 
13 #include <cstdlib>
14 #include <fstream>
15 #include <iomanip>
16 #include <iostream>
17 #include <sstream>
18 
19 //#define VERBOSE
20 
21 void RunDBodbc::identify() const
22 {
23  std::cout << "DB Name: " << dbname << std::endl;
24  std::cout << "DB Owner: " << dbowner << std::endl;
25  std::cout << "DB Pwd: " << dbpasswd << std::endl;
26  return;
27 }
28 
30 RunDBodbc::RunType(const int runnoinput) const
31 {
32  std::string runtype = "UNKNOWN";
33  odbc::Connection *con = nullptr;
34  odbc::Statement *query = nullptr;
35  odbc::ResultSet *rs = nullptr;
36  std::ostringstream cmd;
37  int runno = 221;
38  if (runnoinput == 221)
39  {
40  runno = runnoinput;
41  }
42  try
43  {
45  }
46  catch (odbc::SQLException &e)
47  {
48  std::cout << __PRETTY_FUNCTION__
49  << " Exception caught during DriverManager::getConnection" << std::endl;
50  std::cout << "Message: " << e.getMessage() << std::endl;
51  goto noopen;
52  }
53 
54  query = con->createStatement();
55  cmd << "SELECT runtype,runstate,eventsinrun,brunixtime,erunixtime FROM RUN WHERE RUNNUMBER = "
56  << runno;
57  if (verbosity > 0)
58  {
59  std::cout << "command: " << cmd.str() << std::endl;
60  }
61  try
62  {
63  rs = query->executeQuery(cmd.str());
64  }
65  catch (odbc::SQLException &e)
66  {
67  std::cout << "Exception caught" << std::endl;
68  std::cout << "Message: " << e.getMessage() << std::endl;
69  }
70  if (rs && rs->next())
71  {
72  runtype = rs->getString("runtype");
73  if (runtype == "PHYSICS")
74  {
75  std::string runstate = rs->getString("runstate");
76  unsigned int brunixtime = rs->getInt("brunixtime");
77  unsigned int erunixtime = rs->getInt("erunixtime");
78  if (erunixtime - brunixtime < 300 && runstate == "ENDED") // 5 min limit
79  {
80  runtype = "PREJECTED";
81  }
82  else
83  {
84  int eventsinrun = rs->getInt("eventsinrun");
85  if (eventsinrun <= 100 && runstate != "ENDED")
86  {
87  if (verbosity > 0)
88  {
89  std::cout << "Run not ended and eventsinrun : " << eventsinrun << std::endl;
90  }
91  cmd.str("");
92  cmd << "SELECT sum(scalerupdatescaled) FROM trigger WHERE RUNNUMBER = "
93  << runno;
94 
95  odbc::ResultSet *rs1 = nullptr;
96 
97  odbc::Statement *query1 = con->createStatement();
98  try
99  {
100  rs1 = query1->executeQuery(cmd.str());
101  }
102  catch (odbc::SQLException &e)
103  {
104  std::cout << "Exception caught" << std::endl;
105  std::cout << "Message: " << e.getMessage() << std::endl;
106  }
107  if (rs1 && rs1->next())
108  {
109  eventsinrun = rs1->getLong(1);
110  }
111  if (verbosity > 0)
112  {
113  std::cout << "Run not ended and eventsinrun < 500000, sum of scaled triggers: "
114  << eventsinrun << std::endl;
115  }
116  }
117  if (eventsinrun <= 100)
118  {
119  runtype = "PREJECTED";
120  }
121  }
122  }
123  }
124 noopen:
125  delete con;
126 
127  // try to get this info from the beginrun sql command saved in $ONLINE_LOG/runinfo
128  if (runtype == "UNKNOWN")
129  {
130  if (verbosity > 0)
131  {
132  std::cout << "Run unknown in DB trying from file" << std::endl;
133  }
134  // runtype = RunTypeFromFile(runno, runtype);
135  }
136 
137  if (verbosity > 0)
138  {
139  std::cout << "Run Type is " << runtype << std::endl;
140  }
141 
142  return runtype;
143 }
144 
145 int RunDBodbc::GetRunNumbers(std::set<int> &result, const std::string &type, const int nruns, const int lastrunexclusive) const
146 {
147  odbc::Connection *con = nullptr;
148  odbc::Statement *query = nullptr;
149  odbc::ResultSet *rs = nullptr;
150  std::ostringstream cmd;
151 
152  try
153  {
154  con = odbc::DriverManager::getConnection(dbname.c_str(), dbowner.c_str(), dbpasswd.c_str());
155  }
156  catch (odbc::SQLException &e)
157  {
158  std::cout << __PRETTY_FUNCTION__
159  << " Exception caught during DriverManager::getConnection" << std::endl;
160  std::cout << "Message: " << e.getMessage() << std::endl;
161  return -1;
162  }
163 
164  query = con->createStatement();
165  cmd << "SELECT runnumber FROM RUN WHERE eventsinrun > 100000 and runtype = '"
166  << type << "' and runnumber < "
167  << lastrunexclusive
168  << " order by runnumber desc limit " << nruns;
169  if (verbosity > 0)
170  {
171  std::cout << "command: " << cmd.str() << std::endl;
172  }
173  try
174  {
175  rs = query->executeQuery(cmd.str());
176  }
177  catch (odbc::SQLException &e)
178  {
179  std::cout << "Exception caught" << std::endl;
180  std::cout << "Message: " << e.getMessage() << std::endl;
181  delete con;
182  }
183  while (rs->next())
184  {
185  int runnumber = rs->getInt("runnumber");
186  result.insert(runnumber);
187  if (verbosity > 0)
188  {
189  std::cout << "Choosing " << runnumber << std::endl;
190  }
191  }
192  delete rs;
193  delete con;
194  return 0;
195 }