CMS 3D CMS Logo

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