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
16 changes: 13 additions & 3 deletions Analysis/Tutorials/include/Analysis/configurableCut.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
#ifndef CONFIGURABLECUT_H
#define CONFIGURABLECUT_H

#include "Framework/Array2D.h"
#include <iosfwd>
#include <Rtypes.h>
#include <TMath.h>

static constexpr double default_matrix[3][3] = {{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}, {3.1, 3.2, 3.3}};

class configurableCut
{
public:
configurableCut(float cut_ = 2., int state_ = 1, bool option_ = true, std::vector<float> bins_ = {0.5, 1.5, 2.5}, std::vector<std::string> labels_ = {"l1", "l2", "l3"})
: cut{cut_}, state{state_}, option{option_}, bins{bins_}, labels{labels_}
configurableCut(float cut_ = 2., int state_ = 1, bool option_ = true,
std::vector<float> bins_ = {0.5, 1.5, 2.5},
std::vector<std::string> labels_ = {"l1", "l2", "l3"},
o2::framework::Array2D<double> cuts_ = {&default_matrix[0][0], 3, 3})
: cut{cut_}, state{state_}, option{option_}, bins{bins_}, labels{labels_}, cuts{cuts_}
{
}

Expand All @@ -40,14 +46,18 @@ class configurableCut
void setLabels(std::vector<std::string> labels_);
std::vector<std::string> getLabels() const;

void setCuts(o2::framework::Array2D<double> cuts_);
o2::framework::Array2D<double> getCuts() const;

private:
float cut;
int state;
bool option;
std::vector<float> bins;
std::vector<std::string> labels;
o2::framework::Array2D<double> cuts;

ClassDef(configurableCut, 4);
ClassDef(configurableCut, 5);
};

std::ostream& operator<<(std::ostream& os, configurableCut const& c);
Expand Down
31 changes: 21 additions & 10 deletions Analysis/Tutorials/src/configurableCut.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include "Analysis/configurableCut.h"
#include <iostream>

std::ostream& operator<<(std::ostream& os, configurableCut const& c)
{
os << "Cut value: " << c.getCut() << "; state: " << c.getState();
return os;
}

bool configurableCut::method(float arg) const
{
return arg > cut;
Expand All @@ -26,12 +32,6 @@ float configurableCut::getCut() const
return cut;
}

std::ostream& operator<<(std::ostream& os, configurableCut const& c)
{
os << "Cut value: " << c.getCut() << "; state: " << c.getState();
return os;
}

void configurableCut::setState(int state_)
{
state = state_;
Expand All @@ -54,19 +54,30 @@ bool configurableCut::getOption() const

void configurableCut::setBins(std::vector<float> bins_)
{
bins = bins_;
};
bins = std::move(bins_);
}

std::vector<float> configurableCut::getBins() const
{
return bins;
};
}

void configurableCut::setLabels(std::vector<std::string> labels_)
{
labels = labels_;
labels = std::move(labels_);
}

std::vector<std::string> configurableCut::getLabels() const
{
return labels;
}

void configurableCut::setCuts(o2::framework::Array2D<double> cuts_)
{
cuts = std::move(cuts_);
}

o2::framework::Array2D<double> configurableCut::getCuts() const
{
return cuts;
}
35 changes: 26 additions & 9 deletions Analysis/Tutorials/src/configurableObjects.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,41 @@ template <typename T>
auto printArray(std::vector<T> const& vec)
{
std::stringstream ss;
ss << "[";
auto count = 0u;
for (auto& entry : vec) {
ss << entry;
if (count < vec.size() - 1) {
ss << ",";
}
++count;
ss << "[" << vec[0];
for (auto i = 1u; i < vec.size(); ++i) {
ss << ", " << vec[i];
}
ss << "]";
return ss.str();
}

template <typename T>
auto printMatrix(Array2D<T> const& m)
{
std::stringstream ss;
ss << "[[" << m(0, 0);
for (auto i = 1u; i < m.cols; ++i) {
ss << "," << m(0, i);
}
for (auto i = 1u; i < m.rows; ++i) {
ss << "], [" << m(i, 0);
for (auto j = 1u; j < m.cols; ++j) {
ss << "," << m(i, j);
}
}
ss << "]]";
return ss.str();
}

static constexpr float defaultm[3][4] = {{1.1, 1.2, 1.3, 1.4}, {2.1, 2.2, 2.3, 2.4}, {3.1, 3.2, 3.3, 3.4}};

struct ConfigurableObjectDemo {
Configurable<configurableCut> cut{"cut", {0.5, 1, true}, "generic cut"};
MutableConfigurable<configurableCut> mutable_cut{"mutable_cut", {1., 2, false}, "generic cut"};

// note that size is fixed by this declaration - externally supplied vector needs to be the same size!
Configurable<std::vector<int>> array{"array", {0, 0, 0, 0, 0, 0, 0}, "generic array"};
Configurable<Array2D<float>> vmatrix{"matrix", {&defaultm[0][0], 3, 4}, "generic matrix"};

void init(InitContext const&){};
void process(aod::Collision const&, aod::Tracks const& tracks)
Expand All @@ -53,7 +69,8 @@ struct ConfigurableObjectDemo {
LOGF(INFO, "Cut1 labels: %s; Cut2 labels: %s", printArray(cut->getLabels()), printArray(mutable_cut->getLabels()));
auto vec = (std::vector<int>)array;
LOGF(INFO, "Array: %s", printArray(vec).c_str());
for (auto& track : tracks) {
LOGF(INFO, "Matrix: %s", printMatrix((Array2D<float>)vmatrix));
for (auto const& track : tracks) {
if (track.globalIndex() % 500 == 0) {
std::string decision1;
std::string decision2;
Expand Down
112 changes: 112 additions & 0 deletions Framework/Core/include/Framework/Array2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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.
#ifndef FRAMEWORK_ARRAY2D_H
#define FRAMEWORK_ARRAY2D_H
#include <cstdint>
#include <vector>

namespace o2::framework
{
// matrix-like wrapper for std::vector
// has no range checks
template <typename T>
struct Array2D {
using element_t = T;

Array2D(T const* data_, uint32_t r, uint32_t c)
: rows{r}, cols{c}
{
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = *(data_ + (i * cols + j));
}
}
}
Array2D(std::vector<T> data_, uint32_t r, uint32_t c)
: rows{r}, cols{c}
{
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = data_[i * cols + j];
}
}
}

Array2D(Array2D<T> const& other)
: rows{other.rows},
cols{other.cols}
{
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = *(other.data + (i * cols + j));
}
}
}

Array2D(Array2D<T>&& other)
: rows{other.rows},
cols{other.cols}
{
data = other.data;
other.data = nullptr;
other.rows = 0;
other.cols = 0;
}

Array2D& operator=(Array2D<T> const& other)
{
this->rows = other.rows;
this->cols = other.cols;
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = *(other.data + (i * cols + j));
}
}
return *this;
}

Array2D& operator=(Array2D<T>&& other)
{
this->rows = other.rows;
this->cols = other.cols;
data = other.data;
other.data = nullptr;
other.rows = 0;
other.cols = 0;
return *this;
}

~Array2D()
{
if (data != nullptr) {
delete[] data;
}
}

T operator()(uint32_t y, uint32_t x) const
{
return data[y * cols + x];
}
T* operator[](uint32_t y) const
{
return data + y * cols;
}

T* data = nullptr;
uint32_t rows = 0;
uint32_t cols = 0;
};
} // namespace o2::framework

#endif // FRAMEWORK_ARRAY2D_H
27 changes: 26 additions & 1 deletion Framework/Core/include/Framework/ConfigParamRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace o2::framework
namespace
{
template <typename T>
std::vector<T> extractVector(boost::property_tree::ptree& tree)
std::vector<T> extractVector(boost::property_tree::ptree const& tree)
{
std::vector<T> result(tree.size());
auto count = 0u;
Expand All @@ -34,6 +34,29 @@ std::vector<T> extractVector(boost::property_tree::ptree& tree)
}
return result;
}

template <typename T>
o2::framework::Array2D<T> extractMatrix(boost::property_tree::ptree const& tree)
{
std::vector<T> cache;
uint32_t nrows = tree.size();
uint32_t ncols = 0;
bool first = true;
auto irow = 0u;
for (auto& row : tree) {
if (first) {
ncols = row.second.size();
first = false;
}
auto icol = 0u;
for (auto& entry : row.second) {
cache.push_back(entry.second.get_value<T>());
++icol;
}
++irow;
}
return o2::framework::Array2D<T>{cache, nrows, ncols};
}
} // namespace

class ConfigParamStore;
Expand Down Expand Up @@ -80,6 +103,8 @@ class ConfigParamRegistry
return std::string_view{mStore->store().get<std::string>(key)};
} else if constexpr (is_base_of_template<std::vector, T>::value) {
return extractVector<typename T::value_type>(mStore->store().get_child(key));
} else if constexpr (is_base_of_template<o2::framework::Array2D, T>::value) {
return extractMatrix<typename T::element_t>(mStore->store().get_child(key));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably be extractArray2D, fine for now.

} else if constexpr (std::is_same_v<T, boost::property_tree::ptree>) {
return mStore->store().get_child(key);
} else if constexpr (std::is_constructible_v<T, boost::property_tree::ptree>) {
Expand Down
7 changes: 5 additions & 2 deletions Framework/Core/include/Framework/ConfigParamsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct ConfigParamsHelper {
/// all options which are found in the vetos are skipped
static bool dpl2BoostOptions(const std::vector<ConfigParamSpec>& spec,
options_description& options,
options_description vetos = options_description());
boost::program_options::options_description vetos = options_description());

/// populate boost program options for a complete workflow
template <typename ContainerType>
Expand Down Expand Up @@ -119,7 +119,10 @@ struct ConfigParamsHelper {
V == VariantType::ArrayFloat ||
V == VariantType::ArrayDouble ||
V == VariantType::ArrayBool ||
V == VariantType::ArrayString) {
V == VariantType::ArrayString ||
V == VariantType::MatrixInt ||
V == VariantType::MatrixFloat ||
V == VariantType::MatrixDouble) {
Comment on lines +123 to +125
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be Array2DInt, etc.

auto value = boost::program_options::value<std::string>();
value = value->default_value(spec.defaultValue.asString());
if constexpr (V != VariantType::String) {
Expand Down
Loading