EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4HitContainer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4HitContainer.cc
1 #include "PHG4HitContainer.h"
2 
3 #include "PHG4Hit.h"
4 #include "PHG4Hitv1.h"
5 
6 #include <phool/phool.h>
7 
8 #include <TSystem.h>
9 
10 #include <cstdlib>
11 
12 using namespace std;
13 
15  : id(-1), hitmap(), layers()
16 {
17 }
18 
19 PHG4HitContainer::PHG4HitContainer(const std::string &nodename)
20  : id(PHG4HitDefs::get_volume_id(nodename)), hitmap(), layers()
21 {
22 }
23 
24 void
26 {
27  while(hitmap.begin() != hitmap.end())
28  {
29  delete hitmap.begin()->second;
30  hitmap.erase(hitmap.begin());
31  }
32  return;
33 }
34 
35 void
36 PHG4HitContainer::identify(ostream& os) const
37 {
38  ConstIterator iter;
39  os << "Number of hits: " << size() << endl;
40  for (iter = hitmap.begin(); iter != hitmap.end(); ++iter)
41  {
42  os << "hit key 0x" << hex << iter->first << dec << endl;
43  (iter->second)->identify();
44  }
45  set<unsigned int>::const_iterator siter;
46  os << "Number of layers: " << num_layers() << endl;
47  for (siter = layers.begin(); siter != layers.end(); ++siter)
48  {
49  os << "layer : " << *siter << endl;
50  }
51  return;
52 }
53 
55 PHG4HitContainer::getmaxkey(const unsigned int detid)
56 {
57  ConstRange miter = getHits(detid);
58  // first handle 2 special cases where there is no hit in the current layer
59  // no hits in this layer and higher layers (lower layers can contain hits)
60  if (miter.first == hitmap.end())
61  {
62  return 0;
63  }
64  // no hits in this layer - but hits in higher layers
65  if (miter.first == miter.second)
66  {
67  return 0;
68  }
69  PHG4HitDefs::keytype detidlong = detid;
70  PHG4HitDefs::keytype shiftval = detidlong << PHG4HitDefs::hit_idbits;
71  ConstIterator lastlayerentry = miter.second;
72  --lastlayerentry;
73  PHG4HitDefs::keytype iret = lastlayerentry->first - shiftval; // subtract layer mask
74  return iret;
75 }
76 
77 
79 PHG4HitContainer::genkey(const unsigned int detid)
80 {
81  PHG4HitDefs::keytype detidlong = detid;
82  if ((detidlong >> PHG4HitDefs::keybits) > 0)
83  {
84  cout << PHWHERE << " detector id too large: " << detid << endl;
85  gSystem->Exit(1);
86  }
87  PHG4HitDefs::keytype shiftval = detidlong << PHG4HitDefs::hit_idbits;
88  // cout << "max index: " << (detminmax->second)->first << endl;
89  // after removing hits with no energy deposition, we have holes
90  // in our hit ranges. This construct will get us the last hit in
91  // a layer and return it's hit id. Adding 1 will put us at the end of this layer
92  PHG4HitDefs::keytype hitid = getmaxkey(detid);
93  hitid++;
94  PHG4HitDefs::keytype newkey = hitid | shiftval;
95  if (hitmap.find(newkey) != hitmap.end())
96  {
97  cout << PHWHERE << " duplicate key: 0x"
98  << hex << newkey << dec
99  << " for detector " << detid
100  << " hitmap.size: " << hitmap.size()
101  << " hitid: " << hitid << " exiting now" << endl;
102  exit(1);
103  }
104  return newkey;
105 }
106 
109 {
110  PHG4HitDefs::keytype key = newhit->get_hit_id();
111  if (hitmap.find(key) != hitmap.end())
112  {
113  cout << "hit with id 0x" << hex << key << dec << " exists already" << endl;
114  return hitmap.find(key);
115  }
117  unsigned int detid = detidlong;
118  layers.insert(detid);
119  return hitmap.insert( std::make_pair( key, newhit ) ).first;
120 }
121 
123 PHG4HitContainer::AddHit(const unsigned int detid, PHG4Hit *newhit)
124 {
125  PHG4HitDefs::keytype key = genkey(detid);
126  layers.insert(detid);
127  newhit->set_hit_id(key);
128  return hitmap.insert( std::make_pair( key, newhit ) ).first;
129 }
130 
132 {
133  PHG4HitDefs::keytype detidlong = detid;
134  if ((detidlong >> PHG4HitDefs::keybits) > 0)
135  {
136  cout << " detector id too large: " << detid << endl;
137  exit(1);
138  }
139  PHG4HitDefs::keytype keylow = detidlong << PHG4HitDefs::hit_idbits;
140  PHG4HitDefs::keytype keyup = ((detidlong + 1) << PHG4HitDefs::hit_idbits) -1 ;
141  ConstRange retpair;
142  retpair.first = hitmap.lower_bound(keylow);
143  retpair.second = hitmap.upper_bound(keyup);
144  return retpair;
145 }
146 
148 { return std::make_pair( hitmap.begin(), hitmap.end() ); }
149 
150 
152 {
154  if(it == hitmap.end())
155  {
156  hitmap[key] = new PHG4Hitv1();
157  it = hitmap.find(key);
158  PHG4Hit* mhit = it->second;
159  mhit->set_hit_id(key);
160  mhit->set_edep(0.);
161  layers.insert(mhit->get_layer()); // add layer to our set of layers
162  }
163  return it;
164 }
165 
167 {
169  if(it != hitmap.end())
170  {
171  return it->second;
172  }
173 
174  return nullptr;
175 }
176 
177 void
179 {
180  // unsigned int hitsbef = hitmap.size();
181  Iterator itr = hitmap.begin();
182  Iterator last = hitmap.end();
183  for (; itr != last; )
184  {
185  PHG4Hit *hit = itr->second;
186  if (hit->get_edep() == 0)
187  {
188  delete hit;
189  hitmap.erase(itr++);
190  }
191  else
192  {
193  ++itr;
194  }
195  }
196 // unsigned int hitsafter = hitmap.size();
197 // cout << "hist before: " << hitsbef
198 // << ", hits after: " << hitsafter << endl;
199  return;
200 }
201