LDMX Software
TrackerDaqMap.cxx
1#include "Tracking/Reco/TrackerDaqMap.h"
2
3#include <fstream>
4#include <nlohmann/json.hpp>
5
6#include "Framework/Exception/Exception.h"
7
8namespace tracking::reco {
9
11 std::ifstream in(path);
12 if (!in) {
13 EXCEPTION_RAISE(
14 "FileNotFound",
15 "TrackerDaqMap could not open DAQ map file '" + path + "'.");
16 }
17
18 nlohmann::json doc;
19 try {
20 in >> doc;
21 } catch (const nlohmann::json::parse_error& e) {
22 EXCEPTION_RAISE("BadFormat",
23 "TrackerDaqMap failed to parse DAQ map file '" + path +
24 "': " + e.what());
25 }
26
27 if (!doc.contains("sensors") || !doc.at("sensors").is_array() ||
28 doc.at("sensors").empty()) {
29 EXCEPTION_RAISE("BadFormat", "TrackerDaqMap file '" + path +
30 "' has no non-empty 'sensors' array.");
31 }
32
33 TrackerDaqMap map;
34 for (const auto& s : doc.at("sensors")) {
35 for (const char* required :
36 {"feb", "hybrid", "layer_id", "n_strips", "first_strip", "reversed"}) {
37 if (!s.contains(required)) {
38 EXCEPTION_RAISE("BadFormat", "TrackerDaqMap file '" + path +
39 "' has a sensor entry missing '" +
40 required + "': " + s.dump());
41 }
42 }
43
44 const auto feb = s.at("feb").get<uint8_t>();
45 const auto hybrid = s.at("hybrid").get<uint8_t>();
46 const uint16_t k = key(feb, hybrid);
47 if (map.sensors_.count(k) != 0u) {
48 EXCEPTION_RAISE("BadFormat", "TrackerDaqMap file '" + path +
49 "' defines (feb=" + std::to_string(feb) +
50 ", hybrid=" + std::to_string(hybrid) +
51 ") more than once.");
52 }
53
54 SensorInfo info;
55 info.layer_id_ = s.at("layer_id").get<int>();
56 info.n_strips_ = s.at("n_strips").get<int>();
57 info.first_strip_ = s.at("first_strip").get<int>();
58 info.reversed_ = s.at("reversed").get<bool>();
59 map.sensors_[k] = info;
60 }
61
62 return map;
63}
64
66 uint8_t hybrid) const {
67 auto it = sensors_.find(key(feb, hybrid));
68 if (it == sensors_.end()) {
69 EXCEPTION_RAISE("NotFound", "TrackerDaqMap has no entry for (feb=" +
70 std::to_string(feb) +
71 ", hybrid=" + std::to_string(hybrid) +
72 "); guard with has() before calling at().");
73 }
74 return it->second;
75}
76
77} // namespace tracking::reco
Tracker DAQ map: the correspondence between an electronics sensor address (feb, hybrid) and the detec...
std::map< uint16_t, SensorInfo > sensors_
key = (feb << 8) | hybrid
const SensorInfo & at(uint8_t feb, uint8_t hybrid) const
Look up the sensor read by (feb, hybrid).
static TrackerDaqMap fromJsonFile(const std::string &path)
Load a DAQ map from a JSON file.
Everything the mapper needs to know about one physical sensor.