CMS 3D CMS Logo

PFResolutionMap.cc
Go to the documentation of this file.
2 #include <fstream>
3 #include <sstream>
4 #include <algorithm>
5 #include <functional>
6 #include <stdexcept>
7 #include <TFile.h>
8 #include <TMath.h>
9 #include <vector>
10 using namespace std;
12 
13 const unsigned PFResolutionMap::lineSize_ = 10000;
14 
16  unsigned nbinseta,
17  double mineta,
18  double maxeta,
19  unsigned nbinse,
20  double mine,
21  double maxe,
22  double value)
23  : TH2D(name, name, nbinseta, mineta, maxeta, nbinse, mine, maxe) {
24  GetXaxis()->SetTitle("#eta");
25  GetYaxis()->SetTitle("E");
26 
27  if (value > 0) {
28  for (int ie = 1; ie <= GetNbinsY(); ie++) {
29  for (int ieta = 1; ieta <= GetNbinsX(); ieta++) {
30  SetBinContent(ieta, ie, value);
31  }
32  }
33  }
34  // cout<<"creating resolution map "<<endl;
35  // Print("all");
36 }
37 
38 PFResolutionMap::PFResolutionMap(const char* name, const char* mapfile) {
39  SetTitle(name);
40  GetXaxis()->SetTitle("#eta");
41  GetYaxis()->SetTitle("E");
42  if (!ReadMapFile(mapfile)) {
43  string err = "PFResolutionMap::PFResolutionMap : cannot read file ";
44  err += mapfile;
45  throw invalid_argument(err);
46  }
47 }
48 
49 bool PFResolutionMap::WriteMapFile(const char* mapfile) {
50  // assert(fData.size() == fNbinsEta*fNbinsE);
51 
52  // open the file
53 
54  std::ofstream outf(mapfile);
55  if (!outf.good()) {
56  edm::LogWarning("PFResolutionMap::Write") << " : cannot open file " << mapfile;
57  return false;
58  }
59 
60  outf << (*this) << endl;
61  if (!outf.good()) {
62  edm::LogError("PFResolutionMap::Write") << " : corrupted file " << mapfile;
63  return false;
64  } else {
65  mapFile_ = mapfile;
66  return true;
67  }
68 }
69 
70 bool PFResolutionMap::ReadMapFile(const char* mapfile) {
71  // open the file
72 
73  std::ifstream inf(mapfile);
74  if (!inf.good()) {
75  // cout<<"PFResolutionMap::Read : cannot open file "<<mapfile<<endl;
76  return false;
77  }
78 
79  // first data describes the map
80 
81  int nbinseta = 0;
82  double mineta = 0;
83  double maxeta = 0;
84 
85  int nbinse = 0;
86  double mine = 0;
87  double maxe = 0;
88 
89  inf >> nbinseta;
90  inf >> mineta;
91  inf >> maxeta;
92 
93  inf >> nbinse;
94  inf >> mine;
95  inf >> maxe;
96 
97  SetBins(nbinseta, mineta, maxeta, nbinse, mine, maxe);
98 
99  char s[lineSize_];
100  int pos = inf.tellg();
101 
102  // parse map data
103  int i = 1;
104  do {
105  inf.seekg(pos);
106  inf.getline(s, lineSize_);
107 
108  pos = inf.tellg();
109 
110  if (string(s).empty()) {
111  continue; // remove empty lines
112  }
113 
114  istringstream lin(s);
115 
116  double dataw;
117  int j = 1;
118  do {
119  lin >> dataw;
120  SetBinContent(j, i, dataw);
121  j++;
122  } while (lin.good());
123  i++;
124  } while (inf.good());
125 
126  if (inf.eof()) {
127  mapFile_ = mapfile;
128  return true;
129  } else {
130  // cout<<"error"<<endl;
131  return false;
132  }
133 
134  mapFile_ = mapfile;
135  return true;
136 }
137 
138 double PFResolutionMap::getRes(double eta, double phi, double e, int MapEta) {
139  constexpr double fMinEta = -2.95;
140  constexpr double fMaxEta = 2.95;
141  constexpr double fMinE = 0;
142  constexpr double fMaxE = 100;
143 
144  if (eta < fMinEta)
145  eta = fMinEta + 0.001;
146  if (eta > fMaxEta)
147  eta = fMaxEta - 0.001;
148 
149  if (e < fMinE)
150  e = fMinE + 0.001;
151  if (e > fMaxE)
152  e = fMaxE - 0.001;
153 
154  unsigned bin = FindBin(TMath::Abs(eta), e);
155 
156  double res = GetBinContent(bin);
157  if (MapEta > -1) {
158  if ((eta < 1.48) && IsInAPhiCrack(phi, eta)) {
159  if (MapEta == 1)
160  res *= 1.88;
161  else
162  res *= 1.65;
163  }
164  }
165  return res;
166 }
167 
168 int PFResolutionMap::FindBin(double eta, double e, double z) {
169  if (e >= GetYaxis()->GetXmax())
170  e = GetYaxis()->GetXmax() - 0.001;
171 
172  return TH2D::FindBin(eta, e);
173 }
174 
175 ostream& operator<<(ostream& outf, const PFResolutionMap& rm) {
176  if (!outf.good())
177  return outf;
178 
179  // first data describes the map
180  outf << rm.GetNbinsX() << endl;
181  outf << rm.GetXaxis()->GetXmin() << endl;
182  outf << rm.GetXaxis()->GetXmax() << endl;
183 
184  outf << rm.GetNbinsY() << endl;
185  outf << rm.GetYaxis()->GetXmin() << endl;
186  outf << rm.GetYaxis()->GetXmax() << endl;
187 
188  for (int ie = 1; ie <= rm.GetNbinsY(); ie++) {
189  for (int ieta = 1; ieta <= rm.GetNbinsX(); ieta++) {
190  outf << rm.GetBinContent(ieta, ie) << "\t";
191  }
192  outf << endl;
193  }
194 
195  return outf;
196 }
197 
198 bool PFResolutionMap::IsInAPhiCrack(double phi, double eta) {
199  double dminPhi = dCrackPhi(phi, eta);
200  bool Is = (TMath::Abs(dminPhi) < 0.005);
201  return Is;
202 }
203 
204 //useful to compute the signed distance to the closest crack in the barrel
205 double PFResolutionMap::minimum(double a, double b) {
206  if (TMath::Abs(b) < TMath::Abs(a))
207  a = b;
208  return a;
209 }
210 
211 #ifndef M_PI
212 #define M_PI 3.14159265358979323846
213 #endif
214 
215 //compute the unsigned distance to the closest phi-crack in the barrel
216 double PFResolutionMap::dCrackPhi(double phi, double eta) {
217  constexpr double pi = M_PI; // 3.14159265358979323846;
218  constexpr double twopi = 2. * pi;
219  constexpr double twopiO18 = pi / 9;
220 
221  //Location of the 18 phi-cracks
222  constexpr double c0 = 2.97025;
223  constexpr std::array<double, 18> cPhi{{c0,
224  c0 - twopiO18,
225  c0 - 2 * twopiO18,
226  c0 - 3 * twopiO18,
227  c0 - 4 * twopiO18,
228  c0 - 5 * twopiO18,
229  c0 - 6 * twopiO18,
230  c0 - 7 * twopiO18,
231  c0 - 8 * twopiO18,
232  c0 - 9 * twopiO18,
233  c0 - 10 * twopiO18,
234  c0 - 11 * twopiO18,
235  c0 - 12 * twopiO18,
236  c0 - 13 * twopiO18,
237  c0 - 14 * twopiO18,
238  c0 - 15 * twopiO18,
239  c0 - 16 * twopiO18,
240  c0 - 17 * twopiO18}};
241 
242  //Shift of this location if eta<0
243  constexpr double delta_cPhi = 0.00638;
244 
245  double m; //the result
246 
247  //the location is shifted
248  if (eta < 0) {
249  phi += delta_cPhi;
250  if (phi > pi)
251  phi -= twopi;
252  }
253  if (phi >= -pi && phi <= pi) {
254  //the problem of the extrema
255  if (phi < cPhi[17] || phi >= cPhi[0]) {
256  if (phi < 0)
257  phi += twopi;
258  m = minimum(phi - cPhi[0], phi - cPhi[17] - twopi);
259  }
260 
261  //between these extrema...
262  else {
263  bool OK = false;
264  unsigned i = 16;
265  while (!OK) {
266  if (phi < cPhi[i]) {
267  m = minimum(phi - cPhi[i + 1], phi - cPhi[i]);
268  OK = true;
269  } else
270  i -= 1;
271  }
272  }
273  } else {
274  m = 0.; //if there is a problem, we assum that we are in a crack
275  edm::LogWarning("PFResolutionMap:Problem") << "Problem in dminphi";
276  }
277  if (eta < 0)
278  m = -m; //because of the disymetry
279  return m;
280 }
def rm(path, rec=False)
Definition: eostools.py:363
double minimum(double a, double b)
bool ReadMapFile(const char *mapfile)
read text file
#define M_PI
double dCrackPhi(double phi, double eta)
static const unsigned lineSize_
bool WriteMapFile(const char *mapfile)
Log< level::Error, false > LogError
ostream & operator<<(ostream &outf, const PFResolutionMap &rm)
float float float z
Definition: Electron.h:6
const Double_t pi
std::string mapFile_
Definition: value.py:1
double getRes(double eta, double phi, double e, int MapEta=-1)
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:126
bool IsInAPhiCrack(double phi, double eta)
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
PFResolutionMap()
default constructor
int FindBin(double eta, double e, double z=0) override
extrapolation requires overloading of this function
Log< level::Warning, false > LogWarning
Resolution Map (resolution as a function of eta and E)