ATLAS Offline Software
JsonFileLoader.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #define BOOST_BIND_GLOBAL_PLACEHOLDERS // Needed to silence Boost pragma message
8 #include <boost/property_tree/ptree.hpp>
9 #include <boost/property_tree/json_parser.hpp>
10 
11 #include <sys/stat.h>
12 #include <filesystem>
13 #include <cstdlib>
14 #include <cstring>
15 
17 
18 
20  TrigConfMessaging( "JsonFileLoader")
21 {}
22 
23 /*
24  File will be search first absolute (when starting with "/" or
25  relative to the current path.
26 */
27 std::string
28 TrigConf::JsonFileLoader::findFile(const std::string & filename) const {
29 
30  // check for the file as specified
32  return filename;
33  }
34 
35  // check if absolute location has been specified
36  if (!filename.empty() && filename[0] == '/') {
37  TRG_MSG_WARNING("Can not find file with absolute location " << filename);
38  return {};
39  }
40 
41  return {};
42 }
43 
44 bool
47  const std::string & pathToChild ) const
48 {
49 
50  std::string file = findFile(filename); // resolved file name
51  if ( file.empty() ) {
52  return false;
53  }
54  TRG_MSG_INFO("Reading information from " << file);
55 
56  // Load the json file
57  try {
58  boost::property_tree::read_json(file, data);
59  }
60  catch (const boost::property_tree::json_parser_error& e) {
61  TRG_MSG_WARNING("Could either not locate or parse the file " << file);
62  return false;
63  }
64 
65  if( ! pathToChild.empty() ) {
66  // the user has specified that s/he wants to access a certain part of the file
67  // the pathToChild is specified like this "a.b.c"
68  boost::optional<ptree&> subtree = data.get_child_optional(pathToChild);
69  if(subtree) {
70  data = *subtree;
71  } else {
72  TRG_MSG_WARNING("Did not find element '" << pathToChild << "' in the file " << file);
73  data.clear();
74  }
75  }
76 
77  return true;
78 }
79 
80 
81 bool
84  const std::string & pathToChild ) const
85 {
87 
88  if( ! loadFile( filename, pt, pathToChild) )
89  return false;
90 
91  data.setData(std::move(pt));
92 
93  return true;
94 }
95 
96 
97 
98 bool
100  const DataStructure & data ) const
101 {
102  if ( filename == "" ) {
103  TRG_MSG_ERROR("Could not save to file, as specified filename is empty");
104  return false;
105  }
106  boost::property_tree::write_json(filename, data.data());
107  TRG_MSG_INFO("Saved file " << filename);
108 
109  return true;
110 }
111 
112 
113 std::string
115 {
116  std::string ft = "UNKNOWN";
117 
119  if ( loadFile ( filename, data ) ) {
120  ft = data.getAttribute("filetype", /*ignoreIfMissing*/ true, ft);
121  }
122 
123  return ft;
124 }
125 
126 
127 bool
129  std::string & level ) const
130 {
131  level = "UNKNOWN";
132 
134 
135  bool succeeded = this -> loadFile ( filename, data );
136 
137  if ( succeeded ) {
138  if (data.hasChild("chains")) {
139  level = "HLT";
140  } else if (data.hasChild("items")) {
141  level = "L1";
142  }
143  }
144 
145  return succeeded;
146 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
TRG_MSG_ERROR
#define TRG_MSG_ERROR(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:29
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
PlotCalibFromCool.ft
ft
Definition: PlotCalibFromCool.py:329
TrigConf::JsonFileLoader::findFile
std::string findFile(const std::string &filename) const
Definition: JsonFileLoader.cxx:28
TrigConf::JsonFileLoader::loadFile
bool loadFile(const std::string &filename, boost::property_tree::ptree &data, const std::string &pathToChild="") const
Load content of json file into a ptree.
Definition: JsonFileLoader.cxx:45
test_pyathena.pt
pt
Definition: test_pyathena.py:11
TrigConf::JsonFileLoader::JsonFileLoader
JsonFileLoader()
Constructor.
Definition: JsonFileLoader.cxx:19
TrigConf::JsonFileLoader::checkTriggerLevel
bool checkTriggerLevel(const std::string &filename, std::string &level) const
Checks the trigger level of a given json file.
Definition: JsonFileLoader.cxx:128
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
TrigConf::JsonFileLoader::saveFile
bool saveFile(const std::string &filename, const DataStructure &data) const
Save content of DataStructure (underlying ptree) to a file.
Definition: JsonFileLoader.cxx:99
file
TFile * file
Definition: tile_monitor.h:29
TrigConf::JsonFileLoader::getFileType
std::string getFileType(const std::string &filename) const
Checks the trigger level of a given json file.
Definition: JsonFileLoader.cxx:114
TRG_MSG_INFO
#define TRG_MSG_INFO(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:27
TRG_MSG_WARNING
#define TRG_MSG_WARNING(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:28
ptree
boost::property_tree::ptree ptree
Definition: JsonFileLoader.cxx:16
TrigConf::DataStructure
Base class for Trigger configuration data and wrapper around underlying representation.
Definition: DataStructure.h:37
TrigConf::TrigConfMessaging
Class to provide easy access to TrigConf::MsgStream for TrigConf classes.
Definition: TrigConfMessaging.h:28
python.trfUtils.findFile
def findFile(pathvar, fname)
Find a named file along a colon separated PATH type variable.
Definition: trfUtils.py:37
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
JsonFileLoader.h
Loader class for Trigger configuration from Json.
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9