10#include <fairlogger/Logger.h>
11#include <fmt/format.h>
13#include <magic_enum/magic_enum.hpp>
16#include <unordered_map>
22#include <fmt/ranges.h>
23#include <range/v3/algorithm/adjacent_find.hpp>
24#include <range/v3/algorithm/all_of.hpp>
25#include <range/v3/algorithm/any_of.hpp>
26#include <range/v3/algorithm/count_if.hpp>
27#include <range/v3/algorithm/find.hpp>
28#include <range/v3/algorithm/find_if.hpp>
29#include <range/v3/algorithm/for_each.hpp>
30#include <range/v3/algorithm/max_element.hpp>
31#include <range/v3/algorithm/sort.hpp>
32#include <range/v3/range/conversion.hpp>
33#include <range/v3/view/drop.hpp>
34#include <range/v3/view/enumerate.hpp>
35#include <range/v3/view/filter.hpp>
36#include <range/v3/view/iota.hpp>
37#include <range/v3/view/map.hpp>
38#include <range/v3/view/transform.hpp>
48 static constexpr auto quantile_ratio = 0.9;
49 auto* hist_y_proj =
histogram->ProjectionY(
"_py", module_num, module_num);
50 if (hist_y_proj->GetEntries() == 0)
52 LOGP(warn,
"Histogram {} has not entry at the bar number {}",
histogram->GetName(), module_num);
56 hist_y_proj->GetXaxis()->SetRangeUser(x_pos[0], x_pos[1]);
57 return { hist_y_proj->GetMean(), hist_y_proj->GetStdDev() };
60 void offset_to_tsync_ref_bar(Cal2HitPar& hit_par)
64 for (
auto& [module_number, module_par] : hit_par.GetListOfModuleParRef())
66 module_par.t_sync -= ref_t_sync;
70 template <
typename ViewType>
71 auto check_all_same_view(ViewType view) ->
bool
73 auto is_ok = ranges::adjacent_find(view, std::not_equal_to{}) == view.end();
87 for (
const auto double_plane_id : ranges::views::iota(0,
num_of_dp_))
89 const auto plane_id = double_plane_id * 2;
90 const auto local_ref_module_num = (plane_id + 1) *
BarsPerPlane;
98 "Time diff values of the bars in a horizontal plane",
106 "Time diff values of the bars in a horizontal double plane",
114 "Time diff values of the bars in a vertical double plane",
146 "Filling data of record type {:?} with bar numbers: [{}]",
159 LOGP(error,
"reference time is still 0.");
163 "Filling data of record type {:?} with time values: [{}]",
244 if (ref_bar_iter != collection.end())
251 "module numbers [{}] doesn't contain any reference numbers: [{}]",
252 fmt::join(collection | ranges::views::transform([](
const Point& point) {
return point.
module_num; }),
268 if (not ranges::all_of(collection,
269 [plane_num](
const auto& point) ->
bool
277 if (ref_bar_iter != collection.end())
288 const auto ref_dp_plane_id = ((
ModuleID2PlaneID(collection.front().module_num - 1) + offset) / 2);
296 if (not ranges::all_of(collection,
297 [ref_dp_plane_id, offset](
const auto& point) ->
bool
298 {
return (
ModuleID2PlaneID(point.module_num - 1) + offset) / 2 == ref_dp_plane_id; }))
305 if (ref_bar_iter != collection.end())
341 auto vertical_entry = [
this](
int vertical_number)
344 for (
int dp_id{}; dp_id <
num_of_dp_; ++dp_id)
346 auto plane_num = ((dp_id * 2 + 1) *
BarsPerPlane) + vertical_number;
353 auto vertical_bar_num_entries_view =
355 ranges::views::transform(
356 [vertical_entry](
auto vertical_offset_number)
357 {
return std::make_pair(vertical_offset_number, vertical_entry(vertical_offset_number)); });
358 auto element = ranges::max_element(vertical_bar_num_entries_view, std::less{}, &std::pair<int, double>::second);
359 return (*element).first;
364 const auto module_num = ((plane_id - 1) *
BarsPerPlane) + best_vert_bar_num;
368 return t_diff_prev - t_diff_after;
374 LOGP(info,
"Best vertical bar number: {}", best_vert_bar_num);
376 using EntryPair = std::pair<int, ValueErrorD>;
377 auto ref_plane_consecutive_t_diff =
379 ranges::views::transform(
380 [best_vert_bar_num,
this](
auto plane_id)
382 return (plane_id == 0)
386 ranges::to<std::vector<EntryPair>>();
387 ranges::sort(ref_plane_consecutive_t_diff, std::less{}, &EntryPair::first);
389 return ref_plane_consecutive_t_diff |
390 ranges::views::transform(
391 [&time_diff](
const auto& entry_pair)
393 time_diff += entry_pair.second;
394 return std::make_pair(entry_pair.first, time_diff);
396 ranges::to<std::unordered_map<int, ValueErrorD>>();
401 LOGP(info,
"Begin time synchronization calibration ...");
404 LOGP(info,
"TSync diffs from the top horizontal bars: {}", fmt::join(ref_time_diff_to_first,
", "));
408 const auto module_num = module_par.module_num;
410 const auto t_sync_corr = ref_time_diff_to_first.at(dp_id * 2);
412 const auto t_sync_val = [
this](
int p_module_num)
416 static constexpr auto top_bar_number = 50;
417 const auto vert_dist_diff =
424 module_par.t_sync = t_sync_val + t_sync_corr;
426 offset_to_tsync_ref_bar(hit_par);
427 LOGP(info,
"Time synchronization calibration ends.");
auto add_hist(std::unique_ptr< TH1 > hist) -> TH1 *
auto GetListOfModuleParRef() -> auto &
void fill_one_plane_histogram(double time_val, int module_num)
void calibrate(Cal2HitPar &hit_par)
static constexpr auto TIME_SUM_BIN_NUM
auto calculate_time_diff_to_prev_ref(int plane_id, int best_vert_bar_num) const -> ValueErrorD
std::vector< Point > buffer_points_
void end_of_event()
Actions in the end of the event.
auto try_in_same_dp(const std::vector< Point > &collection, int offset=0) -> bool
Check if all points are in the same horizontal or vertical double plane.
auto calculate_best_vertical_bar_num() const -> int
@ same_vertical_dp
All the points within vertical plane followed by horizontal one.
@ xy_plane
All points within one single horizontal plane.
@ same_horizontal_dp
All the points within horizontal plane followed by vertical one.
@ invalid
Invalid points to be thrown away.
void init_hist(DataMonitor &data_monitor)
set histograms.
std::unordered_map< int, int > ref_bars_in_planes_
key: plane_id, value: global module number
auto try_in_xy_plane(const std::vector< Point > &collection) -> bool
auto calculate_time_diffs_to_first() const -> std::unordered_map< int, ValueErrorD >
double max_time_difference_
TH2D * hist_horizontal_dp_
void fill_histogram(RecordType record_type)
int event_ref_module_num_
TH2D * hist_horizontal_plane_
void fill_two_planes_histogram(double time_val, int module_num)
void add_point(double t_sum, int module_num)
Add the t_sum value from a bar.
void set_plane_ref_bar_time(const std::vector< Point > &collection, int plane_num)
constexpr auto DEFAULT_TSYNC_REFERENCE_BAR_NUM
auto calculate_hist_quantiles(TH1 *histogram, double quantile_ratio) -> std::array< double, 2 >
constexpr auto BarsPerPlane
constexpr auto ModuleID2PlaneID(int moduleID) -> int
constexpr auto GetBarVerticalDisplacement(int module_num) -> double
constexpr auto ModuleID2PlaneNum(int moduleID) -> int
constexpr auto IsModuleNumHorizontal(int module_num) -> bool
constexpr auto IsPlaneIDHorizontal(int plane_id) -> bool
ValueError< double > ValueErrorD