Files
karl/src/generators/binning_generator.cpp
T

181 lines
6.1 KiB
C++
Executable File

#include "generators/binning_generator.h"
#include <sstream>
#include <string>
#include "simulation/errors.h"
#include "simulation/jansson_additionals.h"
#include "simulation/unit_converter.h"
namespace generator {
CylinderBinning buildPositionBinning(json_t* object) {
if (json_object_by_path(object, {"CylinderBinning"})) {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating position binning: cylinder binning.");
#endif
object = json_object_by_path(object, {"CylinderBinning"});
json_t* radial = json_object_by_path(object, {"Radial"});
int number_of_bins_radial =
json_int(json_object_by_path(radial, {"NumberOfBins"}));
ASSERT_POSITIVE(&number_of_bins_radial, "Number of Bins must be positive");
double max_radius =
json_read_length(json_object_by_path(radial, {"MaxRadius"}), "m");
ASSERT_POSITIVE(&max_radius, "Radius must be positive");
json_t* longitudinal = json_object_by_path(object, {"Longitudinal"});
int number_of_bins_longitudinal =
json_int(json_object_by_path(longitudinal, {"NumberOfBins"}));
ASSERT_POSITIVE(&number_of_bins_longitudinal,
"Number of Bins must be positive");
double max_length =
json_read_length(json_object_by_path(longitudinal, {"MaxLength"}), "m");
ASSERT_POSITIVE(&max_length, "Radius must be positive");
#ifdef debug_level3
DEBUG_LEVEL_3("with number of bins radial: " +
std::to_string(number_of_bins_radial));
DEBUG_LEVEL_3("with number of bins longitudinal: " +
std::to_string(number_of_bins_longitudinal));
DEBUG_LEVEL_3("with radius: " + std::to_string(max_radius) + " m");
DEBUG_LEVEL_3("with height: " + std::to_string(max_length) + " m");
#endif
return CylinderBinning(number_of_bins_radial, max_radius,
number_of_bins_longitudinal, max_length);
} else {
ERROR("This position binning is not implemented.");
return CylinderBinning(0, 0, 0, 0);
}
}
CylinderBinning buildPositionBinningIncludingRedirection(
Parameter& parameter,
json_path& path_to_binning) {
if (parameter.jsonObjectFromBase(
json_path_append(path_to_binning, {"CreateSameAs"}))) {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating position binning: CreateSameAs.");
#endif
json_path path_to_same = json_path_get(parameter.jsonObjectFromBase(
json_path_append(path_to_binning, {"CreateSameAs"})));
std::stringstream path;
for (auto& p : path_to_same) {
path << p << " ";
}
#ifdef debug_level3
DEBUG_LEVEL_3("From path: " + path.str());
#endif
return buildPositionBinningIncludingRedirection(parameter, path_to_same);
} else {
return buildPositionBinning(parameter.jsonObjectFromBase(path_to_binning));
}
}
EnergyBinning buildEnergyBinning(json_t* object) {
std::string energy_type = json_string(json_object_by_path(object, {"Type"}));
if (energy_type == "Logarithmic") {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating energy binning: logarithmic.");
#endif
int number_of_bins =
json_int(json_object_by_path(object, {"NumberOfBins"}));
ASSERT_POSITIVE(&number_of_bins, "Number of Bins must be positive");
double min_energy =
json_read_energy(json_object_by_path(object, {"MinEnergy"}), "eV");
ASSERT_POSITIVE(&min_energy, "Min Energy must be positive");
double max_energy =
json_read_energy(json_object_by_path(object, {"MaxEnergy"}), "eV");
ASSERT_POSITIVE(&max_energy, "Max Energy must be positive");
#ifdef debug_level3
DEBUG_LEVEL_3("with number of bins: " + std::to_string(number_of_bins));
DEBUG_LEVEL_3("with min energy: " + std::to_string(min_energy));
DEBUG_LEVEL_3("with max energy: " + std::to_string(max_energy));
#endif
return EnergyBinning(min_energy, max_energy, number_of_bins);
} else {
ERROR("This energy binning is not implemented.");
return EnergyBinning(0, 0, 0);
}
}
EnergyBinning buildEnergyBinningIncludingRedirection(
Parameter& parameter,
json_path& path_to_binning) {
if (parameter.jsonObjectFromBase(
json_path_append(path_to_binning, {"CreateSameAs"}))) {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating energy binning: CreateSameAs.");
#endif
json_path path_to_same = json_path_get(parameter.jsonObjectFromBase(
json_path_append(path_to_binning, {"CreateSameAs"})));
#ifdef debug_level3
std::stringstream path;
for (auto& p : path_to_same) {
path << p << " ";
}
DEBUG_LEVEL_3("From path: " + path.str());
#endif
return buildEnergyBinningIncludingRedirection(parameter, path_to_same);
} else {
return buildEnergyBinning(parameter.jsonObjectFromBase(path_to_binning));
}
}
PitchAngleBinning buildPitchAngleBinning(json_t* object) {
std::string pitch_angle_type = json_string(json_object_by_path(object, {"Type"}));
if (pitch_angle_type == "Linear") {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating pitch angle binning: linear.");
#endif
int number_of_bins =
json_int(json_object_by_path(object, {"NumberOfBins"}));
ASSERT_POSITIVE(&number_of_bins, "Number of Bins must be positive");
#ifdef debug_level3
DEBUG_LEVEL_3("with number of bins: " + std::to_string(number_of_bins));
#endif
return PitchAngleBinning(number_of_bins);
} else {
ERROR("This pitch angle binning is not implemented: " + pitch_angle_type);
return PitchAngleBinning(0);
}
}
PitchAngleBinning buildPitchAngleBinningIncludingRedirection(
Parameter& parameter,
json_path& path_to_binning) {
if (parameter.jsonObjectFromBase(
json_path_append(path_to_binning, {"CreateSameAs"}))) {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating energy binning: CreateSameAs.");
#endif
json_path path_to_same = json_path_get(parameter.jsonObjectFromBase(
json_path_append(path_to_binning, {"CreateSameAs"})));
#ifdef debug_level3
std::stringstream path;
for (auto& p : path_to_same) {
path << p << " ";
}
DEBUG_LEVEL_3("From path: " + path.str());
#endif
return buildPitchAngleBinningIncludingRedirection(parameter, path_to_same);
} else {
return buildPitchAngleBinning(parameter.jsonObjectFromBase(path_to_binning));
}
}
}; // namespace generator