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