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