00001
00002 #include "RecoEcal/EgammaClusterAlgos/interface/EndcapPiZeroDiscriminatorAlgo.h"
00003 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00004 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00005
00006 #include "FWCore/ParameterSet/interface/FileInPath.h"
00007 #include <fstream>
00008 #include <iostream>
00009
00010 using namespace std;
00011
00012 EndcapPiZeroDiscriminatorAlgo::EndcapPiZeroDiscriminatorAlgo(double stripEnergyCut, int nStripCut, const string& path,
00013 DebugLevel_pi0 debugLevel = pINFO ) :
00014 preshStripEnergyCut_(stripEnergyCut), preshSeededNstr_(nStripCut), debugLevel_(debugLevel), pathToFiles_(path)
00015 {
00016
00017
00018 Nfiles_EB = 5;
00019 Nfiles_EE = 5;
00020 string file_pt[5] = {"20","30","40","50","60"};
00021 string file_barrel_pt[5] = {"20","30","40","50","60"};
00022
00023 string nn_paterns_file = "";
00024 for(int j=0;j<Nfiles_EE;j++) {
00025 nn_paterns_file = "endcapPiZeroDiscriminatorWeights_et"+file_pt[j]+".wts";
00026 edm::FileInPath WFile(pathToFiles_+nn_paterns_file);
00027 readWeightFile(WFile.fullPath().c_str());
00028
00029 EE_Layers = Layers;
00030 EE_Indim = Indim;
00031 EE_Hidden = Hidden;
00032 EE_Outdim = Outdim;
00033
00034 for(int i=0;i<Indim*Hidden;i++) I_H_Weight_all.push_back(I_H_Weight[i]);
00035 for(int i=0;i<Hidden;i++) H_Thresh_all.push_back(H_Thresh[i]);
00036 for(int i=0;i<Outdim*Hidden;i++) H_O_Weight_all.push_back(H_O_Weight[i]);
00037 for(int i=0;i<Outdim;i++) O_Thresh_all.push_back(O_Thresh[i]);
00038 }
00039
00040 for(int k=0;k<Nfiles_EB;k++) {
00041 nn_paterns_file = "barrelPiZeroDiscriminatorWeights_et"+file_barrel_pt[k]+".wts";
00042 edm::FileInPath WFile(pathToFiles_+nn_paterns_file);
00043 readWeightFile(WFile.fullPath().c_str());
00044
00045 EB_Layers = Layers;
00046 EB_Indim = Indim;
00047 EB_Hidden = Hidden;
00048 EB_Outdim = Outdim;
00049
00050 for(int i=0;i<Indim*Hidden;i++) I_H_Weight_all.push_back(I_H_Weight[i]);
00051 for(int i=0;i<Hidden;i++) H_Thresh_all.push_back(H_Thresh[i]);
00052 for(int i=0;i<Outdim*Hidden;i++) H_O_Weight_all.push_back(H_O_Weight[i]);
00053 for(int i=0;i<Outdim;i++) O_Thresh_all.push_back(O_Thresh[i]);
00054 }
00055 delete [] I_H_Weight;
00056 delete [] H_Thresh;
00057 delete [] H_O_Weight;
00058 delete [] O_Thresh;
00059 }
00060
00061
00062 vector<float> EndcapPiZeroDiscriminatorAlgo::findPreshVector(ESDetId strip, RecHitsMap *rechits_map,
00063 CaloSubdetectorTopology *topology_p)
00064 {
00065 vector<float> vout_stripE;
00066
00067 vout_stripE.clear();
00068
00069 vector<ESDetId> road_2d;
00070 road_2d.clear();
00071
00072 int plane = strip.plane();
00073
00074 if ( debugLevel_ <= pDEBUG ) {
00075 cout << "findPreshVectors: Preshower Seeded Algorithm - looking for clusters" << "n"
00076 << "findPreshVectors: Preshower is intersected at strip " << strip.strip() << ", at plane " << plane << endl;
00077 }
00078
00079 if ( strip == ESDetId(0) ) {
00080 for(int i=0;i<11;i++) {
00081 vout_stripE.push_back(-100.);
00082 }
00083 }
00084
00085
00086 road_2d.push_back(strip);
00087
00088
00089 EcalPreshowerNavigator navigator(strip, topology_p);
00090 navigator.setHome(strip);
00091
00092 findPi0Road(strip, navigator, plane, road_2d);
00093 if ( debugLevel_ <= pDEBUG ) cout << "findPreshVectors: Total number of strips in the central road: " << road_2d.size() << endl;
00094
00095
00096 RecHitsMap::iterator final_strip = rechits_map->end();
00097 final_strip--;
00098 ESDetId last_stripID = final_strip->first;
00099
00100 float E = 0;
00101 vector<ESDetId>::iterator itID;
00102 for (itID = road_2d.begin(); itID != road_2d.end(); itID++) {
00103 if ( debugLevel_ == pDEBUG ) cout << " findPreshVectors: ID = " << *itID << endl;
00104 RecHitsMap::iterator strip_it = rechits_map->find(*itID);
00105 if(goodPi0Strip(strip_it,last_stripID)) {
00106 E = strip_it->second.energy();
00107 } else E = 0;
00108 vout_stripE.push_back(E);
00109 if ( debugLevel_ == pDEBUG ) cout << " findPreshVectors: E = " << E << endl;
00110 }
00111
00112 return vout_stripE;
00113
00114 }
00115
00116
00117 bool EndcapPiZeroDiscriminatorAlgo::goodPi0Strip(RecHitsMap::iterator candidate_it, ESDetId lastID)
00118 {
00119 RecHitsMap::iterator candidate_tmp = candidate_it;
00120 candidate_tmp--;
00121 if ( debugLevel_ == pDEBUG ) {
00122 if (candidate_tmp->first == lastID )
00123 cout << " goodPi0Strip No such a strip in rechits_map " << endl;
00124 if (candidate_it->second.energy() <= preshStripEnergyCut_)
00125 cout << " goodPi0Strip Strip energy " << candidate_it->second.energy() <<" is below threshold " << endl;
00126 }
00127
00128 if ( (candidate_tmp->first == lastID ) ||
00129 (candidate_it->second.energy() <= preshStripEnergyCut_ ) )
00130 {
00131 return false;
00132 }
00133
00134 return true;
00135 }
00136
00137
00138 void EndcapPiZeroDiscriminatorAlgo::findPi0Road(ESDetId strip, EcalPreshowerNavigator& theESNav,
00139 int plane, vector<ESDetId>& vout) {
00140 if ( strip == ESDetId(0) ) return;
00141 ESDetId next;
00142 theESNav.setHome(strip);
00143
00144 if ( debugLevel_ <= pDEBUG ) cout << "findPi0Road: starts from strip " << strip << endl;
00145 if (plane == 1) {
00146
00147 int n_east= 0;
00148 if ( debugLevel_ == pDEBUG ) cout << " findPi0Road: Go to the East " << endl;
00149 while ( ((next=theESNav.east()) != ESDetId(0) && next != strip) ) {
00150 if ( debugLevel_ == pDEBUG ) cout << "findPi0Road: East: " << n_east << " current strip is " << next << endl;
00151 vout.push_back(next);
00152 ++n_east;
00153 if (n_east == preshSeededNstr_) break;
00154 }
00155
00156 int n_west= 0;
00157 if ( debugLevel_ == pDEBUG ) cout << " findPi0Road: Go to the West " << endl;
00158 theESNav.home();
00159 while ( ((next=theESNav.west()) != ESDetId(0) && next != strip )) {
00160 if ( debugLevel_ == pDEBUG ) cout << "findPi0Road: West: " << n_west << " current strip is " << next << endl;
00161 vout.push_back(next);
00162 ++n_west;
00163 if (n_west == preshSeededNstr_) break;
00164 }
00165 if ( debugLevel_ == pDEBUG ) cout << "findPi0Road: Total number of strips found in the road at 1-st plane is " << n_east+n_west << endl;
00166 }
00167 else if (plane == 2) {
00168
00169 int n_north= 0;
00170 if ( debugLevel_ == pDEBUG ) cout << " findPi0Road: Go to the North " << endl;
00171 while ( ((next=theESNav.north()) != ESDetId(0) && next != strip) ) {
00172 if ( debugLevel_ == pDEBUG ) cout << "findPi0Road: North: " << n_north << " current strip is " << next << endl;
00173 vout.push_back(next);
00174 ++n_north;
00175 if (n_north == preshSeededNstr_) break;
00176 }
00177
00178 int n_south= 0;
00179 if ( debugLevel_ == pDEBUG ) cout << " findPi0Road: Go to the South " << endl;
00180 theESNav.home();
00181 while ( ((next=theESNav.south()) != ESDetId(0) && next != strip) ) {
00182 if ( debugLevel_ == pDEBUG ) cout << "findPi0Road: South: " << n_south << " current strip is " << next << endl;
00183 vout.push_back(next);
00184 ++n_south;
00185 if (n_south == preshSeededNstr_) break;
00186 }
00187 if ( debugLevel_ == pDEBUG ) cout << "findPi0Road: Total number of strips found in the road at 2-nd plane is " << n_south+n_north << endl;
00188 }
00189 else {
00190 if ( debugLevel_ == pDEBUG ) cout << " findPi0Road: Wrong plane number, null cluster will be returned! " << endl;
00191 }
00192
00193 theESNav.home();
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 void EndcapPiZeroDiscriminatorAlgo::readWeightFile(const char *Weights_file){
00203 FILE *weights;
00204
00205 char *line;
00206 line = new char[80];
00207
00208 bool checkinit=false;
00209
00210
00211
00212 weights = fopen(Weights_file, "r");
00213 if ( debugLevel_ <= pDEBUG ) cout << " I opeded the Weights file = " << Weights_file << endl;
00214
00215 while( !feof(weights) ){
00216 fscanf(weights, "%s", line);
00217 if (line[0] == 'A') {
00218 fscanf(weights, "%d", &Layers);
00219 fscanf(weights, "%d", &Indim);
00220 fscanf(weights, "%d", &Hidden);
00221 fscanf(weights, "%d", &Outdim);
00222
00223 inp_var = Indim + 1;
00224
00225 I_H_Weight = new float[Indim*Hidden];
00226 H_Thresh = new float[Hidden];
00227 H_O_Weight = new float[Hidden*Outdim];
00228 O_Thresh = new float[Outdim];
00229 checkinit=true;
00230 }else if (line[0] == 'B') {
00231 assert(checkinit);
00232 for (int i = 0; i<Indim; i++){
00233 for (int j = 0; j<Hidden; j++){
00234 fscanf(weights, "%f", &I_H_Weight[i*Hidden+j]);
00235 }
00236 }
00237 }else if (line[0] == 'C'){
00238 assert(checkinit);
00239 for (int i = 0; i<Hidden; i++){
00240 fscanf(weights, "%f", &H_Thresh[i]);
00241 }
00242 }else if (line[0] == 'D'){
00243 assert(checkinit);
00244 for (int i = 0; i<Hidden*Outdim; i++){
00245 fscanf(weights, "%f", &H_O_Weight[i]);
00246 }
00247 }else if (line[0] == 'E'){
00248 assert(checkinit);
00249 for (int i = 0; i<Outdim; i++){
00250 fscanf(weights, "%f", &O_Thresh[i]);
00251
00252 }
00253 }
00254 else{cout << "Not a Net file of Corrupted Net file " << endl;
00255 }
00256 }
00257 fclose(weights);
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 float EndcapPiZeroDiscriminatorAlgo::getNNoutput(int sel_wfile)
00267 {
00268 float* I_SUM;
00269 float* OUT;
00270 float nnout=0.0;
00271 int mij;
00272
00273 I_SUM = new float[Hidden];
00274 OUT = new float[Outdim];
00275
00276 for(int k=0;k<Hidden;k++) I_SUM[k]=0.0;
00277 for(int k1=0;k1<Outdim;k1++) OUT[k1]=0.0;
00278
00279 for (int h = 0; h<Hidden; h++){
00280 mij = h - Hidden;
00281 for (int i = 0; i<Indim; i++){
00282 mij = mij + Hidden;
00283 I_SUM[h] += I_H_Weight_all[mij+sel_wfile*Indim*Hidden + barrelstart*Nfiles_EE*EE_Indim*EE_Hidden] * input_var[i];
00284 }
00285 I_SUM[h] += H_Thresh_all[h+sel_wfile*Hidden + barrelstart*Nfiles_EE*EE_Hidden];
00286 for (int o1 = 0; o1<Outdim; o1++) {
00287 OUT[o1] += H_O_Weight_all[barrelstart*Nfiles_EE*EE_Outdim*EE_Hidden + h*Outdim+o1 + sel_wfile*Outdim*Hidden]*Activation_fun(I_SUM[h]);
00288
00289 }
00290 }
00291 for (int o2 = 0; o2<Outdim; o2++){
00292 OUT[o2] += O_Thresh_all[barrelstart*Nfiles_EE*EE_Outdim + o2 + sel_wfile*Outdim];
00293 }
00294 nnout = Activation_fun(OUT[0]);
00295
00296 if ( debugLevel_ <= pDEBUG ) cout << "getNNoutput :: -> NNout = " << nnout << endl;
00297
00298 delete I_SUM;
00299 delete OUT;
00300 delete input_var;
00301
00302 return (nnout);
00303 }
00304
00305
00306 float EndcapPiZeroDiscriminatorAlgo::Activation_fun(float SUM){
00307 return( 1.0 / ( 1.0 + exp(-2.0*SUM) ) );
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 bool EndcapPiZeroDiscriminatorAlgo::calculateNNInputVariables(vector<float>& vph1, vector<float>& vph2,
00321 float pS1_max, float pS9_max, float pS25_max)
00322 {
00323 input_var = new float[EE_Indim];
00324
00325 bool valid_NNinput = true;
00326
00327 if ( debugLevel_ <= pDEBUG ) {
00328 cout << "Energies of the Preshower Strips in X plane = ( ";
00329 for(int i = 0; i<11;i++) {
00330 cout << " " << vph1[i];
00331 }
00332 cout << ")" << endl;
00333 cout << "Energies of the Preshower Strips in Y plane = ( ";
00334 for(int i = 0; i<11;i++) {
00335 cout << " " << vph2[i];
00336 }
00337 cout << ")" << endl;
00338 }
00339
00340 for(int k = 0; k<11; k++) {
00341 if(vph1[k] < 0 ) {
00342 if ( debugLevel_ <= pDEBUG ) {
00343 cout << "Oops!!! Preshower Info for strip : " << k << " of X plane Do not exists" << endl;
00344 }
00345 vph1[k] = 0.0;
00346 }
00347 if(vph2[k] < 0 ) {
00348 if ( debugLevel_ <= pDEBUG ) {
00349 cout << "Oops!!! Preshower Info for strip : " << k << " of Y plane Do not exists" << endl;
00350 }
00351 vph2[k] = 0.0;
00352 }
00353 }
00354 if ( debugLevel_ <= pDEBUG ) {
00355 cout << "After: Energies of the Preshower Strips in X plane = ( ";
00356 for(int i = 0; i<11;i++) {
00357 cout << " " << vph1[i];
00358 }
00359 cout << ")" << endl;
00360 cout << "After: Energies of the Preshower Strips in Y plane = ( ";
00361 for(int i = 0; i<11;i++) {
00362 cout << " " << vph2[i];
00363 }
00364 cout << ")" << endl;
00365 }
00366
00367
00368
00369
00370 for(int kk=0;kk<11;kk++){
00371 input_var[kk] = fabs(vph1[kk]/0.01);
00372 input_var[kk + 11] = fabs(vph2[kk]/0.02);
00373 if(input_var[kk] < 0.0001) input_var[kk] = 0.;
00374 if(input_var[kk + 11] < 0.0001) input_var[kk + 11] = 0.;
00375 }
00376 input_var[0] = fabs(input_var[0]/2.);
00377 input_var[1] = fabs(input_var[1]/2.);
00378 input_var[6] = fabs(input_var[6]/2.);
00379 input_var[11] = fabs(input_var[11]/2.);
00380 input_var[12] = fabs(input_var[12]/2.);
00381 input_var[17] = fabs(input_var[17]/2.);
00382
00383
00384
00385 input_var[22] = pS1_max/500.;
00386 input_var[23] = pS9_max/500.;
00387 input_var[24] = pS25_max/500.;
00388
00389 if ( debugLevel_ <= pDEBUG ) {
00390 cout << "S1/500. = " << input_var[22] << endl;
00391 cout << "S9/500. = " << input_var[23] << endl;
00392 cout << "S25/500. = " << input_var[24] << endl;
00393 }
00394 for(int i=0;i<EE_Indim;i++){
00395 if(input_var[i] > 1.0e+00) {
00396 valid_NNinput = false;
00397 break;
00398 }
00399 }
00400 if ( debugLevel_ <= pDEBUG ) {
00401 cout << " valid_NNinput = " << valid_NNinput << endl; }
00402 return valid_NNinput;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 void EndcapPiZeroDiscriminatorAlgo::calculateBarrelNNInputVariables(float et, double s1, double s9, double s25,
00414 double m2, double cee, double cep,double cpp,
00415 double s4, double s6, double ratio,
00416 double xcog, double ycog)
00417 {
00418 input_var = new float[EB_Indim];
00419
00420 double lam, lam1, lam2;
00421 if(xcog < 0.) input_var[0] = -xcog/s25; else input_var[0] = xcog/s25;
00422 if(ycog < 0.) input_var[6] = -ycog/s25; else input_var[6] = ycog/s25;
00423 input_var[1] = cee/0.0004;
00424 if(cpp<.001) input_var[2] = cpp/.001; else input_var[2] = 0.;
00425 if(s9!=0.) { input_var[3] = s1/s9; input_var[8] = s6/s9; input_var[10] = (m2+s1)/s9; }
00426 else { input_var[3] = 0.; input_var[8] = 0.; input_var[10] = 0.; }
00427 if(s25-s1>0.) input_var[4] = (s9-s1)/(s25-s1); else input_var[4] = 0.;
00428 if(s25>0.) input_var[5] = s4/s25; else input_var[5] = 0.;
00429 lam=sqrt((cee -cpp)*(cee -cpp)+4*cep*cep); lam1=(cee + cpp + lam)/2; lam2=(cee + cpp - lam)/2;
00430 if(lam1 == 0) input_var[9] = .0; else input_var[9] = lam2/lam1;
00431 if(s4!=0.) input_var[11] = (m2+s1)/s4; else input_var[11] = 0.;
00432 input_var[7] = ratio;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441 float EndcapPiZeroDiscriminatorAlgo::GetNNOutput(float EE_Et)
00442 {
00443 Layers = EE_Layers; Indim = EE_Indim; Hidden = EE_Hidden; Outdim = EE_Outdim; barrelstart = 0;
00444
00445 float nnout = -1;
00446
00447
00448 if ( debugLevel_ <= pDEBUG )cout << " EndcapPiZeroDiscriminatorAlgo::GetNNoutput :nn_invar_presh = " ;
00449 for(int k1=0;k1<Indim;k1++) {
00450 if ( debugLevel_ <= pDEBUG )cout << input_var[k1] << " " ;
00451 }
00452 if ( debugLevel_ <= pDEBUG )cout << endl;
00453
00454
00455 int sel_wfile;
00456 if(EE_Et<25.0) {sel_wfile = 0;}
00457 else if(EE_Et>=25.0 && EE_Et<35.0) {sel_wfile = 1;}
00458 else if(EE_Et>=35.0 && EE_Et<45.0) {sel_wfile = 2;}
00459 else if(EE_Et>=45.0 && EE_Et<55.0) {sel_wfile = 3;}
00460 else {sel_wfile = 4;}
00461
00462 if ( debugLevel_ <= pDEBUG ) {
00463 cout << " Et_SC = " << EE_Et << " and I select Weight file Number = " << sel_wfile << endl;
00464 }
00465
00466 nnout = getNNoutput(sel_wfile);
00467 if ( debugLevel_ <= pDEBUG ) cout << "===================> GetNNOutput : NNout = " << nnout << endl;
00468 return nnout;
00469 }
00470
00471
00472
00473
00474
00475
00476
00477 float EndcapPiZeroDiscriminatorAlgo::GetBarrelNNOutput(float EB_Et)
00478 {
00479
00480 Layers = EB_Layers; Indim = EB_Indim; Hidden = EB_Hidden; Outdim = EB_Outdim; barrelstart = 1;
00481
00482 float nnout = -1;
00483
00484
00485 if ( debugLevel_ <= pDEBUG )cout << " EndcapPiZeroDiscriminatorAlgo::GetBarrelNNoutput :nn_invar_presh = " ;
00486 for(int k1=0;k1<Indim;k1++) {
00487 if ( debugLevel_ <= pDEBUG )cout << input_var[k1] << " " ;
00488 }
00489 if ( debugLevel_ <= pDEBUG )cout << 1 << endl;
00490
00491
00492 int sel_wfile;
00493 if(EB_Et<25.0) {sel_wfile = 0;}
00494 else if(EB_Et>=25.0 && EB_Et<35.0) {sel_wfile = 1;}
00495 else if(EB_Et>=35.0 && EB_Et<45.0) {sel_wfile = 2;}
00496 else if(EB_Et>=45.0 && EB_Et<55.0) {sel_wfile = 3;}
00497 else {sel_wfile = 4;}
00498
00499 if ( debugLevel_ <= pDEBUG ) {
00500 cout << " E_SC = " << EB_Et << " and I select Weight file Number = " << sel_wfile << endl;
00501 }
00502
00503 nnout = getNNoutput(sel_wfile);
00504 if ( debugLevel_ <= pDEBUG ) cout << "===================> GetNNOutput : NNout = " << nnout << endl;
00505
00506 return nnout;
00507 }
00508