CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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;
11 
12 
13 const unsigned PFResolutionMap::lineSize_ = 10000;
14 
15 
16 PFResolutionMap::PFResolutionMap(const char* name, unsigned nbinseta,
17  double mineta, double maxeta,
18  unsigned nbinse,
19  double mine, double maxe, double value)
20  : TH2D(name, name, nbinseta, mineta, maxeta, nbinse, mine, maxe) {
21  // fNbinsEta(nbinseta), fMinEta(mineta), fMaxEta(maxeta),
22  // fNbinsE(nbinse), fMinE(mine), fMaxE(maxe) {
23 
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 
39 // void PFResolutionMap::Init(unsigned nbinseta,
40 // double mineta, double maxeta,
41 // unsigned nbinse,
42 // double mine, double maxe) {
43 // assert(mineta<maxeta);
44 // assert(mine<maxe);
45 
46 // unsigned nbins = nbinseta*nbinse;
47 // fData.reserve( nbins );
48 // for(unsigned i=0; i<nbins; i++) {
49 // fData.push_back(-1);
50 // }
51 
52 // // calculate lower bound of eta and e bins
53 // double binsize = (fMaxEta - fMinEta)/fNbinsEta;
54 // double lb = fMinEta;
55 // // cout<<"eta bins lower bounds : "<<endl;
56 // for(unsigned i=0; i<nbinseta; i++) {
57 // fEtaBinsLowerBounds[lb] = i;
58 // // cout<<i<<" "<<lb<<endl;
59 // lb += binsize;
60 // }
61 
62 // binsize = (fMaxE - fMinE)/fNbinsE;
63 // lb = fMinE;
64 // // cout<<"E bins lower bounds : "<<endl;
65 // for(unsigned i=0; i<nbinse; i++) {
66 // fEBinsLowerBounds[lb] = i;
67 // // cout<<i<<" "<<lb<<endl;
68 // lb += binsize;
69 // }
70 // }
71 
72 PFResolutionMap::PFResolutionMap(const char* name, const char* mapfile) {
73  SetTitle(name);
74  GetXaxis()->SetTitle("#eta");
75  GetYaxis()->SetTitle("E");
76  if( ! ReadMapFile(mapfile) ) {
77  string err = "PFResolutionMap::PFResolutionMap : cannot read file ";
78  err += mapfile;
79  throw invalid_argument(err);
80  }
81 }
82 
83 
84 bool PFResolutionMap::WriteMapFile(const char* mapfile) {
85 
86  // assert(fData.size() == fNbinsEta*fNbinsE);
87 
88 
89  // open the file
90 
91  ofstream outf(mapfile);
92  if( !outf.good() ) {
93  cout<<"PFResolutionMap::Write : cannot open file "<<mapfile<<endl;
94  return false;
95  }
96 
97  outf<<(*this)<<endl;
98  if(!outf.good() ) {
99  cerr<<"PFResolutionMap::Write : corrupted file "<<mapfile<<endl;
100  return false;
101  }
102  else {
103  mapFile_ = mapfile;
104  return true;
105  }
106 }
107 
108 
109 
110 bool PFResolutionMap::ReadMapFile(const char* mapfile) {
111 
112  // open the file
113 
114  ifstream inf(mapfile);
115  if( !inf.good() ) {
116  // cout<<"PFResolutionMap::Read : cannot open file "<<mapfile<<endl;
117  return false;
118  }
119 
120  // first data describes the map
121 
122  int nbinseta=0;
123  double mineta=0;
124  double maxeta=0;
125 
126  int nbinse=0;
127  double mine=0;
128  double maxe=0;
129 
130  inf>>nbinseta;
131  inf>>mineta;
132  inf>>maxeta;
133 
134  inf>>nbinse;
135  inf>>mine;
136  inf>>maxe;
137 
138  SetBins(nbinseta, mineta, maxeta, nbinse, mine, maxe);
139 
140  // Init(fNbinsEta,fMinEta,fMaxEta,fNbinsE,fMinE,fMaxE);
141 
142  // char data[lineSize_];
143  char s[lineSize_];
144  int pos=inf.tellg();
145 
146  // parse map data
147  int i=1;
148  do {
149  inf.seekg(pos);
150  inf.getline(s,lineSize_);
151 
152  pos = inf.tellg();
153 
154  if(string(s).empty()) {
155  continue; // remove empty lines
156  }
157 
158  istringstream lin(s);
159 
160  double dataw;
161  int j=1;
162  do {
163  lin>>dataw;
164  SetBinContent(j, i, dataw);
165  j++;
166  } while (lin.good() );
167  i++;
168  } while(inf.good());
169 
170  if(inf.eof()) {
171  mapFile_ = mapfile;
172  return true;
173  }
174  else {
175  // cout<<"error"<<endl;
176  return false;
177  }
178 
179  mapFile_ = mapfile;
180  return true;
181 }
182 
183 
184 
185 // int PFResolutionMap::FindBin(double eta, double e) const {
186 
187 // if(eta<fMinEta || eta>=fMaxEta) {
188 // cout<<"PFResolutionMap::FindBin "<<eta<<" out of eta bounds "<<fMinEta<<" "<<fMaxEta<<endl;
189 // return -1;
190 // }
191 // if(e<fMinE || e>=fMaxE) {
192 // cout<<"PFResolutionMap::FindBin "<<e<<" out of e bounds "<<fMinE<<" "<<fMaxE<<endl;
193 // return -1;
194 // }
195 
196 // map<double,unsigned >::const_iterator iteta =
197 // fEtaBinsLowerBounds.upper_bound( eta );
198 // iteta--;
199 
200 // // if( iteta != fEtaBinsLowerBounds.end() ) {
201 // // cout<<"eta lower bound "<<iteta->first<<" "<<iteta->second<<endl;
202 // // }
203 // // else return -1;
204 
205 // map<double,unsigned>::const_iterator ite =
206 // fEBinsLowerBounds.upper_bound( e );
207 // ite--;
208 // // if( ite != fEBinsLowerBounds.end() ) {
209 // // cout<<"e lower bound "<<ite->first<<" "<<ite->second<<endl;
210 // // }
211 // // else return -1;
212 
213 // // cout<<"returning "<<ite->second * fNbinsEta + iteta->second<<endl;
214 // // cout<<"returning "<<ite->second<<" "<<iteta->second<<endl;
215 
216 // return ite->second * fNbinsEta + iteta->second;
217 // }
218 
219 // void PFResolutionMap::Fill(double eta, double e, double res) {
220 
221 // unsigned bin = FindBin(eta, e);
222 // if( bin<0 || bin>fData.size() ) {
223 // // cout<<"PFResolutionMap::Fill : out of range " <<bin<<endl;
224 // return;
225 // }
226 
227 // fData[bin] = res;
228 // }
229 
230 
231 double PFResolutionMap::getRes(double eta, double phi, double e, int MapEta){
232  static double fMinEta = -2.95;
233  static double fMaxEta = 2.95;
234  static double fMinE=0;
235  static double fMaxE=100;
236 
237  if( eta<fMinEta ) eta = fMinEta+0.001;
238  if( eta>fMaxEta ) eta = fMaxEta-0.001;
239 
240  if( e<fMinE ) e = fMinE+0.001;
241  if( e>fMaxE ) e = fMaxE-0.001;
242 
243  unsigned bin = FindBin(TMath::Abs(eta),e);
244 
245  double res= GetBinContent(bin);
246  if(MapEta>-1){
247  if((eta<1.48) && IsInAPhiCrack(phi,eta)) {
248  if(MapEta==1) res *= 1.88;
249  else res *= 1.65;
250  }
251  }
252  return res;
253 }
254 
255 
256 
257 int PFResolutionMap::FindBin(double eta, double e) {
258  if(e >= GetYaxis()->GetXmax() )
259  e = GetYaxis()->GetXmax() - 0.001;
260 
261  return TH2D::FindBin(eta,e);
262 }
263 
264 
265 
266 ostream& operator<<(ostream& outf, const PFResolutionMap& rm) {
267 
268  if(!outf.good() ) return outf;
269 
270  // first data describes the map
271  outf<<rm.GetNbinsX()<<endl;
272  outf<<rm.GetXaxis()->GetXmin()<<endl;
273  outf<<rm.GetXaxis()->GetXmax()<<endl;
274 
275  outf<<rm.GetNbinsY()<<endl;
276  outf<<rm.GetYaxis()->GetXmin()<<endl;
277  outf<<rm.GetYaxis()->GetXmax()<<endl;
278 
279  for(int ie=1; ie<=rm.GetNbinsY(); ie++) {
280  for(int ieta=1; ieta<=rm.GetNbinsX(); ieta++) {
281  outf<<rm.GetBinContent(ieta,ie)<<"\t";
282  }
283  outf<<endl;
284  }
285 
286  return outf;
287 }
288 
290  double dminPhi = dCrackPhi(phi,eta);
291  bool Is = (TMath::Abs(dminPhi)<0.005);
292  return Is;
293 }
294 
295 //useful to compute the signed distance to the closest crack in the barrel
296 double PFResolutionMap::minimum(double a,double b){
297  if(TMath::Abs(b)<TMath::Abs(a)) a=b;
298  return a;
299 }
300 
301 
302 #ifndef M_PI
303 #define M_PI 3.14159265358979323846
304 #endif
305 
306 //compute the unsigned distance to the closest phi-crack in the barrel
307 double PFResolutionMap::dCrackPhi(double phi, double eta){
308 
309  static double pi= M_PI;// 3.14159265358979323846;
310 
311  //Location of the 18 phi-cracks
312  static std::vector<double> cPhi;
313  cPhi.resize(18,0);
314  cPhi[0]=2.97025;
315  for(unsigned i=1;i<=17;i++) cPhi[i]=cPhi[0]-2*i*pi/18;
316 
317  //Shift of this location if eta<0
318  static double delta_cPhi=0.00638;
319 
320  double m; //the result
321 
322  //the location is shifted
323  if(eta<0){
324  phi +=delta_cPhi;
325  if(phi>pi) phi-=2*pi;
326  }
327  if (phi>=-pi && phi<=pi){
328 
329  //the problem of the extrema
330  if (phi<cPhi[17] || phi>=cPhi[0]){
331  if (phi<0) phi+= 2*pi;
332  m = minimum(phi -cPhi[0],phi-cPhi[17]-2*pi);
333  }
334 
335  //between these extrema...
336  else{
337  bool OK = false;
338  unsigned i=16;
339  while(!OK){
340  if (phi<cPhi[i]){
341  m=minimum(phi-cPhi[i+1],phi-cPhi[i]);
342  OK=true;
343  }
344  else i-=1;
345  }
346  }
347  }
348  else{
349  m=0.; //if there is a problem, we assum that we are in a crack
350  std::cout<<"Problem in dminphi"<<std::endl;
351  }
352  if(eta<0) m=-m; //because of the disymetry
353  return m;
354 }
int i
Definition: DBlmapReader.cc:9
double minimum(double a, double b)
pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:70
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)
int FindBin(double eta, double e)
extrapolation requires overloading of this function
T eta() const
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
std::string mapFile_
string rm
Definition: submit.py:76
string inf
Definition: EcalCondDB.py:94
int j
Definition: DBlmapReader.cc:9
double getRes(double eta, double phi, double e, int MapEta=-1)
bool IsInAPhiCrack(double phi, double eta)
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
PFResolutionMap()
default constructor
tuple cout
Definition: gather_cfg.py:121
double pi
Resolution Map (resolution as a function of eta and E)
Definition: DDAxes.h:10