Skip to content

Commit 2d711f4

Browse files
authored
DPL Analysis: add string array Configurable (#5063)
1 parent 00d2046 commit 2d711f4

11 files changed

Lines changed: 107 additions & 8 deletions

File tree

Analysis/Tutorials/include/Analysis/configurableCut.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
class configurableCut
1919
{
2020
public:
21-
configurableCut(float cut_ = 2., int state_ = 1, bool option_ = true, std::vector<float> bins_ = {0.5, 1.5, 2.5})
22-
: cut{cut_}, state{state_}, option{option_}, bins{bins_}
21+
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"})
22+
: cut{cut_}, state{state_}, option{option_}, bins{bins_}, labels{labels_}
2323
{
2424
}
2525

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

40+
void setLabels(std::vector<std::string> labels_);
41+
std::vector<std::string> getLabels() const;
42+
4043
private:
4144
float cut;
4245
int state;
4346
bool option;
4447
std::vector<float> bins;
48+
std::vector<std::string> labels;
4549

4650
ClassDef(configurableCut, 4);
4751
};

Analysis/Tutorials/src/configurableCut.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,13 @@ std::vector<float> configurableCut::getBins() const
6060
{
6161
return bins;
6262
};
63+
64+
void configurableCut::setLabels(std::vector<std::string> labels_)
65+
{
66+
labels = labels_;
67+
}
68+
69+
std::vector<std::string> configurableCut::getLabels() const
70+
{
71+
return labels;
72+
}

Analysis/Tutorials/src/configurableObjects.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct ConfigurableObjectDemo {
5050
{
5151
LOGF(INFO, "Cut1: %.3f; Cut2: %.3f", cut, mutable_cut);
5252
LOGF(INFO, "Cut1 bins: %s; Cut2 bins: %s", printArray(cut->getBins()), printArray(mutable_cut->getBins()));
53+
LOGF(INFO, "Cut1 labels: %s; Cut2 labels: %s", printArray(cut->getLabels()), printArray(mutable_cut->getLabels()));
5354
auto vec = (std::vector<int>)array;
5455
LOGF(INFO, "Array: %s", printArray(vec).c_str());
5556
for (auto& track : tracks) {

Framework/Core/include/Framework/ConfigParamsHelper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ struct ConfigParamsHelper {
118118
} else if constexpr (V == VariantType::ArrayInt ||
119119
V == VariantType::ArrayFloat ||
120120
V == VariantType::ArrayDouble ||
121-
V == VariantType::ArrayBool) {
121+
V == VariantType::ArrayBool ||
122+
V == VariantType::ArrayString) {
122123
auto value = boost::program_options::value<std::string>();
123124
value = value->default_value(spec.defaultValue.asString());
124125
if constexpr (V != VariantType::String) {

Framework/Core/include/Framework/Variant.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum class VariantType : int { Int = 0,
3636
ArrayFloat,
3737
ArrayDouble,
3838
ArrayBool,
39+
ArrayString,
3940
Empty,
4041
Unknown };
4142

@@ -66,11 +67,13 @@ DECLARE_VARIANT_TRAIT(int*, ArrayInt);
6667
DECLARE_VARIANT_TRAIT(float*, ArrayFloat);
6768
DECLARE_VARIANT_TRAIT(double*, ArrayDouble);
6869
DECLARE_VARIANT_TRAIT(bool*, ArrayBool);
70+
DECLARE_VARIANT_TRAIT(std::string*, ArrayString);
6971

7072
DECLARE_VARIANT_TRAIT(std::vector<int>, ArrayInt);
7173
DECLARE_VARIANT_TRAIT(std::vector<float>, ArrayFloat);
7274
DECLARE_VARIANT_TRAIT(std::vector<double>, ArrayDouble);
7375
DECLARE_VARIANT_TRAIT(std::vector<bool>, ArrayBool);
76+
DECLARE_VARIANT_TRAIT(std::vector<std::string>, ArrayString);
7477

7578
template <typename T>
7679
struct variant_array_symbol {
@@ -97,6 +100,11 @@ struct variant_array_symbol<bool> {
97100
constexpr static char symbol = 'b';
98101
};
99102

103+
template <>
104+
struct variant_array_symbol<std::string> {
105+
constexpr static char symbol = 's';
106+
};
107+
100108
template <typename T>
101109
inline constexpr VariantType variant_trait_v = variant_trait<T>::value;
102110

@@ -121,6 +129,7 @@ DECLARE_VARIANT_TYPE(int*, ArrayInt);
121129
DECLARE_VARIANT_TYPE(float*, ArrayFloat);
122130
DECLARE_VARIANT_TYPE(double*, ArrayDouble);
123131
DECLARE_VARIANT_TYPE(bool*, ArrayBool);
132+
DECLARE_VARIANT_TYPE(std::string*, ArrayString);
124133

125134
template <typename S, typename T>
126135
struct variant_helper {
@@ -217,6 +226,10 @@ class Variant
217226
mSize = other.mSize;
218227
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
219228
return;
229+
case variant_trait_v<std::string*>:
230+
mSize = other.mSize;
231+
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
232+
return;
220233
default:
221234
mStore = other.mStore;
222235
mSize = other.mSize;
@@ -243,6 +256,8 @@ class Variant
243256
case variant_trait_v<bool*>:
244257
*reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
245258
return;
259+
case variant_trait_v<std::string*>:
260+
*reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
246261
default:
247262
return;
248263
}
@@ -258,6 +273,7 @@ class Variant
258273
case variant_trait_v<float*>:
259274
case variant_trait_v<double*>:
260275
case variant_trait_v<bool*>:
276+
case variant_trait_v<std::string*>:
261277
if (reinterpret_cast<void**>(&mStore) != nullptr) {
262278
free(*reinterpret_cast<void**>(&mStore));
263279
}
@@ -290,6 +306,10 @@ class Variant
290306
mSize = other.mSize;
291307
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
292308
return;
309+
case variant_trait_v<std::string*>:
310+
mSize = other.mSize;
311+
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
312+
return;
293313
default:
294314
mStore = other.mStore;
295315
mSize = other.mSize;

Framework/Core/src/BoostOptionsRetriever.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ namespace o2::framework
3030
BoostOptionsRetriever::BoostOptionsRetriever(bool ignoreUnknown,
3131
int argc, char** argv)
3232
: mDescription{std::make_unique<boost::program_options::options_description>("ALICE O2 Framework - Available options")},
33-
mIgnoreUnknown{ignoreUnknown},
3433
mArgc{argc},
35-
mArgv{argv}
34+
mArgv{argv},
35+
mIgnoreUnknown{ignoreUnknown}
3636
{
3737
}
3838

@@ -68,7 +68,8 @@ void BoostOptionsRetriever::update(std::vector<ConfigParamSpec> const& specs,
6868
case VariantType::ArrayFloat:
6969
case VariantType::ArrayDouble:
7070
case VariantType::ArrayBool:
71-
options = options(name, bpo::value<std::string>()->default_value(spec.defaultValue.asString(), help));
71+
case VariantType::ArrayString:
72+
options = options(name, bpo::value<std::string>()->default_value(spec.defaultValue.asString()), help);
7273
break;
7374
case VariantType::Unknown:
7475
case VariantType::Empty:

Framework/Core/src/ConfigParamsHelper.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ void ConfigParamsHelper::populateBoostProgramOptions(
6969
case VariantType::ArrayBool:
7070
addConfigSpecOption<VariantType::ArrayBool>(spec, options);
7171
break;
72+
case VariantType::ArrayString:
73+
addConfigSpecOption<VariantType::ArrayString>(spec, options);
74+
break;
7275
case VariantType::Unknown:
7376
case VariantType::Empty:
7477
break;

Framework/Core/src/PropertyTreeHelpers.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ void PropertyTreeHelpers::populateDefaults(std::vector<ConfigParamSpec> const& s
7474
case VariantType::ArrayBool:
7575
addBranch(key, spec.defaultValue.get<bool*>(), spec.defaultValue.size());
7676
break;
77+
case VariantType::ArrayString:
78+
addBranch(key, spec.defaultValue.get<std::string*>(), spec.defaultValue.size());
79+
break;
7780
case VariantType::Unknown:
7881
case VariantType::Empty:
7982
default:
@@ -106,6 +109,21 @@ std::vector<T> toVector(std::string const& input)
106109
}
107110
return result;
108111
}
112+
113+
template <>
114+
std::vector<std::string> toVector(std::string const& input)
115+
{
116+
std::vector<std::string> result;
117+
//check if the array string has correct array type symbol
118+
assert(input[0] == variant_array_symbol<std::string>::symbol);
119+
std::regex smatch(R"((?:(?!=,)|(?!=\[))\w+(?=,|\]))");
120+
auto end = std::sregex_iterator();
121+
auto values = std::sregex_iterator(input.begin(), input.end(), smatch);
122+
for (auto v = values; v != end; ++v) {
123+
result.push_back(v->str());
124+
}
125+
return result;
126+
}
109127
} // namespace
110128

111129
void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
@@ -171,6 +189,11 @@ void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
171189
addBranch(key, v);
172190
};
173191
break;
192+
case VariantType::ArrayString: {
193+
auto v = toVector<std::string>(vmap[key].as<std::string>());
194+
addBranch(key, v);
195+
};
196+
break;
174197
case VariantType::Unknown:
175198
case VariantType::Empty:
176199
default:
@@ -224,6 +247,7 @@ void PropertyTreeHelpers::populate(std::vector<ConfigParamSpec> const& schema,
224247
case VariantType::ArrayFloat:
225248
case VariantType::ArrayDouble:
226249
case VariantType::ArrayBool:
250+
case VariantType::ArrayString:
227251
pt.put_child(key, *it);
228252
break;
229253
case VariantType::Unknown:

Framework/Core/src/RootConfigParamHelpers.cxx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ void ptreeToMember(boost::property_tree::ptree const& value,
115115
*static_cast<std::vector<double>*>(ptr) = extractVector<double>(value);
116116
return;
117117
case compile_time_hash("vector<bool>"):
118+
throw std::runtime_error("Bool arrays are not implemented yet");
119+
case compile_time_hash("vector<std::string>"):
120+
case compile_time_hash("vector<string>"):
121+
*static_cast<std::vector<std::string>*>(ptr) = extractVector<std::string>(value);
122+
return;
118123
default:
119-
throw std::runtime_error("Not and int/float/double/bool vector");
124+
throw std::runtime_error("Not an int/float/double/bool vector");
120125
}
121126
}
122127
auto dt = dm->GetDataType();
@@ -205,8 +210,11 @@ ConfigParamSpec memberToConfigParamSpec(const char* tname, TDataMember* dm, void
205210
case compile_time_hash("vector<bool>"):
206211
throw std::runtime_error("bool vector not supported yet");
207212
// return ConfigParamSpec{tname, VariantType::ArrayBool, *static_cast<std::vector<bool>*>(ptr), {"No help"}};
213+
case compile_time_hash("vector<std::string>"):
214+
case compile_time_hash("vector<string>"):
215+
return ConfigParamSpec{tname, VariantType::ArrayString, *static_cast<std::vector<std::string>*>(ptr), {"No help"}};
208216
default:
209-
throw std::runtime_error("Not and int/float/double/bool vector");
217+
throw std::runtime_error("Not an int/float/double/bool vector");
210218
}
211219
}
212220
auto dt = dm->GetDataType();

Framework/Core/src/Variant.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ std::ostream& operator<<(std::ostream& oss, Variant const& val)
6363
case VariantType::ArrayBool:
6464
printArray<bool>(oss, val.get<bool*>(), val.size());
6565
break;
66+
case VariantType::ArrayString:
67+
printArray<std::string>(oss, val.get<std::string*>(), val.size());
68+
break;
6669
case VariantType::Empty:
6770
break;
6871
default:

0 commit comments

Comments
 (0)