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){}
25  void resize(int size) { theContainer.resize(size); }
26  const ValueType* get(KeyType key) const { return theContainer[key].get();}
29  if (key>=int(theContainer.size())) resize(key+1);
30  theContainer[key].reset(value);
31  }
32  void extend(const SimpleCache& other) {
33  // N.B. Here we assume that the lifetime of 'other' is longer than of 'this'.
34  if(other.theContainer.size() > theContainer.size())
35  resize(other.theContainer.size());
36 
37  for(size_t i=0, size=other.theContainer.size(); i != size; ++i) {
38  assert(get(i) == nullptr); // We don't want to override any existing value
39  theContainer[i].reset(*(other.get(i))); // pass by reference to denote that we don't own it
40  }
41  }
43  void clear() {
44  for ( auto & v : theContainer) { v.reset(); }
45  }
46  private:
47  std::vector<mayown_ptr<ValueType> > theContainer;
48  private:
49  SimpleCache(const SimpleCache &) { }
50  };
51 
52 private:
53  typedef SimpleCache Cache;
54 public:
55  LayerHitMapCache(unsigned int initSize=50) : theCache(initSize) { }
57 
58  void clear() { theCache.clear(); }
59 
60  void extend(const LayerHitMapCache& other) {
61  theCache.extend(other.theCache);
62  }
63 
64  // Mainly for FastSim, overrides old hits if exists
65  RecHitsSortedInPhi *add(const SeedingLayerSetsHits::SeedingLayer& layer, std::unique_ptr<RecHitsSortedInPhi> hits) {
66  RecHitsSortedInPhi *ptr = hits.get();
67  theCache.add(layer.index(), hits.release());
68  return ptr;
69  }
70 
71  const RecHitsSortedInPhi &
73  const edm::EventSetup & iSetup) {
74  int key = layer.index();
75  assert (key>=0);
76  const RecHitsSortedInPhi * lhm = theCache.get(key);
77  if (lhm==nullptr) {
78  auto tmp = add(layer, std::make_unique<RecHitsSortedInPhi>(region.hits(iSetup,layer), region.origin(), layer.detLayer()));
79  tmp->theOrigin = region.origin();
80  lhm = tmp;
81  LogDebug("LayerHitMapCache")<<" I got"<< lhm->all().second-lhm->all().first<<" hits in the cache for: "<<layer.detLayer();
82  }
83  else{
84  // std::cout << region.origin() << " " << lhm->theOrigin << std::endl;
85  LogDebug("LayerHitMapCache")<<" I got"<< lhm->all().second-lhm->all().first<<" hits FROM THE cache for: "<<layer.detLayer();
86  }
87  return *lhm;
88  }
89 
90 private:
91  Cache theCache;
92 };
93 
94 #endif
95 
#define LogDebug(id)
size
Write out results.
void extend(const SimpleCache &other)
GlobalPoint const & origin() const
LayerHitMapCache(LayerHitMapCache &&rh)
LayerHitMapCache(unsigned int initSize=50)
SimpleCache(const SimpleCache &)
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)
def move(src, dest)
Definition: eostools.py:510