From 8c4606fd37723e2c819134030015b3e8c06ad932 Mon Sep 17 00:00:00 2001 From: christos <christos@cern.ch> Date: Fri, 21 Jan 2022 02:50:01 +0100 Subject: [PATCH] LArTemperatureCorrectionTool: Coverity warnings and tidy --- .../Root/LArTemperatureCorrectionTool.cxx | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/PhysicsAnalysis/ElectronPhotonID/ElectronPhotonFourMomentumCorrection/Root/LArTemperatureCorrectionTool.cxx b/PhysicsAnalysis/ElectronPhotonID/ElectronPhotonFourMomentumCorrection/Root/LArTemperatureCorrectionTool.cxx index 34329a5f1e69..ac99a2118657 100644 --- a/PhysicsAnalysis/ElectronPhotonID/ElectronPhotonFourMomentumCorrection/Root/LArTemperatureCorrectionTool.cxx +++ b/PhysicsAnalysis/ElectronPhotonID/ElectronPhotonFourMomentumCorrection/Root/LArTemperatureCorrectionTool.cxx @@ -1,26 +1,34 @@ /* - Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration */ -#include <TParameter.h> - #include <ElectronPhotonFourMomentumCorrection/LArTemperatureCorrectionTool.h> +#include <TParameter.h> +#include <exception> template<typename T> -T get_value_parameter(TFile& f, const std::string& name) +T +get_value_parameter(TFile& f, const std::string& name) { TParameter<T>* p = dynamic_cast<TParameter<T>*>(f.Get(name.c_str())); - if (not p) { return 0; } + if (not p) { + return 0; + } return p->GetVal(); } LArTemperatureCorrectionTool::LArTemperatureCorrectionTool(const std::string& filename) -: asg::AsgMessaging("LArTemperatureCorrectionTool"), m_file(TFile::Open(filename.c_str())) + : asg::AsgMessaging("LArTemperatureCorrectionTool") + , m_file(TFile::Open(filename.c_str())) { - if (!m_file or m_file->IsZombie()) { ATH_MSG_ERROR("cannot open file"); } + if (!m_file or m_file->IsZombie()) { + throw std::runtime_error("LArTemperatureCorrectionTool: Cannot open file"); + } m_tree = dynamic_cast<TTree*>(m_file->Get("temperature")); - if (!m_tree) { ATH_MSG_FATAL("cannot find tree"); } + if (!m_tree) { + throw std::runtime_error("LArTemperatureCorrectionTool: Cannot find tree"); + } Int_t t_run = 0; m_tree->SetBranchAddress("run", &t_run); @@ -29,10 +37,6 @@ LArTemperatureCorrectionTool::LArTemperatureCorrectionTool(const std::string& fi m_tree->GetEntry(m_tree->GetEntries() - 1); m_last_run = t_run; - m_file->Get("base_temperature_barrel"); - TParameter<double>* p = static_cast<TParameter<double>*>(m_file->Get("base_temperature_barrel")); - p->GetVal(); - base_temperature.barrel = get_value_parameter<double>(*m_file, "base_temperature_barrel"); base_temperature.endcapA = get_value_parameter<double>(*m_file, "base_temperature_endcapA"); base_temperature.endcapC = get_value_parameter<double>(*m_file, "base_temperature_endcapC"); @@ -41,12 +45,17 @@ LArTemperatureCorrectionTool::LArTemperatureCorrectionTool(const std::string& fi sensitivity_temperature.endcapA = get_value_parameter<double>(*m_file, "sensitivity_temperature_endcapA"); sensitivity_temperature.endcapC = get_value_parameter<double>(*m_file, "sensitivity_temperature_endcapC"); - ATH_MSG_INFO("LArTemperatureCorrectionTool initialized for runs " << m_first_run << ".." << m_last_run); - ATH_MSG_INFO("base temperatures (barrel/endcapA/endcapC) = " << base_temperature.barrel << "/" << base_temperature.endcapA << "/" << base_temperature.endcapC); - ATH_MSG_INFO("sensitivity relE/K (barrel/endcapA/endcapC) = " << sensitivity_temperature.barrel << "/" << sensitivity_temperature.endcapA << "/" << sensitivity_temperature.endcapC); + ATH_MSG_INFO("LArTemperatureCorrectionTool initialized for runs " << m_first_run << ".." << m_last_run); + ATH_MSG_INFO("base temperatures (barrel/endcapA/endcapC) = " << base_temperature.barrel << "/" + << base_temperature.endcapA << "/" + << base_temperature.endcapC); + ATH_MSG_INFO("sensitivity relE/K (barrel/endcapA/endcapC) = " << sensitivity_temperature.barrel << "/" + << sensitivity_temperature.endcapA << "/" + << sensitivity_temperature.endcapC); } -LArTemperatureCorrectionTool::AllValues LArTemperatureCorrectionTool::search_correction(int run) const +LArTemperatureCorrectionTool::AllValues +LArTemperatureCorrectionTool::search_correction(int run) const { AllValues temp = search_temperature(run); temp.barrel = 1. - (temp.barrel - base_temperature.barrel) * sensitivity_temperature.barrel; @@ -55,7 +64,8 @@ LArTemperatureCorrectionTool::AllValues LArTemperatureCorrectionTool::search_cor return temp; } -LArTemperatureCorrectionTool::AllValues LArTemperatureCorrectionTool::search_temperature(int run) const +LArTemperatureCorrectionTool::AllValues +LArTemperatureCorrectionTool::search_temperature(int run) const { Float_t t_barrel = base_temperature.barrel; Float_t t_endcapA = base_temperature.endcapA; @@ -72,40 +82,46 @@ LArTemperatureCorrectionTool::AllValues LArTemperatureCorrectionTool::search_tem int mid = 0; while (low <= high) { - if (high - low < 50) { // prefer sequential scan + if (high - low < 50) { // prefer sequential scan for (int i = low; i <= high; ++i) { m_tree->GetEntry(i); - if (run == t_run) { return AllValues { t_barrel, t_endcapA, t_endcapC }; } + if (run == t_run) { + return AllValues{ t_barrel, t_endcapA, t_endcapC }; + } } break; } - mid = low + (high - low) / 2; // scared of overflow? + mid = low + (high - low) / 2; // scared of overflow? m_tree->GetEntry(mid); - if (run == t_run) { return AllValues { t_barrel, t_endcapA, t_endcapC }; } - else if (run < t_run) { high = mid - 1; } - else { low = mid + 1; } + if (run == t_run) { + return AllValues{ t_barrel, t_endcapA, t_endcapC }; + } else if (run < t_run) { + high = mid - 1; + } else { + low = mid + 1; + } } ATH_MSG_WARNING("run " << run << " not found - no temperature correction"); return base_temperature; } -LArTemperatureCorrectionTool::AllValues LArTemperatureCorrectionTool::get_corrections(int run) const +LArTemperatureCorrectionTool::AllValues +LArTemperatureCorrectionTool::get_corrections(int run) const { const auto it = m_cache.find(run); - if (it != m_cache.end()) { return it->second; } - else { + if (it != m_cache.end()) { + return it->second; + } else { AllValues corrections{}; if (run < m_first_run) { ATH_MSG_WARNING("run " << run << " is before the first run - using the first run"); corrections = search_correction(m_first_run); - } - else if (run > m_last_run) { + } else if (run > m_last_run) { ATH_MSG_WARNING("run " << run << " is after the last run - using the last run"); corrections = search_correction(m_last_run); - } - else { + } else { corrections = search_correction(run); } m_cache[run] = corrections; -- GitLab