CMS 3D CMS Logo

LayerHitMapCache.h
Go to the documentation of this file.
1 #ifndef LayerHitMapCache_H
2 #define LayerHitMapCache_H
3 
13 
15 
16 private:
17 
18  class SimpleCache {
19  public:
21  using KeyType = int;
22  SimpleCache(unsigned int initSize) : theContainer(initSize){}
23  SimpleCache(const SimpleCache&) = delete;
24  SimpleCache& operator=(const SimpleCache&) = delete;
25  SimpleCache(SimpleCache&&) = default;
26  SimpleCache& operator=(SimpleCache&&) = default;
28  void resize(int size) { theContainer.resize(size); }
29  const ValueType* get(KeyType key) const { return theContainer[key].get();}
32  if (key>=int(theContainer.size())) resize(key+1);
33  theContainer[key].reset(value);
34  }
35  void extend(const SimpleCache& other) {
36  // N.B. Here we assume that the lifetime of 'other' is longer than of 'this'.
37  if(other.theContainer.size() > theContainer.size())
38  resize(other.theContainer.size());
39 
40  for(size_t i=0, size=other.theContainer.size(); i != size; ++i) {
41  assert(get(i) == nullptr); // We don't want to override any existing value
42  theContainer[i].reset(*(other.get(i))); // pass by reference to denote that we don't own it
43  }
44  }
46  void clear() {
47  for ( auto & v : theContainer) { v.reset(); }
48  }
49  private:
50  std::vector<mayown_ptr<ValueType> > theContainer;
51  };
52 
53 private:
54  typedef SimpleCache Cache;
55 public:
56  LayerHitMapCache(unsigned int initSize=50) : theCache(initSize) { }
59 
60 
61  void clear() { theCache.clear(); }
62 
63  void extend(const LayerHitMapCache& other) {
64  theCache.extend(other.theCache);
65  }
66 
67  // Mainly for FastSim, overrides old hits if exists
68  RecHitsSortedInPhi *add(const SeedingLayerSetsHits::SeedingLayer& layer, std::unique_ptr<RecHitsSortedInPhi> hits) {
69  RecHitsSortedInPhi *ptr = hits.get();
70  theCache.add(layer.index(), hits.release());
71  return ptr;
72  }
73 
74  const RecHitsSortedInPhi &
76  const edm::EventSetup & iSetup) {
77  int key = layer.index();
78  assert (key>=0);
79  const RecHitsSortedInPhi * lhm = theCache.get(key);
80  if (lhm==nullptr) {
81  auto tmp = add(layer, std::make_unique<RecHitsSortedInPhi>(region.hits(iSetup,layer), region.origin(), layer.detLayer()));
82  tmp->theOrigin = region.origin();
83  lhm = tmp;
84  LogDebug("LayerHitMapCache")<<" I got"<< lhm->all().second-lhm->all().first<<" hits in the cache for: "<<layer.detLayer();
85  }
86  else{
87  // std::cout << region.origin() << " " << lhm->theOrigin << std::endl;
88  LogDebug("LayerHitMapCache")<<" I got"<< lhm->all().second-lhm->all().first<<" hits FROM THE cache for: "<<layer.detLayer();
89  }
90  return *lhm;
91  }
92 
93 private:
94  Cache theCache;
95 };
96 
97 #endif
98 
#define LogDebug(id)
size
Write out results.
void extend(const SimpleCache &other)
SimpleCache & operator=(const SimpleCache &)=delete
GlobalPoint const & origin() const
LayerHitMapCache(unsigned int initSize=50)
std::vector< mayown_ptr< ValueType > > theContainer
const RecHitsSortedInPhi & operator()(const SeedingLayerSetsHits::SeedingLayer &layer, const TrackingRegion &region, const edm::EventSetup &iSetup)
void add(KeyType key, ValueType *value)
add object to cache. It is caller responsibility to check that object is not yet there.
Definition: value.py:1
void clear()
emptify cache, delete values associated to Key
RecHitsSortedInPhi * add(const SeedingLayerSetsHits::SeedingLayer &layer, std::unique_ptr< RecHitsSortedInPhi > hits)
virtual Hits hits(const edm::EventSetup &es, const SeedingLayerSetsHits::SeedingLayer &layer) const =0
get hits from layer compatible with region constraints
const DetLayer * detLayer() const
void extend(const LayerHitMapCache &other)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
const ValueType * get(KeyType key) const
SimpleCache(unsigned int initSize)