ATLAS Offline Software
Loading...
Searching...
No Matches
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
16using ptree = boost::property_tree::ptree;
17
18
22
23/*
24 File will be search first absolute (when starting with "/" or
25 relative to the current path.
26*/
27std::string
28TrigConf::JsonFileLoader::findFile(const std::string & filename) const {
29
30 // check for the file as specified
31 if( std::filesystem::exists( filename ) ) {
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
44bool
45TrigConf::JsonFileLoader::loadFile( const std::string & filename,
46 boost::property_tree::ptree & data,
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
81bool
82TrigConf::JsonFileLoader::loadFile( const std::string & filename,
84 const std::string & pathToChild ) const
85{
86 boost::property_tree::ptree pt;
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
98bool
99TrigConf::JsonFileLoader::saveFile( const std::string & filename,
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
113std::string
114TrigConf::JsonFileLoader::getFileType( const std::string & filename ) const
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
127bool
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}
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
boost::property_tree::ptree ptree
Loader class for Trigger configuration from Json.
Base class for Trigger configuration data and wrapper around underlying representation.
bool saveFile(const std::string &filename, const DataStructure &data) const
Save content of DataStructure (underlying ptree) to a file.
bool checkTriggerLevel(const std::string &filename, std::string &level) const
Checks the trigger level of a given json file.
std::string findFile(const std::string &filename) const
std::string getFileType(const std::string &filename) const
Checks the trigger level of a given json file.
bool loadFile(const std::string &filename, boost::property_tree::ptree &data, const std::string &pathToChild="") const
Load content of json file into a ptree.
TrigConfMessaging(const std::string &name)
Constructor with parameters.
TFile * file