Belle II Software development
PIDNeuralNetwork Class Reference

Class to call PID neural network. More...

#include <PIDNeuralNetwork.h>

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.
 
size_t getInputSize () const
 Get number of inputs.
 
const std::vector< std::string > & getInputBasf2Names () const
 Get names of input variables in the basf2 naming scheme, which may be different from the one in the payload.
 
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.
 
const std::string & getPIDNeuralNetworkParametersName () const
 Get the name of the used neural network.
 
bool hasPdgCode (const int pdg, const bool throwException=false) const
 
const std::string & getExtraInfoName (const int pdg) const
 

Private Member Functions

void loadParametersFromDB ()
 Load neural-network parameters with name m_pidNeuralNetworkParametersName from the conditions data base.
 
void check ()
 Check that Neural Network can be evaluated, e.g.
 

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
 
std::vector< std::string > m_inputBasf2Names
 list of input names of input variables in the basf2 naming scheme
 
std::map< int, std::string > m_extraInfoNames
 map from PDG code to extraInfo name that stores the output of this network
 

Detailed Description

Class to call PID neural network.

Definition at line 32 of file PIDNeuralNetwork.h.

Constructor & Destructor Documentation

◆ PIDNeuralNetwork() [1/3]

PIDNeuralNetwork ( )
inline

Constructor with default paramenter-set name.

Definition at line 39 of file PIDNeuralNetwork.h.

39 :
41 m_model(nullptr)
42 {
44 check();
45 };
void loadParametersFromDB()
Load neural-network parameters with name m_pidNeuralNetworkParametersName from the conditions data ba...
void check()
Check that Neural Network can be evaluated, e.g.
std::unique_ptr< const fdeep::model > m_model
frugally-deep neural network
std::unique_ptr< DBObjPtr< PIDNeuralNetworkParameters > > m_pidNeuralNetworkParametersDB
db object for the parameter set

◆ PIDNeuralNetwork() [2/3]

PIDNeuralNetwork ( const std::string &  parameterName)
inline

Constructor with given paramenter-set name.

Definition at line 51 of file PIDNeuralNetwork.h.

51 :
54 m_model(nullptr)
55 {
57 check();
58 }
std::string m_pidNeuralNetworkParametersName
name of the parameter set

◆ PIDNeuralNetwork() [3/3]

PIDNeuralNetwork ( PIDNeuralNetwork &&  other)
inline

Move constructor.

Definition at line 63 of file PIDNeuralNetwork.h.

63 :
64 m_pidNeuralNetworkParametersName(std::move(other.m_pidNeuralNetworkParametersName)),
65 m_pidNeuralNetworkParametersDB(std::move(other.m_pidNeuralNetworkParametersDB)),
66 m_model(std::move(other.m_model)),
67 m_inputBasf2Names(std::move(other.m_inputBasf2Names)),
68 m_extraInfoNames(std::move(other.m_extraInfoNames))
69 {
70 }
std::vector< std::string > m_inputBasf2Names
list of input names of input variables in the basf2 naming scheme
std::map< int, std::string > m_extraInfoNames
map from PDG code to extraInfo name that stores the output of this network

Member Function Documentation

◆ check()

void check ( )
inlineprivate

Check that Neural Network can be evaluated, e.g.

that all required inputs exist

Definition at line 143 of file PIDNeuralNetwork.h.

144{
145 for (const auto& name : getInputBasf2Names()) {
146 if (!Variable::Manager::Instance().getVariable(name))
147 B2FATAL("PID neural network needs input '" + name + "', but this input is not available!");
148 }
149
150}
const std::vector< std::string > & getInputBasf2Names() const
Get names of input variables in the basf2 naming scheme, which may be different from the one in the p...
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25

◆ getExtraInfoName()

const std::string & getExtraInfoName ( const int  pdg) const
inline
Parameters
pdgpdg code of hypothesis
Returns
name of the extra info that stores the probability of the given pdg code

Definition at line 117 of file PIDNeuralNetwork.h.

117{return m_extraInfoNames.at(pdg);}

◆ getInputBasf2Names()

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

Get names of input variables in the basf2 naming scheme, which may be different from the one in the payload.

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

Definition at line 90 of file PIDNeuralNetwork.h.

90{ return m_inputBasf2Names; }

◆ getInputSize()

size_t getInputSize ( ) const
inline

Get number of inputs.

Returns
size_t number of inputs

Definition at line 84 of file PIDNeuralNetwork.h.

84{ return (*m_pidNeuralNetworkParametersDB)->getInputSize(); }

◆ 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 97 of file PIDNeuralNetwork.h.

97{return (*m_pidNeuralNetworkParametersDB)->getOutputSpeciesPdg();}

◆ 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 103 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 111 of file PIDNeuralNetwork.h.

111{return (*m_pidNeuralNetworkParametersDB)->hasPdgCode(pdg, throwException);}

◆ loadParametersFromDB()

void loadParametersFromDB ( )
private

Load neural-network parameters with name m_pidNeuralNetworkParametersName from the conditions data base.

Definition at line 17 of file PIDNeuralNetwork.cc.

18{
19 m_pidNeuralNetworkParametersDB = std::make_unique<DBObjPtr<PIDNeuralNetworkParameters>>(m_pidNeuralNetworkParametersName);
20
22 B2FATAL("The dbobject PIDNeuralNetworkParameters, " << m_pidNeuralNetworkParametersName <<
23 ", could not be found! It is necessary for the neural network based PID variables.");
24
25 m_model = std::make_unique<fdeep::model>(fdeep::read_model_from_string((*m_pidNeuralNetworkParametersDB)->getModelDefinition(),
26 false,
27 [](const std::string&) {} // disable logger
28 ));
29
30 // Rename some of the input variables such that they match the basf2 internal names
31 // This is needed for backwards compatibility with older payloads where the input variable names are different from the basf2 internal names
32 std::map<std::string, std::string> inputNameMap; // maps old -> new names
33 for (const Const::EDetector& detector : Const::PIDDetectorSet::set()) {
34 for (const auto& hypeType : Const::chargedStableSet) {
35 const auto oldName = "pidLogLikelihood_Of_" + std::to_string(abs(hypeType.getPDGCode())) + "_From_" + Const::parseDetectors(
36 detector);
37 const auto newName = "pidLogLikelihoodValueExpert(" + std::to_string(abs(hypeType.getPDGCode())) + ',' + Const::parseDetectors(
38 detector) + ")";
39 inputNameMap[oldName] = newName;
40 }
41 }
42 inputNameMap["momentum"] = "p";
43
44 // Build list of input variable names in Basf2 naming scheme
46 for (std::string name : (*m_pidNeuralNetworkParametersDB)->getInputNames()) {
47 const auto itr = inputNameMap.find(name);
48 if (itr != inputNameMap.end()) name = itr->second;
49 m_inputBasf2Names.push_back(name);
50 }
51
52 // Build extraInfo names for this network for all predicted PDG codes
53 for (const auto outputPdgCode : getOutputSpeciesPdg()) {
54 m_extraInfoNames[outputPdgCode] = "pidNeuralNetworkValueExpert(" + std::to_string(outputPdgCode) \
56 }
57}
static DetectorSet set()
Accessor for the set of valid detector IDs.
Definition: Const.h:333
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:618
EDetector
Enum for identifying the detector components (detector and subdetector).
Definition: Const.h:42
static std::string parseDetectors(EDetector det)
Converts Const::EDetector object to string.
Definition: UnitConst.cc:162
size_t getInputSize() const
Get number of inputs.
const std::vector< int > & getOutputSpeciesPdg() const
Get the list of pdg codes of species hypotheses, for which the network predicts the probability in th...

◆ 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 60 of file PIDNeuralNetwork.cc.

61{
62
63 // apply cuts, ie. overwrite certain input values with index `inputSetIndex` with the value `setValue`
64 // if the input with index `inputCutIndex` is in the range (`rangeStart`, `rangeEnd`)
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;
76 }
77 }
78
79 // Normalize inputs
80 // By definition, this has to be done after applying the cuts and before handling missing information
81 for (size_t i = 0; i < input.size(); ++i) {
82 input[i] = (input[i] - (*m_pidNeuralNetworkParametersDB)->getMeanValues()[i]) /
83 (*m_pidNeuralNetworkParametersDB)->getStandardDeviations()[i];
84 }
85
86 // handle missing information
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;
90 }
91
92 // apply neural network
93 const auto inputFdeep = fdeep::tensor(fdeep::tensor_shape(input.size()), input);
94 const auto result = m_model->predict({inputFdeep});
95
96 std::map<int, double> probabilities;
97 for (const auto pdgCode : getOutputSpeciesPdg()) {
98 const int outputIndex = (*m_pidNeuralNetworkParametersDB)->pdg2OutputIndex(pdgCode);
99 probabilities[pdgCode] = result.front().get(fdeep::tensor_pos(outputIndex));
100 }
101 return probabilities;
102}

Member Data Documentation

◆ m_extraInfoNames

std::map<int, std::string> m_extraInfoNames
private

map from PDG code to extraInfo name that stores the output of this network

Definition at line 135 of file PIDNeuralNetwork.h.

◆ m_inputBasf2Names

std::vector<std::string> m_inputBasf2Names
private

list of input names of input variables in the basf2 naming scheme

Definition at line 134 of file PIDNeuralNetwork.h.

◆ m_model

std::unique_ptr<const fdeep::model> m_model
private

frugally-deep neural network

Definition at line 133 of file PIDNeuralNetwork.h.

◆ m_pidNeuralNetworkParametersDB

std::unique_ptr<DBObjPtr<PIDNeuralNetworkParameters> > m_pidNeuralNetworkParametersDB
private

db object for the parameter set

Definition at line 132 of file PIDNeuralNetwork.h.

◆ m_pidNeuralNetworkParametersName

std::string m_pidNeuralNetworkParametersName = "PIDNeuralNetworkParameters"
private

name of the parameter set

Definition at line 131 of file PIDNeuralNetwork.h.


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