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

Class to represent the GRL Neuro. More...

#include <GRLNeuro.h>

Collaboration diagram for GRLNeuro:

Classes

struct  Parameters
 Struct to keep neurotrigger parameters. More...
 

Public Member Functions

 GRLNeuro ()
 Default constructor.
 
virtual ~GRLNeuro ()
 Default destructor.
 
void initialize (const Parameters &p)
 Set parameters and get some network independent parameters.
 
GRLMLPoperator[] (unsigned index)
 Set parameters and get some network independent parameters. More...
 
const GRLMLPoperator[] (unsigned index) const
 return const reference to a neural network
 
unsigned nSectors () const
 return number of neural networks
 
float relu (float x)
 ReLu activation function.
 
float float_to_fixed (float num, int m, int n)
 change the percision of number, m = number of integer bits, n = number of decimal
 
float mysigmiod (float num)
 discrete sigmoid activation function (1024 bins)
 
void save (const std::string &filename, const std::string &arrayname="MLPs")
 Save MLPs to file. More...
 
bool load (unsigned isector, const std::string &wfilename, const std::string &bfilename)
 Load MLPs from file. More...
 
float runMLP (unsigned isector, const std::vector< float > &input)
 Run an expert MLP. More...
 

Private Attributes

std::vector< GRLMLPm_MLPs = {}
 List of networks.
 

Detailed Description

Class to represent the GRL Neuro.

The Neurotrigger consists of one or several Multi Layer Perceptrons. The input values are calculated from ECLTRG cluster and a 2D track estimate. The output is a scaled estimate of the judgement.

See also
GRLNeuro Modules:
GRLTrainerModule for preparing training data and training,
GRLNeuro for loading trained networks and using them.

Definition at line 35 of file GRLNeuro.h.

Member Function Documentation

◆ load()

bool load ( unsigned  isector,
const std::string &  wfilename,
const std::string &  bfilename 
)

Load MLPs from file.

Parameters
filenamename of the TFile to read from
arraynamename of the TObjArray holding the MLPs in the file
Returns
true if the MLPs were loaded correctly

Definition at line 154 of file GRLNeuro.cc.

155 {
156  if (weightfilename.size() < 1) {
157  B2ERROR("Could not load Neurotrigger weights from database!");
158  return false;
159  } else if (biasfilename.size() < 1) {
160  B2ERROR("Could not load Neurotrigger bias from database!");
161  return false;
162  } else {
163  std::ifstream wfile(weightfilename);
164  if (!wfile.is_open()) {
165  B2WARNING("Could not open file " << weightfilename);
166  return false;
167  } else {
168  std::ifstream bfile(biasfilename);
169  if (!bfile.is_open()) {
170  B2WARNING("Could not open file " << biasfilename);
171  return false;
172  } else {
173  GRLMLP& expert = m_MLPs[isector];
174  std::vector<float> warray;
175  std::vector<float> barray;
176  warray.clear();
177  barray.clear();
178 
179  float element;
180  while (wfile >> element) {
181  warray.push_back(element);
182  }
183  while (bfile >> element) {
184  barray.push_back(element);
185  }
186 
187  if (warray.size() != expert.nWeightsCal()) {
188  B2ERROR("Number of weights is not equal to registered architecture!");
189  return false;
190  } else expert.setWeights(warray);
191  if (barray.size() != expert.nBiasCal()) {
192  B2ERROR("Number of bias is not equal to registered architecture!");
193  return false;
194  }
195  //change the precision based in FPGA (hls4ml)
196  for (int it = 0; it < warray.size(); it++) {
197  if (it < 380) warray[it] = float_to_fixed(warray[it], 2, 13);
198  else if (it < 780) warray[it] = float_to_fixed(warray[it], 1, 13);
199  else warray[it] = float_to_fixed(warray[it], 2, 11);
200  }
201 
202  for (int it = 0; it < barray.size(); it++) {
203  if (it < 20) barray[it] = float_to_fixed(barray[it], 4, 2);
204  else if (it < 40) barray[it] = float_to_fixed(barray[it], 4, 3);
205  else barray[it] = float_to_fixed(barray[it], 0, 2);
206  }
207 
208  expert.setWeights(warray);
209  expert.setBias(barray);
210  return true;
211  }
212  }
213  }
214 }
Class to keep all parameters of an expert MLP for the neuro trigger.
Definition: GRLMLP.h:21
std::vector< GRLMLP > m_MLPs
List of networks.
Definition: GRLNeuro.h:116
float float_to_fixed(float num, int m, int n)
change the percision of number, m = number of integer bits, n = number of decimal
Definition: GRLNeuro.cc:216

◆ operator[]()

GRLMLP& operator[] ( unsigned  index)
inline

Set parameters and get some network independent parameters.

return reference to a neural network

Definition at line 82 of file GRLNeuro.h.

82 { return m_MLPs[index]; }

◆ runMLP()

float runMLP ( unsigned  isector,
const std::vector< float > &  input 
)

Run an expert MLP.

Parameters
isectorindex of the MLP
inputvector of input values
Returns
output values (classifier)

Definition at line 87 of file GRLNeuro.cc.

◆ save()

void save ( const std::string &  filename,
const std::string &  arrayname = "MLPs" 
)

Save MLPs to file.

Parameters
filenamename of the TFile to write to
arraynamename of the TObjArray holding the MLPs in the file

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