CMS 3D CMS Logo

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, const bool quadraticEAflag)
10  : filename_(filename), quadraticEAflag_(quadraticEAflag) {
11  // Open the file with the effective area constants
12  std::ifstream inputFile;
13  inputFile.open(filename_.c_str());
14  if (!inputFile.is_open())
15  throw cms::Exception("EffectiveAreas config failure") << "failed to open the file " << filename_ << std::endl;
16 
17  // Read file line by line
19  const float undef = -999;
20 
21  if (!quadraticEAflag_) {
22  while (getline(inputFile, line)) {
23  if (line[0] == '#')
24  continue; // skip the comments lines
25  float etaMin = undef, etaMax = undef, effArea = undef;
26  std::stringstream ss(line);
27  ss >> etaMin >> etaMax >> effArea;
28  // In case if the format is messed up, there are letters
29  // instead of numbers, or not exactly three numbers in the line,
30  // it is likely that one or more of these vars never changed
31  // the original "undef" value:
32  if (etaMin == undef || etaMax == undef || effArea == undef)
33  throw cms::Exception("EffectiveAreas config failure")
34  << "wrong file format, file name " << filename_ << std::endl;
35 
36  absEtaMin_.push_back(etaMin);
37  absEtaMax_.push_back(etaMax);
38  effectiveAreaValues_.push_back(effArea);
39  }
40 
41  }
42 
43  else {
44  while (getline(inputFile, line)) {
45  if (line[0] == '#')
46  continue; // skip the comments lines
47 
48  float etaMin = undef, etaMax = undef;
49  float linEffArea = undef, quadEffArea = undef;
50 
51  std::stringstream ss(line);
52 
53  ss >> etaMin >> etaMax >> linEffArea >> quadEffArea;
54 
55  // In case if the format is messed up, there are letters
56  // instead of numbers, or not exactly three numbers in the line,
57  // it is likely that one or more of these vars never changed
58  // the original "undef" value:
59  if (etaMin == undef || etaMax == undef || linEffArea == undef || quadEffArea == undef)
60  throw cms::Exception("EffectiveAreas config failure")
61  << "wrong file format, file name " << filename_ << std::endl;
62 
63  absEtaMin_.push_back(etaMin);
64  absEtaMax_.push_back(etaMax);
65  linearEffectiveAreaValues_.push_back(linEffArea);
66  quadraticEffectiveAreaValues_.push_back(quadEffArea);
67  }
68  }
69 
70  // Extra consistency checks are in the function below.
71  // If any of them fail, an exception is thrown.
73 }
74 
75 // Return effective area for given eta
76 const float EffectiveAreas::getEffectiveArea(float eta) const {
77  float effArea = 0;
78  uint nEtaBins = absEtaMin_.size();
79  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
81  effArea = effectiveAreaValues_[iEta];
82  break;
83  }
84  }
85 
86  return effArea;
87 }
88 
89 // Return linear term of EA for given eta
90 const float EffectiveAreas::getLinearEA(float eta) const {
91  float linEffArea = 0;
92  uint nEtaBins = absEtaMin_.size();
93 
94  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
96  linEffArea = linearEffectiveAreaValues_[iEta];
97  break;
98  }
99  }
100 
101  return linEffArea;
102 }
103 
104 // Return quadratic term of EA for given eta
105 const float EffectiveAreas::getQuadraticEA(float eta) const {
106  float quadEffArea = 0;
107  uint nEtaBins = absEtaMin_.size();
108 
109  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
111  quadEffArea = quadraticEffectiveAreaValues_[iEta];
112  break;
113  }
114  }
115 
116  return quadEffArea;
117 }
118 
120  printf("EffectiveAreas: source file %s\n", filename_.c_str());
121 
122  if (!quadraticEAflag_) {
123  printf(" eta_min eta_max effective area\n");
124  uint nEtaBins = absEtaMin_.size();
125 
126  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
127  printf(" %8.4f %8.4f %8.5f\n", absEtaMin_[iEta], absEtaMax_[iEta], effectiveAreaValues_[iEta]);
128  }
129  }
130 
131  else {
132  printf(" eta_min eta_max EA linear term EA quadratic term\n");
133  uint nEtaBins = absEtaMin_.size();
134  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
135  printf(" %8.4f %8.4f %8.5f %8.5f\n",
136  absEtaMin_[iEta],
137  absEtaMax_[iEta],
140  }
141  }
142 }
143 
144 // Basic common sense checks
146  // There should be at least one eta range with one constant
147 
148  if (!quadraticEAflag_) {
149  if (effectiveAreaValues_.empty())
150  throw cms::Exception("EffectiveAreas config failure")
151  << "found no effective area constants in the file " << filename_ << std::endl;
152 
153  uint nEtaBins = absEtaMin_.size();
154 
155  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
156  // The low limit should be lower than the upper limit
157  if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
158  throw cms::Exception("EffectiveAreas config failure")
159  << "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
160 
161  // The low limit of the next range should be (near) equal to the
162  // upper limit of the previous range
163  if (iEta != nEtaBins - 1) // don't do the check for the last bin
164  if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
165  throw cms::Exception("EffectiveAreas config failure")
166  << "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
167 
168  // The effective area should be non-negative number,
169  // and should be less than the whole calorimeter area
170  // eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
171  if (!(effectiveAreaValues_[iEta] >= 0 && effectiveAreaValues_[iEta] < 31.4))
172  throw cms::Exception("EffectiveAreas config failure")
173  << "effective area values are too large or negative in the file" << filename_ << std::endl;
174  }
175  }
176 
177  else {
179  throw cms::Exception("EffectiveAreas config failure")
180  << "found no effective area constants (linear and/or quadratic) in the file " << filename_ << std::endl;
181 
182  uint nEtaBins = absEtaMin_.size();
183 
184  for (uint iEta = 0; iEta < nEtaBins; iEta++) {
185  // The low limit should be lower than the upper limit
186  if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
187  throw cms::Exception("EffectiveAreas config failure")
188  << "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
189 
190  // The low limit of the next range should be (near) equal to the
191  // upper limit of the previous range
192  if (iEta != nEtaBins - 1) // don't do the check for the last bin
193  if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
194  throw cms::Exception("EffectiveAreas config failure")
195  << "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
196 
197  // The linear effective area should be non-negative number,
198  // and should be less than the whole calorimeter area
199  // eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
201  throw cms::Exception("EffectiveAreas config failure")
202  << "effective area values are too large or negative in the file" << filename_ << std::endl;
203  }
204  }
205 }
void printEffectiveAreas() const
std::vector< float > absEtaMax_
std::vector< float > effectiveAreaValues_
const float getQuadraticEA(float eta) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
EffectiveAreas(const std::string &filename, const bool quadraticEAflag=false)
const float getEffectiveArea(float eta) const
std::vector< float > absEtaMin_
const bool quadraticEAflag_
std::vector< float > linearEffectiveAreaValues_
void checkConsistency() const
std::vector< float > quadraticEffectiveAreaValues_
const float getLinearEA(float eta) const
const std::string filename_