Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPL Analysis: add string array Configurable #5063

Merged
merged 3 commits into from
Dec 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions Analysis/Tutorials/include/Analysis/configurableCut.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
class configurableCut
{
public:
configurableCut(float cut_ = 2., int state_ = 1, bool option_ = true, std::vector<float> bins_ = {0.5, 1.5, 2.5})
: cut{cut_}, state{state_}, option{option_}, bins{bins_}
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_}
{
}

Expand All @@ -37,11 +37,15 @@ class configurableCut
void setBins(std::vector<float> bins_);
std::vector<float> getBins() const;

void setLabels(std::vector<std::string> labels_);
std::vector<std::string> getLabels() const;

private:
float cut;
int state;
bool option;
std::vector<float> bins;
std::vector<std::string> labels;

ClassDef(configurableCut, 4);
};
Expand Down
10 changes: 10 additions & 0 deletions Analysis/Tutorials/src/configurableCut.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ std::vector<float> configurableCut::getBins() const
{
return bins;
};

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

std::vector<std::string> configurableCut::getLabels() const
{
return labels;
}
1 change: 1 addition & 0 deletions Analysis/Tutorials/src/configurableObjects.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ConfigurableObjectDemo {
{
LOGF(INFO, "Cut1: %.3f; Cut2: %.3f", cut, mutable_cut);
LOGF(INFO, "Cut1 bins: %s; Cut2 bins: %s", printArray(cut->getBins()), printArray(mutable_cut->getBins()));
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) {
Expand Down
3 changes: 2 additions & 1 deletion Framework/Core/include/Framework/ConfigParamsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ struct ConfigParamsHelper {
} else if constexpr (V == VariantType::ArrayInt ||
V == VariantType::ArrayFloat ||
V == VariantType::ArrayDouble ||
V == VariantType::ArrayBool) {
V == VariantType::ArrayBool ||
V == VariantType::ArrayString) {
auto value = boost::program_options::value<std::string>();
value = value->default_value(spec.defaultValue.asString());
if constexpr (V != VariantType::String) {
Expand Down
20 changes: 20 additions & 0 deletions Framework/Core/include/Framework/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum class VariantType : int { Int = 0,
ArrayFloat,
ArrayDouble,
ArrayBool,
ArrayString,
Empty,
Unknown };

Expand Down Expand Up @@ -66,11 +67,13 @@ DECLARE_VARIANT_TRAIT(int*, ArrayInt);
DECLARE_VARIANT_TRAIT(float*, ArrayFloat);
DECLARE_VARIANT_TRAIT(double*, ArrayDouble);
DECLARE_VARIANT_TRAIT(bool*, ArrayBool);
DECLARE_VARIANT_TRAIT(std::string*, ArrayString);

DECLARE_VARIANT_TRAIT(std::vector<int>, ArrayInt);
DECLARE_VARIANT_TRAIT(std::vector<float>, ArrayFloat);
DECLARE_VARIANT_TRAIT(std::vector<double>, ArrayDouble);
DECLARE_VARIANT_TRAIT(std::vector<bool>, ArrayBool);
DECLARE_VARIANT_TRAIT(std::vector<std::string>, ArrayString);

template <typename T>
struct variant_array_symbol {
Expand All @@ -97,6 +100,11 @@ struct variant_array_symbol<bool> {
constexpr static char symbol = 'b';
};

template <>
struct variant_array_symbol<std::string> {
constexpr static char symbol = 's';
};

template <typename T>
inline constexpr VariantType variant_trait_v = variant_trait<T>::value;

Expand All @@ -121,6 +129,7 @@ DECLARE_VARIANT_TYPE(int*, ArrayInt);
DECLARE_VARIANT_TYPE(float*, ArrayFloat);
DECLARE_VARIANT_TYPE(double*, ArrayDouble);
DECLARE_VARIANT_TYPE(bool*, ArrayBool);
DECLARE_VARIANT_TYPE(std::string*, ArrayString);

template <typename S, typename T>
struct variant_helper {
Expand Down Expand Up @@ -217,6 +226,10 @@ class Variant
mSize = other.mSize;
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
return;
case variant_trait_v<std::string*>:
mSize = other.mSize;
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
return;
default:
mStore = other.mStore;
mSize = other.mSize;
Expand All @@ -243,6 +256,8 @@ class Variant
case variant_trait_v<bool*>:
*reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
return;
case variant_trait_v<std::string*>:
*reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
default:
return;
}
Expand All @@ -258,6 +273,7 @@ class Variant
case variant_trait_v<float*>:
case variant_trait_v<double*>:
case variant_trait_v<bool*>:
case variant_trait_v<std::string*>:
if (reinterpret_cast<void**>(&mStore) != nullptr) {
free(*reinterpret_cast<void**>(&mStore));
}
Expand Down Expand Up @@ -290,6 +306,10 @@ class Variant
mSize = other.mSize;
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
return;
case variant_trait_v<std::string*>:
mSize = other.mSize;
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
return;
default:
mStore = other.mStore;
mSize = other.mSize;
Expand Down
7 changes: 4 additions & 3 deletions Framework/Core/src/BoostOptionsRetriever.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace o2::framework
BoostOptionsRetriever::BoostOptionsRetriever(bool ignoreUnknown,
int argc, char** argv)
: mDescription{std::make_unique<boost::program_options::options_description>("ALICE O2 Framework - Available options")},
mIgnoreUnknown{ignoreUnknown},
mArgc{argc},
mArgv{argv}
mArgv{argv},
mIgnoreUnknown{ignoreUnknown}
{
}

Expand Down Expand Up @@ -68,7 +68,8 @@ void BoostOptionsRetriever::update(std::vector<ConfigParamSpec> const& specs,
case VariantType::ArrayFloat:
case VariantType::ArrayDouble:
case VariantType::ArrayBool:
options = options(name, bpo::value<std::string>()->default_value(spec.defaultValue.asString(), help));
case VariantType::ArrayString:
options = options(name, bpo::value<std::string>()->default_value(spec.defaultValue.asString()), help);
break;
case VariantType::Unknown:
case VariantType::Empty:
Expand Down
3 changes: 3 additions & 0 deletions Framework/Core/src/ConfigParamsHelper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ void ConfigParamsHelper::populateBoostProgramOptions(
case VariantType::ArrayBool:
addConfigSpecOption<VariantType::ArrayBool>(spec, options);
break;
case VariantType::ArrayString:
addConfigSpecOption<VariantType::ArrayString>(spec, options);
break;
case VariantType::Unknown:
case VariantType::Empty:
break;
Expand Down
24 changes: 24 additions & 0 deletions Framework/Core/src/PropertyTreeHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ void PropertyTreeHelpers::populateDefaults(std::vector<ConfigParamSpec> const& s
case VariantType::ArrayBool:
addBranch(key, spec.defaultValue.get<bool*>(), spec.defaultValue.size());
break;
case VariantType::ArrayString:
addBranch(key, spec.defaultValue.get<std::string*>(), spec.defaultValue.size());
break;
case VariantType::Unknown:
case VariantType::Empty:
default:
Expand Down Expand Up @@ -106,6 +109,21 @@ std::vector<T> toVector(std::string const& input)
}
return result;
}

template <>
std::vector<std::string> toVector(std::string const& input)
{
std::vector<std::string> result;
//check if the array string has correct array type symbol
assert(input[0] == variant_array_symbol<std::string>::symbol);
std::regex smatch(R"((?:(?!=,)|(?!=\[))\w+(?=,|\]))");
auto end = std::sregex_iterator();
auto values = std::sregex_iterator(input.begin(), input.end(), smatch);
for (auto v = values; v != end; ++v) {
result.push_back(v->str());
}
return result;
}
} // namespace

void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
Expand Down Expand Up @@ -171,6 +189,11 @@ void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
addBranch(key, v);
};
break;
case VariantType::ArrayString: {
auto v = toVector<std::string>(vmap[key].as<std::string>());
addBranch(key, v);
};
break;
case VariantType::Unknown:
case VariantType::Empty:
default:
Expand Down Expand Up @@ -224,6 +247,7 @@ void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
case VariantType::ArrayFloat:
case VariantType::ArrayDouble:
case VariantType::ArrayBool:
case VariantType::ArrayString:
pt.put_child(key, *it);
break;
case VariantType::Unknown:
Expand Down
12 changes: 10 additions & 2 deletions Framework/Core/src/RootConfigParamHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ void ptreeToMember(boost::property_tree::ptree const& value,
*static_cast<std::vector<double>*>(ptr) = extractVector<double>(value);
return;
case compile_time_hash("vector<bool>"):
throw std::runtime_error("Bool arrays are not implemented yet");
case compile_time_hash("vector<std::string>"):
case compile_time_hash("vector<string>"):
*static_cast<std::vector<std::string>*>(ptr) = extractVector<std::string>(value);
return;
default:
throw std::runtime_error("Not and int/float/double/bool vector");
throw std::runtime_error("Not an int/float/double/bool vector");
}
}
auto dt = dm->GetDataType();
Expand Down Expand Up @@ -205,8 +210,11 @@ ConfigParamSpec memberToConfigParamSpec(const char* tname, TDataMember* dm, void
case compile_time_hash("vector<bool>"):
throw std::runtime_error("bool vector not supported yet");
// return ConfigParamSpec{tname, VariantType::ArrayBool, *static_cast<std::vector<bool>*>(ptr), {"No help"}};
case compile_time_hash("vector<std::string>"):
case compile_time_hash("vector<string>"):
return ConfigParamSpec{tname, VariantType::ArrayString, *static_cast<std::vector<std::string>*>(ptr), {"No help"}};
default:
throw std::runtime_error("Not and int/float/double/bool vector");
throw std::runtime_error("Not an int/float/double/bool vector");
}
}
auto dt = dm->GetDataType();
Expand Down
3 changes: 3 additions & 0 deletions Framework/Core/src/Variant.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ std::ostream& operator<<(std::ostream& oss, Variant const& val)
case VariantType::ArrayBool:
printArray<bool>(oss, val.get<bool*>(), val.size());
break;
case VariantType::ArrayString:
printArray<std::string>(oss, val.get<std::string*>(), val.size());
break;
case VariantType::Empty:
break;
default:
Expand Down
24 changes: 24 additions & 0 deletions Framework/Core/test/test_Variants.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,28 @@ BOOST_AUTO_TEST_CASE(VariantTest)
for (auto i = 0u; i < fd.size(); ++i) {
BOOST_CHECK(farr[i] == (fd.get<float*>())[i]);
}

std::vector<std::string> vstrings{"s1", "s2", "s3"};
std::string strings[] = {"l1", "l2", "l3"};
Variant vstr(strings, 3);
Variant vvstr(vstrings);

BOOST_CHECK(vstr.size() == 3);
BOOST_CHECK(vvstr.size() == 3);
for (auto i = 0u; i < vstr.size(); ++i) {
BOOST_CHECK(strings[i] == (vstr.get<std::string*>())[i]);
}
for (auto i = 0u; i < vvstr.size(); ++i) {
BOOST_CHECK(vstrings[i] == (vvstr.get<std::string*>())[i]);
}

Variant vsc(vstr); // Copy constructor
Variant vsm(std::move(vstr)); // Move constructor
Variant vscc = vsm; // Copy assignment
for (auto i = 0u; i < vsm.size(); ++i) {
BOOST_CHECK(strings[i] == (vsm.get<std::string*>())[i]);
}
for (auto i = 0u; i < vscc.size(); ++i) {
BOOST_CHECK(strings[i] == (vscc.get<std::string*>())[i]);
}
}