ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Trigger
TrigConfiguration
TrigConfIO
src
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
5
#include "
TrigConfIO/JsonFileLoader.h
"
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
16
using
ptree
= boost::property_tree::ptree;
17
18
19
TrigConf::JsonFileLoader::JsonFileLoader
() :
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
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
44
bool
45
TrigConf::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
81
bool
82
TrigConf::JsonFileLoader::loadFile
(
const
std::string & filename,
83
DataStructure
& data,
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
98
bool
99
TrigConf::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
113
std::string
114
TrigConf::JsonFileLoader::getFileType
(
const
std::string & filename )
const
115
{
116
std::string ft =
"UNKNOWN"
;
117
118
DataStructure
data;
119
if
(
loadFile
( filename, data ) ) {
120
ft = data.getAttribute(
"filetype"
,
/*ignoreIfMissing*/
true
, ft);
121
}
122
123
return
ft;
124
}
125
126
127
bool
128
TrigConf::JsonFileLoader::checkTriggerLevel
(
const
std::string & filename,
129
std::string & level )
const
130
{
131
level =
"UNKNOWN"
;
132
133
DataStructure
data;
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
}
ptree
boost::property_tree::ptree ptree
Definition
JsonFileLoader.cxx:16
JsonFileLoader.h
Loader class for Trigger configuration from Json.
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
TRG_MSG_ERROR
#define TRG_MSG_ERROR(x)
Definition
Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:29
TrigConf::DataStructure
Base class for Trigger configuration data and wrapper around underlying representation.
Definition
DataStructure.h:37
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
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
TrigConf::JsonFileLoader::JsonFileLoader
JsonFileLoader()
Constructor.
Definition
JsonFileLoader.cxx:19
TrigConf::JsonFileLoader::findFile
std::string findFile(const std::string &filename) const
Definition
JsonFileLoader.cxx:28
TrigConf::JsonFileLoader::getFileType
std::string getFileType(const std::string &filename) const
Checks the trigger level of a given json file.
Definition
JsonFileLoader.cxx:114
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
TrigConf::TrigConfMessaging::TrigConfMessaging
TrigConfMessaging(const std::string &name)
Constructor with parameters.
Definition
TrigConfMessaging.h:34
file
TFile * file
Definition
tile_monitor.h:29
Generated on
for ATLAS Offline Software by
1.17.0