00001 #include "RecoCaloTools/MetaCollections/interface/CaloRecHitMetaCollectionFast.h" 00002 #include <algorithm> 00003 00004 CaloRecHitMetaCollectionFast::CaloRecHitMetaCollectionFast() { 00005 dirty_=false; 00006 } 00007 00008 void CaloRecHitMetaCollectionFast::add(const CaloRecHit* hit) { 00009 hits_.push_back(hit); 00010 dirty_=true; 00011 size_++; 00012 } 00013 00014 class CRHMCVComp { 00015 public: 00016 typedef const CaloRecHit* T; 00017 typedef const DetId& key_type; 00018 00019 bool operator()(key_type a, T const& b) const { return a < b->detid(); } 00020 bool operator()(T const& a, key_type b) const { return a->detid() < b; } 00021 bool operator()(T const& a, T const& b) const { return a->detid() < b->detid(); } 00022 }; 00023 00024 00025 CaloRecHitMetaCollectionV::const_iterator CaloRecHitMetaCollectionFast::find(const DetId& id) const { 00026 if (dirty_) sort(); 00027 00028 CRHMCVComp comp; 00029 00030 std::vector<const CaloRecHit*>::const_iterator last=hits_.end(); 00031 std::vector<const CaloRecHit*>::const_iterator first=hits_.begin(); 00032 std::vector<const CaloRecHit*>::const_iterator loc =std::lower_bound(first, 00033 last, 00034 id, 00035 comp); 00036 return loc == last || comp(id, *loc) ? end() : const_iterator(this,loc - hits_.begin()); 00037 } 00038 00039 00040 const CaloRecHit* CaloRecHitMetaCollectionFast::at(const_iterator::offset_type i) const { 00041 if (dirty_) sort(); 00042 return ((i<0 || i>=(const_iterator::offset_type)size_)?(0):(hits_[i])); 00043 } 00044 00045 00046 void CaloRecHitMetaCollectionFast::sort() const { 00047 if (dirty_) { 00048 CRHMCVComp comp; 00049 std::sort(hits_.begin(),hits_.end(),comp); 00050 dirty_=false; 00051 } 00052 } 00053