File indexing completed on 2024-04-06 12:01:56
0001 #include <fstream>
0002 #include <sstream>
0003
0004 #include "CondFormats/BTauObjects/interface/BTagCalibration.h"
0005 #include "FWCore/Utilities/interface/Exception.h"
0006
0007 BTagCalibration::BTagCalibration(const std::string &taggr) : tagger_(taggr) {}
0008
0009 BTagCalibration::BTagCalibration(const std::string &taggr, const std::string &filename, bool validate)
0010 : tagger_(taggr) {
0011 std::ifstream ifs(filename);
0012 if (!ifs.good()) {
0013 throw cms::Exception("BTagCalibration") << "input file not available: " << filename;
0014 }
0015 readCSV(ifs, validate);
0016 ifs.close();
0017 }
0018
0019 void BTagCalibration::addEntry(const BTagEntry &entry) { data_[token(entry.params)].push_back(entry); }
0020
0021 const std::vector<BTagEntry> &BTagCalibration::getEntries(const BTagEntry::Parameters &par) const {
0022 std::string tok = token(par);
0023 if (!data_.count(tok)) {
0024 throw cms::Exception("BTagCalibration") << "(OperatingPoint, measurementType, sysType) not available: " << tok;
0025 }
0026 return data_.at(tok);
0027 }
0028
0029 void BTagCalibration::readCSV(const std::string &s, bool validate) {
0030 std::stringstream buff(s);
0031 readCSV(buff, validate);
0032 }
0033
0034 void BTagCalibration::readCSV(std::istream &s, bool validate) {
0035 std::string line;
0036
0037
0038 getline(s, line);
0039 if (line.find("OperatingPoint") == std::string::npos) {
0040 addEntry(BTagEntry(line, validate));
0041 }
0042
0043 while (getline(s, line)) {
0044 line = BTagEntry::trimStr(line);
0045 if (line.empty()) {
0046 continue;
0047 }
0048 addEntry(BTagEntry(line, validate));
0049 }
0050 }
0051
0052 void BTagCalibration::makeCSV(std::ostream &s) const {
0053 s << tagger_ << ";" << BTagEntry::makeCSVHeader();
0054 for (std::map<std::string, std::vector<BTagEntry> >::const_iterator i = data_.cbegin(); i != data_.cend(); ++i) {
0055 const std::vector<BTagEntry> &vec = i->second;
0056 for (std::vector<BTagEntry>::const_iterator j = vec.cbegin(); j != vec.cend(); ++j) {
0057 s << j->makeCSVLine();
0058 }
0059 }
0060 }
0061
0062 std::string BTagCalibration::makeCSV() const {
0063 std::stringstream buff;
0064 makeCSV(buff);
0065 return buff.str();
0066 }
0067
0068 std::string BTagCalibration::token(const BTagEntry::Parameters &par) {
0069 std::stringstream buff;
0070 buff << par.operatingPoint << ", " << par.measurementType << ", " << par.sysType;
0071 return buff.str();
0072 }