R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
ParticleFilterJson.h
Go to the documentation of this file.
1#pragma once
2
4#include "R3BException.h"
6#include <fmt/core.h>
7#include <magic_enum/magic_enum.hpp>
8#include <nlohmann/json.hpp>
9#include <nlohmann/json_fwd.hpp>
10#include <string>
11#include <vector>
12
13namespace R3B::Neuland
14{
15 template <>
16 inline void to_json(nlohmann::ordered_json& json_obj, const ParticleFilter::Options& options)
17 {
18 json_obj = nlohmann::ordered_json{
19 { "mode", magic_enum::enum_name(options.mode) }, { "global-min-energy", options.global_min_energy },
20 { "global-max-energy", options.global_max_energy }, { "particle-types", options.particle_names },
21 { "energy-range-mins", options.energy_range_mins }, { "energy-range-maxs", options.energy_range_maxs },
22 };
23 }
24
25 template <>
26 inline void from_json(const nlohmann::ordered_json& json_obj, ParticleFilter::Options& option)
27 {
28 json_obj.at("particle-types").get_to(option.particle_names);
29 json_obj.at("global-min-energy").get_to(option.global_min_energy);
30 json_obj.at("global-max-energy").get_to(option.global_max_energy);
31 json_obj.at("energy-range-mins").get_to(option.energy_range_mins);
32 json_obj.at("energy-range-maxs").get_to(option.energy_range_maxs);
33
34 // parse mode
35 auto mode_name = std::string{};
36 json_obj.at("mode").get_to(mode_name);
37 auto mode = magic_enum::enum_cast<R3B::Neuland::ParticleFilter::Mode>(mode_name, magic_enum::case_insensitive);
38 if (mode.has_value())
39 {
40 option.mode = mode.value();
41 }
42 else
43 {
44 throw R3B::logic_error(fmt::format("Cannot parse the mode string {:?} to the corresponding enum class. "
45 "Please check if the enum string is correct!",
46 mode_name));
47 }
48
49 // append zeros if size unmatched
50 const auto particle_size = option.particle_names.size();
51 option.energy_range_maxs.resize(particle_size, 0.);
52 option.energy_range_mins.resize(particle_size, 0.);
53 }
54
55} // namespace R3B::Neuland
Simulation of NeuLAND Bar/Paddle.
void from_json(const nlohmann::ordered_json &json_obj, GeneratorFactory::Options &options)
void to_json(nlohmann::ordered_json &json_obj, const GeneratorFactory::Options &options)
Option structure to create a filter.