File indexing completed on 2025-08-06 08:17:32
0001 #include "CaloTowerStatus.h"
0002 #include "CaloTowerDefs.h"
0003
0004 #include <calobase/TowerInfo.h> // for TowerInfo
0005 #include <calobase/TowerInfoContainer.h>
0006 #include <calobase/TowerInfoContainerv1.h>
0007 #include <calobase/TowerInfoContainerv2.h>
0008 #include <calobase/TowerInfov1.h>
0009 #include <calobase/TowerInfov2.h>
0010
0011 #include <cdbobjects/CDBTTree.h> // for CDBTTree
0012
0013 #include <ffamodules/CDBInterface.h>
0014
0015 #include <ffaobjects/EventHeader.h>
0016
0017 #include <fun4all/Fun4AllReturnCodes.h>
0018 #include <fun4all/SubsysReco.h> // for SubsysReco
0019
0020 #include <phool/PHCompositeNode.h>
0021 #include <phool/PHIODataNode.h> // for PHIODataNode
0022 #include <phool/PHNode.h> // for PHNode
0023 #include <phool/PHNodeIterator.h> // for PHNodeIterator
0024 #include <phool/PHObject.h> // for PHObject
0025 #include <phool/getClass.h>
0026 #include <phool/phool.h>
0027 #include <phool/recoConsts.h>
0028
0029 #include <TSystem.h>
0030
0031 #include <cmath>
0032 #include <cstdlib> // for exit
0033 #include <exception> // for exception
0034 #include <iostream> // for operator<<, basic_ostream
0035 #include <stdexcept> // for runtime_error
0036
0037
0038 CaloTowerStatus::CaloTowerStatus(const std::string &name)
0039 : SubsysReco(name)
0040 {
0041 if (Verbosity() > 0)
0042 {
0043 std::cout << "CaloTowerStatus::CaloTowerStatus(const std::string &name) Calling ctor" << std::endl;
0044 }
0045 }
0046
0047
0048 CaloTowerStatus::~CaloTowerStatus()
0049 {
0050 if (Verbosity() > 0)
0051 {
0052 std::cout << "CaloTowerStatus::~CaloTowerStatus() Calling dtor" << std::endl;
0053 }
0054 delete m_cdbttree_chi2;
0055 delete m_cdbttree_time;
0056 delete m_cdbttree_hotMap;
0057 }
0058
0059
0060 int CaloTowerStatus::InitRun(PHCompositeNode *topNode)
0061 {
0062 PHNodeIterator nodeIter(topNode);
0063
0064 if (m_dettype == CaloTowerDefs::CEMC)
0065 {
0066 m_detector = "CEMC";
0067 }
0068 else if (m_dettype == CaloTowerDefs::HCALIN)
0069 {
0070 m_detector = "HCALIN";
0071 }
0072 else if (m_dettype == CaloTowerDefs::HCALOUT)
0073 {
0074 m_detector = "HCALOUT";
0075 }
0076 else if (m_dettype == CaloTowerDefs::ZDC)
0077 {
0078 m_detector = "ZDC";
0079 }
0080
0081 else if (m_dettype == CaloTowerDefs::SEPD)
0082 {
0083 m_detector = "SEPD";
0084 }
0085
0086 m_calibName_chi2 = m_detector + "_hotTowers_fracBadChi2";
0087 m_fieldname_chi2 = "fraction";
0088
0089 std::string calibdir = CDBInterface::instance()->getUrl(m_calibName_chi2);
0090 if (!calibdir.empty())
0091 {
0092 m_cdbttree_chi2 = new CDBTTree(calibdir);
0093 if (Verbosity() > 0)
0094 {
0095 std::cout << "CaloTowerStatus::InitRun Found " << m_calibName_chi2 << " Doing isHot for frac bad chi2" << std::endl;
0096 }
0097 }
0098 else
0099 {
0100 if (use_directURL_chi2)
0101 {
0102 calibdir = m_directURL_chi2;
0103 std::cout << "CaloTowerStatus::InitRun: Using default hotBadChi2" << calibdir << std::endl;
0104 m_cdbttree_chi2 = new CDBTTree(calibdir);
0105 }
0106 else
0107 {
0108 m_doHotChi2 = false;
0109 if (Verbosity() > 0)
0110 {
0111 std::cout << "CaloTowerStatus::InitRun No masking file for domain " << m_calibName_chi2 << " found, not doing isHot from isBadChi2" << std::endl;
0112 }
0113 }
0114 }
0115
0116 m_calibName_time = m_detector + "_meanTime";
0117 m_fieldname_time = "time";
0118
0119 calibdir = CDBInterface::instance()->getUrl(m_calibName_time);
0120 if (!calibdir.empty())
0121 {
0122 m_cdbttree_time = new CDBTTree(calibdir);
0123 if (Verbosity() > 0)
0124 {
0125 std::cout << "CaloTowerStatus::InitRun Found " << m_calibName_time << " not Doing isBadTime" << std::endl;
0126 }
0127 }
0128 else
0129 {
0130 if (use_directURL_time)
0131 {
0132 calibdir = m_directURL_time;
0133 std::cout << "CaloTowerStatus::InitRun: Using default time " << calibdir << std::endl;
0134 m_cdbttree_time = new CDBTTree(calibdir);
0135 }
0136 else
0137 {
0138 m_doTime = false;
0139 if (Verbosity() > 1)
0140 {
0141 std::cout << "CaloTowerStatus::InitRun no timing info, " << m_calibName_time << " not found, not doing isBadTime" << std::endl;
0142 }
0143 }
0144 }
0145
0146 m_calibName_hotMap = m_detector + "nome";
0147 if (m_dettype == CaloTowerDefs::CEMC)
0148 {
0149 m_calibName_hotMap = m_detector + "_BadTowerMap";
0150 }
0151 m_fieldname_hotMap = "status";
0152 m_fieldname_z_score = m_detector + "_sigma";
0153
0154 calibdir = CDBInterface::instance()->getUrl(m_calibName_hotMap);
0155 if (!calibdir.empty())
0156 {
0157 m_cdbttree_hotMap = new CDBTTree(calibdir);
0158 if (Verbosity() > 1)
0159 {
0160 std::cout << "CaloTowerStatus::Init " << m_detector << " hot map found " << m_calibName_hotMap << " Ddoing isHot" << std::endl;
0161 }
0162 }
0163 else
0164 {
0165 if (m_doAbortNoHotMap)
0166 {
0167 std::cout << "CaloTowerStatus::InitRun: No hot map.. exiting" << std::endl;
0168 gSystem->Exit(1);
0169 }
0170 if (use_directURL_hotMap)
0171 {
0172 calibdir = m_directURL_hotMap;
0173 std::cout << "CaloTowerStatus::InitRun: Using default map " << calibdir << std::endl;
0174 m_cdbttree_hotMap = new CDBTTree(calibdir);
0175 }
0176 else
0177 {
0178 m_doHotMap = false;
0179 if (Verbosity() > 1)
0180 {
0181 std::cout << "CaloTowerStatus::InitRun hot map info, " << m_calibName_hotMap << " not found, not doing isHot" << std::endl;
0182 }
0183 }
0184 }
0185
0186 if (Verbosity() > 0)
0187 {
0188 std::cout << "CaloTowerStatus::Init " << m_detector << " doing time status =" << std::boolalpha << m_doTime << " doing hotBadChi2=" << std::boolalpha << m_doHotChi2 << " doing hot map=" << std::boolalpha << m_doHotMap << std::endl;
0189 }
0190
0191 PHNodeIterator iter(topNode);
0192
0193
0194 PHCompositeNode *dstNode;
0195 dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
0196 if (!dstNode)
0197 {
0198 std::cout << Name() << "::" << m_detector << "::" << __PRETTY_FUNCTION__
0199 << "DST Node missing, doing nothing." << std::endl;
0200 exit(1);
0201 }
0202 try
0203 {
0204 CreateNodeTree(topNode);
0205 }
0206 catch (std::exception &e)
0207 {
0208 std::cout << e.what() << std::endl;
0209 return Fun4AllReturnCodes::ABORTRUN;
0210 }
0211 if (Verbosity() > 0)
0212 {
0213 topNode->print();
0214 }
0215 return Fun4AllReturnCodes::EVENT_OK;
0216 }
0217
0218
0219 int CaloTowerStatus::process_event(PHCompositeNode * )
0220 {
0221 unsigned int ntowers = m_raw_towers->size();
0222 float fraction_badChi2 = 0;
0223 float mean_time = 0;
0224 int hotMap_val = 0;
0225 float z_score = 0;
0226 for (unsigned int channel = 0; channel < ntowers; channel++)
0227 {
0228 unsigned int key = m_raw_towers->encode_key(channel);
0229
0230 m_raw_towers->get_tower_at_channel(channel)->set_isHot(false);
0231 m_raw_towers->get_tower_at_channel(channel)->set_isBadTime(false);
0232 m_raw_towers->get_tower_at_channel(channel)->set_isBadChi2(false);
0233
0234 if (m_doHotChi2)
0235 {
0236 fraction_badChi2 = m_cdbttree_chi2->GetFloatValue(key, m_fieldname_chi2);
0237 }
0238 if (m_doTime)
0239 {
0240 mean_time = m_cdbttree_time->GetFloatValue(key, m_fieldname_time);
0241 }
0242 if (m_doHotMap)
0243 {
0244 hotMap_val = m_cdbttree_hotMap->GetIntValue(key, m_fieldname_hotMap);
0245 if(!m_isSim)
0246 {
0247 z_score = m_cdbttree_hotMap->GetFloatValue(key, m_fieldname_z_score);
0248 }
0249 }
0250 float chi2 = m_raw_towers->get_tower_at_channel(channel)->get_chi2();
0251 float time = m_raw_towers->get_tower_at_channel(channel)->get_time_float();
0252 float adc = m_raw_towers->get_tower_at_channel(channel)->get_energy();
0253
0254 if (fraction_badChi2 > fraction_badChi2_threshold && m_doHotChi2)
0255 {
0256 m_raw_towers->get_tower_at_channel(channel)->set_isHot(true);
0257 }
0258 if (!m_raw_towers->get_tower_at_channel(channel)->get_isZS() && std::fabs(time - mean_time) > time_cut && m_doTime)
0259 {
0260 m_raw_towers->get_tower_at_channel(channel)->set_isBadTime(true);
0261 }
0262
0263 bool is_bad_tower_data = hotMap_val == 1 ||
0264 std::fabs(z_score) > z_score_threshold ||
0265 (hotMap_val == 3 && z_score >= -1 * z_score_threshold_default);
0266
0267 bool is_bad_tower_sim = hotMap_val != 0;
0268
0269 bool is_bad_tower = (!m_isSim && is_bad_tower_data) || (m_isSim && is_bad_tower_sim);
0270
0271 if (is_bad_tower && m_doHotMap)
0272 {
0273 m_raw_towers->get_tower_at_channel(channel)->set_isHot(true);
0274 }
0275 if (chi2 > std::min(std::max(badChi2_treshold_const, adc * adc * badChi2_treshold_quadratic),badChi2_treshold_max))
0276 {
0277 m_raw_towers->get_tower_at_channel(channel)->set_isBadChi2(true);
0278 }
0279 }
0280 return Fun4AllReturnCodes::EVENT_OK;
0281 }
0282
0283 void CaloTowerStatus::CreateNodeTree(PHCompositeNode *topNode)
0284 {
0285 std::string RawTowerNodeName = m_inputNodePrefix + m_detector;
0286 m_raw_towers = findNode::getClass<TowerInfoContainer>(topNode, RawTowerNodeName);
0287 if (!m_raw_towers)
0288 {
0289 std::cout << Name() << "::" << m_detector.c_str() << "::" << __PRETTY_FUNCTION__
0290 << " " << RawTowerNodeName << " Node missing, doing bail out!"
0291 << std::endl;
0292 throw std::runtime_error(
0293 "Failed to find " + RawTowerNodeName + " node in CaloTowerStatus::CreateNodes");
0294 }
0295
0296 return;
0297 }