Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Framework/include/QualityControl/CustomParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,34 @@ class CustomParameters
/**
* Return the optional value for the given key, runType and beamType.
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
* 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
* 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
* @param activity
* @return an optional with the value for the given key and for the given activity.
*/
std::optional<std::string> atOptional(const std::string& key, const Activity& activity) const;

/**
* Return the ptree representation of the optional value for the given key, runType and beamType.
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
* 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
* @param runType
* @param beamType
* @return an optional with the ptree representation of the value for the given key, runType and beamType or empty if not found.
*/
std::optional<boost::property_tree::ptree> getOptionalPtree(const std::string& key, const std::string& runType = "default", const std::string& beamType = "default") const;

/**
* Return the ptree representation of the optional value for the given key, runType and beamType.
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
* 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
* @param activity
* @return an optional with the ptree representation of the value for the given key, runType and beamType or empty if not found.
*/
std::optional<boost::property_tree::ptree> getOptionalPtree(const std::string& key, const Activity& activity) const;

/**
* 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.
* @param key
Expand Down
27 changes: 26 additions & 1 deletion Framework/src/CustomParameters.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
// or submit itself to any jurisdiction.

#include "QualityControl/CustomParameters.h"
#include <DataFormatsParameters/ECSDataAdapters.h>
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <string_view>
#include <vector>
#include <boost/property_tree/json_parser.hpp>

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

std::optional<boost::property_tree::ptree> CustomParameters::getOptionalPtree(const std::string& key, const std::string& runType, const std::string& beamType) const
{
std::optional<boost::property_tree::ptree> result = std::nullopt;

// get the text and make it a ptree
auto text = atOptional(key, runType, beamType);
if (text.has_value()) {
std::stringstream listingAsStringStream{ text.value() };
boost::property_tree::ptree pt;
try {
boost::property_tree::read_json(listingAsStringStream, pt);
} catch (const boost::property_tree::json_parser::json_parser_error& e) {
return result;
}
result = pt;
}

return result;
}

std::optional<boost::property_tree::ptree> CustomParameters::getOptionalPtree(const std::string& key, const Activity& activity) const
{
return getOptionalPtree(key, activity.mType, activity.mBeamType);
}

std::string CustomParameters::atOrDefaultValue(const std::string& key, std::string defaultValue, const std::string& runType, const std::string& beamType) const
{
try {
Expand Down
74 changes: 74 additions & 0 deletions Framework/test/testCustomParameters.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,77 @@ TEST_CASE("test_getAllDefaults")
auto result = cp.getAllDefaults();
CHECK(result.size() == 0);
}

TEST_CASE("test_getOptionalPtree")
{
CustomParameters cp;
std::string content = R""""(
[
{
"name": "mean_of_histogram",
"title": "Mean trend of the example histogram",
"graphAxisLabel": "Mean X:time",
"graphYRange": "0:10000",
"graphs" : [
{
"name": "mean_trend",
"title": "mean trend",
"varexp": "example.mean:time",
"selection": "",
"option": "*L PLC PMC"
}, {
"name": "mean_trend_1000",
"title": "mean trend + 1000",
"varexp": "example.mean + 1000:time",
"selection": "",
"option": "* PMC",
"graphErrors": "1:200"
}
]
},
{
"name": "histogram_of_means",
"title": "Distribution of mean values in the example histogram",
"graphs" : [{
"varexp": "example.mean",
"selection": "",
"option": ""
}]
},
{
"name": "example_quality",
"title": "Trend of the example histogram's quality",
"graphs" : [{
"varexp": "QcCheck.name:time",
"selection": "",
"option": "*"
}]
}
]
)"""";
cp.set("key", content);
auto pt = cp.getOptionalPtree("key");
CHECK(pt.has_value());

std::size_t number_plots = std::distance(pt->begin(), pt->end());
CHECK(number_plots == 3);

auto first_plot = pt->begin()->second;
CHECK(first_plot.get<string>("name") == "mean_of_histogram");
auto graphs = first_plot.get_child("graphs");
CHECK(graphs.size() == 2);

auto last_plot = std::prev(pt->end())->second;
CHECK(last_plot.get<string>("name") == "example_quality");

// test for failure
CustomParameters cp2;
cp2.set("key", "blabla");
auto pt2 = cp2.getOptionalPtree("key");
CHECK(!pt2.has_value());

// try to get it as text
auto text = cp.atOptional("key");
CHECK(text.has_value());
CHECK(text == content);
}