Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-04-05 08:10:22

0001 #include <fun4all/DBInterface.h>
0002 #include <odbc++/resultset.h>
0003 #include <odbc++/statement.h>
0004 
0005 // https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
0006 string exec(const char *cmd)
0007 {
0008   array<char, 128> buffer;
0009   string result;
0010   unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
0011   if (!pipe)
0012   {
0013     throw runtime_error("popen() failed!");
0014   }
0015   while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
0016   {
0017     result += buffer.data();
0018   }
0019   return result;
0020 }
0021 
0022 float getNEventsFromDAQ(const int& runNumber)
0023 {
0024   float query = numeric_limits<float>::quiet_NaN();
0025   odbc::Statement *statement = DBInterface::instance()->getStatement("daq");
0026   string sqlcmd = "SELECT eventsinrun FROM run WHERE runnumber = " + to_string(runNumber);
0027   unique_ptr<odbc::ResultSet> resultSet(statement->executeQuery(sqlcmd));
0028   if (resultSet && resultSet->next())
0029   {
0030     query = resultSet->getFloat("eventsinrun");
0031   }
0032   return query;
0033 }
0034 
0035 string getRunTime(const int& runNumber, const string& begin_end = "brtimestamp")
0036 {
0037   string query = "dummy value";
0038   odbc::Statement *statement = DBInterface::instance()->getStatement("daq");
0039   string sqlcmd = "SELECT " + begin_end + " FROM run WHERE runnumber = " + to_string(runNumber);
0040   unique_ptr<odbc::ResultSet> resultSet(statement->executeQuery(sqlcmd));
0041   if (resultSet && resultSet->next())
0042   {
0043     query = resultSet->getString(begin_end);
0044   }
0045   return query;
0046 }
0047 
0048 float getMBD(const int& runNumber)
0049 {
0050   float query = numeric_limits<float>::quiet_NaN();
0051   odbc::Statement *statement = DBInterface::instance()->getStatement("daq");
0052   string sqlcmd = "SELECT avg_value FROM run_timeseries_db_summary WHERE run_number=" + to_string(runNumber) + " AND time_series_name = 'sphenix_cad_sisScaler_Hz'and labels->>'short_name' = 'MBD N\u00b7S'";
0053   unique_ptr<odbc::ResultSet> resultSet(statement->executeQuery(sqlcmd));
0054   if (resultSet && resultSet->next())
0055   {
0056     query = resultSet->getFloat("avg_value");
0057   }
0058   return query;
0059 }
0060 
0061 
0062 template <typename T>
0063 string to_string_with_precision(const T a_value, const int n = 0)
0064 {
0065     ostringstream out;
0066     out.precision(n);
0067     out << fixed << a_value;
0068     return out.str();
0069 }
0070 
0071 void buildLumiTable()
0072 {
0073   cout.setf(ios::fixed);
0074 
0075   int sumTotalEvents = 0;
0076   int sumProceEvents = 0;
0077   float sumLumi = 0;
0078 
0079   float pp_cross_section_200GeV = 20e6;// Roughly 20 mb for MBD, written here in nb (https://arxiv.org/abs/0704.3599)
0080 
0081  int runList[] = {79507, 79509, 79510, 79511, 79512, 79513, 79514, 79515, 79516, 79524, 79525, 79526, 79528, 79529, 79530, 79563, 79565, 79567, 79568, 79570, 79571, 79572, 79593, 79594, 79595, 79596, 79597, 79598, 79599, 79600, 79614, 79617, 79627, 79652, 79653, 79656, 79660, 79707, 79708, 79709, 79711, 79712};
0082 
0083   cout << "  \\begin{tabular}{p{2.5cm}p{2.5cm}p{3.0cm}p{2.5cm}p{2.4cm}}" << endl;
0084   cout << "    \\toprule[1pt]" << endl;
0085   cout << "    Run number  & No. recorded triggers [$\\times 10^{6}$] & No. processed triggers [$\\times 10^{6}$] & Analyzed $\\int \\mathcal{L} dt$ [$\\rm nb^{-1}$] & Avg. MBD rate [\\khz] \\\\" << endl;
0086   cout << "    \\midrule[0.2pt]" << endl;
0087 
0088   for (const int run : runList)
0089   {
0090     int nTotalEvents = getNEventsFromDAQ(run);
0091 
0092     float rate = getMBD(run)/1e3; //Average rate in kHz
0093 
0094     string catFileLists = "cat /sphenix/user/aopatton/Feb3Analysis/data/fileLists/file_run" + to_string(run) + "_*.list | wc -l";
0095 
0096     string nProceEvents = exec(catFileLists.c_str());
0097     nProceEvents.erase(remove(nProceEvents.begin(), nProceEvents.end(), '\n'), nProceEvents.end());
0098 
0099     int i_nProceEvents = stoi(nProceEvents)*1000;
0100 
0101     float lumi = i_nProceEvents/(pp_cross_section_200GeV);// Triggered lumi in inv nb
0102 
0103     cout << "    " << run << " & " << to_string_with_precision(nTotalEvents/1e6, 1) << " & " << to_string_with_precision(i_nProceEvents/1e6, 1) << " & " << to_string_with_precision(lumi, 3) << " & " + to_string_with_precision(rate, 1) + "\\\\" << endl;
0104 
0105     sumTotalEvents += nTotalEvents;
0106     sumProceEvents += i_nProceEvents;
0107     sumLumi += lumi;
0108   }
0109 
0110   cout << "    \\bottomrule[1pt]" << endl;
0111   cout << "    Total & " << to_string_with_precision(sumTotalEvents/1e6, 1) <<  " & " << to_string_with_precision(sumProceEvents/1e6, 1) << " & " << to_string_with_precision(sumLumi, 1) << " & - \\\\" << endl;
0112   cout << "    \\bottomrule[1pt]" << endl;
0113   cout << "  \\end{tabular}" << endl;
0114 }