18 preshStripEnergyCut_(stripEnergyCut), preshSeededNstr_(nStripCut)
22 constexpr std::array<char const*,Nfiles_EE> file_pt{ {
"20",
"30",
"40",
"50",
"60"} };
23 constexpr std::array<char const*,5> file_barrel_pt { {
"20",
"30",
"40",
"50",
"60"} };
25 for(
auto ptName: file_pt) {
27 string nn_paterns_file =
"endcapPiZeroDiscriminatorWeights_et";
28 nn_paterns_file +=ptName;
29 nn_paterns_file +=
".wts";
31 readWeightFile(WFile.fullPath().c_str(), EE_Layers, EE_Indim, EE_Hidden,EE_Outdim);
34 for(
auto ptName: file_barrel_pt) {
35 string nn_paterns_file =
"barrelPiZeroDiscriminatorWeights_et";
36 nn_paterns_file +=ptName;
37 nn_paterns_file +=
".wts";
39 readWeightFile(WFile.fullPath().c_str(), EB_Layers, EB_Indim, EB_Hidden, EB_Outdim);
47 vector<float> vout_stripE;
50 if ( rechits_map->empty() ) {
51 edm::LogWarning(
"EndcapPiZeroDiscriminatorAlgo") <<
"RecHitsMap has size 0.";
57 vector<ESDetId> road_2d;
60 int plane = strip.
plane();
62 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPreshVectors: Preshower Seeded Algorithm - looking for clusters" <<
"n"<<
"findPreshVectors: Preshower is intersected at strip " << strip.
strip() <<
", at plane " << plane ;
66 for(
int i=0;
i<11;
i++) {
67 vout_stripE.push_back(-100.);
72 road_2d.push_back(strip);
78 findPi0Road(strip, navigator, plane, road_2d);
80 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo:findPreshVectors: Total number of strips in the central road: " << road_2d.size() ;
83 RecHitsMap::iterator final_strip = rechits_map->end();
87 ESDetId last_stripID = final_strip->first;
89 vector<ESDetId>::iterator itID;
90 for (itID = road_2d.begin(); itID != road_2d.end(); itID++) {
91 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPreshVectors: ID = " << *itID ;
94 RecHitsMap::iterator strip_it = rechits_map->find(*itID);
95 if(goodPi0Strip(strip_it,last_stripID)) {
96 E = strip_it->second.energy();
98 vout_stripE.push_back(E);
99 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPreshVectors: E = " << E ;
106 vector<float> vout_ElevenStrips_Energy;
107 for(
int i=0;
i<11;
i++)
109 vout_ElevenStrips_Energy.push_back(0.);
112 for(
unsigned int i=0;
i<vout_stripE.size();
i++)
114 vout_ElevenStrips_Energy[
i] = vout_stripE.at(
i);
118 return vout_ElevenStrips_Energy;
126 RecHitsMap::iterator candidate_tmp = candidate_it;
130 if ( candidate_tmp->first == lastID )
132 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: goodPi0Strip No such a strip in rechits_map " ;
135 else if (candidate_it->second.energy() <= preshStripEnergyCut_ )
137 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: goodPi0Strip Strip energy " << candidate_it->second.energy() <<
" is below threshold " ;
146 int plane, vector<ESDetId>& vout) {
147 if ( strip ==
ESDetId(0) )
return;
150 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: starts from strip " <<
strip ;
155 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Go to the East " ;
158 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: East: " << n_east <<
" current strip is " <<
next ;
160 vout.push_back(next);
162 if (n_east == preshSeededNstr_)
break;
166 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Go to the West " ;
170 LogTrace(
"EcalClusters") <<
"findPi0Road: West: " << n_west <<
" current strip is " <<
next ;
172 vout.push_back(next);
174 if (n_west == preshSeededNstr_)
break;
176 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Total number of strips found in the road at 1-st plane is " << n_east+n_west ;
179 else if (plane == 2) {
182 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Go to the North " ;
185 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: North: " << n_north <<
" current strip is " <<
next;
187 vout.push_back(next);
189 if (n_north == preshSeededNstr_)
break;
193 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Go to the South " ;
197 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: South: " << n_south <<
" current strip is " <<
next ;
199 vout.push_back(next);
201 if (n_south == preshSeededNstr_)
break;
203 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Total number of strips found in the road at 2-nd plane is " << n_south+n_north ;
207 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: findPi0Road: Wrong plane number, null cluster will be returned! " ;
225 bool checkinit=
false;
229 weights = fopen(Weights_file,
"r");
230 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: I opeded the Weights file = " << Weights_file ;
231 if(weights ==
nullptr) {
232 throw cms::Exception(
"MissingWeightFile")<<
"Could not open the weights file: "<<Weights_file;
235 const auto I_H_W_offset = I_H_Weight_all.size();
236 const auto H_O_W_offset = H_O_Weight_all.size();
237 const auto H_T_offset = H_Thresh_all.size();
238 const auto O_T_offset = O_Thresh_all.size();
240 while( !feof(weights) ){
241 fscanf(weights,
"%s", line);
242 if (line[0] ==
'A') {
243 fscanf(weights,
"%d", &Layers);
244 fscanf(weights,
"%d", &Indim);
245 fscanf(weights,
"%d", &Hidden);
246 fscanf(weights,
"%d", &Outdim);
248 I_H_Weight_all.resize(I_H_W_offset+Indim*Hidden);
249 H_Thresh_all.resize(H_T_offset+Hidden);
250 H_O_Weight_all.resize(H_O_W_offset+Hidden*Outdim);
251 O_Thresh_all.resize(O_T_offset+Outdim);
253 }
else if (line[0] ==
'B') {
255 for (
int i = 0;
i<Indim;
i++){
256 for (
int j = 0; j<Hidden; j++){
257 fscanf(weights,
"%f", &I_H_Weight_all[I_H_W_offset+
i*Hidden+j]);
260 }
else if (line[0] ==
'C'){
262 for (
int i = 0;
i<Hidden;
i++){
263 fscanf(weights,
"%f", &H_Thresh_all[H_T_offset+
i]);
265 }
else if (line[0] ==
'D'){
267 for (
int i = 0;
i<Hidden*Outdim;
i++){
268 fscanf(weights,
"%f", &H_O_Weight_all[H_O_W_offset+
i]);
270 }
else if (line[0] ==
'E'){
272 for (
int i = 0;
i<Outdim;
i++){
273 fscanf(weights,
"%f", &O_Thresh_all[O_T_offset+
i]);
277 else{
edm::LogError(
"EEPi0Discrim")<<
"EndcapPiZeroDiscriminatorAlgo: Not a Net file of Corrupted Net file " << endl;
294 std::vector<float> I_SUM(
size_t(Hidden), 0.0);
295 std::vector<float>
OUT(
size_t(Outdim), 0.0);
297 for (
int h = 0;
h<Hidden;
h++){
299 for (
int i = 0;
i<Indim;
i++){
301 I_SUM[
h] += I_H_Weight_all[mij+sel_wfile*Indim*Hidden + barrelstart*Nfiles_EE*EE_Indim*EE_Hidden] * input_var[
i];
303 I_SUM[
h] += H_Thresh_all[
h+sel_wfile*Hidden + barrelstart*Nfiles_EE*EE_Hidden];
304 for (
int o1 = 0;
o1<Outdim;
o1++) {
305 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]);
309 for (
int o2 = 0; o2<Outdim; o2++){
310 OUT[o2] += O_Thresh_all[barrelstart*Nfiles_EE*EE_Outdim + o2 + sel_wfile*Outdim];
312 nnout = Activation_fun(OUT[0]);
313 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: getNNoutput :: -> NNout = " << nnout ;
322 return( 1.0 / ( 1.0 +
exp(-2.0*SUM) ) );
336 float pS1_max,
float pS9_max,
float pS25_max,
int EScorr)
338 input_var.resize(EE_Indim);
339 bool valid_NNinput =
true;
352 for(
int k = 0;
k<11;
k++) {
355 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: Oops!!! Preshower Info for strip : " <<
k <<
" of X plane Do not exists" ;
360 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: Oops!!! Preshower Info for strip : " <<
k <<
" of Y plane Do not exists" ;
382 input_var[
kk] = fabs(vph1[
kk]/0.01);
383 input_var[
kk + 11] = fabs(vph2[
kk]/0.02);
384 if(input_var[
kk] < 0.0001) input_var[
kk] = 0.;
385 if(input_var[
kk + 11] < 0.0001) input_var[
kk + 11] = 0.;
387 input_var[0] = fabs(input_var[0]/2.);
388 input_var[1] = fabs(input_var[1]/2.);
389 input_var[6] = fabs(input_var[6]/2.);
390 input_var[11] = fabs(input_var[11]/2.);
391 input_var[12] = fabs(input_var[12]/2.);
392 input_var[17] = fabs(input_var[17]/2.);
397 input_var[0] -= 0.05;
398 input_var[1] -= 0.035;
399 input_var[2] -= 0.035;
400 input_var[3] -= 0.02;
401 input_var[4] -= 0.015;
402 input_var[5] -= 0.0075;
403 input_var[6] -= 0.035;
404 input_var[7] -= 0.035;
405 input_var[8] -= 0.02;
406 input_var[9] -= 0.015;
407 input_var[10] -= 0.0075;
409 input_var[11] -= 0.05;
410 input_var[12] -= 0.035;
411 input_var[13] -= 0.035;
412 input_var[14] -= 0.02;
413 input_var[15] -= 0.015;
414 input_var[16] -= 0.0075;
415 input_var[17] -= 0.035;
416 input_var[18] -= 0.035;
417 input_var[19] -= 0.02;
418 input_var[20] -= 0.015;
419 input_var[21] -= 0.0075;
421 for(
int kk1=0;kk1<22;kk1++){
422 if(input_var[kk1] < 0 ) input_var[kk1] = 0.0;
427 float ECAL_norm_factor = 500.;
428 if(pS25_max>500&&pS25_max<=1000) ECAL_norm_factor = 1000;
429 if(pS25_max>1000) ECAL_norm_factor = 7000;
431 input_var[22] = pS1_max/ECAL_norm_factor;
432 input_var[23] = pS9_max/ECAL_norm_factor;
433 input_var[24] = pS25_max/ECAL_norm_factor;
435 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: S1/ECAL_norm_factor = " << input_var[22];
436 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: S9/ECAL_norm_factor = " << input_var[23];
437 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: S25/ECAL_norm_factor = " << input_var[24] ;
439 for(
int i=0;
i<EE_Indim;
i++){
440 if(input_var[
i] > 1.0
e+00) {
441 valid_NNinput =
false;
446 LogTrace(
"EcalClusters") <<
" valid_NNinput = " << valid_NNinput ;
449 return valid_NNinput;
461 double m2,
double cee,
double cep,
double cpp,
462 double s4,
double s6,
double ratio,
463 double xcog,
double ycog)
465 input_var.resize(EB_Indim);
467 double lam, lam1, lam2;
470 input_var[0] = -xcog/s25;
472 input_var[0] = xcog/s25;
475 input_var[1] = cee/0.0004;
478 input_var[2] = cpp/.001;
484 input_var[3] = s1/s9;
485 input_var[8] = s6/s9;
486 input_var[10] = (m2+s1)/s9;
495 input_var[4] = (s9-s1)/(s25-s1);
501 input_var[5] = s4/s25;
507 input_var[6] = -ycog/s25;
509 input_var[6] = ycog/s25;
512 input_var[7] =
ratio;
514 lam=
sqrt((cee -cpp)*(cee -cpp)+4*cep*cep);
515 lam1=(cee + cpp + lam)/2;
516 lam2=(cee + cpp - lam)/2;
521 input_var[9] = lam2/lam1;
524 input_var[11] = (m2+s1)/s4;
542 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo::GetNNoutput :nn_invar_presh = " ;
544 LogTrace(
"EcalClusters").log([&](
auto& lt) {
545 for(
auto const v: input_var) {
553 if(EE_Et<25.0) {sel_wfile = 0;}
554 else if(EE_Et>=25.0 && EE_Et<35.0) {sel_wfile = 1;}
555 else if(EE_Et>=35.0 && EE_Et<45.0) {sel_wfile = 2;}
556 else if(EE_Et>=45.0 && EE_Et<55.0) {sel_wfile = 3;}
557 else {sel_wfile = 4;}
559 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: Et_SC = " << EE_Et <<
" and I select Weight file Number = " << sel_wfile ;
562 nnout = getNNoutput(sel_wfile, EE_Layers, EE_Indim, EE_Hidden, EE_Outdim, 0);
564 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: ===================> GetNNOutput : NNout = " << nnout ;
582 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo::GetBarrelNNoutput :nn_invar_presh = " ;
584 LogTrace(
"EcalCluster").log([&](
auto& lt) {
585 for(
auto const v: input_var) {
593 if(EB_Et<25.0) {sel_wfile = 0;}
594 else if(EB_Et>=25.0 && EB_Et<35.0) {sel_wfile = 1;}
595 else if(EB_Et>=35.0 && EB_Et<45.0) {sel_wfile = 2;}
596 else if(EB_Et>=45.0 && EB_Et<55.0) {sel_wfile = 3;}
597 else {sel_wfile = 4;}
598 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: E_SC = " << EB_Et <<
" and I select Weight file Number = " << sel_wfile ;
600 nnout = getNNoutput(sel_wfile, EB_Layers, EB_Indim, EB_Hidden, EB_Outdim, 1);
602 LogTrace(
"EcalClusters") <<
"EndcapPiZeroDiscriminatorAlgo: ===================> GetNNOutput : NNout = " << nnout ;
void findPi0Road(ESDetId strip, EcalPreshowerNavigator &theESNav, int plane, std::vector< ESDetId > &vout)
float GetNNOutput(float EE_Et)
float Activation_fun(float SUM) const
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
void calculateBarrelNNInputVariables(float et, double s1, double s9, double s25, double m2, double cee, double cep, double cpp, double s4, double s6, double ratio, double xcog, double ycog)
float getNNoutput(int sel_wfile, int Layers, int Indim, int Hidden, int Outdim, int barrelstart) const
std::map< DetId, EcalRecHit > RecHitsMap
EndcapPiZeroDiscriminatorAlgo()
void readWeightFile(const char *WFile, int &Layers, int &Indim, int &Hidden, int &Outdim)
void setHome(const T &startingPoint)
set the starting position
std::vector< float > findPreshVector(ESDetId strip, RecHitsMap *rechits_map, CaloSubdetectorTopology *topology_p)
T west() const
move the navigator west
T south() const
move the navigator south
T east() const
move the navigator east
void home() const
move the navigator back to the starting point
float GetBarrelNNOutput(float EB_Et)
bool goodPi0Strip(RecHitsMap::iterator candidate_it, ESDetId lastID)
et
define resolution functions of each parameter
bool calculateNNInputVariables(std::vector< float > &vph1, std::vector< float > &vph2, float pS1_max, float pS9_max, float pS25_max, int EScorr)
T north() const
move the navigator north