Skip to content

Commit 46aa0b9

Browse files
authored
[QC-1020] Fall back to default values if beamtype or runtype cannot b… (#2033)
* [QC-1020] Fall back to default values if beamtype or runtype cannot be found. * [QC-1020] Remove cout * format * explicit use of nullopt * Address Andrea's comments
1 parent 40082d3 commit 46aa0b9

4 files changed

Lines changed: 118 additions & 12 deletions

File tree

Framework/include/QualityControl/CustomParameters.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,33 @@ class CustomParameters
6969

7070
/**
7171
* Return the value for the given key, runType and beamType.
72+
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
73+
* An exception is raised only if the key could not be found in any combination of the provided run and beam types with "default".
7274
* @param key
7375
* @param runType
7476
* @param beamType
7577
* @return the value for the given key, runType and beamType.
76-
* @throw std::out_of_range if no key-value pair corresponds to this key and to these beamType and runType
78+
* @throw std::out_of_range if no key-value pair corresponds to this key and to these beamType and runType and that substitutions
79+
* with "default" failed.
7780
*/
7881
std::string at(const std::string& key, const std::string& runType = "default", const std::string& beamType = "default") const;
7982

8083
/**
81-
* Return the optional value for the given key, runType and beamType (the two latter optional).
84+
* Return the value for the given key, runType and beamType.
85+
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
86+
* An exception is raised only if the key could not be found in any combination of the provided run and beam types with "default".
87+
* @param key
88+
* @param activity
89+
* @return the value for the given key and for the given activity.
90+
* @throw std::out_of_range if no key-value pair corresponds to this key and to this activity and that substitutions
91+
* with "default" failed.
92+
*/
93+
std::string at(const std::string& key, const Activity& activity) const;
94+
95+
/**
96+
* Return the optional value for the given key, runType and beamType.
97+
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
98+
* Empty is only returned if the key could not be found in any combination of the provided run and beam types with "default".
8299
* @param key
83100
* @param runType
84101
* @param beamType
@@ -87,8 +104,9 @@ class CustomParameters
87104
std::optional<std::string> atOptional(const std::string& key, const std::string& runType = "default", const std::string& beamType = "default") const;
88105

89106
/**
90-
* Return the optional value for the given key in the specified activity.
91-
* @param key
107+
* Return the optional value for the given key, runType and beamType.
108+
* If no key is found for the runType and the Beamtype, the fallback is to substitute with "default", first for beamType then for runType.
109+
* 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
92110
* @param activity
93111
* @return an optional with the value for the given key and for the given activity.
94112
*/

Framework/src/CustomParameters.cxx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "QualityControl/CustomParameters.h"
1313
#include <DataFormatsParameters/ECSDataAdapters.h>
1414
#include <iostream>
15+
#include <string_view>
16+
#include <vector>
1517

1618
namespace o2::quality_control::core
1719
{
@@ -54,16 +56,39 @@ const std::unordered_map<std::string, std::string>& CustomParameters::getAllDefa
5456

5557
std::string CustomParameters::at(const std::string& key, const std::string& runType, const std::string& beamType) const
5658
{
57-
return mCustomParameters.at(runType).at(beamType).at(key);
59+
auto optionalResult = atOptional(key, runType, beamType); // just reuse the logic we developed in atOptional
60+
if (!optionalResult.has_value()) {
61+
return mCustomParameters.at(runType).at(beamType).at(key); // we know we will get a out_of_range exception
62+
}
63+
return optionalResult.value();
64+
}
65+
66+
std::string CustomParameters::at(const std::string& key, const Activity& activity) const
67+
{
68+
// Get the proper parameter for the given activity
69+
const int runType = activity.mType; // get the type for this run
70+
// convert it to a string (via a string_view as this is what we get from O2)
71+
const std::string_view runTypeStringView = o2::parameters::GRPECS::RunTypeNames[runType];
72+
const std::string runTypeString{ runTypeStringView };
73+
// get the param
74+
return at(key, runTypeString, activity.mBeamType);
5875
}
5976

6077
std::optional<std::string> CustomParameters::atOptional(const std::string& key, const std::string& runType, const std::string& beamType) const
6178
{
62-
try {
63-
return mCustomParameters.at(runType).at(beamType).at(key);
64-
} catch (const std::out_of_range& exc) {
65-
return {};
79+
std::optional<std::string> result = std::nullopt;
80+
const std::vector<std::string> runTypes = { runType, std::string("default") };
81+
const std::vector<std::string> beamTypes = { beamType, std::string("default") };
82+
for (const auto& rt : runTypes) {
83+
for (const auto& bt : beamTypes) {
84+
try {
85+
result = mCustomParameters.at(rt).at(bt).at(key);
86+
return result;
87+
} catch (const std::out_of_range& exc) { // ignored on purpose
88+
}
89+
}
6690
}
91+
return result;
6792
}
6893

6994
std::optional<std::string> CustomParameters::atOptional(const std::string& key, const Activity& activity) const

Framework/test/testCustomParameters.cxx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,59 @@ BOOST_AUTO_TEST_CASE(test_cp_new_access_pattern)
182182
BOOST_CHECK_EQUAL(std::stoi(param2->second), 1);
183183
}
184184
}
185+
186+
BOOST_AUTO_TEST_CASE(test_default_if_not_found_at_optional)
187+
{
188+
CustomParameters cp;
189+
190+
// no default values are in the CP, we get an empty result
191+
BOOST_CHECK_EQUAL(cp.atOptional("key", "PHYSICS", "proton-proton").has_value(), false);
192+
BOOST_CHECK_EQUAL(cp.atOptional("key", "TECHNICAL", "proton-proton").has_value(), false);
193+
194+
// prepare the CP
195+
cp.set("key", "valueDefaultDefault", "default", "default");
196+
cp.set("key", "valuePhysicsDefault", "PHYSICS", "default");
197+
cp.set("key", "valuePhysicsPbPb", "PHYSICS", "Pb-Pb");
198+
cp.set("key", "valueCosmicsDefault", "COSMICS", "default");
199+
cp.set("key", "valueCosmicsDefault", "default", "PROTON-PROTON");
200+
201+
// check the data
202+
BOOST_CHECK_EQUAL(cp.atOptional("key").value(), "valueDefaultDefault");
203+
BOOST_CHECK_EQUAL(cp.atOptional("key", "PHYSICS").value(), "valuePhysicsDefault");
204+
BOOST_CHECK_EQUAL(cp.atOptional("key", "PHYSICS", "Pb-Pb").value(), "valuePhysicsPbPb");
205+
BOOST_CHECK_EQUAL(cp.atOptional("key", "COSMICS", "default").value(), "valueCosmicsDefault");
206+
BOOST_CHECK_EQUAL(cp.atOptional("key", "default", "PROTON-PROTON").value(), "valueCosmicsDefault");
207+
208+
// check when something is missing
209+
BOOST_CHECK_EQUAL(cp.atOptional("key", "PHYSICS", "PROTON-PROTON").value(), "valuePhysicsDefault"); // key is not defined for pp
210+
BOOST_CHECK_EQUAL(cp.atOptional("key", "TECHNICAL", "STRANGE").value(), "valueDefaultDefault"); // key is not defined for run nor beam
211+
BOOST_CHECK_EQUAL(cp.atOptional("key", "TECHNICAL", "PROTON-PROTON").value(), "valueCosmicsDefault"); // key is not defined for technical
212+
}
213+
214+
BOOST_AUTO_TEST_CASE(test_default_if_not_found_at)
215+
{
216+
CustomParameters cp;
217+
218+
// no default values are in the CP, we get an empty result
219+
BOOST_CHECK_THROW(cp.at("key", "PHYSICS", "proton-proton"), std::out_of_range);
220+
BOOST_CHECK_THROW(cp.at("key", "TECHNICAL", "proton-proton"), std::out_of_range);
221+
222+
// prepare the CP
223+
cp.set("key", "valueDefaultDefault", "default", "default");
224+
cp.set("key", "valuePhysicsDefault", "PHYSICS", "default");
225+
cp.set("key", "valuePhysicsPbPb", "PHYSICS", "Pb-Pb");
226+
cp.set("key", "valueCosmicsDefault", "COSMICS", "default");
227+
cp.set("key", "valueCosmicsDefault", "default", "PROTON-PROTON");
228+
229+
// check the data
230+
BOOST_CHECK_EQUAL(cp.at("key"), "valueDefaultDefault");
231+
BOOST_CHECK_EQUAL(cp.at("key", "PHYSICS"), "valuePhysicsDefault");
232+
BOOST_CHECK_EQUAL(cp.at("key", "PHYSICS", "Pb-Pb"), "valuePhysicsPbPb");
233+
BOOST_CHECK_EQUAL(cp.at("key", "COSMICS", "default"), "valueCosmicsDefault");
234+
BOOST_CHECK_EQUAL(cp.at("key", "default", "PROTON-PROTON"), "valueCosmicsDefault");
235+
236+
// check when something is missing
237+
BOOST_CHECK_EQUAL(cp.at("key", "PHYSICS", "PROTON-PROTON"), "valuePhysicsDefault"); // key is not defined for pp
238+
BOOST_CHECK_EQUAL(cp.at("key", "TECHNICAL", "STRANGE"), "valueDefaultDefault"); // key is not defined for run nor beam
239+
BOOST_CHECK_EQUAL(cp.at("key", "TECHNICAL", "PROTON-PROTON"), "valueCosmicsDefault"); // key is not defined for technical
240+
}

doc/Advanced.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ The simple, deprecated, syntax is
10441044
"tasks": {
10451045
"QcTask": {
10461046
"taskParameters": {
1047-
"myOwnKey": "myOwnValue"
1047+
"myOwnKey1": "myOwnValue1"
10481048
},
10491049
```
10501050
It is accessed with : `mCustomParameters["myOwnKey"]`.
@@ -1056,7 +1056,7 @@ The new syntax is
10561056
"extendedTaskParameters": {
10571057
"default": {
10581058
"default": {
1059-
"myOwnKey": "myOwnValue",
1059+
"myOwnKey1": "myOwnValue1",
10601060
"myOwnKey2": "myOwnValue2",
10611061
"myOwnKey3": "myOwnValue3"
10621062
}
@@ -1087,8 +1087,12 @@ The values can be accessed in various ways.
10871087

10881088
### Access optional values with or without activity
10891089

1090+
The value for the key, runType and beamType is returned if found, or an empty value otherwise.
1091+
However, before returning an empty value we try to substitute the runType and the beamType with "default".
1092+
10901093
```c++
1091-
// returns an Optional<string> if it finds the key `myOwnKey` for the runType and beamType of the provided activity.
1094+
// returns an Optional<string> if it finds the key `myOwnKey` for the runType and beamType of the provided activity,
1095+
// or if it can find the key with the runType or beamType substituted with "default".
10921096
auto param = mCustomParameters.atOptional("myOwnKey", activity); // activity is "PHYSICS", "Pb-Pb" , returns "myOwnValue1d"
10931097
// same but passing directly the run and beam types
10941098
auto param = mCustomParameters.atOptional("myOwnKey", "PHYSICS", "Pb-Pb"); // returns "myOwnValue1d"
@@ -1098,6 +1102,9 @@ auto param = mCustomParameters.atOptional("myOwnKey", "PHYSICS"); // returns "my
10981102

10991103
### Access values directly specifying the run and beam type
11001104

1105+
The value for the key, runType and beamType is returned if found, or an exception is thrown otherwise..
1106+
However, before throwing we try to substitute the runType and the beamType with "default".
1107+
11011108
```c++
11021109
mCustomParameters["myOwnKey"]; // considering that run and beam type are `default` --> returns `myOwnValue`
11031110
mCustomParameters.at("myOwnKey"); // returns `myOwnValue`

0 commit comments

Comments
 (0)