11#include <nlohmann/json_fwd.hpp>
14#define JSON_USE_IMPLICIT_CONVERSIONS 0
15#include <nlohmann/json.hpp>
17namespace nlh = nlohmann;
24 auto counter = record.find(trigID);
25 if (counter == record.end())
27 counter = record.insert({ trigID, 0 }).first;
29 (counter->second) += counts;
35 auto criteria = [](
auto pre,
auto post) {
return pre.second < post.second; };
36 auto result = std::max_element(record.begin(), record.end(), criteria);
37 return (result != record.end()) ? result->first : std::optional<unsigned int>{};
44 auto [moduleID, trigID] =
pair;
45 auto record =
records_.find(moduleID);
50 record->second.add(trigID, side, counts);
55 for (
auto& [moduleID, trigIDs] : trigIDMap)
57 if (trigIDs.first == 0)
59 auto pre = trigIDMap.find(moduleID - 1);
60 auto post = trigIDMap.find(moduleID + 1);
61 auto is_existed = (pre != trigIDMap.end() and post != trigIDMap.end());
62 auto is_same_left = (pre->second.first == post->second.first);
63 auto is_same_right = (pre->second.second == post->second.second);
64 if (is_existed and is_same_left and is_same_right)
66 trigIDs = { pre->second.first, pre->second.second };
70 fmt::format(
"trigID can't be auto-resolved for moduleID: {}! Increase the event numbers to make "
71 "trigIDs on the two sides different.",
79 if (force_find or
idMap_.empty())
90 for (
const auto& [moduleID, record] :
records_)
94 if (trigID_left.has_value() and trigID_right.has_value())
96 if (trigID_left.value() != trigID_right.value())
98 idMap_.insert({ moduleID, { trigID_left.value(), trigID_right.value() } });
102 idMap_.insert({ moduleID, { 0, 0 } });
107 R3BLOG(error, fmt::format(
"Empty record for moduleID {}", moduleID));
130 fmt::print(
"==============TrigIDMapping=================\n");
131 fmt::print(
"{0:^18s}|{1:^18s}|{2:^18s}\n",
"barID",
"trigID_l",
"trigID_r");
132 for (
unsigned int moduleID{ 1 }; moduleID <=
numOfModule_; ++moduleID)
134 if (
auto trigID = trigMap.find(moduleID); trigID == trigMap.end())
136 fmt::print(
"{0:^18d}|{1:^18s}|{2:^18s}\n", moduleID,
"NAN",
"NAN");
140 fmt::print(
"{0:^18d}|{1:^18d}|{2:^18d}\n", moduleID, trigID->second.first, trigID->second.second);
143 fmt::print(
"============================================\n");
149 fmt::print(
"Writing trigIDMap to JSON file {} ...\n", filepath);
150 auto ostream = std::ofstream(filepath, std::ios_base::out | std::ios_base::trunc);
151 auto jsonData = nlh::json::array();
152 auto jsonEntry = nlh::json::object();
153 for (
const auto& [moduleID, id_pair] : trigMap)
155 jsonEntry[
"barID"] = moduleID;
156 jsonEntry[
"trigID_left"] = id_pair.first;
157 jsonEntry[
"trigID_right"] = id_pair.second;
158 jsonData.push_back(jsonEntry);
160 std::sort(jsonData.begin(),
162 [](
const auto&
left,
const auto&
right) { return left.at(
"barID") < right.at(
"barID"); });
163 ostream << std::setw(4) << jsonData <<
"\n";
171 auto istream = std::ifstream(filepath);
172 if (not istream.is_open())
177 auto jsonData = nlh::json::parse(istream);
178 for (
const auto& jsonEntry : jsonData)
180 const auto moduleID = jsonEntry.at(
"barID").template get<int>();
181 const auto trigID_left = jsonEntry.at(
"trigID_left").template get<int>();
182 const auto trigID_right = jsonEntry.at(
"trigID_right").template get<int>();
183 trigMap.insert({ moduleID, std::make_pair(trigID_left, trigID_right) });
#define R3BLOG(severity, x)
std::unordered_map< unsigned int, TrigIDMappingRecorder > records_
void add_id_pair(std::pair< unsigned int, unsigned int > pair, R3B::Side side, unsigned int counts=1)
auto extract_trigIDMap(bool force_find=false) -> TrigIDMap
std::unordered_map< unsigned int, std::pair< unsigned int, unsigned int > > TrigIDMap
static void AutoResolve(TrigIDMap &trigIDMap)
TrigIDMappingPrintFormat format_
unsigned int numOfModule_
void Save_json(const TrigIDMappingFinder::TrigIDMap &trigMap) const
auto Read_json() const -> TrigIDMappingFinder::TrigIDMap
void Print_screen(const TrigIDMappingFinder::TrigIDMap &trigMap) const
void Print(const TrigIDMappingFinder::TrigIDMap &trigMap) const
Simulation of NeuLAND Bar/Paddle.
void add(unsigned int trigID, R3B::Side side, unsigned int counts=1)
auto findID(R3B::Side side) const -> std::optional< unsigned int >