R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BNeulandMapToCalParTask.cxx
Go to the documentation of this file.
2#include "R3BException.h"
5#include <FairParRootFileIo.h>
6#include <FairRootManager.h>
7#include <FairRun.h>
8#include <FairRuntimeDb.h>
9#include <R3BLogger.h>
10#include <R3BParRootFileIo.h>
11#include <TDirectory.h>
12#include <fmt/core.h>
13#include <string_view>
14#include <utility>
15
16namespace
17{
18 template <typename EngineType, typename... Args>
19 void FillEngine(EngineType& engine, R3B::FTType type, unsigned int ftValue, Args&&... args)
20 {
21 engine.Fill(type, ftValue, std::forward<Args>(args)...);
22 }
23} // namespace
24
25namespace R3B::Neuland
26{
27 // NOLINTNEXTLINE: bugprone-easily-swappable-parameters
28 Map2CalParTask::Map2CalParTask(std::string_view mapped_data_name,
29 std::string_view trig_mapped_data_name,
30 std::string_view par_name,
31 std::string_view trig_par_name)
32 : CalibrationTask("NeulandMapToCalParTask", 1)
33 , map_data_{ mapped_data_name }
34 , trig_map_data_{ trig_mapped_data_name }
35 , map_to_cal_par_{ par_name }
36 , map_to_cal_trig_par_{ trig_par_name }
37 {
38 cal_strategy_.Set_cycle_period(1000. / coarse_time_frequency_);
39 }
40
41 void Map2CalParTask::ExtraInit(FairRootManager* /*rootMan*/)
42 {
43 map_data_.init();
44 map_to_cal_par_.init(this);
45 map_to_cal_trig_par_.init(this);
47 {
48 trig_map_data_.init();
49 // if (is_trigID_auto_)
50 // {
51 // R3BLOG(info, "TrigIDMap is automatically determined!");
52 // }
53 // else
54 // {
55 // R3BLOG(info, "TrigIDMap is read from the base par file!");
56 // // trigIDMappingFinder_.set_trigIDMap(base_par_->GetTrigIDMap());
57 // }
58 }
59
60 if (auto* base_par = GetBasePar(); base_par != nullptr)
61 {
62 plane_num_ = base_par->get_num_of_planes();
63 }
64 map_to_cal_par_->SetTrigEnabled(is_trig_enabled_);
65 // trigIDIO.SetNumOfModule(plane_num_ * BarsPerPlane);
66 }
67
69 {
72 {
74 // if (is_trigID_auto_)
75 // {
76 // RecordTrigMappingID();
77 // }
78 }
79 }
80
86
87 // void Map2CalParTask::RecordTrigMappingID()
88 // {
89 // for (const auto& planeSignals : mappedData_)
90 // {
91 // auto planeNum = planeSignals.plane_num;
92 // for (const auto& [barID, barSignals] : planeSignals.bars)
93 // {
94 // auto moduleID = Neuland_PlaneBar2ModuleNum(planeNum, barID);
95 // for (const auto& [trigID, trigSignal] : trigMappedData_)
96 // {
97 // trigIDMappingFinder_.add_id_pair(
98 // std::make_pair(moduleID, trigID), Side::left, barSignals.left.size());
99 // trigIDMappingFinder_.add_id_pair(
100 // std::make_pair(moduleID, trigID), Side::right, barSignals.right.size());
101 // }
102 // }
103 // }
104 // }
105
107 {
108 for (const auto& [plane_num, planeSignals] : map_data_)
109 {
110 for (const auto& [barNum, barSignals] : planeSignals.bars)
111 {
112 auto barNum_tmp = barNum;
113 R3BLOG(debug,
114 fmt::format("Calibrating with the map-level bar signal: {}, barNum: {}", barSignals, barNum));
115 // C++17 can't capture structure bindings
116 const auto plane_number = plane_num;
117 auto FillData = [&](FTType type, auto value)
118 { FillEngine(map_cal_engine_, type, value, plane_number, barNum_tmp); };
119 for (const auto& signal : barSignals.left)
120 {
121 FillData(FTType::leftleading, signal.leading.fine);
122 FillData(FTType::lefttrailing, signal.trailing.fine);
123 }
124 for (const auto& signal : barSignals.right)
125 {
126 FillData(FTType::rightleading, signal.leading.fine);
127 FillData(FTType::righttrailing, signal.trailing.fine);
128 }
129 }
130 }
131 }
132
134 {
135 for (const auto& [moduleNum, moduleSignals] : trig_map_data_)
136 {
137 R3BLOG(debug,
138 fmt::format("Calibrating with the map-level bar trig signal: {}, module num: {}",
139 moduleSignals.signal,
140 moduleNum));
141 FillEngine(trig_map_cal_engine_, FTType::trigger, moduleSignals.signal.fine, moduleNum);
142 }
143 }
144
146 {
147 R3BLOG(debug, "Starting to write calibration parameter...");
148 map_cal_engine_.Writer_to_TCalPar(cal_strategy_, *map_to_cal_par_);
149 // if (is_trigID_auto_)
150 // {
151 // GetBasePar()->set_trig_id_map(trigIDMappingFinder_.extract_trigIDMap());
152 // }
153 map_to_cal_par_->SetSlowClockFrequency(coarse_time_frequency_);
154
156 R3BLOG(debug, "Calibration parameter written complete.");
157 }
158
160 {
161 auto old_dir = gDirectory;
162 auto* runDb = FairRuntimeDb::instance();
163 if (auto* parRootFileIo = dynamic_cast<ParRootFileIo*>(runDb->getOutput()); parRootFileIo != nullptr)
164 {
165 auto* parRootFile = parRootFileIo->get_first_root_file();
166 if (!parRootFile->IsOpen() || !parRootFile->IsWritable())
167 {
168 throw R3B::runtime_error("R3B::ParRootFile is either closed or unable to be written!");
169 }
170 parRootFile->cd();
171 auto* new_dir = gDirectory->mkdir("CalParHistograms", "", true);
172 if (new_dir == nullptr)
173 {
174 throw R3B::runtime_error("Cannot create a directory for the histrogams!");
175 }
176 map_cal_engine_.WriteHist2File(new_dir);
178 {
179 trig_map_cal_engine_.WriteHist2File(new_dir);
180 }
181 old_dir->cd();
182 }
183 else
184 {
185 throw R3B::runtime_error("unable to obtain the R3B::ParRootFileIo object!");
186 }
187 }
188
189 // void Map2CalParTask::PrintTrigID() const
190 // {
191 // if (calibrationPar_ == nullptr)
192 // {
193 // throw R3B::runtime_error("Cannot print trigIDMapping from a nullptr parameter!");
194 // }
195
196 // const auto& trigID = base_par_->GetTrigIDMap();
197 // if (is_trigID_auto_)
198 // {
199 // trigIDIO.Save_json(trigID);
200 // }
201 // trigIDIO.Print(trigID);
202 // }
203} // namespace R3B::Neuland
#define R3BLOG(severity, x)
Definition R3BLogger.h:32
OutputParView< Map2CalPar > map_to_cal_trig_par_
calibration::FTEngine< calibration::ModuleCal > trig_map_cal_engine_
InputMapConnector< int, PaddleTamexMappedData > map_data_
void ExtraInit(FairRootManager *rootMan) override
calibration::FTCalStrategy cal_strategy_
calibration::FTEngine< calibration::PlaneCal > map_cal_engine_
Map2CalParTask(std::string_view mapped_data_name="NeulandMappedData", std::string_view trig_mapped_data_name="NeulandTrigMappedData", std::string_view par_name="LandTCalPar", std::string_view trig_par_name="LandTrigTCalPar")
OutputParView< Map2CalPar > map_to_cal_par_
InputMapConnector< int, PaddleTamexTrigMappedData > trig_map_data_
Simulation of NeuLAND Bar/Paddle.