Skip to content

Commit c1b1a25

Browse files
authored
DPL Analysis: allow to omit LabeledArray labels in JSON (#5671)
1 parent 582b5e4 commit c1b1a25

File tree

7 files changed

+137
-28
lines changed

7 files changed

+137
-28
lines changed

Analysis/Tutorials/src/configurableObjects.cxx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,20 @@ struct ConfigurableObjectDemo {
6363
Configurable<Array2D<float>> vmatrix{"matrix", {&defaultm[0][0], 3, 4}, "generic matrix"};
6464
Configurable<LabeledArray<float>> vla{"vla", {defaultm[0], 3, 4, {"r 1", "r 2", "r 3"}, {"c 1", "c 2", "c 3", "c 4"}}, "labeled array"};
6565

66-
void init(InitContext const&){};
67-
void process(aod::Collision const&, aod::Tracks const& tracks)
66+
void init(InitContext const&)
6867
{
69-
LOGF(INFO, "Cut1: %.3f; Cut2: %.3f", cut, mutable_cut);
7068
LOGF(INFO, "Cut1 bins: %s; Cut2 bins: %s", printArray(cut->getBins()), printArray(mutable_cut->getBins()));
7169
LOGF(INFO, "Cut1 labels: %s; Cut2 labels: %s", printArray(cut->getLabels()), printArray(mutable_cut->getLabels()));
7270
auto vec = (std::vector<int>)array;
7371
LOGF(INFO, "Array: %s", printArray(vec).c_str());
7472
LOGF(INFO, "Matrix: %s", printMatrix((Array2D<float>)vmatrix));
73+
LOGF(INFO, "Labeled:\n %s\n %s\n %s", printArray(vla->getLabelsRows()), printArray(vla->getLabelsCols()), printMatrix(vla->getData()));
74+
};
75+
76+
void process(aod::Collision const&, aod::Tracks const& tracks)
77+
{
78+
LOGF(INFO, "Cut1: %.3f; Cut2: %.3f", cut, mutable_cut);
79+
7580
for (auto const& track : tracks) {
7681
if (track.globalIndex() % 500 == 0) {
7782
std::string decision1;

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ o2_add_library(Framework
9191
src/DataInputDirector.cxx
9292
src/DataOutputDirector.cxx
9393
src/Task.cxx
94+
src/Array2D.cxx
9495
src/Variant.cxx
9596
src/WorkflowCustomizationHelpers.cxx
9697
src/WorkflowHelpers.cxx

Framework/Core/include/Framework/Array2D.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#ifndef FRAMEWORK_ARRAY2D_H
1111
#define FRAMEWORK_ARRAY2D_H
1212

13-
#include <Framework/RuntimeError.h>
14-
1513
#include <cstdint>
1614
#include <vector>
1715
#include <unordered_map>
@@ -160,29 +158,20 @@ struct LabelMap {
160158
std::vector<std::string> labels_rows;
161159
std::vector<std::string> labels_cols;
162160

163-
labelmap_t populate(uint32_t size, std::vector<std::string> labels)
164-
{
165-
labelmap_t map;
166-
if (labels.empty() == false) {
167-
if (labels.size() != size) {
168-
throw runtime_error("labels array size != array dimension");
169-
}
170-
for (auto i = 0u; i < size; ++i) {
171-
map.emplace(labels[i], (uint32_t)i);
172-
}
173-
}
174-
return map;
175-
}
161+
static labelmap_t populate(uint32_t size, std::vector<std::string> labels);
176162

177-
auto getLabelsRows()
163+
auto getLabelsRows() const
178164
{
179165
return labels_rows;
180166
}
181167

182-
auto getLabelsCols()
168+
auto getLabelsCols() const
183169
{
184170
return labels_cols;
185171
}
172+
173+
void replaceLabelsRows(uint32_t size, std::vector<std::string> const& labels);
174+
void replaceLabelsCols(uint32_t size, std::vector<std::string> const& labels);
186175
};
187176

188177
template <typename T>
@@ -257,21 +246,31 @@ class LabeledArray : public LabelMap
257246
return values[y];
258247
}
259248

260-
auto getData()
249+
auto getData() const
261250
{
262251
return values;
263252
}
264253

265-
auto rows()
254+
auto rows() const
266255
{
267256
return values.rows;
268257
}
269258

270-
auto cols()
259+
auto cols() const
271260
{
272261
return values.cols;
273262
}
274263

264+
void replaceLabelsRows(std::vector<std::string> const& labels)
265+
{
266+
LabelMap::replaceLabelsRows(values.rows, labels);
267+
}
268+
269+
void replaceLabelsCols(std::vector<std::string> const& labels)
270+
{
271+
LabelMap::replaceLabelsCols(values.cols, labels);
272+
}
273+
275274
private:
276275
Array2D<T> values;
277276
};

Framework/Core/include/Framework/VariantPropertyTreeHelpers.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ auto array2DFromBranch(boost::property_tree::ptree const& ptree)
129129
return basicArray2DFromBranch<T>(ptree.get_child("values"));
130130
}
131131

132+
std::pair<std::vector<std::string>, std::vector<std::string>> extractLabels(boost::property_tree::ptree const& tree);
133+
132134
template <typename T>
133135
auto labeledArrayFromBranch(boost::property_tree::ptree const& tree)
134136
{
135-
auto labels_rows = basicVectorFromBranch<std::string>(tree.get_child(labels_rows_str));
136-
auto labels_cols = basicVectorFromBranch<std::string>(tree.get_child(labels_cols_str));
137+
auto [labels_rows, labels_cols] = extractLabels(tree);
137138
auto values = basicArray2DFromBranch<T>(tree.get_child("values"));
138139

139140
return LabeledArray<T>{values, labels_rows, labels_cols};

Framework/Core/src/Array2D.cxx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#include <Framework/Array2D.h>
12+
#include <Framework/RuntimeError.h>
13+
#include <Framework/CompilerBuiltins.h>
14+
15+
namespace o2::framework
16+
{
17+
labelmap_t LabelMap::populate(uint32_t size, std::vector<std::string> labels)
18+
{
19+
labelmap_t map;
20+
if (labels.empty() == false) {
21+
if (labels.size() != size) {
22+
throw runtime_error("labels array size != array dimension");
23+
}
24+
for (auto i = 0u; i < size; ++i) {
25+
map.emplace(labels[i], (uint32_t)i);
26+
}
27+
}
28+
return map;
29+
}
30+
31+
void LabelMap::replaceLabelsRows(uint32_t size, std::vector<std::string> const& labels)
32+
{
33+
if (O2_BUILTIN_UNLIKELY(size != labels.size())) {
34+
throw runtime_error_f("Row labels array has different size (%d) than number of rows (%d)", labels.size(), size);
35+
};
36+
labels_rows = labels;
37+
rowmap.clear();
38+
rowmap = populate(labels.size(), labels);
39+
}
40+
41+
void LabelMap::replaceLabelsCols(uint32_t size, std::vector<std::string> const& labels)
42+
{
43+
if (O2_BUILTIN_UNLIKELY(size != labels.size())) {
44+
throw runtime_error_f("Column labels array has different size (%d) than number of columns (%d)", labels.size(), size);
45+
};
46+
labels_cols = labels;
47+
colmap.clear();
48+
colmap = populate(labels.size(), labels);
49+
}
50+
51+
} // namespace o2::framework

Framework/Core/src/PropertyTreeHelpers.cxx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
172172
}
173173
}
174174

175+
template <typename T>
176+
auto replaceLabels(LabeledArray<T>& input, LabeledArray<T>&& spec)
177+
{
178+
if (!input.getLabelsCols().empty() || !input.getLabelsRows().empty()) {
179+
return false;
180+
}
181+
if (spec.getLabelsCols().empty() == false) {
182+
input.replaceLabelsCols(spec.getLabelsCols());
183+
}
184+
if (spec.getLabelsRows().empty() == false) {
185+
input.replaceLabelsRows(spec.getLabelsRows());
186+
}
187+
return true;
188+
}
189+
175190
void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
176191
boost::property_tree::ptree& pt,
177192
boost::property_tree::ptree const& in,
@@ -216,11 +231,32 @@ void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
216231
case VariantType::Array2DInt:
217232
case VariantType::Array2DFloat:
218233
case VariantType::Array2DDouble:
219-
case VariantType::LabeledArrayInt:
220-
case VariantType::LabeledArrayFloat:
221-
case VariantType::LabeledArrayDouble:
222234
pt.put_child(key, *it);
223235
break;
236+
case VariantType::LabeledArrayInt: {
237+
auto v = labeledArrayFromBranch<int>(it.value());
238+
if (!replaceLabels(v, spec.defaultValue.get<LabeledArray<int>>())) {
239+
pt.put_child(key, *it);
240+
} else {
241+
pt.put_child(key, labeledArrayToBranch(std::move(v)));
242+
}
243+
}; break;
244+
case VariantType::LabeledArrayFloat: {
245+
auto v = labeledArrayFromBranch<float>(it.value());
246+
if (!replaceLabels(v, spec.defaultValue.get<LabeledArray<float>>())) {
247+
pt.put_child(key, *it);
248+
} else {
249+
pt.put_child(key, labeledArrayToBranch(std::move(v)));
250+
}
251+
}; break;
252+
case VariantType::LabeledArrayDouble: {
253+
auto v = labeledArrayFromBranch<double>(it.value());
254+
if (!replaceLabels(v, spec.defaultValue.get<LabeledArray<double>>())) {
255+
pt.put_child(key, *it);
256+
} else {
257+
pt.put_child(key, labeledArrayToBranch(std::move(v)));
258+
}
259+
}; break;
224260
case VariantType::Unknown:
225261
case VariantType::Empty:
226262
default:

Framework/Core/src/Variant.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// granted to it by virtue of its status as an Intergovernmental Organization
99
// or submit itself to any jurisdiction.
1010
#include "Framework/Variant.h"
11+
#include "Framework/VariantPropertyTreeHelpers.h"
1112
#include <iostream>
1213
#include <sstream>
1314

@@ -255,4 +256,19 @@ Variant& Variant::operator=(Variant&& other)
255256
}
256257
}
257258

259+
std::pair<std::vector<std::string>, std::vector<std::string>> extractLabels(boost::property_tree::ptree const& tree)
260+
{
261+
std::vector<std::string> labels_rows;
262+
std::vector<std::string> labels_cols;
263+
auto lrc = tree.get_child_optional(labels_rows_str);
264+
if (lrc) {
265+
labels_rows = basicVectorFromBranch<std::string>(lrc.value());
266+
}
267+
auto lcc = tree.get_child_optional(labels_cols_str);
268+
if (lcc) {
269+
labels_cols = basicVectorFromBranch<std::string>(lcc.value());
270+
}
271+
return std::make_pair(labels_rows, labels_cols);
272+
}
273+
258274
} // namespace o2::framework

0 commit comments

Comments
 (0)