File indexing completed on 2021-02-14 12:49:57
0001 #include "CommonTools/Egamma/interface/EffectiveAreas.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003
0004 #include <cmath>
0005 #include <fstream>
0006 #include <string>
0007 #include <sstream>
0008
0009 EffectiveAreas::EffectiveAreas(const std::string& filename) : filename_(filename) {
0010
0011 std::ifstream inputFile;
0012 inputFile.open(filename_.c_str());
0013 if (!inputFile.is_open())
0014 throw cms::Exception("EffectiveAreas config failure") << "failed to open the file " << filename_ << std::endl;
0015
0016
0017 std::string line;
0018 const float undef = -999;
0019 while (getline(inputFile, line)) {
0020 if (line[0] == '#')
0021 continue;
0022 float etaMin = undef, etaMax = undef, effArea = undef;
0023 std::stringstream ss(line);
0024 ss >> etaMin >> etaMax >> effArea;
0025
0026
0027
0028
0029 if (etaMin == undef || etaMax == undef || effArea == undef)
0030 throw cms::Exception("EffectiveAreas config failure")
0031 << "wrong file format, file name " << filename_ << std::endl;
0032
0033 absEtaMin_.push_back(etaMin);
0034 absEtaMax_.push_back(etaMax);
0035 effectiveAreaValues_.push_back(effArea);
0036 }
0037
0038
0039
0040 checkConsistency();
0041 }
0042
0043
0044 const float EffectiveAreas::getEffectiveArea(float eta) const {
0045 float effArea = 0;
0046 uint nEtaBins = absEtaMin_.size();
0047 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0048 if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
0049 effArea = effectiveAreaValues_[iEta];
0050 break;
0051 }
0052 }
0053
0054 return effArea;
0055 }
0056
0057 void EffectiveAreas::printEffectiveAreas() const {
0058 printf("EffectiveAreas: source file %s\n", filename_.c_str());
0059 printf(" eta_min eta_max effective area\n");
0060 uint nEtaBins = absEtaMin_.size();
0061 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0062 printf(" %8.4f %8.4f %8.5f\n", absEtaMin_[iEta], absEtaMax_[iEta], effectiveAreaValues_[iEta]);
0063 }
0064 }
0065
0066
0067 void EffectiveAreas::checkConsistency() const {
0068
0069 if (effectiveAreaValues_.empty())
0070 throw cms::Exception("EffectiveAreas config failure")
0071 << "found no effective area constans in the file " << filename_ << std::endl;
0072
0073 uint nEtaBins = absEtaMin_.size();
0074 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0075
0076 if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
0077 throw cms::Exception("EffectiveAreas config failure")
0078 << "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
0079
0080
0081
0082 if (iEta != nEtaBins - 1)
0083 if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
0084 throw cms::Exception("EffectiveAreas config failure")
0085 << "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
0086
0087
0088
0089
0090 if (!(effectiveAreaValues_[iEta] >= 0 && effectiveAreaValues_[iEta] < 31.4))
0091 throw cms::Exception("EffectiveAreas config failure")
0092 << "effective area values are too large or negative in the file" << filename_ << std::endl;
0093 }
0094 }