CMS 3D CMS Logo

JetCrystalsAssociator.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    JetCrystalsAssociator
00004 // Class:      JetCrystalsAssociator
00005 // 
00013 //
00014 // Original Author:  Simone Gennai
00015 //         Created:  Wed Apr 12 11:12:49 CEST 2006
00016 //
00017 //
00018 
00019 
00020 // system include files
00021 #include <memory>
00022 #include <string>
00023 #include <iostream>
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025 
00026 // user include files
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDProducer.h"
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/Framework/interface/MakerMacros.h"
00032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00033 #include "FWCore/ParameterSet/interface/InputTag.h"
00034 #include "FWCore/Framework/interface/ESHandle.h"
00035 
00036 
00037 #include "DataFormats/Common/interface/Ref.h"
00038 #include "DataFormats/JetReco/interface/Jet.h"
00039 #include "DataFormats/BTauReco/interface/JetCrystalsAssociation.h"
00040 
00041 //Calorimeter stuff
00042 #include "DataFormats/CaloTowers/interface/CaloTowerCollection.h"
00043 #include "DataFormats/DetId/interface/DetId.h"
00044 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00045 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00046 
00047 #include "DataFormats/CaloTowers/interface/CaloTowerDetId.h" 
00048 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00049 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00050 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00051 
00052 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00053 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00054 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00055 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00056 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00057 
00058 // Math
00059 #include "Math/GenVector/VectorUtil.h"
00060 #include "Math/GenVector/PxPyPzE4D.h"
00061 using namespace reco;
00062 
00063 //
00064 // class decleration
00065 //
00066 
00067 class JetCrystalsAssociator : public edm::EDProducer {
00068 
00069    public:
00070       explicit JetCrystalsAssociator(const edm::ParameterSet&);
00071       ~JetCrystalsAssociator();
00072 
00073 
00074       virtual void produce(edm::Event&, const edm::EventSetup&);
00075    private:
00076       std::auto_ptr<JetCrystalsAssociationCollection> associate( 
00077           const edm::Handle<CaloJetCollection> & jets,
00078           const edm::OrphanHandle<EMLorentzVectorCollection> & myLorentzRecHits) const;
00079 
00080 
00081   // ----------member data ---------------------------
00082   edm::InputTag m_jetsSrc;
00083   edm::InputTag m_EBRecHits;
00084   edm::InputTag m_EERecHits;
00085 
00086   double m_deltaRCut;
00087 };
00088 
00089 JetCrystalsAssociator::JetCrystalsAssociator(const edm::ParameterSet& iConfig)
00090 {
00091   produces<reco::JetCrystalsAssociationCollection>();
00092   produces<reco::EMLorentzVectorCollection>();
00093 
00094   m_EBRecHits = iConfig.getParameter<edm::InputTag>("EBRecHits");
00095   m_EERecHits = iConfig.getParameter<edm::InputTag>("EERecHits");
00096   m_jetsSrc   = iConfig.getParameter<edm::InputTag>("jets");
00097   m_deltaRCut = iConfig.getParameter<double>("coneSize");
00098 }
00099 
00100 
00101 JetCrystalsAssociator::~JetCrystalsAssociator()
00102 {
00103  
00104   // do anything here that needs to be done at desctruction time
00105   // (e.g. close files, deallocate resources etc.)
00106 
00107 }
00108 
00109 
00110 //
00111 // member functions
00112 //
00113 
00114 // ------------ method called to produce the data  ------------
00115 void
00116 JetCrystalsAssociator::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00117 {
00118   using namespace edm;
00119   using namespace reco;
00120   using namespace std;
00121 
00122   // geometry initialization
00123   ESHandle<CaloGeometry> geometry;
00124   iSetup.get<CaloGeometryRecord>().get(geometry);
00125   
00126   const CaloSubdetectorGeometry* EB = geometry->getSubdetectorGeometry(DetId::Ecal,EcalBarrel);
00127    const CaloSubdetectorGeometry* EE = geometry->getSubdetectorGeometry(DetId::Ecal,EcalEndcap);
00128    // end 
00129    
00130    Handle<CaloJetCollection> jets;
00131    iEvent.getByLabel(m_jetsSrc, jets);
00132    
00133    // get calo towers collection
00134    //   Handle<CaloTowerCollection> caloTowers; 
00135    //   iEvent.getByLabel(m_towersSrc, caloTowers);
00136 
00137    // calculation of ECAL isolation
00138    Handle<EBRecHitCollection> EBRecHits;
00139    Handle<EERecHitCollection> EERecHits;
00140    iEvent.getByLabel( m_EBRecHits, EBRecHits );
00141    iEvent.getByLabel( m_EERecHits, EERecHits );
00142    
00143    std::auto_ptr<EMLorentzVectorCollection> jetRecHits( new EMLorentzVectorCollection() );
00144    //loop on jets and associate
00145    for (size_t t = 0; t < jets->size(); t++)
00146     {
00147       const std::vector<CaloTowerPtr>  myTowers=(*jets)[t].getCaloConstituents();
00148       //      cout <<"Jet id "<<t<<endl;
00149       //      cout <<"Tower size "<<myTowers.size()<<endl;
00150       for (unsigned int iTower = 0; iTower < myTowers.size(); iTower++)
00151         {
00152           CaloTowerPtr theTower = myTowers[iTower];
00153           size_t numRecHits = theTower->constituentsSize();
00154         // access CaloRecHits
00155         for (size_t j = 0; j < numRecHits; j++) {
00156           DetId RecHitDetID=theTower->constituent(j);
00157           DetId::Detector DetNum=RecHitDetID.det();
00158           if( DetNum == DetId::Ecal ){
00159             int EcalNum =  RecHitDetID.subdetId();
00160             if( EcalNum == 1 ){
00161               EBDetId EcalID = RecHitDetID;
00162               EBRecHitCollection::const_iterator theRecHit=EBRecHits->find(EcalID);
00163               if(theRecHit != EBRecHits->end()){
00164                 DetId id = theRecHit->detid();
00165                 const CaloCellGeometry* this_cell = EB->getGeometry(id);
00166                 if (this_cell) {
00167                   GlobalPoint posi = this_cell->getPosition();
00168                   double energy = theRecHit->energy();
00169                   double eta = posi.eta();
00170                   double phi = posi.phi();
00171                   double theta = posi.theta();
00172                   if(theta > M_PI) theta = 2 * M_PI- theta;
00173                   double et = energy * sin(theta);
00174                   // cout <<"Et "<<et<<endl;
00175                   EMLorentzVector p(et, eta, phi, energy);
00176                   jetRecHits->push_back(p);
00177                 }
00178               }
00179             } else if ( EcalNum == 2 ) {
00180               EEDetId EcalID = RecHitDetID;
00181               EERecHitCollection::const_iterator theRecHit=EERecHits->find(EcalID);         
00182               if(theRecHit != EBRecHits->end()){
00183                 DetId id = theRecHit->detid();
00184                 const CaloCellGeometry* this_cell = EE->getGeometry(id);
00185                 if (this_cell) {
00186                   GlobalPoint posi = this_cell->getPosition();
00187                   double energy = theRecHit->energy();
00188                   double eta = posi.eta();
00189                   double phi = posi.phi();
00190                   double theta = posi.theta();
00191                   if (theta > M_PI) theta = 2 * M_PI - theta;
00192                   double et = energy * sin(theta);
00193                   // cout <<"Et "<<et<<endl;
00194                   EMLorentzVector p(et, eta, phi, energy);
00195                   jetRecHits->push_back(p);
00196                 }
00197               }
00198             }
00199           }
00200         }
00201       }
00202     }
00203 
00204   edm::OrphanHandle <reco::EMLorentzVectorCollection> myRecHits = iEvent.put(jetRecHits);
00205 
00206   std::auto_ptr<JetCrystalsAssociationCollection> jetCrystals = associate(jets,myRecHits);
00207   iEvent.put( jetCrystals );
00208 }
00209 
00210 std::auto_ptr<JetCrystalsAssociationCollection> JetCrystalsAssociator::associate( 
00211         const edm::Handle<CaloJetCollection> & jets,
00212         const edm::OrphanHandle<EMLorentzVectorCollection> & myLorentzRecHits) const
00213 {
00214   // we know we will save an element per input jet
00215   std::auto_ptr<JetCrystalsAssociationCollection> outputCollection( new JetCrystalsAssociationCollection( jets->size() ) );
00216 
00217   //loop on jets and associate
00218   for (size_t j = 0; j < jets->size(); j++) {
00219     (*outputCollection)[j].first = edm::RefToBase<Jet>(CaloJetRef(jets, j));
00220     for (size_t t = 0; t < myLorentzRecHits->size(); t++) {
00221       double delta = ROOT::Math::VectorUtil::DeltaR((*jets)[j].p4().Vect(), (*myLorentzRecHits)[t]);
00222       if (delta < m_deltaRCut)
00223         (*outputCollection)[j].second.push_back( EMLorentzVectorRef(myLorentzRecHits, t) );
00224     }
00225   }  
00226   return outputCollection;
00227 }
00228 
00229 //define this as a plug-in
00230 DEFINE_FWK_MODULE(JetCrystalsAssociator);

Generated on Tue Jun 9 17:43:07 2009 for CMSSW by  doxygen 1.5.4