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