R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BFTCalEngine.h
Go to the documentation of this file.
1#pragma once
2
4#include <R3BLogger.h>
5#include <R3BNeulandCommon.h>
6#include <R3BShared.h>
7#include <TDirectory.h>
8#include <TH1.h>
9#include <TH2.h>
10#include <cstdint>
11#include <fmt/core.h>
12#include <map>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <vector>
18
20{
21 enum class FTCalErrorMethod : uint8_t
22 {
27 };
28
29 auto FTType2Str(FTType type) -> std::string_view;
30
32 {
33 public:
34 FTCalStrategy() = default;
36
37 void Set_max_bin_number(unsigned int num) { max_bin_number_ = num; }
38 void Set_cycle_period(double time) { cycle_period_ = time; } // ns
40
41 [[nodiscard]] auto Get_cycle_period() const -> auto { return cycle_period_; }
42 auto GetChannel2Time(TH1* hist) const -> FTChannel2TimeRelation;
43
44 private:
45 unsigned int max_bin_number_ = 0;
46 double cycle_period_ = 0.; // nano seconds
48 };
49
50 class ModuleCal;
51 class PlaneCal;
52
53 template <typename Derived>
55 {
56 public:
58 void InitAllDistributions(TH1* hist)
59 {
60 for (auto& [type, distribution] : fTDistribution_)
61 {
62 distribution = std::unique_ptr<TH1>(dynamic_cast<TH1*>(hist->Clone()));
63 distribution->SetDirectory(nullptr);
64 auto name = fmt::format("{0}_{1:04d}_{2}", hist_name_, moduleNum_, FTType2Str(type));
65 distribution->SetNameTitle(name.c_str(), name.c_str());
66 }
67 }
68
69 void WriteHist2File(TDirectory* sink)
70 {
71 for (const auto& [ft_type, distribution] : fTDistribution_)
72 {
73 R3BLOG(
74 debug4,
75 fmt::format("Writting histogram {} into sink file {}", distribution->GetName(), sink->GetName()));
76 sink->WriteObject(distribution.get(), distribution->GetName());
77 }
78 }
79
80 void Write2Par(unsigned int moduleID,
81 Map2CalPar& t_cal_par,
82 std::map<FTType, FTChannel2TimeRelation> valueErrors)
83 {
84 auto modulePar = TCalVFTXModulePar{ moduleID };
85
86 for (const auto& [type, rel] : valueErrors)
87 {
88 R3BLOG(debug4, fmt::format("type: {} CalRelSize: {}", FTType2Str(type), rel.value_error.size()));
89 }
90
91 modulePar.Set_correlation(std::move(valueErrors));
92 t_cal_par.SetModuleParam(moduleID, std::move(modulePar));
93 }
94
95 // Getter:
96 [[nodiscard]] auto GetDistribution(FTType type) { return fTDistribution_.at(type).get(); }
97 [[nodiscard]] auto GetAllDistributions() -> const auto& { return fTDistribution_; }
98 [[nodiscard]] auto GetHistName() const -> const auto& { return hist_name_; }
99 [[nodiscard]] auto GetModuleNum() const -> const auto& { return moduleNum_; }
100
101 template <typename... Args>
102 void AddFineTime(FTType type, Args&&... args)
103 {
104 auto* hist = static_cast<typename Derived::HistType*>(GetDistribution(type)); // NOLINT
105 hist->Fill(std::forward<Args>(args)...);
106 }
107
108 private:
109 std::string_view hist_name_;
110 unsigned int moduleNum_ = 0;
111 // fine time distributions:
112 std::map<FTType, std::unique_ptr<TH1>> fTDistribution_;
113
114 FTBaseCal(std::string_view hist_name, unsigned int moduleNum, const std::vector<FTType>& types)
115 : hist_name_(hist_name)
116 , moduleNum_{ moduleNum }
117 {
119 }
120
121 void EmptyInitAllDistributions(const std::vector<FTType>& types)
122 {
123 for (const auto& type : types)
124 {
125 fTDistribution_.insert_or_assign(type, nullptr);
126 }
127 }
128
129 friend ModuleCal;
130 friend PlaneCal;
131 };
132
133 // calirbation class for trigger mapped data
134 class ModuleCal : public FTBaseCal<ModuleCal>
135 {
136 public:
137 using HistType = TH1I;
138 explicit ModuleCal(std::string_view hist_name, unsigned int mID);
139 void Fill(FTType type, unsigned int ftValue) { AddFineTime(type, ftValue); }
140 template <typename Strategy>
141 void Write_to_par(const Strategy& strategy, Map2CalPar& t_cal_par)
142 {
143 const auto& distributions = GetAllDistributions();
144 auto correlations = std::map<FTType, FTChannel2TimeRelation>{};
145 for (const auto& [ftType, distribution2D] : distributions)
146 {
147 auto* hist = static_cast<HistType*>(distribution2D.get()); // NOLINT
148 if (hist != nullptr)
149 {
150 correlations.insert_or_assign(ftType, strategy.GetChannel2Time(hist));
151 }
152 }
153 Write2Par(GetModuleNum(), t_cal_par, std::move(correlations));
154 }
155 };
156
157 // calibration class for mapped data
158 class PlaneCal : public FTBaseCal<PlaneCal>
159 {
160 public:
161 using HistType = TH2I;
162 explicit PlaneCal(std::string_view hist_name, unsigned int mID);
163
164 void Fill(FTType type, unsigned int ftValue, unsigned int barID) { AddFineTime(type, barID, ftValue); }
165
166 // template <typename Strategy>
167 void Write_to_par(const FTCalStrategy& strategy, Map2CalPar& t_cal_par)
168 {
169 const auto& distributions = GetAllDistributions();
170 for (int barNum{ 1 }; barNum <= BarsPerPlane; ++barNum)
171 {
172 auto correlations = std::map<FTType, FTChannel2TimeRelation>{};
173 for (const auto& [ftType, distribution2D] : distributions)
174 {
175 auto* hist2D = static_cast<HistType*>(distribution2D.get()); // NOLINT
176 auto* hist1D = hist2D->ProjectionY("projection", barNum, barNum);
177 if (hist1D != nullptr)
178 {
179 correlations.insert_or_assign(ftType, strategy.GetChannel2Time(hist1D));
180 }
181 }
182 Write2Par(Neuland_PlaneBar2ModuleNum(GetModuleNum(), barNum), t_cal_par, std::move(correlations));
183 }
184 }
185 };
186
187 template <typename CalType>
189 {
190 public:
191 explicit FTEngine(std::string hist_name)
192 : hist_name_{ std::move(hist_name) }
193 {
194 }
195
196 template <typename... AdditionalPar>
197 void Fill(FTType type, unsigned int ftValue, unsigned int moduleNum, AdditionalPar&&... pars)
198 {
199 auto iter = cals_.find(moduleNum);
200 if (iter == cals_.end())
201 {
202 iter = cals_.emplace(moduleNum, CalType{ hist_name_, moduleNum }).first;
203 }
204 iter->second.Fill(type, ftValue, std::forward<AdditionalPar>(pars)...);
205 }
206
207 template <typename Strategy>
208 void Writer_to_TCalPar(const Strategy& write_strategy, Map2CalPar& t_cal_par)
209 {
210 for (auto& [_, cal] : cals_)
211 {
212 cal.Write_to_par(write_strategy, t_cal_par);
213 }
214 }
215
216 void WriteHist2File(TDirectory* sink)
217 {
218 for (auto& [_, cal] : cals_)
219 {
220 cal.WriteHist2File(sink);
221 }
222 }
223
224 private:
225 std::map<unsigned int, CalType> cals_;
226 std::string hist_name_;
227 };
228
229} // namespace R3B::Neuland::calibration
#define R3BLOG(severity, x)
Definition R3BLogger.h:32
void SetModuleParam(unsigned int module_num, TCalVFTXModulePar par)
std::map< FTType, std::unique_ptr< TH1 > > fTDistribution_
auto GetModuleNum() const -> const auto &
FTBaseCal(std::string_view hist_name, unsigned int moduleNum, const std::vector< FTType > &types)
void Write2Par(unsigned int moduleID, Map2CalPar &t_cal_par, std::map< FTType, FTChannel2TimeRelation > valueErrors)
auto GetAllDistributions() -> const auto &
void AddFineTime(FTType type, Args &&... args)
auto GetHistName() const -> const auto &
void EmptyInitAllDistributions(const std::vector< FTType > &types)
void WriteHist2File(TDirectory *sink)
TCalVFTXModulePar::ValueErrors ValueErrors
TCalVFTXModulePar::ValueErrors ValueErrors
auto GetChannel2Time(TH1 *hist) const -> FTChannel2TimeRelation
void Set_error_method(FTCalErrorMethod method)
void Writer_to_TCalPar(const Strategy &write_strategy, Map2CalPar &t_cal_par)
std::map< unsigned int, CalType > cals_
void WriteHist2File(TDirectory *sink)
void Fill(FTType type, unsigned int ftValue, unsigned int moduleNum, AdditionalPar &&... pars)
void Fill(FTType type, unsigned int ftValue)
ModuleCal(std::string_view hist_name, unsigned int mID)
void Write_to_par(const Strategy &strategy, Map2CalPar &t_cal_par)
void Write_to_par(const FTCalStrategy &strategy, Map2CalPar &t_cal_par)
void Fill(FTType type, unsigned int ftValue, unsigned int barID)
PlaneCal(std::string_view hist_name, unsigned int mID)
std::vector< ValueError< double > > ValueErrors
void Set_correlation(std::map< FTType, FTChannel2TimeRelation > correlation)
auto FTType2Str(FTType type) -> std::string_view
constexpr auto BarsPerPlane
constexpr auto Neuland_PlaneBar2ModuleNum(unsigned int planeNum, unsigned int barNum) -> unsigned int