R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
neuland_lmd_to_map.cxx
Go to the documentation of this file.
1#include "R3BLogger.h"
4#include "R3BShared.h"
6#include "ext_h101_unpack.h"
7// #include "ext_h101_wrmaster.h"
8#include <FairLogger.h>
9#include <FairParRootFileIo.h>
10#include <FairRootFileSink.h>
11#include <FairRunOnline.h>
12#include <FairRuntimeDb.h>
13#include <R3BEventHeader.h>
16#include <R3BProgramOptions.h>
17#include <R3BTrloiiTpatReader.h>
18#include <R3BUcesbSource.h>
19#include <R3BUcesbSource2.h>
20#include <R3BUnpackReader.h>
21#include <R3BWhiterabbitMasterReader.h>
22#include <TStopwatch.h>
23#include <bits/basic_string.h>
24#include <cstddef>
25#include <cstdlib>
26#include <ext_h101_tpat.h>
27#include <fairlogger/Logger.h>
28#include <filesystem>
29#include <fmt/core.h>
30#include <iostream>
31#include <memory>
32#include <regex>
33#include <stdexcept>
34#include <string>
35
36namespace fs = std::filesystem;
37
39{
40 EXT_STR_h101_unpack_t unpack;
41 EXT_STR_h101_TPAT_t tpat;
42 EXT_STR_h101_raw_nnp_tamex_onion_t raw_nnp;
43 // EXT_STR_h101_WRMASTER_t wrmaster;
44};
45
46constexpr int DEFAULT_EVENT_NUM = 1000;
47constexpr int DEFAULT_TIME_STITCH = 4000;
48constexpr int DEFAULT_RUN_ID = 999;
49constexpr auto DEFAULT_UNPACKER_PATH = "/../unpack/202205_s509/202205_s509";
50constexpr auto NEULAND_DEFAULT_DOUBLE_PLANE = 13;
51
52auto main(int argc, char** argv) -> int
53{
54 using namespace std::string_literals;
55 auto const* ucesb_dir = getenv("UCESB_DIR");
56 TStopwatch timer;
57 timer.Start();
58
59 //====================================================================================
60 // Program options:
61 auto programOptions = R3B::ProgramOptions("options for neuland data analysis");
62 auto help = programOptions.create_option<bool>("help,h", "help message", false);
63 auto logLevel = programOptions.create_option<std::string>("logLevel,v", "set log level of fairlog", "info");
64 auto no_trig_neuland = programOptions.create_option<bool>("no-trig", "auto detect trigID", false);
65 auto input_par = programOptions.create_option<std::string>("in-par,p", "set the input parameter");
66 auto eventNum = programOptions.create_option<int>("eventNum,n", "set the event number", DEFAULT_EVENT_NUM);
67 auto input_files = programOptions.create_option<std::string>("in,i", "set the input files");
68 auto output_file = programOptions.create_option<std::string>("out,o", "set the output file");
69 auto inputRunID = programOptions.create_option<int>("runID,r", "set the input runID", DEFAULT_RUN_ID);
70 auto time_stich =
71 programOptions.create_option<int>("time-stitch,t", "set time stitch for ucesb", DEFAULT_TIME_STITCH);
72 // auto neulandDP = programOptions.create_option<int>(
73 // "dp", "set the number of double planes for neuland", NEULAND_DEFAULT_DOUBLE_PLANE);
74 auto unpacker_path = programOptions.create_option<std::string>(
75 "unpack", "set the path of unpacker executable", std::string{ ucesb_dir } + DEFAULT_UNPACKER_PATH);
76 if (!programOptions.verify(argc, argv))
77 {
78 return EXIT_FAILURE;
79 }
80
81 //====================================================================================
82 // Input definitions:
83 FairLogger::GetLogger()->SetLogScreenLevel(logLevel().c_str());
84
85 // const auto whiterabbit_id = std::stoi(wr_ID(), nullptr, 16);
86
87 const unsigned int planeNum = NEULAND_DEFAULT_DOUBLE_PLANE * 2;
88 const auto runID = inputRunID.value();
89 const auto outputfile_path = fs::path{ output_file.value() };
90 const auto outputDir = R3B::GetParentDir(output_file.value());
91 const auto parfile =
92 outputDir / fmt::format("{}.par{}", outputfile_path.stem().string(), outputfile_path.extension().string());
93 // const auto ntuple_options = "RAW"s;
94 if (ucesb_dir == nullptr)
95 {
96 R3BLOG(error, "ucesb_dir is not defined!");
97 return 1;
98 }
99 // const auto upexps_dir = std::string{ ucesb_dir } + "/../upexps"s;
100 const auto upexps_exe = fs::path{ unpacker_path.value() };
101 const auto ntuple_options =
102 (time_stich.value() > 0) ? fmt::format("RAW,time-stitch={}", time_stich.value()) : fmt::format("RAW");
103 const auto max_event_num = (eventNum.value() < 0) ? -1 : eventNum.value();
104
105 auto ucesb_command = upexps_exe.string() + " --allow-errors --input-buffer=600Mi"s;
106 // auto ucesb_command = upexps_exe.string();
107 ucesb_command = std::regex_replace(ucesb_command, std::regex("//"), "/");
108
109 auto ucesbStruct = EXT_STR_h101{};
110 auto source = std::make_unique<R3BUcesbSource2>(
111 input_files(), ntuple_options, ucesb_command, &ucesbStruct, sizeof(ucesbStruct));
112 source->SetMaxEvents(max_event_num);
113
114 //====================================================================================
115 // Adding readers
116 source->AddReader<R3BUnpackReader>(&ucesbStruct.unpack, offsetof(EXT_STR_h101, unpack));
117 source->AddReader<R3BTrloiiTpatReader>(&ucesbStruct.tpat, offsetof(EXT_STR_h101, tpat));
118
119 auto* neulandReader =
120 source->AddReader<R3BNeulandTamexReader2>(&ucesbStruct.raw_nnp, offsetof(EXT_STR_h101, raw_nnp));
122 neulandReader->SetMaxNbPlanes(planeNum);
123 if (no_trig_neuland())
124 {
125 R3BLOG(info, "Disable NeuLAND trigger times");
126 neulandReader->SetSkipTriggerTimes();
127 }
128
129 // source->AddReader<R3BWhiterabbitMasterReader>(
130 // static_cast<EXT_STR_h101_WRMASTER*>(&ucesbStruct.wrmaster), offsetof(EXT_STR_h101, wrmaster),
131 // whiterabbit_id);
132
133 //====================================================================================
134 auto run = std::make_unique<FairRunOnline>();
135 run->SetSource(source.release());
136 auto EvntHeader = std::make_unique<R3BEventHeader>();
137 run->SetEventHeader(EvntHeader.release());
138 run->SetRunId(runID);
139 run->SetSink(std::make_unique<FairRootFileSink>(output_file().c_str()));
140
141 //=====================================================================================
142 // set parameter files:
143 auto* rtdb = run->GetRuntimeDb();
144 auto parIn = std::make_unique<FairParRootFileIo>(false);
145 auto parOut = std::make_unique<FairParRootFileIo>(true);
146 parIn->open(input_par().c_str());
147 parOut->open(parfile.c_str());
148 rtdb->setFirstInput(parIn.release());
149 rtdb->setOutput(parOut.release());
150 rtdb->saveOutput();
151
152 //=====================================================================================
153 // set tasks:
154 auto calPar = std::make_unique<R3B::Neuland::Map2CalParTask>();
155 calPar->SetTrigger(R3B::Neuland::CalTrigger::all);
156 // calPar->SetPlaneNum(planeNum);
157 calPar->SetTrigEnabled(not no_trig_neuland.value());
158 calPar->SetErrorMethod(R3B::Neuland::Map2CalParTask::ErrorMethod::approx);
159 // calPar->SetTrigIDMapPrintFormat(R3B::Neuland::TrigIDMappingPrintFormat::screen);
160 calPar->SetTrigIDMapAutoFind(false);
161 calPar->SetTrigIDMapDir(outputDir.string());
162 // calPar->SetTrigIDMapFileName(jsonName());
163 run->AddTask(calPar.release());
164
165 //=====================================================================================
166 // init
167 try
168 {
169 run->Init();
170 run->Run(-1, eventNum());
171 std::cout << "Analysis finished succesfully.\n";
172 std::cout << "Output file is " << output_file() << "\n";
173
174 timer.Stop();
175 const double rtime = timer.RealTime();
176 const double ctime = timer.CpuTime();
177 std::cout << "Real time " << rtime << " s, CPU time " << ctime << " s\n";
178 }
179 catch (fair::FatalException& ex)
180 {
181 std::cerr << ex.What();
182 std::cout << "\n\n";
183 }
184 catch (std::runtime_error& ex)
185 {
186 std::cout << "A runtime error has occured: \n";
187 std::cerr << ex.what();
188 std::cout << "\n\n";
189 }
190 catch (std::logic_error& ex)
191 {
192 std::cout << "A logic error has occured: \n";
193 std::cerr << ex.what();
194 std::cout << "\n\n";
195 }
196 catch (...)
197 {
198 std::cout << "A unrecognised error has occured! \n";
199 std::cout << "\n\n";
200 }
201
202 auto* sinkfile = run->GetSink();
203 if (sinkfile != nullptr)
204 {
205 sinkfile->Close();
206 }
207
208 return 0;
209}
#define R3BLOG(severity, x)
Definition R3BLogger.h:32
struct EXT_STR_h101_t EXT_STR_h101
void AddExtraConditions(R3B::UcesbMap conditions)
Definition R3BReader.h:37
auto GetParentDir(std::string_view filename) -> fs::path
Definition R3BShared.h:184
constexpr int DEFAULT_EVENT_NUM
constexpr auto DEFAULT_UNPACKER_PATH
constexpr int DEFAULT_RUN_ID
auto main(int argc, char **argv) -> int
constexpr int DEFAULT_TIME_STITCH
constexpr auto NEULAND_DEFAULT_DOUBLE_PLANE
EXT_STR_h101_TPAT_t tpat
EXT_STR_h101_raw_nnp_tamex_onion_t raw_nnp
EXT_STR_h101_unpack_t unpack