LDMX Software
ElectronicsMap.h
Go to the documentation of this file.
1
9#ifndef TOOLS_ELECTRONICSMAP_H_
10#define TOOLS_ELECTRONICSMAP_H_
11
12#include <map>
13#include <sstream>
14#include <vector>
15
16namespace ldmx {
17
27template <class ElectronicsID, class DetID>
29 public:
30 ElectronicsMap(bool want_d2e = false)
31 : eid2did_{ElectronicsID::MAX_INDEX, 0}, makeD2E_(want_d2e) {}
32
36 void clear() {
37 eid2did_ = std::vector<DetectorID::RawValue>(ElectronicsID::MAX_INDEX, 0);
38 did2eid_.clear();
39 }
40
44 void addEntry(ElectronicsID eid, DetID did) {
45 unsigned int index = eid.index();
46 if (index >= eid2did_.size()) {
47 EXCEPTION_RAISE("ElectronicsMapOverflow",
48 "Attempted to insert electronics with index " +
49 std::to_string(index) +
50 " which is larger than allowed in this map");
51 }
52 eid2did_[index] = did.raw();
53 if (makeD2E_) did2eid_.insert(std::make_pair(did.raw(), eid));
54 }
55
59 bool exists(ElectronicsID eid) const {
60 return (eid.index() < eid2did_.size() && eid2did_[eid.index()] != 0);
61 }
62
68 bool exists(DetID did) const {
69 if (makeD2E_) {
70 return did2eid_.find(did.raw()) == did2eid_.end();
71 } else {
72 for (auto i : eid2did_) {
73 if (i == did.raw()) return true;
74 }
75 return false;
76 }
77 }
78
82 DetID get(ElectronicsID eid) const {
83 if (eid.index() >= eid2did_.size())
84 return DetID();
85 else
86 return DetID(eid2did_[eid.index()]);
87 }
88
95 ElectronicsID get(DetID did) const {
96 if (makeD2E_) {
97 auto itr = did2eid_.find(did.raw());
98 if (itr == did2eid_.end()) {
99 std::stringstream ss;
100 ss << "Unable to find mapping for det id " << did;
101 EXCEPTION_RAISE("ElectronicsMapNotFound", ss.str());
102 }
103 return itr->second;
104 } else {
105 for (unsigned int i = 0; i < eid2did_.size(); i++) {
106 if (eid2did_[i] == did.raw()) return ElectronicsID::idFromIndex(i);
107 }
108 std::stringstream ss;
109 ss << "Unable to find mapping for det id " << did;
110 EXCEPTION_RAISE("ElectronicsMapNotFound", ss.str());
111 }
112 }
113
114 private:
118 std::vector<DetectorID::RawValue> eid2did_;
123
127 std::map<DetectorID::RawValue, ElectronicsID> did2eid_;
128};
129
130} // namespace ldmx
131#endif // TOOLS_ELECTRONICSMAP_H_
ElectronicsMap.
DetID get(ElectronicsID eid) const
Get the detector ID for this electronics ID.
void addEntry(ElectronicsID eid, DetID did)
Add an entry to the map.
void clear()
Remove all entries from the map.
ElectronicsID get(DetID did) const
Get the electronics ID for this detector ID This method is slow O(N) if the map is not configured for...
bool exists(DetID did) const
Tests if a given detector id is in the map This method is slow O(N) if the map is not configured for ...
std::vector< DetectorID::RawValue > eid2did_
Linear-time map for electronics (packed index) to raw detector id.
bool makeD2E_
Flag to determine if did2eid should be filled (resource optimization)
bool exists(ElectronicsID eid) const
Tests if a given electronics id is in the map.
std::map< DetectorID::RawValue, ElectronicsID > did2eid_
Log(N) map for raw detector id to electronics id.