3#include <FairRootFileSink.h>
13#include <range/v3/view.hpp>
14#define JSON_USE_IMPLICIT_CONVERSIONS 0
15#include <nlohmann/json.hpp>
17namespace fs = std::filesystem;
18namespace nlh = nlohmann;
25 auto counter = record.find(trigID);
26 if (counter == record.end())
28 counter = record.insert({ trigID, 0 }).first;
30 (counter->second) += counts;
36 auto criteria = [](
auto pre,
auto post) {
return pre.second < post.second; };
37 auto result = std::max_element(record.begin(), record.end(), criteria);
38 return (result != record.end()) ? result->first : std::optional<unsigned int>{};
45 auto [moduleID, trigID] =
pair;
46 auto record = records_.find(moduleID);
47 if (record == records_.end())
51 record->second.add(trigID, side, counts);
54 void TrigIDMappingFinder::AutoResolve(TrigIDMap& trigIDMap)
56 for (
auto& [moduleID, trigIDs] : trigIDMap)
58 if (trigIDs.first == 0)
60 auto pre = trigIDMap.find(moduleID - 1);
61 auto post = trigIDMap.find(moduleID + 1);
62 auto is_existed = (pre != trigIDMap.end() and post != trigIDMap.end());
63 auto is_same_left = (pre->second.first == post->second.first);
64 auto is_same_right = (pre->second.second == post->second.second);
65 if (is_existed and is_same_left and is_same_right)
67 trigIDs = { pre->second.first, pre->second.second };
71 fmt::format(
"trigID can't be auto-resolved for moduleID: {}! Increase the event numbers to make "
72 "trigIDs on the two sides different.",
80 if (force_find or idMap_.empty())
84 return std::move(idMap_);
88 void TrigIDMappingFinder::FindTrigIDMap()
91 for (
const auto& [moduleID, record] : records_)
95 if (trigID_left.has_value() and trigID_right.has_value())
97 if (trigID_left.value() != trigID_right.value())
99 idMap_.insert({ moduleID, { trigID_left.value(), trigID_right.value() } });
103 idMap_.insert({ moduleID, { 0, 0 } });
108 R3BLOG(error, fmt::format(
"Empty record for moduleID {}", moduleID));
121 Print_screen(trigMap);
131 fmt::print(
"==============TrigIDMapping=================\n");
132 fmt::print(
"{0:^18s}|{1:^18s}|{2:^18s}\n",
"barID",
"trigID_l",
"trigID_r");
133 for (
unsigned int moduleID{ 1 }; moduleID <= numOfModule_; ++moduleID)
135 if (
auto trigID = trigMap.find(moduleID); trigID == trigMap.end())
137 fmt::print(
"{0:^18d}|{1:^18s}|{2:^18s}\n", moduleID,
"NAN",
"NAN");
141 fmt::print(
"{0:^18d}|{1:^18d}|{2:^18d}\n", moduleID, trigID->second.first, trigID->second.second);
144 fmt::print(
"============================================\n");
149 const auto filepath = fs::path{ fileDir_ } / fs::path{ filename_ };
150 fmt::print(
"Writing trigIDMap to JSON file {} ...\n", filepath);
151 auto ostream = std::ofstream(filepath, std::ios_base::out | std::ios_base::trunc);
152 auto jsonData = nlh::json::array();
153 auto jsonEntry = nlh::json::object();
154 for (
const auto& [moduleID, id_pair] : trigMap)
156 jsonEntry[
"barID"] = moduleID;
157 jsonEntry[
"trigID_left"] = id_pair.first;
158 jsonEntry[
"trigID_right"] = id_pair.second;
159 jsonData.push_back(jsonEntry);
161 std::sort(jsonData.begin(),
163 [](
const auto&
left,
const auto&
right) { return left.at(
"barID") < right.at(
"barID"); });
164 ostream << std::setw(4) << jsonData << std::endl;
170 const auto filepath = fs::path{ fileDir_ } / fs::path{ filename_ };
172 auto istream = std::ifstream(filepath);
173 if (not istream.is_open())
178 auto jsonData = nlh::json::parse(istream);
179 for (
const auto& jsonEntry : jsonData)
181 const auto moduleID = jsonEntry.at(
"barID").template get<int>();
182 const auto trigID_left = jsonEntry.at(
"trigID_left").template get<int>();
183 const auto trigID_right = jsonEntry.at(
"trigID_right").template get<int>();
184 trigMap.insert({ moduleID, std::make_pair(trigID_left, trigID_right) });
#define R3BLOG(severity, x)
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
void Save_json(const TrigIDMappingFinder::TrigIDMap &trigMap) const
auto Read_json() const -> TrigIDMappingFinder::TrigIDMap
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 >