Back to home page

Project CMSSW displayed by LXR

 
 

    


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   // Open the file with the effective area constants
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   // Read file line by line
0017   std::string line;
0018   const float undef = -999;
0019   while (getline(inputFile, line)) {
0020     if (line[0] == '#')
0021       continue;  // skip the comments lines
0022     float etaMin = undef, etaMax = undef, effArea = undef;
0023     std::stringstream ss(line);
0024     ss >> etaMin >> etaMax >> effArea;
0025     // In case if the format is messed up, there are letters
0026     // instead of numbers, or not exactly three numbers in the line,
0027     // it is likely that one or more of these vars never changed
0028     // the original "undef" value:
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   // Extra consistency checks are in the function below.
0039   // If any of them fail, an exception is thrown.
0040   checkConsistency();
0041 }
0042 
0043 // Return effective area for given eta
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 // Basic common sense checks
0067 void EffectiveAreas::checkConsistency() const {
0068   // There should be at least one eta range with one constant
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     // The low limit should be lower than the upper limit
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     // The low limit of the next range should be (near) equal to the
0081     // upper limit of the previous range
0082     if (iEta != nEtaBins - 1)  // don't do the check for the last bin
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     // The effective area should be non-negative number,
0088     // and should be less than the whole calorimeter area
0089     // eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
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 }