R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
NeulandParticleFilter.cxx
Go to the documentation of this file.
2#include "R3BException.h"
3#include "R3BNeulandPoint.h"
4#include <fmt/core.h>
5#include <fmt/ranges.h> // NOLINT
6#include <magic_enum/magic_enum.hpp>
7#include <range/v3/view/zip.hpp>
8#include <string>
9#include <utility>
10
11namespace R3B::Neuland
12{
13 namespace
14 {
15 inline auto check_if_within_range(double value, std::pair<double, double> min_max) -> bool
16 {
17
18 if (min_max.first == min_max.second)
19 {
20 return true;
21 }
22 if (min_max.first < min_max.second)
23 {
24 return (value > min_max.first) and (value < min_max.second);
25 }
26 return (value > min_max.first) or (value < min_max.second);
27 }
28 } // namespace
29
31 {
32 if (options.particle_names.size() != options.energy_range_maxs.size())
33 {
34 throw R3B::logic_error(fmt::format(
35 "Number of particle types specified (={}) is not equal to the number of range maximum (={}).",
36 options.particle_names.size(),
37 options.energy_range_maxs.size()));
38 }
39 if (options.particle_names.size() != options.energy_range_mins.size())
40 {
41 throw R3B::logic_error(fmt::format(
42 "Number of particle types specified (={}) is not equal to the number of range minimum (={}).",
43 options.particle_names.size(),
44 options.energy_range_mins.size()));
45 }
46
47 auto filter = ParticleFilter{ options.mode };
48 filter.SetMaxEnergy(options.global_max_energy);
49 filter.SetMinEnergy(options.global_min_energy);
50
51 for (auto [type, range_min, range_max] :
52 ranges::views::zip(options.particle_names, options.energy_range_mins, options.energy_range_maxs))
53 {
54 filter.AddParticle(type, range_min, range_max);
55 }
56
57 return filter;
58 }
59
60 auto ParticleFilter::IsPointAllowed(const R3BNeulandPoint& neuland_point) -> bool
61 {
62 if (not check_if_within_range(neuland_point.GetEnergyLoss(),
63 std::pair{ minimum_allowed_energy_, maximum_allowed_energy_ }))
64 {
65 return false;
66 }
67
68 switch (mode_)
69 {
70 case Mode::whitelist:
71 return is_allowed_with_whitelist(neuland_point);
72 case Mode::blacklist:
73 return is_allowed_with_blacklist(neuland_point);
74 case Mode::pass:
75 return true;
76 }
77 return true;
78 }
79
80 void ParticleFilter::AddParticle(const std::string& particle_name, double min_energy, double max_energy)
81 {
82 const auto pid = pdg_converter_.get_pid(particle_name);
83 if (list_.find(pid) == list_.end())
84 {
85 list_.emplace(pid, std::pair{ min_energy, max_energy });
86 }
87 else
88 {
89 list_.at(pid) = std::pair{ min_energy, max_energy };
90 }
91 }
92
94 {
95 const auto particle_pid = neuland_point.GetPID();
96 if (list_.find(particle_pid) != list_.end())
97 {
98 return check_if_within_range(neuland_point.GetEnergyLoss(), list_.at(particle_pid));
99 }
100 return false;
101 }
102
104 {
105 const auto particle_pid = neuland_point.GetPID();
106 if (list_.find(particle_pid) != list_.end())
107 {
108 return not check_if_within_range(neuland_point.GetEnergyLoss(), list_.at(particle_pid));
109 }
110 return true;
111 }
112
113 auto ParticleFilter::Print() -> std::string
114 {
115 return fmt::format("Filter Content: \n"
116 "=============================\n"
117 "-- Mode: {}\n"
118 "-- Global minimun energy:{}\n"
119 "-- Global maximum energy: {}\n"
120 "-- particles: {}\n"
121 "=============================",
122 magic_enum::enum_name(mode_),
125 list_);
126 }
127
129 {
133 list_.clear();
134 }
135} // namespace R3B::Neuland
double maximum_allowed_energy_
global maximum engergy in GeV
ParticleFilter()=default
Default constructor.
auto IsPointAllowed(const R3BNeulandPoint &neuland_point) -> bool
Check if the neuland point is allowed.
auto Print() -> std::string
Print the configuration of the current filter.
auto is_allowed_with_whitelist(const R3BNeulandPoint &neuland_point) -> bool
static auto Create(const Options &options) -> ParticleFilter
Generator for the filter object from the configuration.
std::unordered_map< int32_t, std::pair< double, double > > list_
Whitelist with min energy values [GeV].
void AddParticle(const std::string &particle_name, double min_energy=0., double max_energy=0.)
Add particle to the list.
double minimum_allowed_energy_
global minimum engergy in GeV
auto is_allowed_with_blacklist(const R3BNeulandPoint &neuland_point) -> bool
Simulation of NeuLAND Bar/Paddle.
Option structure to create a filter.