CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/RecoEcal/EgammaClusterProducers/src/PreshowerClusterProducer.cc

Go to the documentation of this file.
00001 // system include files
00002 #include <vector>
00003 #include <memory>
00004 
00005 // user include files
00006 #include "FWCore/Framework/interface/Frameworkfwd.h"
00007 #include "FWCore/Framework/interface/EDAnalyzer.h"
00008 
00009 #include "FWCore/Framework/interface/Event.h"
00010 #include "FWCore/Framework/interface/EventSetup.h"
00011 #include "DataFormats/Common/interface/Handle.h"
00012 #include "FWCore/Framework/interface/ESHandle.h"
00013 #include "FWCore/Framework/interface/MakerMacros.h"
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 #include "FWCore/Utilities/interface/Exception.h"
00016 
00017 // Reconstruction Classes
00018 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
00019 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00020 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00021 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
00022 #include "DataFormats/EgammaReco/interface/PreshowerClusterFwd.h"
00023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00024 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00025 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00026 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00027 #include "Geometry/CaloGeometry/interface/TruncatedPyramid.h"
00028 #include "Geometry/EcalAlgo/interface/EcalPreshowerGeometry.h"
00029 #include "Geometry/CaloTopology/interface/EcalPreshowerTopology.h"
00030 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00031 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00032 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00033 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00034 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00035 #include "DataFormats/EcalDetId/interface/ESDetId.h"
00036 #include "CondFormats/DataRecord/interface/ESGainRcd.h"
00037 #include "CondFormats/DataRecord/interface/ESMIPToGeVConstantRcd.h"
00038 #include "CondFormats/DataRecord/interface/ESEEIntercalibConstantsRcd.h"
00039 #include "CondFormats/DataRecord/interface/ESChannelStatusRcd.h"
00040 #include <fstream>
00041 
00042 #include "RecoEcal/EgammaClusterProducers/interface/PreshowerClusterProducer.h"
00043 
00044 
00045 using namespace edm;
00046 using namespace std;
00047 
00048 
00049 PreshowerClusterProducer::PreshowerClusterProducer(const edm::ParameterSet& ps) {
00050 
00051   // use configuration file to setup input/output collection names 
00052   preshHitProducer_   = ps.getParameter<edm::InputTag>("preshRecHitProducer");
00053 
00054   // Name of a SuperClusterCollection to make associations:
00055   endcapSClusterProducer_   = ps.getParameter<edm::InputTag>("endcapSClusterProducer");
00056 
00057   // Output collections:
00058   preshClusterCollectionX_ = ps.getParameter<std::string>("preshClusterCollectionX");
00059   preshClusterCollectionY_ = ps.getParameter<std::string>("preshClusterCollectionY");
00060   preshNclust_             = ps.getParameter<int>("preshNclust");
00061 
00062   etThresh_ =  ps.getParameter<double>("etThresh");
00063 
00064   assocSClusterCollection_ = ps.getParameter<std::string>("assocSClusterCollection");
00065 
00066   produces< reco::PreshowerClusterCollection >(preshClusterCollectionX_);
00067   produces< reco::PreshowerClusterCollection >(preshClusterCollectionY_);
00068   produces< reco::SuperClusterCollection >(assocSClusterCollection_);
00069 
00070   float preshStripECut = ps.getParameter<double>("preshStripEnergyCut");
00071     int preshSeededNst = ps.getParameter<int>("preshSeededNstrip");
00072   preshClustECut = ps.getParameter<double>("preshClusterEnergyCut");
00073 
00074 
00075   presh_algo = new PreshowerClusterAlgo(preshStripECut,preshClustECut,preshSeededNst);
00076 
00077   nEvt_ = 0;  
00078 
00079 }
00080 
00081 
00082 PreshowerClusterProducer::~PreshowerClusterProducer() {
00083    delete presh_algo;
00084 }
00085 
00086 
00087 void PreshowerClusterProducer::produce(edm::Event& evt, const edm::EventSetup& es) {
00088 
00089   edm::Handle< EcalRecHitCollection >   pRecHits;
00090   edm::Handle< reco::SuperClusterCollection > pSuperClusters;
00091 
00092   // get the ECAL geometry:
00093   edm::ESHandle<CaloGeometry> geoHandle;
00094   es.get<CaloGeometryRecord>().get(geoHandle);
00095 
00096   // retrieve ES-EE intercalibration constants and channel status
00097   set(es);
00098   const ESChannelStatus *channelStatus = esChannelStatus_.product();
00099 
00100   const CaloSubdetectorGeometry *geometry = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalPreshower);
00101   const CaloSubdetectorGeometry *& geometry_p = geometry;
00102 
00103    // create auto_ptr to a PreshowerClusterCollection
00104    std::auto_ptr< reco::PreshowerClusterCollection > clusters_p1(new reco::PreshowerClusterCollection);
00105    std::auto_ptr< reco::PreshowerClusterCollection > clusters_p2(new reco::PreshowerClusterCollection);
00106    // create new collection of corrected super clusters
00107    std::auto_ptr< reco::SuperClusterCollection > superclusters_p(new reco::SuperClusterCollection);
00108 
00109    CaloSubdetectorTopology * topology_p=0;
00110    if (geometry)
00111      topology_p  = new EcalPreshowerTopology(geoHandle);
00112 
00113    // fetch the product (pSuperClusters)
00114    evt.getByLabel(endcapSClusterProducer_, pSuperClusters);   
00115    const reco::SuperClusterCollection* SClusts = pSuperClusters.product();
00116    
00117    // fetch the product (RecHits)
00118    evt.getByLabel( preshHitProducer_, pRecHits);
00119    // pointer to the object in the product
00120    const EcalRecHitCollection* rechits = pRecHits.product(); // EcalRecHitCollection hit_collection = *rhcHandle;
00121 
00122    LogTrace("EcalClusters") << "PreshowerClusterProducerInfo: ### Total # of preshower RecHits: "<< rechits->size();
00123    
00124   // make the map of rechits:
00125    std::map<DetId, EcalRecHit> rechits_map;
00126    EcalRecHitCollection::const_iterator it;
00127    for (it = rechits->begin(); it != rechits->end(); it++) {
00128      // remove bad ES rechits
00129      if (it->recoFlag()==1 || it->recoFlag()==14 || (it->recoFlag()<=10 && it->recoFlag()>=5)) continue;
00130      //Make the map of DetID, EcalRecHit pairs
00131      rechits_map.insert(std::make_pair(it->id(), *it));   
00132    }
00133    // The set of used DetID's for a given event:
00134    std::set<DetId> used_strips;
00135    used_strips.clear();
00136    LogTrace("EcalClusters") << "PreshowerClusterProducerInfo: ### rechits_map of size " << rechits_map.size() <<" was created!";
00137    
00138   
00139   reco::PreshowerClusterCollection clusters1, clusters2;   // output collection of corrected PCs
00140   reco::SuperClusterCollection new_SC; // output collection of corrected SCs
00141 
00142   //make cycle over super clusters
00143   reco::SuperClusterCollection::const_iterator it_super;
00144   int isc = 0;
00145   for ( it_super=SClusts->begin();  it_super!=SClusts->end(); it_super++ ) {     
00146        float e1=0;
00147        float e2=0;
00148        float deltaE=0;
00149        reco::CaloClusterPtrVector new_BC; 
00150        ++isc;
00151        LogTrace("EcalClusters")<< " superE = " << it_super->energy() << " superETA = " << it_super->eta() << " superPHI = " << it_super->phi() ;
00152        
00153        
00154        int nBC = 0;
00155        int condP1 = 1; // 0: dead channel; 1: active channel
00156        int condP2 = 1;
00157        reco::CaloCluster_iterator bc_iter = it_super->clustersBegin();
00158        for ( ; bc_iter !=it_super->clustersEnd(); ++bc_iter ) {  
00159          if (geometry) {
00160            
00161            // Get strip position at intersection point of the line EE - Vertex:
00162            double X = (*bc_iter)->x();
00163            double Y = (*bc_iter)->y();
00164            double Z = (*bc_iter)->z();        
00165            const GlobalPoint point(X,Y,Z);    
00166            
00167            DetId tmp1 = (dynamic_cast<const EcalPreshowerGeometry*>(geometry_p))->getClosestCellInPlane(point, 1);
00168            DetId tmp2 = (dynamic_cast<const EcalPreshowerGeometry*>(geometry_p))->getClosestCellInPlane(point, 2);
00169            ESDetId strip1 = (tmp1 == DetId(0)) ? ESDetId(0) : ESDetId(tmp1);
00170            ESDetId strip2 = (tmp2 == DetId(0)) ? ESDetId(0) : ESDetId(tmp2);     
00171            
00172            if (nBC == 0) {
00173              if (strip1 != ESDetId(0) && strip2 != ESDetId(0)) {
00174                ESChannelStatusMap::const_iterator status_p1 = channelStatus->getMap().find(strip1);
00175                ESChannelStatusMap::const_iterator status_p2 = channelStatus->getMap().find(strip2);
00176                if (status_p1->getStatusCode() == 1) condP1 = 0;
00177                if (status_p2->getStatusCode() == 1) condP2 = 0;
00178              } else if (strip1 == ESDetId(0))
00179                condP1 = 0;
00180              else if (strip2 == ESDetId(0))
00181                condP2 = 0;
00182            }
00183            
00184            
00185            // Get a vector of ES clusters (found by the PreshSeeded algorithm) associated with a given EE basic cluster.           
00186            for (int i=0; i<preshNclust_; i++) {
00187              reco::PreshowerCluster cl1 = presh_algo->makeOneCluster(strip1,&used_strips,&rechits_map,geometry_p,topology_p);   
00188              cl1.setBCRef(*bc_iter);
00189              if (cl1.energy() > preshClustECut) {
00190                clusters1.push_back(cl1);
00191                e1 += cl1.energy();       
00192              }
00193              reco::PreshowerCluster cl2 = presh_algo->makeOneCluster(strip2,&used_strips,&rechits_map,geometry_p,topology_p); 
00194              cl2.setBCRef(*bc_iter);
00195              
00196              if ( cl2.energy() > preshClustECut) {
00197                clusters2.push_back(cl2);
00198                e2 += cl2.energy();
00199              }                                 
00200            } // end of cycle over ES clusters
00201          }
00202 
00203          new_BC.push_back(*bc_iter);
00204          nBC++;
00205        }  // end of cycle over BCs
00206        
00207        LogTrace("EcalClusters") << " For SC #" << isc-1 << ", containing " 
00208                          << it_super->clustersSize()                          
00209                          << " basic clusters, PreshowerClusterAlgo made " 
00210                          << clusters1.size() << " in X plane and " 
00211                          << clusters2.size()       
00212                          << " in Y plane " << " preshower clusters " ;
00213        
00214        // update energy of the SuperCluster    
00215        if(e1+e2 > 1.0e-10) {
00216          // GeV to #MIPs
00217          e1 = e1 / mip_;
00218          e2 = e2 / mip_;
00219 
00220          if (condP1 == 1 && condP2 == 1) deltaE = gamma0_*(e1 + alpha0_*e2);       
00221          else if (condP1 == 1 && condP2 == 0) deltaE = gamma1_*(e1 + alpha1_*e2);       
00222          else if (condP1 == 0 && condP2 == 1) deltaE = gamma2_*(e1 + alpha2_*e2);       
00223          else if (condP1 == 0 && condP2 == 0) deltaE = gamma3_*(e1 + alpha3_*e2);       
00224        }
00225        
00226        //corrected Energy
00227        float E = it_super->energy() + deltaE;
00228        
00229        LogTrace("EcalClusters") << " Creating corrected SC ";
00230     
00231        reco::SuperCluster sc(E, it_super->position(), it_super->seed(), new_BC, deltaE);
00232        if (condP1 == 1 && condP2 == 1) sc.setPreshowerPlanesStatus(0);
00233        else if (condP1 == 1 && condP2 == 0) sc.setPreshowerPlanesStatus(1);
00234        else if (condP1 == 0 && condP2 == 1) sc.setPreshowerPlanesStatus(2);
00235        else if (condP1 == 0 && condP2 == 0) sc.setPreshowerPlanesStatus(3);
00236        
00237        if (sc.energy()*sin(sc.position().theta())>etThresh_)
00238          new_SC.push_back(sc);
00239          LogTrace("EcalClusters") << " SuperClusters energies: new E = " << sc.energy() << " vs. old E =" << it_super->energy();
00240       
00241    } // end of cycle over SCs
00242 
00243    // copy the preshower clusters into collections and put in the Event:
00244    clusters_p1->assign(clusters1.begin(), clusters1.end());
00245    clusters_p2->assign(clusters2.begin(), clusters2.end());
00246    // put collection of preshower clusters to the event
00247    evt.put( clusters_p1, preshClusterCollectionX_ );
00248    evt.put( clusters_p2, preshClusterCollectionY_ );
00249    LogTrace("EcalClusters") << "Preshower clusters added to the event" ;
00250 
00251    // put collection of corrected super clusters to the event
00252    superclusters_p->assign(new_SC.begin(), new_SC.end());
00253    evt.put(superclusters_p, assocSClusterCollection_);
00254    LogTrace("EcalClusters") << "Corrected SClusters added to the event" ;
00255 
00256    if (topology_p)
00257      delete topology_p;
00258 
00259    nEvt_++;
00260 
00261 }
00262 
00263 void PreshowerClusterProducer::set(const edm::EventSetup& es) {
00264 
00265   es.get<ESGainRcd>().get(esgain_);
00266   const ESGain *gain = esgain_.product();
00267 
00268   double ESGain = gain->getESGain();
00269 
00270   es.get<ESMIPToGeVConstantRcd>().get(esMIPToGeV_);
00271   const ESMIPToGeVConstant *mipToGeV = esMIPToGeV_.product();
00272 
00273   mip_ = (ESGain == 1) ? mipToGeV->getESValueLow() : mipToGeV->getESValueHigh(); 
00274 
00275   es.get<ESChannelStatusRcd>().get(esChannelStatus_);
00276 
00277   es.get<ESEEIntercalibConstantsRcd>().get(esEEInterCalib_);
00278   const ESEEIntercalibConstants *esEEInterCalib = esEEInterCalib_.product();
00279 
00280   // both planes work
00281   gamma0_ = (ESGain == 1) ? esEEInterCalib->getGammaLow0() : esEEInterCalib->getGammaHigh0();
00282   alpha0_ = (ESGain == 1) ? esEEInterCalib->getAlphaLow0() : esEEInterCalib->getAlphaHigh0();
00283 
00284   // only first plane works 
00285   gamma1_ = (ESGain == 1) ? esEEInterCalib->getGammaLow1() : esEEInterCalib->getGammaHigh1();
00286   alpha1_ = (ESGain == 1) ? esEEInterCalib->getAlphaLow1() : esEEInterCalib->getAlphaHigh1();
00287 
00288   // only second plane works
00289   gamma2_ = (ESGain == 1) ? esEEInterCalib->getGammaLow2() : esEEInterCalib->getGammaHigh2();
00290   alpha2_ = (ESGain == 1) ? esEEInterCalib->getAlphaLow2() : esEEInterCalib->getAlphaHigh2();
00291 
00292   // both planes do not work
00293   gamma3_ = (ESGain == 1) ? esEEInterCalib->getGammaLow3() : esEEInterCalib->getGammaHigh3();
00294   alpha3_ = (ESGain == 1) ? esEEInterCalib->getAlphaLow3() : esEEInterCalib->getAlphaHigh3();
00295 
00296 }