Skip to content

Commit 3dba4e6

Browse files
Barthelemyknopers8
authored andcommitted
[QC-1297] CustomParameters support for nested configuration (AliceO2Group#2609)
* [QC-1297] CustomParameters support for nested configuration * fix the case where it is not parsable. * clang-format --------- Co-authored-by: Piotr Konopka <piotr.jan.konopka@cern.ch>
1 parent 942bd10 commit 3dba4e6

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

Framework/include/QualityControl/CustomParameters.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,34 @@ class CustomParameters
108108
/**
109109
* Return the optional value for the given key, runType and beamType.
110110
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
111-
* Empty is only returned if the key could not be found in any combination of the provided run and beam types with "default". * @param key
111+
* Empty is only returned if the key could not be found in any combination of the provided run and beam types with "default".
112+
* @param key
112113
* @param activity
113114
* @return an optional with the value for the given key and for the given activity.
114115
*/
115116
std::optional<std::string> atOptional(const std::string& key, const Activity& activity) const;
116117

118+
/**
119+
* Return the ptree representation of the optional value for the given key, runType and beamType.
120+
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
121+
* Empty is only returned if the key could not be found in any combination of the provided run and beam types with "default".
122+
* @param key
123+
* @param runType
124+
* @param beamType
125+
* @return an optional with the ptree representation of the value for the given key, runType and beamType or empty if not found.
126+
*/
127+
std::optional<boost::property_tree::ptree> getOptionalPtree(const std::string& key, const std::string& runType = "default", const std::string& beamType = "default") const;
128+
129+
/**
130+
* Return the ptree representation of the optional value for the given key, runType and beamType.
131+
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
132+
* Empty is only returned if the key could not be found in any combination of the provided run and beam types with "default".
133+
* @param key
134+
* @param activity
135+
* @return an optional with the ptree representation of the value for the given key, runType and beamType or empty if not found.
136+
*/
137+
std::optional<boost::property_tree::ptree> getOptionalPtree(const std::string& key, const Activity& activity) const;
138+
117139
/**
118140
* Return the value for the given key, runType and beamType (the two latter optional). If it does not exist, returns the default value if provided or an empty string.
119141
* @param key

Framework/src/CustomParameters.cxx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
// or submit itself to any jurisdiction.
1111

1212
#include "QualityControl/CustomParameters.h"
13-
#include <DataFormatsParameters/ECSDataAdapters.h>
1413
#include <iostream>
1514
#include <boost/property_tree/ptree.hpp>
1615
#include <string_view>
1716
#include <vector>
17+
#include <boost/property_tree/json_parser.hpp>
1818

1919
namespace o2::quality_control::core
2020
{
@@ -96,6 +96,31 @@ std::optional<std::string> CustomParameters::atOptional(const std::string& key,
9696
return atOptional(key, activity.mType, activity.mBeamType);
9797
}
9898

99+
std::optional<boost::property_tree::ptree> CustomParameters::getOptionalPtree(const std::string& key, const std::string& runType, const std::string& beamType) const
100+
{
101+
std::optional<boost::property_tree::ptree> result = std::nullopt;
102+
103+
// get the text and make it a ptree
104+
auto text = atOptional(key, runType, beamType);
105+
if (text.has_value()) {
106+
std::stringstream listingAsStringStream{ text.value() };
107+
boost::property_tree::ptree pt;
108+
try {
109+
boost::property_tree::read_json(listingAsStringStream, pt);
110+
} catch (const boost::property_tree::json_parser::json_parser_error& e) {
111+
return result;
112+
}
113+
result = pt;
114+
}
115+
116+
return result;
117+
}
118+
119+
std::optional<boost::property_tree::ptree> CustomParameters::getOptionalPtree(const std::string& key, const Activity& activity) const
120+
{
121+
return getOptionalPtree(key, activity.mType, activity.mBeamType);
122+
}
123+
99124
std::string CustomParameters::atOrDefaultValue(const std::string& key, std::string defaultValue, const std::string& runType, const std::string& beamType) const
100125
{
101126
try {

Framework/test/testCustomParameters.cxx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,77 @@ TEST_CASE("test_getAllDefaults")
265265
auto result = cp.getAllDefaults();
266266
CHECK(result.size() == 0);
267267
}
268+
269+
TEST_CASE("test_getOptionalPtree")
270+
{
271+
CustomParameters cp;
272+
std::string content = R""""(
273+
[
274+
{
275+
"name": "mean_of_histogram",
276+
"title": "Mean trend of the example histogram",
277+
"graphAxisLabel": "Mean X:time",
278+
"graphYRange": "0:10000",
279+
"graphs" : [
280+
{
281+
"name": "mean_trend",
282+
"title": "mean trend",
283+
"varexp": "example.mean:time",
284+
"selection": "",
285+
"option": "*L PLC PMC"
286+
}, {
287+
"name": "mean_trend_1000",
288+
"title": "mean trend + 1000",
289+
"varexp": "example.mean + 1000:time",
290+
"selection": "",
291+
"option": "* PMC",
292+
"graphErrors": "1:200"
293+
}
294+
]
295+
},
296+
{
297+
"name": "histogram_of_means",
298+
"title": "Distribution of mean values in the example histogram",
299+
"graphs" : [{
300+
"varexp": "example.mean",
301+
"selection": "",
302+
"option": ""
303+
}]
304+
},
305+
{
306+
"name": "example_quality",
307+
"title": "Trend of the example histogram's quality",
308+
"graphs" : [{
309+
"varexp": "QcCheck.name:time",
310+
"selection": "",
311+
"option": "*"
312+
}]
313+
}
314+
]
315+
)"""";
316+
cp.set("key", content);
317+
auto pt = cp.getOptionalPtree("key");
318+
CHECK(pt.has_value());
319+
320+
std::size_t number_plots = std::distance(pt->begin(), pt->end());
321+
CHECK(number_plots == 3);
322+
323+
auto first_plot = pt->begin()->second;
324+
CHECK(first_plot.get<string>("name") == "mean_of_histogram");
325+
auto graphs = first_plot.get_child("graphs");
326+
CHECK(graphs.size() == 2);
327+
328+
auto last_plot = std::prev(pt->end())->second;
329+
CHECK(last_plot.get<string>("name") == "example_quality");
330+
331+
// test for failure
332+
CustomParameters cp2;
333+
cp2.set("key", "blabla");
334+
auto pt2 = cp2.getOptionalPtree("key");
335+
CHECK(!pt2.has_value());
336+
337+
// try to get it as text
338+
auto text = cp.atOptional("key");
339+
CHECK(text.has_value());
340+
CHECK(text == content);
341+
}

0 commit comments

Comments
 (0)