Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:19:36

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