Go to the documentation of this file.00001 #include "RecoParticleFlow/PFClusterTools/interface/PFResolutionMap.h"
00002 #include <fstream>
00003 #include <sstream>
00004 #include <algorithm>
00005 #include <functional>
00006 #include <stdexcept>
00007 #include <TFile.h>
00008 #include <TMath.h>
00009 #include <vector>
00010 using namespace std;
00011
00012
00013 const unsigned PFResolutionMap::lineSize_ = 10000;
00014
00015
00016 PFResolutionMap::PFResolutionMap(const char* name, unsigned nbinseta,
00017 double mineta, double maxeta,
00018 unsigned nbinse,
00019 double mine, double maxe, double value)
00020 : TH2D(name, name, nbinseta, mineta, maxeta, nbinse, mine, maxe) {
00021
00022
00023
00024 GetXaxis()->SetTitle("#eta");
00025 GetYaxis()->SetTitle("E");
00026
00027 if(value>0) {
00028 for(int ie=1; ie<=GetNbinsY(); ie++) {
00029 for(int ieta=1; ieta<=GetNbinsX(); ieta++) {
00030 SetBinContent(ieta,ie, value);
00031 }
00032 }
00033 }
00034
00035
00036
00037 }
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 PFResolutionMap::PFResolutionMap(const char* name, const char* mapfile) {
00073 SetTitle(name);
00074 GetXaxis()->SetTitle("#eta");
00075 GetYaxis()->SetTitle("E");
00076 if( ! ReadMapFile(mapfile) ) {
00077 string err = "PFResolutionMap::PFResolutionMap : cannot read file ";
00078 err += mapfile;
00079 throw invalid_argument(err);
00080 }
00081 }
00082
00083
00084 bool PFResolutionMap::WriteMapFile(const char* mapfile) {
00085
00086
00087
00088
00089
00090
00091 ofstream outf(mapfile);
00092 if( !outf.good() ) {
00093 cout<<"PFResolutionMap::Write : cannot open file "<<mapfile<<endl;
00094 return false;
00095 }
00096
00097 outf<<(*this)<<endl;
00098 if(!outf.good() ) {
00099 cerr<<"PFResolutionMap::Write : corrupted file "<<mapfile<<endl;
00100 return false;
00101 }
00102 else {
00103 mapFile_ = mapfile;
00104 return true;
00105 }
00106 }
00107
00108
00109
00110 bool PFResolutionMap::ReadMapFile(const char* mapfile) {
00111
00112
00113
00114 ifstream inf(mapfile);
00115 if( !inf.good() ) {
00116
00117 return false;
00118 }
00119
00120
00121
00122 int nbinseta=0;
00123 double mineta=0;
00124 double maxeta=0;
00125
00126 int nbinse=0;
00127 double mine=0;
00128 double maxe=0;
00129
00130 inf>>nbinseta;
00131 inf>>mineta;
00132 inf>>maxeta;
00133
00134 inf>>nbinse;
00135 inf>>mine;
00136 inf>>maxe;
00137
00138 SetBins(nbinseta, mineta, maxeta, nbinse, mine, maxe);
00139
00140
00141
00142
00143 char s[lineSize_];
00144 int pos=inf.tellg();
00145
00146
00147 int i=1;
00148 do {
00149 inf.seekg(pos);
00150 inf.getline(s,lineSize_);
00151
00152 pos = inf.tellg();
00153
00154 if(string(s).empty()) {
00155 continue;
00156 }
00157
00158 istringstream lin(s);
00159
00160 double dataw;
00161 int j=1;
00162 do {
00163 lin>>dataw;
00164 SetBinContent(j, i, dataw);
00165 j++;
00166 } while (lin.good() );
00167 i++;
00168 } while(inf.good());
00169
00170 if(inf.eof()) {
00171 mapFile_ = mapfile;
00172 return true;
00173 }
00174 else {
00175
00176 return false;
00177 }
00178
00179 mapFile_ = mapfile;
00180 return true;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 double PFResolutionMap::getRes(double eta, double phi, double e, int MapEta){
00232 static double fMinEta = -2.95;
00233 static double fMaxEta = 2.95;
00234 static double fMinE=0;
00235 static double fMaxE=100;
00236
00237 if( eta<fMinEta ) eta = fMinEta+0.001;
00238 if( eta>fMaxEta ) eta = fMaxEta-0.001;
00239
00240 if( e<fMinE ) e = fMinE+0.001;
00241 if( e>fMaxE ) e = fMaxE-0.001;
00242
00243 unsigned bin = FindBin(TMath::Abs(eta),e);
00244
00245 double res= GetBinContent(bin);
00246 if(MapEta>-1){
00247 if((eta<1.48) && IsInAPhiCrack(phi,eta)) {
00248 if(MapEta==1) res *= 1.88;
00249 else res *= 1.65;
00250 }
00251 }
00252 return res;
00253 }
00254
00255
00256
00257 int PFResolutionMap::FindBin(double eta, double e) {
00258 if(e >= GetYaxis()->GetXmax() )
00259 e = GetYaxis()->GetXmax() - 0.001;
00260
00261 return TH2D::FindBin(eta,e);
00262 }
00263
00264
00265
00266 ostream& operator<<(ostream& outf, const PFResolutionMap& rm) {
00267
00268 if(!outf.good() ) return outf;
00269
00270
00271 outf<<rm.GetNbinsX()<<endl;
00272 outf<<rm.GetXaxis()->GetXmin()<<endl;
00273 outf<<rm.GetXaxis()->GetXmax()<<endl;
00274
00275 outf<<rm.GetNbinsY()<<endl;
00276 outf<<rm.GetYaxis()->GetXmin()<<endl;
00277 outf<<rm.GetYaxis()->GetXmax()<<endl;
00278
00279 for(int ie=1; ie<=rm.GetNbinsY(); ie++) {
00280 for(int ieta=1; ieta<=rm.GetNbinsX(); ieta++) {
00281 outf<<rm.GetBinContent(ieta,ie)<<"\t";
00282 }
00283 outf<<endl;
00284 }
00285
00286 return outf;
00287 }
00288
00289 bool PFResolutionMap::IsInAPhiCrack(double phi, double eta){
00290 double dminPhi = dCrackPhi(phi,eta);
00291 bool Is = (TMath::Abs(dminPhi)<0.005);
00292 return Is;
00293 }
00294
00295
00296 double PFResolutionMap::minimum(double a,double b){
00297 if(TMath::Abs(b)<TMath::Abs(a)) a=b;
00298 return a;
00299 }
00300
00301
00302 #ifndef M_PI
00303 #define M_PI 3.14159265358979323846
00304 #endif
00305
00306
00307 double PFResolutionMap::dCrackPhi(double phi, double eta){
00308
00309 static double pi= M_PI;
00310
00311
00312 static std::vector<double> cPhi;
00313 cPhi.resize(18,0);
00314 cPhi[0]=2.97025;
00315 for(unsigned i=1;i<=17;i++) cPhi[i]=cPhi[0]-2*i*pi/18;
00316
00317
00318 static double delta_cPhi=0.00638;
00319
00320 double m;
00321
00322
00323 if(eta<0){
00324 phi +=delta_cPhi;
00325 if(phi>pi) phi-=2*pi;
00326 }
00327 if (phi>=-pi && phi<=pi){
00328
00329
00330 if (phi<cPhi[17] || phi>=cPhi[0]){
00331 if (phi<0) phi+= 2*pi;
00332 m = minimum(phi -cPhi[0],phi-cPhi[17]-2*pi);
00333 }
00334
00335
00336 else{
00337 bool OK = false;
00338 unsigned i=16;
00339 while(!OK){
00340 if (phi<cPhi[i]){
00341 m=minimum(phi-cPhi[i+1],phi-cPhi[i]);
00342 OK=true;
00343 }
00344 else i-=1;
00345 }
00346 }
00347 }
00348 else{
00349 m=0.;
00350 std::cout<<"Problem in dminphi"<<std::endl;
00351 }
00352 if(eta<0) m=-m;
00353 return m;
00354 }