R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BNeulandMapToCalParTask.cxx
Go to the documentation of this file.
2#include "R3BException.h"
3#include <FairParIo.h>
4#include <FairParRootFileIo.h>
5#include <FairRootManager.h>
6#include <FairRun.h>
7#include <FairRuntimeDb.h>
8#include <R3BLogger.h>
9
10namespace
11{
12 template <typename EngineType, typename... Args>
13 void FillEngine(EngineType& engine, R3B::FTType type, unsigned int ftValue, Args&&... args)
14 {
15 engine.Fill(type, ftValue, std::forward<Args>(args)...);
16 }
17} // namespace
18
19namespace R3B::Neuland
20{
21 Map2CalParTask::Map2CalParTask(std::string_view name, int iVerbose)
22 : CalibrationTask(name.data(), iVerbose)
23 {
24 cal_strategy_.Set_cycle_period(1000. / coarse_time_frequency_);
25 }
26
28 : Map2CalParTask("NeulandMapToCalParTask", 1)
29 {
30 }
31
32 void Map2CalParTask::ExtraInit(FairRootManager* /*rootMan*/)
33 {
34 mappedData_.init();
35 if (is_trig_enabled_)
36 {
37 trigMappedData_.init();
38 if (is_trigID_auto_)
39 {
40 R3BLOG(info, "TrigIDMap is automatically determined!");
41 }
42 else
43 {
44 R3BLOG(info, "TrigIDMap is read from the base par file!");
45 // trigIDMappingFinder_.set_trigIDMap(base_par_->GetTrigIDMap());
46 }
47 }
48
49 plane_num_ = GetBasePar()->GetNumOfPlanes();
50 calibrationPar_->SetTrigEnabled(is_trig_enabled_);
51 // trigIDIO.SetNumOfModule(plane_num_ * BarsPerPlane);
52 }
53
55 {
56 FillMapData();
57 if (is_trig_enabled_)
58 {
59 FillTrigMapData();
60 if (is_trigID_auto_)
61 {
62 RecordTrigMappingID();
63 }
64 }
65 }
66
68 {
69 WriteHists();
70 write_parameter();
71 }
72
73 void Map2CalParTask::RecordTrigMappingID()
74 {
75 for (const auto& planeSignals : mappedData_)
76 {
77 auto planeNum = planeSignals.plane_num;
78 for (const auto& [barID, barSignals] : planeSignals.bars)
79 {
80 auto moduleID = Neuland_PlaneBar2ModuleNum(planeNum, barID);
81 for (const auto& [trigID, trigSignal] : trigMappedData_)
82 {
83 trigIDMappingFinder_.add_id_pair(
84 std::make_pair(moduleID, trigID), Side::left, barSignals.left.size());
85 trigIDMappingFinder_.add_id_pair(
86 std::make_pair(moduleID, trigID), Side::right, barSignals.right.size());
87 }
88 }
89 }
90 }
91
92 void Map2CalParTask::FillMapData()
93 {
94 for (const auto& planeSignals : mappedData_)
95 {
96 auto planeID = planeSignals.plane_num;
97 for (const auto& [barNum, barSignals] : planeSignals.bars)
98 {
99 auto barNum_tmp = barNum;
100 R3BLOG(debug,
101 fmt::format("Calibrating with the map-level bar signal: {}, barNum: {}", barSignals, barNum));
102 auto FillData = [&](FTType type, auto value)
103 { FillEngine(mapCalEngine_, type, value, planeID, barNum_tmp); };
104 for (const auto& signal : barSignals.left)
105 {
106 FillData(FTType::leftleading, signal.leading.fine);
107 FillData(FTType::lefttrailing, signal.trailing.fine);
108 }
109 for (const auto& signal : barSignals.right)
110 {
111 FillData(FTType::rightleading, signal.leading.fine);
112 FillData(FTType::righttrailing, signal.trailing.fine);
113 }
114 }
115 }
116 }
117
118 void Map2CalParTask::FillTrigMapData()
119 {
120 for (const auto& [moduleNum, moduleSignals] : trigMappedData_)
121 {
122 R3BLOG(debug,
123 fmt::format("Calibrating with the map-level bar trig signal: {}, module num: {}",
124 moduleSignals.signal,
125 moduleNum));
126 FillEngine(trigMapCalEngine_, FTType::trigger, moduleSignals.signal.fine, moduleNum);
127 }
128 }
129
130 void Map2CalParTask::write_parameter()
131 {
132 R3BLOG(debug, "Starting to write calibration parameter...");
133 mapCalEngine_.Writer_to_TCalPar(cal_strategy_, *calibrationPar_);
134 if (is_trigID_auto_)
135 {
136 GetBasePar()->SetTrigIDMap(trigIDMappingFinder_.extract_trigIDMap());
137 }
138 calibrationPar_->SetSlowClockFrequency(coarse_time_frequency_);
139
140 trigMapCalEngine_.Writer_to_TCalPar(cal_strategy_, *calibrationTrigPar_);
141 R3BLOG(debug, "Calibration parameter written complete.");
142 }
143
144 void Map2CalParTask::WriteHists()
145 {
146 auto old_dir = gDirectory;
147 auto* runDb = FairRuntimeDb::instance();
148 if (auto* parRootFileIo = dynamic_cast<FairParRootFileIo*>(runDb->getOutput()); parRootFileIo != nullptr)
149 {
150 auto* parRootFile = parRootFileIo->getParRootFile();
151 if (!parRootFile->IsOpen() || !parRootFile->IsWritable())
152 {
153 throw R3B::runtime_error("parRootFile is either closed or unable to be written!");
154 }
155 parRootFile->cd();
156 auto* new_dir = gDirectory->mkdir("CalParHistograms", "", true);
157 if (new_dir == nullptr)
158 {
159 throw R3B::runtime_error("Cannot create a directory for the histrogams!");
160 }
161 mapCalEngine_.WriteHist2File(new_dir);
162 if (is_trig_enabled_)
163 {
164 trigMapCalEngine_.WriteHist2File(new_dir);
165 }
166 old_dir->cd();
167 }
168 else
169 {
170 throw R3B::runtime_error("unable to obtain the FairParRootFileIo object!");
171 }
172 }
173
174 // void Map2CalParTask::PrintTrigID() const
175 // {
176 // if (calibrationPar_ == nullptr)
177 // {
178 // throw R3B::runtime_error("Cannot print trigIDMapping from a nullptr parameter!");
179 // }
180
181 // const auto& trigID = base_par_->GetTrigIDMap();
182 // if (is_trigID_auto_)
183 // {
184 // trigIDIO.Save_json(trigID);
185 // }
186 // trigIDIO.Print(trigID);
187 // }
188} // namespace R3B::Neuland
#define R3BLOG(severity, x)
Definition R3BLogger.h:35
void ExtraInit(FairRootManager *rootMan) override
Simulation of NeuLAND Bar/Paddle.
constexpr auto Neuland_PlaneBar2ModuleNum(unsigned int planeNum, unsigned int barNum) -> unsigned int