CMS 3D CMS Logo

BTagCalibration.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <sstream>
3 
6 
7 BTagCalibration::BTagCalibration(const std::string &taggr) : tagger_(taggr) {}
8 
10  : tagger_(taggr) {
11  std::ifstream ifs(filename);
12  if (!ifs.good()) {
13  throw cms::Exception("BTagCalibration")
14  << "input file not available: "
15  << filename;
16  }
17  readCSV(ifs, validate);
18  ifs.close();
19 }
20 
22 {
23  data_[token(entry.params)].push_back(entry);
24 }
25 
26 const std::vector<BTagEntry>& BTagCalibration::getEntries(
27  const BTagEntry::Parameters &par) const
28 {
29  std::string tok = token(par);
30  if (!data_.count(tok)) {
31  throw cms::Exception("BTagCalibration")
32  << "(OperatingPoint, measurementType, sysType) not available: "
33  << tok;
34  }
35  return data_.at(tok);
36 }
37 
38 void BTagCalibration::readCSV(const std::string &s, bool validate) {
39  std::stringstream buff(s);
40  readCSV(buff, validate);
41 }
42 
43 void BTagCalibration::readCSV(std::istream &s, bool validate) {
45 
46  // firstline might be the header
47  getline(s,line);
48  if (line.find("OperatingPoint") == std::string::npos) {
49  addEntry(BTagEntry(line, validate));
50  }
51 
52  while (getline(s,line)) {
53  line = BTagEntry::trimStr(line);
54  if (line.empty()) { // skip empty lines
55  continue;
56  }
57  addEntry(BTagEntry(line, validate));
58  }
59 }
60 
61 void BTagCalibration::makeCSV(std::ostream &s) const
62 {
63  s << tagger_ << ";" << BTagEntry::makeCSVHeader();
64  for (std::map<std::string, std::vector<BTagEntry> >::const_iterator i
65  = data_.cbegin(); i != data_.cend(); ++i) {
66  const std::vector<BTagEntry> &vec = i->second;
67  for (std::vector<BTagEntry>::const_iterator j
68  = vec.cbegin(); j != vec.cend(); ++j) {
69  s << j->makeCSVLine();
70  }
71  }
72 }
73 
75 {
76  std::stringstream buff;
77  makeCSV(buff);
78  return buff.str();
79 }
80 
82 {
83  std::stringstream buff;
84  buff << par.operatingPoint << ", "
85  << par.measurementType << ", "
86  << par.sysType;
87  return buff.str();
88 }
std::string sysType
Definition: BTagEntry.h:41
static std::string trimStr(std::string str)
Definition: BTagEntry.cc:250
static std::string makeCSVHeader()
Definition: BTagEntry.cc:217
const std::vector< BTagEntry > & getEntries(const BTagEntry::Parameters &par) const
OperatingPoint operatingPoint
Definition: BTagEntry.h:39
std::string makeCSV() const
Parameters params
Definition: BTagEntry.h:79
void addEntry(const BTagEntry &entry)
std::string tagger_
std::string measurementType
Definition: BTagEntry.h:40
void readCSV(std::istream &s, bool validate)
static std::string token(const BTagEntry::Parameters &par)
std::map< std::string, std::vector< BTagEntry > > data_