CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/RecoCaloTools/MetaCollections/src/CaloRecHitMetaCollection.cc

Go to the documentation of this file.
00001 #include "RecoCaloTools/MetaCollections/interface/CaloRecHitMetaCollection.h"
00002 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00003 
00004 class CaloRecHitMetaCollectionItem {
00005 public:
00006   virtual ~CaloRecHitMetaCollectionItem() {}
00007   virtual int find(const DetId& id) const = 0;
00008   virtual const CaloRecHit* at(int index) const = 0;
00009 };
00010 
00011 template <class T>
00012 class CaloRecHitMetaCollectionItemT : public CaloRecHitMetaCollectionItem {
00013 public:
00014   CaloRecHitMetaCollectionItemT(const T* coll, int start) : m_collection(coll),m_start(start) { }
00015   virtual int find(const DetId& id) const {
00016     typename T::const_iterator i;
00017     i=m_collection->find(id);
00018     return (i==m_collection->end())?(-1):(i-m_collection->begin()+m_start);
00019   }
00020   virtual const CaloRecHit* at(int index) const {
00021     return &((*m_collection)[index-m_start]);
00022   }
00023 private:
00024   const T* m_collection;
00025   int m_start;
00026 };
00027 
00028 typedef std::multimap<int, CaloRecHitMetaCollectionItem*>::const_iterator find_iterator;
00029 
00030 int CaloRecHitMetaCollection::findIndex(const DetId& id) const {
00031   return id.rawId()>>25;
00032 }
00033 
00034 CaloRecHitMetaCollection::const_iterator CaloRecHitMetaCollection::find(const DetId& id) const {
00035   std::pair<find_iterator,find_iterator> options=m_findTool.equal_range(findIndex(id));
00036   int pos=-1;
00037   for (find_iterator i=options.first; i!=options.second; i++) {
00038     pos=i->second->find(id);
00039     if (pos>=0) break;
00040   }
00041   return (pos<0)?(end()):(const_iterator(this,pos));
00042 }
00043 
00044 const CaloRecHit* CaloRecHitMetaCollection::at(const_iterator::offset_type i) const {
00045   std::map<int, CaloRecHitMetaCollectionItem*>::const_iterator q=m_items.lower_bound(i);
00046   return (q==m_items.end())?(0):(q->second->at(i));
00047 }
00048 
00049 CaloRecHitMetaCollection::~CaloRecHitMetaCollection() {
00050   for (std::map<int, CaloRecHitMetaCollectionItem*>::iterator i=m_items.begin(); i!=m_items.end(); i++)
00051     delete i->second;
00052 }
00053 
00054 
00055 void CaloRecHitMetaCollection::add(const HBHERecHitCollection* hbhe) {
00056   if (hbhe->size()==0) return; // do not add empty collections (can cause problems)
00057   CaloRecHitMetaCollectionItem* i=new CaloRecHitMetaCollectionItemT<HBHERecHitCollection>(hbhe,size_);
00058   size_+=hbhe->size();
00059   m_items.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(size_-1,i));
00060   m_findTool.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(findIndex(DetId(DetId::Hcal,HcalBarrel)),i));
00061   m_findTool.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(findIndex(DetId(DetId::Hcal,HcalEndcap)),i));
00062 }
00063 
00064 void CaloRecHitMetaCollection::add(const HORecHitCollection* ho) {
00065   if (ho->size()==0) return; // do not add empty collections (can cause problems)
00066   CaloRecHitMetaCollectionItem* i=new CaloRecHitMetaCollectionItemT<HORecHitCollection>(ho,size_);
00067   size_+=ho->size();
00068   m_items.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(size_-1,i));
00069   m_findTool.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(findIndex(DetId(DetId::Hcal,HcalOuter)),i));
00070 }
00071 
00072 void CaloRecHitMetaCollection::add(const HFRecHitCollection* hf) {
00073   if (hf->size()==0) return; // do not add empty collections (can cause problems)
00074   CaloRecHitMetaCollectionItem* i=new CaloRecHitMetaCollectionItemT<HFRecHitCollection>(hf,size_);
00075   size_+=hf->size();
00076   m_items.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(size_-1,i));
00077   m_findTool.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(findIndex(DetId(DetId::Hcal,HcalForward)),i));
00078 }
00079 
00080 
00081 void CaloRecHitMetaCollection::add(const EcalRecHitCollection* ecal) {
00082   if (ecal->size()==0) return; // do not add empty collections (can cause problems)
00083   CaloRecHitMetaCollectionItem* i=new CaloRecHitMetaCollectionItemT<EcalRecHitCollection>(ecal,size_);
00084   size_+=ecal->size();
00085   m_items.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(size_-1,i));
00086   m_findTool.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(findIndex(DetId(DetId::Ecal,EcalBarrel)),i));
00087   m_findTool.insert(std::pair<int,CaloRecHitMetaCollectionItem*>(findIndex(DetId(DetId::Ecal,EcalEndcap)),i));
00088 }
00089