File indexing completed on 2025-12-18 09:22:11
0001
0002
0003 #include <TSystem.h>
0004 #include <TString.h>
0005
0006 #include <ctime>
0007 #include <format>
0008 #include <fstream>
0009 #include <iostream>
0010 #include <limits>
0011 #include <cmath>
0012
0013
0014 bool psql_run(const TString& dbhost,
0015 const TString& dbname,
0016 const TString& sql_quoted,
0017 TString& out,
0018 const char* flags = "-X -q -t -A -w",
0019 int max_retries = 3,
0020 int backoff_ms = 400)
0021 {
0022 out.Clear();
0023
0024 for (int attempt = 1; attempt <= max_retries; ++attempt) {
0025 Long64_t now = static_cast<Long64_t>(time(nullptr));
0026 TString tmp = std::format("/tmp/getCaloTemp_{}_{}_{}.txt",
0027 gSystem->GetPid(), now, attempt).c_str();
0028
0029
0030 TString cmd = std::format("psql -h {} -d {} {} -c {} > {} 2>/dev/null",
0031 dbhost.Data(), dbname.Data(), flags, sql_quoted.Data(), tmp.Data());
0032
0033 int code = gSystem->Exec(cmd.Data());
0034 TString contents = gSystem->GetFromPipe(std::format("cat {}; rm -f {}", tmp.Data(), tmp.Data()).c_str());
0035 contents = contents.Strip(TString::kBoth, '\n');
0036
0037 if (code == 0 && contents.Length() > 0 && !contents.Contains("ERROR")) {
0038 out = contents;
0039 return true;
0040 }
0041
0042 if (attempt < max_retries) gSystem->Sleep(backoff_ms * attempt);
0043 }
0044 return false;
0045 }
0046
0047
0048 float fetch_hcal_temp_degC(int runnumber,
0049 const TString& detector = "HCALOUT",
0050 int max_retries = 3,
0051 int backoff_ms = 400)
0052 {
0053 const TString dbhost = "sphnxdaqdbreplica";
0054 const TString dbname = "daq";
0055
0056
0057
0058 TString runtime;
0059 {
0060 TString sql = std::format("\"SELECT brtimestamp FROM run WHERE runnumber={};\"", runnumber);
0061 if (!psql_run(dbhost, dbname, sql, runtime, "-X -q -t -A -w", max_retries, backoff_ms)) {
0062 std::cout << "Run " << runnumber << ": FAILED to fetch brtimestamp" << std::endl;
0063 return std::numeric_limits<float>::quiet_NaN();
0064 }
0065 runtime = runtime.Strip(TString::kBoth, '\n');
0066 }
0067
0068 int det = -1;
0069 if (detector.EqualTo("HCALOUT", TString::kIgnoreCase)) det = 0;
0070 else if (detector.EqualTo("HCALIN", TString::kIgnoreCase)) det = 1;
0071 else {
0072 return std::numeric_limits<float>::quiet_NaN();
0073 }
0074
0075 TString closest_time;
0076 {
0077 TString sql = std::format("\"SELECT time FROM hcal_heartbeat "
0078 "WHERE detector={} "
0079 "ORDER BY ABS(EXTRACT(epoch FROM time) - EXTRACT(epoch FROM '{}'::timestamp)) "
0080 "LIMIT 1;\"",
0081 det, runtime.Data());
0082 if (!psql_run(dbhost, dbname, sql, closest_time, "-X -q -t -A -w", max_retries, backoff_ms)) {
0083 std::cout << "Run " << runnumber << ": FAILED to fetch closest heartbeat time" << std::endl;
0084 return std::numeric_limits<float>::quiet_NaN();
0085 }
0086 closest_time = closest_time.Strip(TString::kBoth, '\n');
0087 }
0088
0089 TString avg_temp_str;
0090 {
0091 TString sql = std::format("\"SELECT AVG(temp) "
0092 "FROM hcal_heartbeat "
0093 "WHERE detector=%d AND time='{}' "
0094 " AND temp > -50 AND temp < 100;\"",
0095 det, closest_time.Data());
0096 if (!psql_run(dbhost, dbname, sql, avg_temp_str, "-X -q -t -A -w", max_retries, backoff_ms)) {
0097 return std::numeric_limits<float>::quiet_NaN();
0098 }
0099 avg_temp_str = avg_temp_str.Strip(TString::kBoth, '\n');
0100 }
0101
0102 float tdeg = std::numeric_limits<float>::quiet_NaN();
0103 try { tdeg = std::stof(std::string(avg_temp_str.Data())); }
0104 catch (...) {std::cout << "caught unknown exception from std::stof" << std::endl;}
0105
0106 return tdeg;
0107 }
0108
0109
0110
0111 void getCosmicTemps(const char* runlist_path = "cos_runs_pp_2024.txt",
0112 const char* detector = "HCALOUT")
0113 {
0114 std::ifstream fin(runlist_path);
0115 if (!fin) { std::cerr << "Could not open " << runlist_path << std::endl; return; }
0116 std::string line;
0117 while (std::getline(fin, line)) {
0118 if (line.empty() || line[0]=='#') continue;
0119 int run = 0; try { run = std::stoi(line); } catch (...) { continue; }
0120 (void)fetch_hcal_temp_degC(run, detector);
0121
0122 }
0123 }
0124