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/Exceptions.hh"
7#include "lwtnn/LightweightGraph.hh"
8#include "lwtnn/LightweightNeuralNetwork.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 }
28 catch (lwt::LightweightNNException& e) {
29 input.close();
30 throw IOError("Error loading LWTNN JSON config");
31 }
32 }
33
34
41 lwt::GraphConfig readLWTNNGraphConfig(const string& jsonpath) {
42 ifstream input;
43 try {
44 // Note: a failed read here may fail quietly, and cause the filestream to
45 // go bad, making it look like the hepmc event-read has failed.
46 input = std::ifstream(jsonpath);
47 return lwt::parse_json_graph(input);
48 }
49 catch (lwt::LightweightNNException& e) {
50 input.close();
51 throw IOError("Error loading LWTNN JSON config");
52 }
53 }
54
59 std::unique_ptr<lwt::LightweightNeuralNetwork> mkLWTNN(const lwt::JSONConfig& jsonconfig) {
60 try {
61 return std::make_unique<lwt::LightweightNeuralNetwork>(jsonconfig.inputs, jsonconfig.layers,
62 jsonconfig.outputs);
63 }
64 catch (lwt::LightweightNNException& e) {
65 throw IOError("Error initialising from LWTNN JSON config");
66 }
67 }
68
74 std::unique_ptr<lwt::LightweightGraph> mkGraphLWTNN(const lwt::GraphConfig& graphconfig) {
75 try {
76 return std::make_unique<lwt::LightweightGraph>(graphconfig);
77 }
78 catch (lwt::LightweightNNException& e) {
79 throw IOError("Error initialising from LWTNN JSON config");
80 }
81 }
82
83
88 std::unique_ptr<lwt::LightweightNeuralNetwork> mkLWTNN(const string& jsonpath) {
89 lwt::JSONConfig config = readLWTNNConfig(jsonpath);
90 return mkLWTNN(config);
91 }
92
99 std::unique_ptr<lwt::LightweightGraph> mkGraphLWTNN(const string& jsonpath) {
100 lwt::GraphConfig config = readLWTNNGraphConfig(jsonpath);
101 return mkGraphLWTNN(config);
102 }
103
104}
105
106#endif
Definition LHCbCommon.hh:9
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:41
std::unique_ptr< lwt::LightweightNeuralNetwork > mkLWTNN(const lwt::JSONConfig &jsonconfig)
Definition RivetLWTNN.hh:59
std::unique_ptr< lwt::LightweightGraph > mkGraphLWTNN(const lwt::GraphConfig &graphconfig)
Make a LWT Graph from the JSON config object.
Definition RivetLWTNN.hh:74
Error for I/O failures.
Definition Exceptions.hh:96