ATLAS Offline Software
VNetworkLWTNN.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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),
17  m_printable_name (copy_from.m_printable_name)
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 
44 void VNetworkLWTNN::print(std::ostream &strm) const {
45  strm << m_printable_name;
46 };
47 
50 };
51 
52 void 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 = 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 
81 void 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 };
VNetworkLWTNN::fillJson
void fillJson(std::string const &tree_name=m_defaultTreeName)
Fill out m_json from a file provided to the constructor.
Definition: VNetworkLWTNN.cxx:52
tree
TChain * tree
Definition: tile_monitor.h:30
VNetworkLWTNN::m_json
std::string m_json
String containing json input file.
Definition: VNetworkLWTNN.h:84
VNetworkBase::isFile
bool isFile() const
Check if the argument inputFile is the path of a file on disk.
Definition: VNetworkBase.cxx:117
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
VNetworkLWTNN::writeStringToTTree
void writeStringToTTree(TTree &tree, std::string json_string)
Get json string from TTree.
Definition: VNetworkLWTNN.cxx:81
VNetworkLWTNN::deleteAllButNet
void deleteAllButNet() override
Get rid of any memory objects that arn't needed to run the net.
Definition: VNetworkLWTNN.cxx:87
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
VNetworkLWTNN::readStringFromTTree
std::string readStringFromTTree(TTree &tree)
Get json string from TTree.
Definition: VNetworkLWTNN.cxx:73
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
VNetworkLWTNN.h
VNetworkLWTNN::~VNetworkLWTNN
virtual ~VNetworkLWTNN()
Definition: VNetworkLWTNN.cxx:26
checkTriggerxAOD.tfile
tfile
Definition: checkTriggerxAOD.py:277
VNetworkBase
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
Definition: VNetworkBase.h:38
VNetworkLWTNN::m_printable_name
std::string m_printable_name
Stores a printable identifyer for the net.
Definition: VNetworkLWTNN.h:144
VNetworkLWTNN
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
Definition: VNetworkLWTNN.h:31
VNetworkLWTNN::writeNetToTTree
void writeNetToTTree(TTree &tree) override
Save the network to a TTree.
Definition: VNetworkLWTNN.cxx:48
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
VNetworkLWTNN::VNetworkLWTNN
VNetworkLWTNN(const VNetworkLWTNN &copy_from)
VNetworkLWTNN copy constructor.
Definition: VNetworkLWTNN.cxx:14
VNetworkBase::isRootFile
bool isRootFile(std::string const &filename="") const
Check if a string is possibly a root file path.
Definition: VNetworkBase.cxx:101
VNetworkLWTNN::setupPersistedVariables
void setupPersistedVariables() override
Perform actions that prep data to create the net.
Definition: VNetworkLWTNN.cxx:30
VNetworkBase::m_inputFile
std::string m_inputFile
Path to the file describing the network, including filename.
Definition: VNetworkBase.h:245
VNetworkLWTNN::print
virtual void print(std::ostream &strm) const override
Write a short description of this net to the string stream.
Definition: VNetworkLWTNN.cxx:44