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"
15#include "R3BNeulandBaseParJson.h" //NOLINT
18#include <FairBaseParSet.h>
19#include <FairGeoParSet.h>
20#include <FairLogger.h>
21#include <FairParSet.h>
22#include <FairRuntimeDb.h>
23#include <R3BException.h>
24#include <R3BNeulandBasePar.h>
25#include <R3BProgramOptions.h>
26#include <cstdlib>
27#include <exception>
28#include <fmt/core.h>
29#include <fstream>
30#include <memory>
31#include <nlohmann/json.hpp>
32#include <nlohmann/json_fwd.hpp>
33#include <string>
34#include <string_view>
35
36namespace nlh = nlohmann;
37
38// auto get_json_from_url(std::string_view url) -> nlh::json
39// {
40// auto response = cpr::Get(cpr::Url{ url });
41// if (response.status_code == 0)
42// {
43// throw R3B::runtime_error(fmt::format("{}", response.error.message));
44// }
45// constexpr auto DEFAULT_CODE_ERROR = 400;
46// if (response.status_code >= DEFAULT_CODE_ERROR)
47// {
48// throw R3B::runtime_error(fmt::format("Error \"{}\" making request", response.error.message));
49// }
50// std::cout << "JSON url request successful! Took " << response.elapsed << std::endl;
51// return nlh::json::parse(response.text);
52// }
53
54namespace
55{
56 auto get_json_from_file(std::string_view filepath) -> nlh::ordered_json
57 {
58 fmt::print("Reading trigIDMap from file {} ...\n", filepath);
59 auto istream = std::ifstream{ filepath.data() };
60 if (not istream.is_open())
61 {
62 throw R3B::runtime_error(fmt::format("Cannot open JSON file {}", filepath));
63 }
64 return nlh::ordered_json::parse(istream);
65 }
66
67 // inline auto is_http_address(const std::string& name) { return std::regex_search(name, std::regex{ "^http" }); }
68
69 void add_parameter(FairParSet* par, FairRuntimeDb* rtdb)
70 {
71 par->setChanged();
72 if (rtdb->addContainer(par); par == nullptr)
73 {
74 throw R3B::runtime_error("Calibration parameter becomes nullptr!");
75 }
76 }
77} // namespace
78
79auto main(int argc, char** argv) -> int
80{
81 constexpr auto DEFAULT_RUN_ID = 999;
82 auto programOptions = R3B::ProgramOptions("NeuLAND calibration base parameter generator");
83 auto help = programOptions.create_option<bool>("help,h", "help message", false);
84 auto logLevel = programOptions.create_option<std::string>("logLevel,v", "set log level of fairlog", "info");
85 auto output = programOptions.create_option<std::string>("output,o", "set the output root filename");
86 auto source = programOptions.create_option<std::string>("source,s", "set the JSON file source").make_positional(1);
87 auto runID = programOptions.create_option<int>("runID,r", "set the event number", DEFAULT_RUN_ID);
88
89 if (!programOptions.verify(argc, argv))
90 {
91 return EXIT_FAILURE;
92 }
93
94 try
95 {
96 const auto json_obj = get_json_from_file(source());
97 auto base_par = std::make_unique<R3B::Neuland::CalibrationBasePar>();
98 json_obj.get_to(*base_par);
99
100 FairLogger::GetLogger()->SetLogScreenLevel(logLevel().c_str());
101 auto rtdb = std::unique_ptr<FairRuntimeDb>(FairRuntimeDb::instance());
102 auto parOut = std::make_unique<FairParRootFileIo>(false);
103 parOut->open(output().c_str(), "RECREATE");
104 add_parameter(base_par.release(), rtdb.get());
105 add_parameter(std::make_unique<FairBaseParSet>().release(), rtdb.get());
106 add_parameter(std::make_unique<FairGeoParSet>().release(), rtdb.get());
107 add_parameter(std::make_unique<R3B::Map2CalPar>("LandTCalPar").release(), rtdb.get());
108 add_parameter(std::make_unique<R3B::Map2CalPar>("LandTrigTCalPar").release(), rtdb.get());
109 add_parameter(std::make_unique<R3B::Neuland::Cal2HitPar>("NeulandHitPar").release(), rtdb.get());
110 rtdb->setOutput(parOut.release());
111 rtdb->saveOutput();
112 rtdb->addRun(runID());
113 rtdb->writeContainers();
114 }
115 catch (const std::exception& ex)
116 {
117 fmt::print("An exception has occurred: {}\n", ex.what());
118 }
119 catch (...)
120 {
121 fmt::print("An unrecgonised exception has occurred!\n");
122 }
123
124 fmt::print("Parameters are created successfully in file {:?}!", output());
125
126 return 0;
127}
auto main(int argc, char **argv) -> int
constexpr int DEFAULT_RUN_ID