Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:15:27

0001 #include <sstream>
0002 #include <fstream>
0003 #include <string>
0004 #include <TSQLServer.h>
0005 #include <TSQLResult.h>
0006 #include <TSQLRow.h>
0007 
0008 void get_mbdlivecounts(int runnumber, long &live_counts)
0009 {
0010 
0011   TSQLServer *db = TSQLServer::Connect("pgsql://sphnxdaqdbreplica:5432/daq","phnxro","");
0012 
0013   if (db)
0014   {
0015     printf("Server info: %s\n", db->ServerInfo());
0016   }
0017   else
0018   {
0019     printf("bad\n");
0020   }
0021 
0022   TSQLRow *row;
0023   TSQLResult *res;
0024   TString cmd = "";
0025   char sql[1000];
0026 
0027   sprintf(sql, "select live from gl1_scalers where runnumber = %d and index = 10;", runnumber);
0028   printf("%s \n" , sql);
0029 
0030   res = db->Query(sql);
0031   if (res->GetRowCount() == 1 && res->GetFieldCount() == 1) {
0032     live_counts = stol(res->Next()->GetField(0));
0033     std::cout << "live_counts " << live_counts << std::endl; 
0034   } else {
0035     std::cout << "live_counts unexpected output: rows " << res->GetRowCount() << " fields " << res->GetFieldCount() << std::endl;
0036   }
0037 
0038   delete res;
0039   delete db;
0040 }
0041 
0042 void get_runlength(int runnumber, double &run_length)
0043 {
0044 
0045   TSQLServer *db = TSQLServer::Connect("pgsql://sphnxdaqdbreplica:5432/daq","phnxro","");
0046 
0047   if (db)
0048   {
0049     printf("Server info: %s\n", db->ServerInfo());
0050   }
0051   else
0052   {
0053     printf("bad\n");
0054   }
0055 
0056   TSQLRow *row;
0057   TSQLResult *res;
0058   TString cmd = "";
0059   char sql[1000];
0060 
0061   sprintf(sql, "select (extract(epoch from ertimestamp) - extract(epoch from brtimestamp)) as run_length from run where runnumber = %d;", runnumber);
0062   printf("%s \n" , sql);
0063 
0064   res = db->Query(sql);
0065   if (res->GetRowCount() == 1 && res->GetFieldCount() == 1) {
0066     run_length = stod(res->Next()->GetField(0));
0067     std::cout << "run_length " << run_length << std::endl; 
0068   } else {
0069     std::cout << "run_length unexpected output: rows " << res->GetRowCount() << " fields " << res->GetFieldCount() << std::endl;
0070   }
0071 
0072   delete res;
0073   delete db;
0074 }
0075 
0076 void export_mbdliverate(const std::string &input_file, const std::string &output_file)
0077 {
0078   std::ifstream infile(input_file);
0079   std::ofstream outfile(output_file);
0080 
0081   if (!infile.is_open())
0082   {
0083     std::cerr << "Failed to open input file.\n";
0084     return;
0085   }
0086 
0087   if (!outfile.is_open())
0088   {
0089     std::cerr << "Failed to open output file.\n";
0090     return;
0091   }
0092 
0093   // Write CSV header
0094   outfile << "runnumber live_rate\n";
0095 
0096   int runnumber;
0097   while (infile >> runnumber)
0098   {
0099     long live_counts;
0100     double run_length;
0101     get_mbdlivecounts(runnumber, live_counts);
0102     get_runlength(runnumber, run_length);
0103     outfile << runnumber << " " << std::fixed << live_counts/run_length << "\n";
0104 
0105   }
0106 
0107   infile.close();
0108   outfile.close();
0109 }