File indexing completed on 2025-12-16 09:21:58
0001 #include "PHG4HitContainer.h"
0002
0003 #include "PHG4Hit.h"
0004 #include "PHG4Hitv1.h"
0005
0006 #include <phool/phool.h>
0007
0008 #include <TSystem.h>
0009
0010 #include <cstdlib>
0011
0012 PHG4HitContainer::PHG4HitContainer(const std::string &nodename)
0013 : id(PHG4HitDefs::get_volume_id(nodename))
0014
0015 {
0016 }
0017
0018 void PHG4HitContainer::Reset()
0019 {
0020 while (hitmap.begin() != hitmap.end())
0021 {
0022 delete hitmap.begin()->second;
0023 hitmap.erase(hitmap.begin());
0024 }
0025 return;
0026 }
0027
0028 void PHG4HitContainer::identify(std::ostream &os) const
0029 {
0030 ConstIterator iter;
0031 os << "Number of hits: " << size() << std::endl;
0032 for (iter = hitmap.begin(); iter != hitmap.end(); ++iter)
0033 {
0034 os << "hit key 0x" << std::hex << iter->first << std::dec << std::endl;
0035 (iter->second)->identify();
0036 }
0037 std::set<unsigned int>::const_iterator siter;
0038 os << "Number of layers: " << num_layers() << std::endl;
0039 for (siter = layers.begin(); siter != layers.end(); ++siter)
0040 {
0041 os << "layer : " << *siter << std::endl;
0042 }
0043 return;
0044 }
0045
0046 PHG4HitDefs::keytype
0047 PHG4HitContainer::getmaxkey(const unsigned int detid)
0048 {
0049 ConstRange miter = getHits(detid);
0050
0051
0052 if (miter.first == hitmap.end())
0053 {
0054 return 0;
0055 }
0056
0057 if (miter.first == miter.second)
0058 {
0059 return 0;
0060 }
0061 PHG4HitDefs::keytype detidlong = detid;
0062 PHG4HitDefs::keytype shiftval = detidlong << PHG4HitDefs::hit_idbits;
0063 ConstIterator lastlayerentry = miter.second;
0064 --lastlayerentry;
0065 PHG4HitDefs::keytype iret = lastlayerentry->first - shiftval;
0066 return iret;
0067 }
0068
0069 PHG4HitDefs::keytype
0070 PHG4HitContainer::genkey(const unsigned int detid)
0071 {
0072 PHG4HitDefs::keytype detidlong = detid;
0073 if ((detidlong >> PHG4HitDefs::keybits) > 0)
0074 {
0075 std::cout << PHWHERE << " detector id too large: " << detid << std::endl;
0076 gSystem->Exit(1);
0077 }
0078 PHG4HitDefs::keytype shiftval = detidlong << PHG4HitDefs::hit_idbits;
0079
0080
0081
0082
0083 PHG4HitDefs::keytype hitid = getmaxkey(detid);
0084 hitid++;
0085 PHG4HitDefs::keytype newkey = hitid | shiftval;
0086 if (hitmap.contains(newkey))
0087 {
0088 std::cout << PHWHERE << " duplicate key: 0x"
0089 << std::hex << newkey << std::dec
0090 << " for detector " << detid
0091 << " hitmap.size: " << hitmap.size()
0092 << " hitid: " << hitid << " exiting now" << std::endl;
0093 exit(1);
0094 }
0095 return newkey;
0096 }
0097
0098 PHG4HitContainer::ConstIterator
0099 PHG4HitContainer::AddHit(PHG4Hit *newhit)
0100 {
0101 PHG4HitDefs::keytype key = newhit->get_hit_id();
0102 if (hitmap.contains(key))
0103 {
0104 std::cout << "hit with id 0x" << std::hex << key << std::dec << " exists already" << std::endl;
0105 return hitmap.find(key);
0106 }
0107 PHG4HitDefs::keytype detidlong = key >> PHG4HitDefs::hit_idbits;
0108 unsigned int detid = detidlong;
0109 layers.insert(detid);
0110 return hitmap.insert(std::make_pair(key, newhit)).first;
0111 }
0112
0113 PHG4HitContainer::ConstIterator
0114 PHG4HitContainer::AddHit(const unsigned int detid, PHG4Hit *newhit)
0115 {
0116 PHG4HitDefs::keytype key = genkey(detid);
0117 layers.insert(detid);
0118 newhit->set_hit_id(key);
0119 return hitmap.insert(std::make_pair(key, newhit)).first;
0120 }
0121
0122 PHG4HitContainer::ConstRange PHG4HitContainer::getHits(const unsigned int detid) const
0123 {
0124 PHG4HitDefs::keytype detidlong = detid;
0125 if ((detidlong >> PHG4HitDefs::keybits) > 0)
0126 {
0127 std::cout << " detector id too large: " << detid << std::endl;
0128 exit(1);
0129 }
0130 PHG4HitDefs::keytype keylow = detidlong << PHG4HitDefs::hit_idbits;
0131 PHG4HitDefs::keytype keyup = ((detidlong + 1) << PHG4HitDefs::hit_idbits) - 1;
0132 ConstRange retpair;
0133 retpair.first = hitmap.lower_bound(keylow);
0134 retpair.second = hitmap.upper_bound(keyup);
0135 return retpair;
0136 }
0137
0138 PHG4HitContainer::ConstRange PHG4HitContainer::getHits() const
0139 {
0140 return std::make_pair(hitmap.begin(), hitmap.end());
0141 }
0142
0143 PHG4HitContainer::Iterator PHG4HitContainer::findOrAddHit(PHG4HitDefs::keytype key)
0144 {
0145 PHG4HitContainer::Iterator it = hitmap.find(key);
0146 if (it == hitmap.end())
0147 {
0148 hitmap[key] = new PHG4Hitv1();
0149 it = hitmap.find(key);
0150 PHG4Hit *mhit = it->second;
0151 mhit->set_hit_id(key);
0152 mhit->set_edep(0.);
0153 layers.insert(mhit->get_layer());
0154 }
0155 return it;
0156 }
0157
0158 PHG4Hit *PHG4HitContainer::findHit(PHG4HitDefs::keytype key)
0159 {
0160 PHG4HitContainer::ConstIterator it = hitmap.find(key);
0161 if (it != hitmap.end())
0162 {
0163 return it->second;
0164 }
0165
0166 return nullptr;
0167 }
0168
0169 void PHG4HitContainer::RemoveZeroEDep()
0170 {
0171
0172 Iterator itr = hitmap.begin();
0173 Iterator last = hitmap.end();
0174 for (; itr != last;)
0175 {
0176 PHG4Hit *hit = itr->second;
0177 if (hit->get_edep() == 0)
0178 {
0179 delete hit;
0180 hitmap.erase(itr++);
0181 }
0182 else
0183 {
0184 ++itr;
0185 }
0186 }
0187
0188
0189
0190 return;
0191 }