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_scaledowns(int runnumber, int scaledowns[])
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 
0023   TSQLRow *row;
0024   TSQLResult *res;
0025   TString cmd = "";
0026   char sql[1000];
0027 
0028 
0029   for (int is = 0; is < 64; is++)
0030   {
0031     sprintf(sql, "select scaledown%02d from gl1_scaledown where runnumber = %d;", is, runnumber);
0032     printf("%s \n" , sql);
0033 
0034     res = db->Query(sql);
0035 
0036     int nrows = res->GetRowCount();
0037 
0038     int nfields = res->GetFieldCount();
0039     for (int i = 0; i < nrows; i++) {
0040       row = res->Next();
0041       for (int j = 0; j < nfields; j++) {
0042         scaledowns[is] = stoi(row->GetField(j));
0043       }
0044       delete row;
0045     }
0046 
0047     delete res;
0048   }
0049   delete db;
0050 }
0051 
0052 void export_scaledowns(const std::string &input_file, const std::string &output_file)
0053 {
0054   std::ifstream infile(input_file);
0055   std::ofstream outfile(output_file);
0056 
0057   if (!infile.is_open())
0058   {
0059     std::cerr << "Failed to open input file.\n";
0060     return;
0061   }
0062 
0063   if (!outfile.is_open())
0064   {
0065     std::cerr << "Failed to open output file.\n";
0066     return;
0067   }
0068 
0069   // Write CSV header
0070   outfile << "runnumber";
0071   for (int i = 0; i < 64; ++i)
0072   {
0073     outfile << ",scaledown" << std::setw(2) << std::setfill('0') << i;
0074   }
0075   outfile << "\n";
0076 
0077   int runnumber;
0078   while (infile >> runnumber)
0079   {
0080     int scaledowns[64] = {0};
0081     get_scaledowns(runnumber, scaledowns);
0082 
0083     outfile << runnumber;
0084     for (int i = 0; i < 64; ++i)
0085     {
0086       outfile << "," << scaledowns[i];
0087     }
0088     outfile << "\n";
0089   }
0090 
0091   infile.close();
0092   outfile.close();
0093 }