R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BFTCalEngine.cxx
Go to the documentation of this file.
1#include "R3BFTCalEngine.h"
2#include "R3BException.h"
3#include <range/v3/all.hpp>
4
6{
7 auto FTType2Str(FTType type) -> std::string_view
8 {
9 switch (type)
10 {
12 return "left_leading";
14 return "right_leading";
16 return "left_trailing";
18 return "right_trailing";
19 case FTType::trigger:
20 return "trigger";
21 default:
22 throw R3B::logic_error("unresolvable types");
23 }
24 }
25
26 ModuleCal::ModuleCal(std::string_view hist_name, unsigned int mID)
27 : FTBaseCal{ hist_name, mID, { FTType::trigger } }
28 {
29 auto initHist = R3B::make_hist<TH1I>(hist_name.data(), hist_name.data(), MaxFTValue, 0.5, MaxFTValue + 0.5);
30 InitAllDistributions(initHist.get());
31 }
32
33 PlaneCal::PlaneCal(std::string_view hist_name, unsigned int mID)
34 : FTBaseCal{ hist_name,
35 mID,
37 {
38 auto initHist = R3B::make_hist<TH2I>(hist_name.data(),
39 hist_name.data(),
41 +0.5,
42 BarsPerPlane + 0.5,
44 +0.5,
45 MaxFTValue + 0.5);
46 InitAllDistributions(initHist.get());
47 }
48
49 namespace
50 {
51 using ValueErrors = FTCalStrategy::ValueErrors;
52 constexpr unsigned int uniform_err_divider = 12;
53 constexpr auto uniform_err_divider_sqrt = SQRT_12;
54 const auto sqrt_3 = std::sqrt(3);
55
56 struct InputInfo
57 {
58 double previous_sum;
59 double bin_entry;
60 double total_entry;
61 };
62
63 auto calculate_meanerror_exact(const InputInfo& input) -> ValueError<double>
64 {
65 const auto base_vairance = input.bin_entry * input.bin_entry / uniform_err_divider;
66 const auto sum_term = input.previous_sum + input.bin_entry / 3;
67 const auto bin_prob = input.bin_entry / input.total_entry;
68 const auto pre_prob = input.previous_sum / input.total_entry;
69 const auto residual = (1 - bin_prob) * sum_term - input.previous_sum * pre_prob;
70
71 const auto mean = input.previous_sum + input.bin_entry / 2;
72 return ValueError<double>{ mean, std::sqrt(base_vairance + residual) };
73 }
74
75 auto calculate_meanerror_approx(const InputInfo& input) -> ValueError<double>
76 {
77 const auto mean = input.previous_sum + input.bin_entry / 2;
78
79 const auto base_error = input.bin_entry / uniform_err_divider_sqrt;
80 const auto bin_prob = input.bin_entry / input.total_entry;
81 const auto pre_prob = input.previous_sum / input.total_entry;
82 const auto residual_main = -(pre_prob - 0.5) * (pre_prob - 0.5) + 0.25;
83 const auto residual_factor = (bin_prob == 0.) ? 0. : sqrt_3 / bin_prob;
84
85 return ValueError<double>{ mean, base_error + residual_main * residual_factor };
86 }
87
88 auto calculate_meanerror_uniform_only(const InputInfo& input) -> ValueError<double>
89 {
90 const auto mean = input.previous_sum + input.bin_entry / 2;
91 const auto base_error = input.bin_entry / uniform_err_divider_sqrt;
92 return ValueError<double>{ mean, base_error };
93 }
94
95 auto calculate_meanerror_none(const InputInfo& input) -> ValueError<double>
96 {
97 const auto mean = input.previous_sum + input.bin_entry / 2;
98 return ValueError<double>{ mean, 0. };
99 }
100
101 auto use_method(FTCalErrorMethod methodtype)
102 {
103 switch (methodtype)
104 {
106 return +[](const InputInfo& input) { return calculate_meanerror_exact(input); };
108 return +[](const InputInfo& input) { return calculate_meanerror_approx(input); };
110 return +[](const InputInfo& input) { return calculate_meanerror_uniform_only(input); };
112 return +[](const InputInfo& input) { return calculate_meanerror_none(input); };
113 default:
114 throw R3B::logic_error("undefined enumerator for method type!");
115 }
116 }
117
118 auto extract_bin_data(TH1* hist, unsigned int max_bin) -> ValueErrors
119 {
120 auto output = ValueErrors{};
121 output.reserve(max_bin);
122 // root histrogram starts from index 1
123 for (size_t index{ 1 }; index < max_bin + 1; ++index)
124 {
125 output.emplace_back(hist->GetBinContent(static_cast<int>(index)), 0.);
126 }
127 return output;
128 }
129
130 void scale_to_real_ns(ValueErrors& value_errors, double period, double total_entry)
131 {
132 ranges::for_each(value_errors,
133 [&](ValueError<double>& value_error)
134 {
135 value_error.value = value_error.value / total_entry * period;
136 value_error.error = value_error.error / total_entry * period;
137 });
138 }
139 } // namespace
140
142 unsigned int max_bin,
143 double total_entry,
144 FTCalErrorMethod methodtype) -> std::pair<ValueErrors, unsigned int>
145 {
146 auto output = extract_bin_data(hist, max_bin);
147
148 auto method = use_method(methodtype);
149 auto previous_sum = 0.;
150 for (auto& value_error : output)
151 {
152 auto& value = value_error.value;
153 auto& error = value_error.error;
154 const auto inputInfo =
155 InputInfo{ .previous_sum = previous_sum, .bin_entry = value, .total_entry = total_entry };
156 previous_sum += value;
157 auto new_value_error = method(inputInfo);
158 value = new_value_error.value;
159 error = new_value_error.error;
160 }
161 const auto overflow = total_entry - previous_sum;
162 return std::make_pair(output, overflow);
163 }
164
166 {
167 const auto total_entry = hist->GetEntries();
168
169 auto bin_num = hist->GetNbinsX();
170 auto max_bin = (max_bin_number_ == 0) ? bin_num : max_bin_number_;
171 max_bin = (bin_num > max_bin) ? max_bin : bin_num;
172
173 auto [value_errors, overflow] = calculate_value_errors(hist, max_bin, total_entry, error_method_);
174
175 scale_to_real_ns(value_errors, cycle_period_, total_entry);
176
177 auto calRelation = FTChannel2TimeRelation{};
178 calRelation.value_error = std::move(value_errors);
179 calRelation.hist_overflow = overflow;
180 return calRelation;
181 }
182} // namespace R3B::Neuland::calibration
FTBaseCal(std::string_view hist_name, unsigned int moduleNum, const std::vector< FTType > &types)
TCalVFTXModulePar::ValueErrors ValueErrors
auto GetChannel2Time(TH1 *hist) const -> FTChannel2TimeRelation
ModuleCal(std::string_view hist_name, unsigned int mID)
PlaneCal(std::string_view hist_name, unsigned int mID)
auto FTType2Str(FTType type) -> std::string_view
auto calculate_value_errors(TH1 *hist, unsigned int max_bin, double total_entry, FTCalErrorMethod methodtype) -> std::pair< ValueErrors, unsigned int >
constexpr auto BarsPerPlane
constexpr auto SQRT_12
constexpr auto MaxFTValue
auto make_hist(Args &&... args)
Definition R3BShared.h:62
std::vector< ValueError< double > > value_error