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