32 size_t first = line.find (
'{');
33 size_t last = line.find (
'}');
35 if (first != std::string::npos && last != std::string::npos && first < last)
36 return std::string(line, first + 1, last - first - 1);
43 throw edm::Exception(static_cast<edm::errors::ErrorCodes>(code), message);
45 std::stringstream
error;
46 error << message <<
" Error code: " << code;
47 throw std::runtime_error(error.str());
54 {Binning::JetAbsEta,
"JetAbsEta"}, {Binning::JetE,
"JetE"},
63 JetParameters::JetParameters(std::initializer_list<typename value_type::value_type>
init) {
65 set(
i.first,
i.second);
76 m_values[Binning::JetAbsEta] = fabs(eta);
81 m_values[Binning::JetE] =
e;
86 m_values[Binning::JetArea] =
area;
101 m_values[Binning::Rho] = rho;
106 m_values.emplace(bin, value);
110 m_values.emplace(Binning::JetAbsEta, fabs(value));
117 set(value.first, value.second);
121 std::vector<float> JetParameters::createVector(
const std::vector<Binning>&
binning)
const {
122 std::vector<float>
values;
123 for (
const auto&
bin: binning) {
124 const auto& it = m_values.find(
bin);
125 if (it == m_values.cend()) {
127 JetParameters::binning_to_string.left.at(
bin) +
128 "' but no value for this parameter has been specified. Please call the appropriate 'set' function of the JME::JetParameters object");
131 values.push_back(it->second);
138 JetResolutionObject::Definition::Definition(
const std::string& definition) {
140 std::vector<std::string> tokens = getTokens(definition);
143 if (tokens.size() < 3) {
147 size_t n_bins = std::stoul(tokens[0]);
149 if (tokens.size() < (n_bins + 2)) {
153 for (
size_t i = 0;
i < n_bins;
i++) {
154 m_bins_name.push_back(tokens[
i + 1]);
157 size_t n_variables = std::stoul(tokens[n_bins + 1]);
159 if (tokens.size() < (1 + n_bins + 1 + n_variables + 1)) {
163 for (
size_t i = 0;
i < n_variables;
i++) {
164 m_variables_name.push_back(tokens[n_bins + 2 +
i]);
167 m_formula_str = tokens[n_bins + n_variables + 2];
170 std::transform(formula_str_lower.begin(), formula_str_lower.end(), formula_str_lower.begin(), ::tolower);
172 if (formula_str_lower ==
"none") {
175 if ((tokens.size() > n_bins + n_variables + 3) && (std::atoi(tokens[n_bins + n_variables + 3].c_str()))) {
176 size_t n_parameters = std::stoul(tokens[n_bins + n_variables + 3]);
178 if (tokens.size() < (1 + n_bins + 1 + n_variables + 1 + 1 + n_parameters)) {
182 for (
size_t i = 0;
i < n_parameters;
i++) {
183 m_formula_str += tokens[n_bins + n_variables + 4 +
i] +
" ";
192 if (!m_formula_str.empty()) {
193 if (m_formula_str.find(
' ') == std::string::npos)
195 m_formula = std::make_shared<reco::FormulaEvaluator>(m_formula_str);
197 m_formula = std::make_shared<TFormula>(
"jet_resolution_formula", m_formula_str.c_str());
200 m_parameters_name = getTokens(m_formula_str);
202 for (
const auto&
bin: m_bins_name) {
203 const auto&
b = JetParameters::binning_to_string.right.find(
bin);
204 if (
b == JetParameters::binning_to_string.right.cend()) {
207 m_bins.push_back(
b->second);
210 for (
const auto&
v: m_variables_name) {
211 const auto&
var = JetParameters::binning_to_string.right.find(
v);
212 if (
var == JetParameters::binning_to_string.right.cend()) {
215 m_variables.push_back(
var->second);
221 std::vector<std::string> tokens = getTokens(line);
229 for (
size_t i = 0;
i < def.
nBins();
i++) {
230 Range r(std::stof(tokens[pos]), std::stof(tokens[pos + 1]));
232 m_bins_range.push_back(r);
235 size_t n_parameters = std::stoul(tokens[pos++]);
242 Range r(std::stof(tokens[pos]), std::stof(tokens[pos + 1]));
244 m_variables_range.push_back(r);
248 for (
size_t i = 0;
i < n_parameters;
i++) {
249 m_parameters_values.push_back(std::stof(tokens[pos++]));
256 std::ifstream
f(filename);
263 if ((
line.empty()) || (
line[0] ==
'#'))
268 if (!definition.empty()) {
271 m_records.push_back(
Record(
line, m_definition));
279 m_definition =
object.m_definition;
280 m_records =
object.m_records;
281 m_valid =
object.m_valid;
286 JetResolutionObject::JetResolutionObject() {
292 std::cout <<
"Definition: " << std::endl;
293 std::cout <<
" Number of binning variables: " << m_definition.nBins() << std::endl;
295 for (
const auto&
bin: m_definition.getBinsName()) {
299 std::cout <<
" Number of variables: " << m_definition.nVariables() << std::endl;
301 for (
const auto&
bin: m_definition.getVariablesName()) {
305 std::cout <<
" Formula: " << m_definition.getFormulaString() << std::endl;
307 std::cout << std::endl <<
"Bin contents" << std::endl;
309 for (
const auto&
record: m_records) {
312 for (
const auto&
bin:
record.getBinsRange()) {
313 std::cout <<
" " << m_definition.getBinName(index) <<
" [" <<
bin.min <<
" - " <<
bin.max <<
"]" << std::endl;
319 for (
const auto&
r:
record.getVariablesRange()) {
320 std::cout <<
" " << m_definition.getVariableName(index) <<
" [" <<
r.min <<
" - " <<
r.max <<
"] " << std::endl;
326 for (
const auto& par:
record.getParametersValues()) {
327 std::cout <<
" Parameter #" << index <<
" = " << par << std::endl;
335 std::ofstream
fout(file);
336 fout.setf(std::ios::right);
339 fout <<
"{" << m_definition.nBins();
341 for (
auto&
bin: m_definition.getBinsName())
344 fout <<
" " << m_definition.nVariables();
346 for (
auto&
var: m_definition.getVariablesName())
349 fout <<
" " << (m_definition.getFormulaString().empty() ?
"None" : m_definition.getFormulaString()) <<
" Resolution}" << std::endl;
352 for (
auto&
record: m_records) {
353 for (
auto&
r:
record.getBinsRange()) {
354 fout << std::left << std::setw(15) <<
r.min << std::setw(15) <<
r.max << std::setw(15);
358 for (
auto&
r:
record.getVariablesRange()) {
359 fout <<
r.min << std::setw(15) <<
r.max << std::setw(15);
362 for (
auto&
p:
record.getParametersValues()) {
363 fout <<
p << std::setw(15);
366 fout << std::endl << std::setw(0);
377 std::vector<float>
bins = bins_parameters.
createVector(m_definition.getBins());
380 const Record* good_record =
nullptr;
381 for (
const auto&
record: m_records) {
384 size_t valid_bins = 0;
385 size_t current_bin = 0;
386 for (
const auto&
bin:
record.getBinsRange()) {
387 if (
bin.is_inside(bins[current_bin]))
393 if (valid_bins == m_definition.nBins()) {
408 const auto*
formula = m_definition.getFormula();
411 auto const* pFormula = m_definition.getFormula();
419 double variables_[4] = {0};
427 std::vector<double> parametersD(parameters.begin(),parameters.end());
437 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)
bin
set the eta bin as selection string.
#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)