00001 #ifndef LayerHitMapCache_H
00002 #define LayerHitMapCache_H
00003
00008 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegion.h"
00009 #include "RecoTracker/TkHitPairs/interface/LayerHitMap.h"
00010 #include "RecoTracker/TkSeedingLayers/interface/SeedingLayer.h"
00011 #include "FWCore/Framework/interface/EventSetup.h"
00012
00013
00014 class LayerHitMapCache {
00015
00016 private:
00017
00018 template <class KeyType, class ValueType> class SimpleCache {
00019 public:
00020 SimpleCache(int initSize) { reserve(initSize); }
00021 virtual ~SimpleCache() { clear(); }
00022 void reserve(int size) { theContainer.reserve(size); }
00023 const ValueType* get(const KeyType & key) {
00024 for (ConstItr it = theContainer.begin(); it != theContainer.end(); it++) {
00025 if ( it->first == key) return it->second;
00026 }
00027 return 0;
00028 }
00030 void add(const KeyType & key, const ValueType * value) {
00031 theContainer.push_back( std::make_pair(key,value));
00032 }
00034 virtual void clear() {
00035 for (ConstItr i=theContainer.begin(); i!= theContainer.end(); i++) { delete i->second; }
00036 theContainer.clear();
00037 }
00038 protected:
00039 typedef std::pair< KeyType, const ValueType * > KeyValuePair;
00040 std::vector< KeyValuePair > theContainer;
00041 typedef typename std::vector< KeyValuePair >::const_iterator ConstItr;
00042 private:
00043 SimpleCache(const SimpleCache &) { }
00044 };
00045
00046 private:
00047
00048 typedef const DetLayer * LayerRegionKey;
00049 typedef SimpleCache<LayerRegionKey, LayerHitMap> Cache;
00050 public:
00051 LayerHitMapCache(int initSize=50) { theCache = new Cache(initSize); }
00052
00053 ~LayerHitMapCache() { delete theCache; }
00054
00055 void clear() { theCache->clear(); }
00056
00057 const LayerHitMap & operator()(
00058 const ctfseeding::SeedingLayer * layer, const TrackingRegion & region,
00059 const edm::Event & iEvent, const edm::EventSetup & iSetup) {
00060
00061 LayerRegionKey key(layer->detLayer());
00062 const LayerHitMap * lhm = theCache->get(key);
00063 if (lhm==0) {
00064 lhm=new LayerHitMap( layer->detLayer(), region.hits(iEvent,iSetup,layer));
00065 theCache->add( key, lhm);
00066 }
00067 return *lhm;
00068 }
00069
00070 public:
00071 LayerHitMapCache(const LayerHitMapCache &) { }
00072
00073 private:
00074 Cache * theCache;
00075 };
00076
00077 #endif
00078