Skip to content

Commit

Permalink
Use std::from_chars instead of std::regex+std::atof
Browse files Browse the repository at this point in the history
  • Loading branch information
makortel committed Mar 3, 2023
1 parent 2f899a2 commit a178cc6
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions PhysicsTools/IsolationAlgos/plugins/CandIsolatorFromDeposits.cc
Expand Up @@ -17,8 +17,8 @@
#include "PhysicsTools/IsolationAlgos/interface/EventDependentAbsVeto.h"
#include "PhysicsTools/IsolationAlgos/interface/IsoDepositVetoFactory.h"

#include <charconv>
#include <string>
#include <regex>

class CandIsolatorFromDeposits : public edm::stream::EDProducer<> {
public:
Expand Down Expand Up @@ -60,11 +60,20 @@ using namespace edm;
using namespace reco;
using namespace reco::isodeposit;

bool isNumber(const std::string &str) {
static const std::regex re("^[+-]?(\\d+\\.?|\\d*\\.\\d*)$");
return regex_match(str.c_str(), re);
namespace {
bool toNumber(const std::string &str, double& result) {
if (str.empty()) {
return false;
}
const char* first = str.c_str();
const char* last = first+str.size();
if (*first == '+') {
++first;
}
const auto [ptr, ec] = std::from_chars(first, last, result);
return ec == std::errc();
}
}
double toNumber(const std::string &str) { return atof(str.c_str()); }

CandIsolatorFromDeposits::SingleDeposit::SingleDeposit(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
: srcToken_(iC.consumes<reco::IsoDepositMap>(iConfig.getParameter<edm::InputTag>("src"))),
Expand Down Expand Up @@ -108,9 +117,8 @@ CandIsolatorFromDeposits::SingleDeposit::SingleDeposit(const edm::ParameterSet &
evdepVetos_.push_back(evdep);
}
std::string weight = iConfig.getParameter<std::string>("weight");
if (isNumber(weight)) {
if (toNumber(weight, weight_)) {
//std::cout << "Weight is a simple number, " << toNumber(weight) << std::endl;
weight_ = toNumber(weight);
usesFunction_ = false;
} else {
usesFunction_ = true;
Expand Down

0 comments on commit a178cc6

Please sign in to comment.