File indexing completed on 2025-08-05 08:16:05
0001 #include "CaloTemp.h"
0002
0003 #include <boost/format.hpp>
0004
0005
0006 #include <calobase/TowerInfo.h>
0007 #include <calobase/TowerInfoContainer.h>
0008 #include <calobase/TowerInfoDefs.h>
0009
0010 #include <fun4all/Fun4AllHistoManager.h>
0011 #include <fun4all/Fun4AllReturnCodes.h>
0012
0013 #include <phool/getClass.h>
0014
0015 #include <TFile.h>
0016 #include <TH1.h>
0017 #include <TH2.h>
0018 #include <TTree.h>
0019 #include <TProfile2D.h>
0020 #include <TRandom3.h>
0021 #include <TString.h>
0022
0023 #include <Event/Event.h>
0024 #include <Event/packet.h>
0025 #include <cassert>
0026 #include <sstream>
0027 #include <string>
0028 #include <chrono>
0029 #include <iostream>
0030 #include <thread>
0031
0032 #include <ffaobjects/RunHeader.h>
0033
0034 #include <odbc++/connection.h>
0035 #pragma GCC diagnostic push
0036 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0037 #include <odbc++/drivermanager.h>
0038 #pragma GCC diagnostic pop
0039 #include <odbc++/resultset.h>
0040 #include <odbc++/statement.h> // for Statement
0041 #include <odbc++/types.h>
0042
0043 using namespace std;
0044
0045 CaloTemp::CaloTemp(const std::string& name, const std::string& filename)
0046 : SubsysReco(name)
0047 , detector("HCALIN")
0048 , outfilename(filename)
0049 , temphist(true)
0050 {
0051 runnumber = -9999;
0052 second = -9999;
0053 minute = -9999;
0054 hour = -9999;
0055 day = -9999;
0056 month = -9999;
0057 year = -9999;
0058 }
0059
0060 CaloTemp::~CaloTemp()
0061 {
0062 delete hm;
0063 }
0064
0065 int CaloTemp::Init(PHCompositeNode* )
0066 {
0067 hm = new Fun4AllHistoManager(Name());
0068
0069
0070 if (Verbosity() > 1) {
0071 std::cout << "Enter CaloTemp Init function" << std::endl;
0072 }
0073
0074 outfile = new TFile(outfilename.c_str(), "UPDATE");
0075 if (temphist) {
0076 if (detector == "CEMC") {
0077 h_calo_temp = new TProfile2D("h_cemc_sipm_temp", ";eta;phi", 96, 0, 96, 256, 0, 256);
0078 h_calo_temp2 = new TProfile2D("h_cemc_pa_temp", ";eta;phi", 96, 0, 96, 256, 0, 256);
0079 } else if (detector == "HCALIN") {
0080 h_calo_temp = new TProfile2D("h_ihcal_temp", ";eta;phi", 24, 0, 24, 64, 0, 64);
0081 } else if (detector == "HCALOUT") {
0082 h_calo_temp = new TProfile2D("h_ohcal_temp", ";eta;phi", 24, 0, 24, 64, 0, 64);
0083 } else {
0084 std::cout << "Unknown detector type " << detector << ". Options are CEMC, HCALIN and HCALOUT." << std::endl;
0085 return 1;
0086 }
0087 }
0088
0089 tree = new TTree("runtime", "");
0090 tree->Branch("runnumber", &runnumber, "runnumber/I");
0091 tree->Branch("year", &year, "year/I");
0092 tree->Branch("month", &month, "month/I");
0093 tree->Branch("day", &day, "day/I");
0094 tree->Branch("hour", &hour, "hour/I");
0095 tree->Branch("minute", &minute, "minute/I");
0096 tree->Branch("second", &second, "second/I");
0097
0098 return 0;
0099 }
0100
0101 int CaloTemp::InitRun(PHCompositeNode* topNode)
0102 {
0103 if (Verbosity() > 1) {
0104 std::cout << "Get run header" << std::endl;
0105 }
0106
0107 runheader = findNode::getClass<RunHeader>(topNode, "RunHeader");
0108 if (!runheader) {
0109 std::cout << "can't find runheader" << std::endl;
0110 return 1;
0111 }
0112 runnumber = runheader->get_RunNumber();
0113
0114 if (Verbosity()) {
0115 std::cout << "run number " << runnumber << std::endl;
0116 }
0117
0118 int result;
0119 if (temphist) {
0120 result = getTempHist();
0121 if (result == 1) {
0122 std::cout << "Unable to fill calorimeter temperature from database. Exiting now." << std::endl;
0123 return 1;
0124 }
0125 } else {
0126 result = getRunTime();
0127 if (result == 1) {
0128 std::cout << "Unable to connect to database. Exiting now." << std::endl;
0129 return 1;
0130 }
0131 }
0132
0133 if (Verbosity()) {
0134 std::cout << "runtime " << runtime << std::endl;
0135 }
0136
0137
0138 string output_runtime = runtime;
0139 for (char& c : output_runtime)
0140 {
0141 if (c == '-' || c == ':')
0142 {
0143 c = ' ';
0144 }
0145 }
0146
0147 std::istringstream iss(output_runtime);
0148 iss >> year >> month >> day >> hour >> minute >> second;
0149
0150 tree->Fill();
0151
0152 return 0;
0153 }
0154
0155 int CaloTemp::getRunTime() {
0156 odbc::Connection *m_OdbcConnection = nullptr;
0157 int icount = 0;
0158 TRandom3 rnd = TRandom3();
0159 do {
0160 try {
0161 m_OdbcConnection = odbc::DriverManager::getConnection("daq","","");
0162 } catch (odbc::SQLException &e) {
0163 std::cout << " Exception caught during DriverManager::getConnection" << std::endl;
0164 std::cout << "Message: " << e.getMessage() << std::endl;
0165 }
0166 icount++;
0167 int wait = (int) 20 + rnd.Uniform()*300;
0168 if (!m_OdbcConnection) { std::this_thread::sleep_for(std::chrono::seconds(wait)); }
0169 } while (!m_OdbcConnection && icount < 10);
0170 if (icount == 10) { return 1; }
0171
0172 std::string sql = "SELECT brtimestamp FROM run WHERE runnumber = " + std::to_string(runnumber) + ";";
0173
0174 if (Verbosity() > 1) {
0175 std::cout << sql << std::endl;
0176 }
0177 odbc::Statement* stmt = m_OdbcConnection->createStatement();
0178 odbc::ResultSet* resultSet = stmt->executeQuery(sql);
0179
0180 if (resultSet && resultSet->next()) {
0181 odbc::Timestamp brtimestamp = resultSet->getTimestamp("brtimestamp");
0182 runtime = brtimestamp.toString();
0183 }
0184
0185 std::cout << "Runtime for Run " << runnumber << ": " << runtime << std::endl;
0186
0187 delete resultSet;
0188 delete stmt;
0189 delete m_OdbcConnection;
0190 return 0;
0191
0192 }
0193
0194
0195 int CaloTemp::getTempHist() {
0196
0197 odbc::Connection *m_OdbcConnection = nullptr;
0198 int icount = 0;
0199 TRandom3 rnd = TRandom3();
0200 do {
0201 try {
0202 m_OdbcConnection = odbc::DriverManager::getConnection("daq","","");
0203 } catch (odbc::SQLException &e) {
0204 std::cout << " Exception caught during DriverManager::getConnection" << std::endl;
0205 std::cout << "Message: " << e.getMessage() << std::endl;
0206 }
0207 icount++;
0208 int wait = (int) 20 + rnd.Uniform()*300;
0209 if (!m_OdbcConnection) { std::this_thread::sleep_for(std::chrono::seconds(wait)); }
0210 } while (!m_OdbcConnection && icount < 10);
0211 if (icount == 10) { return 1; }
0212
0213 std::string sql = "SELECT brtimestamp FROM run WHERE runnumber = " + std::to_string(runnumber) + ";";
0214
0215 if (Verbosity() > 1) {
0216 std::cout << sql << std::endl;
0217 }
0218 odbc::Statement* stmt = m_OdbcConnection->createStatement();
0219 odbc::ResultSet* resultSet = stmt->executeQuery(sql);
0220
0221 if (resultSet && resultSet->next()) {
0222 odbc::Timestamp brtimestamp = resultSet->getTimestamp("brtimestamp");
0223 runtime = brtimestamp.toString();
0224 }
0225
0226 if (Verbosity() > 1) {
0227 std::cout << "runtime " << runtime << std::endl;
0228 }
0229
0230 delete resultSet;
0231 delete stmt;
0232
0233 int det = 0;
0234 std::string tablename;
0235 std::string tempstring = "temp";
0236 if (detector == "CEMC") {
0237 tablename = "emcal_heartbeat";
0238 tempstring = "temp_sipm as temp, temp_pa";
0239 } else if (detector == "HCALIN") {
0240 tablename = "hcal_heartbeat";
0241 det = 1;
0242 } else if (detector == "HCALOUT") {
0243 tablename = "hcal_heartbeat";
0244 det = 0;
0245 } else {
0246 std::cout << "Unknown detector type " << detector << ". Options are CEMC, HCALIN and HCALOUT." << std::endl;
0247 return 1;
0248 }
0249
0250
0251 sql = "SELECT time FROM " + tablename + " ORDER BY ABS(EXTRACT(epoch FROM time) - EXTRACT(epoch FROM '" + runtime + "'::timestamp)) LIMIT 1;";
0252 if (Verbosity() > 1) {
0253 std::cout << sql << std::endl;
0254 }
0255
0256 odbc::Statement* timeStmt = m_OdbcConnection->createStatement();
0257 odbc::ResultSet* timeResultSet = timeStmt->executeQuery(sql);
0258
0259 std::string closest_time;
0260 if (timeResultSet && timeResultSet->next()) {
0261 odbc::Timestamp timestamp = timeResultSet->getTimestamp("time");
0262 closest_time = timestamp.toString();
0263 }
0264
0265 if (Verbosity() > 1) {
0266 std::cout << "closest time " << closest_time << std::endl;
0267 }
0268
0269 delete timeResultSet;
0270 delete timeStmt;
0271
0272 odbc::Statement* tempStmt = m_OdbcConnection->createStatement();
0273 sql = "SELECT towerid, " + tempstring + " FROM " + tablename + " WHERE time = '" + closest_time + "'";
0274 if (detector == "HCALIN" || detector == "HCALOUT") {
0275 sql += " and detector = " + std::to_string(det);
0276 }
0277 sql += ";";
0278
0279 if (Verbosity() > 1) {
0280 std::cout << sql << std::endl;
0281 }
0282
0283 odbc::ResultSet* tempResultSet = tempStmt->executeQuery(sql);
0284 string calo_title = boost::str(boost::format("%s Temperature RunNumber %d RunTime %s DBTime %s") %detector %runnumber %runtime %closest_time);
0285 h_calo_temp->SetTitle(calo_title.c_str());
0286 if (detector == "CEMC") {
0287 calo_title = boost::str(boost::format("%s SIPM Temperature RunNumber %d RunTime %s DBTime %s") %detector %runnumber %runtime %closest_time);
0288 string calo_title2 = calo_title = boost::str(boost::format("%s PreAmp Temperature RunNumber %d RunTime %s DBTime %s") %detector %runnumber %runtime %closest_time);
0289 h_calo_temp->SetTitle(calo_title.c_str());
0290 h_calo_temp2->SetTitle(calo_title2.c_str());
0291 }
0292 if (tempResultSet) {
0293 while (tempResultSet->next()) {
0294 int towerid = tempResultSet->getInt("towerid");
0295 float temp = tempResultSet->getFloat("temp");
0296 int calo_key;
0297 if (detector == "CEMC") { calo_key = TowerInfoDefs::encode_emcal(towerid); }
0298 else { calo_key = TowerInfoDefs::encode_hcal(towerid); }
0299 int etabin = TowerInfoDefs::getCaloTowerEtaBin(calo_key);
0300 int phibin = TowerInfoDefs::getCaloTowerPhiBin(calo_key);
0301 h_calo_temp->Fill(etabin, phibin, temp);
0302 if (detector == "CEMC") {
0303 float temp_pa = tempResultSet->getFloat("temp_pa");
0304 h_calo_temp2->Fill(etabin, phibin, temp_pa);
0305 }
0306 }
0307 } else {
0308 std::cerr << "Error: tempResultSet is NULL." << std::endl;
0309 }
0310
0311 delete tempResultSet;
0312 delete tempStmt;
0313 delete m_OdbcConnection;
0314 return 0;
0315 }
0316
0317
0318 int CaloTemp::End(PHCompositeNode* )
0319 {
0320 outfile->cd();
0321 outfile->Write();
0322 outfile->Close();
0323 delete outfile;
0324 hm->dumpHistos(outfilename, "UPDATE");
0325 return 0;
0326 }