Belle II Software development
trgcdc_3dhroottodat.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 <iostream>
10#include <fstream>
11#include <vector>
12#include <cstdint>
13#include <cstdlib>
14
15#include <TFile.h>
16#include <TObjArray.h>
17
18#include "trg/cdc/dataobjects/CDCTriggerHoughMLP.h"
19#include "trg/cdc/NeuroTrigger3DH.h"
20
21using namespace Belle2;
22
23// Converts a .root network to a plaintext integerized .dat file for FPGA implementation
24int main(int argc, const char* argv[])
25{
26 if (argc != 3) {
27 std::cout << "Program requires the following 2 arguments:\n"
28 << " 1: mlp rootfile\n"
29 << " 2: output filename\n";
30 return -1;
31 }
32 const std::string fileNameMLP = argv[1];
33 const std::string fileNameDAT = argv[2];
34
35 const CDCTrigger3DHMLP& mlp = CDCTrigger3DHMLP::loadMLPFromFile<CDCTrigger3DHMLP>(fileNameMLP, "MLP");
36 const NeuroParametersHough neuroParameters3DH = mlp.getNeuroParameters();
37
38 NeuroTrigger3DH neuroTrigger3DH;
39 neuroTrigger3DH.initialize();
40 neuroTrigger3DH.setMLP(mlp);
41 neuroTrigger3DH.createIntWeights();
42
43 std::ofstream weightStream(fileNameDAT);
44
45 // Write architecture to file
46 weightStream << neuroParameters3DH.nInput << " ";
47 for (const size_t hiddenNodes : neuroParameters3DH.nHidden) {
48 weightStream << hiddenNodes << " ";
49 }
50 weightStream << neuroParameters3DH.nOutput << "\n";
51 // Write int weights to file
52 const std::vector<int32_t>& weights = neuroTrigger3DH.getIntWeights();
53 int32_t minIntWeight = 0, maxIntWeight = 0;
54 for (int32_t weight : weights) {
55 if (weight < minIntWeight) minIntWeight = weight;
56 if (weight > maxIntWeight) maxIntWeight = weight;
57 weightStream << weight << " ";
58 }
59 weightStream << "\n";
60
61 int fractionalWeightBits = neuroTrigger3DH.getFractionalWeightBits();
62 int weightBits = neuroTrigger3DH.getWeightBits();
63 std::cout << "Writing " << weights.size() << " weights.\n"
64 << "With a precision of " << weightBits << " bits, corresponding to " << fractionalWeightBits << " fractional bits.\n"
65 << "Max weight: " << maxIntWeight << ", Min weight: " << minIntWeight << "\n";
66 weightStream.close();
67
68 return 0;
69}
Abstract base class for different kinds of events.