27 size_t first = line.find(
'{');
28 size_t last = line.find(
'}');
30 if (first != std::string::npos && last != std::string::npos && first < last)
31 return std::string(line, first + 1, last - first - 1);
38 throw edm::Exception(static_cast<edm::errors::ErrorCodes>(code), message);
40 std::stringstream
error;
41 error << message <<
" Error code: " << code;
42 throw std::runtime_error(error.str());
48 {Binning::JetEta,
"JetEta"},
49 {Binning::JetAbsEta,
"JetAbsEta"},
50 {Binning::JetE,
"JetE"},
51 {Binning::JetArea,
"JetArea"},
53 {Binning::Rho,
"Rho"},
58 JetParameters::JetParameters(std::initializer_list<typename value_type::value_type>
init) {
59 for (
auto&
i : init) {
60 set(
i.first,
i.second);
65 m_values[Binning::JetPt] =
pt;
70 m_values[Binning::JetEta] =
eta;
71 m_values[Binning::JetAbsEta] = fabs(eta);
76 m_values[Binning::JetE] =
e;
81 m_values[Binning::JetArea] =
area;
96 m_values[Binning::Rho] = rho;
101 m_values.emplace(bin, value);
104 if (bin == Binning::JetEta) {
105 m_values.emplace(Binning::JetAbsEta, fabs(value));
112 set(value.first, value.second);
116 std::vector<float> JetParameters::createVector(
const std::vector<Binning>&
binning)
const {
117 std::vector<float>
values;
118 for (
const auto&
bin : binning) {
119 const auto& it = m_values.find(
bin);
120 if (it == m_values.cend()) {
122 "JER parametrisation depends on '" + JetParameters::binning_to_string.left.at(
bin) +
123 "' but no value for this parameter has been specified. Please call the appropriate 'set' " 124 "function of the JME::JetParameters object");
127 values.push_back(it->second);
133 JetResolutionObject::Definition::Definition(
const std::string& definition) {
134 std::vector<std::string> tokens = getTokens(definition);
137 if (tokens.size() < 3) {
139 "Definition line needs at least three tokens. Please check file format.");
142 size_t n_bins = std::stoul(tokens[0]);
144 if (tokens.size() < (n_bins + 2)) {
148 for (
size_t i = 0;
i < n_bins;
i++) {
149 m_bins_name.push_back(tokens[
i + 1]);
152 size_t n_variables = std::stoul(tokens[n_bins + 1]);
154 if (tokens.size() < (1 + n_bins + 1 + n_variables + 1)) {
158 for (
size_t i = 0;
i < n_variables;
i++) {
159 m_variables_name.push_back(tokens[n_bins + 2 +
i]);
162 m_formula_str = tokens[n_bins + n_variables + 2];
165 std::transform(formula_str_lower.begin(), formula_str_lower.end(), formula_str_lower.begin(), ::tolower);
167 if (formula_str_lower ==
"none")
174 if (!m_formula_str.empty())
176 m_formula = std::make_shared<reco::FormulaEvaluator>(m_formula_str);
178 m_formula = std::make_shared<TFormula>(
"jet_resolution_formula", m_formula_str.c_str());
180 for (
const auto&
bin : m_bins_name) {
181 const auto&
b = JetParameters::binning_to_string.right.find(
bin);
182 if (
b == JetParameters::binning_to_string.right.cend()) {
185 m_bins.push_back(
b->second);
188 for (
const auto&
v : m_variables_name) {
189 const auto&
var = JetParameters::binning_to_string.right.find(
v);
190 if (
var == JetParameters::binning_to_string.right.cend()) {
193 m_variables.push_back(
var->second);
198 std::vector<std::string> tokens = getTokens(line);
206 for (
size_t i = 0;
i < def.
nBins();
i++) {
207 Range r(std::stof(tokens[pos]), std::stof(tokens[pos + 1]));
209 m_bins_range.push_back(r);
212 size_t n_parameters = std::stoul(tokens[pos++]);
219 Range r(std::stof(tokens[pos]), std::stof(tokens[pos + 1]));
221 m_variables_range.push_back(r);
225 for (
size_t i = 0;
i < n_parameters;
i++) {
226 m_parameters_values.push_back(std::stof(tokens[pos++]));
232 std::ifstream
f(filename);
239 if ((
line.empty()) || (
line[0] ==
'#'))
244 if (!definition.empty()) {
247 m_records.push_back(
Record(
line, m_definition));
255 m_definition =
object.m_definition;
256 m_records =
object.m_records;
257 m_valid =
object.m_valid;
262 JetResolutionObject::JetResolutionObject() {
267 std::cout <<
"Definition: " << std::endl;
268 std::cout <<
" Number of binning variables: " << m_definition.nBins() << std::endl;
270 for (
const auto&
bin : m_definition.getBinsName()) {
274 std::cout <<
" Number of variables: " << m_definition.nVariables() << std::endl;
276 for (
const auto&
bin : m_definition.getVariablesName()) {
280 std::cout <<
" Formula: " << m_definition.getFormulaString() << std::endl;
282 std::cout << std::endl <<
"Bin contents" << std::endl;
284 for (
const auto&
record : m_records) {
287 for (
const auto&
bin :
record.getBinsRange()) {
288 std::cout <<
" " << m_definition.getBinName(index) <<
" [" <<
bin.min <<
" - " <<
bin.max <<
"]" 295 for (
const auto&
r :
record.getVariablesRange()) {
296 std::cout <<
" " << m_definition.getVariableName(index) <<
" [" <<
r.min <<
" - " <<
r.max <<
"] " 303 for (
const auto& par :
record.getParametersValues()) {
304 std::cout <<
" Parameter #" << index <<
" = " << par << std::endl;
311 std::ofstream
fout(file);
312 fout.setf(std::ios::right);
315 fout <<
"{" << m_definition.nBins();
317 for (
auto&
bin : m_definition.getBinsName())
320 fout <<
" " << m_definition.nVariables();
322 for (
auto&
var : m_definition.getVariablesName())
325 fout <<
" " << (m_definition.getFormulaString().empty() ?
"None" : m_definition.getFormulaString())
326 <<
" Resolution}" << std::endl;
329 for (
auto&
record : m_records) {
330 for (
auto&
r :
record.getBinsRange()) {
331 fout << std::left << std::setw(15) <<
r.min << std::setw(15) <<
r.max << std::setw(15);
335 for (
auto&
r :
record.getVariablesRange()) {
336 fout <<
r.min << std::setw(15) <<
r.max << std::setw(15);
339 for (
auto&
p :
record.getParametersValues()) {
340 fout <<
p << std::setw(15);
343 fout << std::endl << std::setw(0);
353 std::vector<float>
bins = bins_parameters.
createVector(m_definition.getBins());
356 const Record* good_record =
nullptr;
357 for (
const auto&
record : m_records) {
359 size_t valid_bins = 0;
360 size_t current_bin = 0;
361 for (
const auto&
bin :
record.getBinsRange()) {
362 if (
bin.is_inside(bins[current_bin]))
368 if (valid_bins == m_definition.nBins()) {
383 const auto*
formula = m_definition.getFormula();
386 auto const* pFormula = m_definition.getFormula();
394 double variables_[4] = {0};
403 std::vector<double> parametersD(parameters.begin(), parameters.end());
411 return formula.EvalPar(variables_);
ErrorCode
Error code: whether the classification was successful or failed.
T clip(const T &n, const T &lower, const T &upper)
const std::vector< Range > & getVariablesRange() const
Container::value_type value_type
size_t nVariables() const
std::vector< float > createVector(const std::vector< Binning > &binning) const
std::string getDefinitionLine(const std::string &line)
#define TYPELOOKUP_DATA_REG(_dataclass_)
const std::vector< float > & getParametersValues() const
unsigned nParameters() const
T first(std::pair< T, U > const &p)
void throwException(uint32_t code, const std::string &message)