From 4b6323e1964fb7f4ff82dd8a30e2d151a563d147 Mon Sep 17 00:00:00 2001 From: Yuan CHAO Date: Thu, 6 Jul 2023 11:40:36 +0200 Subject: [PATCH 1/2] Fixing crash when accessing JetResolutionObject in CondDB --- .../interface/JetResolutionObject.h | 3 +- .../JetMETObjects/src/JetResolutionObject.cc | 39 ++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CondFormats/JetMETObjects/interface/JetResolutionObject.h b/CondFormats/JetMETObjects/interface/JetResolutionObject.h index 62de225939cf3..de77e831d88dd 100644 --- a/CondFormats/JetMETObjects/interface/JetResolutionObject.h +++ b/CondFormats/JetMETObjects/interface/JetResolutionObject.h @@ -108,6 +108,7 @@ namespace JME { static const bimap binning_to_string; std::vector createVector(const std::vector& binning) const; + std::vector createVector(const std::vector& binname) const; private: value_type m_values; @@ -155,7 +156,7 @@ namespace JME { std::string getVariableName(size_t variable) const { return m_variables_name[variable]; } - size_t nVariables() const { return m_variables.size(); } + size_t nVariables() const { return m_variables_name.size(); } const std::vector& getParametersName() const { return m_parameters_name; } diff --git a/CondFormats/JetMETObjects/src/JetResolutionObject.cc b/CondFormats/JetMETObjects/src/JetResolutionObject.cc index e3e85ab96960e..785ce8f440df5 100644 --- a/CondFormats/JetMETObjects/src/JetResolutionObject.cc +++ b/CondFormats/JetMETObjects/src/JetResolutionObject.cc @@ -115,6 +115,7 @@ namespace JME { std::vector JetParameters::createVector(const std::vector& binning) const { std::vector values; + for (const auto& bin : binning) { const auto& it = m_values.find(bin); if (it == m_values.cend()) { @@ -130,6 +131,27 @@ namespace JME { return values; } + std::vector JetParameters::createVector(const std::vector& binname) const { + std::vector values; + + for (const auto& name : binname) { + Binning bi = binning_to_string.right.find(name)->second; + + const auto& it = m_values.find(bi); + if (it == m_values.cend()) { + edm::LogPrint("JPM") << "Bin name " << name << " not found!"; + throwException(edm::errors::NotFound, + "JER parametrisation depends on '" + JetParameters::binning_to_string.left.at(bi) + + "' but no value for this parameter has been specified. Please call the appropriate 'set' " + "function of the JME::JetParameters object"); + } + + values.push_back(it->second); + } + + return values; + } + JetResolutionObject::Definition::Definition(const std::string& definition) { std::vector tokens = getTokens(definition); @@ -367,7 +389,7 @@ namespace JME { return nullptr; // Create vector of bins value. Throw if some values are missing - std::vector bins = bins_parameters.createVector(m_definition.getBins()); + std::vector bins = bins_parameters.createVector(m_definition.getBinsName()); // Iterate over all records, and find the one for which all bins are valid const Record* good_record = nullptr; @@ -376,8 +398,9 @@ namespace JME { size_t valid_bins = 0; size_t current_bin = 0; for (const auto& bin : record.getBinsRange()) { - if (bin.is_inside(bins[current_bin])) + if (bin.is_inside(bins[current_bin])) { valid_bins++; + } current_bin++; } @@ -397,7 +420,10 @@ namespace JME { return 1; #ifndef STANDALONE - const auto* formula = m_definition.getFormula(); + reco::FormulaEvaluator* Formula = new reco::FormulaEvaluator(m_definition.getFormulaString()); + if (!Formula) + return 1; + auto formula = *Formula; #else // Set parameters auto const* pFormula = m_definition.getFormula(); @@ -406,20 +432,21 @@ namespace JME { auto formula = *pFormula; #endif // Create vector of variables value. Throw if some values are missing - std::vector variables = variables_parameters.createVector(m_definition.getVariables()); + std::vector variables = variables_parameters.createVector(m_definition.getVariablesName()); double variables_[4] = {0}; for (size_t index = 0; index < m_definition.nVariables(); index++) { variables_[index] = clip(variables[index], record.getVariablesRange()[index].min, record.getVariablesRange()[index].max); } + const std::vector& parameters = record.getParametersValues(); #ifndef STANDALONE //ArrayAdaptor only takes doubles std::vector parametersD(parameters.begin(), parameters.end()); - return formula->evaluate(reco::formula::ArrayAdaptor(variables_, m_definition.nVariables()), - reco::formula::ArrayAdaptor(parametersD.data(), parametersD.size())); + return formula.evaluate(reco::formula::ArrayAdaptor(variables_, m_definition.nVariables()), + reco::formula::ArrayAdaptor(parametersD.data(), parametersD.size())); #else for (size_t index = 0; index < parameters.size(); index++) { formula.SetParameter(index, parameters[index]); From 3097d89b65e35140553ce40f3900a51721febaf9 Mon Sep 17 00:00:00 2001 From: Yuan CHAO Date: Fri, 7 Jul 2023 13:25:02 +0200 Subject: [PATCH 2/2] eliminate 'new' clause --- CondFormats/JetMETObjects/src/JetResolutionObject.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CondFormats/JetMETObjects/src/JetResolutionObject.cc b/CondFormats/JetMETObjects/src/JetResolutionObject.cc index 785ce8f440df5..11d422df4f0b2 100644 --- a/CondFormats/JetMETObjects/src/JetResolutionObject.cc +++ b/CondFormats/JetMETObjects/src/JetResolutionObject.cc @@ -420,10 +420,7 @@ namespace JME { return 1; #ifndef STANDALONE - reco::FormulaEvaluator* Formula = new reco::FormulaEvaluator(m_definition.getFormulaString()); - if (!Formula) - return 1; - auto formula = *Formula; + reco::FormulaEvaluator formula = reco::FormulaEvaluator(m_definition.getFormulaString()); #else // Set parameters auto const* pFormula = m_definition.getFormula();