CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoEcal/EgammaClusterAlgos/src/PreshowerClusterAlgo.cc

Go to the documentation of this file.
00001 #include <vector>
00002 #include <map>
00003 
00004 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00005 #include "RecoEcal/EgammaClusterAlgos/interface/PreshowerClusterAlgo.h"
00006 #include "RecoCaloTools/Navigation/interface/EcalPreshowerNavigator.h"
00007 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00008 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 #include "TH1.h"
00012 
00013 reco::PreshowerCluster PreshowerClusterAlgo::makeOneCluster(ESDetId strip,
00014                                                             HitsID *used_strips,
00015                                                             RecHitsMap *the_rechitsMap_p,
00016                                                             const CaloSubdetectorGeometry*& geometry_p,
00017                                                             CaloSubdetectorTopology*& topology_p)
00018 {
00019   road_2d.clear();
00020 
00021   rechits_map = the_rechitsMap_p;
00022 
00023   used_s = used_strips;
00024 
00025   int plane = strip.plane();
00026   
00027   LogTrace("PreShowerClusterAlgo") << "Preshower Seeded Algorithm - looking for clusters";
00028   LogTrace("PreShowerClusterAlgo")<< "Preshower is intersected at strip" << strip.strip() << ",at plane" << plane ;
00029 
00030 
00031   // create null-cluster
00032   std::vector< std::pair<DetId,float> > dummy;
00033   Point posi(0,0,0); 
00034   LogTrace("PreShowerClusterAlgo") <<  " Creating a null-cluster" ;
00035 
00036   reco::PreshowerCluster nullcluster=reco::PreshowerCluster(0.,posi,dummy,plane);
00037 
00038   if ( strip == ESDetId(0) ) return nullcluster;   //works in case of no intersected strip found (e.g. in the Barrel)
00039 
00040   // Collection of cluster strips
00041   EcalRecHitCollection clusterRecHits;
00042   // Map of strips for position calculation
00043   RecHitsMap recHits_pos;
00044 
00045   //Make a navigator, and set it to the strip cell.
00046   EcalPreshowerNavigator navigator(strip, topology_p);
00047   navigator.setHome(strip);
00048  //search for neighbours in the central road
00049   findRoad(strip,navigator,plane);
00050   LogTrace("PreShowerClusterAlgo") << "Total number of strips in the central road:" << road_2d.size() ;
00051 
00052   if ( plane == 1 ) {
00053      ESDetId strip_north = navigator.north();
00054      findRoad(strip_north,navigator,plane);
00055      navigator.home();
00056      ESDetId strip_south = navigator.south();
00057      findRoad(strip_south,navigator,plane);
00058      navigator.home();
00059   }
00060   if ( plane == 2 ) {
00061      ESDetId strip_east = navigator.east();
00062      findRoad(strip_east,navigator,plane);
00063      navigator.home();
00064      ESDetId strip_west = navigator.west();
00065      findRoad(strip_west,navigator,plane);
00066      navigator.home();
00067   }
00068   LogTrace("PreShowerClusterAlgo") << "Total number of strips in all three roads:" << road_2d.size() ;
00069   
00070   // Start clustering from strip with max Energy in the road
00071   float E_max = 0.;
00072   bool found = false;
00073   RecHitsMap::iterator max_it;
00074   // Loop over strips:
00075   std::vector<ESDetId>::iterator itID;
00076   for (itID = road_2d.begin(); itID != road_2d.end(); itID++) {
00077     LogTrace("PreShowerClusterAlgo") << "ID ="<<*itID ;
00078   
00079     RecHitsMap::iterator strip_it = rechits_map->find(*itID);   
00080     //if ( strip_it->second.energy() < 0 ) std::cout << "           ##### E = " << strip_it->second.energy() << std::endl;
00081     if(!goodStrip(strip_it)) continue;
00082     LogTrace("PreShowerClusterAlgo") << " strip is " << ESDetId(strip_it->first) <<"E ="<< strip_it->second.energy();
00083     
00084     float E = strip_it->second.energy();
00085     if ( E > E_max) {
00086        E_max = E;
00087        found = true;
00088        max_it = strip_it;
00089     }
00090   }
00091   // no hottest strip found ==> null cluster will be returned
00092   if ( !found ) return nullcluster;
00093 
00094   // First, save the hottest strip
00095   clusterRecHits.push_back(max_it->second);  
00096   recHits_pos.insert(std::make_pair(max_it->first, max_it->second));
00097   used_s->insert(max_it->first);
00098   LogTrace("PreShowerClusterAlgo") << "Central hottest strip" << ESDetId(max_it->first) << "is saved with energy E =" <<  E_max ;
00099   
00100 
00101   // Find positions of adjacent strips:
00102   ESDetId next, strip_1, strip_2;
00103   navigator.setHome(max_it->first);
00104   ESDetId startES = max_it->first;
00105 
00106    if (plane == 1) {
00107     // Save two neighbouring strips to the east
00108     int nadjacents_east = 0;
00109     while ( (next=navigator.east()) != ESDetId(0) && next != startES && nadjacents_east < 2 ) {
00110        ++nadjacents_east;
00111        LogTrace("PreShowerClusterAlgo") << "Adjacent east #" << nadjacents_east <<":"<< next ;
00112   
00113        RecHitsMap::iterator strip_it = rechits_map->find(next);
00114 
00115        if(!goodStrip(strip_it)) continue;
00116        // Save strip for clustering if it exists, not already in use, and satisfies an energy threshold
00117        clusterRecHits.push_back(strip_it->second);       
00118        // save strip for position calculation
00119        if ( nadjacents_east==1 ) strip_1 = next;
00120        used_s->insert(strip_it->first);
00121        LogTrace("PreShowerClusterAlgo") << "East adjacent strip #" << nadjacents_east << "is saved with energy E =" << strip_it->second.energy() ;
00122   
00123     }
00124     // Save two neighbouring strips to the west
00125     navigator.home();
00126     int nadjacents_west = 0;
00127     while ( (next=navigator.west()) != ESDetId(0) && next != startES && nadjacents_west < 2 ) {
00128        ++nadjacents_west;
00129        LogTrace("PreShowerClusterAlgo") << "Adjacent west #" << nadjacents_west <<":"<< next ;
00130   
00131        RecHitsMap::iterator strip_it = rechits_map->find(next);
00132        if(!goodStrip(strip_it)) continue;
00133        clusterRecHits.push_back(strip_it->second);
00134        if ( nadjacents_west==1 ) strip_2 = next;
00135        used_s->insert(strip_it->first);
00136        LogTrace("PreShowerClusterAlgo") << "West adjacent strip #" << nadjacents_west << "is saved with energy E =" << strip_it->second.energy();
00137   
00138     }
00139   }
00140   else if (plane == 2) {
00141 
00142   // Save two neighbouring strips to the north
00143     int nadjacents_north = 0;
00144     while ( (next=navigator.north()) != ESDetId(0) && next != startES && nadjacents_north < 2 ) {
00145        ++nadjacents_north;
00146        LogTrace("PreShowerClusterAlgo") << "Adjacent north #" << nadjacents_north <<":"<< next ;
00147   
00148        RecHitsMap::iterator strip_it = rechits_map->find(next); 
00149        if(!goodStrip(strip_it)) continue;      
00150        clusterRecHits.push_back(strip_it->second);
00151        if ( nadjacents_north==1 ) strip_1 = next;
00152        used_s->insert(strip_it->first);
00153        LogTrace("PreShowerClusterAlgo") << "North adjacent strip #" << nadjacents_north << "is saved with energy E =" << strip_it->second.energy() ;
00154        
00155     }
00156     // Save two neighbouring strips to the south
00157     navigator.home();
00158     int nadjacents_south = 0;
00159     while ( (next=navigator.south()) != ESDetId(0) && next != startES && nadjacents_south < 2 ) {
00160        ++nadjacents_south;
00161        LogTrace("PreShowerClusterAlgo") << "Adjacent south #" << nadjacents_south <<":"<< next ;
00162        
00163        RecHitsMap::iterator strip_it = rechits_map->find(next);   
00164        if(!goodStrip(strip_it)) continue;      
00165        clusterRecHits.push_back(strip_it->second);
00166        if ( nadjacents_south==1 ) strip_2 = next;
00167        used_s->insert(strip_it->first);
00168        LogTrace("PreShowerClusterAlgo") << "South adjacent strip #" << nadjacents_south << "is saved with energy E =" << strip_it->second.energy() ;
00169   
00170     }
00171   }
00172   else {
00173     LogTrace("PreShowerClusterAlgo") << " Wrong plane number" << plane <<", null cluster will be returned! " << std::endl;
00174     return nullcluster;
00175   } // end of if
00176 
00177   LogTrace("PreShowerClusterAlgo") << "Total size of clusterRecHits is" << clusterRecHits.size();
00178   LogTrace("PreShowerClusterAlgo") << "Two adjacent strips for position calculation are:" << strip_1 <<"and" << strip_2;
00179   
00180 
00181   // strips for position calculation
00182   RecHitsMap::iterator strip_it1, strip_it2;
00183   if ( strip_1 != ESDetId(0)) {
00184     strip_it1 = rechits_map->find(strip_1);
00185     recHits_pos.insert(std::make_pair(strip_it1->first, strip_it1->second));  
00186   }
00187   if ( strip_2 != ESDetId(0) ) {
00188     strip_it2 = rechits_map->find(strip_2);
00189     recHits_pos.insert(std::make_pair(strip_it2->first, strip_it2->second));  
00190   }
00191 
00192   RecHitsMap::iterator cp;
00193   double energy_pos = 0;
00194   double x_pos = 0;
00195   double y_pos = 0;
00196   double z_pos = 0;
00197   for (cp = recHits_pos.begin(); cp!=recHits_pos.end(); cp++ ) {
00198      double E = cp->second.energy();
00199      energy_pos += E; 
00200      const CaloCellGeometry *thisCell = geometry_p->getGeometry(cp->first);
00201      GlobalPoint position = thisCell->getPosition();
00202      x_pos += E * position.x();
00203      y_pos += E * position.y();
00204      z_pos += E * position.z();     
00205   }
00206   if(energy_pos>0.) {
00207      x_pos /= energy_pos;
00208      y_pos /= energy_pos;
00209      z_pos /= energy_pos;
00210   }
00211   Point pos(x_pos,y_pos,z_pos);
00212   LogTrace("PreShowerClusterAlgo") << "ES Cluster position =" << "("<< x_pos <<","<< y_pos <<","<< z_pos <<")";
00213 
00214   EcalRecHitCollection::iterator it;
00215   double Eclust = 0;
00216 
00217 
00218   std::vector<std::pair<DetId,float > > usedHits;
00219   for (it=clusterRecHits.begin(); it != clusterRecHits.end(); it++) {
00220      Eclust += it->energy();
00221      usedHits.push_back(std::pair<DetId,float > (it->id(),1.));
00222 
00223   }   
00224   
00225 
00226   // ES cluster is created from vector clusterRecHits
00227   reco::PreshowerCluster cluster=reco::PreshowerCluster(Eclust,pos,usedHits,plane);
00228   LogTrace("PreShowerClusterAlgo") << " ES Cluster is created with:" ;
00229   LogTrace("PreShowerClusterAlgo") << " energy =" << cluster.energy();
00230   LogTrace("PreShowerClusterAlgo") << " (eta,phi) =" << "("<<cluster.eta()<<","<<cluster.phi()<<")";
00231   LogTrace("PreShowerClusterAlgo") << " nhits =" << cluster.nhits();
00232   LogTrace("PreShowerClusterAlgo") << " radius =" << cluster.position().r();
00233   LogTrace("PreShowerClusterAlgo") << " (x,y,z) =" << "(" << cluster.x() <<", "<< cluster.y() <<","<< cluster.z()<<")" ;
00234  
00235   used_strips = used_s;
00236 
00237   // return the cluster if its energy is greater a threshold
00238   if( cluster.energy() > preshClusterEnergyCut_ ) 
00239      return cluster; 
00240   else
00241      return nullcluster;
00242 
00243 }
00244 
00245 // returns true if the candidate strip fulfills the requirements to be added to the cluster:
00246 bool PreshowerClusterAlgo::goodStrip(RecHitsMap::iterator candidate_it)
00247 {
00248  
00249   if ( used_s->find(candidate_it->first) != used_s->end())
00250   LogTrace("PreShowerClusterAlgo") << " This strip is in use";
00251   if (candidate_it == rechits_map->end() )
00252   LogTrace("PreShowerClusterAlgo") << " No such a strip in rechits_map";
00253   if (candidate_it->second.energy() <= preshStripEnergyCut_)
00254   LogTrace("PreShowerClusterAlgo") << "Strip energy" << candidate_it->second.energy() <<"is below threshold";
00255   
00256   // crystal should not be included...
00257   if ( (used_s->find(candidate_it->first) != used_s->end())  ||       // ...if it already belongs to a cluster
00258        (candidate_it == rechits_map->end() )                    ||       // ...if it corresponds to a hit
00259        (candidate_it->second.energy() <= preshStripEnergyCut_ ) )   // ...if it has a negative or zero energy
00260     {
00261       return false;
00262     }
00263   return true;
00264 }
00265 
00266 
00267 // find strips in the road of size +/- preshSeededNstr_ from the central strip
00268 void PreshowerClusterAlgo::findRoad(ESDetId strip, EcalPreshowerNavigator theESNav, int plane) {
00269   
00270   if ( strip == ESDetId(0) ) return;
00271 
00272    ESDetId next;
00273    theESNav.setHome(strip);
00274 // First, add a central strip to the road 
00275    road_2d.push_back(strip);   
00276    LogTrace("PreShowerClusterAlgo") << "findRoad starts from strip" << strip;
00277  
00278    if (plane == 1) {
00279      // east road
00280      int n_east= 0;
00281      LogTrace("PreShowerClusterAlgo") << " Go to the East ";
00282  
00283      while ( ((next=theESNav.east()) != ESDetId(0) && next != strip) ) {
00284        LogTrace("PreShowerClusterAlgo") << "East:" << n_east << "current strip is"<< next;
00285  
00286        road_2d.push_back(next);   
00287        ++n_east;  
00288        if (n_east == preshSeededNstr_) break; 
00289      }
00290      // west road
00291      int n_west= 0;
00292      LogTrace("PreShowerClusterAlgo") <<  "Go to the West" ;
00293     
00294      theESNav.home();
00295      while ( ((next=theESNav.west()) != ESDetId(0) && next != strip )) {
00296        LogTrace("PreShowerClusterAlgo") << "West: " << n_west << "current strip is" << next ;
00297        
00298         road_2d.push_back(next);   
00299         ++n_west;  
00300         if (n_west == preshSeededNstr_) break; 
00301      }
00302      LogTrace("PreShowerClusterAlgo") << "Total number of strips found in the road at 1-st plane is" << n_east+n_west ;
00303      
00304   } 
00305   else if (plane == 2) {
00306     // north road
00307     int n_north= 0;
00308     LogTrace("PreShowerClusterAlgo") << "Go to the North";
00309     
00310     while ( ((next=theESNav.north()) != ESDetId(0) && next != strip) ) {   
00311         LogTrace("PreShowerClusterAlgo") << "North:" << n_north << "current strip is" << next ;
00312        
00313        road_2d.push_back(next);   
00314        ++n_north;  
00315        if (n_north == preshSeededNstr_) break; 
00316     }
00317     // south road
00318     int n_south= 0;
00319     LogTrace("PreShowerClusterAlgo") << "Go to the South";
00320     
00321     theESNav.home();
00322     while ( ((next=theESNav.south()) != ESDetId(0) && next != strip) ) {
00323 
00324        LogTrace("PreShowerClusterAlgo") << "South:" << n_south << "current strip is" << next ;
00325     
00326        road_2d.push_back(next);   
00327        ++n_south;  
00328        if (n_south == preshSeededNstr_) break; 
00329     }
00330 
00331     LogTrace("PreShowerClusterAlgo") << "Total number of strips found in the road at 2-nd plane is" << n_south+n_north;
00332 
00333   } 
00334   else {
00335   LogTrace("PreShowerClusterAlgo") << " Wrong plane number, null cluster will be returned!";
00336 
00337   } // end of if
00338 
00339   theESNav.home();
00340 }
00341 
00342 
00343