Skip to content

Commit

Permalink
Merge pull request #43334 from iarspider/no-classlib-regex
Browse files Browse the repository at this point in the history
Replace classlib's Regex with std::regex
  • Loading branch information
cmsbuild authored Feb 9, 2024
2 parents 510d56a + 7fc353e commit de80e9e
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions Validation/RecoTau/plugins/DQMHistNormalizer.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** \class DQMHistNormalizer
*
*
* Class to produce efficiency histograms by dividing nominator by denominator histograms
*
* \author Christian Veelken, UC Davis
Expand All @@ -18,15 +18,30 @@
#include "FWCore/ServiceRegistry/interface/Service.h"

//Regexp handling
#include "classlib/utils/RegexpMatch.h"
#include "classlib/utils/Regexp.h"

#include <regex>
#include <string>
#include <vector>
#include <map>

using namespace std;

// Three implementations were tested: char-by-char (this version),
// using std::string::find + std::string::replace and std::regex_replace.
// First one takes ~60 ns per iteration, second one ~85 ns,
// and the regex implementation takes nearly 1 us
std::string globToRegex(const std::string& s) {
std::string out;
out.reserve(s.size());
for (auto ch : s) {
if (ch == '*') {
out.push_back('.');
out.push_back('*');
}
out.push_back(ch);
}
return out;
}

class DQMHistNormalizer : public edm::one::EDAnalyzer<edm::one::SharedResources, edm::one::WatchRuns> {
public:
typedef dqm::legacy::DQMStore DQMStore;
Expand All @@ -39,7 +54,6 @@ class DQMHistNormalizer : public edm::one::EDAnalyzer<edm::one::SharedResources,
void endRun(const edm::Run& r, const edm::EventSetup& c) override;

private:
lat::Regexp* buildRegex(const string& expr);
vector<string> plotNamesToNormalize_; //root name used by all the plots that must be normalized
string reference_;
};
Expand All @@ -59,18 +73,6 @@ void DQMHistNormalizer::analyze(const edm::Event&, const edm::EventSetup&) {
//--- nothing to be done yet
}

lat::Regexp* DQMHistNormalizer::buildRegex(const string& expr) {
lat::Regexp* rx = nullptr;
try {
rx = new lat::Regexp(expr, 0, lat::Regexp::Wildcard);
rx->study();
} catch (lat::Error& e) {
throw cms::Exception("DQMHistNormalizer")
<< "Invalid regular expression '" << expr.c_str() << "':" << e.explain().c_str();
}
return rx;
}

void DQMHistNormalizer::endRun(const edm::Run& r, const edm::EventSetup& c) {
//std::cout << "<DQMHistNormalizer::endJob>:" << std::endl;

Expand All @@ -83,21 +85,22 @@ void DQMHistNormalizer::endRun(const edm::Run& r, const edm::EventSetup& c) {
DQMStore& dqmStore = (*edm::Service<DQMStore>());

vector<MonitorElement*> allOurMEs = dqmStore.getAllContents("RecoTauV/");
lat::Regexp* refregex = buildRegex("*RecoTauV/*/" + reference_);
vector<lat::Regexp*> toNormRegex;
std::regex refregex = std::regex(".*RecoTauV/.*/" + globToRegex(reference_), std::regex::nosubs);
vector<std::regex> toNormRegex;
for (std::vector<string>::const_iterator toNorm = plotNamesToNormalize_.begin();
toNorm != plotNamesToNormalize_.end();
++toNorm)
toNormRegex.push_back(buildRegex("*RecoTauV/*/" + *toNorm));
toNormRegex.emplace_back(".*RecoTauV/.*/" + globToRegex(*toNorm), std::regex::nosubs);

map<string, MonitorElement*> refsMap;
vector<MonitorElement*> toNormElements;
std::smatch path_match;

for (vector<MonitorElement*>::const_iterator element = allOurMEs.begin(); element != allOurMEs.end(); ++element) {
string pathname = (*element)->getFullname();
//cout << pathname << endl;
//Matches reference
if (refregex->match(pathname)) {
if (std::regex_match(pathname, path_match, refregex)) {
//cout << "Matched to ref" << endl;
string dir = pathname.substr(0, pathname.rfind('/'));
if (refsMap.find(dir) != refsMap.end()) {
Expand All @@ -111,19 +114,15 @@ void DQMHistNormalizer::endRun(const edm::Run& r, const edm::EventSetup& c) {
}

//Matches targets
for (vector<lat::Regexp*>::const_iterator reg = toNormRegex.begin(); reg != toNormRegex.end(); ++reg) {
if ((*reg)->match(pathname)) {
for (const auto& reg : toNormRegex) {
if (std::regex_match(pathname, path_match, reg)) {
//cout << "Matched to target" << endl;
toNormElements.push_back(*element);
//cout << "Filled the collection" << endl;
}
}
}

delete refregex;
for (vector<lat::Regexp*>::const_iterator reg = toNormRegex.begin(); reg != toNormRegex.end(); ++reg)
delete *reg;

for (vector<MonitorElement*>::const_iterator matchingElement = toNormElements.begin();
matchingElement != toNormElements.end();
++matchingElement) {
Expand Down

0 comments on commit de80e9e

Please sign in to comment.