R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BNeulandBaseParCreator.cxx
Go to the documentation of this file.
2#include "CLI/CLI.hpp"
3#include "R3BException.h"
4#include "R3BNeulandBasePar.h"
5#include "R3BNeulandCommon.h"
6#include <FairBaseParSet.h>
7#include <FairGeoParSet.h>
8#include <FairLogger.h>
9#include <FairParRootFileIo.h>
10#include <FairParSet.h>
11#include <FairRuntimeDb.h>
12#include <fairlogger/Logger.h>
13#include <fmt/core.h>
14#include <fstream>
15#include <ios>
16#include <memory>
17#include <re2/re2.h>
18#include <string>
19#include <utility>
20
21namespace R3B::Neuland
22{
23 namespace
24 {
25 void add_parameter(FairParSet* par, FairRuntimeDb* rtdb)
26 {
27 par->setChanged();
28 if (rtdb->addContainer(par); par == nullptr)
29 {
30 throw R3B::runtime_error("Calibration parameter becomes nullptr!");
31 }
32 }
33
34 auto parsing_a_line(const std::string& line_str, CalibrationBasePar::TrigIDMap& trig_id_map) -> bool
35 {
36 auto plane_id = -1;
37 auto side_id = -1;
38 auto bar_id = -1;
39 auto trig_module_id = -1;
40 auto res = RE2::FullMatch(line_str,
41 R"(.*\[(\d+)\]\[(\d+)\]\[(\d+)\]\s+\=\s+(\d+);)",
42 &plane_id,
43 &side_id,
44 &bar_id,
45 &trig_module_id);
46 if (not res)
47 {
48 return false;
49 }
50
51 auto& module_nums =
52 trig_id_map.try_emplace((plane_id * BarsPerPlane) + bar_id + 1, std::make_pair(-1, -1)).first->second;
53 if (side_id == 0)
54 {
55 module_nums.first = trig_module_id + 1;
56 }
57 else
58 {
59 module_nums.second = trig_module_id + 1;
60 }
61
62 return true;
63 }
64
65 auto check_trig_id_map(CalibrationBasePar::TrigIDMap& trig_id_map) -> bool
66 {
67 auto res = true;
68 for (const auto& [bar_number, trig_ids] : trig_id_map)
69 {
70 auto has_trig_id = (trig_ids.first != -1) and (trig_ids.second != -1);
71 if (not has_trig_id)
72 {
73 LOGP(error, "Trig module numbers are incomplete for the bar number {}!", bar_number);
74 }
75 res &= has_trig_id;
76 }
77 return res;
78 }
79 } // namespace
80
81 void BaseParCreator::setup_options(CLI::App& program_options)
82 {
83
84 program_options.add_option("-s, --severity", log_level_, "Set the severity level");
85 program_options
86 .add_option("-i, --input", input_header_file_name_, "Set the filename of the input header file.")
87 ->required(true);
88 program_options
89 .add_option("-o, --output", output_par_file_name_, "Set the filename of the ouput parameter root file.")
90 ->required(true);
91 program_options.add_option("-e, --exp-id", exp_id_, "Set the experiment id, such as \"s118\"")->required(true);
92 program_options
93 .add_option("--offspill-pos", offspill_pos_, "Set the TPAT bit position of neuland offspill data")
94 ->required(true);
95 }
96
97 void BaseParCreator::init() { FairLogger::GetLogger()->SetLogScreenLevel(log_level_.c_str()); }
98
100 {
101 auto ifile = std::ifstream{ input_header_file_name_, std::ios::in };
102 if (not ifile.is_open())
103 {
104 throw R3B::logic_error(fmt::format("Cannot open the header file {}", input_header_file_name_));
105 }
106 auto line_string = std::string{};
107 std::getline(ifile, line_string, '\n');
108 parsing_first_line(line_string);
109
110 auto trig_id_map = CalibrationBasePar::TrigIDMap{};
111 while (std::getline(ifile, line_string, '\n'))
112 {
113 parsing_a_line(line_string, trig_id_map);
114 }
115 return trig_id_map;
116 }
117
118 void BaseParCreator::parsing_first_line(const std::string& line_str)
119 {
120 if (not RE2::FullMatch(line_str, R"(.*\[(\d+)\]\[\d+\]\[\d+\];)", &num_of_planes_))
121 {
122 throw R3B::runtime_error(fmt::format("Failed to extract the plane number from the string {:?}.", line_str));
123 }
124 }
125
127 {
128 auto trig_id_map = parsing_header_file();
129
130 if (not check_trig_id_map(trig_id_map))
131 {
132 throw R3B::runtime_error("Trig ID map is incomplete for some bars!");
133 }
134
135 auto base_par = std::make_unique<R3B::Neuland::CalibrationBasePar>();
136 base_par->set_num_of_planes(num_of_planes_);
137 base_par->set_exp_ids(exp_id_);
138 base_par->set_offspill_tpat_pos(offspill_pos_);
139 base_par->set_trig_id_map(std::move(trig_id_map));
140
141 auto rtdb = std::unique_ptr<FairRuntimeDb>(FairRuntimeDb::instance());
142 auto parOut = std::make_unique<FairParRootFileIo>(false);
143 parOut->open(output_par_file_name_.c_str(), "RECREATE");
144 add_parameter(base_par.release(), rtdb.get());
145 add_parameter(std::make_unique<FairBaseParSet>().release(), rtdb.get());
146 add_parameter(std::make_unique<FairGeoParSet>().release(), rtdb.get());
147 rtdb->setOutput(parOut.release());
148 rtdb->saveOutput();
149 rtdb->addRun(run_id_);
150 rtdb->writeContainers();
151 }
152} // namespace R3B::Neuland
void init() override
Initialization of a CLI program.
void run() override
Run the CLI program.
void parsing_first_line(const std::string &line_str)
auto parsing_header_file() -> CalibrationBasePar::TrigIDMap
void setup_options(CLI::App &program_options) override
Setup the CLI options given to the program.
std::unordered_map< int, std::pair< int, int > > TrigIDMap
Simulation of NeuLAND Bar/Paddle.
constexpr auto BarsPerPlane