Belle II Software  release-08-01-10
PIDNeuralNetwork Class Reference

Class to call PID neural network. More...

#include <PIDNeuralNetwork.h>

Collaboration diagram for PIDNeuralNetwork:

Public Member Functions

 PIDNeuralNetwork ()
 Constructor with default paramenter-set name.
 
 PIDNeuralNetwork (const std::string &parameterName)
 Constructor with given paramenter-set name.
 
 PIDNeuralNetwork (PIDNeuralNetwork &&other)
 Move constructor.
 
std::map< int, double > predict (std::vector< float > inputs) const
 Predict neural-network output for all implemented hypotheses using the given inputs. More...
 
size_t getInputSize () const
 Get number of inputs. More...
 
size_t getInputIndex (const std::string &name) const
 Get input names. More...
 
const std::vector< std::string > & getInputNames () const
 Get input names. More...
 
const std::vector< int > & getOutputSpeciesPdg () const
 Get the list of pdg codes of species hypotheses, for which the network predicts the probability in the order defined by the network output layer. More...
 
const std::string & getPIDNeuralNetworkParametersName () const
 Get the name of the used neural network. More...
 
bool hasPdgCode (const int pdg, const bool throwException=false) const
 

Private Member Functions

void loadParametersFromDB ()
 Load neural-network parameters with name m_pidNeuralNetworkParametersName from the conditions data base.
 

Private Attributes

std::string m_pidNeuralNetworkParametersName = "PIDNeuralNetworkParameters"
 name of the parameter set
 
std::unique_ptr< DBObjPtr< PIDNeuralNetworkParameters > > m_pidNeuralNetworkParametersDB
 db object for the parameter set
 
std::unique_ptr< const fdeep::model > m_model
 frugally-deep neural network
 

Detailed Description

Class to call PID neural network.

Definition at line 31 of file PIDNeuralNetwork.h.

Member Function Documentation

◆ getInputIndex()

size_t getInputIndex ( const std::string &  name) const
inline

Get input names.

Returns
const std::vector<std::string>& input names

Definition at line 85 of file PIDNeuralNetwork.h.

85 { return (*m_pidNeuralNetworkParametersDB)->getInputIndex(name); }

◆ getInputNames()

const std::vector<std::string>& getInputNames ( ) const
inline

Get input names.

Returns
const std::vector<std::string>& input names

Definition at line 91 of file PIDNeuralNetwork.h.

◆ getInputSize()

size_t getInputSize ( ) const
inline

Get number of inputs.

Returns
size_t number of inputs

Definition at line 79 of file PIDNeuralNetwork.h.

◆ getOutputSpeciesPdg()

const std::vector<int>& getOutputSpeciesPdg ( ) const
inline

Get the list of pdg codes of species hypotheses, for which the network predicts the probability in the order defined by the network output layer.

Returns
const std::vector<int>& list of pdg codes of hypotheses predicted by the network

Definition at line 98 of file PIDNeuralNetwork.h.

◆ getPIDNeuralNetworkParametersName()

const std::string& getPIDNeuralNetworkParametersName ( ) const
inline

Get the name of the used neural network.

Returns
const std::string& name of the used neural network

Definition at line 104 of file PIDNeuralNetwork.h.

◆ hasPdgCode()

bool hasPdgCode ( const int  pdg,
const bool  throwException = false 
) const
inline
Parameters
pdgpdg code of hypothesis
throwExceptionthrow exception if pdg code is not predicted
Returns
true if neural network predicts probability for the given hypothesis
false if neural network does not predict probability for the given hypothesis

Definition at line 112 of file PIDNeuralNetwork.h.

◆ predict()

std::map< int, double > predict ( std::vector< float >  inputs) const

Predict neural-network output for all implemented hypotheses using the given inputs.

Parameters
inputsset of input variables
Returns
double probability of the asked particle-species hypothesis
std::map<int, double> map of pdg codes to the corresponding probabilities

Definition at line 32 of file PIDNeuralNetwork.cc.

33 {
34 
35  // apply cuts, ie. overwrite certain input values with index `inputSetIndex` with the value `setValue`
36  // if the input with index `inputCutIndex` is in the range (`rangeStart`, `rangeEnd`)
37  for (auto const& inputToCut : (*m_pidNeuralNetworkParametersDB)->getInputsToCut()) {
38  const auto [inputSetIndex, inputCutIndex, rangeStart, rangeEnd, setValue] = inputToCut;
39  if (!std::isnan(rangeStart) and !std::isnan(rangeEnd)) {
40  if (input[inputCutIndex] >= rangeStart and input[inputCutIndex] <= rangeEnd)
41  input[inputSetIndex] = setValue;
42  } else if (!std::isnan(rangeStart)) {
43  if (input[inputCutIndex] >= rangeStart)
44  input[inputSetIndex] = setValue;
45  } else if (!std::isnan(rangeEnd)) {
46  if (input[inputCutIndex] <= rangeEnd)
47  input[inputSetIndex] = setValue;
48  }
49  }
50 
51  // Normalize inputs
52  // By definition, this has to be done after applying the cuts and before handling missing information
53  for (size_t i = 0; i < input.size(); ++i) {
54  input[i] = (input[i] - (*m_pidNeuralNetworkParametersDB)->getMeanValues()[i]) /
55  (*m_pidNeuralNetworkParametersDB)->getStandardDeviations()[i];
56  }
57 
58  // handle missing information
59  for (auto const& index_value : (*m_pidNeuralNetworkParametersDB)->getHandleMissingInputs()) {
60  const auto [index, value] = index_value;
61  if (std::isnan(input[index])) input[index] = value;
62  }
63 
64  // apply neural network
65  const auto inputFdeep = fdeep::tensor(fdeep::tensor_shape(input.size()), input);
66  const auto result = m_model->predict({inputFdeep});
67 
68  std::map<int, double> probabilities;
69  for (const auto pdgCode : getOutputSpeciesPdg()) {
70  const int outputIndex = (*m_pidNeuralNetworkParametersDB)->pdg2OutputIndex(pdgCode);
71  probabilities[pdgCode] = result.front().get(fdeep::tensor_pos(outputIndex));
72  }
73  return probabilities;
74 }
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::unique_ptr< const fdeep::model > m_model
frugally-deep neural network

The documentation for this class was generated from the following files: