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(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:
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(int moduleID, Map2CalPar& t_cal_par, std::map<FTType, FTChannel2TimeRelation> valueErrors)
81 {
82 auto modulePar = TCalVFTXModulePar{ moduleID };
83
84 for (const auto& [type, rel] : valueErrors)
85 {
86 R3BLOG(debug4, fmt::format("type: {} CalRelSize: {}", FTType2Str(type), rel.value_error.size()));
87 }
88
89 modulePar.Set_correlation(std::move(valueErrors));
90 t_cal_par.SetModuleParam(moduleID, std::move(modulePar));
91 }
92
93 // Getter:
94 [[nodiscard]] auto GetDistribution(FTType type) { return fTDistribution_.at(type).get(); }
95 [[nodiscard]] auto GetAllDistributions() -> const auto& { return fTDistribution_; }
96 [[nodiscard]] auto GetHistName() const -> const auto& { return hist_name_; }
97 [[nodiscard]] auto GetModuleNum() const -> const auto& { return moduleNum_; }
98
99 template <typename... Args>
100 void AddFineTime(FTType type, Args&&... args)
101 {
102 auto* hist = static_cast<typename Derived::HistType*>(GetDistribution(type)); // NOLINT
103 hist->Fill(std::forward<Args>(args)...);
104 }
105
106 private:
107 std::string_view hist_name_;
108 int moduleNum_ = 0;
109 // fine time distributions:
110 std::map<FTType, std::unique_ptr<TH1>> fTDistribution_;
111
112 FTBaseCal(std::string_view hist_name, int moduleNum, const std::vector<FTType>& types)
113 : hist_name_(hist_name)
114 , moduleNum_{ moduleNum }
115 {
117 }
118
119 void EmptyInitAllDistributions(const std::vector<FTType>& types)
120 {
121 for (const auto& type : types)
122 {
123 fTDistribution_.insert_or_assign(type, nullptr);
124 }
125 }
126
127 friend ModuleCal;
128 friend PlaneCal;
129 };
130
131 // calirbation class for trigger mapped data
132 class ModuleCal : public FTBaseCal<ModuleCal>
133 {
134 public:
135 using HistType = TH1I;
136 explicit ModuleCal(std::string_view hist_name, int mID);
137 void Fill(FTType type, int ftValue) { AddFineTime(type, ftValue); }
138 template <typename Strategy>
139 void Write_to_par(const Strategy& strategy, Map2CalPar& t_cal_par)
140 {
141 const auto& distributions = GetAllDistributions();
142 auto correlations = std::map<FTType, FTChannel2TimeRelation>{};
143 for (const auto& [ftType, distribution2D] : distributions)
144 {
145 auto* hist = static_cast<HistType*>(distribution2D.get()); // NOLINT
146 if (hist != nullptr)
147 {
148 correlations.insert_or_assign(ftType, strategy.GetChannel2Time(hist));
149 }
150 }
151 Write2Par(GetModuleNum(), t_cal_par, std::move(correlations));
152 }
153 };
154
155 // calibration class for mapped data
156 class PlaneCal : public FTBaseCal<PlaneCal>
157 {
158 public:
159 using HistType = TH2I;
160 explicit PlaneCal(std::string_view hist_name, int mID);
161
162 void Fill(FTType type, int ftValue, int barID) { AddFineTime(type, barID, ftValue); }
163
164 // template <typename Strategy>
165 void Write_to_par(const FTCalStrategy& strategy, Map2CalPar& t_cal_par)
166 {
167 const auto& distributions = GetAllDistributions();
168 for (int barNum{ 1 }; barNum <= BarsPerPlane; ++barNum)
169 {
170 auto correlations = std::map<FTType, FTChannel2TimeRelation>{};
171 for (const auto& [ftType, distribution2D] : distributions)
172 {
173 auto* hist2D = static_cast<HistType*>(distribution2D.get()); // NOLINT
174 auto* hist1D = hist2D->ProjectionY("projection", barNum, barNum);
175 if (hist1D != nullptr)
176 {
177 correlations.insert_or_assign(ftType, strategy.GetChannel2Time(hist1D));
178 }
179 }
180 Write2Par(Neuland_PlaneBar2ModuleNum(GetModuleNum(), barNum), t_cal_par, std::move(correlations));
181 }
182 }
183 };
184
185 template <typename CalType>
187 {
188 public:
189 explicit FTEngine(std::string hist_name)
190 : hist_name_{ std::move(hist_name) }
191 {
192 }
193
194 template <typename... AdditionalPar>
195 void Fill(FTType type, int ftValue, int moduleNum, AdditionalPar&&... pars)
196 {
197 auto iter = cals_.find(moduleNum);
198 if (iter == cals_.end())
199 {
200 iter = cals_.emplace(moduleNum, CalType{ hist_name_, moduleNum }).first;
201 }
202 iter->second.Fill(type, ftValue, std::forward<AdditionalPar>(pars)...);
203 }
204
205 template <typename Strategy>
206 void Writer_to_TCalPar(const Strategy& write_strategy, Map2CalPar& t_cal_par)
207 {
208 for (auto& [_, cal] : cals_)
209 {
210 cal.Write_to_par(write_strategy, t_cal_par);
211 }
212 }
213
214 void WriteHist2File(TDirectory* sink)
215 {
216 for (auto& [_, cal] : cals_)
217 {
218 cal.WriteHist2File(sink);
219 }
220 }
221
222 private:
223 std::map<int, CalType> cals_;
224 std::string hist_name_;
225 };
226
227} // namespace R3B::Neuland::calibration
#define R3BLOG(severity, x)
Definition R3BLogger.h:32
void SetModuleParam(int module_num, TCalVFTXModulePar par)
std::map< FTType, std::unique_ptr< TH1 > > fTDistribution_
auto GetModuleNum() const -> const auto &
auto GetAllDistributions() -> const auto &
void AddFineTime(FTType type, Args &&... args)
auto GetHistName() const -> const auto &
FTBaseCal(std::string_view hist_name, int moduleNum, const std::vector< FTType > &types)
void Write2Par(int moduleID, Map2CalPar &t_cal_par, std::map< FTType, FTChannel2TimeRelation > valueErrors)
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)
void WriteHist2File(TDirectory *sink)
void Fill(FTType type, int ftValue, int moduleNum, AdditionalPar &&... pars)
void Fill(FTType type, int ftValue)
ModuleCal(std::string_view hist_name, 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, int ftValue, int barID)
PlaneCal(std::string_view hist_name, 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(int planeNum, int barNum) -> int