File indexing completed on 2025-05-27 01:56:30
0001 #include "RecoLocalCalo/HGCalRecAlgos/interface/HGCalESProducerTools.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 #include "FWCore/Utilities/interface/RegexMatch.h"
0004 #include <sstream> // for std::istringstream
0005 #include <regex>
0006
0007 namespace hgcal {
0008
0009
0010
0011 std::string search_modkey(const std::string& module, const json& data, const std::string& name = "") {
0012 if (!data.is_object() or data.empty()) {
0013 cms::Exception ex("InvalidData");
0014 ex << "'" << name << "' does not have the expected map/dict structure!";
0015 ex.addContext("Calling hgcal::search_modkey()");
0016 throw ex;
0017 }
0018 for (auto it = data.begin(); it != data.end(); ++it) {
0019 std::regex re(edm::glob2reg(it.key()));
0020 if (std::regex_match(module, re)) {
0021 edm::LogInfo("search_modkey") << "search_modkey: Matched module='" << module << "' to modkey='" << it.key()
0022 << "'";
0023 return it.key();
0024 }
0025 }
0026 cms::Exception ex("InvalidData");
0027 ex << "Could not find matching key for '" << module << "' in '" << name << "'! Returning first key '"
0028 << data.begin().key() << "'...";
0029 ex.addContext("Calling hgcal::search_modkey()");
0030 throw ex;
0031 return data.begin().key();
0032 }
0033
0034
0035
0036
0037 std::string search_fedkey(const int& fedid, const json& data, const std::string& name = "") {
0038 if (!data.is_object() or data.empty()) {
0039 cms::Exception ex("InvalidData");
0040 ex << "'" << name << "' does not have the expected map/dict structure!";
0041 ex.addContext("Calling hgcal::search_fedkey()");
0042 throw ex;
0043 }
0044 auto it = data.begin();
0045 std::string matchedkey = data.begin().key();
0046 while (it != data.end()) {
0047 std::string fedkey = it.key();
0048
0049
0050 int low, high;
0051 char dash;
0052 std::istringstream iss(fedkey.c_str());
0053 iss >> low >> dash >> high;
0054 if (iss.eof() and dash == '-') {
0055 if (low <= fedid and fedid <= high) {
0056 matchedkey = fedkey;
0057 break;
0058 }
0059
0060
0061 } else {
0062 const std::string sfedid = std::to_string(fedid);
0063 std::regex re(edm::glob2reg(fedkey));
0064 if (std::regex_match(sfedid, re)) {
0065 matchedkey = fedkey;
0066 break;
0067 }
0068 }
0069
0070 ++it;
0071 }
0072 if (it == data.end()) {
0073 cms::Exception ex("InvalidData");
0074 ex << "Could not find matching key for '" << fedid << "' in '" << name << "'! Returning first key '" << matchedkey
0075 << "'...";
0076 ex.addContext("Calling hgcal::search_fedkey()");
0077 } else {
0078 edm::LogInfo("search_fedkey") << "search_fedkey: Matched module='" << fedid << "' to fedkey='" << matchedkey
0079 << "'";
0080 }
0081
0082 return matchedkey;
0083 }
0084
0085
0086 bool check_keys(const json& data,
0087 const std::string& firstkey,
0088 const std::vector<std::string>& keys,
0089 const std::string& fname) {
0090 bool iscomplete = true;
0091 for (auto const& key : keys) {
0092 if (not data[firstkey].contains(key)) {
0093 edm::LogWarning("checkkeys") << " JSON is missing key '" << key << "' for " << firstkey << "!"
0094 << " Please check file " << fname;
0095 iscomplete = false;
0096 }
0097 }
0098 return iscomplete;
0099 }
0100
0101 }