10#include <TFitResult.h>
11#include <TFitResultPtr.h>
23 constexpr auto FITTING_RANGE_RATIO = 0.9;
31 auto calculate_toffset_speed(TH1D* histogram,
int bar_id) -> ParResult
33 histogram->Scale(1 / histogram->GetEntries());
34 auto* cumulative = histogram->GetCumulative();
35 constexpr auto QUANTILES_NUM = 2;
36 const auto quantiles =
37 std::array<double, QUANTILES_NUM>{ 0.5 - (FITTING_RANGE_RATIO / 2.), 0.5 + (FITTING_RANGE_RATIO / 2.) };
38 auto x_pos = std::array<double, QUANTILES_NUM>{};
39 histogram->GetQuantiles(QUANTILES_NUM, x_pos.data(), quantiles.data());
42 auto first_fit_res = cumulative->Fit(
"pol1",
"SQ",
"", x_pos[0], x_pos[1]);
43 if (first_fit_res.Get() ==
nullptr)
46 fmt::format(
"First linear fitting on time_diff CFD failed for the bar with id {}", bar_id));
49 const auto slope = first_fit_res->Parameter(1);
50 const auto offset = first_fit_res->Parameter(0);
56 auto fit_fun = TF1{
"fit_fun",
"[1]*x + 0.5 - [0] * [1]", x_pos[0], x_pos[1] };
57 fit_fun.SetParameter(0, (0.5 - offset) / slope);
58 fit_fun.SetParameter(1, slope);
59 auto second_fit_res = cumulative->Fit(&fit_fun,
"SQ",
"", x_pos[0], x_pos[1]);
60 if (second_fit_res.Get() ==
nullptr)
62 R3BLOG(warn, fmt::format(
"Second fitting on time_diff CFD failed for the bar with id {}", bar_id));
66 auto par_result = ParResult();
67 par_result.t_offset.value = second_fit_res->Parameter(0);
68 par_result.t_offset.error = second_fit_res->Error(0);
69 par_result.effective_speed.value = second_fit_res->Parameter(1) * 2 *
BarLength;
70 par_result.effective_speed.error = second_fit_res->Error(1) * 2 *
BarLength;
79 static constexpr auto TIME_DIFF_MAX = 100.;
80 static constexpr auto TIME_DIFF_BIN_NUM = 500;
84 hist_time_diff_ = histograms.
add_hist<TH2D>(
"hist_time_diff",
85 "Time differences between two adjacent PMTs",
108 for (
const auto& signal : signals)
110 if (signal.left.size() != 1 or signal.right.size() != 1)
118 void Predecessor::fill_hist(
const BarCalData& signal)
120 const auto& left_signal = signal.
left.front();
121 const auto& right_signal = signal.
right.front();
124 const auto t_diff = right_signal.leading_time - left_signal.leading_time;
127 hist_time_diff_->Fill(
static_cast<int>(module_num), t_diff.value);
134 return signals.size() >= minimum_hit_;
141 for (
int bar_id{}; bar_id < module_size; ++bar_id)
143 auto* hist_t_diff_py = hist_time_diff_->ProjectionY(
"_py", bar_id + 1, bar_id + 1);
144 const auto par_res = calculate_toffset_speed(hist_t_diff_py, bar_id);
148 module_par.t_diff = par_res.t_offset;
149 module_par.effective_speed = par_res.effective_speed;
#define R3BLOG(severity, x)
auto add_hist(std::unique_ptr< TH1 > hist) -> TH1 *
void AddModulePar(const HitModulePar &module_par)
auto GetTask() -> Cal2HitParTask *
auto GetModuleSize() const -> auto
void Calibrate(Cal2HitPar &hit_par) override
auto SignalFilter(const std::vector< BarCalData > &signals) -> bool override
void AddSignals(const std::vector< BarCalData > &signals) override
void HistInit(DataMonitor &histograms) override
ValueError< double > ValueErrorD
std::vector< CalDataSignal > left
std::vector< CalDataSignal > right