File indexing completed on 2024-04-06 12:01:05
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, const bool quadraticEAflag)
0010 : filename_(filename), quadraticEAflag_(quadraticEAflag) {
0011
0012 std::ifstream inputFile;
0013 inputFile.open(filename_.c_str());
0014 if (!inputFile.is_open())
0015 throw cms::Exception("EffectiveAreas config failure") << "failed to open the file " << filename_ << std::endl;
0016
0017
0018 std::string line;
0019 const float undef = -999;
0020
0021 if (!quadraticEAflag_) {
0022 while (getline(inputFile, line)) {
0023 if (line[0] == '#')
0024 continue;
0025 float etaMin = undef, etaMax = undef, effArea = undef;
0026 std::stringstream ss(line);
0027 ss >> etaMin >> etaMax >> effArea;
0028
0029
0030
0031
0032 if (etaMin == undef || etaMax == undef || effArea == undef)
0033 throw cms::Exception("EffectiveAreas config failure")
0034 << "wrong file format, file name " << filename_ << std::endl;
0035
0036 absEtaMin_.push_back(etaMin);
0037 absEtaMax_.push_back(etaMax);
0038 effectiveAreaValues_.push_back(effArea);
0039 }
0040
0041 }
0042
0043 else {
0044 while (getline(inputFile, line)) {
0045 if (line[0] == '#')
0046 continue;
0047
0048 float etaMin = undef, etaMax = undef;
0049 float linEffArea = undef, quadEffArea = undef;
0050
0051 std::stringstream ss(line);
0052
0053 ss >> etaMin >> etaMax >> linEffArea >> quadEffArea;
0054
0055
0056
0057
0058
0059 if (etaMin == undef || etaMax == undef || linEffArea == undef || quadEffArea == undef)
0060 throw cms::Exception("EffectiveAreas config failure")
0061 << "wrong file format, file name " << filename_ << std::endl;
0062
0063 absEtaMin_.push_back(etaMin);
0064 absEtaMax_.push_back(etaMax);
0065 linearEffectiveAreaValues_.push_back(linEffArea);
0066 quadraticEffectiveAreaValues_.push_back(quadEffArea);
0067 }
0068 }
0069
0070
0071
0072 checkConsistency();
0073 }
0074
0075
0076 const float EffectiveAreas::getEffectiveArea(float eta) const {
0077 float effArea = 0;
0078 uint nEtaBins = absEtaMin_.size();
0079 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0080 if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
0081 effArea = effectiveAreaValues_[iEta];
0082 break;
0083 }
0084 }
0085
0086 return effArea;
0087 }
0088
0089
0090 const float EffectiveAreas::getLinearEA(float eta) const {
0091 float linEffArea = 0;
0092 uint nEtaBins = absEtaMin_.size();
0093
0094 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0095 if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
0096 linEffArea = linearEffectiveAreaValues_[iEta];
0097 break;
0098 }
0099 }
0100
0101 return linEffArea;
0102 }
0103
0104
0105 const float EffectiveAreas::getQuadraticEA(float eta) const {
0106 float quadEffArea = 0;
0107 uint nEtaBins = absEtaMin_.size();
0108
0109 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0110 if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
0111 quadEffArea = quadraticEffectiveAreaValues_[iEta];
0112 break;
0113 }
0114 }
0115
0116 return quadEffArea;
0117 }
0118
0119 void EffectiveAreas::printEffectiveAreas() const {
0120 printf("EffectiveAreas: source file %s\n", filename_.c_str());
0121
0122 if (!quadraticEAflag_) {
0123 printf(" eta_min eta_max effective area\n");
0124 uint nEtaBins = absEtaMin_.size();
0125
0126 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0127 printf(" %8.4f %8.4f %8.5f\n", absEtaMin_[iEta], absEtaMax_[iEta], effectiveAreaValues_[iEta]);
0128 }
0129 }
0130
0131 else {
0132 printf(" eta_min eta_max EA linear term EA quadratic term\n");
0133 uint nEtaBins = absEtaMin_.size();
0134 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0135 printf(" %8.4f %8.4f %8.5f %8.5f\n",
0136 absEtaMin_[iEta],
0137 absEtaMax_[iEta],
0138 linearEffectiveAreaValues_[iEta],
0139 quadraticEffectiveAreaValues_[iEta]);
0140 }
0141 }
0142 }
0143
0144
0145 void EffectiveAreas::checkConsistency() const {
0146
0147
0148 if (!quadraticEAflag_) {
0149 if (effectiveAreaValues_.empty())
0150 throw cms::Exception("EffectiveAreas config failure")
0151 << "found no effective area constants in the file " << filename_ << std::endl;
0152
0153 uint nEtaBins = absEtaMin_.size();
0154
0155 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0156
0157 if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
0158 throw cms::Exception("EffectiveAreas config failure")
0159 << "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
0160
0161
0162
0163 if (iEta != nEtaBins - 1)
0164 if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
0165 throw cms::Exception("EffectiveAreas config failure")
0166 << "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
0167
0168
0169
0170
0171 if (!(effectiveAreaValues_[iEta] >= 0 && effectiveAreaValues_[iEta] < 31.4))
0172 throw cms::Exception("EffectiveAreas config failure")
0173 << "effective area values are too large or negative in the file" << filename_ << std::endl;
0174 }
0175 }
0176
0177 else {
0178 if (linearEffectiveAreaValues_.empty() || quadraticEffectiveAreaValues_.empty())
0179 throw cms::Exception("EffectiveAreas config failure")
0180 << "found no effective area constants (linear and/or quadratic) in the file " << filename_ << std::endl;
0181
0182 uint nEtaBins = absEtaMin_.size();
0183
0184 for (uint iEta = 0; iEta < nEtaBins; iEta++) {
0185
0186 if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
0187 throw cms::Exception("EffectiveAreas config failure")
0188 << "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
0189
0190
0191
0192 if (iEta != nEtaBins - 1)
0193 if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
0194 throw cms::Exception("EffectiveAreas config failure")
0195 << "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
0196
0197
0198
0199
0200 if (!(linearEffectiveAreaValues_[iEta] >= 0 && linearEffectiveAreaValues_[iEta] < 31.4))
0201 throw cms::Exception("EffectiveAreas config failure")
0202 << "effective area values are too large or negative in the file" << filename_ << std::endl;
0203 }
0204 }
0205 }