ATLAS Offline Software
Loading...
Searching...
No Matches
VNetworkLWTNN.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <fstream>
7#include <sstream>
8#include <stdexcept>
9
10// For reading and writing to root
11#include "TFile.h"
12#include "TTree.h"
13
15 : VNetworkBase(copy_from),
16 m_json (copy_from.m_json),
18{
19 if (m_json.length() == 0) {
20 throw std::invalid_argument(
21 "Trying to copy a VNetworkLWTNN with length 0 m_json, probably "
22 "deleteAllButNet was called on the object being coppied from.");
23 };
24};
25
27
28// This setup is going to do it's best to
29// fill in m_json.
31 if (this->isFile(m_inputFile)) {
32 ATH_MSG_DEBUG("Making an LWTNN network using a file on disk, "
33 << m_inputFile);
35 fillJson();
36 } else {
37 ATH_MSG_DEBUG("Making an LWTNN network using a json in memory, length "
38 << m_inputFile.length());
39 m_printable_name = "JSON from memory";
41 };
42};
43
44void VNetworkLWTNN::print(std::ostream &strm) const {
46};
47
51
52void VNetworkLWTNN::fillJson(std::string const &tree_name) {
53 ATH_MSG_VERBOSE("Trying to fill the m_json variable");
54 if (this->isRootFile()) {
55 ATH_MSG_VERBOSE("Treating input file as a root file");
56 TFile tfile(this->m_inputFile.c_str(), "READ");
57 TTree *tree = (TTree *)tfile.Get(tree_name.c_str());
58 std::string found = this->readStringFromTTree(*tree);
59 ATH_MSG_DEBUG("Read json from root file, length " << found.length());
60 m_json = std::move(found);
61 } else {
62 ATH_MSG_VERBOSE("Treating input file as a text json file");
63 // The input file is read into a stringstream
64 std::ifstream input(m_inputFile);
65 std::ostringstream sstr;
66 sstr << input.rdbuf();
67 m_json = sstr.str();
68 input.close();
69 ATH_MSG_DEBUG("Read json from text file");
70 }
71}
72
74 std::string found = std::string();
75 std::string *to_found = &found;
76 tree.SetBranchAddress("lwtnn_json", &to_found);
77 tree.GetEntry(0);
78 return found;
79};
80
81void VNetworkLWTNN::writeStringToTTree(TTree &tree, std::string json_string) {
82 tree.Branch("lwtnn_json", &json_string);
83 tree.Fill();
84 tree.Write();
85};
86
88 ATH_MSG_DEBUG("Replacing m_inputFile with unknown");
89 m_inputFile.assign("unknown");
90 m_inputFile.shrink_to_fit();
91 ATH_MSG_DEBUG("Emptying the m_json string");
92 m_json.clear();
93 m_json.shrink_to_fit();
94 ATH_MSG_VERBOSE("m_json now has capacity "
95 << m_json.capacity() << ". m_inputFile now has capacity "
96 << m_inputFile.capacity()
97 << ". m_printable_name now has capacity "
98 << m_printable_name.capacity());
99};
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
std::string m_inputFile
Path to the file describing the network, including filename.
bool isRootFile(std::string const &filename="") const
Check if a string is possibly a root file path.
bool isFile() const
Check if the argument inputFile is the path of a file on disk.
void deleteAllButNet() override
Get rid of any memory objects that arn't needed to run the net.
virtual void print(std::ostream &strm) const override
Write a short description of this net to the string stream.
virtual ~VNetworkLWTNN()
VNetworkBase()
VNetworkBase default constructor.
void writeNetToTTree(TTree &tree) override
Save the network to a TTree.
void fillJson(std::string const &tree_name=m_defaultTreeName)
Fill out m_json from a file provided to the constructor.
void writeStringToTTree(TTree &tree, std::string json_string)
Get json string from TTree.
std::string m_printable_name
Stores a printable identifyer for the net.
std::string m_json
String containing json input file.
VNetworkLWTNN(const VNetworkLWTNN &copy_from)
VNetworkLWTNN copy constructor.
std::string readStringFromTTree(TTree &tree)
Get json string from TTree.
void setupPersistedVariables() override
Perform actions that prep data to create the net.
TChain * tree