Belle II Software development
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
16using namespace Belle2;
17
18int 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 for (unsigned i = 0; i < nets["expert_" + std::to_string(expert)]["shapes"].size(); i = i + 2) {
44 int numnode = 0;
45 for (auto node : nets["expert_" + std::to_string(expert)]["weights"]["model.net." + std::to_string(i) + ".weight"]) {
46 for (float w : node) {
47 weights.push_back(w);
48 }
49 weights.push_back(nets["expert_" + std::to_string(expert)]["weights"]["model.net." + std::to_string(i) + ".bias"][numnode]);
50 ++numnode;
51 }
52 }
53 std::cout << " writing " << weights.size() << " weights for expert " << expert << std::endl;
54 m_nnt[expert].setWeights(weights);
55 }
56 m_nnt.save(outputfile, "MLPs");
57
58
59 return 0;
60}
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.