CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EffectiveAreas.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 #include <fstream>
6 #include <string>
7 #include <sstream>
8 
9 EffectiveAreas::EffectiveAreas(const std::string& filename) : filename_(filename) {
10  // Open the file with the effective area constants
11  std::ifstream inputFile;
12  inputFile.open(filename_.c_str());
13  if (!inputFile.is_open())
14  throw cms::Exception("EffectiveAreas config failure") << "failed to open the file " << filename_ << std::endl;
15 
16  // Read file line by line
18  const float undef = -999;
19  while (getline(inputFile, line)) {
20  if (line[0] == '#')
21  continue; // skip the comments lines
22  float etaMin = undef, etaMax = undef, effArea = undef;
23  std::stringstream ss(line);
24  ss >> etaMin >> etaMax >> effArea;
25  // In case if the format is messed up, there are letters
26  // instead of numbers, or not exactly three numbers in the line,
27  // it is likely that one or more of these vars never changed
28  // the original "undef" value:
29  if (etaMin == undef || etaMax == undef || effArea == undef)
30  throw cms::Exception("EffectiveAreas config failure")
31  << "wrong file format, file name " << filename_ << std::endl;
32 
33  absEtaMin_.push_back(etaMin);
34  absEtaMax_.push_back(etaMax);
35  effectiveAreaValues_.push_back(effArea);
36  }
37 
38  // Extra consistency checks are in the function below.
39  // If any of them fail, an exception is thrown.
41 }
42 
43 // Return effective area for given eta
44 const float EffectiveAreas::getEffectiveArea(float eta) const {
45  float effArea = 0;
46  uint nEtaBins = absEtaMin_.size();
47  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
48  if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
49  effArea = effectiveAreaValues_[iEta];
50  break;
51  }
52  }
53 
54  return effArea;
55 }
56 
58  printf("EffectiveAreas: source file %s\n", filename_.c_str());
59  printf(" eta_min eta_max effective area\n");
60  uint nEtaBins = absEtaMin_.size();
61  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
62  printf(" %8.4f %8.4f %8.5f\n", absEtaMin_[iEta], absEtaMax_[iEta], effectiveAreaValues_[iEta]);
63  }
64 }
65 
66 // Basic common sense checks
68  // There should be at least one eta range with one constant
69  if (effectiveAreaValues_.empty())
70  throw cms::Exception("EffectiveAreas config failure")
71  << "found no effective area constans in the file " << filename_ << std::endl;
72 
73  uint nEtaBins = absEtaMin_.size();
74  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
75  // The low limit should be lower than the upper limit
76  if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
77  throw cms::Exception("EffectiveAreas config failure")
78  << "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
79 
80  // The low limit of the next range should be (near) equal to the
81  // upper limit of the previous range
82  if (iEta != nEtaBins - 1) // don't do the check for the last bin
83  if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
84  throw cms::Exception("EffectiveAreas config failure")
85  << "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
86 
87  // The effective area should be non-negative number,
88  // and should be less than the whole calorimeter area
89  // eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
90  if (!(effectiveAreaValues_[iEta] >= 0 && effectiveAreaValues_[iEta] < 31.4))
91  throw cms::Exception("EffectiveAreas config failure")
92  << "effective area values are too large or negative in the file" << filename_ << std::endl;
93  }
94 }
tuple etaMin
Definition: Puppi_cff.py:46
const float getEffectiveArea(float eta) const
EffectiveAreas(const std::string &filename)
std::vector< float > absEtaMax_
std::vector< float > effectiveAreaValues_
printf("params %d %f %f %f\n", minT, eps, errmax, chi2max)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< float > absEtaMin_
void checkConsistency() const
tuple filename
Definition: lut2db_cfg.py:20
void printEffectiveAreas() const
tuple etaMax
Definition: Puppi_cff.py:47
const std::string filename_