Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:56

0001 #include "RunDBodbc.h"
0002 
0003 #include <odbc++/connection.h>
0004 #include <odbc++/drivermanager.h>
0005 #include <odbc++/statement.h>  // for Statement
0006 #include <odbc++/types.h>      // for SQLException
0007 
0008 #pragma GCC diagnostic push
0009 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
0010 #include <odbc++/resultset.h>
0011 #pragma GCC diagnostic pop
0012 
0013 #include <cstdlib>
0014 #include <fstream>
0015 #include <iomanip>
0016 #include <iostream>
0017 #include <sstream>
0018 
0019 //#define VERBOSE
0020 
0021 void RunDBodbc::identify() const
0022 {
0023   std::cout << "DB Name: " << dbname << std::endl;
0024   std::cout << "DB Owner: " << dbowner << std::endl;
0025   std::cout << "DB Pwd: " << dbpasswd << std::endl;
0026   return;
0027 }
0028 
0029 std::string
0030 RunDBodbc::RunType(const int runno) const
0031 {
0032   std::string runtype = "UNKNOWN";
0033   odbc::Connection *con = nullptr;
0034   odbc::Statement *query = nullptr;
0035   odbc::ResultSet *rs = nullptr;
0036   std::ostringstream cmd;
0037   try
0038   {
0039     con = odbc::DriverManager::getConnection(dbname, dbowner, dbpasswd);
0040   }
0041   catch (odbc::SQLException &e)
0042   {
0043     std::cout << __PRETTY_FUNCTION__
0044               << " Exception caught during DriverManager::getConnection" << std::endl;
0045     std::cout << "Message: " << e.getMessage() << std::endl;
0046     goto noopen;
0047   }
0048 
0049   query = con->createStatement();
0050   cmd << "SELECT runtype FROM RUN WHERE RUNNUMBER = "
0051       << runno;
0052   if (verbosity > 0)
0053   {
0054     std::cout << "command: " << cmd.str() << std::endl;
0055   }
0056   try
0057   {
0058     rs = query->executeQuery(cmd.str());
0059   }
0060   catch (odbc::SQLException &e)
0061   {
0062     std::cout << "Exception caught" << std::endl;
0063     std::cout << "Message: " << e.getMessage() << std::endl;
0064   }
0065   if (rs && rs->next())
0066   {
0067     runtype = rs->getString("runtype");
0068     // if (runtype == "PHYSICS")
0069     // {
0070     //   std::string runstate = rs->getString("runstate");
0071     //   unsigned int brunixtime = rs->getInt("brunixtime");
0072     //   unsigned int erunixtime = rs->getInt("erunixtime");
0073     //   if (erunixtime - brunixtime < 300 && runstate == "ENDED")  // 5 min limit
0074     //   {
0075     //     runtype = "PREJECTED";
0076     //   }
0077     //   else
0078     //   {
0079     //     int eventsinrun = rs->getInt("eventsinrun");
0080     //     if (eventsinrun <= 100 && runstate != "ENDED")
0081     //     {
0082     //       if (verbosity > 0)
0083     //       {
0084     //         std::cout << "Run not ended and eventsinrun : " << eventsinrun << std::endl;
0085     //       }
0086     //       cmd.str("");
0087     //       cmd << "SELECT sum(scalerupdatescaled) FROM trigger WHERE RUNNUMBER = "
0088     //           << runno;
0089 
0090     //       odbc::ResultSet *rs1 = nullptr;
0091 
0092     //       odbc::Statement *query1 = con->createStatement();
0093     //       try
0094     //       {
0095     //         rs1 = query1->executeQuery(cmd.str());
0096     //       }
0097     //       catch (odbc::SQLException &e)
0098     //       {
0099     //         std::cout << "Exception caught" << std::endl;
0100     //         std::cout << "Message: " << e.getMessage() << std::endl;
0101     //       }
0102     //       if (rs1 && rs1->next())
0103     //       {
0104     //         eventsinrun = rs1->getLong(1);
0105     //       }
0106     //       if (verbosity > 0)
0107     //       {
0108     //         std::cout << "Run not ended and eventsinrun < 500000, sum of scaled triggers: "
0109     //                   << eventsinrun << std::endl;
0110     //       }
0111     //     }
0112     //     if (eventsinrun <= 100)
0113     //     {
0114     //       runtype = "PREJECTED";
0115     //     }
0116     //   }
0117     // }
0118   }
0119 noopen:
0120   delete con;
0121 
0122   // // try to get this info from the beginrun sql command saved in $ONLINE_LOG/runinfo
0123   // if (runtype == "UNKNOWN")
0124   // {
0125   //   if (verbosity > 0)
0126   //   {
0127   //     std::cout << "Run unknown in DB trying from file" << std::endl;
0128   //   }
0129   //   //    runtype = RunTypeFromFile(runno, runtype);
0130   // }
0131 
0132   if (verbosity > 0)
0133   {
0134     std::cout << "Run Type is " << runtype << std::endl;
0135   }
0136 
0137   return runtype;
0138 }
0139 
0140 int RunDBodbc::GetRunNumbers(std::set<int> &result, const std::string &type, const int nruns, const int lastrunexclusive) const
0141 {
0142   odbc::Connection *con = nullptr;
0143   odbc::Statement *query = nullptr;
0144   odbc::ResultSet *rs = nullptr;
0145   std::ostringstream cmd;
0146 
0147   try
0148   {
0149     con = odbc::DriverManager::getConnection(dbname.c_str(), dbowner.c_str(), dbpasswd.c_str());
0150   }
0151   catch (odbc::SQLException &e)
0152   {
0153     std::cout << __PRETTY_FUNCTION__
0154               << " Exception caught during DriverManager::getConnection" << std::endl;
0155     std::cout << "Message: " << e.getMessage() << std::endl;
0156     return -1;
0157   }
0158 
0159   query = con->createStatement();
0160   cmd << "SELECT runnumber FROM RUN WHERE eventsinrun > 100000 and runtype = '"
0161       << type << "' and runnumber < "
0162       << lastrunexclusive
0163       << " order by runnumber desc limit " << nruns;
0164   if (verbosity > 0)
0165   {
0166     std::cout << "command: " << cmd.str() << std::endl;
0167   }
0168   try
0169   {
0170     rs = query->executeQuery(cmd.str());
0171   }
0172   catch (odbc::SQLException &e)
0173   {
0174     std::cout << "Exception caught" << std::endl;
0175     std::cout << "Message: " << e.getMessage() << std::endl;
0176     delete con;
0177   }
0178   while (rs->next())
0179   {
0180     int runnumber = rs->getInt("runnumber");
0181     result.insert(runnumber);
0182     if (verbosity > 0)
0183     {
0184       std::cout << "Choosing " << runnumber << std::endl;
0185     }
0186   }
0187   delete rs;
0188   delete con;
0189   return 0;
0190 }
0191 
0192 int RunDBodbc::GetScaledowns(std::vector<int> &result, const int runno) const
0193 {
0194   odbc::Connection *con = nullptr;
0195   odbc::Statement *query = nullptr;
0196   odbc::ResultSet *rs = nullptr;
0197   std::ostringstream cmd;
0198 
0199   result.clear();   // clear result, in case it is not empty
0200 
0201   try
0202   {
0203     con = odbc::DriverManager::getConnection(dbname.c_str(), dbowner.c_str(), dbpasswd.c_str());
0204   }
0205   catch (odbc::SQLException &e)
0206   {
0207     std::cout << __PRETTY_FUNCTION__
0208               << " Exception caught during DriverManager::getConnection" << std::endl;
0209     std::cout << "Message: " << e.getMessage() << std::endl;
0210     return -1;
0211   }
0212 
0213   query = con->createStatement();
0214   cmd << "SELECT * FROM gl1_scaledown WHERE runnumber = " << runno;
0215   if (verbosity > 0)
0216   {
0217     std::cout << "command: " << cmd.str() << std::endl;
0218   }
0219   try
0220   {
0221     rs = query->executeQuery(cmd.str());
0222   }
0223   catch (odbc::SQLException &e)
0224   {
0225     std::cout << "Exception caught" << std::endl;
0226     std::cout << "Message: " << e.getMessage() << std::endl;
0227     delete con;
0228   }
0229 
0230   while (rs->next())
0231   {
0232     int runnumber = rs->getInt("runnumber");
0233     if ( runnumber != runno )
0234     {
0235       std::cout << "ODBC ERROR, runnumber is not equal to runno, " << runnumber << "\t" << runno << std::endl;
0236       continue;
0237     }
0238     char column_name[BUFSIZ];
0239     for (int itrig=0; itrig<64; itrig++)
0240     {
0241       sprintf(column_name,"scaledown%02d",itrig);
0242       int scaledown = rs->getInt(column_name);
0243       result.push_back( scaledown );
0244 
0245       if (verbosity > 0)
0246       {
0247           std::cout << column_name << "\t" << result[itrig] << std::endl;
0248       }
0249     }
0250   }
0251   delete rs;
0252   delete con;
0253   return 0;
0254 }