Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:21

0001 #include "MvtxRawDefs.h"
0002 
0003 #include <odbc++/connection.h>  // odbc::Connection
0004 #pragma GCC diagnostic push
0005 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0006 #include <odbc++/drivermanager.h>
0007 #pragma GCC diagnostic pop
0008 #include <odbc++/resultset.h>
0009 #include <odbc++/statement.h>  // for Statement
0010 #include <odbc++/types.h>
0011 
0012 #include <TRandom3.h>
0013 
0014 #include <chrono>
0015 #include <iostream>
0016 #include <limits>
0017 #include <memory>
0018 #include <stdexcept>
0019 #include <string>
0020 #include <thread>
0021 
0022 uint8_t MvtxRawDefs::getStaveIndex(const uint8_t& lyrId, const uint8_t& stvId)
0023 {
0024   return firstStaveIndex[lyrId] + stvId;
0025 };
0026 
0027 std::pair<uint8_t, uint8_t> const& MvtxRawDefs::get_flx_endpoint(const uint8_t& lyrId, const uint8_t& stvId)
0028 {
0029   return stave_felix_map.at(getStaveIndex(lyrId, stvId));
0030 };
0031 
0032 MvtxRawDefs::linkId_t MvtxRawDefs::decode_feeid(const uint16_t feeid)
0033 {
0034   linkId_t ret = {};
0035   // the static_cast< uint16_t> is needed to because the result of (feeid >> 12U)
0036   // is promoted to int which then triggers a (correct) clang-tidy warning that
0037   // a bitwise operation is performed on a signed integer
0038   ret.layer = static_cast<uint16_t>(feeid >> 12U) & 0x7U;
0039   ret.stave = feeid & 0x1FU;
0040   ret.gbtid = static_cast<uint16_t>(feeid >> 8U) & 0x3U;
0041   return ret;
0042 };
0043 
0044 // anonymous namespace
0045 namespace
0046 {
0047   float getStrobeLengthFromOCDB(const int& runNumber)
0048   {
0049     constexpr uint8_t MAX_NUM_RETRIES = 10;
0050     std::unique_ptr<odbc::Connection> m_OdbcConnection(nullptr);
0051     int num_tries = 0;
0052     TRandom3 rnd = TRandom3();
0053     do
0054     {
0055       try
0056       {
0057         std::unique_ptr<odbc::Connection> temp(odbc::DriverManager::getConnection("mvtx_read", "", ""));
0058         m_OdbcConnection = std::move(temp);
0059       }
0060       catch (odbc::SQLException& e)
0061       {
0062         std::cout << " Exception caught during DriverManager::getConnection" << std::endl;
0063         std::cout << "Message: " << e.getMessage() << std::endl;
0064       }
0065       ++num_tries;
0066       int wait = 20 + (rnd.Uniform() * 300);
0067       if (!m_OdbcConnection)
0068       {
0069         std::this_thread::sleep_for(std::chrono::seconds(wait));  // sleep 30 seconds before retry
0070       }
0071     } while ((!m_OdbcConnection) && (num_tries < MAX_NUM_RETRIES));
0072 
0073     if (!m_OdbcConnection)
0074     {
0075       return std::numeric_limits<float>::quiet_NaN();
0076     }
0077 
0078     std::string sqlcmd = "SELECT strobe FROM mvtx_strobe_offline WHERE runnumber = " + std::to_string(runNumber) + ";";
0079 
0080     std::unique_ptr<odbc::Statement> statement(m_OdbcConnection->createStatement());
0081     std::unique_ptr<odbc::ResultSet> resultSet(statement->executeQuery(sqlcmd));
0082 
0083     if (resultSet && resultSet->next())
0084     {
0085       float strobe = resultSet->getFloat("strobe");
0086       std::cout << "MVTX strobe read from ocdb: " << strobe << std::endl;
0087       return strobe;
0088     }
0089 
0090     return std::numeric_limits<float>::quiet_NaN();
0091   }
0092 
0093   float getStrobeLengthFromDAQ(const int& runNumber)
0094   {
0095     float strobeWidth = std::numeric_limits<float>::quiet_NaN();
0096 
0097     std::string executable_command = "psql -h sphnxdaqdbreplica daq --csv -c \"SELECT strobe FROM mvtx_strobe WHERE hostname = \'mvtx0\' AND runnumber = ";
0098     executable_command += std::to_string(runNumber);
0099     executable_command += ";\" | tail -n 1";
0100 
0101     std::array<char, 128> buffer = {};
0102     std::string result;
0103     std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(executable_command.c_str(), "r"), pclose);
0104     if (!pipe)
0105     {
0106       throw std::runtime_error("popen() failed!");
0107     }
0108     while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
0109     {
0110       result += buffer.data();
0111     }
0112     try
0113     {
0114       strobeWidth = stof(result);
0115     }
0116     catch (std::invalid_argument const& ex)
0117     {
0118       std::cout << "mvtx_utils::getStrobeLength() Run number " << runNumber << " has no strobe length in the DAQ database, returning NAN" << std::endl;
0119     }
0120 
0121     std::cout << "MVTX strobe read from daq db: " << strobeWidth << std::endl;
0122     return strobeWidth;
0123   }
0124 }  // namespace
0125 
0126 float MvtxRawDefs::getStrobeLength(const int& runNumber)
0127 {
0128   {
0129     float strobe_length = getStrobeLengthFromOCDB(runNumber);
0130     return (!std::isnan(strobe_length)) ? strobe_length : getStrobeLengthFromDAQ(runNumber);
0131   }
0132 }