Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-01 22:41:02

0001 #ifndef LayerHitMapCache_H
0002 #define LayerHitMapCache_H
0003 
0004 /** A cache adressable by DetLayer* and TrackingRegion* .
0005  *  Used to cache all the hits of a DetLayer.
0006  */
0007 
0008 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegion.h"
0009 #include "RecoTracker/TkHitPairs/interface/RecHitsSortedInPhi.h"
0010 #include "TrackingTools/TransientTrackingRecHit/interface/SeedingLayerSetsHits.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "DataFormats/TrackingRecHit/interface/mayown_ptr.h"
0013 
0014 class LayerHitMapCache {
0015 private:
0016   class SimpleCache {
0017   public:
0018     using ValueType = RecHitsSortedInPhi;
0019     using KeyType = int;
0020     SimpleCache(unsigned int initSize) : theContainer(initSize) {}
0021     SimpleCache(const SimpleCache&) = delete;
0022     SimpleCache& operator=(const SimpleCache&) = delete;
0023     SimpleCache(SimpleCache&&) = default;
0024     SimpleCache& operator=(SimpleCache&&) = default;
0025     ~SimpleCache() { clear(); }
0026     void resize(int size) { theContainer.resize(size); }
0027     const ValueType* get(KeyType key) const { return theContainer[key].get(); }
0028     /// add object to cache. It is caller responsibility to check that object is not yet there.
0029     void add(KeyType key, ValueType* value) {
0030       if (key >= int(theContainer.size()))
0031         resize(key + 1);
0032       theContainer[key].reset(value);
0033     }
0034     void extend(const SimpleCache& other) {
0035       // N.B. Here we assume that the lifetime of 'other' is longer than of 'this'.
0036       if (other.theContainer.size() > theContainer.size())
0037         resize(other.theContainer.size());
0038 
0039       for (size_t i = 0, size = other.theContainer.size(); i != size; ++i) {
0040         assert(get(i) == nullptr);  // We don't want to override any existing value
0041         auto v = other.get(i);
0042         if (v) {
0043           // pass by reference to denote that we don't own it
0044           theContainer[i].reset(*(other.get(i)));
0045         } else {
0046           theContainer[i].reset();
0047         }
0048       }
0049     }
0050     /// emptify cache, delete values associated to Key
0051     void clear() {
0052       for (auto& v : theContainer) {
0053         v.reset();
0054       }
0055     }
0056 
0057   private:
0058     std::vector<mayown_ptr<ValueType> > theContainer;
0059   };
0060 
0061 private:
0062   typedef SimpleCache Cache;
0063 
0064 public:
0065   LayerHitMapCache(unsigned int initSize = 50) : theCache(initSize) {}
0066   LayerHitMapCache(LayerHitMapCache&&) = default;
0067   LayerHitMapCache& operator=(LayerHitMapCache&&) = default;
0068 
0069   void clear() { theCache.clear(); }
0070 
0071   void extend(const LayerHitMapCache& other) { theCache.extend(other.theCache); }
0072 
0073   // Mainly for FastSim, overrides old hits if exists
0074   RecHitsSortedInPhi* add(const SeedingLayerSetsHits::SeedingLayer& layer, std::unique_ptr<RecHitsSortedInPhi> hits) {
0075     RecHitsSortedInPhi* ptr = hits.get();
0076     theCache.add(layer.index(), hits.release());
0077     return ptr;
0078   }
0079 
0080   const RecHitsSortedInPhi& operator()(const SeedingLayerSetsHits::SeedingLayer& layer, const TrackingRegion& region) {
0081     int key = layer.index();
0082     assert(key >= 0);
0083     const RecHitsSortedInPhi* lhm = theCache.get(key);
0084     if (lhm == nullptr) {
0085       auto tmp =
0086           add(layer, std::make_unique<RecHitsSortedInPhi>(region.hits(layer), region.origin(), layer.detLayer()));
0087       tmp->theOrigin = region.origin();
0088       lhm = tmp;
0089       LogDebug("LayerHitMapCache") << " I got" << lhm->all().second - lhm->all().first
0090                                    << " hits in the cache for: " << layer.detLayer();
0091     } else {
0092       // std::cout << region.origin() << " " <<  lhm->theOrigin << std::endl;
0093       LogDebug("LayerHitMapCache") << " I got" << lhm->all().second - lhm->all().first
0094                                    << " hits FROM THE cache for: " << layer.detLayer();
0095     }
0096     return *lhm;
0097   }
0098 
0099 private:
0100   Cache theCache;
0101 };
0102 
0103 #endif