Skip to content

Commit

Permalink
Merge pull request #42199 from yuanchao/jreobj
Browse files Browse the repository at this point in the history
Fixing crash when accessing JetResolutionObject in CondDB
  • Loading branch information
cmsbuild authored Jul 8, 2023
2 parents 209422e + 3097d89 commit 239d50b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CondFormats/JetMETObjects/interface/JetResolutionObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ namespace JME {
static const bimap<Binning, std::string> binning_to_string;

std::vector<float> createVector(const std::vector<Binning>& binning) const;
std::vector<float> createVector(const std::vector<std::string>& binname) const;

private:
value_type m_values;
Expand Down Expand Up @@ -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<std::string>& getParametersName() const { return m_parameters_name; }

Expand Down
36 changes: 30 additions & 6 deletions CondFormats/JetMETObjects/src/JetResolutionObject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace JME {

std::vector<float> JetParameters::createVector(const std::vector<Binning>& binning) const {
std::vector<float> values;

for (const auto& bin : binning) {
const auto& it = m_values.find(bin);
if (it == m_values.cend()) {
Expand All @@ -130,6 +131,27 @@ namespace JME {
return values;
}

std::vector<float> JetParameters::createVector(const std::vector<std::string>& binname) const {
std::vector<float> 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<std::string> tokens = getTokens(definition);

Expand Down Expand Up @@ -367,7 +389,7 @@ namespace JME {
return nullptr;

// Create vector of bins value. Throw if some values are missing
std::vector<float> bins = bins_parameters.createVector(m_definition.getBins());
std::vector<float> 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;
Expand All @@ -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++;
}
Expand All @@ -397,7 +420,7 @@ namespace JME {
return 1;

#ifndef STANDALONE
const auto* formula = m_definition.getFormula();
reco::FormulaEvaluator formula = reco::FormulaEvaluator(m_definition.getFormulaString());
#else
// Set parameters
auto const* pFormula = m_definition.getFormula();
Expand All @@ -406,20 +429,21 @@ namespace JME {
auto formula = *pFormula;
#endif
// Create vector of variables value. Throw if some values are missing
std::vector<float> variables = variables_parameters.createVector(m_definition.getVariables());
std::vector<float> 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<float>& parameters = record.getParametersValues();

#ifndef STANDALONE
//ArrayAdaptor only takes doubles
std::vector<double> 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]);
Expand Down

0 comments on commit 239d50b

Please sign in to comment.