Rivet API documentation

Rivet 4.1.3
RivetLWTNN.hh
1// -*- C++ -*-
2#ifndef RIVET_RivetLWTNN_HH
3#define RIVET_RivetLWTNN_HH
4
5#include "Rivet/Tools/RivetPaths.hh"
6#include "lwtnn/LightweightNeuralNetwork.hh"
7#include "lwtnn/LightweightGraph.hh"
8#include "lwtnn/Exceptions.hh"
9#include "lwtnn/parse_json.hh"
10#include <fstream>
11
12namespace Rivet {
13 using namespace std;
14
15
20 lwt::JSONConfig readLWTNNConfig(const string& jsonpath) {
21 ifstream input;
22 try {
23 // Note: a failed read here may fail quietly, and cause the filestream to
24 // go bad, making it look like the hepmc event-read has failed.
25 input = std::ifstream(jsonpath);
26 return lwt::parse_json(input);
27 } catch (lwt::LightweightNNException &e) {
28 input.close();
29 throw IOError("Error loading LWTNN JSON config");
30 }
31 }
32
33
40 lwt::GraphConfig readLWTNNGraphConfig(const string& jsonpath) {
41 ifstream input;
42 try {
43 // Note: a failed read here may fail quietly, and cause the filestream to
44 // go bad, making it look like the hepmc event-read has failed.
45 input = std::ifstream(jsonpath);
46 return lwt::parse_json_graph(input);
47 } catch (lwt::LightweightNNException &e) {
48 input.close();
49 throw IOError("Error loading LWTNN JSON config");
50 }
51 }
52
57 std::unique_ptr<lwt::LightweightNeuralNetwork> mkLWTNN(const lwt::JSONConfig& jsonconfig) {
58 try {
59 return std::make_unique<lwt::LightweightNeuralNetwork>(jsonconfig.inputs, jsonconfig.layers, jsonconfig.outputs);
60 } catch (lwt::LightweightNNException &e) {
61 throw IOError("Error initialising from LWTNN JSON config");
62 }
63 }
64
70 std::unique_ptr<lwt::LightweightGraph> mkGraphLWTNN(const lwt::GraphConfig& graphconfig) {
71 try {
72 return std::make_unique<lwt::LightweightGraph>(graphconfig);
73 } catch (lwt::LightweightNNException &e) {
74 throw IOError("Error initialising from LWTNN JSON config");
75 }
76 }
77
78
83 std::unique_ptr<lwt::LightweightNeuralNetwork> mkLWTNN(const string& jsonpath) {
84 lwt::JSONConfig config = readLWTNNConfig(jsonpath);
85 return mkLWTNN(config);
86 }
87
94 std::unique_ptr<lwt::LightweightGraph> mkGraphLWTNN(const string& jsonpath) {
95 lwt::GraphConfig config = readLWTNNGraphConfig(jsonpath);
96 return mkGraphLWTNN(config);
97 }
98
99}
100
101#endif
Definition MC_CENT_PPB_Projections.hh:10
lwt::JSONConfig readLWTNNConfig(const string &jsonpath)
Definition RivetLWTNN.hh:20
lwt::GraphConfig readLWTNNGraphConfig(const string &jsonpath)
Read a LWT Graph config from the JSON path.
Definition RivetLWTNN.hh:40
std::unique_ptr< lwt::LightweightNeuralNetwork > mkLWTNN(const lwt::JSONConfig &jsonconfig)
Definition RivetLWTNN.hh:57
std::unique_ptr< lwt::LightweightGraph > mkGraphLWTNN(const lwt::GraphConfig &graphconfig)
Make a LWT Graph from the JSON config object.
Definition RivetLWTNN.hh:70
Error for I/O failures.
Definition Exceptions.hh:85