ModelConfig.cxx
Go to the documentation of this file.
2 
3 #include <boost/property_tree/json_parser.hpp>
4 
6  const std::string &savedir,
7  const std::vector<InputConfigKeys> &scalarInputKeys,
8  const std::vector<InputConfigKeys> &vectorInputKeys,
9  const std::vector<std::string> &outputKeys
10 ) : savedir(savedir),
11  loaded(false),
12  scalarInputKeys(scalarInputKeys),
13  vectorInputKeys(vectorInputKeys),
14  outputKeys(outputKeys)
15 {
16  scalarInputs.reserve(scalarInputKeys.size());
17  vectorInputs.reserve(vectorInputKeys.size());
18  outputNodes.reserve(outputKeys.size());
19 }
20 
22 {
23  return loaded;
24 }
25 
27 {
28  if (loaded) {
29  return;
30  }
31 
32  const std::string configPath = getConfigPath();
33  try {
34  parseConfig(configPath);
35  }
36  catch (const std::exception& e) {
37  std::throw_with_nested(
38  std::runtime_error("Failed to parse config file: " + configPath)
39  );
40  }
41 
42  loaded = true;
43 }
44 
46  pt::ptree &tree, const InputConfigKeys &keys
47 )
48 {
49  std::string nodeName = tree.get<std::string>(keys.nodeName);
50  std::vector<std::string> varNames;
51 
52  for (auto &stree : tree.get_child(keys.varNames)) {
53  varNames.push_back(stree.second.get_value<std::string>());
54  }
55 
56  return InputConfig{ std::move(nodeName), std::move(varNames) };
57 }
58 
60 {
61  pt::ptree tree;
62  pt::read_json(fname, tree);
63 
64  for (const auto &inputKeys : scalarInputKeys) {
65  scalarInputs.emplace_back(parseInput(tree, inputKeys));
66  }
67 
68  for (const auto &inputKeys : vectorInputKeys) {
69  vectorInputs.emplace_back(parseInput(tree, inputKeys));
70  }
71 
72  for (const auto &outputKey : outputKeys) {
73  outputNodes.emplace_back(tree.get<std::string>(outputKey));
74  }
75 }
76 
78 {
79  return savedir;
80 }
81 
83 {
84  return savedir + "/" + CONFIG_NAME;
85 }
86 
88 {
89  return savedir + "/" + MODEL_NAME;
90 }
91 
std::string getConfigPath() const
Definition: ModelConfig.cxx:82
keys
Reco plots.
Definition: caf_analysis.py:46
std::vector< std::string > outputKeys
Definition: ModelConfig.h:40
std::vector< InputConfigKeys > scalarInputKeys
Definition: ModelConfig.h:38
std::string nodeName
Definition: ModelConfig.h:16
std::vector< std::string > outputNodes
Definition: ModelConfig.h:59
std::vector< InputConfigKeys > vectorInputKeys
Definition: ModelConfig.h:39
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
void parseConfig(const std::string &fname)
Definition: ModelConfig.cxx:59
std::string getModelPath() const
Definition: ModelConfig.cxx:87
std::string varNames
Definition: ModelConfig.h:17
std::string savedir
Definition: ModelConfig.h:29
std::string getSavedir() const
Definition: ModelConfig.cxx:77
std::vector< InputConfig > vectorInputs
Definition: ModelConfig.h:58
std::vector< InputConfig > scalarInputs
Definition: ModelConfig.h:57
static InputConfig parseInput(pt::ptree &tree, const InputConfigKeys &keys)
Definition: ModelConfig.cxx:45
const std::string CONFIG_NAME
Definition: ModelConfig.h:10
Float_t e
Definition: plot.C:35
bool isLoaded() const
Definition: ModelConfig.cxx:21
ModelConfig(const std::string &savedir, const std::vector< InputConfigKeys > &scalarInputKeys, const std::vector< InputConfigKeys > &vectorInputKeys, const std::vector< std::string > &outputKeys)
Definition: ModelConfig.cxx:5
const std::string MODEL_NAME
Definition: ModelConfig.h:11
enum BeamMode string