R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
neuland_create_base_par.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2023 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include "FairParRootFileIo.h"
17#include <FairBaseParSet.h>
18#include <FairGeoParSet.h>
19#include <FairParSet.h>
20#include <FairRtdbRun.h>
21#include <R3BException.h>
22#include <R3BLogger.h>
23#include <R3BNeulandBasePar.h>
24#include <R3BProgramOptions.h>
25#include <fstream>
26#include <regex>
27
28namespace nlh = nlohmann;
29
30// auto get_json_from_url(std::string_view url) -> nlh::json
31// {
32// auto response = cpr::Get(cpr::Url{ url });
33// if (response.status_code == 0)
34// {
35// throw R3B::runtime_error(fmt::format("{}", response.error.message));
36// }
37// constexpr auto DEFAULT_CODE_ERROR = 400;
38// if (response.status_code >= DEFAULT_CODE_ERROR)
39// {
40// throw R3B::runtime_error(fmt::format("Error \"{}\" making request", response.error.message));
41// }
42// std::cout << "JSON url request successful! Took " << response.elapsed << std::endl;
43// return nlh::json::parse(response.text);
44// }
45
46auto get_json_from_file(std::string_view filepath) -> nlh::json
47{
48 fmt::print("Reading trigIDMap from file {} ...\n", filepath);
49 auto istream = std::ifstream{ filepath.data() };
50 if (not istream.is_open())
51 {
52 throw R3B::runtime_error(fmt::format("Cannot open JSON file {}", filepath));
53 }
54 return nlh::json::parse(istream);
55}
56
57inline auto is_http_address(const std::string& name) { return std::regex_search(name, std::regex{ "^http" }); }
58
59void add_parameter(FairParSet* par, FairRuntimeDb* rtdb)
60{
61 par->setChanged();
62 if (rtdb->addContainer(par); par == nullptr)
63 {
64 throw R3B::runtime_error("Calibration parameter becomes nullptr!");
65 }
66}
67
68auto main(int argc, char** argv) -> int
69{
70 constexpr auto DEFAULT_RUN_ID = 999;
71 auto programOptions = R3B::ProgramOptions("NeuLAND calibration base parameter generator");
72 auto help = programOptions.create_option<bool>("help,h", "help message", false);
73 auto logLevel = programOptions.create_option<std::string>("logLevel,v", "set log level of fairlog", "info");
74 auto output = programOptions.create_option<std::string>("output,o", "set the output root filename");
75 auto source = programOptions.create_option<std::string>("source,s", "set the JSON file source").make_positional(1);
76 auto runID = programOptions.create_option<int>("runID,r", "set the event number", DEFAULT_RUN_ID);
77
78 if (!programOptions.verify(argc, argv))
79 {
80 return EXIT_FAILURE;
81 }
82
83 try
84 {
85 auto json_obj = get_json_from_file(source());
86 auto base_par = std::make_unique<R3B::Neuland::CalibrationBasePar>();
87 json_obj.get_to(*base_par);
88
89 FairLogger::GetLogger()->SetLogScreenLevel(logLevel().c_str());
90 auto rtdb = std::unique_ptr<FairRuntimeDb>(FairRuntimeDb::instance());
91 auto parOut = std::make_unique<FairParRootFileIo>(false);
92 parOut->open(output().c_str(), "RECREATE");
93 add_parameter(base_par.release(), rtdb.get());
94 add_parameter(std::make_unique<FairBaseParSet>().release(), rtdb.get());
95 add_parameter(std::make_unique<FairGeoParSet>().release(), rtdb.get());
96 add_parameter(std::make_unique<R3B::Map2CalPar>("LandTCalPar").release(), rtdb.get());
97 add_parameter(std::make_unique<R3B::Map2CalPar>("LandTrigTCalPar").release(), rtdb.get());
98 add_parameter(std::make_unique<R3B::Neuland::Cal2HitPar>("NeulandHitPar").release(), rtdb.get());
99 rtdb->setOutput(parOut.release());
100 rtdb->saveOutput();
101 rtdb->addRun(runID());
102 rtdb->writeContainers();
103 }
104 catch (const std::exception& ex)
105 {
106 fmt::print("An exception has occurred: {}\n", ex.what());
107 }
108 catch (...)
109 {
110 fmt::print("An unrecgonised exception has occurred!\n");
111 }
112
113 fmt::print("Parameters are created successfully in file {:?}!", output());
114
115 return 0;
116}
auto main() -> int
Definition ex_test.cxx:109
auto is_http_address(const std::string &name)
void add_parameter(FairParSet *par, FairRuntimeDb *rtdb)
auto get_json_from_file(std::string_view filepath) -> nlh::json
constexpr int DEFAULT_RUN_ID