-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathtestCustomParameters.cxx
More file actions
374 lines (313 loc) · 12.2 KB
/
testCustomParameters.cxx
File metadata and controls
374 lines (313 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///
/// \file testCustomParameters.cxx
/// \author Barthelemy von Haller
///
#include "QualityControl/CustomParameters.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "getTestDataDirectory.h"
#include "QualityControl/InfrastructureGenerator.h"
#include <iostream>
#include <catch_amalgamated.hpp>
#include <Configuration/ConfigurationFactory.h>
#include <DataSampling/DataSampling.h>
using namespace o2::quality_control::core;
using namespace std;
TEST_CASE("test_cp_basic")
{
CustomParameters cp;
cp.set("key", "value");
CHECK(cp.at("key") == "value");
CHECK(cp.at("key", "default") == "value");
CHECK(cp.at("key", "default", "default") == "value");
cp.set("key", "value_run1", "run1");
CHECK(cp.at("key") == "value");
CHECK(cp.at("key", "run1") == "value_run1");
CHECK(cp.at("key", "run1", "default") == "value_run1");
cp.set("key", "value_beam1", "default", "beam1");
CHECK(cp.at("key") == "value");
CHECK(cp.at("key", "default") == "value");
CHECK(cp.at("key", "default", "beam1") == "value_beam1");
cp.set("key", "value_run1_beam1", "run1", "beam1");
CHECK(cp.at("key") == "value");
CHECK(cp.at("key", "run1", "beam1") == "value_run1_beam1");
CHECK_NOTHROW(cout << cp << endl);
}
TEST_CASE("test_cp_iterators")
{
CustomParameters cp;
cp.set("key1", "value1", "run1", "beam1");
auto param = cp.find("key1", "run1", "beam1");
CHECK(param != cp.end());
if (param != cp.end()) {
CHECK(param->second == "value1");
}
cp.set("key2", "value2");
param = cp.find("key2");
CHECK(param != cp.end());
if (param != cp.end()) {
CHECK(param->second == "value2");
}
param = cp.find("not_found");
CHECK(param == cp.end());
param = cp.find("not_found", "run1");
CHECK(param == cp.end());
param = cp.find("not_found", "run1", "beam1");
CHECK(param == cp.end());
}
TEST_CASE("test_cp_misc")
{
CustomParameters cp;
cp.set("aaa", "AAA");
cp.set("bbb", "BBB");
cp.set("aaa", "AAA", "runX");
cp.set("aaa", "AAA", "runX", "beamB");
cp.set("ccc", "CCC");
cp.set("bbb", "BBB", "runX");
cp.set("ccc", "CCC", "runY");
CHECK(cp.count("aaa") == 1);
CHECK(cp.count("bbb") == 1);
CHECK(cp.count("aaa") == 1);
CHECK(cp.count("aaa") == 1);
CHECK(cp.at("aaa") == "AAA");
CHECK(cp.at("bbb") == "BBB");
CHECK(cp.at("aaa", "runX") == "AAA");
CHECK_THROWS_AS(cp.at("not_found"), std::out_of_range);
CHECK(cp.atOrDefaultValue("not_found", "default_value") == "default_value");
CHECK(cp.atOrDefaultValue("not_found") == "");
CHECK(cp.atOrDefaultValue("not_found", "default_value2", "asdf", "adsf") == "default_value2");
CHECK(cp["aaa"] == "AAA");
CHECK(cp["ccc"] == "CCC");
auto all = cp.getAllForRunBeam("runX", "default");
auto another = cp.getAllForRunBeam("default", "default");
auto same = cp.getAllDefaults();
CHECK(another.size() == same.size());
CHECK(all.size() == 2);
CHECK(another.size() == 3);
CHECK_NOTHROW(cp["not_found"]); // this won't throw, it will create an empty value at "not_found"
CHECK(cp.count("not_found") == 1);
CHECK(cp.at("not_found") == "");
// test bracket assignemnt
cp["something"] = "else";
CHECK(cp.at("something") == "else");
cp["something"] = "asdf";
CHECK(cp.at("something") == "asdf");
}
TEST_CASE("test_at_optional")
{
CustomParameters cp;
cp.set("aaa", "AAA");
cp.set("bbb", "BBB");
cp.set("aaa", "AAA", "runX");
cp.set("aaa", "AAA", "runX", "beamB");
CHECK(cp.atOptional("aaa").value() == "AAA");
CHECK(cp.atOptional("abc").has_value() == false);
CHECK(cp.atOptional("abc").value_or("bla") == "bla");
}
TEST_CASE("test_at_optional_activity")
{
Activity activity;
activity.mBeamType = "pp";
activity.mType = "PHYSICS";
CustomParameters cp;
cp.set("aaa", "AAA");
cp.set("bbb", "BBB");
cp.set("aaa", "asdf", "PHYSICS");
cp.set("aaa", "CCC", "PHYSICS", "pp");
cp.set("aaa", "DDD", "PHYSICS", "PbPb");
cp.set("aaa", "AAA", "TECHNICAL", "pp");
CHECK(cp.atOptional("aaa", activity).value() == "CCC");
CHECK(cp.atOptional("abc", activity).has_value() == false);
CHECK(cp.atOptional("abc", activity).value_or("bla") == "bla");
Activity activity2;
activity.mBeamType = "PbPb";
activity.mType = "PHYSICS";
CHECK(cp.atOptional("aaa", activity).value() == "DDD");
}
TEST_CASE("test_cp_new_access_pattern")
{
CustomParameters cp;
cp.set("aaa", "AAA");
cp.set("bbb", "BBB");
cp.set("aaa", "AAA", "runX");
cp.set("aaa", "AAA", "runX", "beamB");
cp.set("ccc", "1");
cp.set("bbb", "BBB", "runX");
cp.set("ccc", "CCC", "runY");
// if we have a default value
std::string param = cp.atOrDefaultValue("myOwnKey", "1");
CHECK(std::stoi(param) == 1);
param = cp.atOrDefaultValue("aaa", "1");
CHECK(param == "AAA");
// if we don't have a default value and only want to do something if there is a value:
if (auto param2 = cp.find("ccc"); param2 != cp.end()) {
CHECK(std::stoi(param2->second) == 1);
}
}
TEST_CASE("test_load_from_ptree")
{
boost::property_tree::ptree jsontree;
std::string configFilePath = std::string(getTestDataDirectory()) + "testWorkflow.json";
boost::property_tree::read_json(configFilePath, jsontree);
string v0 = jsontree.get<string>("qc.tasks.skeletonTask.extendedTaskParameters.default.default.myOwnKey");
boost::property_tree::ptree params = jsontree.get_child("qc.tasks.skeletonTask.extendedTaskParameters");
CustomParameters cp;
cp.populateCustomParameters(params);
CHECK_NOTHROW(cout << cp << endl);
CHECK(cp.at("myOwnKey") == "myOwnValue");
CHECK(cp.at("myOwnKey1", "PHYSICS") == "myOwnValue1b");
CHECK(cp.atOptional("asdf").has_value() == false);
auto value = cp.getOptionalPtree("myOwnKey3");
CHECK(value.has_value() == true);
// Check that it's an array with 1 element
std::size_t arraySize = std::distance(value->begin(), value->end());
CHECK(arraySize == 1);
// Get the first (and only) element of the array
auto firstElement = value->begin()->second;
// Check the top-level properties
CHECK(firstElement.get<string>("name") == "mean_of_histogram");
CHECK(firstElement.get<string>("title") == "Mean trend of the example histogram");
CHECK(firstElement.get<string>("graphAxisLabel") == "Mean X:time");
CHECK(firstElement.get<string>("graphYRange") == "0:10000");
// Check the graphs array
auto graphs = firstElement.get_child("graphs");
std::size_t graphsSize = std::distance(graphs.begin(), graphs.end());
CHECK(graphsSize == 1);
// Check the first graph properties
auto firstGraph = graphs.begin()->second;
CHECK(firstGraph.get<string>("name") == "mean_trend");
CHECK(firstGraph.get<string>("title") == "mean trend");
CHECK(firstGraph.get<string>("varexp") == "example.mean:time");
CHECK(firstGraph.get<string>("selection") == "");
CHECK(firstGraph.get<string>("option") == "*L PLC PMC");
}
TEST_CASE("test_default_if_not_found_at_optional")
{
CustomParameters cp;
// no default values are in the CP, we get an empty result
CHECK(cp.atOptional("key", "PHYSICS", "proton-proton").has_value() == false);
CHECK(cp.atOptional("key", "TECHNICAL", "proton-proton").has_value() == false);
// prepare the CP
cp.set("key", "valueDefaultDefault", "default", "default");
cp.set("key", "valuePhysicsDefault", "PHYSICS", "default");
cp.set("key", "valuePhysicsPbPb", "PHYSICS", "PbPb");
cp.set("key", "valueCosmicsDefault", "COSMICS", "default");
cp.set("key", "valueCosmicsDefault", "default", "pp");
// check the data
CHECK(cp.atOptional("key").value() == "valueDefaultDefault");
CHECK(cp.atOptional("key", "PHYSICS").value() == "valuePhysicsDefault");
CHECK(cp.atOptional("key", "PHYSICS", "PbPb").value() == "valuePhysicsPbPb");
CHECK(cp.atOptional("key", "COSMICS", "default").value() == "valueCosmicsDefault");
CHECK(cp.atOptional("key", "default", "pp").value() == "valueCosmicsDefault");
// check when something is missing
CHECK(cp.atOptional("key", "PHYSICS", "pp").value() == "valuePhysicsDefault"); // key is not defined for pp
CHECK(cp.atOptional("key", "TECHNICAL", "STRANGE").value() == "valueDefaultDefault"); // key is not defined for run nor beam
CHECK(cp.atOptional("key", "TECHNICAL", "pp").value() == "valueCosmicsDefault"); // key is not defined for technical
}
TEST_CASE("test_default_if_not_found_at")
{
CustomParameters cp;
// no default values are in the CP, we get an empty result
CHECK_THROWS_AS(cp.at("key", "PHYSICS", "proton-proton"), std::out_of_range);
CHECK_THROWS_AS(cp.at("key", "TECHNICAL", "proton-proton"), std::out_of_range);
// prepare the CP
cp.set("key", "valueDefaultDefault", "default", "default");
cp.set("key", "valuePhysicsDefault", "PHYSICS", "default");
cp.set("key", "valuePhysicsPbPb", "PHYSICS", "PbPb");
cp.set("key", "valueCosmicsDefault", "COSMICS", "default");
cp.set("key", "valueCosmicsDefault", "default", "pp");
// check the data
CHECK(cp.at("key") == "valueDefaultDefault");
CHECK(cp.at("key", "PHYSICS") == "valuePhysicsDefault");
CHECK(cp.at("key", "PHYSICS", "PbPb") == "valuePhysicsPbPb");
CHECK(cp.at("key", "COSMICS", "default") == "valueCosmicsDefault");
CHECK(cp.at("key", "default", "pp") == "valueCosmicsDefault");
// check when something is missing
CHECK(cp.at("key", "PHYSICS", "pp") == "valuePhysicsDefault"); // key is not defined for pp
CHECK(cp.at("key", "TECHNICAL", "STRANGE") == "valueDefaultDefault"); // key is not defined for run nor beam
CHECK(cp.at("key", "TECHNICAL", "pp") == "valueCosmicsDefault"); // key is not defined for technical
}
TEST_CASE("test_getAllDefaults")
{
CustomParameters cp;
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);
}