Belle II Software development
CDCTriggerHoughMLP Class Reference
Inheritance diagram for CDCTriggerHoughMLP:
CDCTrigger3DHMLP CDCTriggerDVTMLP

Public Member Functions

 CDCTriggerHoughMLP (const NeuroParametersHough &neuroParametersHough)
 
void setFloatWeights (const std::vector< float > &weights)
 
size_t getNumberOfLayers () const
 
size_t getNumberOfNodes (const size_t layerIdx) const
 
size_t getNumberOfWeights () const
 
const std::vector< float > & getFloatWeights () const
 
const NeuroParametersHoughgetNeuroParameters () const
 
void saveMLPToFile (const std::string &fileName, const std::string &objName) const
 

Static Public Member Functions

template<typename T>
static T loadMLPFromFile (const std::string &fileName, const std::string &key)
 
static NeuroParametersHough loadConfigFromFile (const std::string &fileName)
 

Private Member Functions

 ClassDef (CDCTriggerHoughMLP, 1)
 

Static Private Member Functions

template<typename T>
static std::vector< T > readArray (const std::string &rawString)
 

Private Attributes

NeuroParametersHough m_neuroParametersHough
 
std::vector< size_t > m_nodes
 
std::vector< float > m_floatWeights
 

Detailed Description

Definition at line 41 of file CDCTriggerHoughMLP.h.

Constructor & Destructor Documentation

◆ CDCTriggerHoughMLP()

CDCTriggerHoughMLP ( const NeuroParametersHough & neuroParametersHough)

Definition at line 20 of file CDCTriggerHoughMLP.cc.

20 :
21 m_neuroParametersHough(neuroParametersHough)
22{
23 std::vector<size_t> nHidden = m_neuroParametersHough.nHidden;
24 m_nodes = {static_cast<size_t>(m_neuroParametersHough.nInput)};
25 for (size_t hiddenLayerIdx = 0; hiddenLayerIdx < nHidden.size(); ++hiddenLayerIdx) {
26 m_nodes.push_back(nHidden[hiddenLayerIdx]);
27 }
28 m_nodes.push_back(m_neuroParametersHough.nOutput);
29 m_floatWeights.assign(getNumberOfWeights(), 0.0f);
30}

Member Function Documentation

◆ getFloatWeights()

const std::vector< float > & getFloatWeights ( ) const
inline

Definition at line 58 of file CDCTriggerHoughMLP.h.

58{ return m_floatWeights; }

◆ getNeuroParameters()

const NeuroParametersHough & getNeuroParameters ( ) const
inline

Definition at line 59 of file CDCTriggerHoughMLP.h.

59{ return m_neuroParametersHough; }

◆ getNumberOfLayers()

size_t getNumberOfLayers ( ) const
inline

Definition at line 55 of file CDCTriggerHoughMLP.h.

55{ return m_nodes.size(); }

◆ getNumberOfNodes()

size_t getNumberOfNodes ( const size_t layerIdx) const
inline

Definition at line 56 of file CDCTriggerHoughMLP.h.

56{ return m_nodes[layerIdx]; }

◆ getNumberOfWeights()

size_t getNumberOfWeights ( ) const

Definition at line 33 of file CDCTriggerHoughMLP.cc.

34{
35 size_t nWeights = 0;
36 size_t nLayers = getNumberOfLayers();
37 for (size_t i = 1; i < nLayers; ++i) {
38 // +1 for bias node
39 nWeights += (m_nodes[i - 1] + 1) * m_nodes[i];
40 }
41 return nWeights;
42}

◆ loadConfigFromFile()

NeuroParametersHough loadConfigFromFile ( const std::string & fileName)
static

Definition at line 54 of file CDCTriggerHoughMLP.cc.

55{
56 NeuroParametersHough neuroParametersHough;
57 std::ifstream configFile(fileName);
58 if (!configFile.is_open()) {
59 B2ERROR("Could not open configuration file: " + fileName);
60 exit(EXIT_FAILURE);
61 }
62 std::string completeLine;
63 while (std::getline(configFile, completeLine)) {
64 std::size_t hashtag = completeLine.find('#'); // Remove comments
65 std::string line = completeLine.substr(0, hashtag);
66 std::string configParameter;
67 std::string parameterValue;
68 if (line.length() < 3 || line.find('=') == std::string::npos) {
69 continue;
70 }
71 line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); // Remove whitspaces
72 size_t equalPosition = line.find('=');
73 configParameter = line.substr(0, equalPosition);
74 parameterValue = line.substr((equalPosition + 1), line.length() - equalPosition - 1);
75 if (configParameter == "nInput") {
76 neuroParametersHough.nInput = std::stoull(parameterValue);
77 } else if (configParameter == "nOutput") {
78 neuroParametersHough.nOutput = std::stoull(parameterValue);
79 } else if (configParameter == "nHidden") {
80 neuroParametersHough.nHidden = readArray<size_t>(parameterValue);
81 } else if (configParameter == "outputScale") {
82 neuroParametersHough.outputScale = readArray<float>(parameterValue);
83 } else {
84 B2WARNING("Unknown config parameter: " + configParameter);
85 }
86 }
87 return neuroParametersHough;
88}

◆ loadMLPFromFile()

template<typename T>
static T loadMLPFromFile ( const std::string & fileName,
const std::string & key )
inlinestatic

Definition at line 65 of file CDCTriggerHoughMLP.h.

66 {
67 TFile datafile(fileName.c_str(), "READ");
68 if (!datafile.IsOpen()) {
69 B2ERROR("Could not open file " << fileName);
70 throw std::runtime_error("Could not open file " + fileName);
71 }
72 T* network = dynamic_cast<T*>(datafile.Get(key.c_str()));
73 if (!network) {
74 throw std::runtime_error("File " + fileName + " does not contain key " + key + " of requested type");
75 }
76 T result = *network;
77 datafile.Close();
78 return result;
79 }

◆ readArray()

template<typename T>
std::vector< T > readArray ( const std::string & rawString)
staticprivate

Definition at line 92 of file CDCTriggerHoughMLP.cc.

93{
94 std::vector<T> configVector;
95 std::string strippedString = rawString.substr(1, rawString.size() - 2); // Strip brackets
96 std::stringstream strippedStream(strippedString);
97 std::string entry;
98 while (std::getline(strippedStream, entry, ',')) {
99 configVector.push_back(static_cast<T>(std::stod(entry)));
100 }
101 return configVector;
102}

◆ saveMLPToFile()

void saveMLPToFile ( const std::string & fileName,
const std::string & objName ) const

Definition at line 45 of file CDCTriggerHoughMLP.cc.

46{
47 B2INFO(std::string("Saving network to file ") + fileName + ", object " + objName);
48 TFile datafile(fileName.c_str(), "UPDATE");
49 this->Write(objName.c_str(), TObject::kSingleKey | TObject::kOverwrite);
50 datafile.Close();
51}

◆ setFloatWeights()

void setFloatWeights ( const std::vector< float > & weights)
inline

Definition at line 52 of file CDCTriggerHoughMLP.h.

52{ m_floatWeights = weights; }

Member Data Documentation

◆ m_floatWeights

std::vector<float> m_floatWeights
private

Definition at line 94 of file CDCTriggerHoughMLP.h.

◆ m_neuroParametersHough

NeuroParametersHough m_neuroParametersHough
private

Definition at line 90 of file CDCTriggerHoughMLP.h.

◆ m_nodes

std::vector<size_t> m_nodes
private

Definition at line 92 of file CDCTriggerHoughMLP.h.


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