9#include <analysis/utility/PIDNeuralNetwork.h>
23 ", could not be found! It is necessary for the neural network based PID variables.");
25 m_model = std::make_unique<fdeep::model>(fdeep::read_model_from_string((*m_pidNeuralNetworkParametersDB)->getModelDefinition(),
27 [](
const std::string&) {}
32 std::map<std::string, std::string> inputNameMap;
35 const auto oldName =
"pidLogLikelihood_Of_" + std::to_string(abs(hypeType.getPDGCode())) +
"_From_" +
Const::parseDetectors(
37 const auto newName =
"pidLogLikelihoodValueExpert(" + std::to_string(abs(hypeType.getPDGCode())) +
',' +
Const::parseDetectors(
39 inputNameMap[oldName] = newName;
42 inputNameMap[
"momentum"] =
"p";
46 for (std::string name : (*m_pidNeuralNetworkParametersDB)->getInputNames()) {
47 const auto itr = inputNameMap.find(name);
48 if (itr != inputNameMap.end()) name = itr->second;
54 m_extraInfoNames[outputPdgCode] =
"pidNeuralNetworkValueExpert(" + std::to_string(outputPdgCode) \
65 for (
auto const& inputToCut : (*m_pidNeuralNetworkParametersDB)->getInputsToCut()) {
66 const auto [inputSetIndex, inputCutIndex, rangeStart, rangeEnd, setValue] = inputToCut;
67 if (!std::isnan(rangeStart) and !std::isnan(rangeEnd)) {
68 if (input[inputCutIndex] >= rangeStart and input[inputCutIndex] <= rangeEnd)
69 input[inputSetIndex] = setValue;
70 }
else if (!std::isnan(rangeStart)) {
71 if (input[inputCutIndex] >= rangeStart)
72 input[inputSetIndex] = setValue;
73 }
else if (!std::isnan(rangeEnd)) {
74 if (input[inputCutIndex] <= rangeEnd)
75 input[inputSetIndex] = setValue;
81 for (
size_t i = 0; i < input.size(); ++i) {
82 input[i] = (input[i] - (*m_pidNeuralNetworkParametersDB)->getMeanValues()[i]) /
83 (*m_pidNeuralNetworkParametersDB)->getStandardDeviations()[i];
87 for (
auto const& index_value : (*m_pidNeuralNetworkParametersDB)->getHandleMissingInputs()) {
88 const auto [index, value] = index_value;
89 if (std::isnan(input[index])) input[index] = value;
93 const auto inputFdeep = fdeep::tensor(fdeep::tensor_shape(input.size()), input);
94 const auto result =
m_model->predict({inputFdeep});
96 std::map<int, double> probabilities;
98 const int outputIndex = (*m_pidNeuralNetworkParametersDB)->pdg2OutputIndex(pdgCode);
99 probabilities[pdgCode] = result.front().get(fdeep::tensor_pos(outputIndex));
101 return probabilities;
static DetectorSet set()
Accessor for the set of valid detector IDs.
static const ParticleSet chargedStableSet
set of charged stable particles
EDetector
Enum for identifying the detector components (detector and subdetector).
static std::string parseDetectors(EDetector det)
Converts Const::EDetector object to string.
size_t getInputSize() const
Get number of inputs.
std::vector< std::string > m_inputBasf2Names
list of input names of input variables in the basf2 naming scheme
std::string m_pidNeuralNetworkParametersName
name of the parameter set
void loadParametersFromDB()
Load neural-network parameters with name m_pidNeuralNetworkParametersName from the conditions data ba...
std::map< int, double > predict(std::vector< float > inputs) const
Predict neural-network output for all implemented hypotheses using the given inputs.
std::unique_ptr< const fdeep::model > m_model
frugally-deep neural network
const std::vector< int > & getOutputSpeciesPdg() const
Get the list of pdg codes of species hypotheses, for which the network predicts the probability in th...
std::map< int, std::string > m_extraInfoNames
map from PDG code to extraInfo name that stores the output of this network
std::unique_ptr< DBObjPtr< PIDNeuralNetworkParameters > > m_pidNeuralNetworkParametersDB
db object for the parameter set
Abstract base class for different kinds of events.