CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/Calibration/EcalAlCaRecoProducers/src/AlCaElectronsProducer.cc

Go to the documentation of this file.
00001 #include "Calibration/EcalAlCaRecoProducers/interface/AlCaElectronsProducer.h"
00002 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00003 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
00004 #include "DataFormats/EgammaCandidates/interface/SiStripElectron.h"
00005 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00006 #include "DataFormats/EgammaCandidates/interface/ElectronFwd.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00009 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00010 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00011 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00012 #include "DataFormats/EcalDetId/interface/ESDetId.h"
00013 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00014 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00015 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00016 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00017 #include "Geometry/EcalAlgo/interface/EcalPreshowerGeometry.h"
00018 #include "Geometry/CaloTopology/interface/EcalPreshowerTopology.h"
00019 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00020 #include "FWCore/Framework/interface/ESHandle.h"
00021 
00022 AlCaElectronsProducer::AlCaElectronsProducer(const edm::ParameterSet& iConfig) 
00023 {
00024 
00025   ebRecHitsLabel_ = iConfig.getParameter< edm::InputTag > ("ebRecHitsLabel");
00026   eeRecHitsLabel_ = iConfig.getParameter< edm::InputTag > ("eeRecHitsLabel");
00027   esRecHitsLabel_ = iConfig.getParameter< edm::InputTag > ("esRecHitsLabel");
00028   electronLabel_ = iConfig.getParameter< edm::InputTag > ("electronLabel");
00029 
00030   alcaBarrelHitsCollection_ = iConfig.getParameter<std::string>("alcaBarrelHitCollection");
00031   alcaEndcapHitsCollection_ = iConfig.getParameter<std::string>("alcaEndcapHitCollection");
00032   alcaPreshowerHitsCollection_ = iConfig.getParameter<std::string>("alcaPreshowerHitCollection");
00033   
00034   etaSize_ = iConfig.getParameter<int> ("etaSize");
00035   phiSize_ = iConfig.getParameter<int> ("phiSize");
00036   if ( phiSize_ % 2 == 0 ||  etaSize_ % 2 == 0)
00037     edm::LogError("AlCaElectronsProducerError") << "Size of eta/phi should be odd numbers";
00038  
00039   weight_= iConfig.getParameter<double> ("eventWeight");
00040  
00041   esNstrips_  = iConfig.getParameter<int> ("esNstrips");
00042   esNcolumns_ = iConfig.getParameter<int> ("esNcolumns");
00043   
00044    //register your products
00045   produces< EBRecHitCollection > (alcaBarrelHitsCollection_) ;
00046   produces< EERecHitCollection > (alcaEndcapHitsCollection_) ;
00047   produces< ESRecHitCollection > (alcaPreshowerHitsCollection_) ;
00048   produces< double > ("weight") ;
00049 }
00050 
00051 
00052 AlCaElectronsProducer::~AlCaElectronsProducer()
00053 {}
00054 
00055 
00056 // ------------ method called to produce the data  ------------
00057 void
00058 AlCaElectronsProducer::produce (edm::Event& iEvent, 
00059                                 const edm::EventSetup& iSetup)
00060 {
00061   using namespace edm;
00062   using namespace std;
00063   
00064   // get the ECAL geometry:
00065   ESHandle<CaloGeometry> geoHandle;
00066   iSetup.get<CaloGeometryRecord>().get(geoHandle);
00067   
00068   const CaloSubdetectorGeometry *geometry = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalPreshower);
00069   const CaloSubdetectorGeometry *& geometry_p = geometry;
00070  
00071   // Get GSFElectrons
00072   Handle<reco::GsfElectronCollection> pElectrons;
00073   iEvent.getByLabel(electronLabel_, pElectrons);
00074   if (!pElectrons.isValid()) {
00075     edm::LogError ("reading") << electronLabel_ << " not found" ; 
00076     //      std::cerr << "[AlCaElectronsProducer]" << electronLabel_ << " not found" ; 
00077     return ;
00078   }
00079   
00080   const reco::GsfElectronCollection * electronCollection = 
00081     pElectrons.product();
00082   
00083   // get RecHits
00084   Handle<EBRecHitCollection> barrelRecHitsHandle;
00085   bool barrelIsFull = true ;
00086   
00087   iEvent.getByLabel(ebRecHitsLabel_,barrelRecHitsHandle);
00088   if (!barrelRecHitsHandle.isValid()) {
00089     edm::LogError ("reading") << ebRecHitsLabel_ << " not found" ; 
00090     barrelIsFull = false ;
00091   }
00092   
00093   const EBRecHitCollection * barrelHitsCollection = 0 ;
00094   if (barrelIsFull)  
00095     barrelHitsCollection = barrelRecHitsHandle.product () ;
00096   
00097   // get RecHits
00098   Handle<EERecHitCollection> endcapRecHitsHandle;
00099   bool endcapIsFull = true ;
00100   
00101   iEvent.getByLabel(eeRecHitsLabel_,endcapRecHitsHandle);
00102   if (!endcapRecHitsHandle.isValid()) {
00103     edm::LogError ("reading") << eeRecHitsLabel_ << " not found" ; 
00104     endcapIsFull = false ;
00105   }
00106   
00107   const EERecHitCollection * endcapHitsCollection = 0 ;
00108   if (endcapIsFull)  
00109     endcapHitsCollection = endcapRecHitsHandle.product () ;
00110   //  const EERecHitCollection * endcapHitsCollection = endcapRecHitsHandle.product();
00111   
00112   // get ES RecHits
00113   Handle<ESRecHitCollection> preshowerRecHitsHandle;
00114   bool preshowerIsFull = true ;
00115   
00116   iEvent.getByLabel(esRecHitsLabel_,preshowerRecHitsHandle);
00117   if (!preshowerRecHitsHandle.isValid()) {
00118     edm::LogError ("reading") << esRecHitsLabel_ << " not found" ; 
00119     preshowerIsFull = false ;
00120   }
00121   
00122   const ESRecHitCollection * preshowerHitsCollection = 0 ;
00123   if (preshowerIsFull)  
00124     preshowerHitsCollection = preshowerRecHitsHandle.product () ;
00125 
00126   // make a vector to store the used ES rechits:
00127   set<ESDetId> used_strips;
00128   used_strips.clear();
00129  
00130   //Create empty output collections
00131   std::auto_ptr< EBRecHitCollection > miniEBRecHitCollection (new EBRecHitCollection) ;
00132   std::auto_ptr< EERecHitCollection > miniEERecHitCollection (new EERecHitCollection) ;  
00133   std::auto_ptr< ESRecHitCollection > miniESRecHitCollection (new ESRecHitCollection) ;  
00134   std::auto_ptr< double > weight (new double(1));
00135   (*weight) = weight_;
00136   
00137   //  loop on SiStrip Electrons
00138   
00139   reco::GsfElectronCollection::const_iterator eleIt;
00140   int ii=0;
00141   
00142   for (eleIt=electronCollection->begin(); eleIt!=electronCollection->end(); eleIt++) {
00143     //PG barrel
00144     if (fabs(eleIt->eta()) <= 1.479) {
00145       
00146       ii++;         
00147       const reco::SuperCluster& sc = *(eleIt->superCluster()) ;
00148       
00149       int yy = 0;
00150       double currEnergy = 0.;
00151       EBDetId maxHit(0);
00152       std::vector<EBDetId> scXtals;
00153       scXtals.clear();
00154       const std::vector< std::pair<DetId, float> > & v1 = sc.hitsAndFractions();
00155       
00156       for(std::vector< std::pair<DetId, float> >::const_iterator idsIt = v1.begin(); 
00157           idsIt != v1.end(); ++idsIt) 
00158         {
00159           yy++;
00160           
00161           //PG discard hits not belonging to the Ecal Barrel
00162           if((*idsIt).first.subdetId()!=EcalBarrel || (*idsIt).first.det()!= DetId::Ecal) continue;
00163           
00164           if((barrelHitsCollection->find( (*idsIt).first ))->energy() > currEnergy) {
00165             currEnergy=(barrelHitsCollection->find( (*idsIt).first ))->energy();
00166             maxHit=(*idsIt).first;
00167           }
00168           miniEBRecHitCollection->push_back(*(barrelHitsCollection->find( (*idsIt).first )));
00169           scXtals.push_back( (*idsIt).first );
00170         }
00171       
00172       if (!maxHit.null())
00173         for (int icry=0;icry< etaSize_*phiSize_;icry++)
00174           {
00175             
00176             unsigned int row = icry / etaSize_;
00177             unsigned int column= icry % etaSize_;
00178             int curr_eta=maxHit.ieta() + column - (etaSize_/2);
00179             int curr_phi=maxHit.iphi() + row - (phiSize_/2);
00180             
00181             if (curr_eta * maxHit.ieta() <= 0) {if (maxHit.ieta() > 0) curr_eta--; else curr_eta++; }  // JUMP over 0
00182             if (curr_phi < 1) curr_phi += 360;
00183             if (curr_phi > 360) curr_phi -= 360;
00184             if (!(EBDetId::validDetId(curr_eta,curr_phi))) continue; 
00185             EBDetId det = EBDetId(curr_eta,curr_phi,EBDetId::ETAPHIMODE);
00186             std::vector<EBDetId>::const_iterator usedIds;
00187             
00188             bool HitAlreadyUsed=false;
00189             for(usedIds=scXtals.begin(); usedIds!=scXtals.end(); usedIds++)
00190               if(*usedIds==det)
00191                 {
00192                   HitAlreadyUsed=true;
00193                   break;
00194                 }
00195             
00196             if(!HitAlreadyUsed)
00197               if (barrelHitsCollection->find(det) != barrelHitsCollection->end())
00198                 miniEBRecHitCollection->push_back(*(barrelHitsCollection->find(det)));
00199           }
00200     } //PG barrel
00201     else //PG endcap
00202       {
00203         
00204         ii++;         
00205         const reco::SuperCluster& sc = *(eleIt->superCluster()) ;
00206         
00207         int yy = 0;
00208         double currEnergy = 0.;
00209         EEDetId maxHit(0);
00210         vector<EEDetId> scXtals;
00211         scXtals.clear();
00212         const std::vector< std::pair<DetId, float> > & v1 = sc.hitsAndFractions();
00213         
00214         for(std::vector< std::pair<DetId, float> >::const_iterator idsIt = v1.begin(); 
00215             idsIt != v1.end(); ++idsIt) 
00216           {
00217             yy++;
00218             
00219             //PG discard hits belonging to the Ecal Barrel
00220             if((*idsIt).first.subdetId()!=EcalEndcap || 
00221                (*idsIt).first.det()!= DetId::Ecal) continue;
00222             
00223             if((endcapHitsCollection->find( (*idsIt).first ))->energy() > currEnergy) 
00224               {
00225                 currEnergy=(endcapHitsCollection->find( (*idsIt).first ))->energy();
00226                 maxHit=(*idsIt).first;
00227               }
00228             miniEERecHitCollection->push_back (
00229                                                *(endcapHitsCollection->find ( (*idsIt).first ))
00230                                                );
00231             scXtals.push_back ( (*idsIt).first ) ; 
00232           }  
00233         
00234         int side = phiSize_ ;
00235         if (phiSize_ < etaSize_) side = etaSize_ ;
00236         int iz = 1 ;
00237         if (eleIt->eta () < 0)  iz = -1 ;
00238         if (!maxHit.null())
00239           //PG loop over the local array of xtals
00240           for (int icry = 0 ; icry < side*side ; icry++)
00241             {          
00242               unsigned int row = icry / side ;
00243               unsigned int column = icry % side ;
00244               int curr_eta = maxHit.ix () + column - (side/2);
00245               int curr_phi = maxHit.iy () + row - (side/2);
00246               if (   curr_eta <= 0 || curr_phi <= 0
00247                      || curr_eta > 100 || curr_phi > 100 ) continue ;
00248               if (!(EEDetId::validDetId(curr_eta,curr_phi,iz))) continue;
00249               EEDetId det = EEDetId (curr_eta,curr_phi,iz,EEDetId::XYMODE) ; 
00250               if (find (scXtals.begin (), scXtals.end (), det) != scXtals.end ())
00251                 if (endcapHitsCollection->find (det) != endcapHitsCollection->end ())
00252                   miniEERecHitCollection->push_back (*(endcapHitsCollection->find (det))) ;
00253             }
00254         
00255         // Loop over basic clusters to find ES rec hits
00256         reco::CaloCluster_iterator bc_iter = sc.clustersBegin();
00257         for ( ; bc_iter != sc.clustersEnd(); ++bc_iter ) {  
00258           if (geometry) {
00259             double X = (*bc_iter)->x();
00260             double Y = (*bc_iter)->y();
00261             double Z = (*bc_iter)->z();        
00262             const GlobalPoint point(X,Y,Z);    
00263             
00264             DetId tmp1 = (dynamic_cast<const EcalPreshowerGeometry*>(geometry_p))->getClosestCellInPlane(point, 1);
00265             DetId tmp2 = (dynamic_cast<const EcalPreshowerGeometry*>(geometry_p))->getClosestCellInPlane(point, 2);
00266             ESDetId strip1 = (tmp1 == DetId(0)) ? ESDetId(0) : ESDetId(tmp1);
00267             ESDetId strip2 = (tmp2 == DetId(0)) ? ESDetId(0) : ESDetId(tmp2);     
00268 
00269             int esindexE1 = (tmp1 == DetId(0)) ? -1 : (strip1.six()-1)*32+strip1.strip();
00270             int esindexE2 = (tmp2 == DetId(0)) ? -1 : (strip2.siy()-1)*32+strip2.strip();
00271             int esindexRh = 0; 
00272             
00273             EcalRecHitCollection::const_iterator esit;
00274             for (esit = preshowerHitsCollection->begin(); esit != preshowerHitsCollection->end(); esit++) {
00275 
00276               ESDetId esid = ESDetId(esit->id());
00277 
00278               if (used_strips.find(esid) != used_strips.end()) continue;
00279 
00280               if (strip1 != ESDetId(0) && esid.plane()==1) {
00281                
00282                 esindexRh = (esid.six()-1)*32+esid.strip();             
00283                 
00284                 if (fabs(esindexE1-esindexRh) <= esNstrips_ && fabs(strip1.siy()-esid.siy()) <= esNcolumns_ && strip1.zside()==esid.zside()) {
00285                   miniESRecHitCollection->push_back(*esit);
00286                   used_strips.insert(esid);
00287                 }
00288               }
00289               if (strip2 != ESDetId(0) && esid.plane()==2) {
00290 
00291                 esindexRh = (esid.siy()-1)*32+esid.strip();
00292 
00293                 if (fabs(esindexE2-esindexRh) <= esNstrips_ && fabs(strip2.six()-esid.six()) <= esNcolumns_ && strip2.zside()==esid.zside()) {
00294                   miniESRecHitCollection->push_back(*esit);
00295                   used_strips.insert(esid);
00296                 }
00297               }
00298 
00299             }
00300           }
00301         }
00302         
00303       } //PG endcap
00304     
00305   } //PG loop on Si strip electrons
00306   
00307   //Put selected information in the event
00308   iEvent.put( miniEBRecHitCollection,alcaBarrelHitsCollection_ );
00309   iEvent.put( miniEERecHitCollection,alcaEndcapHitsCollection_ );     
00310   iEvent.put( miniESRecHitCollection,alcaPreshowerHitsCollection_ );     
00311   iEvent.put( weight, "weight");     
00312 }