Skip to content

Commit 681f99d

Browse files
Barthelemyknopers8
andauthored
[QC-977] Select automatically the correct params (#1865)
* [QC-977] Select automatically the correct params based on the current activity. * test * doc * Update Framework/src/CustomParameters.cxx Co-authored-by: Piotr Konopka <piotr.jan.konopka@cern.ch> * Update Framework/src/CustomParameters.cxx Co-authored-by: Piotr Konopka <piotr.jan.konopka@cern.ch> --------- Co-authored-by: Piotr Konopka <piotr.jan.konopka@cern.ch>
1 parent a9eab82 commit 681f99d

5 files changed

Lines changed: 81 additions & 20 deletions

File tree

Framework/include/QualityControl/CustomParameters.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef QC_CUSTOM_PARAMETERS_H
1818
#define QC_CUSTOM_PARAMETERS_H
1919

20+
#include "QualityControl/Activity.h"
21+
2022
#include <string>
2123
#include <unordered_map>
2224
#include <optional>
@@ -84,6 +86,14 @@ class CustomParameters
8486
*/
8587
std::optional<std::string> atOptional(const std::string& key, const std::string& runType = "default", const std::string& beamType = "default") const;
8688

89+
/**
90+
* Return the optional value for the given key in the specified activity.
91+
* @param key
92+
* @param activity
93+
* @return an optional with the value for the given key and for the given activity.
94+
*/
95+
std::optional<std::string> atOptional(const std::string& key, const Activity& activity) const;
96+
8797
/**
8898
* 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.
8999
* @param key

Framework/src/CustomParameters.cxx

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

1212
#include "QualityControl/CustomParameters.h"
13-
13+
#include <DataFormatsParameters/ECSDataAdapters.h>
1414
#include <iostream>
1515

1616
namespace o2::quality_control::core
@@ -66,6 +66,17 @@ std::optional<std::string> CustomParameters::atOptional(const std::string& key,
6666
}
6767
}
6868

69+
std::optional<std::string> CustomParameters::atOptional(const std::string& key, const Activity& activity) const
70+
{
71+
// Get the proper parameter for the given activity
72+
const int runType = activity.mType; // get the type for this run
73+
// convert it to a string (via a string_view as this is what we get from O2)
74+
const std::string_view runTypeStringView = o2::parameters::GRPECS::RunTypeNames[runType];
75+
const std::string runTypeString{ runTypeStringView };
76+
// get the param
77+
return atOptional(key, runTypeString, activity.mBeamType);
78+
}
79+
6980
std::string CustomParameters::atOrDefaultValue(const std::string& key, std::string defaultValue, const std::string& runType, const std::string& beamType)
7081
{
7182
try {

Framework/test/testCustomParameters.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ BOOST_AUTO_TEST_CASE(test_at_optional)
136136
BOOST_CHECK_EQUAL(cp.atOptional("abc").value_or("bla"), "bla");
137137
}
138138

139+
BOOST_AUTO_TEST_CASE(test_at_optional_activity)
140+
{
141+
Activity activity;
142+
activity.mBeamType = "PROTON-PROTON";
143+
activity.mType = 1;
144+
145+
CustomParameters cp;
146+
cp.set("aaa", "AAA");
147+
cp.set("bbb", "BBB");
148+
cp.set("aaa", "asdf", "PHYSICS");
149+
cp.set("aaa", "CCC", "PHYSICS", "PROTON-PROTON");
150+
cp.set("aaa", "DDD", "PHYSICS", "Pb-Pb");
151+
cp.set("aaa", "AAA", "TECHNICAL", "PROTON-PROTON");
152+
153+
BOOST_CHECK_EQUAL(cp.atOptional("aaa", activity).value(), "CCC");
154+
BOOST_CHECK_EQUAL(cp.atOptional("abc", activity).has_value(), false);
155+
BOOST_CHECK_EQUAL(cp.atOptional("abc", activity).value_or("bla"), "bla");
156+
157+
Activity activity2;
158+
activity.mBeamType = "Pb-Pb";
159+
activity.mType = 1;
160+
BOOST_CHECK_EQUAL(cp.atOptional("aaa", activity).value(), "DDD");
161+
}
162+
139163
BOOST_AUTO_TEST_CASE(test_cp_new_access_pattern)
140164
{
141165
CustomParameters cp;

Modules/Skeleton/src/SkeletonTask.cxx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919

2020
#include "QualityControl/QcInfoLogger.h"
2121
#include "Skeleton/SkeletonTask.h"
22-
#include <Framework/InputRecord.h>
2322
#include <Framework/InputRecordWalker.h>
2423
#include <Framework/DataRefUtils.h>
25-
#include "DataFormatsParameters/ECSDataAdapters.h"
2624

2725
namespace o2::quality_control_modules::skeleton
2826
{
@@ -64,14 +62,14 @@ void SkeletonTask::startOfActivity(const Activity& activity)
6462
ILOG(Debug, Devel) << "startOfActivity " << activity.mId << ENDM;
6563
mHistogram->Reset();
6664

67-
// Get the proper parameter for the given activity
68-
int runType = activity.mType; // get the type for this run
69-
// convert it to a string (via a string_view as this is what we get from O2)
70-
string_view runTypeStringView = o2::parameters::GRPECS::RunTypeNames[runType];
71-
string runTypeString = { runTypeStringView.begin(), runTypeStringView.end() };
72-
// get the param
73-
if (auto param = mCustomParameters.atOptional("myOwnKey", runTypeString)) {
74-
ILOG(Debug, Devel) << "Custom parameter - myOwnKey: " << param.value() << ENDM;
65+
// Example: retrieve custom parameters
66+
std::string parameter;
67+
// first we try for the current activity. It returns an optional.
68+
if (auto param = mCustomParameters.atOptional("myOwnKey", activity)) {
69+
parameter = param.value(); // we got a value
70+
} else {
71+
// we did not get a value. We ask for the "default" runtype and beamtype and we specify a default return value.
72+
parameter = mCustomParameters.atOrDefaultValue("myOwnKey", "some default");
7573
}
7674
}
7775

doc/Advanced.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Advanced topics
4848
* [Enable the repo cleaner](#enable-the-repo-cleaner)
4949
* [Configuration](#configuration-1)
5050
* [Merging multiple configuration files into one](#merging-multiple-configuration-files-into-one)
51-
* [Definition and access of task-specific configuration](#definition-and-access-of-task-specific-configuration)
51+
* [Definition and access of user-specific configuration](#definition-and-access-of-user-specific-configuration)
52+
* [Definition of new arguments](#definition-of-new-arguments)
5253
* [Configuration files details](#configuration-files-details)
5354
* [Global configuration structure](#global-configuration-structure)
5455
* [Common configuration](#common-configuration)
@@ -995,7 +996,7 @@ The following tasks will be merged correctly:
995996
```
996997
The same approach can be applied to other actors in the QC framework, like Checks (`checkName`), Aggregators(`aggregatorName`), External Tasks (`taskName`) and Postprocessing Tasks (`taskName`).
997998

998-
## Definition and access of task-specific configuration
999+
## Definition and access of user-specific configuration
9991000

10001001
A task can access custom parameters declared in the configuration file at `qc.tasks.<task_id>.extendedTaskParameters` or `qc.tasks.<task_id>.taskParameters`. They are stored inside an object of type `CustomParameters` named `mCustomParameters`, which is a protected member of `TaskInterface`.
10011002

@@ -1026,10 +1027,10 @@ The new syntax is
10261027
"myOwnKey1": "myOwnValue1b",
10271028
"myOwnKey2": "myOwnValue2b"
10281029
},
1029-
"pp": {
1030+
"PROTON-PROTON": {
10301031
"myOwnKey1": "myOwnValue1c"
10311032
},
1032-
"PbPb": {
1033+
"Pb-Pb": {
10331034
"myOwnKey1": "myOwnValue1d"
10341035
}
10351036
},
@@ -1043,7 +1044,21 @@ It allows to have variations of the parameters depending on the run and beam typ
10431044
to ignore the run or the beam type.
10441045
The beam type is one of the following: `PROTON-PROTON`, `Pb-Pb`, `Pb-PROTON`
10451046

1046-
The values can be accessed this way:
1047+
The values can be accessed in various ways.
1048+
1049+
### Access optional values with or without activity
1050+
1051+
```c++
1052+
// returns an Optional<string> if it finds the key `myOwnKey` for the runType and beamType of the provided activity.
1053+
auto param = mCustomParameters.atOptional("myOwnKey", activity); // activity is "PHYSICS", "Pb-Pb" , returns "myOwnValue1d"
1054+
// same but passing directly the run and beam types
1055+
auto param = mCustomParameters.atOptional("myOwnKey", "PHYSICS", "Pb-Pb"); // returns "myOwnValue1d"
1056+
// or with only the run type
1057+
auto param = mCustomParameters.atOptional("myOwnKey", "PHYSICS"); // returns "myOwnValue1b"
1058+
```
1059+
1060+
### Access values directly specifying the run and beam type
1061+
10471062
```c++
10481063
mCustomParameters["myOwnKey"]; // considering that run and beam type are `default` --> returns `myOwnValue`
10491064
mCustomParameters.at("myOwnKey"); // returns `myOwnValue`
@@ -1054,22 +1069,25 @@ mCustomParameters.at("myOwnKey1", "PHYSICS", "PROTON-PROTON"); // returns `myOwn
10541069
mCustomParameters.at("myOwnKey1", "PHYSICS", "Pb-Pb"); // returns `myOwnValue1d`
10551070
mCustomParameters.at("myOwnKey2", "COSMICS"); // returns `myOwnValue2e`
10561071
```
1072+
1073+
### Access values and return default if not found
1074+
10571075
The correct way of accessing a parameter and to default to a value if it is not there, is the following:
10581076
```c++
10591077
std::string param = mCustomParameters.atOrDefaultValue("myOwnKey1", "physics", "pp", "1");
10601078
int casted = std::stoi(param);
10611079
```
1080+
1081+
### Find a value
1082+
10621083
Finally the way to search for a value and only act if it is there is the following:
10631084
```c++
10641085
if (auto param2 = mCustomParameters.find("myOwnKey1", "physics", "pp"); param2 != cp.end()) {
10651086
int casted = std::stoi(param);
10661087
}
10671088
```
10681089

1069-
1070-
A task can access custom parameters declared in the configuration file at `qc.tasks.<task_id>.taskParameters`. They are stored inside a key-value map named mCustomParameters, which is a protected member of `TaskInterface`.
1071-
1072-
1090+
## Definition of new arguments
10731091

10741092
One can also tell the DPL driver to accept new arguments. This is done using the `customize` method at the top of your workflow definition (usually called "runXXX" in the QC).
10751093

0 commit comments

Comments
 (0)