Belle II Software  release-08-00-04
trgcdc_neurorootconverter.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <trg/cdc/NeuroTrigger.h>
10 #include <trg/cdc/NeuroTriggerParameters.h>
11 #include <trg/cdc/dataobjects/CDCTriggerMLP.h>
12 #include <nlohmann/json.hpp>
13 #include <fstream>
14 #include <iostream>
15 
16 using namespace Belle2;
17 
18 int main(int argc, const char* argv[])
19 {
20 
21  // get arguments
22  if (argc < 4) {
23  std::cout << "Program needs at 3 arguments:" << std::endl
24  << " 1: json weights" << std::endl
25  << " 2: configuration file name" << std::endl
26  << " 3: output filename" << std::endl;
27  return -1;
28  }
29  std::string jsonweights = argv[1];
30  std::string configfile = argv[2];
31  std::string outputfile = argv[3];
32 
33  NeuroTriggerParameters p(configfile);
34  NeuroTrigger m_nnt;
35  m_nnt.initialize(p);
36  std::ifstream netfile(jsonweights, std::ifstream::binary);
37  nlohmann::json nets;
38  netfile >> nets;
39 
40  for (unsigned expert = 0; expert < m_nnt.nSectors(); expert++) {
41 
42  std::vector<float> weights;
43  int numnode = 0;
44  for (auto node : nets["expert_" + std::to_string(expert)]["weights"]["model.net.0.weight"]) {
45  for (float w : node) {
46  weights.push_back(w);
47  }
48  weights.push_back(nets["expert_" + std::to_string(expert)]["weights"]["model.net.0.bias"][numnode]);
49  ++numnode;
50  }
51  numnode = 0;
52  for (auto node : nets["expert_" + std::to_string(expert)]["weights"]["model.net.2.weight"]) {
53  for (float w : node) {
54  weights.push_back(w);
55  }
56  weights.push_back(nets["expert_" + std::to_string(expert)]["weights"]["model.net.2.bias"][numnode]);
57  ++numnode;
58  }
59  std::cout << " writing " << weights.size() << " weights for expert " << expert << std::endl;
60  m_nnt[expert].setWeights(weights);
61  }
62  m_nnt.save(outputfile, "MLPs");
63 
64 
65  return 0;
66 }
Class to represent the CDC Neurotrigger.
Definition: NeuroTrigger.h:40
void initialize(const Parameters &p)
Set parameters and get some network independent parameters.
void save(const std::string &filename, const std::string &arrayname="MLPs")
Save MLPs to file.
unsigned nSectors() const
return number of neural networks
Definition: NeuroTrigger.h:164
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91